networks.py
Go to the documentation of this file.
1 import sys
2 sys.path.append("/home/salonsom/cvn_tensorflow/networks")
3 
4 from keras.applications.xception import Xception
5 from keras.applications.vgg16 import VGG16
6 from keras.applications.vgg19 import VGG19
7 from keras.applications.resnet50 import ResNet50
8 from keras.applications.inception_v3 import InceptionV3
9 from keras.applications.inception_resnet_v2 import InceptionResNetV2
10 from keras.applications.mobilenet import MobileNet
11 from keras.applications.densenet import DenseNet121
12 from keras.applications.densenet import DenseNet169
13 from keras.applications.densenet import DenseNet201
14 import my_model, inception_v4, se_resnet, se_resnet_saul, se_inception_v3, se_inception_resnet_v2, se_resnext, resnet, resnext
15 from keras.layers import GlobalAveragePooling2D, Dense, Dropout,Activation,Flatten
16 from keras.layers import Input
17 from keras.models import Model
18 
19 '''
20 ################
21 ### Xception ###
22 ################
23 '''
24 
25 def _xception(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
26 
27  print('Architecture: Xception...')
28 
29  model = Xception(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
30 
31  return model
32 
33 '''
34 ################
35 #### VGG16 #####
36 ################
37 '''
38 
39 def _vgg16(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
40 
41  print('Architecture: VGG-16...')
42 
43  model = VGG16(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
44 
45  return model
46 
47 '''
48 ################
49 #### VGG19 #####
50 ################
51 '''
52 
53 def _vgg19(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
54 
55  print('Architecture: VGG-19...')
56 
57  model = VGG19(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
58 
59  return model
60 
61 '''
62 ################
63 ### ResNet18 ###
64 ################
65 '''
66 
67 def _resnet18(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
68 
69  print('Architecture: ResNet-18...')
70 
71  model = resnet.ResnetBuilder.build_resnet_18(input_shape=input_shape, num_outputs=num_classes)
72 
73  return model
74 
75 '''
76 ################
77 ### ResNet34 ###
78 ################
79 '''
80 
81 def _resnet34(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
82 
83  print('Architecture: ResNet-34...')
84 
85  model = resnet.ResnetBuilder.build_resnet_34(input_shape=input_shape, num_outputs=num_classes)
86 
87  return model
88 
89 '''
90 ################
91 ### ResNet50 ###
92 ################
93 '''
94 
95 def _resnet50(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
96 
97  print('Architecture: ResNet-50...')
98 
99  # Ordinary model
100 
101  if None == transfer_learning:
102 
103  model = ResNet50(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
104 
105  # Transfer learning model
106 
107  else:
108 
109  pre_model = ResNet50(weights='imagenet', include_top=False)
110 
111  last_layer = pre_model.output
112  x = Flatten()(last_layer)
113  x = Dense(num_classes, activation='softmax', name='fc1000')(x)
114 
115  model = Model(inputs=pre_model.input, outputs=x)
116 
117  if transfer_learning == 'finetuning':
118  for layer in model.layers[:-4]:
119  layer.trainable = False
120 
121  return model
122 
123 '''
124 ################
125 ## ResNet101 ###
126 ################
127 '''
128 
129 def _resnet101(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
130 
131  print('Architecture: ResNet-101...')
132 
133  model = resnet.ResnetBuilder.build_resnet_101(input_shape=input_shape, num_outputs=num_classes)
134 
135  return model
136 
137 '''
138 ################
139 ## ResNet152 ###
140 ################
141 '''
142 
143 def _resnet152(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
144 
145  print('Architecture: ResNet-152...')
146 
147  model = resnet.ResnetBuilder.build_resnet_152(input_shape=input_shape, num_outputs=num_classes)
148 
149  return model
150 
151 '''
152 ################
153 # InceptionV3 ##
154 ################
155 '''
156 
157 def _inceptionv3(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
158 
159  print('Architecture: Inception-v3...')
160 
161  model = InceptionV3(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
162 
163  return model
164 
165 '''
166 ################
167 # InceptionV4 ##
168 ################
169 '''
170 
171 def _inceptionv4(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
172 
173  print('Architecture: Inception-v4...')
174 
175  model = inception_v4.create_model(include_top=True, input_shape=input_shape, weights=None, num_classes=num_classes)
176 
177  return model
178 
179 '''
180 #####################
181 # InceptionResNetV2 #
182 #####################
183 '''
184 
185 def _inceptionresnetv2(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
186 
187  print('Architecture: Inception-ResNet-v2...')
188 
189  model = InceptionResNetV2(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
190 
191  return model
192 
193 '''
194 ################
195 ### ResNeXt ####
196 ################
197 '''
198 
199 def _resnext(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
200 
201  print('Architecture: ResNeXt...')
202 
203  model = resnext.ResNext(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
204 
205  return model
206 
207 '''
208 ################
209 ## SEResNet18 ##
210 ################
211 '''
212 
213 def _seresnet18(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
214 
215  print('Architecture: SE-ResNet-18...')
216 
217  model = se_resnet.SEResNet18(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
218 
219  return model
220 
221 '''
222 ################
223 ## SEResNet34 ##
224 ################
225 '''
226 
227 def _seresnet34(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
228 
229  print('Architecture: SE-ResNet-34...')
230 
231  model = se_resnet.SEResNet34(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
232 
233  return model
234 
235 '''
236 ################
237 ## SEResNet50 ##
238 ################
239 '''
240 
241 def _seresnet50(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
242 
243  print('Architecture: SE-ResNet-50...')
244 
245  model = se_resnet.SEResNet50(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
246 
247  return model
248 
249 '''
250 ################
251 # SEResNet101 ##
252 ################
253 '''
254 
255 def _seresnet101(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
256 
257  print('Architecture: SE-ResNet-101...')
258 
259  model = se_resnet.SEResNet101(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
260 
261  return model
262 
263 '''
264 ################
265 # SEResNet154 ##
266 ################
267 '''
268 
269 def _seresnet154(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
270 
271  print('Architecture: SE-ResNet-154...')
272 
273  model = se_resnet.SEResNet154(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
274 
275  return model
276 
277 '''
278 ###################
279 ## SEResNet Saul ##
280 ###################
281 '''
282 
283 def _seresnetsaul(num_classes=13, input_shape=[500, 500, 1], transfer_learning=None):
284 
285  print('Architecture: SE-ResNet-34-Saul...')
286 
287  model = se_resnet_saul.SEResNet34(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
288 
289  return model
290 
291 
292 '''
293 #################
294 # SEInceptionV3 #
295 #################
296 '''
297 
298 def _seinceptionv3(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
299 
300  print('Architecture: SE-Inception-v3...')
301 
302  model = se_inception_v3.SEInceptionV3(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
303 
304  return model
305 
306 '''
307 #######################
308 # SEInceptionResNetV2 #
309 #######################
310 '''
311 
312 def _seinceptionresnetv2(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
313 
314  print('Architecture: SE-Inception-ResNet-v2...')
315 
316  model = se_inception_resnet_v2.SEInceptionResNetV2(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
317 
318  return model
319 
320 '''
321 ################
322 ## SEResNeXt ###
323 ################
324 '''
325 
326 def _seresnext(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
327 
328  print('Architecture: SE-ResNeXt...')
329 
330  model = se_resnext.SEResNext(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
331 
332  return model
333 
334 '''
335 ################
336 ## MobileNet ###
337 ################
338 '''
339 
340 def _mobilenet(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
341 
342  print('Architecture: MobileNet...')
343 
344  model = MobileNet(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
345 
346  return model
347 
348 '''
349 ################
350 # DenseNet121 ##
351 ################
352 '''
353 
354 def _densenet121(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
355 
356  print('Architecture: DenseNet-121...')
357 
358  model = DenseNet121(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
359 
360  return model
361 
362 '''
363 ################
364 # DenseNet169 ##
365 ################
366 '''
367 
368 def _densenet169(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
369 
370  print('Architecture: DenseNet-169...')
371 
372  model = DenseNet169(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
373 
374  return model
375 
376 '''
377 ################
378 # DenseNet201 ##
379 ################
380 '''
381 
382 def _densenet201(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
383 
384  print('Architecture: DenseNet-201...')
385 
386  model = DenseNet201(include_top=True, input_shape=input_shape, weights=None, classes=num_classes)
387 
388  return model
389 
390 '''
391 ################
392 # Custom Model #
393 ################
394 '''
395 
396 def _mymodel(num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
397 
398  print('Architecture: Custom model...')
399 
400  model = my_model.my_model(input_shape=input_shape, classes=num_classes)
401 
402  return model
403 
404 
405 '''
406 ################
407 # Create Model #
408 ################
409 '''
410 
411 def create_model(network='resnet50', num_classes=13, input_shape=[500, 500, 3], transfer_learning=None):
412 
413  network = network.lower()
414 
415  if network == 'xception':
416 
417  model = _xception(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
418 
419  elif network == 'vgg16':
420 
421  model = _vgg16(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
422 
423  elif network == 'vgg19':
424 
425  model = _vgg19(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
426 
427  elif network == 'resnet18':
428 
429  model = _resnet18(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
430 
431  elif network == 'resnet34':
432 
433  model = _resnet34(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
434 
435  elif network == 'resnet50':
436 
437  model = _resnet50(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
438 
439  elif network == 'resnet101':
440 
441  model = _resnet101(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
442 
443  elif network == 'resnet152':
444 
445  model = _resnet152(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
446 
447  elif network == 'inceptionv3':
448 
449  model = _inceptionv3(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
450 
451  elif network == 'inceptionv4':
452 
453  model = _inceptionv4(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
454 
455  elif network == 'inceptionresnetv2':
456 
457  model = _inceptionresnetv2(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
458 
459  elif network == 'resnext':
460 
461  model = _resnext(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
462 
463  elif network == 'seresnet18':
464 
465  model = _seresnet18(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
466 
467  elif network == 'seresnet34':
468 
469  model = _seresnet34(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
470 
471  elif network == 'seresnet50':
472 
473  model = _seresnet50(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
474 
475  elif network == 'seresnet101':
476 
477  model = _seresnet101(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
478 
479  elif network == 'seresnet154':
480 
481  model = _seresnet154(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
482 
483  elif network == 'seresnetsaul':
484 
485  model = _seresnetsaul(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
486 
487  elif network == 'seinceptionv3':
488 
489  model = _seinceptionv3(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
490 
491  elif network == 'seinceptionresnetv2':
492 
493  model = _seinceptionresnetv2(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
494 
495  elif network == 'seresnext':
496 
497  model = _seresnext(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
498 
499  elif network == 'mobilenet':
500 
501  model = _mobilenet(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
502 
503  elif network == 'densenet121':
504 
505  model = _densenet121(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
506 
507  elif network == 'densenet169':
508 
509  model = _densenet169(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
510 
511  elif network == 'densenet201':
512 
513  model = _densenet201(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
514 
515  else:
516 
517  model = _mymodel(num_classes=num_classes, input_shape=input_shape, transfer_learning=transfer_learning)
518 
519  return model
def _resnet50(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:95
def _inceptionv3(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:157
def _vgg16(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:39
def _densenet201(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:382
def _seresnet101(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:255
def SEResNet18(input_shape=None, width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
Definition: se_resnet.py:159
def _resnet152(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:143
def _seinceptionresnetv2(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:312
def SEResNext(input_shape=None, depth=29, cardinality=8, width=64, weight_decay=5e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=10)
Definition: se_resnext.py:48
def _inceptionv4(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:171
def _mymodel(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:396
def _seinceptionv3(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:298
def _inceptionresnetv2(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:185
def _seresnet18(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:213
def SEResNet34(input_shape=None, width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
def _seresnet154(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:269
def _xception(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:25
def create_model(num_classes=1001, dropout_prob=0.2, weights=None, include_top=True, transfer_learning=None, input_shape=None)
def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)
def _seresnext(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:326
def build_resnet_18(input_shape, num_outputs)
Definition: resnet.py:317
def SEResNet101(input_shape=None, width=1, bottleneck=True, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
Definition: se_resnet.py:221
def _densenet121(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:354
def SEResNet34(input_shape=None, width=1, bottleneck=False, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
Definition: se_resnet.py:180
def _seresnetsaul(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:283
def SEResNet50(input_shape=None, width=1, bottleneck=True, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
Definition: se_resnet.py:201
def my_model(input_shape=[500, classes=3)
Definition: my_model.py:6
def build_resnet_152(input_shape, num_outputs)
Definition: resnet.py:333
def _resnet101(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:129
def _densenet169(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:368
def _mobilenet(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:340
def create_model(network='resnet50', num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:411
def build_resnet_34(input_shape, num_outputs)
Definition: resnet.py:321
def SEInceptionResNetV2(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=1000)
def _resnext(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:199
def SEResNet154(input_shape=None, width=1, bottleneck=True, weight_decay=1e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=1000)
Definition: se_resnet.py:242
def _seresnet34(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:227
def _resnet34(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:81
def SEInceptionV3(include_top=True, weights=None, input_tensor=None, input_shape=None, pooling=None, classes=1000)
def _seresnet50(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:241
def _resnet18(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:67
def ResNext(input_shape=None, depth=29, cardinality=8, width=64, weight_decay=5e-4, include_top=True, weights=None, input_tensor=None, pooling=None, classes=10)
Definition: resnext.py:39
def build_resnet_101(input_shape, num_outputs)
Definition: resnet.py:329
def _vgg19(num_classes=13, input_shape=[500, transfer_learning=None)
Definition: networks.py:53