-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModelLayers.py
310 lines (219 loc) · 14.7 KB
/
ModelLayers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- coding: utf-8 -*-
"""
Created on Tues Nov 15 2022
@author: Simon Bilik
This class returns convolutional layers later used in the ModelSaved.py script. Feel free to define any new layers if necessary.
"""
import logging
import numpy as np
from keras.layers import Input, Conv2D, Conv2DTranspose, MaxPooling2D, UpSampling2D, BatchNormalization, LeakyReLU, Normalization
## Class with the saved models
class ModelLayers():
## Set the constants and paths
def __init__(self, layerSel, imageDim):
# Global parameters
self.layerSel = layerSel
# Image dimensions
self.imHeight = imageDim[0]
self.imWidth = imageDim[1]
self.imChannel = imageDim[2]
## Get encoder convolutional layers
def getEncoder(self):
input_img = Input(shape = (self.imHeight, self.imWidth, self.imChannel), name='input_layer')
# Structure from https://keras.io/examples/generative/vae/
if self.layerSel == 'ConvM1':
# Block-1
netEnc = Conv2D(32, kernel_size=3, strides= 2, padding='same', name='conv_E1')(input_img)
netEnc = BatchNormalization(name='bn_E1')(netEnc)
netEnc = LeakyReLU(name='lrelu_E1')(netEnc)
# Block-2
netEnc = Conv2D(64, kernel_size=3, strides= 2, padding='same', name='conv_E2')(netEnc)
netEnc = BatchNormalization(name='bn_E2')(netEnc)
netEnc = LeakyReLU(name='lrelu_E2')(netEnc)
# Block-3
netEnc = Conv2D(64, 3, 2, padding='same', name='conv_E3')(netEnc)
netEnc = BatchNormalization(name='bn_E3')(netEnc)
netEnc = LeakyReLU(name='lrelu_E3')(netEnc)
# Block-4
netEnc = Conv2D(64, 3, 2, padding='same', name='conv_E4')(netEnc)
netEnc = BatchNormalization(name='bn_E4')(netEnc)
netEnc = LeakyReLU(name='lrelu_E4')(netEnc)
# Block-5
netEnc = Conv2D(64, 3, 2, padding='same', name='conv_E5')(netEnc)
netEnc = BatchNormalization(name='bn_E5')(netEnc)
netEnc = LeakyReLU(name='out_E')(netEnc)
## Encoding reduction parameters
redEncHeight = np.int32(self.imHeight / 32)
redEncWidth = np.int32(self.imWidth / 32)
filterCount = np.int32(64)
# Structure from MVT
elif self.layerSel == 'ConvM2':
netEnc = Conv2D(32, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_E1')(input_img)
netEnc = BatchNormalization(name='bn_E1')(netEnc)
netEnc = Conv2D(32, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_E2')(netEnc)
netEnc = BatchNormalization(name='bn_E2')(netEnc)
netEnc = Conv2D(32, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_E3')(netEnc)
netEnc = BatchNormalization(name='bn_E3')(netEnc)
netEnc = Conv2D(64, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_E4')(netEnc)
netEnc = BatchNormalization(name='bn_E4')(netEnc)
netEnc = Conv2D(64, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_E5')(netEnc)
netEnc = BatchNormalization(name='bn_E5')(netEnc)
netEnc = Conv2D(128, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_E6')(netEnc)
netEnc = BatchNormalization(name='bn_E6')(netEnc)
netEnc = Conv2D(64, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_E7')(netEnc)
netEnc = BatchNormalization(name='bn_E7')(netEnc)
netEnc = Conv2D(32, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_E8')(netEnc)
netEnc = BatchNormalization(name='bn_E8')(netEnc)
netEnc = Conv2D(1, (8, 8), strides=1, padding='same', name='out_E')(netEnc)
netEnc = BatchNormalization(name='bn_E9')(netEnc)
## Encoding reduction parameters
redEncHeight = np.int32(self.imHeight / 16)
redEncWidth = np.int32(self.imWidth / 16)
filterCount = np.int32(16)
# Basic structure 1
elif self.layerSel == 'ConvM3':
netEnc = Conv2D(32, 3, activation="sigmoid", strides=2, padding="same", name='conv_E1')(input_img)
netEnc = BatchNormalization(name='bn_E1')(netEnc)
netEnc = Conv2D(64, 3, activation="sigmoid", strides=2, padding="same", name='out_E')(netEnc)
netEnc = BatchNormalization(name='bn_E2')(netEnc)
## Encoding reduction parameters
redEncHeight = np.int32(self.imHeight / 4)
redEncWidth = np.int32(self.imWidth / 4)
filterCount = np.int32(64)
# Basic structure 2
elif self.layerSel == 'ConvM4':
netEnc = Conv2D(8, (5, 5), activation='sigmoid', padding='same', name='conv_E1')(input_img)
netEnc = MaxPooling2D((2, 2), padding='same', name='mpool_E1')(netEnc)
netEnc = BatchNormalization(name='bn_E1')(netEnc)
netEnc = Conv2D(4, (3, 3), activation='sigmoid', padding='same', name='conv_E2')(netEnc)
netEnc = MaxPooling2D((2, 2), padding='same', name='out_E')(netEnc)
netEnc = BatchNormalization(name='bn_E2')(netEnc)
## Encoding reduction parameters
redEncHeight = np.int32(self.imHeight / 4)
redEncWidth = np.int32(self.imWidth / 4)
filterCount = np.int32(4)
# Basic structure 3 from https://blog.keras.io/building-autoencoders-in-keras.html
elif self.layerSel == 'ConvM5':
netEnc = Conv2D(16, (3, 3), activation='sigmoid', padding='same', name='conv_E1')(input_img)
netEnc = MaxPooling2D((2, 2), padding='same', name='mpool_E1')(netEnc)
netEnc = BatchNormalization(name='bn_E1')(netEnc)
netEnc = Conv2D(8, (3, 3), activation='sigmoid', padding='same', name='conv_E2')(netEnc)
netEnc = MaxPooling2D((2, 2), padding='same', name='mpool_E2')(netEnc)
netEnc = BatchNormalization(name='bn_E2')(netEnc)
netEnc = Conv2D(4, (3, 3), activation='sigmoid', padding='same', name='conv_E3')(netEnc)
netEnc = MaxPooling2D((2, 2), padding='same', name='out_E')(netEnc)
netEnc = BatchNormalization(name='bn_E3')(netEnc)
## Encoding reduction parameters
redEncHeight = np.int32(self.imHeight / 8)
redEncWidth = np.int32(self.imWidth / 8)
filterCount = np.int32(4)
# Asymetric encoder and decoder (ConvM5 vs ConvM4)
elif self.layerSel == 'ConvM6':
netEnc = Conv2D(16, (3, 3), activation='sigmoid', padding='same', name='conv_E1')(input_img)
netEnc = MaxPooling2D((2, 2), padding='same', name='mpool_E1')(netEnc)
netEnc = BatchNormalization(name='bn_E1')(netEnc)
netEnc = Conv2D(8, (3, 3), activation='sigmoid', padding='same', name='conv_E2')(netEnc)
netEnc = MaxPooling2D((2, 2), padding='same', name='mpool_E2')(netEnc)
netEnc = BatchNormalization(name='bn_E2')(netEnc)
netEnc = Conv2D(4, (3, 3), activation='sigmoid', padding='same', name='conv_E3')(netEnc)
netEnc = MaxPooling2D((2, 2), padding='same', name='out_E')(netEnc)
netEnc = BatchNormalization(name='bn_E3')(netEnc)
## Encoding reduction parameters
redEncHeight = np.int32(self.imHeight / 8)
redEncWidth = np.int32(self.imWidth / 8)
filterCount = np.int32(4)
# TODO: Define and add other models in the same way
else:
logging.error('Unknown convolutional net name: ' + self.layerSel)
raise ValueError('Unknown convolutional net name: ' + self.layerSel)
return
return netEnc, input_img, redEncHeight, redEncWidth, filterCount
## Get decoder convolutional layers
def getDecoder(self, inputNet, filterCount):
# Structure from https://keras.io/examples/generative/vae/
if self.layerSel == 'ConvM1':
# Block-1
netDec = Conv2DTranspose(filterCount, 3, strides= 2, padding='same', name='convT_D1')(inputNet)
netDec = BatchNormalization(name='bn_D1')(netDec)
netDec = LeakyReLU(name='lrelu_D1')(netDec)
# Block-2
netDec = Conv2DTranspose(64, 3, strides= 2, padding='same', name='convT_D2')(netDec)
netDec = BatchNormalization(name='bn_D2')(netDec)
netDec = LeakyReLU(name='lrelu_D2')(netDec)
# Block-3
netDec = Conv2DTranspose(64, 3, 2, padding='same', name='convT_D3')(netDec)
netDec = BatchNormalization(name='bn_D3')(netDec)
netDec = LeakyReLU(name='lrelu_D3')(netDec)
# Block-4
netDec = Conv2DTranspose(32, 3, 2, padding='same', name='convT_D4')(netDec)
netDec = BatchNormalization(name='bn_D4')(netDec)
netDec = LeakyReLU(name='lrelu_D4')(netDec)
# Block-5
output_img = Conv2DTranspose(self.imChannel, 3, 2,padding='same', activation='sigmoid', name='convT_D5')(netDec)
# Structure from MVT
elif self.layerSel == 'ConvM2':
netDec = Conv2D(filterCount, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_D1')(inputNet)
netDec = Conv2D(64, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_D2')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D1')(netDec)
netDec = BatchNormalization(name='bn_D1')(netDec)
netDec = Conv2D(128, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_D3')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D2')(netDec)
netDec = BatchNormalization(name='bn_D2')(netDec)
netDec = Conv2D(64, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_D4')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D3')(netDec)
netDec = BatchNormalization(name='bn_D3')(netDec)
netDec = Conv2D(64, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_D5')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D4')(netDec)
netDec = BatchNormalization(name='bn_D4')(netDec)
netDec = Conv2D(32, (3, 3), strides=1, activation='sigmoid', padding='same', name='conv_D6')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D5')(netDec)
netDec = BatchNormalization(name='bn_D5')(netDec)
netDec = Conv2D(32, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_D7')(netDec)
netDec = UpSampling2D((4, 4), name='upsmp_D6')(netDec)
netDec = BatchNormalization(name='bn_D6')(netDec)
netDec = Conv2D(32, (4, 4), strides=2, activation='sigmoid', padding='same', name='conv_D8')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D7')(netDec)
netDec = BatchNormalization(name='bn_D7')(netDec)
output_img = Conv2D(self.imChannel, (8, 8), activation='sigmoid', padding='same', name='conv_D9')(netDec)
# Basic structure 1
elif self.layerSel == 'ConvM3':
netDec = Conv2DTranspose(filterCount, (3, 3), activation="sigmoid", strides = 2, padding="same", name='convT_D1')(inputNet)
netDec = Conv2DTranspose(32, (3, 3), activation="sigmoid", strides = 2, padding="same", name='convT_D2')(netDec)
netDec = BatchNormalization(name='bn_D1')(netDec)
output_img = Conv2DTranspose(self.imChannel, (3, 3), activation="sigmoid", padding="same", name='convT_D3')(netDec)
# Basic structure 2
elif self.layerSel == 'ConvM4':
netDec = Conv2D(filterCount, (3, 3), activation='sigmoid', padding='same', name='conv_D1')(inputNet)
netDec = UpSampling2D((2, 2), name='upsmp_D1')(netDec)
netDec = BatchNormalization(name='bn_D1')(netDec)
netDec = Conv2D(8, (5, 5), activation='sigmoid', padding='same', name='conv_D2')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D2')(netDec)
netDec = BatchNormalization(name='bn_D2')(netDec)
output_img = Conv2D(self.imChannel, (3, 3), activation='sigmoid', padding='same', name='conv_D3')(netDec)
# Basic structure 3
elif self.layerSel == 'ConvM5':
netDec = Conv2D(filterCount, (3, 3), activation='sigmoid', padding='same', name='conv_D1')(inputNet)
netDec = UpSampling2D((2, 2), name='upsmp_D1')(netDec)
netDec = BatchNormalization(name='bn_D1')(netDec)
netDec = Conv2D(8, (3, 3), activation='sigmoid', padding='same', name='conv_D2')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D2')(netDec)
netDec = BatchNormalization(name='bn_D2')(netDec)
netDec = Conv2D(16, (3, 3), activation='sigmoid', padding='same', name='conv_D3')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D3')(netDec)
netDec = BatchNormalization(name='bn_D3')(netDec)
output_img = Conv2D(self.imChannel, (3, 3), activation='sigmoid', padding='same', name='conv_D4')(netDec)
# Asymetric encoder and decoder (ConvM5 vs ConvM4)
elif self.layerSel == 'ConvM6':
netDec = Conv2D(filterCount, (3, 3), activation='sigmoid', padding='same', name='conv_D1')(inputNet)
netDec = UpSampling2D((4, 4), name='upsmp_D1')(netDec)
netDec = BatchNormalization(name='bn_D1')(netDec)
netDec = Conv2D(8, (5, 5), activation='sigmoid', padding='same', name='conv_D2')(netDec)
netDec = UpSampling2D((2, 2), name='upsmp_D2')(netDec)
netDec = BatchNormalization(name='bn_D2')(netDec)
output_img = Conv2D(self.imChannel, (3, 3), activation='sigmoid', padding='same', name='conv_D3')(netDec)
# TODO: Define and add other models in the same way
else:
logging.error('Unknown convolutional net name: ' + self.layerSel)
raise ValueError('Unknown convolutional net name: ' + self.layerSel)
return
return output_img