-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metrics.py
880 lines (783 loc) · 49.5 KB
/
Metrics.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
import argparse
import os
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader, Dataset, TensorDataset
from Map2Partition import get_sequence_partition_for_VTM, map_to_parititon
L1_Loss = nn.L1Loss()
Cross_Entropy = nn.CrossEntropyLoss()
def Mul_Scale_L1Loss(pred_map, label_map):
pred_map_max1 = F.max_pool2d(pred_map, 8)
pred_map_max2 = F.max_pool2d(pred_map, 4)
pred_map_max4 = F.max_pool2d(pred_map, 2)
pred_map_min1 = -F.max_pool2d(-pred_map, 8)
pred_map_min2 = -F.max_pool2d(-pred_map, 4)
pred_map_min4 = -F.max_pool2d(-pred_map, 2)
label_map_max1 = F.max_pool2d(label_map, 8)
label_map_max2 = F.max_pool2d(label_map, 4)
label_map_max4 = F.max_pool2d(label_map, 2)
label_map_min1 = -F.max_pool2d(-label_map, 8)
label_map_min2 = -F.max_pool2d(-label_map, 4)
label_map_min4 = -F.max_pool2d(-label_map, 2)
# MS_L1_Loss = L1_Loss(pred_map_max1, label_map_max1) * 1/170.0 + L1_Loss(pred_map_max2, label_map_max2) * 4/170.0+ L1_Loss(pred_map_max4, label_map_max4) * 16/170.0 + \
# L1_Loss(pred_map_min1, label_map_min1) * 1/170.0 + L1_Loss(pred_map_min2, label_map_min2) * 4/170.0 + L1_Loss(pred_map_min4, label_map_min4) * 16/170.0 + \
# 2.0 * L1_Loss(pred_map, label_map) * 64/170.0
MS_L1_Loss = L1_Loss(pred_map_max1, label_map_max1) * 1/30.0 + L1_Loss(pred_map_max2, label_map_max2) * 2/30.0 + L1_Loss(pred_map_max4, label_map_max4) * 4/30.0 + \
L1_Loss(pred_map_min1, label_map_min1) * 1/30.0 + L1_Loss(pred_map_min2, label_map_min2) * 2/30.0 + L1_Loss(pred_map_min4, label_map_min4) * 4/30.0 + \
2.0 * L1_Loss(pred_map, label_map) * 8/30.0
return MS_L1_Loss
def loss_func_D(dire_out_batch, dire_label_batch): # b*9*16*16, b*3*16*16
loss = 0
dire_out_batch = dire_out_batch.permute((0, 2, 3, 1))
vec_dire_out_batch = dire_out_batch.reshape((-1, 9))
for i in range(3):
vec_dire_out_batch_i = vec_dire_out_batch[:, i*3:(i+1)*3]
vec_dire_label_batch_i = dire_label_batch[:, i, :, :].reshape(-1)
loss += Cross_Entropy(vec_dire_out_batch_i, vec_dire_label_batch_i)
return loss
def adjust_learning_rate(lr, optimizer, epoch, decay_rate):
adj_lr = lr * (0.5 ** (epoch // decay_rate))
if adj_lr > 1e-6:
for param_group in optimizer.param_groups:
param_group['lr'] = adj_lr
#****************************************************************************************************************
# Pre Train
#****************************************************************************************************************
def Load_Pre_VP_Dataset(path, QP, batchSize, datasetID=0, PredID=0 ,isLuma=True, num_workers=2):
# datasetID [train validation test]; PredID [QT BT Direction]
# add variance map to the input
if isLuma:
comp = 'Luma'
else:
comp = 'Chroma'
tr_val_test = ['Train', 'Validate', 'TestSub']
dataset_type = tr_val_test[datasetID]
print('Start loading pre-train ' + comp + ' ' + dataset_type + ' dataset...')
# if isLuma: # luma input
input_path = os.path.join(path, dataset_type + '_Y_Block68.npy')
print('input path0:', input_path)
input_batch = torch.FloatTensor(np.expand_dims(np.load(input_path), 1))
if not isLuma: # chroma input
input_path1 = os.path.join(path, dataset_type + '_U_Block34.npy')
input_path2 = os.path.join(path, dataset_type + '_V_Block34.npy')
print('input path1:', input_path1)
print('input path2:', input_path2)
input_batch = F.max_pool2d(input_batch, 2)
input_batch1 = torch.FloatTensor(np.expand_dims(np.load(input_path1), 1))
input_batch2 = torch.FloatTensor(np.expand_dims(np.load(input_path2), 1))
input_batch = torch.cat([input_batch, input_batch1, input_batch2], 1)
del input_batch1, input_batch2
print('input_batch.shape:', input_batch.shape)
if PredID == 0: # Q
qt_label_path = os.path.join(path, dataset_type + '_' + comp + '_QP' + str(QP) + '_QTdepth_Block8.npy')
print('qt_label path:', qt_label_path)
qt_label_batch = torch.FloatTensor(np.expand_dims(np.load(qt_label_path), 1) - 1) # qt depth start form 1
print('qt_label_batch.shape:', qt_label_batch.shape)
print("Creating Q data loader...")
# input_batch = input_batch[0:1157480]
# qt_label_batch = qt_label_batch[0:1157480]
dataset = TensorDataset(input_batch, qt_label_batch)
dataLoader = DataLoader(dataset=dataset, num_workers=num_workers, batch_size=batchSize, pin_memory=True, shuffle=True)
elif PredID == 1: # QB
qt_label_path = os.path.join(path, dataset_type + '_' + comp + '_QP' + str(QP) + '_QTdepth_Block8.npy')
bt_label_path = os.path.join(path, dataset_type + '_' + comp + '_QP' + str(QP) + '_MSBTdepth_Block16.npy')
print('qt_label path:', qt_label_path)
print('bt_label path:', bt_label_path)
qt_label_batch = torch.FloatTensor(np.expand_dims(np.load(qt_label_path), 1) - 1) # qt depth start form 1
bt_label_batch = torch.FloatTensor(np.expand_dims(np.load(bt_label_path), 1))
print('qt_label_batch.shape:', qt_label_batch.shape)
print('bt_label_batch.shape:', bt_label_batch.shape)
# norm_input_batch = block_qtnode_norm(qt_map=qt_label_batch, block=input_batch, isLuma=isLuma)
print("Creating BD data loader...")
# input_batch = input_batch[0:1157480]
# qt_label_batch = qt_label_batch[0:1157480]
# bt_label_batch = bt_label_batch[0:1157480]
dataset = TensorDataset(input_batch, qt_label_batch, bt_label_batch)
dataLoader = DataLoader(dataset=dataset, num_workers=num_workers, batch_size=batchSize, pin_memory=True, shuffle=True)
elif PredID == 2: # QBD for MSBD training
qt_label_path = os.path.join(path, dataset_type + '_' + comp + '_QP' + str(QP) + '_QTdepth_Block8.npy')
bt_label_path = os.path.join(path, dataset_type + '_' + comp + '_QP' + str(QP) + '_MSBTdepth_Block16.npy')
dire_label_path = os.path.join(path, dataset_type + '_' + comp + '_QP' + str(QP) + '_MSdirection_Block16.npy')
print('qt_label path:', qt_label_path)
print('bt_label path:', bt_label_path)
print('direction_label path:', dire_label_path)
qt_label_batch = torch.FloatTensor(np.expand_dims(np.load(qt_label_path), 1) - 1) # qt depth start form 1
# bt_label_batch = torch.FloatTensor(np.load(bt_label_path))
# dire_label_batch_reg = torch.FloatTensor(np.load(dire_label_path))
# dire_label_batch_cla = torch.LongTensor(
# torch.where(dire_label_batch_reg == -1, torch.full_like(dire_label_batch_reg, 2), dire_label_batch_reg))
# del dire_label_batch_reg
bt_label_batch = torch.FloatTensor(np.load(bt_label_path))
dire_label_batch_reg = torch.FloatTensor(np.load(dire_label_path))
print('qt_label_batch.shape:', qt_label_batch.shape)
print('bt_label_batch.shape:', bt_label_batch.shape)
print('dire_label_batch.shape:', dire_label_batch_reg.shape)
print("Creating QBD data loader...")
dataset = TensorDataset(input_batch, qt_label_batch, bt_label_batch, dire_label_batch_reg)
dataLoader = DataLoader(dataset=dataset, num_workers=num_workers, batch_size=batchSize, pin_memory=True, shuffle=True)
else:
print("Unknown Dataset!!!")
return
return dataLoader
weight_mat = 0.5 * np.array([[1.0, 0.73, 0.15],
[2.43, 0.35, 0.10],
[0.96, 0.23, 0.07],
[0.59, 0.16, 0.05]])
# weight_mat = 0.5 * np.array([[17.83, 0.49, 0.11],
# [1.20, 0.25, 0.07],
# [0.58, 0.17, 0.05],
# [0.38, 0.12, 0.04]])
def loss_func_MSBD_val(bd_out_batch0, bd_out_batch1, bd_out_batch2, bt_label_batch, dire_label_batch_reg, qp):
weight_d0 = dire_label_batch_reg[:, 0:1, :, :] * dire_label_batch_reg[:, 0:1, :, :] + weight_mat[int((qp-22)/5)][0]
weight_d1 = dire_label_batch_reg[:, 1:2, :, :] * dire_label_batch_reg[:, 1:2, :, :] + weight_mat[int((qp-22)/5)][1]
weight_d2 = dire_label_batch_reg[:, 2:3, :, :] * dire_label_batch_reg[:, 2:3, :, :] + weight_mat[int((qp-22)/5)][2]
if qp == 22:
weight_d0 = 1.0
return 0.8 * L1_Loss(bd_out_batch0[:, 0:1, :, :], bt_label_batch[:, 0:1, :, :]) + \
1.0 * L1_Loss(bd_out_batch1[:, 0:1, :, :], bt_label_batch[:, 1:2, :, :]) + \
1.2 * L1_Loss(bd_out_batch2[:, 0:1, :, :], bt_label_batch[:, 2:3, :, :]) + \
1.0 * L1_Loss(weight_d0 * bd_out_batch0[:, 1:2, :, :], weight_d0 * dire_label_batch_reg[:, 0:1, :, :]) + \
1.0 * L1_Loss(weight_d1 * bd_out_batch1[:, 1:2, :, :], weight_d1 * dire_label_batch_reg[:, 1:2, :, :]) + \
1.0 * L1_Loss(weight_d2 * bd_out_batch2[:, 1:2, :, :], weight_d2 * dire_label_batch_reg[:, 2:3, :, :]) + \
0.5 * L1_Loss(weight_d0 * bd_out_batch0[:, 0:1, :, :],
weight_d0 * bt_label_batch[:, 0:1, :, :]) + \
0.5 * L1_Loss(weight_d1 * (bd_out_batch1[:, 0:1, :, :] - bd_out_batch0[:, 0:1, :, :]),
weight_d1 * (bt_label_batch[:, 1:2, :, :] - bt_label_batch[:, 0:1, :, :])) + \
0.5 * L1_Loss(weight_d2 * (bd_out_batch2[:, 0:1, :, :] - bd_out_batch1[:, 0:1, :, :]),
weight_d2 * (bt_label_batch[:, 2:3, :, :] - bt_label_batch[:, 1:2, :, :]))
def loss_func_QBD_val(qt_out_batch, bd_out_batch0, bd_out_batch1, bd_out_batch2, qt_label_batch, bt_label_batch, dire_label_batch_reg, qp):
weight_d0 = dire_label_batch_reg[:, 0:1, :, :] * dire_label_batch_reg[:, 0:1, :, :] + weight_mat[int((qp-22)/5)][0]
weight_d1 = dire_label_batch_reg[:, 1:2, :, :] * dire_label_batch_reg[:, 1:2, :, :] + weight_mat[int((qp-22)/5)][1]
weight_d2 = dire_label_batch_reg[:, 2:3, :, :] * dire_label_batch_reg[:, 2:3, :, :] + weight_mat[int((qp-22)/5)][2]
if qp == 22:
weight_d0 = 1.0
return 1.0 * L1_Loss(qt_out_batch, qt_label_batch) + \
0.8 * L1_Loss(bd_out_batch0[:, 0:1, :, :], bt_label_batch[:, 0:1, :, :]) + \
1.0 * L1_Loss(bd_out_batch1[:, 0:1, :, :], bt_label_batch[:, 1:2, :, :]) + \
1.2 * L1_Loss(bd_out_batch2[:, 0:1, :, :], bt_label_batch[:, 2:3, :, :]) + \
1.0 * L1_Loss(weight_d0 * bd_out_batch0[:, 1:2, :, :], weight_d0 * dire_label_batch_reg[:, 0:1, :, :]) + \
1.0 * L1_Loss(weight_d1 * bd_out_batch1[:, 1:2, :, :], weight_d1 * dire_label_batch_reg[:, 1:2, :, :]) + \
1.0 * L1_Loss(weight_d2 * bd_out_batch2[:, 1:2, :, :], weight_d2 * dire_label_batch_reg[:, 2:3, :, :]) + \
0.5 * L1_Loss(weight_d0 * bd_out_batch0[:, 0:1, :, :],
weight_d0 * bt_label_batch[:, 0:1, :, :]) + \
0.5 * L1_Loss(weight_d1 * (bd_out_batch1[:, 0:1, :, :] - bd_out_batch0[:, 0:1, :, :]),
weight_d1 * (bt_label_batch[:, 1:2, :, :] - bt_label_batch[:, 0:1, :, :])) + \
0.5 * L1_Loss(weight_d2 * (bd_out_batch2[:, 0:1, :, :] - bd_out_batch1[:, 0:1, :, :]),
weight_d2 * (bt_label_batch[:, 2:3, :, :] - bt_label_batch[:, 1:2, :, :]))
@torch.no_grad()
def pre_validation(val_loader, Net, predID, qp=22, args=None):
if predID == 0: # Q
with torch.no_grad():
L1_loss_list = []
accu_list = []
zero_list = []
for step, data in enumerate(val_loader):
input_batch, qt_label_batch = data
input_batch = input_batch.cuda()
qt_label_batch = qt_label_batch.cuda()
qt_out_batch = Net(input_batch)
if args is not None and args.classification:
qt_out_batch = torch.argmax(torch.softmax(qt_out_batch, dim=-1), dim=-1).unsqueeze(1).float()
qt_accuracy = torch.sum(torch.round(qt_out_batch) == qt_label_batch).item() / float(qt_out_batch.numel())
L1_loss = L1_Loss(qt_out_batch, qt_label_batch)
L1_loss_list.append(L1_loss.item())
accu_list.append(qt_accuracy)
zero_list.append( torch.sum(torch.round(qt_out_batch) == 0).item() / float(qt_out_batch.numel()))
del input_batch, qt_label_batch, qt_out_batch
return [np.mean(L1_loss_list), np.mean(accu_list), np.mean(zero_list)]
elif predID == 1: # BD
with torch.no_grad():
val_loss_list = []
b0_L1_loss_list, b1_L1_loss_list, b2_L1_loss_list = [], [], []
d0_L1_loss_list, d1_L1_loss_list, d2_L1_loss_list = [], [], []
b0_accu_list, b1_accu_list, b2_accu_list = [], [], []
d0_accu_list, d1_accu_list, d2_accu_list = [], [], []
for step, data in enumerate(val_loader):
input_batch, qt_label_batch, bt_label_batch, dire_label_batch_reg = data
input_batch = input_batch.cuda()
qt_label_batch = qt_label_batch.cuda()
bt_label_batch = bt_label_batch.cuda()
dire_label_batch_reg = dire_label_batch_reg.cuda()
if args.classification:
out_batch = Net(input_batch, qt_label_batch)
b_out_batch0, b_out_batch1, b_out_batch2, d_out_batch0, d_out_batch1, d_out_batch2 = [torch.argmax(torch.softmax(ele, dim=-1), dim=-1) for ele in out_batch]
d_out_batch0, d_out_batch1, d_out_batch2 = d_out_batch0 - 1, d_out_batch1 - 1, d_out_batch2 - 1
b_out_batch1 += b_out_batch0
b_out_batch2 += b_out_batch1
bd_out_batch0 = torch.stack([b_out_batch0, d_out_batch0], dim=1)
bd_out_batch1 = torch.stack([b_out_batch1, d_out_batch1], dim=1)
bd_out_batch2 = torch.stack([b_out_batch2, d_out_batch2], dim=1)
else:
bd_out_batch0, bd_out_batch1, bd_out_batch2 = Net(input_batch, qt_label_batch)
val_loss = loss_func_MSBD_val(bd_out_batch0, bd_out_batch1, bd_out_batch2, bt_label_batch, dire_label_batch_reg, qp)
val_loss_list.append(val_loss.item())
b0_L1_loss = L1_Loss(bd_out_batch0[:, 0:1, :, :], bt_label_batch[:, 0:1, :, :])
b1_L1_loss = L1_Loss(bd_out_batch1[:, 0:1, :, :], bt_label_batch[:, 1:2, :, :])
b2_L1_loss = L1_Loss(bd_out_batch2[:, 0:1, :, :], bt_label_batch[:, 2:3, :, :])
d0_L1_loss = L1_Loss(bd_out_batch0[:, 1:2, :, :], dire_label_batch_reg[:, 0:1, :, :])
d1_L1_loss = L1_Loss(bd_out_batch1[:, 1:2, :, :], dire_label_batch_reg[:, 1:2, :, :])
d2_L1_loss = L1_Loss(bd_out_batch2[:, 1:2, :, :], dire_label_batch_reg[:, 2:3, :, :])
b0_accuracy = torch.sum(
torch.round(bd_out_batch0[:, 0:1, :, :]) == bt_label_batch[:, 0:1, :, :]).item() / float(
bd_out_batch0[:, 0:1, :, :].numel())
b1_accuracy = torch.sum(
torch.round(bd_out_batch1[:, 0:1, :, :]) == bt_label_batch[:, 1:2, :, :]).item() / float(
bd_out_batch1[:, 0:1, :, :].numel())
b2_accuracy = torch.sum(
torch.round(bd_out_batch2[:, 0:1, :, :]) == bt_label_batch[:, 2:3, :, :]).item() / float(
bd_out_batch2[:, 0:1, :, :].numel())
d0_accuracy = torch.sum(
torch.round(bd_out_batch0[:, 1:2, :, :]) == dire_label_batch_reg[:, 0:1, :, :]).item() / float(
bd_out_batch0[:, 1:2, :, :].numel())
d1_accuracy = torch.sum(
torch.round(bd_out_batch1[:, 1:2, :, :]) == dire_label_batch_reg[:, 1:2, :, :]).item() / float(
bd_out_batch1[:, 1:2, :, :].numel())
d2_accuracy = torch.sum(
torch.round(bd_out_batch2[:, 1:2, :, :]) == dire_label_batch_reg[:, 2:3, :, :]).item() / float(
bd_out_batch2[:, 1:2, :, :].numel())
b0_L1_loss_list.append(b0_L1_loss.item())
b1_L1_loss_list.append(b1_L1_loss.item())
b2_L1_loss_list.append(b2_L1_loss.item())
d0_L1_loss_list.append(d0_L1_loss.item())
d1_L1_loss_list.append(d1_L1_loss.item())
d2_L1_loss_list.append(d2_L1_loss.item())
b0_accu_list.append(b0_accuracy)
b1_accu_list.append(b1_accuracy)
b2_accu_list.append(b2_accuracy)
d0_accu_list.append(d0_accuracy)
d1_accu_list.append(d1_accuracy)
d2_accu_list.append(d2_accuracy)
del input_batch, qt_label_batch, bt_label_batch, dire_label_batch_reg, bd_out_batch0, bd_out_batch1, bd_out_batch2
return [np.mean(b0_L1_loss_list), np.mean(b1_L1_loss_list), np.mean(b2_L1_loss_list),
np.mean(d0_L1_loss_list), np.mean(d1_L1_loss_list), np.mean(d2_L1_loss_list),
np.mean(b0_accu_list), np.mean(b1_accu_list), np.mean(b2_accu_list),
np.mean(d0_accu_list), np.mean(d1_accu_list), np.mean(d2_accu_list), np.mean(val_loss_list)]
elif predID == 2: # D
with torch.no_grad():
loss_list = []
L1_loss_list = []
d0_accu_list, d1_accu_list, d2_accu_list = [], [], []
for step, data in enumerate(val_loader):
input_batch, qt_label_batch, bt_label_batch, dire_label_batch_cla = data
input_batch = input_batch.cuda()
qt_label_batch = qt_label_batch.cuda()
bt_label_batch = bt_label_batch.cuda()
dire_label_batch_cla = dire_label_batch_cla.cuda()
dire_out_batch = Net(input_batch, qt_label_batch, bt_label_batch)
loss = loss_func_D(dire_out_batch, dire_label_batch_cla)
num = dire_out_batch.shape[0]
dire_out_batch_cla = torch.zeros(num, 3, 16, 16).cuda()
for i in range(3):
dire_out_batch_cla[:, i, :, :] = torch.argmax(dire_out_batch[:, i * 3:(i + 1) * 3, :, :], dim=1)
d0_accu = torch.sum(dire_out_batch_cla[:, 0, :, :] == dire_label_batch_cla[:, 0, :, :]).item() \
/ float(dire_out_batch_cla[:, 0, :, :].numel())
d1_accu = torch.sum(dire_out_batch_cla[:, 1, :, :] == dire_label_batch_cla[:, 1, :, :]).item() \
/ float(dire_out_batch_cla[:, 1, :, :].numel())
d2_accu = torch.sum(dire_out_batch_cla[:, 2, :, :] == dire_label_batch_cla[:, 2, :, :]).item() \
/ float(dire_out_batch_cla[:, 2, :, :].numel())
dire_L1_loss = L1_Loss(dire_out_batch_cla.float(), dire_label_batch_cla.float())
loss_list.append(loss.item())
L1_loss_list.append(dire_L1_loss.item())
d0_accu_list.append(d0_accu)
d1_accu_list.append(d1_accu)
d2_accu_list.append(d2_accu)
del input_batch, qt_label_batch, bt_label_batch, dire_label_batch_cla, dire_out_batch, dire_out_batch_cla
return np.mean(loss_list), np.mean(L1_loss_list), np.mean(d0_accu_list), np.mean(d1_accu_list), np.mean(d2_accu_list)
else:
print("Unknown Validation !!!")
return
@torch.no_grad()
def validation_QBD(val_loader, Net_Q, Net_BD, qp=22, args=None):
with torch.no_grad():
val_loss_list = []
q_L1_loss_list, q_accu_list = [], []
b0_L1_loss_list, b1_L1_loss_list, b2_L1_loss_list = [], [], []
d0_L1_loss_list, d1_L1_loss_list, d2_L1_loss_list = [], [], []
b0_accu_list, b1_accu_list, b2_accu_list = [], [], []
d0_accu_list, d1_accu_list, d2_accu_list = [], [], []
# post_algorithm
q_accu_list_post = []
b0_accu_list_post, b1_accu_list_post, b2_accu_list_post = [], [], []
d0_accu_list_post, d1_accu_list_post, d2_accu_list_post = [], [], []
for step, data in enumerate(val_loader):
input_batch, qt_label_batch, bt_label_batch, dire_label_batch_reg = data
input_batch = input_batch.cuda()
qt_label_batch = qt_label_batch.cuda()
bt_label_batch = bt_label_batch.cuda()
dire_label_batch_reg = dire_label_batch_reg.cuda()
# if args.classification:
# qt_out_batch = Net_Q(input_batch)
# qt_out_batch = torch.argmax(torch.softmax(qt_out_batch, dim=-1), dim=-1).unsqueeze(1).float()
# out_batch = Net_BD(input_batch, qt_out_batch)
# b_out_batch0, b_out_batch1, b_out_batch2, d_out_batch0, d_out_batch1, d_out_batch2 = [torch.argmax(torch.softmax(ele, dim=-1), dim=-1) for ele in out_batch]
# d_out_batch0, d_out_batch1, d_out_batch2 = d_out_batch0 - 1, d_out_batch1 - 1, d_out_batch2 - 1
# b_out_batch1 += b_out_batch0
# b_out_batch2 += b_out_batch1
# bd_out_batch0 = torch.stack([b_out_batch0, d_out_batch0], dim=1)
# bd_out_batch1 = torch.stack([b_out_batch1, d_out_batch1], dim=1)
# bd_out_batch2 = torch.stack([b_out_batch2, d_out_batch2], dim=1)
# else:
# qt_out_batch = Net_Q(input_batch)
# bd_out_batch0, bd_out_batch1, bd_out_batch2 = Net_BD(input_batch, qt_out_batch)
if 'SA' in args.model_type:
qt_out_batch = Net_Q(input_batch)
qt_out_batch = torch.argmax(torch.softmax(qt_out_batch, dim=-1), dim=-1).unsqueeze(1).float()
out_batch = Net_BD(input_batch, qt_out_batch)
b_out_batch0, b_out_batch1, b_out_batch2, d_out_batch0, d_out_batch1, d_out_batch2 = [torch.argmax(torch.softmax(ele, dim=-1), dim=-1) for ele in out_batch]
d_out_batch0, d_out_batch1, d_out_batch2 = d_out_batch0 - 1, d_out_batch1 - 1, d_out_batch2 - 1
b_out_batch1 += b_out_batch0
b_out_batch2 += b_out_batch1
bd_out_batch0 = torch.stack([b_out_batch0, d_out_batch0], dim=1)
bd_out_batch1 = torch.stack([b_out_batch1, d_out_batch1], dim=1)
bd_out_batch2 = torch.stack([b_out_batch2, d_out_batch2], dim=1)
else:
qt_out_batch = Net_Q(input_batch)
qt_out_batch = torch.argmax(torch.softmax(qt_out_batch, dim=-1), dim=-1).unsqueeze(1).float()
bd_out_batch0, bd_out_batch1, bd_out_batch2 = Net_BD(input_batch, qt_out_batch)
val_loss = loss_func_QBD_val(qt_out_batch, bd_out_batch0, bd_out_batch1, bd_out_batch2, qt_label_batch,
bt_label_batch, dire_label_batch_reg, qp)
val_loss_list.append(val_loss.item())
q_L1_loss = L1_Loss(qt_out_batch, qt_label_batch)
if args.depth_label:
b0_L1_loss = L1_Loss(bd_out_batch0[:, 0:1, :, :] + F.interpolate(qt_out_batch, scale_factor=2, mode='nearest'), bt_label_batch[:, 0:1, :, :] + F.interpolate(qt_label_batch, scale_factor=2, mode='nearest'))
b1_L1_loss = L1_Loss(bd_out_batch1[:, 0:1, :, :] + F.interpolate(qt_out_batch, scale_factor=2, mode='nearest'), bt_label_batch[:, 1:2, :, :] + F.interpolate(qt_label_batch, scale_factor=2, mode='nearest'))
b2_L1_loss = L1_Loss(bd_out_batch2[:, 0:1, :, :] + F.interpolate(qt_out_batch, scale_factor=2, mode='nearest'), bt_label_batch[:, 2:3, :, :] + F.interpolate(qt_label_batch, scale_factor=2, mode='nearest'))
else:
b0_L1_loss = L1_Loss(bd_out_batch0[:, 0:1, :, :], bt_label_batch[:, 0:1, :, :])
b1_L1_loss = L1_Loss(bd_out_batch1[:, 0:1, :, :], bt_label_batch[:, 1:2, :, :])
b2_L1_loss = L1_Loss(bd_out_batch2[:, 0:1, :, :], bt_label_batch[:, 2:3, :, :])
d0_L1_loss = L1_Loss(bd_out_batch0[:, 1:2, :, :], dire_label_batch_reg[:, 0:1, :, :])
d1_L1_loss = L1_Loss(bd_out_batch1[:, 1:2, :, :], dire_label_batch_reg[:, 1:2, :, :])
d2_L1_loss = L1_Loss(bd_out_batch2[:, 1:2, :, :], dire_label_batch_reg[:, 2:3, :, :])
q_accuracy = torch.sum(torch.round(qt_out_batch.float()) == qt_label_batch).item() / float(qt_out_batch.numel())
b0_accuracy = torch.sum(
torch.round(bd_out_batch0[:, 0:1, :, :].float()) == bt_label_batch[:, 0:1, :, :]).item() / float(
bd_out_batch0[:, 0:1, :, :].numel())
b1_accuracy = torch.sum(
torch.round(bd_out_batch1[:, 0:1, :, :].float()) == bt_label_batch[:, 1:2, :, :]).item() / float(
bd_out_batch1[:, 0:1, :, :].numel())
b2_accuracy = torch.sum(
torch.round(bd_out_batch2[:, 0:1, :, :].float()) == bt_label_batch[:, 2:3, :, :]).item() / float(
bd_out_batch2[:, 0:1, :, :].numel())
d0_accuracy = torch.sum(
torch.round(bd_out_batch0[:, 1:2, :, :].float()) == dire_label_batch_reg[:, 0:1, :, :]).item() / float(
bd_out_batch0[:, 1:2, :, :].numel())
d1_accuracy = torch.sum(
torch.round(bd_out_batch1[:, 1:2, :, :].float()) == dire_label_batch_reg[:, 1:2, :, :]).item() / float(
bd_out_batch1[:, 1:2, :, :].numel())
d2_accuracy = torch.sum(
torch.round(bd_out_batch2[:, 1:2, :, :].float()) == dire_label_batch_reg[:, 2:3, :, :]).item() / float(
bd_out_batch2[:, 1:2, :, :].numel())
q_L1_loss_list.append(q_L1_loss.item())
b0_L1_loss_list.append(b0_L1_loss.item())
b1_L1_loss_list.append(b1_L1_loss.item())
b2_L1_loss_list.append(b2_L1_loss.item())
d0_L1_loss_list.append(d0_L1_loss.item())
d1_L1_loss_list.append(d1_L1_loss.item())
d2_L1_loss_list.append(d2_L1_loss.item())
q_accu_list.append(q_accuracy)
b0_accu_list.append(b0_accuracy)
b1_accu_list.append(b1_accuracy)
b2_accu_list.append(b2_accuracy)
d0_accu_list.append(d0_accuracy)
d1_accu_list.append(d1_accuracy)
d2_accu_list.append(d2_accuracy)
if args.post_test:
# post-algorithm
qt_out_batch_post = torch.from_numpy(eli_structual_error(qt_out_batch).cpu().numpy().squeeze(axis=1)).to(qt_out_batch.device)
bd_out_batch0_post, bd_out_batch1_post, bd_out_batch2_post = torch.zeros_like(bd_out_batch0).cpu().numpy(), torch.zeros_like(bd_out_batch1).cpu().numpy(), torch.zeros_like(bd_out_batch2).cpu().numpy()
bt_map, dire_map = torch.stack([bd_out_batch0[:,0], bd_out_batch1[:,0], bd_out_batch2[:,0]], dim=1), torch.stack([bd_out_batch0[:,1], bd_out_batch1[:,1], bd_out_batch2[:,1]], dim=1)
for block_id in range(qt_out_batch.shape[0]):
out_bt_map, out_dire_map = map_to_parititon(qt_out_batch_post[block_id].cpu().numpy(), bt_map[block_id].cpu().numpy(), dire_map[block_id].cpu().numpy(), 1, debug_mode=True)
bd_out_batch0_post[block_id, 0], bd_out_batch1_post[block_id, 0], bd_out_batch2_post[block_id, 0] = torch.from_numpy(out_bt_map[0]), torch.from_numpy(out_bt_map[1]), torch.from_numpy(out_bt_map[2])
bd_out_batch0_post[block_id, 1], bd_out_batch1_post[block_id, 1], bd_out_batch2_post[block_id, 1] = torch.from_numpy(out_dire_map[0]), torch.from_numpy(out_dire_map[1]), torch.from_numpy(out_dire_map[2])
bd_out_batch0_post, bd_out_batch1_post, bd_out_batch2_post = torch.from_numpy(bd_out_batch0_post).cuda(), torch.from_numpy(bd_out_batch1_post).cuda(), torch.from_numpy(bd_out_batch2_post).cuda()
q_accuracy_post = torch.sum(torch.round(qt_out_batch_post.float()) == qt_label_batch.squeeze(1)).item() / float(qt_out_batch.numel())
b0_accuracy_post = torch.sum(
torch.round(bd_out_batch0_post[:, 0:1, :, :].float()) == bt_label_batch[:, 0:1, :, :]).item() / float(
bd_out_batch0_post[:, 0:1, :, :].numel())
b1_accuracy_post = torch.sum(
torch.round(bd_out_batch1_post[:, 0:1, :, :].float()) == bt_label_batch[:, 1:2, :, :]).item() / float(
bd_out_batch1_post[:, 0:1, :, :].numel())
b2_accuracy_post = torch.sum(
torch.round(bd_out_batch2_post[:, 0:1, :, :].float()) == bt_label_batch[:, 2:3, :, :]).item() / float(
bd_out_batch2_post[:, 0:1, :, :].numel())
d0_accuracy_post = torch.sum(
torch.round(bd_out_batch0_post[:, 1:2, :, :].float()) == dire_label_batch_reg[:, 0:1, :, :]).item() / float(
bd_out_batch0_post[:, 1:2, :, :].numel())
d1_accuracy_post = torch.sum(
torch.round(bd_out_batch1_post[:, 1:2, :, :].float()) == dire_label_batch_reg[:, 1:2, :, :]).item() / float(
bd_out_batch1_post[:, 1:2, :, :].numel())
d2_accuracy_post = torch.sum(
torch.round(bd_out_batch2_post[:, 1:2, :, :].float()) == dire_label_batch_reg[:, 2:3, :, :]).item() / float(
bd_out_batch2_post[:, 1:2, :, :].numel())
q_accu_list_post.append(q_accuracy_post)
b0_accu_list_post.append(b0_accuracy_post)
b1_accu_list_post.append(b1_accuracy_post)
b2_accu_list_post.append(b2_accuracy_post)
d0_accu_list_post.append(d0_accuracy_post)
d1_accu_list_post.append(d1_accuracy_post)
d2_accu_list_post.append(d2_accuracy_post)
del input_batch, qt_label_batch, bt_label_batch, dire_label_batch_reg, qt_out_batch, bd_out_batch0, bd_out_batch1, bd_out_batch2
if args.post_test:
return [np.mean(q_L1_loss_list), np.mean(b0_L1_loss_list), np.mean(b1_L1_loss_list), np.mean(b2_L1_loss_list),
np.mean(d0_L1_loss_list), np.mean(d1_L1_loss_list), np.mean(d2_L1_loss_list),
np.mean(q_accu_list), np.mean(b0_accu_list), np.mean(b1_accu_list), np.mean(b2_accu_list),
np.mean(d0_accu_list), np.mean(d1_accu_list), np.mean(d2_accu_list),
np.mean(val_loss_list),
np.mean(q_accu_list_post), np.mean(b0_accu_list_post), np.mean(b1_accu_list_post), np.mean(b2_accu_list_post),
np.mean(d0_accu_list_post), np.mean(d1_accu_list_post), np.mean(d2_accu_list_post),]
else:
return [np.mean(q_L1_loss_list), np.mean(b0_L1_loss_list), np.mean(b1_L1_loss_list), np.mean(b2_L1_loss_list),
np.mean(d0_L1_loss_list), np.mean(d1_L1_loss_list), np.mean(d2_L1_loss_list),
np.mean(q_accu_list), np.mean(b0_accu_list), np.mean(b1_accu_list), np.mean(b2_accu_list),
np.mean(d0_accu_list), np.mean(d1_accu_list), np.mean(d2_accu_list),
np.mean(val_loss_list)]
@torch.no_grad()
def inference_pre_QBD(infe_loader_QB, Net_Q, Net_BD): # for overall inference
total_qt_out_batch = torch.zeros((1, 1, 8, 8))
total_bt_out_batch = torch.zeros((1, 3, 16, 16))
total_dire_out_batch_reg = torch.zeros((1, 3, 16, 16))
with torch.no_grad():
for step, data in enumerate(infe_loader_QB):
# print("step: ", step)
input_batch = data[0]
input_batch = input_batch.cuda()
qt_out_batch = Net_Q(input_batch)
bd_out_batch0, bd_out_batch1, bd_out_batch2 = Net_BD(input_batch, qt_out_batch)
bt_out_batch = torch.cat(
[bd_out_batch0[:, 0:1, :, :], bd_out_batch1[:, 0:1, :, :], bd_out_batch2[:, 0:1, :, :]], 1)
dire_out_batch = torch.cat(
[bd_out_batch0[:, 1:2, :, :], bd_out_batch1[:, 1:2, :, :], bd_out_batch2[:, 1:2, :, :]], 1)
# dire_out_batch = Net_D(input_batch, qt_out_batch, bt_out_batch)
# qt_out_batch = torch.round(qt_out_batch).type(torch.int8)
# bt_out_batch = torch.round(bt_out_batch).type(torch.int8)
# dire_out_batch = torch.round(dire_out_batch).type(torch.int8)
total_qt_out_batch = torch.cat([total_qt_out_batch, qt_out_batch.cpu()], 0)
total_bt_out_batch = torch.cat([total_bt_out_batch, bt_out_batch.cpu()], 0)
total_dire_out_batch_reg = torch.cat([total_dire_out_batch_reg, dire_out_batch.cpu()], 0)
if step % 100 == 0:
print("Number of finished blocks: ", total_qt_out_batch.shape[0])
# del input_batch, qt_out_batch, bt_out_batch
total_qt_out_batch = total_qt_out_batch[1:]
total_bt_out_batch = total_bt_out_batch[1:]
total_dire_out_batch_reg = total_dire_out_batch_reg[1:]
return total_qt_out_batch, total_bt_out_batch, total_dire_out_batch_reg
@torch.no_grad()
def inference_pre_SepQBD(infe_loader_QB, Net_Q, Net_B, Net_D): # for overall inference
total_qt_out_batch = torch.zeros(1, 1, 8, 8)
total_bt_out_batch = torch.zeros(1, 3, 16, 16)
total_dire_out_batch_cla = torch.zeros(1, 3, 16, 16)
with torch.no_grad():
for step, data in enumerate(infe_loader_QB):
# print("step: ", step)
input_batch = data[0]
input_batch = input_batch.cuda()
qt_out_batch = Net_Q(input_batch)
bt_out_batch0, bt_out_batch1, bt_out_batch2 = Net_B(input_batch, qt_out_batch)
bt_out_batch = torch.cat([bt_out_batch0, bt_out_batch1, bt_out_batch2], 1)
dire_out_batch = Net_D(input_batch, qt_out_batch, bt_out_batch)
num = dire_out_batch.shape[0]
dire_out_batch_cla = torch.zeros(num, 3, 16, 16).cuda()
for i in range(3):
dire_out_batch_cla[:, i, :, :] = torch.argmax(dire_out_batch[:, i * 3:(i + 1) * 3, :, :], dim=1)
total_qt_out_batch = torch.cat([total_qt_out_batch, qt_out_batch.cpu()], 0)
total_bt_out_batch = torch.cat([total_bt_out_batch, bt_out_batch.cpu()], 0)
total_dire_out_batch_cla = torch.cat([total_dire_out_batch_cla, dire_out_batch_cla.cpu()], 0)
if step % 100 == 0:
print("Number of finished blocks: ", total_qt_out_batch.shape[0])
# del input_batch, qt_out_batch, bt_out_batch
total_qt_out_batch = total_qt_out_batch[1:]
total_bt_out_batch = total_bt_out_batch[1:]
total_dire_out_batch_cla = total_dire_out_batch_cla[1:]
return total_qt_out_batch, total_bt_out_batch, total_dire_out_batch_cla
#****************************************************************************************************************
# Joint Train
#****************************************************************************************************************
def Load_VP_Dataset(path, QP, batchSize, datasetID=0, isLuma=True, isQB=True, Net_QB=None):
# [train validation test] [0 1 2] VVC partition
if isLuma:
comp = ['Luma', 'Y']
block_size = '68'
else:
comp = ['Chroma', 'U', 'V']
block_size = '34'
tr_val_test = ['Train', 'Validate', 'TestSub']
dataset_type = tr_val_test[datasetID]
print('Start loading ' + comp[0] + ' ' + dataset_type + ' dataset...')
input_path = os.path.join(path, dataset_type + '_' + comp[1] + '_Block' + block_size + '.npy')
print('input path:', input_path)
input_batch = torch.FloatTensor(np.expand_dims(np.load(input_path), 1))
if not isLuma: # chroma input
input_path1 = os.path.join(path, dataset_type + '_' + comp[2] + '_Block' + block_size + '.npy')
print('input path1:', input_path1)
input_batch1 = torch.FloatTensor(np.expand_dims(np.load(input_path1), 1))
input_batch = torch.cat([input_batch, input_batch1], 1) # concat U V component
del input_batch1
print('input_batch.shape:', input_batch.shape)
if isQB: # QB dataset
qt_label_path = os.path.join(path, dataset_type + '_' + comp[0] + '_QP' + str(QP) + '_QTdepth_Block8.npy')
bt_label_path = os.path.join(path, dataset_type + '_' + comp[0] + '_QP' + str(QP) + '_BTdepth_Block16.npy')
print('qt_label path:', qt_label_path)
print('bt_label path:', bt_label_path)
qt_label_batch = torch.FloatTensor(np.expand_dims(np.load(qt_label_path), 1) - 1) # qt depth start form 1
bt_label_batch = torch.FloatTensor(np.expand_dims(np.load(bt_label_path), 1))
# bt_label_batch += F.interpolate(qt_label_batch, scale_factor=2) * 2.0
print('qt_label_batch.shape:', qt_label_batch.shape)
print('bt_label_batch.shape:', bt_label_batch.shape)
print("Creating QB data loader...")
dataset = TensorDataset(input_batch, qt_label_batch, bt_label_batch)
dataLoader = DataLoader(dataset=dataset, num_workers=2,
batch_size=batchSize,
pin_memory=True,
shuffle=True)
else: # D dataset
print("Creating inference data loader...")
infe_dataset = TensorDataset(input_batch)
infe_loader = DataLoader(dataset=infe_dataset, num_workers=2, batch_size=batchSize, pin_memory=True, shuffle=False)
qt_out_batch, bt_out_batch = inference_QB(infe_loader, Net_QB)
dire_label_path = os.path.join(path, dataset_type + '_' + comp[0] + '_QP' + str(QP) + '_MSdirection_Block16.npy')
print('direction_label path:', dire_label_path)
dire_label_batch_reg = torch.LongTensor(np.load(dire_label_path))
dire_label_batch_cla = torch.LongTensor(
torch.where(dire_label_batch_reg == -1, torch.full_like(dire_label_batch_reg, 2), dire_label_batch_reg))
del dire_label_batch_reg
print('qt_out_batch.shape:', qt_out_batch.shape)
print('bt_out_batch.shape:', bt_out_batch.shape)
print('dire_label_batch.shape:', dire_label_batch_cla.shape)
print("Creating D data loader...")
dataset = TensorDataset(input_batch, qt_out_batch, bt_out_batch, dire_label_batch_cla)
dataLoader = DataLoader(dataset=dataset, num_workers=2, batch_size=batchSize, pin_memory=True, shuffle=True)
return dataLoader
@torch.no_grad()
def validation_BD(val_loader, Net_B, Net_D):
bt_loss_list = []
bt_L1_loss_list = []
bt_accu_list = []
dire_loss_list = []
dire_L1_loss_list = []
dire_accu_list = []
with torch.no_grad():
for step, data in enumerate(val_loader):
input_batch, qt_label_batch, bt_label_batch, dire_label_batch_cla = data
input_batch = input_batch.cuda()
qt_label_batch = qt_label_batch.cuda()
bt_label_batch = bt_label_batch.cuda()
dire_label_batch_cla = dire_label_batch_cla.cuda()
bt_out_batch = Net_B(input_batch, qt_label_batch)
dire_out_batch = Net_D(input_batch, qt_label_batch, bt_out_batch)
dire_loss = loss_func_D(dire_out_batch, dire_label_batch_cla)
num = dire_out_batch.shape[0]
dire_out_batch_cla = torch.zeros(num, 3, 16, 16).cuda()
for i in range(3):
dire_out_batch_cla[:, i, :, :] = torch.argmax(dire_out_batch[:, i * 3:(i + 1) * 3, :, :], dim=1)
dire_accuracy = torch.sum(dire_out_batch_cla == dire_label_batch_cla).item() / dire_out_batch_cla.numel()
dire_L1_loss = L1_Loss(dire_out_batch_cla.float(), dire_label_batch_cla.float())
bt_loss = Mul_Scale_L1Loss(bt_out_batch, bt_label_batch)
bt_L1_loss = L1_Loss(bt_out_batch, bt_label_batch)
bt_accuracy = torch.sum(torch.round(bt_out_batch) == bt_label_batch).item() / bt_out_batch.numel()
bt_loss_list.append(bt_loss.item())
bt_L1_loss_list.append(bt_L1_loss.item())
bt_accu_list.append(bt_accuracy)
dire_loss_list.append(dire_loss.item())
dire_L1_loss_list.append(dire_L1_loss.item())
dire_accu_list.append(dire_accuracy)
del input_batch, qt_label_batch, bt_label_batch, dire_label_batch_cla, bt_out_batch, dire_out_batch, dire_out_batch
return np.mean(bt_loss_list), np.mean(bt_L1_loss_list), np.mean(bt_accu_list), np.mean(dire_loss_list), np.mean(dire_L1_loss_list), np.mean(dire_accu_list)
@torch.no_grad()
def inference_QB(infe_loader_QB, Net_QB): # for Net_D training
total_qt_out_batch = torch.zeros(1, 1, 8, 8).cuda()
total_bt_out_batch = torch.zeros(1, 1, 16, 16).cuda()
with torch.no_grad():
for step, data in enumerate(infe_loader_QB):
input_batch = data[0]
input_batch = input_batch.cuda()
qt_out_batch, bt_out_batch = Net_QB(input_batch)
total_qt_out_batch = torch.cat([total_qt_out_batch, qt_out_batch], 0)
total_bt_out_batch = torch.cat([total_bt_out_batch, bt_out_batch], 0)
# del input_batch, qt_out_batch, bt_out_batch
total_qt_out_batch = total_qt_out_batch[1:]
total_bt_out_batch = total_bt_out_batch[1:]
return total_qt_out_batch, total_bt_out_batch
#****************************************************************************************************************
# Inference
#****************************************************************************************************************
@torch.no_grad()
def inference_QBD(infe_loader_QB, Net_QB, Net_D): # for overall inference
total_qt_out_batch = torch.zeros(1, 1, 8, 8)
total_bt_out_batch = torch.zeros(1, 3, 16, 16)
total_dire_out_batch_cla = torch.zeros(1, 3, 16, 16)
with torch.no_grad():
for step, data in enumerate(infe_loader_QB):
# print("step: ", step)
input_batch = data[0]
input_batch = input_batch.cuda()
qt_out_batch, bt_out_batch = Net_QB(input_batch)
dire_out_batch = Net_D(input_batch, qt_out_batch, bt_out_batch)
num = dire_out_batch.shape[0]
dire_out_batch_cla = torch.zeros(num, 3, 16, 16).cuda()
for i in range(3):
dire_out_batch_cla[:, i, :, :] = torch.argmax(dire_out_batch[:, i * 3:(i + 1) * 3, :, :], dim=1)
total_qt_out_batch = torch.cat([total_qt_out_batch, qt_out_batch.cpu()], 0)
total_bt_out_batch = torch.cat([total_bt_out_batch, bt_out_batch.cpu()], 0)
total_dire_out_batch_cla = torch.cat([total_dire_out_batch_cla, dire_out_batch_cla.cpu()], 0)
print("Number of finished blocks: ", total_qt_out_batch.shape[0])
# del input_batch, qt_out_batch, bt_out_batch
total_qt_out_batch = total_qt_out_batch[1:]
total_bt_out_batch = total_bt_out_batch[1:]
total_dire_out_batch_cla = total_dire_out_batch_cla[1:]
return total_qt_out_batch, total_bt_out_batch, total_dire_out_batch_cla
#****************************************************************************************************************
# Post Process Metrics
#****************************************************************************************************************
def check_square_unity(mat): # input 4*4 tensor
num0 = len(torch.where(mat == 0)[0])
if num0 >= 0 and num0 <= 12: # 0 in the minority
mat = torch.where(mat == 0, torch.full_like(mat, 1).cuda(), mat)
# process 4 sub-mats
for i in [0, 2]:
for j in [0, 2]:
sum_sub_mat = torch.sum(mat[i:i + 2, j:j + 2])
if sum_sub_mat <= 10 and sum_sub_mat >= 5: # 1 and 2 or 3 mixed
sub_num1 = len(torch.where(mat[i:i + 2, j:j + 2] == 1)[0])
if sub_num1 < 3:
mat[i:i + 2, j:j + 2] = torch.where(mat[i:i + 2, j:j + 2] == 1, (torch.ones((2, 2)) * 2).cuda(), mat[i:i + 2, j:j + 2])
else:
mat[i:i + 2, j:j + 2] = torch.ones((2, 2)).cuda()
elif num0 > 12 and num0 < 16:
mat = torch.zeros((4, 4)).cuda()
return mat
def eli_structual_error(out_batch):
N = out_batch.shape[0]
pooled_batch = torch.clamp(torch.round(F.max_pool2d(out_batch, 2)), min=0, max=3)
for num in range(N):
pooled_batch[num][0] = check_square_unity(pooled_batch[num][0])
post_batch = F.interpolate(pooled_batch, scale_factor=2)
del pooled_batch
return post_batch
def get_norm_block(depth, x, y, norm_block, qt_map, block_size):
cur_depth = qt_map[x, y]
if cur_depth == depth: # end partition
sub_size = block_size >> depth
scale = block_size // 8
block_x = x * scale
block_y = y * scale
block_mean = torch.mean(norm_block[block_x:block_x+sub_size, block_y:block_y+sub_size])
# block_std = torch.std(norm_block[block_x:block_x+sub_size, block_y:block_y+sub_size])
# if block_std == 0:
# block_std = 1
# normalize
norm_block[block_x:block_x+sub_size, block_y:block_y+sub_size] -= block_mean
return
elif cur_depth > depth: # carry on partition
sub_map_size = 8 >> depth
for i_offset in range(2):
for j_offset in range(2):
get_norm_block(depth + 1, x + i_offset * sub_map_size // 2, y + j_offset * sub_map_size // 2, norm_block, qt_map, block_size)
return
# normalize the input block according to qt map
def block_qtnode_norm(qt_map, block, isLuma=True):
b, c, h, w = block.shape
if isLuma:
block_size = 64
else:
block_size = 32
# post_qt_map = eli_structual_error(qt_map)
post_qt_map = torch.clamp(torch.round(qt_map), min=0, max=3).cuda()
norm_block = torch.FloatTensor(b, c, block_size, block_size).cuda()
norm_block[:, :, :, :] = block[:, :, h-block_size:h, w-block_size:w].detach()
for i in range(b):
for j in range(c):
get_norm_block(0, 0, 0, norm_block[i][j], post_qt_map[i][0], block_size)
del post_qt_map
return Variable(norm_block, requires_grad=False)
def remove_prefix(state_dict, prefix):
f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
return {f(key): value for key, value in state_dict.items()}
def load_pretrain_model(current_model, pretrain_model):
source_dict = torch.load(pretrain_model)
if "state_dict" in source_dict.keys():
source_dict = remove_prefix(source_dict['state_dict'], 'module.')
else:
source_dict = remove_prefix(source_dict, 'module.')
dest_dict = current_model.state_dict()
trained_dict = {k: v for k, v in source_dict.items() if k in dest_dict and source_dict[k].shape == dest_dict[k].shape}
dest_dict.update(trained_dict)
current_model.load_state_dict(dest_dict)
# for k, v in trained_dict.items():
# if "conv_d2.bias" in k:
# print(k)
# print(v)
# for k, v in dest_dict.items():
# if "conv_d2.bias" in k:
# print(k)
# print(v)
return current_model
def load_sequences_info():
num = 22
seqs_info_path = r"E:\VVC-Fast-Partition-DP\Code\Debug\VVC_Test_Sequences.txt"
seqs_info_fp = open(seqs_info_path, 'r')
data = []
for line in seqs_info_fp:
if "end!!!!" in line:
break
data.append(line.rstrip('\n').split(','))
seqs_info_fp.close()
data = np.array(data)
print(data.shape)
seqs_name = data[:num, 0]
seqs_path_name = data[:num, 1]
seqs_width = data[:num, 2].astype(np.int64) # enough bits for calculating h*w
seqs_height = data[:num, 3].astype(np.int64)
seqs_frmnum = data[:num, 4].astype(np.int64)
sub_frmnum_list, block_num_list = [], []
for i in range(num):
SubSampleRatio = 30
if i >= 79:
SubSampleRatio = 1
SubSampleRatio = 8
sub_frmnum = (seqs_frmnum[i] + SubSampleRatio - 1) // SubSampleRatio
sub_frmnum_list.append(sub_frmnum)
block_num = (seqs_width[i] // 64) * (seqs_height[i] // 64) * sub_frmnum
block_num_list.append(block_num)
return seqs_path_name, seqs_width, seqs_height, sub_frmnum_list, block_num_list
def post_process(qt_out_batch, bt_out_batch, dire_out_batch, comp, qp, save_dir):
if comp == "Luma":
is_luma = True
else:
is_luma = False
qt_out_batch = eli_structual_error(qt_out_batch).cpu().numpy().squeeze(axis=1)
# dire_out_batch_cla = dire_out_batch_cla.cpu().numpy()
start_block_id = 0
seqs_path_name, seqs_width, seqs_height, sub_frmnum_list, block_num_list = load_sequences_info()
for seq_id in range(0, 22):
seq_name = seqs_path_name[seq_id].rstrip(".yuv")
width = seqs_width[seq_id]
height = seqs_height[seq_id]
sub_frmnum = sub_frmnum_list[seq_id]
block_num = block_num_list[seq_id]
print(comp, qp, seq_name)
input_qt_batch = qt_out_batch[start_block_id:start_block_id + block_num]
input_bt_batch = bt_out_batch[start_block_id:start_block_id + block_num]
input_dire_batch = dire_out_batch[start_block_id:start_block_id + block_num]
start_block_id += block_num
save_path = os.path.join(save_dir, seq_name + "_" + comp + "_QP" + str(qp) + "_PartitionMat.txt")
print("Save:", save_path)
get_sequence_partition_for_VTM(qt_map=input_qt_batch, bt_map=input_bt_batch, dire_map=input_dire_batch,
is_luma=is_luma,
save_path=save_path, frm_num=sub_frmnum, frm_width=width, frm_height=height)
del qt_out_batch
def seq_post_process(input_qt_batch, input_bt_batch, input_dire_batch, comp, sub_numfrm, width, height, save_path):
if comp == "Luma":
is_luma = True
else:
is_luma = False
input_qt_batch = eli_structual_error(input_qt_batch).cpu().numpy().squeeze(axis=1)
#input_qt_batch = torch.clamp(torch.round(input_qt_batch), min=0, max=3).cpu().numpy().squeeze(axis=1)
get_sequence_partition_for_VTM(qt_map=input_qt_batch, bt_map=input_bt_batch, dire_map=input_dire_batch,
is_luma=is_luma,
save_path=save_path, frm_num=sub_numfrm, frm_width=width, frm_height=height)