-
Notifications
You must be signed in to change notification settings - Fork 2
/
opconty_shufflenetv2.py
169 lines (136 loc) · 6.77 KB
/
opconty_shufflenetv2.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
# -*- coding:utf-8 -*-
# '''
# Created on 18-8-14 下午4:48
#
# @Author: Greg Gao(laygin)
# '''
import numpy as np
from keras.utils import plot_model
from keras_applications.imagenet_utils import _obtain_input_shape
# from keras.applications.imagenet_utils import _obtain_input_shape
from keras.engine.topology import get_source_inputs
from keras.layers import Input, Conv2D, MaxPool2D, GlobalMaxPooling2D, GlobalAveragePooling2D, DepthwiseConv2D
from keras.layers import Activation, Dense, Lambda, BatchNormalization, Concatenate
from keras.models import Model
import keras.backend as K
def channel_split(x, name=''):
# equipartition
in_channles = x.shape.as_list()[-1]
ip = in_channles // 2
c_hat = Lambda(lambda z: z[:, :, :, 0:ip], name='%s/sp%d_slice' % (name, 0))(x)
c = Lambda(lambda z: z[:, :, :, ip:], name='%s/sp%d_slice' % (name, 1))(x)
return c_hat, c
def channel_shuffle(x):
height, width, channels = x.shape.as_list()[1:]
channels_per_split = channels // 2
x = K.reshape(x, [-1, height, width, 2, channels_per_split])
x = K.permute_dimensions(x, (0, 1, 2, 4, 3))
x = K.reshape(x, [-1, height, width, channels])
return x
def shuffle_unit(inputs, out_channels, bottleneck_ratio, strides=2, stage=1, block=1):
if K.image_data_format() == 'channels_last':
bn_axis = -1
else:
raise ValueError('Only channels last supported')
prefix = 'stage{}/block{}'.format(stage, block)
bottleneck_channels = int(out_channels * bottleneck_ratio)
if strides < 2:
c_hat, c = channel_split(inputs, '{}/spl'.format(prefix))
inputs = c
x = Conv2D(bottleneck_channels, kernel_size=(1, 1), strides=1, padding='same', name='{}/1x1conv_1'.format(prefix))(
inputs)
x = BatchNormalization(axis=bn_axis, name='{}/bn_1x1conv_1'.format(prefix))(x)
x = Activation('relu', name='{}/relu_1x1conv_1'.format(prefix))(x)
x = DepthwiseConv2D(kernel_size=3, strides=strides, padding='same', name='{}/3x3dwconv'.format(prefix))(x)
x = BatchNormalization(axis=bn_axis, name='{}/bn_3x3dwconv'.format(prefix))(x)
x = Conv2D(bottleneck_channels, kernel_size=1, strides=1, padding='same', name='{}/1x1conv_2'.format(prefix))(x)
x = BatchNormalization(axis=bn_axis, name='{}/bn_1x1conv_2'.format(prefix))(x)
x = Activation('relu', name='{}/relu_1x1conv_2'.format(prefix))(x)
if strides < 2:
ret = Concatenate(axis=bn_axis, name='{}/concat_1'.format(prefix))([x, c_hat])
else:
s2 = DepthwiseConv2D(kernel_size=3, strides=2, padding='same', name='{}/3x3dwconv_2'.format(prefix))(inputs)
s2 = BatchNormalization(axis=bn_axis, name='{}/bn_3x3dwconv_2'.format(prefix))(s2)
s2 = Conv2D(bottleneck_channels, kernel_size=1, strides=1, padding='same', name='{}/1x1_conv_3'.format(prefix))(
s2)
s2 = BatchNormalization(axis=bn_axis, name='{}/bn_1x1conv_3'.format(prefix))(s2)
s2 = Activation('relu', name='{}/relu_1x1conv_3'.format(prefix))(s2)
ret = Concatenate(axis=bn_axis, name='{}/concat_2'.format(prefix))([x, s2])
ret = Lambda(channel_shuffle, name='{}/channel_shuffle'.format(prefix))(ret)
return ret
def block(x, channel_map, bottleneck_ratio, repeat=1, stage=1):
x = shuffle_unit(x, out_channels=channel_map[stage - 1],
strides=2, bottleneck_ratio=bottleneck_ratio, stage=stage, block=1)
for i in range(1, repeat + 1):
x = shuffle_unit(x, out_channels=channel_map[stage - 1], strides=1,
bottleneck_ratio=bottleneck_ratio, stage=stage, block=(1 + i))
return x
def ShuffleNetV2(include_top=True,
input_tensor=None,
scale_factor=1.0,
pooling='max',
input_shape=(224, 224, 3),
load_model=None,
num_shuffle_units=[3, 7, 3],
bottleneck_ratio=1,
classes=1000):
if K.backend() != 'tensorflow':
raise RuntimeError('Only tensorflow supported for now')
name = 'ShuffleNetV2_{}_{}_{}'.format(scale_factor, bottleneck_ratio, "".join([str(x) for x in num_shuffle_units]))
input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=28, require_flatten=include_top,
data_format=K.image_data_format())
out_dim_stage_two = {0.5: 48, 1: 116, 1.5: 176, 2: 244}
if pooling not in ['max', 'avg']:
raise ValueError('Invalid value for pooling')
if not (float(scale_factor) * 4).is_integer():
raise ValueError('Invalid value for scale_factor, should be x over 4')
exp = np.insert(np.arange(len(num_shuffle_units), dtype=np.float32), 0, 0) # [0., 0., 1., 2.]
out_channels_in_stage = 2 ** exp
out_channels_in_stage *= out_dim_stage_two[bottleneck_ratio] # calculate output channels for each stage
out_channels_in_stage[0] = 24 # first stage has always 24 output channels
out_channels_in_stage *= scale_factor
out_channels_in_stage = out_channels_in_stage.astype(int)
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
# create shufflenet architecture
x = Conv2D(filters=out_channels_in_stage[0], kernel_size=(3, 3), padding='same', use_bias=False, strides=(2, 2),
activation='relu', name='conv1')(img_input)
x = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='maxpool1')(x)
# create stages containing shufflenet units beginning at stage 2
for stage in range(len(num_shuffle_units)):
repeat = num_shuffle_units[stage]
x = block(x, out_channels_in_stage,
repeat=repeat,
bottleneck_ratio=bottleneck_ratio,
stage=stage + 2)
if bottleneck_ratio < 2:
k = 1024
else:
k = 2048
x = Conv2D(k, kernel_size=1, padding='same', strides=1, name='1x1conv5_out', activation='relu')(x)
if pooling == 'avg':
x = GlobalAveragePooling2D(name='global_avg_pool')(x)
elif pooling == 'max':
x = GlobalMaxPooling2D(name='global_max_pool')(x)
if include_top:
x = Dense(classes, name='fc')(x)
x = Activation('softmax', name='softmax')(x)
if input_tensor:
inputs = get_source_inputs(input_tensor)
else:
inputs = img_input
model = Model(inputs, x, name=name)
if load_model:
model.load_weights('', by_name=True)
return model
if __name__ == '__main__':
import os
os.environ['CUDA_VISIBLE_DEVICES'] = ''
model = ShuffleNetV2(include_top=True, input_shape=(224, 224, 3), bottleneck_ratio=1)
plot_model(model, to_file='shufflenetv2.png', show_layer_names=True, show_shapes=True)
pass