-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResNet.py
More file actions
79 lines (70 loc) · 3.17 KB
/
Copy pathResNet.py
File metadata and controls
79 lines (70 loc) · 3.17 KB
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
from keras import layers
from keras.layers import Dense
from keras.layers import Conv2D
from keras.layers import AveragePooling2D
from keras.layers import Flatten
from keras.layers import MaxPool2D
from keras.layers import Input
from keras.layers import BatchNormalization
from keras.layers import Activation
from keras.models import Model
from keras.regularizers import l2
def conv2d_bn(x, filters, kernel_size, weight_decay=.0, strides=(1, 1)):
layer = Conv2D(filters=filters,
kernel_size=kernel_size,
strides=strides,
padding='same',
use_bias=False,
kernel_regularizer=l2(weight_decay)
)(x)
layer = BatchNormalization()(layer)
return layer
def conv2d_bn_relu(x, filters, kernel_size, weight_decay=.0, strides=(1, 1)):
layer = conv2d_bn(x, filters, kernel_size, weight_decay, strides)
layer = Activation('relu')(layer)
return layer
def ResidualBlock(x, filters, kernel_size, weight_decay, downsample=True):
if downsample:
# residual_x = conv2d_bn_relu(x, filters, kernel_size=1, strides=2)
residual_x = conv2d_bn(x, filters, kernel_size=1, strides=2)
stride = 2
else:
residual_x = x
stride = 1
residual = conv2d_bn_relu(x,
filters=filters,
kernel_size=kernel_size,
weight_decay=weight_decay,
strides=stride,
)
residual = conv2d_bn(residual,
filters=filters,
kernel_size=kernel_size,
weight_decay=weight_decay,
strides=1,
)
out = layers.add([residual_x, residual])
out = Activation('relu')(out)
return out
def ResNet18(classes, input_shape, weight_decay=1e-4):
input = Input(shape=input_shape)
x = input
x = conv2d_bn_relu(x, filters=24, kernel_size=(3, 3), weight_decay=weight_decay, strides=(2, 2))
x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)
# # conv 2
x = ResidualBlock(x, filters=24, kernel_size=(3, 3), weight_decay=weight_decay, downsample=False)
x = ResidualBlock(x, filters=24, kernel_size=(3, 3), weight_decay=weight_decay, downsample=False)
# # conv 3
x = ResidualBlock(x, filters=48, kernel_size=(3, 3), weight_decay=weight_decay, downsample=True)
x = ResidualBlock(x, filters=48, kernel_size=(3, 3), weight_decay=weight_decay, downsample=False)
# # conv 4
x = ResidualBlock(x, filters=96, kernel_size=(3, 3), weight_decay=weight_decay, downsample=True)
x = ResidualBlock(x, filters=96, kernel_size=(3, 3), weight_decay=weight_decay, downsample=False)
# # conv 5
x = ResidualBlock(x, filters=192, kernel_size=(3, 3), weight_decay=weight_decay, downsample=True)
x = ResidualBlock(x, filters=192, kernel_size=(3, 3), weight_decay=weight_decay, downsample=False)
x = AveragePooling2D(pool_size=(2, 2), padding='valid')(x)
x = Flatten()(x)
x = Dense(classes, activation='softmax')(x)
model = Model(input, x, name='ResNet18')
return model