-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
55 lines (42 loc) · 1.93 KB
/
resnet.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
from keras.layers import Add, Conv2D, BatchNormalization, Activation
from keras.layers import MaxPooling2D, ZeroPadding2D
class ResNet():
def _residual_block(self, input, filters, strides=(1, 1), short_cut=False):
if short_cut: # convolution block
short_cut = Conv2D(4 * filters, (1, 1), strides=strides, padding='valid', use_bias=True)(input)
short_cut = BatchNormalization()(short_cut)
else:
short_cut = input # identity block
x = Conv2D(filters, (1, 1), strides=strides, padding='valid', use_bias='True')(input)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(filters, (3, 3), strides=(1, 1), padding='same', use_bias='True')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(4 * filters, (1, 1), strides=(1, 1), padding='valid', use_bias='True')(x)
x = BatchNormalization()(x)
x = Add()([x, short_cut])
x = Activation('relu')(x)
return x
def _stack(self, input, filters, num_blocks, strides=(2, 2)):
x = self._residual_block(input, filters, strides=strides, short_cut=True)
for i in range(2, num_blocks):
x = self._residual_block(x, filters)
return x
def resnet_model(self, inputs, architecture):
# stage1
x = ZeroPadding2D((3, 3))(inputs)
x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', use_bias=True)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
# stage2
x = self._stack(x, 64, num_blocks=3, strides=(1, 1))
# stage3
x = self._stack(x, 128, num_blocks=4)
# stage4
block_count = {"resnet50": 6, "resnet101": 23}[architecture]
x = self._stack(x, 256, num_blocks=block_count)
# stage5
x = self._stack(x, 512, num_blocks=3)
return x