-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathq_resnet_v2.py
409 lines (373 loc) · 23.8 KB
/
q_resnet_v2.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@time:2019/4/13 下午11:06
@author:bigmelon
resnet-v2 implementation for paper
"""
import tensorflow as tf
from q_resnet_utils import subsample_1d, stack_blocks_dense_1d, conv1d_same, Block
from q_attention_module import se_block, sese_block
slim = tf.contrib.slim
@slim.add_arg_scope
def standard_bottleneck_1d(inputs, depth, depth_bottleneck, stride, v, rate=1,
outputs_collections=None, scope=None, attention_module=None):
"""
Args:
inputs: A tensor of size [batch, height, width, channels].
depth、depth_bottleneck、stride三个参数是前面blocks类中的args
depth: 一个block中的某个unit中(第三个conv)输出的feature-map的个数
depth_bottleneck: 一个block中的某个unit中(前面两个conv)输出的feature-map个数
stride: 是short_cut路径对于para_inputs/pre_act(经过bn层的para_inputs)的subsample_2d的步长 -- (是否经过bn层主要看输入输出通道数是否一致)
以及unit中conv-2的步长
rate: An integer, rate for atrous convolution.
outputs_collections: 是收集end_points的collection
scope: 是这个unit的名称
attention_module: SE-blocks or SESE-blocks
"""
with tf.variable_scope(scope, 'bottleneck_v2', [inputs]) as sc:
depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
# pre activate + bn + relu
preact = slim.batch_norm(inputs, activation_fn=tf.nn.relu, scope='preact')
# shortcut fine-tune
if depth == depth_in:
shortcut = subsample_1d(inputs, stride, 'shortcut')
else:
shortcut = slim.conv2d(preact, depth, [1, 1], stride=stride, normalizer_fn=None, activation_fn=None, scope='shortcut')
# convs
residual = slim.conv2d(preact, depth_bottleneck, [1, 1], stride=1, scope='conv1')
residual = conv1d_same(residual, depth_bottleneck, 3, stride, rate=rate, scope='conv2')
residual = slim.conv2d(residual, depth, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='conv3')
# add se
if attention_module == 'se_block':
residual = se_block(residual, name='se_block', ratio=2 if residual.get_shape()[-1] <= 8 else 8)
if attention_module == 'sese_block':
residual = sese_block(input_feature=residual, name='sese_block', v=v, ratio=2 if residual.get_shape()[-1] <= 8 else 8)
# junction
output = shortcut + residual
return slim.utils.collect_named_outputs(outputs_collections, sc.name, output)
@slim.add_arg_scope
def pre_bottleneck_1d(inputs, depth, depth_bottleneck, stride, v, rate=1,
outputs_collections=None, scope=None, attention_module=None):
"""
Args:
inputs: A tensor of size [batch, height, width, channels].
depth、depth_bottleneck、stride三个参数是前面blocks类中的args
depth: 一个block中的某个unit中(第三个conv)输出的feature-map的个数
depth_bottleneck: 一个block中的某个unit中(前面两个conv)输出的feature-map个数
stride: 是short_cut路径对于para_inputs/pre_act(经过bn层的para_inputs)的subsample_2d的步长 -- (是否经过bn层主要看输入输出通道数是否一致)
以及unit中conv-2的步长
rate: An integer, rate for atrous convolution.
outputs_collections: 是收集end_points的collection
scope: 是这个unit的名称
attention_module: SE-blocks or SESE-blocks
"""
with tf.variable_scope(scope, 'bottleneck_v2', [inputs]) as sc:
depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
# add se
if attention_module == 'se_block':
residual = se_block(inputs, name='se_block', ratio=2 if inputs.get_shape()[-1] <= 8 else 8)
# add sese
elif attention_module == 'sese_block':
# todo ratio to be defined...
residual = sese_block(input_feature=inputs, name='sese_block', v=v, ratio=2 if inputs.get_shape()[-1] <= 8 else 8)
# no other block implemented
else:
residual = inputs
# pre activate
preact = slim.batch_norm(inputs, activation_fn=tf.nn.relu, scope='preact')
# shortcut fine-tune
if depth == depth_in:
shortcut = subsample_1d(inputs, stride, 'shortcut')
else:
shortcut = slim.conv2d(preact, depth, [1, 1], stride=stride, normalizer_fn=None, activation_fn=None, scope='shortcut')
# convs
residual = slim.conv2d(residual, depth_bottleneck, [1, 1], stride=1, scope='conv1')
residual = conv1d_same(residual, depth_bottleneck, 3, stride, rate=rate, scope='conv2')
residual = slim.conv2d(residual, depth, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='conv3')
# junction
output = shortcut + residual
return slim.utils.collect_named_outputs(outputs_collections, sc.name, output)
@slim.add_arg_scope
def identity_bottleneck_1d(inputs, depth, depth_bottleneck, stride, v, rate=1,
outputs_collections=None, scope=None, attention_module=None):
"""
Args:
inputs: A tensor of size [batch, height, width, channels].
depth、depth_bottleneck、stride三个参数是前面blocks类中的args
depth: 一个block中的某个unit中(第三个conv)输出的feature-map的个数
depth_bottleneck: 一个block中的某个unit中(前面两个conv)输出的feature-map个数
stride: 是short_cut路径对于para_inputs/pre_act(经过bn层的para_inputs)的subsample_2d的步长 -- (是否经过bn层主要看输入输出通道数是否一致)
以及unit中conv-2的步长
rate: An integer, rate for atrous convolution.
outputs_collections: 是收集end_points的collection
scope: 是这个unit的名称
attention_module: SE-blocks or SESE-blocks
"""
with tf.variable_scope(scope, 'bottleneck_v2', [inputs]) as sc:
depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
# pre activate
preact = slim.batch_norm(inputs, activation_fn=tf.nn.relu, scope='preact')
# shortcut fine-tune
if depth == depth_in:
# pre se/sese in shortcut
if attention_module == 'se_block':
shortcut = se_block(inputs, name='se_block', ratio=2 if inputs.get_shape()[-1] <= 8 else 8)
elif attention_module == 'sese_block':
# todo ratio to be defined...
shortcut = sese_block(input_feature=inputs, name='sese_block', v=v, ratio=2 if inputs.get_shape()[-1] <= 8 else 8)
else:
shortcut = inputs
shortcut = subsample_1d(shortcut, stride, 'shortcut')
else:
# pre se/sese in shortcut
if attention_module == 'se_block':
shortcut = se_block(inputs, name='se_block', ratio=2 if inputs.get_shape()[-1] <= 8 else 8)
elif attention_module == 'sese_block':
# todo ratio to be defined ratio should be smaller than inputs.get_shape()[-1]
shortcut = sese_block(input_feature=inputs, name='sese_block', v=v, ratio=2 if inputs.get_shape()[-1] <= 8 else 8)
else:
shortcut = inputs
shortcut = slim.conv2d(shortcut, depth, [1, 1], stride=stride, normalizer_fn=None, activation_fn=None, scope='shortcut')
# convs
residual = slim.conv2d(preact, depth_bottleneck, [1, 1], stride=1, scope='conv1')
residual = conv1d_same(residual, depth_bottleneck, 3, stride, rate=rate, scope='conv2')
residual = slim.conv2d(residual, depth, [1, 1], stride=1, normalizer_fn=None, activation_fn=None, scope='conv3')
# add se
if attention_module == 'se_block':
residual = se_block(residual, name='se_block', ratio=2 if residual.get_shape()[-1] <= 8 else 8)
if attention_module == 'sese_block':
# todo ratio tobe defined...
residual = sese_block(input_feature=residual, name='sese_block', v=v, ratio=2 if residual.get_shape()[-1] <= 8 else 8)
# junction
output = shortcut + residual
return slim.utils.collect_named_outputs(outputs_collections, sc.name, output)
def resnet_v2_1d(inputs, blocks, num_classes=None, is_training=True, global_pool=True,
output_stride=None, include_root_block=True, spatial_squeeze=True,
reuse=None, scope=None, s=None):
"""
implementation for resnet_v2 1d | more detail see tf/slim/.../nets/resnet_v2_discarded.py
"""
with tf.variable_scope(scope, 'resnet_v2', [inputs], reuse=reuse) as sc:
end_points_collection = sc.original_name_scope + '_end_points'
# todo slim.conv2d?????????
with slim.arg_scope([slim.conv2d, standard_bottleneck_1d if s == 0 else pre_bottleneck_1d if s == 1
else identity_bottleneck_1d, stack_blocks_dense_1d],
outputs_collections=end_points_collection):
with slim.arg_scope([slim.batch_norm], is_training=is_training): # 单独为batch_norm设置train的参数状态
net = inputs
if include_root_block:
if output_stride is not None:
if output_stride % 4 != 0:
raise ValueError('The output_stride needs to be a multiple of 4.')
output_stride /= 4
# We do not include batch normalization or activation functions in
# conv1 because the first ResNet unit will perform these. Cf.
# Appendix of [2].
with slim.arg_scope([slim.conv2d], activation_fn=None, normalizer_fn=None):
net = conv1d_same(net, num_outputs=8, kernel_size=4, stride=2, scope='conv1')
net = slim.max_pool2d(net, [1, 3], stride=[1, 2], scope='pool1')
net = stack_blocks_dense_1d(net, blocks, output_stride)
# This is needed because the pre-activation variant does not have batch
# normalization or activation functions in the residual unit output. See
# Appendix of [2].
net = slim.batch_norm(net, activation_fn=tf.nn.relu, scope='postnorm')
# Convert end_points_collection into a dictionary of end_points.
end_points = slim.utils.convert_collection_to_dict(end_points_collection)
if global_pool:
# Global average pooling.
net = tf.reduce_mean(net, [1, 2], name='pool5', keepdims=True) # (x,1,xx,xxx) -> (x,xxx)
end_points['global_pool'] = net
if num_classes:
net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='logits')
end_points[sc.name + '/logits'] = net
# todo make it clear before run
if spatial_squeeze:
net = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
end_points[sc.name + '/spatial_squeeze'] = net
end_points['predictions'] = slim.softmax(net, scope='predictions')
return net, end_points
def resnet_v2_block_1d(scope, base_depth, num_units, stride, v, attention_module, switch):
"""
implementation for resnet_v2_1d
Args:(depth, depth_bottleneck, stride) | for general cases => depth=4*depth_bottleneck
base_depth: The depth of the bottleneck layer for each unit.
num_units: The number of units in the block.
stride: implemented as a stride in the last unit, other units have stride=1.
"""
if switch != 0 and switch != 1 and switch != 2:
exit('[!] Wrong args.switch input resnet_v1 - line395!')
return Block(scope, standard_bottleneck_1d if switch == 0
else pre_bottleneck_1d if switch == 1
else identity_bottleneck_1d if switch == 2
else None, [{'depth': base_depth * 2,
'depth_bottleneck': base_depth,
'stride': 1,
'v': v,
'attention_module': attention_module
}] * (num_units - 1) + [{
'depth': base_depth * 2,
'depth_bottleneck': base_depth,
'stride': stride,
'v': v,
'attention_module': attention_module
}])
def resnet_v2_block_1d_v1(scope, base_depth, v, attention_module, switch, num_units=None):
"""
implementation for resnet_v2_1d
Args:(depth, depth_bottleneck, stride) | for general cases => depth=4*depth_bottleneck
base_depth: The depth of the bottleneck layer for each unit.
num_units: The number of units in the block.
stride: implemented as a stride in the last unit, other units have stride=1.
"""
if switch != 0 and switch != 1 and switch != 2:
exit('[!] Wrong args.switch input resnet_v1 - line395!')
return Block(scope, standard_bottleneck_1d if switch == 0
else pre_bottleneck_1d if switch == 1
else identity_bottleneck_1d if switch == 2
else None, [{'depth': base_depth * 2,
'depth_bottleneck': base_depth,
'stride': 2,
'v': v,
'attention_module': attention_module
}] * (num_units - 1) + [{'depth': base_depth * 2,
'depth_bottleneck': base_depth,
'stride': 1,
'v': v,
'attention_module': attention_module}])
def resnet_v2_block_1d_v2(scope, base_depth, v, attention_module, switch, num_units=None):
"""
implementation for resnet_v2_1d
Args:(depth, depth_bottleneck, stride) | for general cases => depth=4*depth_bottleneck
base_depth: The depth of the bottleneck layer for each unit.
num_units: The number of units in the block.
stride: implemented as a stride in the last unit, other units have stride=1.
"""
if switch != 0 and switch != 1 and switch != 2:
exit('[!] Wrong args.switch input resnet_v1 - line395!')
return Block(scope, standard_bottleneck_1d if switch == 0
else pre_bottleneck_1d if switch == 1
else identity_bottleneck_1d if switch == 2
else None, [{'depth': base_depth * 2,
'depth_bottleneck': base_depth,
'stride': 1,
'v': v,
'attention_module': attention_module
}] * (num_units - 1) + [{'depth': base_depth * 4,
'depth_bottleneck': base_depth * 2,
'stride': 2,
'v': v,
'attention_module': attention_module}])
def resnet_v2_11_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_11_1d_dual_channel',
attention_module=None, switch=None):
"""
"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=1, v=v, attention_module=attention_module, switch=switch)
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
def resnet_v2_14_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_14_1d_dual_channel',
attention_module=None, switch=None):
"""
"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block4', base_depth=64, num_units=1, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
def resnet_v2_17_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_17_1d_dual_channel',
attention_module=None, switch=None):
"""
"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block4', base_depth=64, num_units=1, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
def resnet_v2_20_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_20_1d_dual_channel',
attention_module=None, switch=None):
"""
"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block4', base_depth=64, num_units=1, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
# todo -> 23
def resnet_v2_23_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_23_1d_dual_channel',
attention_module=None, switch=None):
"""
"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block4', base_depth=64, num_units=2, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
# todo -> 26
def resnet_v2_26_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_26_1d_dual_channel',
attention_module=None, switch=None):
"""
"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=1, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=3, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block4', base_depth=64, num_units=2, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
# todo -> 34
def resnet_v2_34_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_34_1d_dual_channel',
attention_module=None, switch=None):
"""resnet_v2_34_1d for dual channel"""
blocks = [
resnet_v2_block_1d_v1('block1', base_depth=8, num_units=3, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block2', base_depth=16, num_units=4, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block3', base_depth=32, num_units=6, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d_v1('block4', base_depth=64, num_units=3, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)
# todo -> used
def resnet_v2_50_1d_dual_channel(inputs, v, num_classes=None, is_training=True, global_pool=True, output_stride=None,
spatial_squeeze=True, reuse=None, scope='resnet_v2_50_1d_dual_channel',
attention_module=None, switch=None):
"""resnet_v2_50_1d for dual channel"""
blocks = [
resnet_v2_block_1d('block1', base_depth=8, num_units=3, stride=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d('block2', base_depth=16, num_units=4, stride=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d('block3', base_depth=32, num_units=6, stride=2, v=v, attention_module=attention_module, switch=switch),
resnet_v2_block_1d('block4', base_depth=64, num_units=3, stride=1, v=v, attention_module=attention_module, switch=switch),
]
return resnet_v2_1d(inputs, blocks, num_classes, is_training=is_training, global_pool=global_pool,
output_stride=output_stride, include_root_block=True, spatial_squeeze=spatial_squeeze,
reuse=reuse, scope=scope, s=switch)