-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_factory.py
More file actions
775 lines (670 loc) · 31.9 KB
/
model_factory.py
File metadata and controls
775 lines (670 loc) · 31.9 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import tensorflow as tf
import numpy as np
from keras import backend as K
import os
import sys
import keras
from keras.models import model_from_json
import six
from keras.models import Model
from keras.layers import Input
from keras.layers import Activation
from keras.layers import Reshape
from keras.layers import Dense
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import GlobalMaxPooling2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import Dropout
from keras.layers.merge import add
from keras.layers.normalization import BatchNormalization
from keras.regularizers import l2
from keras_applications.imagenet_utils import _obtain_input_shape
from keras.applications.mobilenet_v2 import MobileNetV2
from efficientnet.keras import EfficientNetB0
"""temporary"""
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
"""---------"""
# In[ ]:
def get_le5(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
keep=0.5 #dropout
nFeatures1=32
nFeatures2=64
nFeatures3=128
nFeatures4=256
nNeuronsfc=1024
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(height,width,1),
filters=nFeatures1,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures2,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(nNeuronsfc, activation=tf.nn.relu),
tf.keras.layers.Dropout(rate=(keep)),
tf.keras.layers.Dense(nClass, activation=activationFunc)
])
return model
# In[ ]:
def get_le7(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
keep=0.5 #dropout
nFeatures1=32
nFeatures2=64
nFeatures3=128
nFeatures4=256
nNeuronsfc=1024
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(height,width,1),
filters=nFeatures1,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures2,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures3,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(nNeuronsfc, activation=tf.nn.relu),
tf.keras.layers.Dropout(rate=(keep)),
tf.keras.layers.Dense(nClass, activation=activationFunc)
])
return model
# In[ ]:
def get_le7n(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
batch_sz=50
keep=0.5 #dropout
nSteps=30 # start training
nFeatures1=16
nFeatures2=32
nFeatures3=64
nNeuronsfc=1024
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(height,width,1),
filters=nFeatures1,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures2,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures3,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(nNeuronsfc, activation=tf.nn.relu),
tf.keras.layers.Dropout(rate=(keep)),
tf.keras.layers.Dense(nClass, activation=activationFunc)
])
return model
# In[ ]:
def get_le7w(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
batch_sz=50
keep=0.5 #dropout
nSteps=30 # start training
nFeatures1=64
nFeatures2=128
nFeatures3=256
nNeuronsfc=1024
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(height,width,1),
filters=nFeatures1,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures2,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures3,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(nNeuronsfc, activation=tf.nn.relu),
tf.keras.layers.Dropout(rate=(keep)),
tf.keras.layers.Dense(nClass, activation=activationFunc)
])
return model
# In[ ]:
def get_le9(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
batch_sz=50
keep=0.5 #dropout
nSteps=30 # start training
nFeatures1=32
nFeatures2=64
nFeatures3=128
nFeatures4=256
nNeuronsfc=1024
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(input_shape=(height,width,1),
filters=nFeatures1,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures2,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
#kernel_initializer=tf.keras.initializers.RandomNormal(stddev=np.sqrt(2 / (height*width))),
#bias_initializer=tf.keras.initializers.Constant(value=0.1)
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures3,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Conv2D(filters=nFeatures4,
kernel_size=(5,5),
strides=(1, 1),
padding='same',
activation=tf.nn.relu,
use_bias=True,
),
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(nNeuronsfc, activation=tf.nn.relu),
tf.keras.layers.Dropout(rate=(keep)),
tf.keras.layers.Dense(nClass, activation=activationFunc)
])
return model
# In[ ]:
""" Adopt from keras_contrib
"""
def _bn_relu(x, bn_name=None, relu_name=None):
"""Helper to build a BN -> relu block
"""
norm = BatchNormalization(axis=CHANNEL_AXIS, name=bn_name)(x)
return Activation("relu", name=relu_name)(norm)
def _conv_bn_relu(**conv_params):
"""Helper to build a conv -> BN -> relu residual unit activation function.
This is the original ResNet v1 scheme in https://arxiv.org/abs/1512.03385
"""
filters = conv_params["filters"]
kernel_size = conv_params["kernel_size"]
strides = conv_params.setdefault("strides", (1, 1))
dilation_rate = conv_params.setdefault("dilation_rate", (1, 1))
conv_name = conv_params.setdefault("conv_name", None)
bn_name = conv_params.setdefault("bn_name", None)
relu_name = conv_params.setdefault("relu_name", None)
kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal")
padding = conv_params.setdefault("padding", "same")
kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4))
def f(x):
x = Conv2D(filters=filters, kernel_size=kernel_size,
strides=strides, padding=padding,
dilation_rate=dilation_rate,
kernel_initializer=kernel_initializer,
kernel_regularizer=kernel_regularizer,
name=conv_name)(x)
return _bn_relu(x, bn_name=bn_name, relu_name=relu_name)
return f
def _bn_relu_conv(**conv_params):
"""Helper to build a BN -> relu -> conv residual unit with full pre-activation
function. This is the ResNet v2 scheme proposed in
http://arxiv.org/pdf/1603.05027v2.pdf
"""
filters = conv_params["filters"]
kernel_size = conv_params["kernel_size"]
strides = conv_params.setdefault("strides", (1, 1))
dilation_rate = conv_params.setdefault("dilation_rate", (1, 1))
conv_name = conv_params.setdefault("conv_name", None)
bn_name = conv_params.setdefault("bn_name", None)
relu_name = conv_params.setdefault("relu_name", None)
kernel_initializer = conv_params.setdefault("kernel_initializer", "he_normal")
padding = conv_params.setdefault("padding", "same")
kernel_regularizer = conv_params.setdefault("kernel_regularizer", l2(1.e-4))
def f(x):
activation = _bn_relu(x, bn_name=bn_name, relu_name=relu_name)
return Conv2D(filters=filters, kernel_size=kernel_size,
strides=strides, padding=padding,
dilation_rate=dilation_rate,
kernel_initializer=kernel_initializer,
kernel_regularizer=kernel_regularizer,
name=conv_name)(activation)
return f
def _shortcut(input_feature, residual, conv_name_base=None, bn_name_base=None):
"""Adds a shortcut between input and residual block and merges them with "sum"
"""
# Expand channels of shortcut to match residual.
# Stride appropriately to match residual (width, height)
# Should be int if network architecture is correctly configured.
input_shape = K.int_shape(input_feature)
residual_shape = K.int_shape(residual)
stride_width = int(round(input_shape[ROW_AXIS] / residual_shape[ROW_AXIS]))
stride_height = int(round(input_shape[COL_AXIS] / residual_shape[COL_AXIS]))
equal_channels = input_shape[CHANNEL_AXIS] == residual_shape[CHANNEL_AXIS]
shortcut = input_feature
# 1 X 1 conv if shape is different. Else identity.
if stride_width > 1 or stride_height > 1 or not equal_channels:
print('reshaping via a convolution...')
if conv_name_base is not None:
conv_name_base = conv_name_base + '1'
shortcut = Conv2D(filters=residual_shape[CHANNEL_AXIS],
kernel_size=(1, 1),
strides=(stride_width, stride_height),
padding="valid",
kernel_initializer="he_normal",
kernel_regularizer=l2(0.0001),
name=conv_name_base)(input_feature)
if bn_name_base is not None:
bn_name_base = bn_name_base + '1'
shortcut = BatchNormalization(axis=CHANNEL_AXIS,
name=bn_name_base)(shortcut)
return add([shortcut, residual])
def _residual_block(block_function, filters, blocks, stage,
transition_strides=None, transition_dilation_rates=None,
dilation_rates=None, is_first_layer=False, dropout=None,
residual_unit=_bn_relu_conv):
"""Builds a residual block with repeating bottleneck blocks.
stage: integer, current stage label, used for generating layer names
blocks: number of blocks 'a','b'..., current block label, used for generating
layer names
transition_strides: a list of tuples for the strides of each transition
transition_dilation_rates: a list of tuples for the dilation rate of each
transition
"""
if transition_dilation_rates is None:
transition_dilation_rates = [(1, 1)] * blocks
if transition_strides is None:
transition_strides = [(1, 1)] * blocks
if dilation_rates is None:
dilation_rates = [1] * blocks
def f(x):
for i in range(blocks):
is_first_block = is_first_layer and i == 0
x = block_function(filters=filters, stage=stage, block=i,
transition_strides=transition_strides[i],
dilation_rate=dilation_rates[i],
is_first_block_of_first_layer=is_first_block,
dropout=dropout,
residual_unit=residual_unit)(x)
return x
return f
def _block_name_base(stage, block):
"""Get the convolution name base and batch normalization name base defined by
stage and block.
If there are less than 26 blocks they will be labeled 'a', 'b', 'c' to match the
paper and keras and beyond 26 blocks they will simply be numbered.
"""
if block < 26:
block = '%c' % (block + 97) # 97 is the ascii number for lowercase 'a'
else:
block = str(block)
conv_name_base = 'res' + str(stage) + block + '_branch'
bn_name_base = 'bn' + str(stage) + block + '_branch'
return conv_name_base, bn_name_base
def basic_block(filters, stage, block, transition_strides=(1, 1),
dilation_rate=(1, 1), is_first_block_of_first_layer=False, dropout=None,
residual_unit=_bn_relu_conv):
"""Basic 3 X 3 convolution blocks for use on resnets with layers <= 34.
Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf
"""
def f(input_features):
conv_name_base, bn_name_base = _block_name_base(stage, block)
if is_first_block_of_first_layer:
# don't repeat bn->relu since we just did bn->relu->maxpool
x = Conv2D(filters=filters, kernel_size=(3, 3),
strides=transition_strides,
dilation_rate=dilation_rate,
padding="same",
kernel_initializer="he_normal",
kernel_regularizer=l2(1e-4),
name=conv_name_base + '2a')(input_features)
else:
x = residual_unit(filters=filters, kernel_size=(3, 3),
strides=transition_strides,
dilation_rate=dilation_rate,
conv_name_base=conv_name_base + '2a',
bn_name_base=bn_name_base + '2a')(input_features)
if dropout is not None:
x = Dropout(dropout)(x)
x = residual_unit(filters=filters, kernel_size=(3, 3),
conv_name_base=conv_name_base + '2b',
bn_name_base=bn_name_base + '2b')(x)
return _shortcut(input_features, x)
return f
def bottleneck(filters, stage, block, transition_strides=(1, 1),
dilation_rate=(1, 1), is_first_block_of_first_layer=False, dropout=None,
residual_unit=_bn_relu_conv):
"""Bottleneck architecture for > 34 layer resnet.
Follows improved proposed scheme in http://arxiv.org/pdf/1603.05027v2.pdf
Returns:
A final conv layer of filters * 4
"""
def f(input_feature):
conv_name_base, bn_name_base = _block_name_base(stage, block)
if is_first_block_of_first_layer:
# don't repeat bn->relu since we just did bn->relu->maxpool
x = Conv2D(filters=filters, kernel_size=(1, 1),
strides=transition_strides,
dilation_rate=dilation_rate,
padding="same",
kernel_initializer="he_normal",
kernel_regularizer=l2(1e-4),
name=conv_name_base + '2a')(input_feature)
else:
x = residual_unit(filters=filters, kernel_size=(1, 1),
strides=transition_strides,
dilation_rate=dilation_rate,
conv_name_base=conv_name_base + '2a',
bn_name_base=bn_name_base + '2a')(input_feature)
if dropout is not None:
x = Dropout(dropout)(x)
x = residual_unit(filters=filters, kernel_size=(3, 3),
conv_name_base=conv_name_base + '2b',
bn_name_base=bn_name_base + '2b')(x)
if dropout is not None:
x = Dropout(dropout)(x)
x = residual_unit(filters=filters * 4, kernel_size=(1, 1),
conv_name_base=conv_name_base + '2c',
bn_name_base=bn_name_base + '2c')(x)
return _shortcut(input_feature, x)
return f
def _handle_dim_ordering():
global ROW_AXIS
global COL_AXIS
global CHANNEL_AXIS
if K.image_data_format() == 'channels_last':
ROW_AXIS = 1
COL_AXIS = 2
CHANNEL_AXIS = 3
else:
CHANNEL_AXIS = 1
ROW_AXIS = 2
COL_AXIS = 3
def _string_to_function(identifier):
if isinstance(identifier, six.string_types):
res = globals().get(identifier)
if not res:
raise ValueError('Invalid {}'.format(identifier))
return res
return identifier
def ResNet(input_shape=None, classes=10, block='bottleneck', residual_unit='v2',
repetitions=None, initial_filters=64, activation='softmax', include_top=True,
input_tensor=None, dropout=None, transition_dilation_rate=(1, 1),
initial_strides=(2, 2), initial_kernel_size=(7, 7), initial_pooling='max',
final_pooling=None, top='classification'):
"""Builds a custom ResNet like architecture. Defaults to ResNet50 v2.
Args:
input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` dim ordering)
or `(3, 224, 224)` (with `channels_first` dim ordering).
It should have exactly 3 dimensions,
and width and height should be no smaller than 8.
E.g. `(224, 224, 3)` would be one valid value.
classes: The number of outputs at final softmax layer
block: The block function to use. This is either `'basic'` or `'bottleneck'`.
The original paper used `basic` for layers < 50.
repetitions: Number of repetitions of various block units.
At each block unit, the number of filters are doubled and the input size
is halved. Default of None implies the ResNet50v2 values of [3, 4, 6, 3].
residual_unit: the basic residual unit, 'v1' for conv bn relu, 'v2' for bn relu
conv. See [Identity Mappings in
Deep Residual Networks](https://arxiv.org/abs/1603.05027)
for details.
dropout: None for no dropout, otherwise rate of dropout from 0 to 1.
Based on [Wide Residual Networks.(https://arxiv.org/pdf/1605.07146) paper.
transition_dilation_rate: Dilation rate for transition layers. For semantic
segmentation of images use a dilation rate of (2, 2).
initial_strides: Stride of the very first residual unit and MaxPooling2D call,
with default (2, 2), set to (1, 1) for small images like cifar.
initial_kernel_size: kernel size of the very first convolution, (7, 7) for
imagenet and (3, 3) for small image datasets like tiny imagenet and cifar.
See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
initial_pooling: Determine if there will be an initial pooling layer,
'max' for imagenet and None for small image datasets.
See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
final_pooling: Optional pooling mode for feature extraction at the final
model layer when `include_top` is `False`.
- `None` means that the output of the model
will be the 4D tensor output of the
last convolutional layer.
- `avg` means that global average pooling
will be applied to the output of the
last convolutional layer, and thus
the output of the model will be a
2D tensor.
- `max` means that global max pooling will
be applied.
top: Defines final layers to evaluate based on a specific problem type. Options
are 'classification' for ImageNet style problems, 'segmentation' for
problems like the Pascal VOC dataset, and None to exclude these layers
entirely.
Returns:
The keras `Model`.
"""
if activation not in ['softmax', 'sigmoid', None]:
raise ValueError('activation must be one of "softmax", "sigmoid", or None')
if activation == 'sigmoid' and classes != 1:
raise ValueError('sigmoid activation can only be used when classes = 1')
if repetitions is None:
repetitions = [3, 4, 6, 3]
# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
default_size=32,
min_size=8,
data_format=K.image_data_format(),
require_flatten=include_top)
_handle_dim_ordering()
if len(input_shape) != 3:
raise Exception("Input shape should be a tuple (nb_channels, nb_rows, nb_cols)")
if block == 'basic':
block_fn = basic_block
elif block == 'bottleneck':
block_fn = bottleneck
elif isinstance(block, six.string_types):
block_fn = _string_to_function(block)
else:
block_fn = block
if residual_unit == 'v2':
residual_unit = _bn_relu_conv
elif residual_unit == 'v1':
residual_unit = _conv_bn_relu
elif isinstance(residual_unit, six.string_types):
residual_unit = _string_to_function(residual_unit)
else:
residual_unit = residual_unit
# Permute dimension order if necessary
if K.image_data_format() == 'channels_first':
input_shape = (input_shape[1], input_shape[2], input_shape[0])
# Determine proper input shape
input_shape = _obtain_input_shape(input_shape,
default_size=32,
min_size=8,
data_format=K.image_data_format(),
require_flatten=include_top)
img_input = Input(shape=input_shape, tensor=input_tensor)
x = _conv_bn_relu(filters=initial_filters, kernel_size=initial_kernel_size,
strides=initial_strides)(img_input)
if initial_pooling == 'max':
x = MaxPooling2D(pool_size=(3, 3), strides=initial_strides, padding="same")(x)
block = x
filters = initial_filters
for i, r in enumerate(repetitions):
transition_dilation_rates = [transition_dilation_rate] * r
transition_strides = [(1, 1)] * r
if transition_dilation_rate == (1, 1):
transition_strides[0] = (2, 2)
block = _residual_block(block_fn, filters=filters,
stage=i, blocks=r,
is_first_layer=(i == 0),
dropout=dropout,
transition_dilation_rates=transition_dilation_rates,
transition_strides=transition_strides,
residual_unit=residual_unit)(block)
filters *= 2
# Last activation
x = _bn_relu(block)
# Classifier block
if include_top and top is 'classification':
x = GlobalAveragePooling2D()(x)
x = Dense(units=classes, activation=activation,
kernel_initializer="he_normal")(x)
elif include_top and top is 'segmentation':
x = Conv2D(classes, (1, 1), activation='linear', padding='same')(x)
if K.image_data_format() == 'channels_first':
channel, row, col = input_shape
else:
row, col, channel = input_shape
x = Reshape((row * col, classes))(x)
x = Activation(activation)(x)
x = Reshape((row, col, classes))(x)
elif final_pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif final_pooling == 'max':
x = GlobalMaxPooling2D()(x)
model = Model(inputs=img_input, outputs=x)
return model
"""end of keras_contrib code
"""
# In[ ]:
def get_res18(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
batch_sz=50
drop=0.5 #dropout
nSteps=30 # start training
model = ResNet((height, width, 1), nClass,
block= 'basic',
repetitions=[2,2,2,2], residual_unit='v1',
dropout=drop,
activation=activationFunc)
return model
# In[ ]:
def get_res152(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
batch_sz=50
drop=0.5 #dropout
nSteps=30 # start training
model = ResNet((height, width, 1), nClass,
block= 'bottleneck',
repetitions=[3, 8, 36, 3], residual_unit='v1',
dropout=drop,
activation=activationFunc)
return model
def get_mobilenetv2(nClass=1, activationFunc='softmax', height = 192, width = 128):
# Dimensions of image (pixels) - 180x120
model = MobileNetV2(input_shape=(height,width,1),
include_top=True,
weights=None,
pooling='max',
classes=nClass)
# out = Dense(16, activation='softmax')(model.get_layer(model.layers[-1].name).output)
# mod = Model(inputs=model.inputs, outputs=out)
return model
# In[ ]:
def get_efficientnetb0(nClass=1, activationFunc='softmax', height = 192, width = 128):
model = EfficientNetB0(weights=None, input_shape=(height,width,1), classes=nClass)
return model
def GetModel(model_name = "le7", nClass=1, activationFunc='softmax', height = 192, width = 128):
if (model_name == "le5"):
return get_le5(nClass, activationFunc, height, width)
elif (model_name == "le7"):
return get_le7(nClass, activationFunc, height, width)
elif (model_name == "le7n"):
return get_le7n(nClass, activationFunc, height, width)
elif (model_name == "le7w"):
return get_le7w(nClass, activationFunc, height, width)
elif (model_name == "le9"):
return get_le9(nClass, activationFunc, height, width)
elif (model_name == "res18"):
return get_res18(nClass, activationFunc, height, width)
elif (model_name == "res152"):
return get_res152(nClass, activationFunc, height, width)
elif (model_name == "mobilenetv2"):
return get_mobilenetv2(nClass, activationFunc, height, width)
elif (model_name == "efficientb0"):
return get_efficientnetb0(nClass, activationFunc, height, width)
else:
return None