-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCliqueFPN.py
1360 lines (1161 loc) · 73.6 KB
/
CliqueFPN.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
from detection_dataloader import create_training_instances,BatchGenerator
import tensorflow as tf
from tensorflow.python.ops import array_ops
import numpy as np
Detection_or_Classifier = 'detection'#'detection','classifier'
class CliqueFPN():
def __init__(self,num_classes,num_anchors,batch_size,max_box_per_image,max_grid,ignore_thresh=0.6,learning_rate=0.0001):
self.num_classes = num_classes
self.num_anchors = num_anchors
self.batch_size = batch_size
self.max_box_per_image = max_box_per_image
self.max_grid = max_grid
self.ignore_thresh = ignore_thresh
self.learning_rate = learning_rate
self.__build()
def __build(self):
self.norm = 'group_norm'#group_norm,batch_norm
self.activate = 'prelu'#selu,leaky,swish,relu,relu6,prelu
self.num_block = {'1':6,'2':7,'3':7,'4':7}#4,6,6,6 6,7,7,7
self.K = {'1':48,'2':48,'3':48,'4':48}
self.use_decoupled = False
self.use_deformconv = True
self.__init_global_epoch()
self.__init_global_step()
self.__init_input()
with tf.variable_scope('zsc_feature'):
red, green, blue = tf.split(self.input_image, num_or_size_splits=3, axis=3)
x = tf.concat([tf.multiply(tf.subtract(blue,127.5),0.0078125),
tf.multiply(tf.subtract(green,127.5),0.0078125),
tf.multiply(tf.subtract(red,127.5),0.0078125)], 3)
#none,none,none,3
x0 = PrimaryConv('PrimaryConv',self.input_image,self.use_decoupled,self.norm,self.activate,self.is_training)
#none,none/2,none/2,64
x = CliqueBlock('CliqueBlock_1',x0,self.num_block['1'],self.K['1'],self.use_decoupled,self.norm,self.activate,self.is_training,False)
#none,none/4,none/4,K['1']*num_block['1']
skip_1 = tf.concat([x0,x],axis=-1)
# none,none/4,none/4,K['1']*(num_block['1']+1)
x0 = Transition('Transition_1',skip_1,self.use_decoupled,self.norm,self.activate,self.is_training)
#none,none/8,none/8,K['1']*num_block['1']
x = CliqueBlock('CliqueBlock_2',x0,self.num_block['2'],self.K['2'],self.use_decoupled,self.norm,self.activate,self.is_training,False)
#none,none/8,none/8,K['2']*num_block['2']
skip_2 = tf.concat([x0,x],axis=-1)
#none,none/8,none/8,K['2']*(num_block['2']+1)
x0 = Transition('Transition_2',skip_2,self.use_decoupled,self.norm,self.activate,self.is_training)
#none,none/16,none/16,K['2']*num_block['2']
x = CliqueBlock('CliqueBlock_3',x0,self.num_block['3'],self.K['3'],self.use_decoupled,self.norm,self.activate,self.is_training,self.use_deformconv)
#none,none/16,none/16,K['3']*num_block['3']
skip_3 = tf.concat([x0,x],axis=-1)
#none,none/16,none/16,K['3']*(num_block['3']+1)
x0 = Transition('Transition_3',skip_3,self.use_decoupled,self.norm,self.activate,self.is_training)
#none,none/32,none/32,K['3']*num_block['3']
x = CliqueBlock('CliqueBlock_4',x0,self.num_block['4'],self.K['4'],self.use_decoupled,self.norm,self.activate,self.is_training,self.use_deformconv)
#none,none/32,none/32,K['4']*num_block['4']
skip_4 = tf.concat([x0,x],axis=-1)
#none,none/32,none/32,K['4']*(num_block['4']+1)
if Detection_or_Classifier=='classifier':
with tf.variable_scope('zsc_classifier'):
skip_4 = bn(skip_4, self.is_training, name='skip_4_BN')
x4 = tf.reduce_mean(skip_4,[1,2],keep_dims=True)
global_pool = x4
self.classifier_logits = tf.reshape(_conv_block('Logits',global_pool,self.num_classes,1,1,'SAME',False,self.norm,self.activate,self.is_training),
[tf.shape(global_pool)[0],self.num_classes])
elif Detection_or_Classifier=='detection':
with tf.variable_scope('zsc_detection'):
with tf.variable_scope('zsc_conv_transpose'):
x = tf.depth_to_space(skip_4,2)
x = tf.concat([x,skip_3],axis=-1)
x = _conv_block('Conv_1',x,skip_3.get_shape().as_list()[-1]//2,1,1,'SAME',False,self.norm,self.activate,self.is_training)
x = _conv_block('Conv_2',x,skip_3.get_shape().as_list()[-1]//2,3,1,'SAME',False,self.norm,self.activate,self.is_training)
x = _conv_block('Conv_3',x,skip_3.get_shape().as_list()[-1],1,1,'SAME',False,self.norm,self.activate,self.is_training)
pred1_x = x*(1+_SE('Gate1',x,False,None,self.activate,self.is_training))
x = tf.depth_to_space(x,2)
x = tf.concat([x,skip_2],axis=-1)
x = _conv_block('Conv_4',x,skip_2.get_shape().as_list()[-1]//2,1,1,'SAME',False,self.norm,self.activate,self.is_training)
x = _conv_block('Conv_5',x,skip_2.get_shape().as_list()[-1]//2,3,1,'SAME',False,self.norm,self.activate,self.is_training)
x = _conv_block('Conv_6',x,skip_2.get_shape().as_list()[-1],1,1,'SAME',False,self.norm,self.activate,self.is_training)
pred2_x = x*(1+_SE('Gate2',x,False,None,self.activate,self.is_training))
x = tf.depth_to_space(x,2)
x = tf.concat([x,skip_1],axis=-1)
x = _conv_block('Conv_7',x,skip_1.get_shape().as_list()[-1]//2,1,1,'SAME',False,self.norm,self.activate,self.is_training)
x = _conv_block('Conv_8',x,skip_1.get_shape().as_list()[-1]//2,3,1,'SAME',False,self.norm,self.activate,self.is_training)
x = _conv_block('Conv_9',x,skip_1.get_shape().as_list()[-1],1,1,'SAME',False,self.norm,self.activate,self.is_training)
pred3_x = x*(1+_SE('Gate3',x,False,None,self.activate,self.is_training))
with tf.variable_scope('zsc_pred'):
self.pred_yolo_1 = _conv_block('Conv_1',pred1_x,self.num_anchors*(5+self.num_classes),1,1,'SAME',False,None,None,self.is_training)
#none,none/32,none/32,5*(5+self.num_classes)
self.pred_yolo_2 = _conv_block('Conv_2',pred2_x,self.num_anchors*(5+self.num_classes),1,1,'SAME',False,None,None,self.is_training)
#none,none/16,none/16,5*(5+self.num_classes)
self.pred_yolo_3 = _conv_block('Conv_3',pred3_x,self.num_anchors*(5+self.num_classes),1,1,'SAME',False,None,None,self.is_training)
#none,none/8,none/8,5*(5+self.num_classes)
self.__init__output()
if Detection_or_Classifier=='classifier':
pass
elif Detection_or_Classifier=='detection':
self.__prob()
def __prob(self):
def decode_pred(pred,anchors,net_factor,obj_thresh=0.6):
pred_list = tf.split(tf.expand_dims(pred,axis=3),num_or_size_splits=self.num_anchors,axis=4)#[(none,none/n,none/n,1,C//3),...]
pred = tf.concat(pred_list,axis=3)#none,none/n,none/n,3,C//3
max_grid_h, max_grid_w = self.max_grid
cell_x = tf.to_float(tf.reshape(tf.tile(tf.range(max_grid_w), [max_grid_h]), (1, max_grid_h, max_grid_w, 1, 1)))
cell_y = tf.transpose(cell_x, (0,2,1,3,4))
cell_grid = tf.tile(tf.concat([cell_x,cell_y],-1), [1, 1, 1, self.num_anchors, 1])#(none,max_grid_h,max_grid_w,3,2)
grid_h = tf.shape(pred)[1]
grid_w = tf.shape(pred)[2]
grid_factor = tf.reshape(tf.cast([grid_w, grid_h], tf.float32), [1,1,1,1,2])
anchors = tf.reshape(tf.constant(anchors,dtype=tf.float32),[1,1,1,-1,2])*net_factor/self.original_wh#1,1,1,3,2
#################################################################################################
pred_xy = ((cell_grid[:,:grid_h,:grid_w,:,:] + tf.sigmoid(pred[...,:2]))/grid_factor)#none,grid_h,grid_w,3,2 每个网格内偏移后的框并归一化
pred_wh = anchors*tf.exp(pred[...,2:4])/net_factor#none,grid_h,grid_w,3,2 每个网格的宽高
pred_min_xy = pred_xy-pred_wh/2.0
pred_max_xy = pred_xy+pred_wh/2.0
pred_conf = tf.expand_dims(tf.sigmoid(pred[...,4]),axis=-1)#none,grid_h,grid_w,3,1
pred_class = pred_conf*tf.sigmoid(pred[...,5:])#none,grid_h,grid_w,3,self.num_classes
pred_class *= tf.cast(pred_class>obj_thresh,tf.float32)#none,grid_h,grid_w,3,self.num_classes
pred = tf.concat([pred_min_xy,pred_max_xy,pred_conf,pred_class],axis=-1)#none,grid_h,grid_w,3,4+1+self.num_classes
#!!目前这个程序只能对单张图像进行测试
obj_index = tf.where(pred[...,4]>obj_thresh)#框数,4 置信索引
obj_index_mask = tf.cast(obj_index[:,0],tf.int32)#把第一维batch维取出来,作为索引掩膜
#先把索引掩膜去重编号,然后计算每个编号的数量,即计算每个batch的框的数量(考虑用tf.bincount比较合适因为可能有batch不存在框,不存在的batch返回计数0)
#infos_split_group = tf.unique_with_counts(tf.unique(obj_index_mask)[1])[2]
#infos_split_group = tf.bincount(obj_index_mask)
infos = tf.gather_nd(pred,obj_index)#框数,4+1+self.num_classes
return infos
def correct_boxes(infos):
pred_min_xy = infos[:,:2]*self.original_wh#框数,2
pred_max_xy = infos[:,2:4]*self.original_wh#框数,2
zeros = tf.zeros_like(pred_min_xy)
ones = self.original_wh*tf.ones_like(pred_min_xy)
pred_min_xy = tf.where(pred_min_xy>zeros,pred_min_xy,zeros)
pred_min_xy = tf.where(pred_min_xy<ones,pred_min_xy,ones)
pred_max_xy = tf.where(pred_max_xy>zeros,pred_max_xy,zeros)
pred_max_xy = tf.where(pred_max_xy<ones,pred_max_xy,ones)
return tf.concat([pred_min_xy,pred_max_xy,infos[:,4:]],axis=-1)
def nms(infos,nms_threshold=0.4):
#提取batch
#infos_mask = tf.ones_like(infos)#框数,4+1+self.num_classes
#batch = tf.cast(tf.reduce_sum(infos_mask)/tf.reduce_sum(infos_mask,axis=1)[0],tf.int32)
batch = tf.shape(infos)[0]
#先把infos按照最大class概率重排序
#pred_max_class = tf.reduce_max(infos[:,5:],axis=1)#batch,
#ix = tf.nn.top_k(tf.transpose(tf.expand_dims(pred_max_class,axis=1),[1,0]), batch, sorted=True, name="top_anchors").indices#1,batch
#infos = tf.gather_nd(infos,tf.transpose(ix,[1,0]))#batch,4+1+self.num_classes
pred_min_yx = infos[:,1::-1]#batch,2
pred_max_yx = infos[:,3:1:-1]#batch,2
pred_yx = tf.concat([pred_min_yx,pred_max_yx],axis=-1)#batch,4
pred_max_class = tf.reduce_max(infos[:,5:],axis=1)#batch,
indices = tf.image.non_max_suppression(pred_yx, pred_max_class, batch,nms_threshold, name="non_max_suppression")
infos = tf.gather(infos, indices,name='zsc_output')
return infos
anchors = [[240,173, 257,326, 412,412],[106,85, 137,151, 145,269],[33,36, 53,69, 71,144]]
#input_mask = tf.ones_like(self.input_image)#none,none,none,3
#net_h = tf.cast(tf.reduce_sum(input_mask)/tf.reduce_sum(input_mask,axis=[0,2,3])[0],tf.int32)#net_h = none
#net_w = tf.cast(tf.reduce_sum(input_mask)/tf.reduce_sum(input_mask,axis=[0,1,3])[0],tf.int32)#net_w = none
#net_factor = tf.reshape(tf.cast([net_w,net_h],tf.float32),[1,1,1,1,2])#1,1,1,1,2
net_h = tf.shape(self.input_image)[1]
net_w = tf.shape(self.input_image)[2]
net_factor = tf.reshape(tf.cast([net_w,net_h],tf.float32),[1,1,1,1,2])
infos_1 = decode_pred(self.pred_yolo_1,anchors[0],net_factor)
infos_2 = decode_pred(self.pred_yolo_2,anchors[1],net_factor)
infos_3 = decode_pred(self.pred_yolo_3,anchors[2],net_factor)
infos = tf.concat([infos_1,infos_2,infos_3],axis=0)#框数,4+1+self.num_classes
self.infos = correct_boxes(infos)#框数,4+1+self.num_classes
#self.infos = nms(infos)#框数,4+1+self.num_classes
def do_nms(self,boxes, nms_thresh):
def _interval_overlap(interval_a, interval_b):
x1, x2 = interval_a
x3, x4 = interval_b
if x3 < x1:
if x4 < x1:
return 0
else:
return min(x2,x4) - x1
else:
if x2 < x3:
return 0
else:
return min(x2,x4) - x3
def bbox_iou(box1, box2):
intersect_w = _interval_overlap([box1[0], box1[2]], [box2[0], box2[2]])
intersect_h = _interval_overlap([box1[1], box1[3]], [box2[1], box2[3]])
intersect = intersect_w * intersect_h
w1, h1 = box1[2]-box1[0], box1[3]-box1[1]
w2, h2 = box2[2]-box2[0], box2[3]-box2[1]
union = w1*h1 + w2*h2 - intersect
return float(intersect) / union+1e-4
for c in range(self.num_classes):
sorted_indices = np.argsort([-box[5:][c] for box in boxes])
for i in range(len(sorted_indices)):
index_i = sorted_indices[i]
if boxes[index_i][5:][c] == 0: continue
for j in range(i+1, len(sorted_indices)):
index_j = sorted_indices[j]
if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_thresh:
boxes[index_j][5:][c] = 0
return boxes
def __init__output(self):
with tf.variable_scope('output'):
regularzation_loss = sum(tf.get_collection("regularzation_loss"))
orth_constraint_loss = sum(tf.get_collection("orth_constraint"))
if Detection_or_Classifier=='classifier':
self.all_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.classifier_logits, labels=self.y, name='loss'))
if self.use_decoupled:
self.all_loss = self.all_loss + regularzation_loss + orth_constraint_loss
else:
self.all_loss = self.all_loss + regularzation_loss
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
learning_rate = tf.train.exponential_decay(self.learning_rate,global_step=self.global_epoch_tensor,decay_steps=1,decay_rate=0.995,staircase=True)
self.optimizer = tf.train.AdamOptimizer(learning_rate)
self.train_op = self.optimizer.minimize(self.all_loss)
self.y_out_softmax = tf.nn.softmax(self.classifier_logits,name='zsc_output')
self.y_out_argmax = tf.cast(tf.argmax(self.y_out_softmax, axis=-1),tf.int32)
self.accuracy = tf.reduce_mean(tf.cast(tf.equal(self.y, self.y_out_argmax), tf.float32))
#with tf.name_scope('train-summary-per-iteration'):
#tf.summary.scalar('loss', self.all_loss)
#tf.summary.scalar('acc', self.accuracy)
#self.summaries_merged = tf.summary.merge_all()
elif Detection_or_Classifier=='detection':
self.loss_yolo_1,class_loss_1,recall50_1,recall75_1,class_acc_1,avg_obj_1,avg_noobj_1,count_1,count_noobj_1 = self.loss(1.0,[self.input_image, self.pred_yolo_1, self.true_yolo_1, self.true_boxes],
self.anchors[:,12:], [1*num for num in self.max_grid], self.ignore_thresh)
self.loss_yolo_2,class_loss_2,recall50_2,recall75_2,class_acc_2,avg_obj_2,avg_noobj_2,count_2,count_noobj_2 = self.loss(1.0,[self.input_image, self.pred_yolo_2, self.true_yolo_2, self.true_boxes],
self.anchors[:,6:12], [1*num for num in self.max_grid], self.ignore_thresh)
self.loss_yolo_3,class_loss_3,recall50_3,recall75_3,class_acc_3,avg_obj_3,avg_noobj_3,count_3,count_noobj_3 = self.loss(1.0,[self.input_image, self.pred_yolo_3, self.true_yolo_3, self.true_boxes],
self.anchors[:,:6], [1*num for num in self.max_grid], self.ignore_thresh)
if self.use_decoupled:
self.all_loss = tf.reduce_mean(self.loss_yolo_1+self.loss_yolo_2+self.loss_yolo_3+class_loss_1+class_loss_2+class_loss_3) + regularzation_loss + orth_constraint_loss
else:
self.all_loss = tf.reduce_mean(self.loss_yolo_1+self.loss_yolo_2+self.loss_yolo_3+class_loss_1+class_loss_2+class_loss_3) + regularzation_loss
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
learning_rate = tf.train.exponential_decay(self.learning_rate,global_step=self.global_epoch_tensor,decay_steps=1,decay_rate=0.995,staircase=True)
self.optimizer = tf.train.AdamOptimizer(learning_rate)
self.train_op = self.optimizer.minimize(self.all_loss)
#with tf.name_scope('train-summary-per-iteration'):
#tf.summary.scalar('loss', tf.cast(self.all_loss,tf.float32))
#tf.summary.scalar('recall50',(recall50_1*count_1+recall50_2*count_2+recall50_3*count_3)/(count_1+count_2+count_3+1e-3) )
#tf.summary.scalar('recall75',(recall75_1*count_1+recall75_2*count_2+recall75_3*count_3)/(count_1+count_2+count_3+1e-3))
#tf.summary.scalar('class_accury',(class_acc_1*count_1+class_acc_2*count_2+class_acc_3*count_3)/(count_1+count_2+count_3+1e-3))
#tf.summary.scalar('avg obj accuracy',(avg_obj_1*count_1+avg_obj_2*count_2+avg_obj_3*count_3)/(count_1+count_2+count_3+1e-3))
#tf.summary.scalar('avg no obj accuracy',(avg_noobj_1*count_1+avg_noobj_2*count_2+avg_noobj_3*count_3)/(count_1+count_2+count_3+1e-3))
#tf.summary.scalar('obj count',(count_1+count_2+count_3))
#tf.summary.scalar('no obj count',(count_noobj_2+count_noobj_2+count_noobj_3))
#self.summaries_merged = tf.summary.merge_all()
def loss(self,scale,x,anchors, max_grid,ignore_thresh):
def focal_loss(prediction_tensor, target_tensor, weights=None, alpha=0.25, gamma=2):
sigmoid_p = tf.nn.sigmoid(prediction_tensor)
zeros = array_ops.zeros_like(sigmoid_p, dtype=sigmoid_p.dtype)
pos_p_sub = array_ops.where(target_tensor > zeros, target_tensor - sigmoid_p, zeros)
neg_p_sub = array_ops.where(target_tensor > zeros, zeros, sigmoid_p)
per_entry_cross_ent = - 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))
return per_entry_cross_ent
max_grid_h, max_grid_w = max_grid
cell_x = tf.to_float(tf.reshape(tf.tile(tf.range(max_grid_w), [max_grid_h]), (1, max_grid_h, max_grid_w, 1, 1)))
cell_y = tf.transpose(cell_x, (0,2,1,3,4))
cell_grid = tf.tile(tf.concat([cell_x,cell_y],-1), [self.batch_size, 1, 1, self.num_anchors, 1])#max_grid的索引grid (1,max_grid_h,max_grid_w,5,1)
input_image, y_pred, y_true, true_boxes = x#(none,H,W,3),(none,grid_h,grid_w,3*(4+1+num_classes))
#(none,grid_h,grid_w,3,4+1+num_classes),(none,1,1,1,max_box_per_image,4)
# adjust the shape of the y_predict [batch, grid_h, grid_w, 3, 4+1+num_classes]
y_pred = tf.reshape(y_pred, tf.concat([tf.shape(y_pred)[:3], tf.constant([self.num_anchors, -1])], axis=0))#(none,grid_h,grid_w,3,4+1+num_classes)
# initialize the masks
object_mask = tf.expand_dims(y_true[..., 4], 4)#score (none,grid_h,grid_w,3,1) y_true里面每个groundtruth只匹配一个特征层的一个anchor,概率为1
no_object_mask = 1 - object_mask # (none,grid_h,grid_w,3,1)
# the variable to keep track of number of batches processed
batch_seen = tf.Variable(0.)
# compute grid factor and net factor
grid_h = tf.shape(y_true)[1]
grid_w = tf.shape(y_true)[2]
grid_factor = tf.reshape(tf.cast([grid_w, grid_h], tf.float32), [1,1,1,1,2])#loss输入层grid (1,1,1,1,2)
net_h = tf.shape(input_image)[1]
net_w = tf.shape(input_image)[2]
net_factor = tf.reshape(tf.cast([net_w, net_h], tf.float32), [1,1,1,1,2])#输入图像grid (1,1,1,1,2)
anchors = tf.reshape(anchors,[-1,1,1,self.num_anchors,2])#none,1,1,3,2
_anchors = anchors/net_factor#none,1,1,3,2
anchors_min_wh = tf.where(_anchors[:,:,:,0,0]<_anchors[:,:,:,0,1],_anchors[:,:,:,0,0],_anchors[:,:,:,0,1])#none,1,1
anchors_min_wh = tf.expand_dims(anchors_min_wh,-1)#none,1,1,1
anchors_max_wh = tf.where(_anchors[:,:,:,-1,0]>_anchors[:,:,:,-1,1],_anchors[:,:,:,-1,0],_anchors[:,:,:,-1,1])#none,1,1
anchors_max_wh = tf.expand_dims(anchors_max_wh,-1)#none,1,1,1
anchors = tf.tile(anchors,[1,grid_h,grid_w,1,1])#none.grid_h,grid_w,3,2
"""
Adjust prediction
"""
pred_box_xy = (cell_grid[:,:grid_h,:grid_w,:,:] + tf.sigmoid(y_pred[..., :2])) # sigma(t_xy) + c_xy (none,grid_h,grid_w,3,2)
pred_box_wh = y_pred[..., 2:4] # t_wh (none,grid_h,grid_w,3,2)
pred_box_conf = tf.expand_dims(tf.sigmoid(y_pred[..., 4]), 4) # adjust confidence (none,grid_h,grid_w,3,1)
pred_box_class = y_pred[..., 5:] # adjust class probabilities (none,grid_h,grid_w,3,num_classes)
"""
Adjust ground truth
"""
true_box_xy = y_true[..., 0:2] # (sigma(t_xy) + c_xy) (none,grid_h,grid_w,3,2)
true_box_wh = y_true[..., 2:4] # t_wh (none,grid_h,grid_w,3,2)
true_box_conf = tf.expand_dims(y_true[..., 4], 4)# (none,grid_h,grid_w,3,1)
true_box_class = tf.argmax(y_true[..., 5:], -1) # (none,grid_h,grid_w,3)
"""
Compare each predicted box to all true boxes
"""
# initially, drag all objectness of all boxes to 0
conf_delta = pred_box_conf - 0 #(none,grid_h,grid_w,3,1)
# then, ignore the boxes which have good overlap with some true box
true_xy = true_boxes[..., 0:2] / grid_factor #(none,1,1,1,max_box_per_image,2)/(1,1,1,1,2)=(none,1,1,1,max_box_per_image,2)
true_wh = true_boxes[..., 2:4] / net_factor #(none,1,1,1,max_box_per_image,2)/(1,1,1,1,2)=(none,1,1,1,max_box_per_image,2)
'''
###################
###################
#snip
#true_min_wh = tf.minimum(true_wh[...,0],true_wh[...,1])#none,1,1,1,max_box_per_image
#snip_mask = tf.cast(true_min_wh>tf.expand_dims(anchors_min_wh,4)*2/3,
# tf.float32)*tf.cast(true_min_wh<tf.expand_dims(anchors_max_wh,4)*1.5,tf.float32)#none,1,1,1,max_box_per_image
#snip_mask = tf.expand_dims(snip_mask,-1)#none,1,1,1,max_box_per_image,1
#true_wh = snip_mask*true_wh#none,1,1,1,max_box_per_image,2
#true_xy = snip_mask*true_xy#none,1,1,1,max_box_per_image,2
###################
###################
'''
#在输入feature map尺度上groundtruth的标记框
true_wh_half = true_wh / 2. #(none,1,1,1,max_box_per_image,2)
true_mins = true_xy - true_wh_half #(none,1,1,1,max_box_per_image,2)
true_maxes = true_xy + true_wh_half #(none,1,1,1,max_box_per_image,2)
pred_xy = tf.expand_dims(pred_box_xy / grid_factor, 4) #(none,grid_h,grid_w,3,1,2)
pred_wh = tf.expand_dims(tf.exp(pred_box_wh) * anchors / net_factor, 4) #(none,grid_h,grid_w,3,1,2)
#feature map预测的标记框
pred_wh_half = pred_wh / 2.
pred_mins = pred_xy - pred_wh_half #(none,grid_h,grid_w,3,1,2)
pred_maxes = pred_xy + pred_wh_half #(none,grid_h,grid_w,3,1,2)
#计算feature map预测和groundtruth的所有iou
intersect_mins = tf.maximum(pred_mins, true_mins) #(none,grid_h,grid_w,3,max_box_per_image,2)
intersect_maxes = tf.minimum(pred_maxes, true_maxes) #(none,grid_h,grid_w,3,max_box_per_image,2)
intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) #(none,grid_h,grid_w,3,max_box_per_image,2)
intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] #(none,grid_h,grid_w,3,max_box_per_image)
true_areas = true_wh[..., 0] * true_wh[..., 1] #(none,1,1,1,max_box_per_image)
pred_areas = pred_wh[..., 0] * pred_wh[..., 1] #(none,grid_h,grid_w,3,1)
union_areas = pred_areas + true_areas - intersect_areas #(none,grid_h,grid_w,3,max_box_per_image)
iou_scores = tf.truediv(intersect_areas, union_areas) #(none,grid_h,grid_w,3,max_box_per_image)
#计算feature map每个位置匹配到的3个最大iou,大于阈值的conf_delta置0
best_ious = tf.reduce_max(iou_scores, axis=4) #(none,grid_h,grid_w,3)
conf_delta *= tf.expand_dims(tf.to_float(best_ious < ignore_thresh), 4) #(none,grid_h,grid_w,3,1)
"""
Compute some online statistics
"""
true_xy = true_box_xy / grid_factor #(none,grid_h,grid_w,3,2)
true_wh = tf.exp(true_box_wh) * anchors / net_factor #(none,grid_h,grid_w,3,2)
'''
###################
###################
#snip
#true_min_wh = tf.minimum(true_wh[...,0],true_wh[...,1])#none,grid_h,grid_w,3
#anchors_min_wh = tf.tile(anchors_min_wh,[1,grid_h,grid_w,self.num_anchors])
#anchors_max_wh = tf.tile(anchors_max_wh,[1,grid_h,grid_w,self.num_anchors])
#snip_mask = tf.cast(true_min_wh>anchors_min_wh*2/3,
# tf.float32)*tf.cast(true_min_wh<anchors_max_wh*1.5,tf.float32)#none,grid_h,grid_w,3
#snip_mask = tf.expand_dims(snip_mask,-1)#none,grid_h,grid_w,3,1
#true_xy = snip_mask*true_xy#none,grid_h,grid_w,3,2
#true_wh = snip_mask*true_wh#none,grid_h,grid_w,3,2
#true_box_xy = snip_mask*true_box_xy#none,grid_h,grid_w,3,2
#true_box_wh = snip_mask*true_box_wh#none,grid_h,grid_w,3,2
#true_box_conf = snip_mask*true_box_conf#none,grid_h,grid_w,3,1
#true_box_class = tf.cast(tf.squeeze(snip_mask,-1),tf.int64)*true_box_class#none,grid_h,grid_w,3
#pred_box_xy = snip_mask*pred_box_xy#none,grid_h,grid_w,3,2
#pred_box_wh = snip_mask*pred_box_wh#none,grid_h,grid_w,3,2
#pred_box_conf = snip_mask*pred_box_conf#none,grid_h,grid_w,3,1
#pred_box_class = snip_mask*pred_box_class#none,grid_h,grid_w,3,2
#object_mask = snip_mask*object_mask#none,grid_h,grid_w,3,1
###################
###################
'''
#在输入feature map尺度上为每个groundtruth匹配到的惟一的标记框
true_wh_half = true_wh / 2. #(none,grid_h,grid_w,3,2)
true_mins = true_xy - true_wh_half #(none,grid_h,grid_w,3,2)
true_maxes = true_xy + true_wh_half #(none,grid_h,grid_w,3,2)
pred_mins = tf.squeeze(pred_mins,axis=4) #(none,grid_h,grid_w,3,2)
pred_maxes = tf.squeeze(pred_maxes,axis=4) #(none,grid_h,grid_w,3,2)
#计算feature map预测和每个groundtruth匹配到的唯一标记框的iou
intersect_mins = tf.maximum(pred_mins, true_mins) #(none,grid_h,grid_w,3,2)
intersect_maxes = tf.minimum(pred_maxes, true_maxes) #(none,grid_h,grid_w,3,2)
intersect_wh = tf.maximum(intersect_maxes - intersect_mins, 0.) #(none,grid_h,grid_w,3,2)
intersect_areas = intersect_wh[..., 0] * intersect_wh[..., 1] #(none,grid_h,grid_w,3)
true_areas = true_wh[..., 0] * true_wh[..., 1] #(none,grid_h,grid_w,3)
pred_areas = tf.squeeze(pred_areas,axis=4) #(none,grid_h,grid_w,3)
union_areas = pred_areas + true_areas - intersect_areas #(none,grid_h,grid_w,3)
iou_scores = tf.truediv(intersect_areas, union_areas) #(none,grid_h,grid_w,3)
#计算feature map预测里面每个groundtruth匹配到的唯一标记框的iou分数,其他预测框分数置0
iou_scores = object_mask * tf.expand_dims(iou_scores, 4) #(none,grid_h,grid_w,3,1)
count = tf.reduce_sum(tf.to_float(object_mask)) #(none,) 计算唯一标记框的数量
count_noobj = tf.reduce_sum(tf.to_float(no_object_mask)) #(none,) 计算非标记框的数量
detect_mask = tf.to_float((pred_box_conf*object_mask) >= 0.5)
class_mask = tf.expand_dims(tf.to_float(tf.equal(tf.argmax(pred_box_class, -1), true_box_class)), 4)
recall50 = tf.reduce_sum(tf.to_float(iou_scores >= 0.5 ) * detect_mask * class_mask) / (count + 1e-3)#(none,) 计算唯一标记框里iou>0.5的分数 recall50
recall75 = tf.reduce_sum(tf.to_float(iou_scores >= 0.75) * detect_mask * class_mask) / (count + 1e-3)#(none,) 计算唯一标记框里iou>0.75的分数 recall75
avg_iou = tf.reduce_sum(iou_scores) / (count + 1e-3) #(none,) 计算唯一标记框的平均iou
avg_obj = tf.reduce_sum(object_mask * pred_box_conf) / (count + 1e-3) #(none,) 计算唯一标记框的预测平均置信度
avg_noobj = tf.reduce_sum(no_object_mask * pred_box_conf) / (count_noobj + 1e-3) #(none,) 计算非标记框里的预测平均置信度
avg_cat = tf.reduce_sum(object_mask * class_mask) / (count + 1e-3) #(none,) 计算唯一标记框的类别平均预测准确率
"""
Warm-up training
"""
batch_seen = tf.assign_add(batch_seen, 1.)
true_box_xy, true_box_wh, xywh_mask = true_box_xy, true_box_wh, object_mask
"""
Compare each true box to all anchor boxes
"""
xywh_scale = tf.exp(true_box_wh) * anchors / net_factor #(none,grid_h,grid_w,3,2)
xywh_scale = tf.expand_dims(2 - xywh_scale[..., 0] * xywh_scale[..., 1], axis=4) #(none,grid_h,grid_w,3,1) 真值框尺寸越小,scale越大
xy_delta = scale*1*xywh_mask * (pred_box_xy-true_box_xy) * xywh_scale #(none,grid_h,grid_w,3,2) 计算唯一标记框的预测和真值xy偏差
wh_delta = scale*1*xywh_mask * (pred_box_wh-true_box_wh) * xywh_scale #(none,grid_h,grid_w,3,2) 计算唯一标记框的预测和真值wh偏差
conf_delta_obj = scale*5*object_mask*(pred_box_conf-true_box_conf)#(none,grid_h,grid_w,3,1) 计算唯一预测框的置信度偏差
conf_delta_noobj = 1*no_object_mask*conf_delta*(1.3-1.0*count_noobj/(count+count_noobj))#(none,grid_h,grid_w,3,1) 计算非唯一预测框的置信度偏差
class_delta = scale*1*object_mask * tf.expand_dims(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=true_box_class, logits=pred_box_class), 4)
#(none,grid_h,grid_w,3,num_classes) 计算唯一标记框的分类偏差
#class_delta = focal_loss(tf.reshape(pred_box_class,[tf.shape(pred_box_class)[0],-1,self.num_classes]),
# tf.reshape(tf.one_hot(true_box_class,self.num_classes),[tf.shape(pred_box_class)[0],-1,self.num_classes]))
#class_delta = scale*1*object_mask*tf.reshape(class_delta,tf.concat([tf.shape(pred_box_class)[:-1],[-1]],axis=-1))
loss = tf.reduce_sum(tf.square(xy_delta), list(range(1,5))) + \
tf.reduce_sum(tf.square(wh_delta), list(range(1,5))) + \
tf.reduce_sum(tf.square(conf_delta_obj), list(range(1,5))) + \
tf.reduce_sum(tf.square(conf_delta_noobj), list(range(1,5)))
class_loss = tf.reduce_sum(class_delta, list(range(1,5)))#(none,)
loss = tf.Print(loss, [grid_h, count], message='obj num: ', summarize=1000)
loss = tf.Print(loss, [grid_h, avg_obj], message='obj average confidence: ', summarize=1000)
loss = tf.Print(loss, [grid_h, avg_noobj], message='unobj average confidence: ', summarize=1000)
loss = tf.Print(loss, [grid_h, avg_iou], message='obj average iou: ', summarize=1000)
loss = tf.Print(loss, [grid_h, avg_cat], message='obj average class average accuracy: ', summarize=1000)
loss = tf.Print(loss, [grid_h, recall50], message='obj recall50: ', summarize=1000)
loss = tf.Print(loss, [grid_h, recall75], message='obj recall75: ', summarize=1000)
loss = tf.Print(loss, [grid_h, tf.reduce_sum(tf.square(xy_delta)),tf.reduce_sum(tf.square(wh_delta)),
tf.reduce_sum(tf.square(conf_delta_obj)),tf.reduce_sum(tf.square(conf_delta_noobj)),
tf.reduce_sum(class_delta)], message='xy loss,wh loss,conf loss,un conf loss,class loss:\n', summarize=1000)
return loss,class_loss,recall50,recall75,avg_cat,avg_obj,avg_noobj,count,count_noobj
def __init_input(self):
if Detection_or_Classifier=='classifier':
with tf.variable_scope('input'):
self.input_image = tf.placeholder(tf.float32,[None, None, None, 3],name='zsc_input')#训练、测试用
self.y = tf.placeholder(tf.int32, [None],name='zsc_input_target')#训练、测试用
self.is_training = tf.placeholder(tf.bool,name='zsc_is_train')#训练、测试用
elif Detection_or_Classifier=='detection':
with tf.variable_scope('input'):
self.input_image = tf.placeholder(tf.float32,[None,None,None,3],name='zsc_input')#训练、测试用
self.original_wh = tf.placeholder(tf.float32,[None,2],name='zsc_original_wh')#仅测试用
self.is_training = tf.placeholder(tf.bool,name='zsc_is_train')#训练、测试(不一定)用
self.anchors = tf.placeholder(tf.float32,[None,self.num_anchors*3*2])#训练用
self.true_boxes = tf.placeholder(tf.float32,[None,1, 1, 1, self.max_box_per_image, 4])#训练用
self.true_yolo_1 = tf.placeholder(tf.float32,[None,None, None, self.num_anchors, 4+1+self.num_classes])#训练用
self.true_yolo_2 = tf.placeholder(tf.float32,[None,None, None, self.num_anchors, 4+1+self.num_classes])#训练用
self.true_yolo_3 = tf.placeholder(tf.float32,[None,None, None, self.num_anchors, 4+1+self.num_classes])#训练用
def __init_global_epoch(self):
with tf.variable_scope('global_epoch'):
self.global_epoch_tensor = tf.Variable(-1, trainable=False, name='global_epoch')
self.global_epoch_input = tf.placeholder('int32', None, name='global_epoch_input')
self.global_epoch_assign_op = self.global_epoch_tensor.assign(self.global_epoch_input)
def __init_global_step(self):
with tf.variable_scope('global_step'):
self.global_step_tensor = tf.Variable(0, trainable=False, name='global_step')
self.global_step_input = tf.placeholder('int32', None, name='global_step_input')
self.global_step_assign_op = self.global_step_tensor.assign(self.global_step_input)
################################################################################################################
################################################################################################################
################################################################################################################
##Clique_conv
def CliqueBlock(name,x,num_block=6,num_filters=80,use_decoupled=True,norm='group_norm',activate='selu',is_training=True,use_deformconv=True,loop=1):
with tf.variable_scope(name):
input = x
FeatureList = []
'''
for i in range(num_block):
_x = _B_conv_block('_B_conv_{}_0'.format(i),x,num_filters,1,1,'SAME',use_decoupled,norm,activate,is_training)
_x = _B_conv_block('_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
x = tf.concat([x,_x],axis=-1)
FeatureList.append(_x)#0,1,2,3,4,5
x0 = tf.concat(FeatureList,axis=-1)
with tf.variable_scope('loop'):
for i in range(num_block):
del FeatureList[0]
_x = _B_conv_block('_II_B_conv_{}_0'.format(i),tf.concat(FeatureList,axis=-1),num_filters,1,1,'SAME',use_decoupled,norm,activate,is_training)
if not use_deformconv:
_x = _B_conv_block('_II_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
else:
if i==num_block-1:
_x = _B_deform_conv('deform_conv_II',_x,num_filters,3,1,3,'SAME',use_decoupled,norm,activate,is_training)
else:
_x = _B_conv_block('_II_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
FeatureList.append(_x)
'''
'''
if loop>1:
for _ in range(1,loop):
with tf.variable_scope('loop',reuse=True):
for i in range(num_block):
del FeatureList[0]
_x = _B_conv_block('_II_B_conv_{}_0'.format(i),tf.concat(FeatureList,axis=-1),num_filters,1,1,'SAME',use_decoupled,norm,activate,is_training)
if not use_deformconv:
_x = _B_conv_block('_II_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
else:
if i==num_block-1:
_x = _B_deform_conv('deform_conv_II',_x,num_filters,3,1,3,'SAME',use_decoupled,norm,activate,is_training)
else:
_x = _B_conv_block('_II_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
FeatureList.append(_x)
'''
'''
x1 = tf.concat(FeatureList,axis=-1)
SE = CompetitiveSE('CompetitiveSE',[input,x0,x1],False,None,activate,is_training)
x = (x0+x1)*(1+SE)
'''
for i in range(num_block):
_x = _B_conv_block('_B_conv_{}_0'.format(i),x,num_filters,1,1,'SAME',use_decoupled,norm,activate,is_training)
if not use_deformconv:
_x = _B_conv_block('_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
else:
if i==num_block-1:
_x = _B_deform_conv('deform_conv',_x,num_filters,3,1,3,'SAME',use_decoupled,norm,activate,is_training)
else:
_x = _B_conv_block('_B_conv_{}_1'.format(i),_x,num_filters,3,1,'SAME',use_decoupled,norm,activate,is_training)
x = tf.concat([x,_x],axis=-1)
FeatureList.append(_x)#0,1,2,3,4,5
x = tf.concat(FeatureList,axis=-1)
x = x*(1+_SE('Gate',x,False,None,activate,is_training))
return x
##Transition
def Transition(name,x,use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
C = x.get_shape().as_list()[-1]
x = _B_conv_block('_B_conv_block',x,C//2,1,1,'SAME',use_decoupled,norm,activate,is_training)
x = Attention('Attention',x,use_decoupled,norm,activate,is_training)
return x
##Dpp
def Dpp(name,Iq):
def lamda_1(x,lamda,eplice=1e-10):
return tf.pow(tf.sqrt(tf.pow(x,2.)+eplice*eplice),lamda)
def lamda_2(x,lamda,eplice=1e-10):
return tf.pow(tf.sqrt(tf.pow(tf.nn.relu(x),2.)+eplice*eplice),lamda)
with tf.variable_scope(name):
Ip = tf.nn.avg_pool(Iq,[1,2,2,1],[1,1,1,1],'SAME')
gaussian_filter = tf.truediv(tf.constant([[1,1,1],[1,2,1],[1,1,1]],dtype=tf.float32),16.0)
gaussian_filter = tf.reshape(gaussian_filter,[3,3,1,1])
gaussian_filter = tf.tile(gaussian_filter,[1,1,Ip.get_shape().as_list()[-1],Ip.get_shape().as_list()[-1]])
Ip = tf.nn.conv2d(Ip,gaussian_filter,[1,1,1,1],'SAME')
#gaussian_filter = tf.tile(gaussian_filter,[1,1,Ip.get_shape().as_list()[-1],1])
#Ip = tf.nn.depthwise_conv2d(Ip,gaussian_filter,[1,1,1,1],'SAME')
alpha = tf.Variable(0.0,trainable=True)
lamda = tf.Variable(1.0,trainable=True)
weight = alpha+lamda_2(Iq-Ip,lamda)
inverse_bilatera = Iq*weight
result = tf.truediv(tf.nn.avg_pool(inverse_bilatera,[1,2,2,1],[1,2,2,1],'SAME'),tf.nn.avg_pool(weight,[1,2,2,1],[1,2,2,1],'SAME'))
return result
##primary_conv
def PrimaryConv(name,x,use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
x = bn(x, is_training, name='PrimaryBN')
#none,none,none,3
x0 = _conv_block('conv_0',x,64,3,2,'SAME',use_decoupled,norm,activate,is_training)#none,none/2,none/2,64
x = _conv_block('conv_1',x0,64,3,1,'SAME',use_decoupled,norm,activate,is_training)#none,none/2,none/2,128
x = _conv_block('conv_2',x,128,3,1,'SAME',use_decoupled,norm,activate,is_training)#none,none/2,none/2,32
'''
x0 = _conv_block('conv_0_0',x0,128,3,1,'SAME',use_decoupled,norm,activate,is_training)#none,none/2,none/2,128
#1*1_conv+3*3_dilated_1_rate
x1 = _conv_block('conv_1',x0,64,1,1,'SAME',False,norm,activate,is_training)
x1 = _atrous_conv_block('atrous_conv_1',x1,64,3,1,'SAME',False,norm,activate,is_training)
#3*3conv+3*3_dilated_3_rate
x2 = _conv_block('conv_2',x0,64,3,1,'SAME',False,norm,activate,is_training)
x2 = _atrous_conv_block('atrous_conv_2',x2,64,3,3,'SAME',False,norm,activate,is_training)
#5*5conv+3*3_dilated_5_rate->3*3conv+3*3conv+3*3_dilated_5_rate
#x3 = _conv_block('conv_3',x0,64,5,1,'SAME',False,norm,activate,is_training)
x3 = _conv_block('conv_3',x0,64,3,1,'SAME',False,norm,activate,is_training)
x3 = _conv_block('conv_3',x3,64,3,1,'SAME',False,norm,activate,is_training)
x3 = _atrous_conv_block('atrous_conv_3',x3,64,3,5,'SAME',False,norm,activate,is_training)
x = tf.concat([x1,x2,x3],axis=-1)
x = _conv_block('conv_4',x,128,1,1,'SAME',False,norm,activate,is_training)
x = x + x0
'''
x = Attention('Attention',x,use_decoupled,norm,activate,is_training,True)
#x = tf.nn.max_pool(x,[1,3,3,1],[1,2,2,1],'SAME')
return x
##DilatedBlock
#def DilatedBlock(name,x,num_filters=16,kernel_size=3,rate=[1,3,5],padding='SAME',norm='group_norm',activate='selu',is_training=True,useBias=True):
# with tf.variable_scope(name):
##_atrous_conv_block
def _atrous_conv_block(name,input,num_filters=16,kernel_size=3,rate=1,padding='SAME',norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
w = GetWeight('weight',[kernel_size,kernel_size,input.shape.as_list()[-1],num_filters])
x = tf.nn.atrous_conv2d(input,w,rate,padding,'atrous_conv2d')
if norm=='batch_norm':
x = bn(x, is_training, name='batch_norm')
elif norm=='group_norm':
x = group_norm(x,name='groupnorm')
else:
b = tf.get_variable('bias',[num_filters],tf.float32,initializer=tf.constant_initializer(0.000))
x = tf.nn.bias_add(x, b)
if activate=='leaky':
x = LeakyRelu(x,leak=0.1, name='leaky')
elif activate=='selu':
x = selu(x,name='selu')
elif activate=='swish':
x = swish(x,name='swish')
elif activate=='relu':
x = tf.nn.relu(x,name='relu')
elif activate=='relu6':
x = tf.nn.relu6(x,name='relu6')
elif activate=='prelu':
x = prelu(x,name='prelu')
else:
pass
return x
##_B_atrous_conv_block
def _B_atrous_conv_block(name,input,num_filters=16,kernel_size=3,rate=1,padding='SAME',norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
if norm=='batch_norm':
x = bn(x, is_training, name='batch_norm')
elif norm=='group_norm':
x = group_norm(x,name='groupnorm')
else:
pass
if activate=='leaky':
x = LeakyRelu(x,leak=0.1, name='leaky')
elif activate=='selu':
x = selu(x,name='selu')
elif activate=='swish':
x = swish(x,name='swish')
elif activate=='relu':
x = tf.nn.relu(x,name='relu')
elif activate=='relu6':
x = tf.nn.relu6(x,name='relu6')
elif activate=='prelu':
x = prelu(x,name='prelu')
else:
pass
w = GetWeight('weight',[kernel_size,kernel_size,input.shape.as_list()[-1],num_filters])
x = tf.nn.atrous_conv2d(input,w,rate,padding,'atrous_conv2d')
return x
##_B_deform_conv
def _B_deform_conv(name,x,num_filters=16,kernel_size=3,stride=1,offect=3,padding='SAME',use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
print('use deformconv')
with tf.variable_scope(name):
C = x.get_shape().as_list()[-1]
if norm=='batch_norm':
x = bn(x, is_training, name='batch_norm')
elif norm=='group_norm':
x = group_norm(x,name='group_norm')
else:
pass
if activate=='leaky':
x = LeakyRelu(x,leak=0.1,name='leaky')
elif activate=='selu':
x = selu(x,name='selu')
elif activate=='swish':
x = swish(x,name='swish')
elif activate=='relu':
x = tf.nn.relu(x,name='relu')
elif activate=='prelu':
x = prelu(x,name='prelu')
else:
pass
x = _deform_conv('deform_conv',x,num_filters,kernel_size,stride,offect,padding,use_decoupled,norm=None,activate=None,is_training=is_training)
return x
##_deform_conv
def _deform_conv(name,x,num_filters=16,kernel_size=3,stride=1,offect=3,padding='SAME',use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
_,_,_,C = x.shape.as_list()
num_012 = tf.shape(x)[:3]
f_h, f_w, f_ic, f_oc = kernel_size,kernel_size,C,num_filters
o_h, o_w, o_ic, o_oc = offect,offect,C,2*f_h*f_w
#offect_map = _conv_block('offect_conv',x,o_oc,offect,1,'SAME',None,None,is_training)#none,none,none,o_oc
w = GetWeight('weight',[offect,offect,C,o_oc])
bias = tf.get_variable('bias',o_oc,tf.float32,initializer=tf.constant_initializer(0.001))
offect_map = tf.nn.atrous_conv2d(x,w,2,'SAME','offect_conv') + bias
offect_map = kernel_size*2*(tf.nn.sigmoid(offect_map)-0.5)#kernel_size*tanh(x)
offect_map = tf.reshape(offect_map,tf.concat( [num_012,[f_h,f_w,2]],axis=-1 ))#none,none,none,f_h,f_w,2
offect_map_h = tf.tile(offect_map[...,0],[C,1,1,1,1])#none*C,none,none,f_h,f_w
offect_map_w = tf.tile(offect_map[...,1],[C,1,1,1,1])#none*C,none,none,f_h,f_w
coord_w,coord_h = tf.meshgrid(tf.range(tf.cast(num_012[2],tf.float32),dtype=tf.float32),
tf.range(tf.cast(num_012[1],tf.float32),dtype=tf.float32))#coord_w:[none,none] coord_h:[none,none]
coord_fw,coord_fh = tf.meshgrid(tf.range(f_w,dtype=tf.float32),tf.range(f_h,dtype=tf.float32))#coord_fw:[f_h,f_w] coord_fh:[f_h,f_w]
'''
coord_w
[[0,1,2,...,i_w-1],...]
coord_h
[[0,...,0],...,[i_h-1,...,i_h-1]]
'''
coord_h = tf.reshape(coord_h,tf.concat([[1],num_012[1:3],[1,1]],axis=-1))#1,none,none,1,1
coord_h = tf.tile(coord_h,tf.concat([[num_012[0]*C],[1,1,f_h,f_w]],axis=-1))#none*C,none,none,f_h,f_w
coord_w = tf.reshape(coord_w,tf.concat([[1],num_012[1:3],[1,1]],axis=-1))#1,none,none,1,1
coord_w = tf.tile(coord_w,tf.concat([[num_012[0]*C],[1,1,f_h,f_w]],axis=-1))#none*C,none,none,f_h,f_w
coord_fh = tf.reshape(coord_fh,[1,1,1,f_h,f_w])#1,1,1,f_h,f_w
coord_fh = tf.tile(coord_fh,tf.concat([[num_012[0]*C],num_012[1:3],[1,1]],axis=-1))#none*C,none,none,f_h,f_w
coord_fw = tf.reshape(coord_fw,[1,1,1,f_h,f_w])#1,1,1,f_h,f_w
coord_fw = tf.tile(coord_fw,tf.concat([[num_012[0]*C],num_012[1:3],[1,1]],axis=-1))#none*C,none,none,f_h,f_w
coord_h = coord_h + coord_fh + offect_map_h#none*C,none,none,f_h,f_w
coord_w = coord_w + coord_fw + offect_map_w#none*C,none,none,f_h,f_w
coord_h = tf.clip_by_value(coord_h,clip_value_min=0,clip_value_max=tf.cast(num_012[1]-1,tf.float32))#none*C,none,none,f_h,f_w
coord_w = tf.clip_by_value(coord_w,clip_value_min=0,clip_value_max=tf.cast(num_012[2]-1,tf.float32))#none*C,none,none,f_h,f_w
coord_hmin = tf.cast(tf.floor(coord_h),tf.int32)#none*C,none,none,f_h,f_w
coord_hmax = tf.cast(tf.ceil(coord_h),tf.int32)#none*C,none,none,f_h,f_w
coord_wmin = tf.cast(tf.floor(coord_w),tf.int32)#none*C,none,none,f_h,f_w
coord_wmax = tf.cast(tf.ceil(coord_w),tf.int32)#none*C,none,none,f_h,f_w
x_r = tf.reshape(tf.transpose(x,[3,0,1,2]),[num_012[0]*C,num_012[1],num_012[2]])#none*C,none,none
bc_index = tf.tile(tf.reshape(tf.range(num_012[0]*C),[-1,1,1,1,1]),tf.concat([[1],num_012[1:3],[f_h,f_w]],axis=-1))#none*C,none,none,f_h,f_w
coord_hminwmin = tf.concat([tf.expand_dims(bc_index,-1),tf.expand_dims(coord_hmin,-1),tf.expand_dims(coord_wmin,-1)],axis=-1)#none*C,none,none,f_h,f_w,3
coord_hminwmax = tf.concat([tf.expand_dims(bc_index,-1),tf.expand_dims(coord_hmin,-1),tf.expand_dims(coord_wmax,-1)],axis=-1)#none*C,none,none,f_h,f_w,3
coord_hmaxwmin = tf.concat([tf.expand_dims(bc_index,-1),tf.expand_dims(coord_hmax,-1),tf.expand_dims(coord_wmin,-1)],axis=-1)#none*C,none,none,f_h,f_w,3
coord_hmaxwmax = tf.concat([tf.expand_dims(bc_index,-1),tf.expand_dims(coord_hmax,-1),tf.expand_dims(coord_wmax,-1)],axis=-1)#none*C,none,none,f_h,f_w,3
var_hminwmin = tf.gather_nd(x_r,coord_hminwmin)#none*C,none,none,f_h,f_w
var_hminwmax = tf.gather_nd(x_r,coord_hminwmax)#none*C,none,none,f_h,f_w
var_hmaxwmin = tf.gather_nd(x_r,coord_hmaxwmin)#none*C,none,none,f_h,f_w
var_hmaxwmax = tf.gather_nd(x_r,coord_hmaxwmax)#none*C,none,none,f_h,f_w
coord_hmin = tf.cast(tf.floor(coord_h),tf.float32)#none*C,none,none,f_h,f_w
coord_hmax = tf.cast(tf.ceil(coord_h),tf.float32)#none*C,none,none,f_h,f_w
coord_wmin = tf.cast(tf.floor(coord_w),tf.float32)#none*C,none,none,f_h,f_w
coord_wmax = tf.cast(tf.ceil(coord_w),tf.float32)#none*C,none,none,f_h,f_w
x_ip = var_hminwmin*(coord_hmax-coord_h)*(coord_wmax-coord_w) + \
var_hminwmax*(coord_hmax-coord_h)*(1-(coord_wmax-coord_w)) + \
var_hmaxwmin*(1-(coord_hmax-coord_h))*(coord_wmax-coord_w) + \
var_hmaxwmax*(1-(coord_hmax-coord_h))*(1-(coord_wmax-coord_w))#none*C,none,none,f_h,f_w
x_ip = tf.transpose(tf.reshape(x_ip,tf.concat([[C],num_012,[f_h,f_w]],axis=-1)), [1,2,4,3,5,0])#none,none,f_h,none,f_w,C
x_ip = tf.reshape(x_ip,[num_012[0],num_012[1]*f_h,num_012[2]*f_w,C])#none,none*f_h,none*f_w,C
out = _B_conv_block('deform_conv',x_ip,num_filters,kernel_size,kernel_size*stride,'SAME',use_decoupled,norm=norm,activate=activate,is_training=is_training)
return out
##_B_conv_block
def _B_conv_block(name,x,num_filters=16,kernel_size=3,stride=2,padding='SAME',use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
C = x.get_shape().as_list()[-1]
if norm=='batch_norm':
x = bn(x, is_training, name='batch_norm')
elif norm=='group_norm':
x = group_norm(x,name='group_norm')
else:
pass
if activate=='leaky':
x = LeakyRelu(x,leak=0.1,name='leaky')
elif activate=='selu':
x = selu(x,name='selu')
elif activate=='swish':
x = swish(x,name='swish')
elif activate=='relu':
x = tf.nn.relu(x,name='relu')
elif activate=='relu6':
x = tf.nn.relu6(x,name='relu6')
elif activate=='prelu':
x = prelu(x,name='prelu')
else:
pass
input = x
w = GetWeight('weight',[kernel_size,kernel_size,C,num_filters])
x = tf.nn.conv2d(x,w,[1,stride,stride,1],padding,name='B_conv')
if use_decoupled:
x = DecoupledOperator('DecoupledOperator',x,input,w,stride)
return x
##_conv_block
def _conv_block(name,input,num_filters=16,kernel_size=3,stride=2,padding='SAME',use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
w = GetWeight('weight',[kernel_size,kernel_size,input.shape.as_list()[-1],num_filters])
x = tf.nn.conv2d(input,w,[1,stride,stride,1],padding=padding,name='conv')
if use_decoupled:
x = DecoupledOperator('DecoupledOperator',x,input,w,stride)
if norm=='batch_norm':
x = bn(x, is_training, name='batch_norm')
elif norm=='group_norm':
x = group_norm(x,name='groupnorm')
else:
b = tf.get_variable('bias',[num_filters],tf.float32,initializer=tf.constant_initializer(0.000))
x = tf.nn.bias_add(x, b)
if activate=='leaky':
x = LeakyRelu(x,leak=0.1, name='leaky')
elif activate=='selu':
x = selu(x,name='selu')
elif activate=='swish':
x = swish(x,name='swish')
elif activate=='relu':
x = tf.nn.relu(x,name='relu')
elif activate=='relu6':
x = tf.nn.relu6(x,name='relu6')
elif activate=='prelu':
x = prelu(x,name='prelu')
else:
pass
return x
##_B_group_conv with channel shuffle
def _B_group_conv(name,x,group=4,num_filters=16,kernel_size=1,stride=1,padding='SAME',use_decoupled=True,norm='group_norm',activate='selu',is_training=True):
with tf.variable_scope(name):
C = x.shape.as_list()[-1]
num_012 = tf.shape(x)[:3]
assert C%group==0 and num_filters%group==0
if norm=='batch_norm':
x = bn(x, is_training, name='batch_norm')
elif norm=='group_norm':
x = group_norm(x,name='groupnorm')
else:
pass
if activate=='leaky':
x = LeakyRelu(x,leak=0.1, name='leaky')
elif activate=='selu':
x = selu(x,name='selu')
elif activate=='swish':
x = swish(x,name='swish')
elif activate=='relu':
x = tf.nn.relu(x,name='relu')
elif activate=='relu6':
x = tf.nn.relu6(x,name='relu6')
elif activate=='prelu':
x = prelu(x,name='prelu')
else:
pass
input = x
w = GetWeight('weight',[kernel_size,kernel_size,C,num_filters//group])
x = tf.nn.depthwise_conv2d(x, w, [1,stride,stride,1], padding)
if use_decoupled:
x = DecoupledOperator('DecoupledOperator',x,input,w,stride)
x = tf.reshape(x,tf.concat([ [num_012[0]], tf.cast(num_012[1:3]/kernel_size,tf.int32), tf.cast([group, C//group, num_filters//group],tf.int32)],axis=-1))
x = tf.reduce_sum(x,axis=4)
x = tf.transpose(x,[0,1,2,4,3])
x = tf.reshape(x,tf.concat([ [num_012[0]], tf.cast(num_012[1:3]/kernel_size,tf.int32), tf.cast([num_filters],tf.int32)],axis=-1))
return x
##CoordConv
def CoordConv(name,x):
print('use CoordConv')
with tf.variable_scope(name):
batch_size_tensor = tf.shape(x)[0]
y_dim = tf.shape(x)[1]
x_dim = tf.shape(x)[2]
xx_ones = tf.ones([batch_size_tensor, x_dim],dtype=tf.int32)
xx_ones = tf.expand_dims(xx_ones, -1)
xx_range = tf.tile(tf.expand_dims(tf.range(x_dim), 0),[batch_size_tensor, 1])
xx_range = tf.expand_dims(xx_range, 1)
xx_channel = tf.matmul(xx_ones, xx_range)
xx_channel = tf.expand_dims(xx_channel, -1)
yy_ones = tf.ones([batch_size_tensor, y_dim],dtype=tf.int32)
yy_ones = tf.expand_dims(yy_ones, 1)
yy_range = tf.tile(tf.expand_dims(tf.range(y_dim), 0),[batch_size_tensor, 1])
yy_range = tf.expand_dims(yy_range, -1)
yy_channel = tf.matmul(yy_range, yy_ones)
yy_channel = tf.expand_dims(yy_channel, -1)
xx_channel = tf.cast(xx_channel, tf.float32) / tf.cast(x_dim - 1, tf.float32)
yy_channel = tf.cast(yy_channel, tf.float32) / tf.cast(y_dim - 1, tf.float32)
xx_channel = xx_channel*2 - 1
yy_channel = yy_channel*2 - 1
ret = tf.concat([x,xx_channel,yy_channel], axis=-1)
if False:
rr = tf.sqrt( tf.square(xx_channel-0.5)+ tf.square(yy_channel-0.5) )
ret = tf.concat([ret, rr], axis=-1)
return ret
##Attention
def Attention(name,x,use_decoupled=True,norm='group_norm',activate='selu',is_training=True,useInPrimary=False):
with tf.variable_scope(name):
if useInPrimary:
x = tf.nn.max_pool(x,[1,3,3,1],[1,2,2,1],'SAME')
else:
x = tf.nn.avg_pool(x,[1,2,2,1],[1,2,2,1],'SAME')
C = x.get_shape().as_list()[-1]
x1 = _B_conv_block('x1',x,C,1,1,'SAME',False,norm,activate,is_training)
attention = SelfAttention('SelfAttention',x1,False,None,activate,is_training)