-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.py
220 lines (192 loc) · 11.7 KB
/
net.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
# ------------------------------------------------------------------------------
# Single Shot Multibox Detector for Vertebra detection
# Jan Kukacka, 11/2017
# jan.kukacka@tum.de
# ------------------------------------------------------------------------------
# Implementation of SSD models
# ------------------------------------------------------------------------------
# --
import tensorflow as tf
# --
from math import sqrt
# --
from keras.layers import Conv2D, Reshape, Concatenate, Input, Activation, MaxPooling2D, Add, AveragePooling2D, BatchNormalization
from keras.models import Model
# --
from anchor_generator_layer import AnchorGeneratorLayer
# --
def Simple_SSD(num_classes=4):
'''
# Arguments
- num_classes: positive int, including background class
'''
net = {}
net['input'] = x = Input((None, None, 1))
net['conv1'] = x = Conv2D(64, (3,3), padding='same', activation='relu', name='conv1')(x)
net['pool1'] = x = Conv2D(64, (2,2), padding='same', strides=(2,2), name='pool1')(x)
net['conv2'] = x = Conv2D(128, (3,3), padding='same', activation='relu', name='conv2')(x)
net['pool2'] = x = Conv2D(128, (2,2), padding='same', strides=(2,2), name='pool2')(x)
net['conv3'] = x = Conv2D(256, (3,3), padding='same', activation='relu', name='conv3')(x)
net['pool3'] = x = Conv2D(256, (2,2), padding='same', strides=(2,2), name='pool3')(x)
net['conv4_1'] = x = Conv2D(512, (3,3), padding='same', activation='relu', name='conv4_1')(x)
net['conv4_2'] = x = Conv2D(512, (3,3), padding='same', activation='relu', name='conv4_2')(x)
net['pool4'] = x = Conv2D(512, (2,2), padding='same', strides=(2,2), name='pool4')(x)
net['conv5_1'] = x = Conv2D(1024, (3,3), padding='same', activation='relu', name='conv5_1')(x)
net['conv5_2'] = x = Conv2D(1024, (3,3), padding='same', activation='relu', name='conv5_2')(x)
net['pool5'] = x = Conv2D(512, (2,2), padding='same', strides=(2,2), name='pool5')(x)
net['conv6_1'] = x = Conv2D(256, (3,3), padding='same', activation='relu', name='conv6_1')(x)
net['conv6_2'] = x = Conv2D(256, (3,3), padding='same', activation='relu', name='conv6_2')(x)
# BBox prediction blocks
# From conv5_2
# anchors = [0.5, 1, 2]
# net['bbox_conf_conv5_2'] = Conv2D(num_classes*len(anchors), (3,3), padding='same')(net['conv5_2'])
# net['resh_bbox_conf_conv5_2'] = Reshape((-1,num_classes))(net['bbox_conf_conv5_2'])
# net['softmax_bbox_conf_conv5_2'] = Activation('softmax')(net['resh_bbox_conf_conv5_2'])
# net['bbox_loc_conv5_2'] = Conv2D(4*len(anchors), (3,3), padding='same')(net['conv5_2'])
# net['resh_bbox_loc_conv5_2'] = Reshape((-1,4))(net['bbox_loc_conv5_2'])
# net['anchor_conv5_2'] = AnchorGeneratorLayer(feature_stride=16, offset=55,
# aspect_ratios=anchors)(net['conv5_2'])
# From conv6_2
anchors = [sqrt(0.5), 1]
net['bbox_conf_conv6_2'] = Conv2D(num_classes*len(anchors), (3,3), padding='same')(net['conv6_2'])
net['resh_bbox_conf_conv6_2'] = Reshape((-1,num_classes))(net['bbox_conf_conv6_2'])
net['softmax_bbox_conf_conv6_2'] = Activation('softmax')(net['resh_bbox_conf_conv6_2'])
net['bbox_loc_conv6_2'] = Conv2D(4*len(anchors), (3,3), padding='same', name='conv6_2_bbox_loc')(net['conv6_2'])
net['resh_bbox_loc_conv6_2'] = Reshape((-1,4))(net['bbox_loc_conv6_2'])
net['anchor_conv6_2'] = AnchorGeneratorLayer(feature_stride=32, offset=0, #119
aspect_ratios=anchors, scale=2)(net['conv6_2'])
# net['cat_loc'] = Concatenate(axis=1)([net['resh_bbox_loc_conv5_2'],
# net['resh_bbox_loc_conv6_2']])
# net['cat_conf'] = Concatenate(axis=1)([net['softmax_bbox_conf_conv5_2'],
# net['softmax_bbox_conf_conv6_2']])
# net['cat_anc'] = Concatenate(axis=1)([net['anchor_conv5_2'],
# net['anchor_conv6_2']])
# net['output'] = Concatenate(axis=2)([net['cat_loc'],
# net['cat_conf'],
# net['cat_anc']])
net['output'] = Concatenate(axis=2)([net['resh_bbox_loc_conv6_2'],
net['softmax_bbox_conf_conv6_2'],
net['anchor_conv6_2']])
return Model(net['input'], net['output'])
def _get_conv(num_filters, kernel_shape, use_bn, name, x, use_relu=True, strides=(1,1)):
if use_bn:
x = Conv2D(num_filters, kernel_shape, padding='same', name=name, use_bias=False, strides=strides)(x)
x = BatchNormalization()(x)
if use_relu:
x = Activation('relu')(x)
else:
if use_relu:
x = Conv2D(num_filters, kernel_shape, padding='same', activation='relu', name=name, strides=strides)(x)
else:
x = Conv2D(num_filters, kernel_shape, padding='same', name=name, strides=strides)(x)
return x
def Residual_SSD(num_classes=4, use_bn=False, num_anchors=2):
'''
# Arguments
- num_classes: positive int, including background class
- num_anchors: positive int. Number of anchor boxes per location.
Default = 2.
'''
net = {}
net['input'] = x = Input((None, None, 1))
with tf.name_scope('conv_block_1'):
net['conv1'] = x = _get_conv(64, (3,3), use_bn, 'conv1', x)
net['pool1'] = x = MaxPooling2D(pool_size=(2,2), padding='same', name='pool1')(x)
use_bn = False
# Residual block 1
with tf.name_scope('res_block_1'):
net['conv2_1'] = x = _get_conv(128, (3,3), use_bn, 'conv2_1', x)
net['conv2_2'] = x = _get_conv(128, (3,3), use_bn, 'conv2_2', x, False)
net['pool1_dup'] = Concatenate(axis=-1, name='pool1_dup')([net['pool1'], net['pool1']])
net['sum2'] = x = Add()([x, net['pool1_dup']])
net['relu2'] = x = Activation('relu')(x)
# Residual block 2
with tf.name_scope('res_block_2'):
net['pool2'] = x = _get_conv(128, (2,2), use_bn, 'pool2', x, strides=(2,2))
net['conv3'] = x = _get_conv(256, (3,3), use_bn, 'conv3', x, False)
net['res2_pool'] = AveragePooling2D(pool_size=(2,2), padding='same')(net['relu2'])
net['res2_pool_dup'] = Concatenate(axis=-1)([net['res2_pool'],net['res2_pool']])
net['sum3'] = x = Add()([x, net['res2_pool_dup']])
net['relu3'] = x = Activation('relu')(x)
# Residual block 3
with tf.name_scope('res_block_3'):
net['pool3'] = x = _get_conv(256, (2,2), use_bn, 'pool3', x, strides=(2,2))
net['conv4_1'] = x = _get_conv(512, (3,3), use_bn, 'conv4_1', x, False)
net['res3_pool'] = AveragePooling2D(pool_size=(2,2), padding='same')(net['relu3'])
net['res3_pool_dup'] = Concatenate(axis=-1)([net['res3_pool'],net['res3_pool']])
net['sum4'] = x = Add()([x, net['res3_pool_dup']])
net['relu4'] = x = Activation('relu')(x)
# Residual block 4
with tf.name_scope('res_block_4'):
net['conv4_2'] = x = _get_conv(512, (3,3), use_bn, 'conv4_2', x)
net['conv4_3'] = x = _get_conv(512, (3,3), use_bn, 'conv4_3', x, False)
net['sum5'] = x = Add()([x, net['relu4']])
net['relu5'] = x = Activation('relu')(x)
# Residual block 5
with tf.name_scope('res_block_5'):
net['pool4'] = x = _get_conv(512, (2,2), use_bn, 'pool4', x, strides=(2,2))
net['conv5_1'] = x = _get_conv(1024, (3,3), use_bn, 'conv5_1', x, False)
net['res5_pool'] = AveragePooling2D(pool_size=(2,2), padding='same')(net['relu5'])
net['res5_pool_dup'] = Concatenate(axis=-1)([net['res5_pool'],net['res5_pool']])
net['sum6'] = x = Add()([x, net['res5_pool_dup']])
net['relu6'] = x = Activation('relu')(x)
# Residual block 6
with tf.name_scope('res_block_6'):
net['conv5_2'] = x = _get_conv(1024, (3,3), use_bn, 'conv5_2', x)
net['conv5_3'] = x = _get_conv(1024, (3,3), use_bn, 'conv5_3', x, False)
net['sum7'] = x = Add()([x, net['relu6']])
net['relu7'] = x = Activation('relu')(x)
# Residual block 7
with tf.name_scope('res_block_7'):
def my_init(shape, dtype=None):
# print shape
import numpy as np
w = np.eye(256) * .25
w = np.repeat(w,4,axis=0)
w = np.reshape(w,(1,1,1024,256))
return w
net['pool5'] = x = _get_conv(512, (2,2), use_bn, 'pool5', x, strides=(2,2))
net['conv6_1'] = x = _get_conv(256, (3,3), use_bn, 'conv6_1', x, False)
net['res7_pool'] = AveragePooling2D(pool_size=(2,2), padding='same')(net['relu7'])
net['res7_pool_red'] = Conv2D(256, (1,1), name='res7_pool_red', use_bias=False, kernel_initializer=my_init, trainable=False)(net['res7_pool'])
net['sum8'] = x = Add()([x, net['res7_pool_red']])
net['relu8'] = x = Activation('relu')(x)
with tf.name_scope('res_block_8'):
net['conv6_2'] = x = _get_conv(256, (3,3), use_bn, 'conv6_2', x)
net['conv6_3'] = x = _get_conv(256, (3,3), use_bn, 'conv6_3', x, False)
net['sum9'] = x = Add()([x, net['relu8']])
net['relu9'] = x = Activation('relu')(x)
# BBox prediction blocks
# From conv5_2
# anchors = [0.5, 1, 2]
# net['bbox_conf_conv5_2'] = Conv2D(num_classes*len(anchors), (3,3), padding='same')(net['conv5_2'])
# net['resh_bbox_conf_conv5_2'] = Reshape((-1,num_classes))(net['bbox_conf_conv5_2'])
# net['softmax_bbox_conf_conv5_2'] = Activation('softmax')(net['resh_bbox_conf_conv5_2'])
# net['bbox_loc_conv5_2'] = Conv2D(4*len(anchors), (3,3), padding='same')(net['conv5_2'])
# net['resh_bbox_loc_conv5_2'] = Reshape((-1,4))(net['bbox_loc_conv5_2'])
# net['anchor_conv5_2'] = AnchorGeneratorLayer(feature_stride=16, offset=55,
# aspect_ratios=anchors)(net['conv5_2'])
# From conv6_2
with tf.name_scope('bbox_predictor_2'):
net['bbox_conf_relu9'] = Conv2D(num_classes*num_anchors, (3,3), padding='same', name='relu9_bbox_conf')(net['relu9'])
net['resh_bbox_conf_relu9'] = Reshape((-1,num_classes))(net['bbox_conf_relu9'])
net['softmax_bbox_conf_relu9'] = Activation('softmax')(net['resh_bbox_conf_relu9'])
net['bbox_loc_relu9'] = Conv2D(4*num_anchors, (3,3), padding='same', name='relu9_bbox_loc')(net['relu9'])
net['resh_bbox_loc_relu9'] = Reshape((-1,4))(net['bbox_loc_relu9'])
# net['anchor_relu9'] = AnchorGeneratorLayer(feature_stride=32, offset=0, #119
# aspect_ratios=anchors, scale=2)(net['relu9'])
# net['cat_loc'] = Concatenate(axis=1)([net['resh_bbox_loc_conv5_2'],
# net['resh_bbox_loc_conv6_2']])
# net['cat_conf'] = Concatenate(axis=1)([net['softmax_bbox_conf_conv5_2'],
# net['softmax_bbox_conf_conv6_2']])
# net['cat_anc'] = Concatenate(axis=1)([net['anchor_conv5_2'],
# net['anchor_conv6_2']])
# net['output'] = Concatenate(axis=2)([net['cat_loc'],
# net['cat_conf'],
# net['cat_anc']])
net['output'] = Concatenate(axis=2)([net['resh_bbox_loc_relu9'],
net['softmax_bbox_conf_relu9']
])
# ,
# net['anchor_relu9']])
return Model(net['input'], net['output'])