-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet.py
161 lines (140 loc) · 7.16 KB
/
resnet.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
from keras_resnet import models as resnet_models
from keras.layers import Input, Conv2DTranspose, BatchNormalization, ReLU, Conv2D, Lambda, Dropout
from keras.models import Model
from keras.regularizers import l2
import keras.backend as K
import tensorflow as tf
def nms(heat, kernel=3):
hmax = tf.nn.max_pool2d(heat, (kernel, kernel), strides=1, padding='SAME')
heat = tf.where(tf.equal(hmax, heat), heat, tf.zeros_like(heat))
return heat
def topk(hm, max_objects=100):
hm = nms(hm)
# (b, h * w * c)
b, h, w, c = tf.shape(hm)[0], tf.shape(hm)[1], tf.shape(hm)[2], tf.shape(hm)[3]
hm = tf.reshape(hm, (b, -1))
# (b, k), (b, k)
scores, indices = tf.nn.top_k(hm, k=max_objects)
class_ids = indices % c
xs = indices // c % w
ys = indices // c // w
indices = ys * w + xs
return scores, indices, class_ids, xs, ys
def evaluate_batch_item(batch_item_detections, num_classes, max_objects_per_class=20, max_objects=100,
iou_threshold=0.5, score_threshold=0.1):
batch_item_detections = tf.boolean_mask(batch_item_detections,
tf.greater(batch_item_detections[:, 4], score_threshold))
detections_per_class = []
for cls_id in range(num_classes):
class_detections = tf.boolean_mask(batch_item_detections, tf.equal(batch_item_detections[:, 5], cls_id))
nms_keep_indices = tf.image.non_max_suppression(class_detections[:, :4],
class_detections[:, 4],
max_objects_per_class,
iou_threshold=iou_threshold)
class_detections = K.gather(class_detections, nms_keep_indices)
detections_per_class.append(class_detections)
batch_item_detections = K.concatenate(detections_per_class, axis=0)
def filter():
nonlocal batch_item_detections
_, indices = tf.nn.top_k(batch_item_detections[:, 4], k=max_objects)
batch_item_detections_ = tf.gather(batch_item_detections, indices)
return batch_item_detections_
def pad():
nonlocal batch_item_detections
batch_item_num_detections = tf.shape(batch_item_detections)[0]
batch_item_num_pad = tf.maximum(max_objects - batch_item_num_detections, 0)
batch_item_detections_ = tf.pad(tensor=batch_item_detections,
paddings=[
[0, batch_item_num_pad],
[0, 0]],
mode='CONSTANT',
constant_values=0.0)
return batch_item_detections_
batch_item_detections = tf.cond(tf.shape(batch_item_detections)[0] >= 100,
filter,
pad)
return batch_item_detections
def decode(hm, wh, reg, max_objects=100, nms=True, flip_test=False, num_classes=20, score_threshold=0.1):
if flip_test:
hm = (hm[0:1] + hm[1:2, :, ::-1]) / 2
wh = (wh[0:1] + wh[1:2, :, ::-1]) / 2
reg = reg[0:1]
scores, indices, class_ids, xs, ys = topk(hm, max_objects=max_objects)
b = tf.shape(hm)[0]
# (b, h * w, 2)
reg = tf.reshape(reg, (b, -1, tf.shape(reg)[-1]))
# (b, h * w, 2)
wh = tf.reshape(wh, (b, -1, tf.shape(wh)[-1]))
# (b, k, 2)
topk_reg = tf.gather(reg, indices, batch_dims=1)
# (b, k, 2)
topk_wh = tf.cast(tf.gather(wh, indices, batch_dims=1), tf.float32)
topk_cx = tf.cast(tf.expand_dims(xs, axis=-1), tf.float32) + topk_reg[..., 0:1]
topk_cy = tf.cast(tf.expand_dims(ys, axis=-1), tf.float32) + topk_reg[..., 1:2]
scores = tf.expand_dims(scores, axis=-1)
class_ids = tf.cast(tf.expand_dims(class_ids, axis=-1), tf.float32)
topk_x1 = topk_cx - topk_wh[..., 0:1] / 2
topk_x2 = topk_cx + topk_wh[..., 0:1] / 2
topk_y1 = topk_cy - topk_wh[..., 1:2] / 2
topk_y2 = topk_cy + topk_wh[..., 1:2] / 2
# (b, k, 6)
detections = tf.concat([topk_x1, topk_y1, topk_x2, topk_y2, scores, class_ids], axis=-1)
if nms:
detections = tf.map_fn(lambda x: evaluate_batch_item(x[0],
num_classes=num_classes,
score_threshold=score_threshold),
elems=[detections],
dtype=tf.float32)
return detections
def centernet(num_classes, backbone='resnet50', input_size=512, max_objects=100, score_threshold=0.1,
nms=True,
flip_test=False,
freeze_bn=True):
assert backbone in ['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152']
output_size = input_size // 4
image_input = Input(shape=(None, None, 3))
if backbone == 'resnet18':
resnet = resnet_models.ResNet18(image_input, include_top=False, freeze_bn=freeze_bn)
elif backbone == 'resnet34':
resnet = resnet_models.ResNet34(image_input, include_top=False, freeze_bn=freeze_bn)
elif backbone == 'resnet50':
resnet = resnet_models.ResNet50(image_input, include_top=False, freeze_bn=freeze_bn)
elif backbone == 'resnet101':
resnet = resnet_models.ResNet101(image_input, include_top=False, freeze_bn=freeze_bn)
else:
resnet = resnet_models.ResNet152(image_input, include_top=False, freeze_bn=freeze_bn)
# (b, 16, 16, 2048)
C5 = resnet.outputs[-1]
x = Dropout(rate=0.5)(C5)
# decoder
num_filters = 256
for i in range(3):
num_filters = num_filters // pow(2, i)
x = Conv2DTranspose(num_filters, (4, 4), strides=2, use_bias=False, padding='same',
kernel_initializer='he_normal',
kernel_regularizer=l2(5e-4))(x)
x = BatchNormalization()(x)
x = ReLU()(x)
# hm header
y1 = Conv2D(64, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x)
y1 = BatchNormalization()(y1)
y1 = ReLU()(y1)
y1 = Conv2D(num_classes, 1, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4), activation='sigmoid')(y1)
# wh header
y2 = Conv2D(64, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x)
y2 = BatchNormalization()(y2)
y2 = ReLU()(y2)
y2 = Conv2D(2, 1, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(y2)
# reg header
y3 = Conv2D(64, 3, padding='same', use_bias=False, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(x)
y3 = BatchNormalization()(y3)
y3 = ReLU()(y3)
y3 = Conv2D(2, 1, kernel_initializer='he_normal', kernel_regularizer=l2(5e-4))(y3)
detections = Lambda(lambda x: decode(*x,
max_objects=max_objects,
score_threshold=score_threshold,
nms=nms,
flip_test=flip_test,
num_classes=num_classes))([y1, y2, y3])
prediction_model = Model(inputs=image_input, outputs=detections)
return prediction_model