-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
209 lines (188 loc) · 8.14 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
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
import time
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Resizing
from tensorflow.keras.layers import Normalization
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import Bidirectional
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import GRU
from tensorflow.keras.regularizers import l2
class resnet20 ():
def __init__ (self, weight_decay, num_classes, ratio = 1.0):
self.weight_decay = weight_decay
self.regularizer = l2(self.weight_decay)
self.initializer = tf.keras.initializers.GlorotUniform(seed = int(time.time()))
self.num_classes = num_classes
self.ratio = ratio
def res_block (self, input_tensor, num_filters, strides = (1, 1), projection = False):
x = Conv2D(num_filters,
(3, 3),
strides = strides,
padding = "same",
use_bias = False,
kernel_initializer = self.initializer,
kernel_regularizer = self.regularizer)(input_tensor)
x = BatchNormalization()(x)
x = tf.nn.relu(x)
x = Conv2D(num_filters,
(3, 3),
padding = "same",
use_bias = False,
kernel_initializer = self.initializer,
kernel_regularizer = self.regularizer)(x)
x = BatchNormalization(gamma_initializer = 'zeros')(x)
if projection:
shortcut = Conv2D(num_filters,
(1, 1),
padding = "same",
use_bias = False,
kernel_initializer = self.initializer,
kernel_regularizer = self.regularizer)(input_tensor)
shortcut = BatchNormalization()(shortcut)
elif strides != (1, 1):
shortcut = Conv2D(num_filters,
(1, 1),
strides = strides,
padding = "same",
use_bias = False,
kernel_initializer = self.initializer,
kernel_regularizer = self.regularizer)(input_tensor)
shortcut = BatchNormalization()(shortcut)
else:
shortcut = input_tensor
x = x + shortcut
y = tf.nn.relu(x)
return y
def build_model (self):
x_in = Input(shape = (None, None, 3), name = "input")
# The first conv layer.
x = Conv2D(16,
(3, 3),
strides=(1, 1),
name='conv0',
padding='same',
use_bias=False,
kernel_initializer = self.initializer,
kernel_regularizer = self.regularizer) (x_in)
x = BatchNormalization()(x)
x = tf.nn.relu(x)
# Residual blocks
for i in range (3):
if i == 0:
x = self.res_block(x, int(16 * self.ratio), projection = True)
else:
x = self.res_block(x, int(16 * self.ratio))
for i in range (3):
if i == 0:
x = self.res_block(x, int(32 * self.ratio), strides = (2, 2))
else:
x = self.res_block(x, int(32 * self.ratio))
for i in range (3):
if i == 0:
x = self.res_block(x, int(64 * self.ratio), strides = (2, 2))
else:
x = self.res_block(x, int(64 * self.ratio))
# The final average pooling layer and fully-connected layer.
x = GlobalAveragePooling2D()(x)
#y = Dense(self.num_classes, activation = 'softmax', name='fully_connected',
y = Dense(self.num_classes, name='fully_connected',
kernel_initializer = self.initializer,
kernel_regularizer = self.regularizer,
bias_regularizer = self.regularizer)(x)
return Model(x_in, y, name = "resnet20")
class wideresnet28 ():
def __init__ (self, weight_decay, num_classes):
self.weight_decay = weight_decay
self.num_classes = num_classes
self.batch_norm_momentum = 0.99
self.batch_norm_epsilon = 1e-5
def res_block (self, input_tensor, num_filters, strides = (1, 1), projection = False):
x = Conv2D(num_filters,
(3, 3),
strides = strides,
padding = "same",
use_bias = False,
kernel_regularizer = self.regularizer)(input_tensor)
x = BatchNormalization(momentum = self.batch_norm_momentum,
epsilon = self.batch_norm_epsilon)(x)
x = tf.nn.relu(x)
x = Dropout(0.3)(x)
x = Conv2D(num_filters,
(3, 3),
padding = "same",
use_bias = False,
kernel_regularizer = self.regularizer)(x)
x = BatchNormalization(momentum = self.batch_norm_momentum,
gamma_initializer = 'zeros',
epsilon = self.batch_norm_epsilon)(x)
if projection:
shortcut = Conv2D(num_filters,
(1, 1),
padding = "same",
use_bias = False,
kernel_regularizer = self.regularizer)(input_tensor)
shortcut = BatchNormalization(momentum = self.batch_norm_momentum,
epsilon = self.batch_norm_epsilon)(shortcut)
elif strides != (1, 1):
shortcut = Conv2D(num_filters,
(1, 1),
strides = strides,
padding = "same",
use_bias = False,
kernel_regularizer = self.regularizer)(input_tensor)
shortcut = BatchNormalization(momentum = self.batch_norm_momentum,
epsilon = self.batch_norm_epsilon)(shortcut)
else:
shortcut = input_tensor
x = x + shortcut
y = tf.nn.relu(x)
return y
def build_model (self):
self.regularizer = l2(self.weight_decay)
d = 28
k = 10
rounds = int((d - 4) / 6)
x_in = Input(shape = (None, None, 3), name = "input")
# The first conv layer.
x = Conv2D(16,
(3, 3),
strides=(1, 1),
name='conv0',
padding='same',
use_bias=False,
kernel_regularizer = self.regularizer) (x_in)
x = BatchNormalization(momentum = self.batch_norm_momentum,
epsilon = self.batch_norm_epsilon)(x)
x = tf.nn.relu(x)
# Residual blocks
for i in range (rounds):
if i == 0:
x = self.res_block(x, 16 * k, projection = True)
else:
x = self.res_block(x, 16 * k)
for i in range (rounds):
if i == 0:
x = self.res_block(x, 32 * k, strides = (2, 2))
else:
x = self.res_block(x, 32 * k)
for i in range (rounds):
if i == 0:
x = self.res_block(x, 64 * k, strides = (2, 2))
else:
x = self.res_block(x, 64 * k)
# The final average pooling layer and fully-connected layer.
x = GlobalAveragePooling2D()(x)
y = Dense(self.num_classes, activation = 'softmax', name='fully_connected',
kernel_regularizer = self.regularizer,
bias_regularizer = self.regularizer)(x)
return Model(x_in, y, name = "wideresnet28-10")