-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
1534 lines (1342 loc) · 61.2 KB
/
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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: model.py
import tensorflow as tf
from tensorpack.tfutils.scope_utils import auto_reuse_variable_scope
from tensorpack.tfutils import get_current_tower_context
from tensorpack.tfutils.summary import add_moving_summary
from tensorpack.tfutils.argscope import argscope
from tensorpack.tfutils.scope_utils import under_name_scope
from tensorpack.models import (
Conv2DTranspose, BNReLU, Conv2D, FullyConnected, GlobalAvgPooling, layer_register, Deconv2D, Dropout)
from utils.box_ops import pairwise_iou
import numpy as np
import config
import math
@under_name_scope()
def cls_loss(label_logits, label):
with tf.name_scope('cls_label_metrics'):
label_pred = tf.round(tf.nn.sigmoid(label_logits))
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(label_pred), tf.to_float(label)), tf.float32), name="accuracy")
add_moving_summary(accuracy)
label_loss = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.to_float(label), logits=label_logits)
label_loss = tf.reduce_mean(label_loss, name='label_loss')
return label_loss
@layer_register(log_shape=True)
def cls_head(feature):
feature = GlobalAvgPooling('gap', feature, data_format='NCHW')
fc1 = FullyConnected(
'fc1', feature, 1024,
W_init=tf.random_normal_initializer(stddev=0.01))
fc1 = Dropout(fc1)
fc2 = FullyConnected(
'fc2', fc1, 1,
W_init=tf.random_normal_initializer(stddev=0.01))
return tf.squeeze(fc2, [1])
@under_name_scope()
def clip_boxes(boxes, window, name=None):
"""
Args:
boxes: nx4, xyxy
window: [h, w]
"""
boxes = tf.maximum(boxes, 0.0)
m = tf.tile(tf.reverse(window, [0]), [2]) # (4,)
boxes = tf.minimum(boxes, tf.to_float(m), name=name)
return boxes
def rpn_head_FPN(prefix, featuremaps, channel, num_anchors):
"""
label_logits: layers xfHxfWxNA
box_logits: layers xfHxfWxNAx4
"""
rpn_label_logits = []
rpn_box_logits = []
is_training = get_current_tower_context().is_training
dropout_rate = 0.5 if not is_training else 0.0
with tf.variable_scope(prefix):
for layer in range(len(featuremaps)):
print('layer', layer)
with argscope(Conv2D, data_format='NCHW',
W_init=tf.random_normal_initializer(stddev=0.01)):
featuremap = featuremaps[layer]
#featuremap = tf.Print(featuremap, [tf.shape(featuremap)], message="FM_{}".format(layer))
hidden = Conv2D('conv0', featuremap, channel, 3, nl=tf.nn.relu, padding='SAME')
#hidden = tf.keras.layers.SpatialDropout2D(rate=dropout_rate, data_format='channels_first')(hidden)
label_logits = Conv2D('class', hidden, num_anchors, 1)
box_logits = Conv2D('box', hidden, 4 * num_anchors, 1)
# 1, NA(*4), im/16, im/16 (NCHW)
label_logits = tf.transpose(label_logits, [0, 2, 3, 1]) # 1xfHxfWxNA
label_logits = tf.squeeze(label_logits, 0) # fHxfWxNA
shp = tf.shape(box_logits) # 1x(NAx4)xfHxfW
box_logits = tf.transpose(box_logits, [0, 2, 3, 1]) # 1xfHxfWx(NAx4)
box_logits = tf.reshape(box_logits, tf.stack([shp[2], shp[3], num_anchors, 4])) # fHxfWxNAx4
#box_logits = tf.Print(box_logits, [tf.shape(box_logits)], message="rpn_box{}".format(layer))
rpn_label_logits.append(tf.reshape(label_logits, [-1]))
rpn_box_logits.append(tf.reshape(box_logits, [-1, 4]))
tf.get_variable_scope().reuse_variables()
return rpn_label_logits, rpn_box_logits
@layer_register(log_shape=True)
def rpn_head(featuremap, channel, num_anchors):
"""
Returns:
label_logits: fHxfWxNA
box_logits: fHxfWxNAx4
"""
with argscope(Conv2D, data_format='NCHW',
W_init=tf.random_normal_initializer(stddev=0.01)):
hidden = Conv2D('conv0', featuremap, channel, 3, nl=tf.nn.relu)
label_logits = Conv2D('class', hidden, num_anchors, 1)
box_logits = Conv2D('box', hidden, 4 * num_anchors, 1)
# 1, NA(*4), im/16, im/16 (NCHW)
label_logits = tf.transpose(label_logits, [0, 2, 3, 1]) # 1xfHxfWxNA
label_logits = tf.squeeze(label_logits, 0) # fHxfWxNA
shp = tf.shape(box_logits) # 1x(NAx4)xfHxfW
box_logits = tf.transpose(box_logits, [0, 2, 3, 1]) # 1xfHxfWx(NAx4)
box_logits = tf.reshape(box_logits, tf.stack([shp[2], shp[3], num_anchors, 4])) # fHxfWxNAx4
return label_logits, box_logits
@under_name_scope()
def rpn_losses(anchor_labels, anchor_boxes, label_logits, box_logits):
"""
Args:
anchor_labels: fHxfWxNA
anchor_boxes: fHxfWxNAx4, encoded
label_logits: fHxfWxNA
box_logits: fHxfWxNAx4
Returns:
label_loss, box_loss
"""
with tf.device('/cpu:0'):
valid_mask = tf.stop_gradient(tf.not_equal(anchor_labels, -1))
pos_mask = tf.stop_gradient(tf.equal(anchor_labels, 1))
nr_valid = tf.stop_gradient(tf.count_nonzero(valid_mask, dtype=tf.int32), name='num_valid_anchor')
nr_pos = tf.count_nonzero(pos_mask, dtype=tf.int32, name='num_pos_anchor')
valid_anchor_labels = tf.boolean_mask(anchor_labels, valid_mask)
valid_label_logits = tf.boolean_mask(label_logits, valid_mask)
with tf.name_scope('label_metrics'):
valid_label_prob = tf.nn.sigmoid(valid_label_logits)
summaries = []
with tf.device('/cpu:0'):
for th in [0.5, 0.2, 0.1]:
valid_prediction = tf.cast(valid_label_prob > th, tf.int32)
nr_pos_prediction = tf.reduce_sum(valid_prediction, name='num_pos_prediction')
pos_prediction_corr = tf.count_nonzero(
tf.logical_and(
valid_label_prob > th,
tf.equal(valid_prediction, valid_anchor_labels)),
dtype=tf.int32)
summaries.append(tf.truediv(
pos_prediction_corr,
nr_pos, name='recall_th{}'.format(th)))
precision = tf.to_float(tf.truediv(pos_prediction_corr, nr_pos_prediction))
precision = tf.where(tf.equal(nr_pos_prediction, 0), 0.0, precision, name='precision_th{}'.format(th))
summaries.append(precision)
add_moving_summary(*summaries)
label_loss = tf.nn.sigmoid_cross_entropy_with_logits(
labels=tf.to_float(valid_anchor_labels), logits=valid_label_logits)
label_loss = tf.reduce_mean(label_loss, name='label_loss')
"""
alpha = 0.25
gamma = 2
sigmoid_p = tf.nn.sigmoid(valid_label_logits)
#valid_labels = tf.cast(tf.one_hot(valid_anchor_labels, config.NUM_CLASS), tf.float32)
valid_labels = tf.cast(valid_anchor_labels, tf.float32)
zeros = tf.zeros_like(sigmoid_p, dtype=sigmoid_p.dtype)
pos_p_sub = tf.where(valid_labels >= sigmoid_p, valid_labels - sigmoid_p, zeros)
neg_p_sub = tf.where(valid_labels > zeros, zeros, sigmoid_p)
label_loss_focal = - alpha * (pos_p_sub ** gamma) * tf.log(tf.clip_by_value(sigmoid_p, 1e-8, 1.0)) \
- (1 - alpha) * (neg_p_sub ** gamma) * tf.log(tf.clip_by_value(1.0 - sigmoid_p, 1e-8, 1.0))
label_loss_focal = tf.reduce_mean(label_loss_focal)
label_loss = label_loss + label_loss_focal
"""
pos_anchor_boxes = tf.boolean_mask(anchor_boxes, pos_mask)
pos_box_logits = tf.boolean_mask(box_logits, pos_mask)
delta = 1.0 / 9
box_loss = tf.losses.huber_loss(
pos_anchor_boxes, pos_box_logits, delta=delta,
reduction=tf.losses.Reduction.SUM) / delta
box_loss = tf.div(
box_loss,
tf.cast(nr_valid, tf.float32), name='box_loss')
add_moving_summary(label_loss, box_loss, nr_valid, nr_pos)
return label_loss, box_loss
@under_name_scope()
def decode_bbox_target_FPN(box_predictions_FPN, anchors_FPN):
"""
Args:
box_predictions: [P2...P6][(..., 4)], logits
anchors: (..., 4), floatbox. Must have the same shape
Returns:
box_decoded: (..., 4), float32. With the same shape.
"""
layer_boxes = []
# P2,3,4,5,6
stride = config.FPN_STRIDES
for layer in range(len(box_predictions_FPN)):
box_predictions = box_predictions_FPN[layer]
anchors = anchors_FPN[layer]
orig_shape = tf.shape(anchors)
box_pred_txtytwth = tf.reshape(box_predictions, (-1, 2, 2))
box_pred_txty, box_pred_twth = tf.split(box_pred_txtytwth, 2, axis=1)
# each is (...)x1x2
anchors_x1y1x2y2 = tf.reshape(anchors, (-1, 2, 2))
anchors_x1y1, anchors_x2y2 = tf.split(anchors_x1y1x2y2, 2, axis=1)
waha = anchors_x2y2 - anchors_x1y1
xaya = (anchors_x2y2 + anchors_x1y1) * 0.5
wbhb = tf.exp(tf.minimum(
box_pred_twth, np.log(config.MAX_SIZE / stride[layer]))) * waha
xbyb = box_pred_txty * waha + xaya
x1y1 = xbyb - wbhb * 0.5
x2y2 = xbyb + wbhb * 0.5 # (...)x1x2
out = tf.concat([x1y1, x2y2], axis=-2)
layer_boxes.append(tf.reshape(out, orig_shape))
return layer_boxes
@under_name_scope()
def decode_bbox_target(box_predictions, anchors):
"""
Args:
box_predictions: (..., 4), logits
anchors: (..., 4), floatbox. Must have the same shape
Returns:
box_decoded: (..., 4), float32. With the same shape.
"""
orig_shape = tf.shape(anchors)
box_pred_txtytwth = tf.reshape(box_predictions, (-1, 2, 2))
box_pred_txty, box_pred_twth = tf.split(box_pred_txtytwth, 2, axis=1)
# each is (...)x1x2
anchors_x1y1x2y2 = tf.reshape(anchors, (-1, 2, 2))
anchors_x1y1, anchors_x2y2 = tf.split(anchors_x1y1x2y2, 2, axis=1)
waha = anchors_x2y2 - anchors_x1y1
xaya = (anchors_x2y2 + anchors_x1y1) * 0.5
wbhb = tf.exp(tf.minimum(
box_pred_twth, config.BBOX_DECODE_CLIP)) * waha
xbyb = box_pred_txty * waha + xaya
x1y1 = xbyb - wbhb * 0.5
x2y2 = xbyb + wbhb * 0.5 # (...)x1x2
out = tf.concat([x1y1, x2y2], axis=-2)
return tf.reshape(out, orig_shape)
@under_name_scope()
def encode_bbox_target(boxes, anchors):
"""
Args:
boxes: (..., 4), float32
anchors: (..., 4), float32
Returns:
box_encoded: (..., 4), float32 with the same shape.
"""
anchors_x1y1x2y2 = tf.reshape(anchors, (-1, 2, 2))
anchors_x1y1, anchors_x2y2 = tf.split(anchors_x1y1x2y2, 2, axis=1)
waha = anchors_x2y2 - anchors_x1y1
xaya = (anchors_x2y2 + anchors_x1y1) * 0.5
boxes_x1y1x2y2 = tf.reshape(boxes, (-1, 2, 2))
boxes_x1y1, boxes_x2y2 = tf.split(boxes_x1y1x2y2, 2, axis=1)
wbhb = boxes_x2y2 - boxes_x1y1
xbyb = (boxes_x2y2 + boxes_x1y1) * 0.5
# Note that here not all boxes are valid. Some may be zero
txty = (xbyb - xaya) / waha
twth = tf.log(wbhb / waha) # may contain -inf for invalid boxes
encoded = tf.concat([txty, twth], axis=1) # (-1x2x2)
return tf.reshape(encoded, tf.shape(boxes))
@under_name_scope()
def generate_rpn_proposals_FPN(boxesFPN, scoresFPN, img_shape):
"""
Args:
boxes: nx4 float dtype, decoded to floatbox already
scores: n float, the logits
img_shape: [h, w]
Returns:
boxes: kx4 float
scores: k logits
"""
#assert boxesFPN.shape.ndims == 2, boxesFPN.shape
if get_current_tower_context().is_training:
PRE_NMS_TOPK = 2000 # per FPN level
POST_NMS_TOPK = config.TRAIN_POST_NMS_TOPK # 2000
else:
PRE_NMS_TOPK = 1000
POST_NMS_TOPK = config.TEST_POST_NMS_TOPK
layer_topk = []
layer_topk_scores = []
for layer in range(len(boxesFPN)):
boxes = boxesFPN[layer]
scores = scoresFPN[layer]
topk = tf.minimum(PRE_NMS_TOPK, tf.size(scores))
topk_scores_per_layer, topk_indices = tf.nn.top_k(scores, k=topk, sorted=False)
topk_boxes_per_layer = tf.gather(boxes, topk_indices)
topk_boxes_per_layer = clip_boxes(topk_boxes_per_layer, img_shape)
layer_topk.append(topk_boxes_per_layer)
layer_topk_scores.append(topk_scores_per_layer)
# collect and flatten
topk_boxes = tf.concat(layer_topk, 0)
# topk_boxes = tf.Print(topk_boxes, [tf.shape(topk_boxes)], message="topk_boxes")
topk_scores = tf.concat(layer_topk_scores, 0)
# topk_scores = tf.Print(topk_scores, [tf.shape(topk_scores)], message="topk_scores")
topk_boxes_x1y1x2y2 = tf.reshape(topk_boxes, (-1, 2, 2))
topk_boxes_x1y1, topk_boxes_x2y2 = tf.split(topk_boxes_x1y1x2y2, 2, axis=1)
# nx1x2 each
wbhb = tf.squeeze(topk_boxes_x2y2 - topk_boxes_x1y1, axis=1)
valid = tf.reduce_all(wbhb > config.RPN_MIN_SIZE, axis=1) # n,
topk_valid_boxes_x1y1x2y2 = tf.boolean_mask(topk_boxes_x1y1x2y2, valid)
topk_valid_scores = tf.boolean_mask(topk_scores, valid)
topk_valid_boxes_y1x1y2x2 = tf.reshape(
tf.reverse(topk_valid_boxes_x1y1x2y2, axis=[2]),
(-1, 4), name='nms_input_boxes')
nms_indices = tf.image.non_max_suppression(
topk_valid_boxes_y1x1y2x2,
topk_valid_scores,
max_output_size=POST_NMS_TOPK,
iou_threshold=config.RPN_PROPOSAL_NMS_THRESH)
topk_valid_boxes = tf.reshape(topk_valid_boxes_x1y1x2y2, (-1, 4))
final_boxes = tf.gather(
topk_valid_boxes,
nms_indices, name='boxes')
final_scores = tf.gather(topk_valid_scores, nms_indices, name='scores')
tf.sigmoid(final_scores, name='probs') # for visualization
return final_boxes, final_scores
@under_name_scope()
def generate_rpn_proposals(boxes, scores, img_shape):
"""
Args:
boxes: nx4 float dtype, decoded to floatbox already
scores: n float, the logits
img_shape: [h, w]
Returns:
boxes: kx4 float
scores: k logits
"""
assert boxes.shape.ndims == 2, boxes.shape
if get_current_tower_context().is_training:
PRE_NMS_TOPK = config.TRAIN_PRE_NMS_TOPK
POST_NMS_TOPK = config.TRAIN_POST_NMS_TOPK
else:
PRE_NMS_TOPK = config.TEST_PRE_NMS_TOPK
POST_NMS_TOPK = config.TEST_POST_NMS_TOPK
topk = tf.minimum(PRE_NMS_TOPK, tf.size(scores))
topk_scores, topk_indices = tf.nn.top_k(scores, k=topk, sorted=False)
topk_boxes = tf.gather(boxes, topk_indices)
topk_boxes = clip_boxes(topk_boxes, img_shape)
topk_boxes_x1y1x2y2 = tf.reshape(topk_boxes, (-1, 2, 2))
topk_boxes_x1y1, topk_boxes_x2y2 = tf.split(topk_boxes_x1y1x2y2, 2, axis=1)
# nx1x2 each
wbhb = tf.squeeze(topk_boxes_x2y2 - topk_boxes_x1y1, axis=1)
valid = tf.reduce_all(wbhb > config.RPN_MIN_SIZE, axis=1) # n,
topk_valid_boxes_x1y1x2y2 = tf.boolean_mask(topk_boxes_x1y1x2y2, valid)
topk_valid_scores = tf.boolean_mask(topk_scores, valid)
topk_valid_boxes_y1x1y2x2 = tf.reshape(
tf.reverse(topk_valid_boxes_x1y1x2y2, axis=[2]),
(-1, 4), name='nms_input_boxes')
nms_indices = tf.image.non_max_suppression(
topk_valid_boxes_y1x1y2x2,
topk_valid_scores,
max_output_size=POST_NMS_TOPK,
iou_threshold=config.RPN_PROPOSAL_NMS_THRESH)
topk_valid_boxes = tf.reshape(topk_valid_boxes_x1y1x2y2, (-1, 4))
final_boxes = tf.gather(
topk_valid_boxes,
nms_indices, name='boxes')
final_scores = tf.gather(topk_valid_scores, nms_indices, name='scores')
tf.sigmoid(final_scores, name='probs') # for visualization
return final_boxes, final_scores
@under_name_scope()
def proposal_metrics(iou):
"""
Add summaries for RPN proposals.
Args:
iou: nxm, #proposal x #gt
"""
# find best roi for each gt, for summary only
best_iou = tf.reduce_max(iou, axis=0)
mean_best_iou = tf.reduce_mean(best_iou, name='best_iou_per_gt')
summaries = [mean_best_iou]
with tf.device('/cpu:0'):
for th in [0.3, 0.5]:
recall = tf.truediv(
tf.count_nonzero(best_iou >= th),
tf.size(best_iou, out_type=tf.int64),
name='recall_iou{}'.format(th))
summaries.append(recall)
add_moving_summary(*summaries)
@under_name_scope()
def sample_fast_rcnn_targets_FPN(boxes, gt_boxes, gt_labels, roi_resized):
"""
Sample some ROIs from all proposals for training.
Args:
boxes: nx4 region proposals, floatbox
gt_boxes: mx4, floatbox
gt_labels: m, int32
roi_resized: n*7*7*256
Returns:
sampled_boxes: tx4 floatbox, the rois
sampled_labels: t labels, in [0, #class-1]. Positive means foreground.
fg_inds_wrt_gt: #fg indices, each in range [0, m-1].
It contains the matching GT of each foreground roi.
"""
iou = pairwise_iou(boxes, gt_boxes) # nxm
proposal_metrics(iou)
# add ground truth as proposals as well
boxes = tf.concat([boxes, gt_boxes], axis=0) # (n+m) x 4
iou = tf.concat([iou, tf.eye(tf.shape(gt_boxes)[0])], axis=0) # (n+m) x m
# #proposal=n+m from now on
def sample_fg_bg(iou):
fg_mask = tf.reduce_max(iou, axis=1) >= config.FASTRCNN_FG_THRESH
fg_inds = tf.reshape(tf.where(fg_mask), [-1])
num_fg = tf.minimum(int(
config.FASTRCNN_BATCH_PER_IM * config.FASTRCNN_FG_RATIO),
tf.size(fg_inds), name='num_fg')
fg_inds = tf.random_shuffle(fg_inds)[:num_fg]
bg_inds = tf.reshape(tf.where(tf.logical_not(fg_mask)), [-1])
num_bg = tf.minimum(
config.FASTRCNN_BATCH_PER_IM - num_fg,
tf.size(bg_inds), name='num_bg')
bg_inds = tf.random_shuffle(bg_inds)[:num_bg]
add_moving_summary(num_fg, num_bg)
return fg_inds, bg_inds
fg_inds, bg_inds = sample_fg_bg(iou)
# fg,bg indices w.r.t proposals
best_iou_ind = tf.argmax(iou, axis=1) # #proposal, each in 0~m-1
fg_inds_wrt_gt = tf.gather(best_iou_ind, fg_inds) # num_fg
all_indices = tf.concat([fg_inds, bg_inds], axis=0) # indices w.r.t all n+m proposal boxes
ret_boxes = tf.gather(boxes, all_indices, name='sampled_proposal_boxes')
###
roi_resized = tf.gather(roi_resized, all_indices, name='sampled_roi_feature')
#roi_resized = tf.Print(roi_resized, [tf.shape(roi_resized)], name="roi_resized_sampled")
###
ret_labels = tf.concat(
[tf.gather(gt_labels, fg_inds_wrt_gt),
tf.zeros_like(bg_inds, dtype=tf.int64)], axis=0, name='sampled_labels')
# stop the gradient -- they are meant to be ground-truth
return tf.stop_gradient(ret_boxes), tf.stop_gradient(ret_labels), fg_inds_wrt_gt, roi_resized
@under_name_scope()
def sample_fast_rcnn_targets(boxes, gt_boxes, gt_labels):
"""
Sample some ROIs from all proposals for training.
Args:
boxes: nx4 region proposals, floatbox
gt_boxes: mx4, floatbox
gt_labels: m, int32
Returns:
sampled_boxes: tx4 floatbox, the rois
sampled_labels: t labels, in [0, #class-1]. Positive means foreground.
fg_inds_wrt_gt: #fg indices, each in range [0, m-1].
It contains the matching GT of each foreground roi.
"""
iou = pairwise_iou(boxes, gt_boxes) # nxm
proposal_metrics(iou)
# add ground truth as proposals as well
boxes = tf.concat([boxes, gt_boxes], axis=0) # (n+m) x 4
iou = tf.concat([iou, tf.eye(tf.shape(gt_boxes)[0])], axis=0) # (n+m) x m
# #proposal=n+m from now on
def sample_fg_bg(iou):
fg_mask = tf.reduce_max(iou, axis=1) >= config.FASTRCNN_FG_THRESH
fg_inds = tf.reshape(tf.where(fg_mask), [-1])
num_fg = tf.minimum(int(
config.FASTRCNN_BATCH_PER_IM * config.FASTRCNN_FG_RATIO),
tf.size(fg_inds), name='num_fg')
fg_inds = tf.random_shuffle(fg_inds)[:num_fg]
bg_inds = tf.reshape(tf.where(tf.logical_not(fg_mask)), [-1])
num_bg = tf.minimum(
config.FASTRCNN_BATCH_PER_IM - num_fg,
tf.size(bg_inds), name='num_bg')
bg_inds = tf.random_shuffle(bg_inds)[:num_bg]
add_moving_summary(num_fg, num_bg)
return fg_inds, bg_inds
fg_inds, bg_inds = sample_fg_bg(iou)
# fg,bg indices w.r.t proposals
best_iou_ind = tf.argmax(iou, axis=1) # #proposal, each in 0~m-1
fg_inds_wrt_gt = tf.gather(best_iou_ind, fg_inds) # num_fg
all_indices = tf.concat([fg_inds, bg_inds], axis=0) # indices w.r.t all n+m proposal boxes
#all_inds_wrt_gt = tf.gather(best_iou_ind, all_indices)
ret_boxes = tf.gather(boxes, all_indices, name='sampled_proposal_boxes')
ret_labels = tf.concat(
[tf.gather(gt_labels, fg_inds_wrt_gt),
tf.zeros_like(bg_inds, dtype=tf.int64)], axis=0, name='sampled_labels')
# stop the gradient -- they are meant to be ground-truth
return tf.stop_gradient(ret_boxes), tf.stop_gradient(ret_labels), tf.stop_gradient(fg_inds_wrt_gt)
@under_name_scope()
def sample_fast_rcnn_targets_RELATION(boxes, gt_boxes, gt_labels):
iou = pairwise_iou(boxes, gt_boxes) # nxm
proposal_metrics(iou)
nongt = tf.zeros(tf.expand_dims(tf.shape(boxes)[0], 0))
gt = tf.ones(tf.expand_dims(tf.shape(gt_boxes)[0], 0))
gt_nongt = tf.concat([nongt, gt], axis=0)
proposal_boxes_num = tf.shape(boxes)[0]
#-------------------
if not config.SAMPLING:
# use all proposal boxes
fg_mask = tf.reduce_max(iou, axis=1) >= config.FASTRCNN_FG_THRESH
fg_inds = tf.reshape(tf.where(fg_mask), [-1])
bg_inds = tf.reshape(tf.where(tf.logical_not(fg_mask)), [-1])
num_fg = tf.size(fg_inds, name="num_fg")
num_bg = tf.size(bg_inds, name="num_bg")
add_moving_summary(num_fg, num_bg)
else:
#-------------------
# sample 3:1 rate
def sample_fg_bg(iou):
fg_mask = tf.reduce_max(iou, axis=1) >= config.FASTRCNN_FG_THRESH
fg_inds = tf.reshape(tf.where(fg_mask), [-1])
num_fg = tf.minimum(int(
config.FASTRCNN_BATCH_PER_IM * config.FASTRCNN_FG_RATIO),
tf.size(fg_inds), name='num_fg')
fg_inds = tf.random_shuffle(fg_inds)[:num_fg]
bg_inds = tf.reshape(tf.where(tf.logical_not(fg_mask)), [-1])
num_bg = tf.minimum(
config.FASTRCNN_BATCH_PER_IM - num_fg,
tf.size(bg_inds), name='num_bg')
bg_inds = tf.random_shuffle(bg_inds)[:num_bg]
add_moving_summary(num_fg, num_bg)
return fg_inds, bg_inds
fg_inds, bg_inds = sample_fg_bg(iou)
#nongt_indices = tf.concat([fg_inds, bg_inds], axis=0)
#sampled_proposal_scores = tf.gather(proposal_scores, nongt_indices)
#---------------------
boxes = tf.concat([boxes, gt_boxes], axis=0) # (n+m) x 4
iou = tf.concat([iou, tf.eye(tf.shape(gt_boxes)[0])], axis=0) # (n+m) x m
gt_inds = tf.cast(tf.range(proposal_boxes_num, tf.shape(boxes)[0], dtype=tf.int32), tf.int64)
fg_inds = tf.concat([fg_inds, gt_inds], axis=0)
best_iou_ind = tf.argmax(iou, axis=1) # #proposal, each in 0~m-1
fg_inds_wrt_gt = tf.gather(best_iou_ind, fg_inds) # num_fg
all_indices = tf.concat([fg_inds, bg_inds], axis=0) # indices w.r.t all n+m proposal boxes
all_inds_wrt_gt = tf.gather(best_iou_ind, all_indices)
ret_boxes = tf.gather(boxes, all_indices)
ret_labels = tf.concat(
[tf.gather(gt_labels, fg_inds_wrt_gt),
tf.zeros_like(bg_inds, dtype=tf.int64)], axis=0)
gt_nongt_after_sampling = tf.gather(gt_nongt, all_indices)
nongt_after_sampling = tf.reshape(tf.where(tf.equal(gt_nongt_after_sampling, 0)), [-1])
gt_after_sampling = tf.reshape(tf.where(tf.equal(gt_nongt_after_sampling, 1)), [-1])
gt_nongt_wrt_retboxes = tf.concat([nongt_after_sampling, gt_after_sampling], axis=0)
ret_boxes = tf.gather(ret_boxes, gt_nongt_wrt_retboxes, name='sampled_proposal_boxes')
ret_labels = tf.gather(ret_labels, gt_nongt_wrt_retboxes, name='sampled_labels')
# stop the gradient -- they are meant to be ground-truth
return tf.stop_gradient(ret_boxes), tf.stop_gradient(ret_labels), tf.stop_gradient(fg_inds_wrt_gt), tf.stop_gradient(all_inds_wrt_gt)
@under_name_scope()
def crop_and_resize(image, boxes, box_ind, crop_size):
"""
Better-aligned version of tf.image.crop_and_resize, following our definition of floating point boxes.
Args:
image: NCHW
boxes: nx4, x1y1x2y2
box_ind: (n,)
crop_size (int):
Returns:
n,C,size,size
"""
assert isinstance(crop_size, int), crop_size
@under_name_scope()
def transform_fpcoor_for_tf(boxes, image_shape, crop_shape):
"""
The way tf.image.crop_and_resize works (with normalized box):
Initial point (the value of output[0]): x0_box * (W_img - 1)
Spacing: w_box * (W_img - 1) / (W_crop - 1)
Use the above grid to bilinear sample.
However, what we want is (with fpcoor box):
Spacing: w_box / W_crop
Initial point: x0_box + spacing/2 - 0.5
(-0.5 because bilinear sample assumes floating point coordinate (0.0, 0.0) is the same as pixel value (0, 0))
This function transform fpcoor boxes to a format to be used by tf.image.crop_and_resize
Returns:
y1x1y2x2
"""
x0, y0, x1, y1 = tf.split(boxes, 4, axis=1)
spacing_w = (x1 - x0) / tf.to_float(crop_shape[1])
spacing_h = (y1 - y0) / tf.to_float(crop_shape[0])
nx0 = (x0 + spacing_w / 2 - 0.5) / tf.to_float(image_shape[1] - 1)
ny0 = (y0 + spacing_h / 2 - 0.5) / tf.to_float(image_shape[0] - 1)
nw = spacing_w * tf.to_float(crop_shape[1] - 1) / tf.to_float(image_shape[1] - 1)
nh = spacing_h * tf.to_float(crop_shape[0] - 1) / tf.to_float(image_shape[0] - 1)
return tf.concat([ny0, nx0, ny0 + nh, nx0 + nw], axis=1)
image_shape = tf.shape(image)[2:]
boxes = transform_fpcoor_for_tf(boxes, image_shape, [crop_size, crop_size])
image = tf.transpose(image, [0, 2, 3, 1]) # 1hwc
ret = tf.image.crop_and_resize(
image, boxes, box_ind,
crop_size=[crop_size, crop_size])
ret = tf.transpose(ret, [0, 3, 1, 2]) # ncss
return ret
@under_name_scope()
def roi_align_FPN(featuremaps, boxes, output_shape):
"""
Args:
featuremap: [1xCxHxW] * 5 P6~P2
boxes: Nx4 floatbox
output_shape: int
Returns:
NxCxoHxoW
"""
def tf_log2(x):
return tf.log(x) / tf.log(2.0)
# feature map [P6, P5, P4, P3, P2]
boxes = tf.stop_gradient(tf.expand_dims(boxes, 0)) # TODO
x1, y1, x2, y2 = tf.split(boxes, 4, axis=2) # num * x1y1x2y2
w = tf.maximum(x2 - x1, 0)
h = tf.maximum(y2 - y1, 0)
roi_level = tf_log2(tf.sqrt(h * w + 1e-8) / (224.0))
roi_level = tf.minimum(5, tf.maximum(
2, 4 + tf.cast(tf.round(roi_level), tf.int32)))
roi_level = tf.squeeze(roi_level, 2)
roi_level = tf.stop_gradient(roi_level)
#roi_level = tf.Print(roi_level, [roi_level], message="This is roi_level: ")
# limit to P5 ~ P2
pooled = tf.zeros([0, 256, output_shape, output_shape])
inds = tf.cast(tf.zeros([0, 1]), tf.int32)
proposals = []
strides = [4., 8., 16., 32.]
for level in range(2, 6):
featuremap_to_crop = featuremaps[4 - (level - 2)]
# order : P6(idx 4) ~ P2(idx0) => idx shift
# P2 -> f[4], P3 -> f[3], P4 -> f[2], P5 -> f[1]
id_for_box_wrt_level = tf.where(tf.equal(roi_level, level))
level_boxes = tf.gather_nd(boxes, id_for_box_wrt_level)
box_indices = tf.reshape(tf.cast(id_for_box_wrt_level[:,1], tf.int32), [-1, 1])
#level_boxes = tf.Print(level_boxes, [tf.shape(level_boxes)], message="level_boxes_{}".format(level))
level_boxes = tf.stop_gradient(level_boxes)
box_indices = tf.stop_gradient(box_indices)
#box_indices = tf.Print(box_indices, [tf.shape(box_indices), box_indices], message="box_indices: ")
def ff_true(level_boxes, level, featuremap_to_crop, pooled, box_indices, inds):
level_boxes = level_boxes * (1.0 / strides[level - 2])
ret = crop_and_resize(
featuremap_to_crop, level_boxes,
tf.zeros([tf.shape(level_boxes)[0]], dtype=tf.int32),
output_shape * 2)
ret = tf.nn.avg_pool(ret, [1, 1, 2, 2], [1, 1, 2, 2], padding='SAME', data_format='NCHW')
pooled_return = tf.cond(tf.size(pooled) > 0, lambda: tf.concat([pooled, ret], axis=0), lambda: ret)
inds_return = tf.cond(tf.size(inds) > 0, lambda: tf.concat([inds, box_indices], axis=0), lambda: box_indices)
pooled_return = tf.identity(pooled_return)
inds_return = tf.identity(inds_return)
### identity to prevent tf.cond cause summary crash
return pooled_return, inds_return
def ff_false(pooled, inds):
#ret = tf.cast(tf.zeros([0, 256, 7, 7]), tf.float32)
return tf.identity(pooled), tf.identity(inds)
pooled, inds = tf.cond(tf.size(level_boxes) > 0,
lambda: ff_true(level_boxes, level,
featuremap_to_crop, pooled,
box_indices, inds),
lambda: ff_false(pooled, inds))
#pooled = tf.concat(pooled, axis=0)
inds = tf.reshape(inds, [-1, 1])
print(inds.get_shape())
pooled = tf.scatter_nd(inds, pooled, tf.shape(pooled))
#pooled = tf.Print(pooled, [tf.shape(pooled)], message="pooled: ")
#inds = tf.Print(inds, [tf.shape(inds), inds], message="inds: ")
print(pooled.get_shape())
# Find a way to get id back to original shape to match label
# Or gather label to match box
return pooled
@under_name_scope()
def roi_align(featuremap, boxes, output_shape):
"""
Args:
featuremap: 1xCxHxW
boxes: Nx4 floatbox
output_shape: int
Returns:
NxCxoHxoW
"""
boxes = tf.stop_gradient(boxes) # TODO
# sample 4 locations per roi bin
ret = crop_and_resize(
featuremap, boxes,
tf.zeros([tf.shape(boxes)[0]], dtype=tf.int32),
output_shape * 2)
ret = tf.nn.avg_pool(ret, [1, 1, 2, 2], [1, 1, 2, 2], padding='SAME', data_format='NCHW')
return ret
@layer_register(log_shape=True)
def fastrcnn_head_RELATION(feature, position_embedding, nongt_dim, num_classes):
with argscope([Conv2D], data_format='NCHW'):
fc1 = Conv2D('rcnn_fc1', feature, 1024, 7, nl=BNReLU, padding='VALID') #1*1
attention_1 = attention_module_multi_head('att1', fc1, position_embedding,
nongt_dim=nongt_dim, fc_dim=16, feat_dim=1024,
index=1, group=16,
dim=(1024, 1024, 1024))
fc1 = tf.nn.relu(fc1 + attention_1)
fc2 = Conv2D('rcnn_fc2', fc1, 1024, 1, nl=BNReLU, padding='SAME')
attention_2 = attention_module_multi_head('att2', fc2, position_embedding,
nongt_dim=nongt_dim, fc_dim=16, feat_dim=1024,
index=1, group=16,
dim=(1024, 1024, 1024))
fc2 = tf.nn.relu(fc2 + attention_2)
fc2 = tf.squeeze(fc2, [2, 3])
# 1*1024*1*1
classification = FullyConnected(
'class', fc2, num_classes,
W_init=tf.random_normal_initializer(stddev=0.01))
box_regression = FullyConnected(
'box', fc2, (num_classes - 1) * 4,
W_init=tf.random_normal_initializer(stddev=0.001))
box_regression = tf.reshape(box_regression, (-1, num_classes - 1, 4))
return classification, box_regression
@layer_register(log_shape=True)
def fastrcnn_head_FPN(feature, num_classes, class_agnostic_regression=False):
with argscope([Conv2D], data_format='NCHW'):
fc1 = Conv2D('rcnn_fc1', feature, 1024, 7, nl=BNReLU, padding='VALID') #1*1
fc2 = Conv2D('rcnn_fc2', fc1, 1024, 1, nl=BNReLU, padding='SAME')
fc2 = tf.squeeze(fc2, [2, 3])
# 1*1024*1*1
classification = FullyConnected(
'class', fc2, num_classes,
W_init=tf.random_normal_initializer(stddev=0.01))
num_classes_for_box = 1 if class_agnostic_regression else num_classes
box_regression = FullyConnected(
'box', fc2, num_classes_for_box * 4,
W_init=tf.random_normal_initializer(stddev=0.001))
box_regression = tf.reshape(box_regression, (-1, num_classes_for_box, 4))
return classification, box_regression
@layer_register(log_shape=True)
def fastrcnn_head(feature, num_classes, class_agnostic_regression=False):
"""
Args:
feature (NxCx7x7):
num_classes(int): num_category + 1
Returns:
cls_logits (Nxnum_class), reg_logits (Nx num_class-1 x 4)
"""
feature = GlobalAvgPooling('gap', feature, data_format='NCHW')
classification = FullyConnected(
'class', feature, num_classes,
W_init=tf.random_normal_initializer(stddev=0.01))
num_classes_for_box = 1 if class_agnostic_regression else num_classes
box_regression = FullyConnected(
'box', feature, num_classes_for_box * 4,
W_init=tf.random_normal_initializer(stddev=0.001))
box_regression = tf.reshape(box_regression, (-1, num_classes_for_box, 4))
return classification, box_regression
def smooth_l1(labels, predictions, delta=1.0):
error = tf.subtract(predictions, labels)
abs_error = tf.abs(error)
quadratic = tf.minimum(abs_error, delta)
linear = tf.subtract(abs_error, quadratic)
losses = tf.add(
tf.multiply(
tf.convert_to_tensor(0.5, dtype=quadratic.dtype),
tf.multiply(quadratic, quadratic)),
tf.multiply(delta, linear))
return losses
@under_name_scope()
def fastrcnn_losses_OHEM(labels, label_logits, boxes, box_logits):
"""
Args:
labels: n,
label_logits: nxC
boxes: nx4, encoded
box_logits: nx(C-1)x4
We need to make sure only correct class have non-zero class regression weight
"""
print(box_logits.shape)
fg_inds = tf.where(labels > 0)[:, 0]
fg_labels = tf.gather(labels, fg_inds)
num_fg = tf.size(fg_inds, out_type=tf.int64)
empty_fg = tf.equal(num_fg, 0)
with tf.name_scope('label_metrics'), tf.device('/cpu:0'):
prediction = tf.argmax(label_logits, axis=1, name='label_prediction')
correct = tf.to_float(tf.equal(prediction, labels)) # boolean/integer gather is unavailable on GPU
accuracy = tf.reduce_mean(correct, name='accuracy')
fg_label_pred = tf.argmax(tf.gather(label_logits, fg_inds), axis=1)
num_zero = tf.reduce_sum(tf.to_int64(tf.equal(fg_label_pred, 0)), name='num_zero')
false_negative = tf.where(
empty_fg, 0., tf.to_float(tf.truediv(num_zero, num_fg)), name='false_negative')
fg_accuracy = tf.where(
empty_fg, 0., tf.reduce_mean(tf.gather(correct, fg_inds)), name='fg_accuracy')
per_roi_label_loss = tf.stop_gradient(tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=label_logits))
# set bg weight to zero
bbox_weight = tf.scatter_nd(tf.cast(fg_inds, tf.int32), tf.ones_like(fg_inds), shape=tf.shape(labels))
#bbox_weight = tf.where(labels > 0, tf.ones_like(labels), tf.zeros_like(labels))
# set only correct class have regression target
if int(box_logits.shape[1]) > 1:
indices = tf.stack(
[tf.range(tf.shape(labels)[0]),
tf.to_int32(labels) - 1], axis=1) # #fgx2
box_target = tf.gather_nd(box_logits, indices) # (num_fg_roi, 4)
else:
box_target = tf.reshape(box_logits, [-1, 4])
per_roi_box_loss = tf.cast(bbox_weight, tf.float32) * tf.reduce_sum(smooth_l1(boxes, box_target), axis=1)
#per_roi_box_loss = tf.reduce_sum(per_roi_box_loss, axis=1)
per_roi_total_loss = tf.stop_gradient(per_roi_box_loss + per_roi_label_loss)
top_k_loss, top_k_indice = tf.nn.top_k(per_roi_total_loss, k=128)
top_k_indice = tf.stop_gradient(top_k_indice)
#===========
label_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=label_logits)
box_loss = tf.cast(bbox_weight, tf.float32) * tf.reduce_sum(smooth_l1(boxes, box_target), axis=1)
label_loss = tf.gather(label_loss, top_k_indice)
box_loss = tf.gather(box_loss, top_k_indice)
label_loss = tf.reduce_mean(label_loss, name='label_loss')
box_loss = tf.truediv(tf.reduce_sum(box_loss), tf.to_float(tf.shape(top_k_indice)[0]), name='box_loss')
add_moving_summary(label_loss, box_loss, accuracy, fg_accuracy, false_negative, tf.to_float(num_fg, name='num_fg_label'))
return label_loss, box_loss
@under_name_scope()
def fastrcnn_losses(labels, label_logits, fg_boxes, fg_box_logits):
"""
Args:
labels: n,
label_logits: nxC
fg_boxes: nfgx4, encoded
fg_box_logits: nfgx(C-1)x4
"""
label_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
labels=labels, logits=label_logits)
label_loss = tf.reduce_mean(label_loss, name='label_loss')
fg_inds = tf.where(labels > 0)[:, 0]
fg_labels = tf.gather(labels, fg_inds)
num_fg = tf.size(fg_inds, out_type=tf.int64)
empty_fg = tf.equal(num_fg, 0)
if int(fg_box_logits.shape[1]) > 1:
indices = tf.stack(
[tf.range(tf.shape(fg_labels)[0]),
tf.to_int32(fg_labels) - 1], axis=1) # #fgx2
fg_box_logits = tf.gather_nd(fg_box_logits, indices) # (num_fg_roi, 4)
else:
fg_box_logits = tf.reshape(fg_box_logits, [-1, 4])
#indices = tf.stack(
# [tf.range(num_fg),
# tf.to_int32(fg_labels) - 1], axis=1) # #fgx2
#fg_box_logits = tf.gather_nd(fg_box_logits, indices)
with tf.name_scope('label_metrics'), tf.device('/cpu:0'):
prediction = tf.argmax(label_logits, axis=1, name='label_prediction')
correct = tf.to_float(tf.equal(prediction, labels)) # boolean/integer gather is unavailable on GPU
accuracy = tf.reduce_mean(correct, name='accuracy')
fg_label_pred = tf.argmax(tf.gather(label_logits, fg_inds), axis=1)
num_zero = tf.reduce_sum(tf.to_int64(tf.equal(fg_label_pred, 0)), name='num_zero')
false_negative = tf.where(
empty_fg, 0., tf.to_float(tf.truediv(num_zero, num_fg)), name='false_negative')
fg_accuracy = tf.where(
empty_fg, 0., tf.reduce_mean(tf.gather(correct, fg_inds)), name='fg_accuracy')
box_loss = tf.losses.huber_loss(
fg_boxes, fg_box_logits, reduction=tf.losses.Reduction.SUM)
box_loss = tf.truediv(
box_loss, tf.to_float(tf.shape(labels)[0]), name='box_loss')
add_moving_summary(label_loss, box_loss, accuracy, fg_accuracy, false_negative, tf.to_float(num_fg, name='num_fg_label'))
return label_loss, box_loss
@under_name_scope()
def rpn_predictions(boxes, probs):
print(boxes.shape) #5,1,4
print(probs.shape) #5 ? ? 7
boxes = tf.transpose(boxes, [1, 0, 2]) # #catxnx4
probs = tf.transpose(probs[:, 1:], [1, 0]) # #catxn
def f(X):
"""
prob: n probabilities
box: nx4 boxes
Returns: n boolean, the selection
"""
prob, box = X
output_shape = tf.shape(prob)
# filter by score threshold
ids = tf.reshape(tf.where(prob > config.RESULT_SCORE_THRESH), [-1])
prob = tf.gather(prob, ids)
box = tf.gather(box, ids)
# NMS within each class
selection = tf.image.non_max_suppression(
box, prob, config.RESULTS_PER_IM, config.FASTRCNN_NMS_THRESH)
selection = tf.to_int32(tf.gather(ids, selection))
# sort available in TF>1.4.0
# sorted_selection = tf.contrib.framework.sort(selection, direction='ASCENDING')
sorted_selection = -tf.nn.top_k(-selection, k=tf.size(selection))[0]
mask = tf.sparse_to_dense(
sparse_indices=sorted_selection,
output_shape=output_shape,
sparse_values=True,
default_value=False)
return mask
masks = tf.map_fn(f, (probs, boxes), dtype=tf.bool,
parallel_iterations=10) # #cat x N
selected_indices = tf.where(masks) # #selection x 2, each is (cat_id, box_id)
probs = tf.boolean_mask(probs, masks)
# filter again by sorting scores
topk_probs, topk_indices = tf.nn.top_k(
probs,
tf.minimum(config.RESULTS_PER_IM, tf.size(probs)),
sorted=False)
filtered_selection = tf.gather(selected_indices, topk_indices)
filtered_selection = tf.reverse(filtered_selection, axis=[1], name='filtered_indices')
return filtered_selection, topk_probs
def np_soft_nms(dets, thresh, max_dets, score_thres=0.5):
if dets.shape[0] == 0:
return np.zeros((0, 5))
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]