-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
184 lines (164 loc) · 8.03 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
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
from model_zoo.model import BaseModel
import tensorflow as tf
class BasicCNNModel(BaseModel):
def __init__(self, config):
super(BasicCNNModel, self).__init__(config)
self.bn1 = tf.keras.layers.BatchNormalization()
self.conv1 = tf.keras.layers.Conv2D(32, (2, 2), padding='same', activation='relu',
kernel_initializer='random_uniform')
self.pool1 = tf.keras.layers.MaxPool2D(padding='same')
self.dropout1 = tf.keras.layers.Dropout(0.5)
self.conv2 = tf.keras.layers.Conv2D(32, (2, 2), padding='same', activation='relu',
kernel_initializer='random_uniform')
self.pool2 = tf.keras.layers.MaxPool2D(padding='same')
self.dropout2 = tf.keras.layers.Dropout(0.5)
self.flatten1 = tf.keras.layers.Flatten()
self.dense1 = tf.keras.layers.Dense(128, activation='relu', kernel_initializer='random_uniform')
self.dense2 = tf.keras.layers.Dense(10, activation='relu')
self.dense3 = tf.keras.layers.Dense(1)
self.reshape = tf.keras.layers.Reshape([])
def call(self, inputs, training=None, mask=None):
o = self.bn1(inputs)
o = self.conv1(o)
o = self.pool1(o)
o = self.dropout1(o, training=training)
o = self.conv2(o)
o = self.pool2(o)
o = self.dropout2(o, training=training)
o = self.flatten1(o)
o = self.dense1(o)
o = self.dense2(o)
o = self.dense3(o)
o = self.reshape(o)
return o
def optimizer(self):
return tf.train.AdamOptimizer(self.config.get('learning_rate'))
def init(self):
self.compile(optimizer=self.optimizer(),
loss='mse',
metrics=['mse', 'mae', 'mape'])
def infer(self, test_data, batch_size=None):
logits = self.predict(test_data)
return logits
class VGGModel(BaseModel):
def __init__(self, config):
super(VGGModel, self).__init__(config)
self.num_features = 64
# layer1
self.conv11 = tf.keras.layers.Conv2D(filters=self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.conv12 = tf.keras.layers.Conv2D(filters=self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.bn1 = tf.keras.layers.BatchNormalization()
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))
self.drop1 = tf.keras.layers.Dropout(rate=0.5)
# layer2
self.conv21 = tf.keras.layers.Conv2D(filters=2 * self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.conv22 = tf.keras.layers.Conv2D(filters=2 * self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.bn2 = tf.keras.layers.BatchNormalization()
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))
self.drop2 = tf.keras.layers.Dropout(rate=0.5)
# layer3
self.conv31 = tf.keras.layers.Conv2D(filters=2 * 2 * self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.conv32 = tf.keras.layers.Conv2D(filters=2 * 2 * self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.conv33 = tf.keras.layers.Conv2D(filters=2 * 2 * self.num_features, kernel_size=(3, 3), activation='relu',
padding='same')
self.bn3 = tf.keras.layers.BatchNormalization()
self.pool3 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))
self.drop3 = tf.keras.layers.Dropout(rate=0.5)
# layer4
self.conv41 = tf.keras.layers.Conv2D(filters=2 * 2 * 2 * self.num_features, kernel_size=(3, 3),
activation='relu',
padding='same')
self.conv42 = tf.keras.layers.Conv2D(filters=2 * 2 * 2 * self.num_features, kernel_size=(3, 3),
activation='relu',
padding='same')
self.conv43 = tf.keras.layers.Conv2D(filters=2 * 2 * 2 * self.num_features, kernel_size=(3, 3),
activation='relu',
padding='same')
self.bn4 = tf.keras.layers.BatchNormalization()
self.pool4 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))
self.drop4 = tf.keras.layers.Dropout(rate=0.5)
# layer5
self.conv51 = tf.keras.layers.Conv2D(filters=2 * 2 * 2 * self.num_features, kernel_size=(3, 3),
activation='relu',
padding='same')
self.conv52 = tf.keras.layers.Conv2D(filters=2 * 2 * 2 * self.num_features, kernel_size=(3, 3),
activation='relu',
padding='same')
self.conv53 = tf.keras.layers.Conv2D(filters=2 * 2 * 2 * self.num_features, kernel_size=(3, 3),
activation='relu',
padding='same')
self.bn5 = tf.keras.layers.BatchNormalization()
self.pool5 = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))
self.drop5 = tf.keras.layers.Dropout(rate=0.5)
# flatten
self.flatten = tf.keras.layers.Flatten()
# dense
self.dense1 = tf.keras.layers.Dense(2 * 2 * 2 * self.num_features, activation='relu')
self.drop5 = tf.keras.layers.Dropout(0.5)
self.dense2 = tf.keras.layers.Dense(2 * 2 * self.num_features, activation='relu')
self.drop6 = tf.keras.layers.Dropout(0.5)
self.dense3 = tf.keras.layers.Dense(2 * self.num_features, activation='relu')
self.drop7 = tf.keras.layers.Dropout(0.5)
self.dense4 = tf.keras.layers.Dense(1)
self.reshape = tf.keras.layers.Reshape([])
def call(self, inputs, training=None, mask=None):
# layer1
x = self.conv11(inputs)
x = self.conv12(x)
x = self.bn1(x, training=training)
x = self.pool1(x)
x = self.drop1(x, training=training)
# layer2
x = self.conv21(x)
x = self.conv22(x)
x = self.bn2(x, training=training)
x = self.pool2(x)
x = self.drop2(x, training=training)
# # layer3
x = self.conv31(x)
x = self.conv32(x)
x = self.conv33(x)
x = self.bn3(x, training=training)
x = self.pool3(x)
x = self.drop3(x, training=training)
# # layer4
x = self.conv41(x)
x = self.conv42(x)
x = self.conv43(x)
x = self.bn4(x, training=training)
x = self.pool4(x)
x = self.drop4(x, training=training)
# layer5
x = self.conv51(x)
x = self.conv52(x)
x = self.conv53(x)
x = self.bn5(x, training=training)
x = self.pool5(x)
x = self.drop5(x, training=training)
# flatten
x = self.flatten(x)
# dense
x = self.dense1(x)
x = self.drop5(x, training=training)
x = self.dense2(x)
x = self.drop6(x, training=training)
x = self.dense3(x)
x = self.drop7(x, training=training)
x = self.dense4(x)
x = self.reshape(x)
return x
def optimizer(self):
return tf.train.AdamOptimizer(self.config.get('learning_rate'))
def init(self):
self.compile(optimizer=self.optimizer(),
loss='mse',
metrics=['mse', 'mae', 'mape'])
def infer(self, test_data, batch_size=None):
logits = self.predict(test_data)
return logits