-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_keras_model.py
85 lines (69 loc) · 3.68 KB
/
my_keras_model.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
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.models import Model
from keras.layers import Dense, Flatten, Conv2D
from keras.regularizers import l2
from kerascv.model_provider import get_model as kecv_get_model
from opconty_shufflenetv2 import ShuffleNetV2
def get_mobilenet_v2(weight_decay=0.0005, input_shape=(64, 64, 3)):
keras_model = MobileNetV2(input_shape=input_shape, include_top=False, weights=None)
# keras_model.summary()
# Total params: 2,257,984
# Trainable params: 2,223,872
# Non-trainable params: 34,112
for layer in keras_model.layers:
layer.trainable = True
net_layer_len = len(keras_model.layers)
flatten = Flatten()(keras_model.layers[net_layer_len - 1].output)
predictions_g = Dense(2, activation='softmax', kernel_regularizer=l2(weight_decay), name='output_gender')(flatten)
predictions_a = Dense(101, activation='softmax', kernel_regularizer=l2(weight_decay), name='output_age')(flatten)
predictions_e = Dense(7, activation='softmax', kernel_regularizer=l2(weight_decay), name='output_emotion')(flatten)
model = Model(inputs=keras_model.layers[0].input, outputs=[predictions_g, predictions_a, predictions_e])
# model.summary()
# Total params: 2,821,294
# Trainable params: 2,787,182
# Non-trainable params: 34,112
return model
def get_shufflenet_v2(weight_decay=0.0005):
kecv_model = kecv_get_model("shufflenetv2_w1", pretrained=False)
kecv_model.layers.pop()
for layer in kecv_model.layers:
layer.trainable = True
net_layer_len = len(kecv_model.layers)
flatten = kecv_model.layers[net_layer_len - 1].output
predictions_g = Dense(2, activation='softmax', kernel_regularizer=l2(weight_decay), name='output_gender')(
flatten)
predictions_a = Dense(101, activation='softmax', kernel_regularizer=l2(weight_decay), name='output_age')(
flatten)
predictions_e = Dense(7, activation='softmax', kernel_regularizer=l2(weight_decay), name='output_emotion')(
flatten)
return Model(inputs=kecv_model.layers[0].input, outputs=[predictions_g, predictions_a, predictions_e])
def get_opconty_shufflenet_v2(weight_decay=0.0005):
opconty_model = ShuffleNetV2(include_top=False, pooling='avg', input_shape=(64, 64, 3))
# opconty_model.summary()
# Total params: 4,018,740
# Trainable params: 3,990,620
# Non-trainable params: 28,120
for layer in opconty_model.layers:
layer.trainable = True
l2_regularizer = l2(weight_decay) if weight_decay else None
for layer in opconty_model.layers:
if isinstance(layer, Conv2D) or isinstance(layer, Dense):
layer.kernel_regularizer = l2_regularizer
# for layer in opconty_model.layers:
# if hasattr(layer, 'kernel_regularizer'):
# print('layer({}): kernel_regularizer = {}, l2 = {}'.format(layer.name, layer.kernel_regularizer,
# layer.kernel_regularizer.l2))
net_layer_len = len(opconty_model.layers)
flatten = opconty_model.layers[net_layer_len - 1].output
predictions_g = Dense(2, activation='softmax', kernel_regularizer=l2_regularizer, name='output_gender')(
flatten)
predictions_a = Dense(101, activation='softmax', kernel_regularizer=l2_regularizer, name='output_age')(
flatten)
predictions_e = Dense(7, activation='softmax', kernel_regularizer=l2_regularizer, name='output_emotion')(
flatten)
model = Model(inputs=opconty_model.layers[0].input, outputs=[predictions_g, predictions_a, predictions_e])
# model.summary()
# Total params: 4,131,490
# Trainable params: 4,103,370
# Non-trainable params: 28,120
return model