-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbpp_model.py
254 lines (197 loc) · 9.97 KB
/
tbpp_model.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
"""Keras implementation of TextBoxes++."""
from tensorflow.keras.layers import Input, Activation
from tensorflow.keras.layers import Conv2D, SeparableConv2D
from tensorflow.keras.layers import Flatten, Reshape
from tensorflow.keras.layers import concatenate
from tensorflow.keras.models import Model
from utils.layers import Normalize
from ssd_model import ssd512_body
from ssd_model_dense import dsod512_body
from ssd_model_separable import ssd512_dense_separable_body
def multibox_head(source_layers, num_priors, normalizations=None, softmax=True, isQuads=True, isRbb=True, num_classes=2):
num_classes = num_classes
class_activation = 'softmax' if softmax else 'sigmoid'
mbox_conf = []
mbox_loc = []
mbox_quad = []
mbox_rbox = []
for i in range(len(source_layers)):
x = source_layers[i]
name = x.name.split('/')[0]
# normalize
if normalizations is not None and normalizations[i] > 0:
name = name + '_norm'
x = Normalize(normalizations[i], name=name)(x)
# confidence
name1 = name + '_mbox_conf'
x1 = Conv2D(num_priors[i] * num_classes, (3, 5), padding='same', name=name1)(x)
x1 = Flatten(name=name1+'_flat')(x1)
mbox_conf.append(x1)
# location, Delta(x,y,w,h)
name2 = name + '_mbox_loc'
x2 = Conv2D(num_priors[i] * 4, (3, 5), padding='same', name=name2)(x)
x2 = Flatten(name=name2+'_flat')(x2)
mbox_loc.append(x2)
# quadrilateral, Delta(x1,y1,x2,y2,x3,y3,x4,y4)
if isQuads:
name3 = name + '_mbox_quad'
x3 = Conv2D(num_priors[i] * 8, (3, 5), padding='same', name=name3)(x)
x3 = Flatten(name=name3+'_flat')(x3)
mbox_quad.append(x3)
# rotated rectangle, Delta(x1,y1,x2,y2,h)
if isRbb:
name4 = name + '_mbox_rbox'
x4 = Conv2D(num_priors[i] * 5, (3, 5), padding='same', name=name4)(x)
x4 = Flatten(name=name4+'_flat')(x4)
mbox_rbox.append(x4)
mbox_conf = concatenate(mbox_conf, axis=1, name='mbox_conf')
mbox_conf = Reshape((-1, num_classes), name='mbox_conf_logits')(mbox_conf)
mbox_conf = Activation(class_activation, name='mbox_conf_final')(mbox_conf)
mbox_loc = concatenate(mbox_loc, axis=1, name='mbox_loc')
mbox_loc = Reshape((-1, 4), name='mbox_loc_final')(mbox_loc)
if isQuads:
mbox_quad = concatenate(mbox_quad, axis=1, name='mbox_quad')
mbox_quad = Reshape((-1, 8), name='mbox_quad_final')(mbox_quad)
if isRbb:
mbox_rbox = concatenate(mbox_rbox, axis=1, name='mbox_rbox')
mbox_rbox = Reshape((-1, 5), name='mbox_rbox_final')(mbox_rbox)
if isQuads and isRbb:
predictions = concatenate([mbox_loc, mbox_quad, mbox_rbox, mbox_conf], axis=2, name='predictions')
elif isRbb:
predictions = concatenate([mbox_loc, mbox_rbox, mbox_conf], axis=2, name='predictions')
elif isQuads:
predictions = concatenate([mbox_loc, mbox_quad, mbox_conf], axis=2, name='predictions')
else:
predictions = concatenate([mbox_loc, mbox_conf], axis=2, name='predictions')
return predictions
def TBPP512(input_shape=(512, 512, 3), softmax=True, aspect_ratios=[1,2,3,5,1/2,1/3,1/5], scale=0.5, isQuads=True, isRbb=True, num_classes=2, activation='relu'):
"""TextBoxes++512 architecture.
# Arguments
input_shape: Shape of the input image.
# References
- [TextBoxes++: A Single-Shot Oriented Scene Text Detector](https://arxiv.org/abs/1801.02765)
"""
# SSD body
x = input_tensor = Input(shape=input_shape)
source_layers = ssd512_body(x)
num_maps = len(source_layers)
# Add multibox head for classification and regression
num_priors = [2*len(aspect_ratios)] * num_maps
normalizations = [1] * num_maps
output_tensor = multibox_head(source_layers, num_priors, normalizations, softmax, isQuads, isRbb, num_classes=num_classes)
model = Model(input_tensor, output_tensor)
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
model.isQuads = isQuads
model.isRbb = isRbb
model.aspect_ratios = [aspect_ratios * 2] * num_maps
#model.shifts = [[(0.0, 0.0)] * 7 + [(0.0, 0.5)] * 7] * num_maps
# vertical-offsets to better cover all the image-area: Reference https://arxiv.org/pdf/1801.02765.pdf
model.shifts = [[(0.0, -0.25)] * len(aspect_ratios) + [(0.0, 0.25)] * len(aspect_ratios)] * num_maps
model.special_ssd_boxes = False
model.scale = scale
return model
def DSODTBPP512(input_shape=(512, 512, 3), softmax=True, aspect_ratios=[1,2,3,5,1/2,1/3,1/5], scale=0.5, isQuads=True, isRbb=True, num_classes=2, activation='relu'):
"""DenseNet based Architecture for TextBoxes++512.
"""
# DSOD body
x = input_tensor = Input(shape=input_shape)
source_layers = dsod512_body(x, activation=activation)
num_maps = len(source_layers)
# Add multibox head for classification and regression
num_priors = [2*len(aspect_ratios)] * num_maps
normalizations = [1] * num_maps
output_tensor = multibox_head(source_layers, num_priors, normalizations, softmax, isQuads, isRbb, num_classes=num_classes)
model = Model(input_tensor, output_tensor)
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
model.isQuads = isQuads
model.isRbb = isRbb
model.aspect_ratios = [aspect_ratios * 2] * num_maps
#model.shifts = [[(0.0, 0.0)] * 7 + [(0.0, 0.5)] * 7] * num_maps
model.shifts = [[(0.0, -0.25)] * len(aspect_ratios) + [(0.0, 0.25)] * len(aspect_ratios)] * num_maps
model.special_ssd_boxes = False
model.scale = scale
return model
TBPP512_dense = DSODTBPP512
def multibox_head_separable(source_layers, num_priors, normalizations=None, softmax=True, isQuads=True, isRbb=True, num_classes=2):
num_classes = num_classes
class_activation = 'softmax' if softmax else 'sigmoid'
mbox_conf = []
mbox_loc = []
mbox_quad = []
mbox_rbox = []
for i in range(len(source_layers)):
x = source_layers[i]
name = x.name.split('/')[0]
# normalize
if normalizations is not None and normalizations[i] > 0:
name = name + '_norm'
x = Normalize(normalizations[i], name=name)(x)
# confidence
name1 = name + '_mbox_conf'
x1 = SeparableConv2D(num_priors[i] * num_classes, (3, 5), padding='same', name=name1)(x)
x1 = Flatten(name=name1+'_flat')(x1)
mbox_conf.append(x1)
# location, Delta(x,y,w,h)
name2 = name + '_mbox_loc'
x2 = SeparableConv2D(num_priors[i] * 4, (3, 5), padding='same', name=name2)(x)
x2 = Flatten(name=name2+'_flat')(x2)
mbox_loc.append(x2)
# quadrilateral, Delta(x1,y1,x2,y2,x3,y3,x4,y4)
if isQuads:
name3 = name + '_mbox_quad'
x3 = SeparableConv2D(num_priors[i] * 8, (3, 5), padding='same', name=name3)(x)
x3 = Flatten(name=name3+'_flat')(x3)
mbox_quad.append(x3)
# rotated rectangle, Delta(x1,y1,x2,y2,h)
if isRbb:
name4 = name + '_mbox_rbox'
x4 = SeparableConv2D(num_priors[i] * 5, (3, 5), padding='same', name=name4)(x)
x4 = Flatten(name=name4+'_flat')(x4)
mbox_rbox.append(x4)
mbox_conf = concatenate(mbox_conf, axis=1, name='mbox_conf')
mbox_conf = Reshape((-1, num_classes), name='mbox_conf_logits')(mbox_conf)
mbox_conf = Activation(class_activation, name='mbox_conf_final')(mbox_conf)
mbox_loc = concatenate(mbox_loc, axis=1, name='mbox_loc')
mbox_loc = Reshape((-1, 4), name='mbox_loc_final')(mbox_loc)
if isQuads:
mbox_quad = concatenate(mbox_quad, axis=1, name='mbox_quad')
mbox_quad = Reshape((-1, 8), name='mbox_quad_final')(mbox_quad)
if isRbb:
mbox_rbox = concatenate(mbox_rbox, axis=1, name='mbox_rbox')
mbox_rbox = Reshape((-1, 5), name='mbox_rbox_final')(mbox_rbox)
if isQuads and isRbb:
predictions = concatenate([mbox_loc, mbox_quad, mbox_rbox, mbox_conf], axis=2, name='predictions')
elif isRbb:
predictions = concatenate([mbox_loc, mbox_rbox, mbox_conf], axis=2, name='predictions')
elif isQuads:
predictions = concatenate([mbox_loc, mbox_quad, mbox_conf], axis=2, name='predictions')
else:
predictions = concatenate([mbox_loc, mbox_conf], axis=2, name='predictions')
return predictions
def TBPP512_dense_separable(input_shape=(512, 512, 3), softmax=True, aspect_ratios=[1,2,3,5,1/2,1/3,1/5], scale=0.5, isQuads=True, isRbb=True, num_dense_segs=3, use_prev_feature_map=True, num_multi_scale_maps=5, num_classes=2, activation='relu'):
"""TextBoxes++512 architecture with dense blocks and separable convolution.
"""
# custom body
x = input_tensor = Input(shape=input_shape)
source_layers = ssd512_dense_separable_body(x, num_dense_segs=num_dense_segs, use_prev_feature_map=use_prev_feature_map, num_multi_scale_maps=num_multi_scale_maps, activation=activation)
num_maps = len(source_layers)
# Add multibox head for classification and regression
num_priors = [2*len(aspect_ratios)] * num_maps
normalizations = [1] * num_maps
output_tensor = multibox_head_separable(source_layers, num_priors, normalizations, softmax, isQuads, isRbb, num_classes=num_classes)
model = Model(input_tensor, output_tensor)
# parameters for prior boxes
model.image_size = input_shape[:2]
model.source_layers = source_layers
model.isQuads = isQuads
model.isRbb = isRbb
model.aspect_ratios = [aspect_ratios * 2] * num_maps
#model.shifts = [[(0.0, 0.0)] * 7 + [(0.0, 0.5)] * 7] * num_maps
model.shifts = [[(0.0, -0.25)] *len(aspect_ratios) + [(0.0, 0.25)] * len(aspect_ratios)] * num_maps
model.special_ssd_boxes = False
model.scale = scale
return model