-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
144 lines (121 loc) · 5.35 KB
/
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
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
import tensorflow.keras.regularizers as regularizers
from tensorflow.python.keras.layers import Activation, Conv2D, BatchNormalization
from tensorflow.python.keras.layers import MaxPooling2D, Dropout, Flatten, Dense
from tensorflow.python.keras.layers import Conv2DTranspose
from tensorflow.python.keras.models import Sequential
from tensorflow.keras.applications import ResNet50V2
from tensorflow.keras.applications import ResNet50
import tensorflow as tf
def get_model(param):
if param["dataset"] == "CIFAR10":
return _get_model_cifar()
if param["dataset"] == "GTSRB":
return _get_model_GTSRB()
if param["dataset"] == "MNIST":
return _get_model_MNIST()
if param["dataset"] == "ImageNet16":
return _get_model_ImageNet16()
if param["dataset"] == "PubFig":
return _get_model_PubFig()
if param["dataset"] == "GTSRB-new":
return _get_model_GTSRB_new()
return None
def _get_model_cifar():
weight_decay = 1e-6
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay),
input_shape=(32, 32, 3)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Conv2D(32, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))
# print("model.summary: \n{}".format(model.summary()))
return model
def _get_model_GTSRB():
# model = ResNet50V2(input_shape=(32, 32, 3), weights=None, classes=43)
# basemodel.trainable = True
# inputs = tf.keras.Input(shape=(32, 32, 3))
# x = basemodel(inputs)
# x = Flatten()(x)
# x = tf.keras.layers.Dense(256, activation="relu")(x)
# outputs = tf.keras.layers.Dense(43, activation="softmax")(x)
# model = tf.keras.Model(inputs, outputs)
# print("model.summary: \n{}".format(model.summary()))
# return model
weight_decay = 1e-6
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay),
input_shape=(32, 32, 3)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Conv2D(32, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Conv2D(64, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(Conv2D(128, (3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Activation('elu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(43, activation='softmax'))
# print("model.summary: \n{}".format(model.summary()))
return model
def _get_model_MNIST():
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(32, 32, 1), activation='elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(15, (3, 3), activation='elu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(150, activation='elu'))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
# print("model.summary: \n{}".format(model.summary()))
return model
def _get_model_ImageNet16():
model = ResNet50V2(input_shape=(224, 224, 3), weights=None, classes=16)
# print("model.summary: \n{}".format(model.summary()))
return model
def _get_model_PubFig():
model = ResNet50V2(input_shape=(224, 224, 3), weights=None, classes=16)
# print("model.summary: \n{}".format(model.summary()))
return model
def _get_model_GTSRB_new():
model = ResNet50V2(input_shape=(224,224,3), weights=None, classes=13)
print("model.summary: \n{}".format(model.summary()))
return model