-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresnet50.py
More file actions
298 lines (254 loc) · 12.7 KB
/
resnet50.py
File metadata and controls
298 lines (254 loc) · 12.7 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# %% [code]
"""
Modifiation of https://github.com/rcmalli/keras-vggface
notes:
* Рассмотрим функцию `VGGFace` в клонированном репозитории - `keras-vggface.keras-vggface.vggface`. Очевидно, что нам не нужен весь код из функции, а достаточно того, что отвечает за сеть `resnet50` - который является обёрткой над функцией `RESNET50`:
```python
classes = 8631
RESNET50(include_top=include_top, input_tensor=input_tensor,
input_shape=input_shape, pooling=pooling,
weights=weights,
classes=classes)
```
Рассмотрим функцию `RESNET50`, которая находится `keras-vggface.keras-vggface.models`.
* Функция строит архитектуру сети по заданным параметрам.
* Делает несколько проверок
* Загружает веса.
Скопируем код функции `RESNET50`, добавим необходимы импорты, а также сделаем класс `utils` как заплатку для путей.
"""
import numpy as np
from keras.layers import Flatten, Dense, Input, GlobalAveragePooling2D, \
GlobalMaxPooling2D, Activation, Conv2D, MaxPooling2D, BatchNormalization, \
AveragePooling2D, Reshape, Permute, multiply
from keras.applications.imagenet_utils import obtain_input_shape # ❗ изменение для tf 2.6
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.utils.layer_utils import get_source_inputs # ❗изменение для tf 2.6
import warnings
from keras.models import Model
from keras import layers
import matplotlib.pyplot as plt
# Перенесём пути из файла utils и некоторые функции
class utils:
def __init__():
# Пустая инициализация, чтобы не инициализировать объекта,
# а использовать прямые вызовы атрибутов и параметров
pass
# переменные с путями создаются для всех
RESNET50_WEIGHTS_PATH = 'https://github.com/rcmalli/keras-vggface/releases/download/v2.0/rcmalli_vggface_tf_resnet50.h5'
RESNET50_WEIGHTS_PATH_NO_TOP = 'https://github.com/rcmalli/keras-vggface/releases/download/v2.0/rcmalli_vggface_tf_notop_resnet50.h5'
VGGFACE_DIR = 'models/vggface'
# Препроцессинг импута
def preprocess_input(x, data_format=None, version=1):
x_temp = np.copy(x)
if data_format is None:
data_format = K.image_data_format()
assert data_format in {'channels_last', 'channels_first'}
if version == 1:
if data_format == 'channels_first':
x_temp = x_temp[:, ::-1, ...]
x_temp[:, 0, :, :] -= 93.5940
x_temp[:, 1, :, :] -= 104.7624
x_temp[:, 2, :, :] -= 129.1863
else:
x_temp = x_temp[..., ::-1]
x_temp[..., 0] -= 93.5940
x_temp[..., 1] -= 104.7624
x_temp[..., 2] -= 129.1863
elif version == 2:
if data_format == 'channels_first':
x_temp = x_temp[:, ::-1, ...]
x_temp[:, 0, :, :] -= 91.4953
x_temp[:, 1, :, :] -= 103.8827
x_temp[:, 2, :, :] -= 131.0912
else:
x_temp = x_temp[..., ::-1]
x_temp[..., 0] -= 91.4953
x_temp[..., 1] -= 103.8827
x_temp[..., 2] -= 131.0912
else:
raise NotImplementedError
return x_temp
# Декодирование предсказаний
def decode_predictions(preds, top=5):
#-----------------
V1_LABELS_PATH = 'https://github.com/rcmalli/keras-vggface/releases/download/v2.0/rcmalli_vggface_labels_v1.npy'
V2_LABELS_PATH = 'https://github.com/rcmalli/keras-vggface/releases/download/v2.0/rcmalli_vggface_labels_v2.npy'
VGGFACE_DIR = 'models/vggface'
#-----------------
LABELS = None
if len(preds.shape) == 2:
if preds.shape[1] == 2622:
fpath = get_file('rcmalli_vggface_labels_v1.npy',
V1_LABELS_PATH,
cache_subdir=VGGFACE_DIR)
LABELS = np.load(fpath)
elif preds.shape[1] == 8631:
fpath = get_file('rcmalli_vggface_labels_v2.npy',
V2_LABELS_PATH,
cache_subdir=VGGFACE_DIR)
LABELS = np.load(fpath)
else:
raise ValueError('`decode_predictions` expects '
'a batch of predictions '
'(i.e. a 2D array of shape (samples, 2622)) for V1 or '
'(samples, 8631) for V2.'
'Found array with shape: ' + str(preds.shape))
else:
raise ValueError('`decode_predictions` expects '
'a batch of predictions '
'(i.e. a 2D array of shape (samples, 2622)) for V1 or '
'(samples, 8631) for V2.'
'Found array with shape: ' + str(preds.shape))
results = []
for pred in preds:
top_indices = pred.argsort()[-top:][::-1]
result = [[str(LABELS[i].encode('utf8')), pred[i]] for i in top_indices]
result.sort(key=lambda x: x[1], reverse=True)
results.append(result)
return results
def resnet_conv_block(input_tensor, kernel_size, filters, stage, block,
strides=(2, 2), bias=False):
filters1, filters2, filters3 = filters
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
conv1_reduce_name = 'conv' + str(stage) + "_" + str(block) + "_1x1_reduce"
conv1_increase_name = 'conv' + str(stage) + "_" + str(
block) + "_1x1_increase"
conv1_proj_name = 'conv' + str(stage) + "_" + str(block) + "_1x1_proj"
conv3_name = 'conv' + str(stage) + "_" + str(block) + "_3x3"
x = Conv2D(filters1, (1, 1), strides=strides, use_bias=bias,
name=conv1_reduce_name)(input_tensor)
x = BatchNormalization(axis=bn_axis, name=conv1_reduce_name + "/bn")(x)
x = Activation('relu')(x)
x = Conv2D(filters2, kernel_size, padding='same', use_bias=bias,
name=conv3_name)(x)
x = BatchNormalization(axis=bn_axis, name=conv3_name + "/bn")(x)
x = Activation('relu')(x)
x = Conv2D(filters3, (1, 1), name=conv1_increase_name, use_bias=bias)(x)
x = BatchNormalization(axis=bn_axis, name=conv1_increase_name + "/bn")(x)
shortcut = Conv2D(filters3, (1, 1), strides=strides, use_bias=bias,
name=conv1_proj_name)(input_tensor)
shortcut = BatchNormalization(axis=bn_axis, name=conv1_proj_name + "/bn")(
shortcut)
x = layers.add([x, shortcut])
x = Activation('relu')(x)
return x
def resnet_identity_block(input_tensor, kernel_size, filters, stage, block,
bias=False):
filters1, filters2, filters3 = filters
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
conv1_reduce_name = 'conv' + str(stage) + "_" + str(block) + "_1x1_reduce"
conv1_increase_name = 'conv' + str(stage) + "_" + str(
block) + "_1x1_increase"
conv3_name = 'conv' + str(stage) + "_" + str(block) + "_3x3"
x = Conv2D(filters1, (1, 1), use_bias=bias, name=conv1_reduce_name)(
input_tensor)
x = BatchNormalization(axis=bn_axis, name=conv1_reduce_name + "/bn")(x)
x = Activation('relu')(x)
x = Conv2D(filters2, kernel_size, use_bias=bias,
padding='same', name=conv3_name)(x)
x = BatchNormalization(axis=bn_axis, name=conv3_name + "/bn")(x)
x = Activation('relu')(x)
x = Conv2D(filters3, (1, 1), use_bias=bias, name=conv1_increase_name)(x)
x = BatchNormalization(axis=bn_axis, name=conv1_increase_name + "/bn")(x)
x = layers.add([x, input_tensor])
x = Activation('relu')(x)
return x
def RESNET50(include_top=True, weights='vggface',
input_tensor=None, input_shape=None,
pooling=None,
classes=8631):
input_shape = obtain_input_shape(input_shape,
default_size=224,
min_size=32,
data_format=K.image_data_format(),
require_flatten=include_top,
weights=weights)
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
bn_axis = 1
x = Conv2D(
64, (7, 7), use_bias=False, strides=(2, 2), padding='same',
name='conv1/7x7_s2')(img_input)
x = BatchNormalization(axis=bn_axis, name='conv1/7x7_s2/bn')(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2))(x)
x = resnet_conv_block(x, 3, [64, 64, 256], stage=2, block=1, strides=(1, 1))
x = resnet_identity_block(x, 3, [64, 64, 256], stage=2, block=2)
x = resnet_identity_block(x, 3, [64, 64, 256], stage=2, block=3)
x = resnet_conv_block(x, 3, [128, 128, 512], stage=3, block=1)
x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=2)
x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=3)
x = resnet_identity_block(x, 3, [128, 128, 512], stage=3, block=4)
x = resnet_conv_block(x, 3, [256, 256, 1024], stage=4, block=1)
x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=2)
x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=3)
x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=4)
x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=5)
x = resnet_identity_block(x, 3, [256, 256, 1024], stage=4, block=6)
x = resnet_conv_block(x, 3, [512, 512, 2048], stage=5, block=1)
x = resnet_identity_block(x, 3, [512, 512, 2048], stage=5, block=2)
x = resnet_identity_block(x, 3, [512, 512, 2048], stage=5, block=3)
x = AveragePooling2D((7, 7), name='avg_pool')(x)
if include_top:
x = Flatten()(x)
x = Dense(classes, activation='softmax', name='classifier')(x)
else:
if pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = GlobalMaxPooling2D()(x)
# Ensure that the model takes into account
# any potential predecessors of `input_tensor`.
if input_tensor is not None:
inputs = get_source_inputs(input_tensor)
else:
inputs = img_input
# Create model.
model = Model(inputs, x, name='vggface_resnet50')
# load weights
if weights == 'vggface':
if include_top:
weights_path = get_file('rcmalli_vggface_tf_resnet50.h5',
utils.RESNET50_WEIGHTS_PATH,
cache_subdir=utils.VGGFACE_DIR)
else:
weights_path = get_file('rcmalli_vggface_tf_notop_resnet50.h5',
utils.RESNET50_WEIGHTS_PATH_NO_TOP,
cache_subdir=utils.VGGFACE_DIR)
model.load_weights(weights_path)
if K.backend() == 'theano':
layer_utils.convert_all_kernels_in_model(model)
if include_top:
maxpool = model.get_layer(name='avg_pool')
shape = maxpool.output_shape[1:]
dense = model.get_layer(name='classifier')
layer_utils.convert_dense_weights_data_format(dense, shape,
'channels_first')
if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image data format convention '
'(`image_data_format="channels_first"`). '
'For best performance, set '
'`image_data_format="channels_last"` in '
'your Keras config '
'at ~/.keras/keras.json.')
elif weights is not None:
model.load_weights(weights)
return model