-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresnet_18.py
154 lines (136 loc) · 6.82 KB
/
resnet_18.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
import tensorflow as tf
BN_AXIS = 3
DATA_FORMAT = 'channels_last'
def ResNet18(include_top=False, weights=None, input_shape=None, layer_params=[2, 2, 2, 2], pooling=None):
input_shape = _obtain_input_shape(input_shape,
default_size=224,
min_size=32,
data_format=DATA_FORMAT,
require_flatten=include_top,
weights=weights)
img_input = tf.keras.layers.Input(shape=input_shape)
x = tf.keras.layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
x = tf.keras.layers.Conv2D(64, (7, 7),
strides=(2, 2),
padding='valid',
kernel_initializer='he_normal',
name='conv1')(x)
x = tf.keras.layers.BatchNormalization(axis=BN_AXIS, name='bn_conv1')(x)
x = tf.keras.layers.Activation('relu')(x)
x = tf.keras.layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
x = tf.keras.layers.MaxPooling2D((3, 3), strides=(2, 2))(x)
x = make_basic_block_layer(x, filter_num=64,
blocks=layer_params[0])
x = make_basic_block_layer(x, filter_num=128,
blocks=layer_params[1],
stride=2)
x = make_basic_block_layer(x, filter_num=256,
blocks=layer_params[2],
stride=2)
x = make_basic_block_layer(x, filter_num=512,
blocks=layer_params[3],
stride=2)
if pooling == 'avg':
x = tf.keras.layers.GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = tf.keras.layers.GlobalMaxPooling2D()(x)
model = tf.keras.Model(img_input, x, name='resnet18')
return model
def make_basic_block_base(inputs, filter_num, stride=1):
x = tf.keras.layers.Conv2D(filters=filter_num,
kernel_size=(3, 3),
strides=stride,
kernel_initializer='he_normal',
padding="same")(inputs)
x = tf.keras.layers.BatchNormalization(axis=BN_AXIS)(x)
x = tf.keras.layers.Conv2D(filters=filter_num,
kernel_size=(3, 3),
strides=1,
kernel_initializer='he_normal',
padding="same")(x)
x = tf.keras.layers.BatchNormalization(axis=BN_AXIS)(x)
shortcut = inputs
if stride != 1:
shortcut = tf.keras.layers.Conv2D(filters=filter_num,
kernel_size=(1, 1),
strides=stride,
kernel_initializer='he_normal')(inputs)
shortcut = tf.keras.layers.BatchNormalization(axis=BN_AXIS)(shortcut)
x = tf.keras.layers.add([x, shortcut])
x = tf.keras.layers.Activation('relu')(x)
return x
def make_basic_block_layer(inputs, filter_num, blocks, stride=1):
x = make_basic_block_base(inputs, filter_num, stride=stride)
for _ in range(1, blocks):
x = make_basic_block_base(x, filter_num, stride=1)
return x
def _obtain_input_shape(input_shape,
default_size,
min_size,
data_format,
require_flatten,
weights=None):
"""
Private function taken from Tensorflow internal library.
"""
if weights != 'imagenet' and input_shape and len(input_shape) == 3:
if data_format == 'channels_first':
default_shape = (input_shape[0], default_size, default_size)
else:
default_shape = (default_size, default_size, input_shape[-1])
else:
if data_format == 'channels_first':
default_shape = (3, default_size, default_size)
else:
default_shape = (default_size, default_size, 3)
if weights == 'imagenet' and require_flatten:
if input_shape is not None:
if input_shape != default_shape:
raise ValueError('When setting `include_top=True` '
'and loading `imagenet` weights, '
'`input_shape` should be ' +
str(default_shape) + '.')
return default_shape
if input_shape:
if data_format == 'channels_first':
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[0] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape=' + str(input_shape) + '`')
if ((input_shape[1] is not None and input_shape[1] < min_size) or
(input_shape[2] is not None and input_shape[2] < min_size)):
raise ValueError('Input size must be at least ' +
str(min_size) + 'x' + str(min_size) +
'; got `input_shape=' +
str(input_shape) + '`')
else:
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[-1] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape=' + str(input_shape) + '`')
if ((input_shape[0] is not None and input_shape[0] < min_size) or
(input_shape[1] is not None and input_shape[1] < min_size)):
raise ValueError('Input size must be at least ' +
str(min_size) + 'x' + str(min_size) +
'; got `input_shape=' +
str(input_shape) + '`')
else:
if require_flatten:
input_shape = default_shape
else:
if data_format == 'channels_first':
input_shape = (3, None, None)
else:
input_shape = (None, None, 3)
if require_flatten:
if None in input_shape:
raise ValueError('If `include_top` is True, '
'you should specify a static `input_shape`. '
'Got `input_shape=' + str(input_shape) + '`')
return input_shape