-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothermain.py
1030 lines (938 loc) · 47.4 KB
/
othermain.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
import pdb
import os
os.environ['CUDA_VISIBLE_DEVICES']='3'
import numpy as np
import cv2
# 读取txt文件中的顶点坐标
import torch
import os.path as osp
import pickle
import glob
from third_parties.lpips import LPIPS
from third_parties.lpips import LPIPS
from skimage.metrics import structural_similarity as compare_ssim
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from skimage.metrics import mean_squared_error as compare_mse
def load_points():
points = np.loadtxt("/mnt/data2/lcj/SelfReconCode2/nerfcaptemplates hapeT.txt")
points=torch.Tensor(points)
# 创建mesh对象
triangles = o3d.utility.Vector3iVector(np.zeros((0, 3), dtype=int))
mesh = o3d.geometry.TriangleMesh(o3d.utility.Vector3dVector(points), triangles)
# 为mesh添加颜色
mesh.paint_uniform_color([0.7, 0.7, 0.7])
# 显示mesh
o3d.visualization.draw_geometries([mesh])
# 输出为.ply文件
o3d.io.write_triangle_mesh("output.ply", mesh)
# vedio2img
def vedio2img():
video_path = '/mnt/data2/lcj/dataset/MonoPerfCapDataset/Franzi_studio/'
folder_name = video_path + "masks" # imgs & masks
os.makedirs(folder_name, exist_ok=True)
vc = cv2.VideoCapture(video_path + "mask.mp4") # 读入视频文件
c = 0
rval = vc.isOpened()
while rval: # 循环读取视频帧
rval, frame = vc.read()
pic_path = folder_name + '/'
if rval:
cv2.imwrite(pic_path + '%06d.png'%c, frame) # 存储为图像,保存名为 文件夹名_数字(第几个文件).jpg
cv2.waitKey(1)
else:
break
c = c + 1
vc.release()
print('save_success')
# img2vedio
def img2vedio():
image_folder = '/mnt/data2/lcj/humannerf-main/experiments/human_nerf/wild/red/single_gpu/latest/movement'
output_path = '/mnt/data2/lcj/humannerf-main/experiments/human_nerf/wild/red/single_gpu/latest/color.mp4'
fps = 30
images = sorted(os.listdir(image_folder)) # 根据首位sort,所以命名不是很好的图像无法生成比较好的视频
num=len(images)
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, _ = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 或者使用其他的视频编码器
video_writer = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
for i in range(1000):
image_path = os.path.join(image_folder, '%06d.png' % (i))
frame = cv2.imread(image_path)
video_writer.write(frame)
# 命名规范的时候可以用
# for image_name in images:
# image_path = os.path.join(image_folder, image_name)
# frame = cv2.imread(image_path)
# video_writer.write(frame)
video_writer.release()
# camera.pkl-->camera.npz
def pkl2npz():
# 新增加extrinsic,外参矩阵
ps = np.array([981.4922,526.2165])
fs = np.array([1515.6488,1516.3548])
trans = np.zeros(3) # 啥也不是 写进pkl的时候就是0
rt = np.zeros(3)
# extrinsic = cam_data['camera_e']
assert (np.linalg.norm(
rt) < 0.0001) # The cameras of snapshot dataset seems no rotation and translation 相机固定的意思
H = 1080
W = 1920
quat = np.array([np.cos(np.pi / 2.), 0., 0., np.sin(np.pi / 2.)])
T = trans
fx = fs[0]
fy = fs[1]
cx = ps[0]
cy = ps[1]
np.savez(osp.join('/mnt/data2/lcj/dataset/MonoPerfCapDataset/Franzi_studio/', 'camera.npz'), fx=fx, fy=fy, cx=cx, cy=cy, quat=quat,
T=T)
print('pkl2npz is ok!')
# 可视化mask之间的差距 #
def vis_mask_delta():
device = torch.device(0)
folder_path = "/mnt/data2/lcj/dataset/deepcap/Antonia1"
gt_mask_folder_path = os.path.join(folder_path, "masks")
result_folder_path=os.path.join(folder_path, "result_origin200")
pred_mask_folder_path = os.path.join(result_folder_path, "masks1")
gt_image_files = glob.glob(os.path.join(gt_mask_folder_path, "*.png"))
num_images = len(gt_image_files)
print("Number of images in folder:", num_images)
mask_delta_save_folder_path=os.path.join(pred_mask_folder_path, "masks_delta")
os.makedirs(mask_delta_save_folder_path, exist_ok = True)
writer_mask_delta = cv2.VideoWriter(osp.join(mask_delta_save_folder_path, 'mask_delta.mp4'),
cv2.VideoWriter.fourcc('m', 'p', '4', 'v'), 30., (1920, 1080))
for i in range(num_images):
print(i)
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
gt_mask = cv2.imread(gt_mask_path)/255.
pred_mask_path = osp.join(pred_mask_folder_path, '%d.png' % i)
pred_mask = cv2.imread(pred_mask_path)/255.
mask_delta = (np.abs(pred_mask - gt_mask) * 255.).astype(np.uint8)
writer_mask_delta.write(mask_delta)
cv2.imwrite(osp.join(mask_delta_save_folder_path, '%06d.png' %i), mask_delta)
torch.cuda.empty_cache()
writer_mask_delta.release()
print("Done!")
# 原论文对mask的操作 #
# mask>0 and dilation #
def original_mask_operation():
device = torch.device(0)
folder_path="/mnt/data2/lcj/dataset/deepcap/Magdalena0"
gt_mask_original_process_save_path = os.path.join(folder_path, "masks_origin_process")
gt_mask_0_process_save_path=os.path.join(folder_path, "masks>0_process")
gt_mask_folder_path = os.path.join(folder_path, "masks")
gt_image_files = glob.glob(os.path.join(gt_mask_folder_path, "*.png"))
num_images = len(gt_image_files)
print("Number of images in folder:", num_images)
for i in range(num_images):
print(i)
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
# 读取mask
gt_mask=(torch.from_numpy(cv2.imread(gt_mask_path))>0).view(1080,1920,-1).any(-1).float().to(device)
gt_mask0=gt_mask
gtMasks0=(gt_mask0 * 255.).detach().cpu().numpy().astype(np.uint8)
cv2.imwrite(osp.join(gt_mask_0_process_save_path, '%06d.png' % i), gtMasks0)
radius=int(np.round(0.0041/ 2. * float(min(1080, 1920)) / 1.2))
mgtMs = torch.nn.functional.max_pool2d(gt_mask.reshape(1,1080,1920), kernel_size=2 * radius + 1, stride=1, padding=radius)
gtMasks = (mgtMs * 255.).detach().cpu().numpy().astype(np.uint8)
cv2.imwrite(osp.join(gt_mask_original_process_save_path, '%06d.png' %i), gtMasks.reshape(1080,1920))
torch.cuda.empty_cache()
print("mask operation is ok!")
###### metircs ######
### color ###
## 只在mask中计算 ##
# def psnr(pred_img, gt_img, peak=255.):
# pdb.set_trace()
# return 10 * torch.log10(peak ** 2 / torch.mean((1. * pred_img - 1. * gt_img) ** 2))
#
# def dssim(pred_img, gt_img, range=255.):
# from skimage.measure import compare_ssim
# return (1 - compare_ssim(pred_img, gt_img, data_range=range, multichannel=True)) / 2.
# [0,1]->[-1,1]
def scale_for_lpips(image_tensor):
return image_tensor * 2. - 1.
def compute_lpips(lpips,pred_img, gt_img):
lpips_metric = lpips(scale_for_lpips(pred_img.permute(2, 0, 1)),
scale_for_lpips(gt_img.permute(2, 0, 1))) # (1080,1920,3)-->(3, 1080, 1920)
return lpips_metric
def compute_iou(pred_mask, gt_mask):
# 要换掉,现在的方法好像只适用于二进制
"""
Calculate intersection over union (IoU) of two binary masks.
Args:
mask1: numpy array of shape (H, W)
mask2: numpy array of shape (H, W)
Returns:
IoU: float, the IoU of the two masks
"""
# intersection = np.logical_and(pred_mask, gt_mask)
# union = np.logical_or(pred_mask, gt_mask)
# iou_score = np.sum(intersection) / np.sum(union)
iou_score = (pred_mask * gt_mask).reshape(-1).sum(0) / (np.abs((pred_mask + gt_mask - pred_mask * gt_mask)).reshape(-1).sum(0))
return iou_score
### color ###
def set_requires_grad(nets, requires_grad=False):
if not isinstance(nets, list):
nets = [nets]
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad
def compute_metrics():
device = torch.device(0)
folder_path="/mnt/data2/lcj/dataset/DynaCap/red/"
gt_img_folder_path =os.path.join(folder_path, "imgs")
gt_mask_folder_path = os.path.join(folder_path, "masks")
gt_normal_folder_path = os.path.join(folder_path, "normals")
gt_image_files = glob.glob(os.path.join(gt_img_folder_path, "*.png"))
num_images = len(gt_image_files)
print("Number of images in folder:", num_images)
pred_folder_path=os.path.join(folder_path, "result_spin_dia1")
pred_img_folder_path = os.path.join(pred_folder_path, "colors1")
pred_mask_folder_path = os.path.join(pred_folder_path, "masks1")
pred_normal_folder_path = os.path.join(pred_folder_path, "normals1")
print("Compute metrics:")
metrics = {}
metrics['mse'] = -1. * np.ones(num_images)
metrics['psnr'] = -1. * np.ones(num_images)
metrics['ssim'] = -1. * np.ones(num_images)
metrics['lpips'] = -1. * np.ones(num_images)
metrics['mae'] = -1. * np.ones(num_images)
metrics['iou'] = -1. * np.ones(num_images)
metrics['mask_mse'] = -1. * np.ones(num_images)
mse_total=0.
psnr_total = 0.
ssim_total = 0.
lpips_total = 0.
mae_total=0.
iou_total = 0.
mask_mse_total = 0.
lpips = LPIPS(net='vgg').to(device)
set_requires_grad(lpips, requires_grad=False)
W = 1284
H = 940
for i in range(num_images):
gt_img_path = osp.join(gt_img_folder_path, '%06d.png' % i)
pred_img_path = osp.join(pred_img_folder_path, '%d.png' % i)
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
pred_mask_path = osp.join(pred_mask_folder_path, '%d.png' % i)
gt_normal_path = osp.join(gt_normal_folder_path, '%06d.png' % i)
pred_normal_path = osp.join(pred_normal_folder_path, '%d.png' % i)
# 读取img 转换为浮点数 归一化到[0,1]
gt_img_origin = cv2.imread(gt_img_path) / 255.
pred_img_origin = cv2.imread(pred_img_path) / 255.
# 读取mask
gt_mask = cv2.imread(gt_mask_path)/ 255.
pred_mask = cv2.imread(pred_mask_path) / 255.
# 读取normal
gt_normal_origin = cv2.imread(gt_normal_path)[:, :, ::-1]
gt_normal_origin = 2. * gt_normal_origin.astype(np.float32) / 255. - 1.
pred_normal_origin = cv2.imread(pred_normal_path)[:, :, ::-1]
pred_normal_origin = 2. * pred_normal_origin.astype(np.float32) / 255. - 1.
# 只取mask内的(如果是概率mask的话,这样相乘,边缘的颜色会有问题)
# 所以一定要使用二值mask
#gt_img = gt_img_origin*gt_mask
gt_img = gt_img_origin
gt_img[gt_mask == 0] = [1.]
pred_img=pred_img_origin
gt_normal=gt_normal_origin*gt_mask
pred_normal = pred_normal_origin * gt_mask
## 计算metric
# color
mse_single = 10000*compare_mse(pred_img, gt_img)
psnr_single = compare_psnr(pred_img, gt_img)
ssim_single = compare_ssim(pred_img, gt_img,data_range=1.0,channel_axis=-1)
lpips_single = 1000*compute_lpips(lpips,torch.from_numpy(pred_img).view(H, W,3).float().to(device), torch.from_numpy(gt_img).view(H, W,3).float().to(device))
# normal #
mae_single = np.mean(np.abs(pred_normal - gt_normal))
# mask #
# pred_mask由pytorch的渲染器渲染mesh生成,因此mash不是0就是1,3个通道的值也是一样的
# gtmask由他人算法得到,范围在[0~1],大小为[1080,1920,3],但是3通道的数值都一样,因为可以直接取某一通道作为该像素点mask的值
# iou
iou_single = compute_iou(pred_mask[:, :, 0].reshape(H, W), gt_mask[:, :, 0].reshape(H, W))
# mse #
mask_mse_single = 10000 * compare_mse(pred_mask[:, :, 0].reshape(H, W), gt_mask[:, :, 0].reshape(H, W))
# 记录指标 #
metrics['mse'][i] = mse_single
metrics['psnr'][i] = psnr_single
metrics['ssim'][i] = ssim_single
metrics['lpips'][i] = lpips_single
metrics['mae'][i] = mae_single
metrics['iou'][i] = iou_single
metrics['mask_mse'][i] = mask_mse_single
print("image", i, " color_mse:", mse_single," color_psnr:", psnr_single, " color_ssim:", ssim_single, " color_lpips:", lpips_single,
" normal_mae:", mae_single," mask_iou:", iou_single," mask_mse:", mask_mse_single)
mse_total+=mse_single
psnr_total += psnr_single
ssim_total += ssim_single
lpips_total += lpips_single
mae_total += mae_single
iou_total += iou_single
mask_mse_total += mask_mse_single
torch.cuda.empty_cache()
# 分别写进一个txt文件 #
with open(osp.join(pred_folder_path, 'color_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好,找出最大mse,也就是找有问题的几帧
# 在默认情况下,argsort() 返回的是按升序排列的数组的索引,即最小值的索引排在最前面。
# 因此,如果我们要找到最大值的索引,我们可以将待排序数组取负数,然后进行排序,这样就会将最大值变成最小值,再取前几个元素,就可以得到最大值的索引。
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('color mse done')
with open(osp.join(pred_folder_path, 'color_psnr.txt'), 'w') as ff:
ff.write(' psnr\n')
psnr = metrics['psnr']
for ind, e in enumerate(psnr.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('psnr mean: %.6f, max: %.6f, min: %.6f, mininds:' % (psnr.mean(), psnr.max(), psnr.min())) # psnr 越高越好,找出最小psnr,也就是找有问题的几帧
for ind in (psnr).argsort()[:10]:
ff.write('%d ' % ind)
print('color psnr done')
with open(osp.join(pred_folder_path, 'color_ssim.txt'), 'w') as ff:
ff.write(' ssim\n')
ssim = metrics['ssim']
for ind, e in enumerate(ssim.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('ssim mean: %.6f, max: %.6f, min: %.6f, mininds:' % (ssim.mean(), ssim.max(), ssim.min())) # ssim 越高越好
for ind in (ssim).argsort()[:10]:
ff.write('%d ' % ind)
print('color ssim done')
with open(osp.join(pred_folder_path, 'color_lpips.txt'), 'w') as ff:
ff.write(' lpips\n')
lpips = metrics['lpips']
for ind, e in enumerate(lpips.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('lpips mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (lpips.mean(), lpips.max(), lpips.min())) # lpips 越小越好
for ind in (-lpips).argsort()[:10]:
ff.write('%d ' % ind)
print('color lpips done')
with open(osp.join(pred_folder_path, 'normal_mae.txt'), 'w') as ff:
ff.write(' mae\n')
mae = metrics['mae']
for ind, e in enumerate(mae.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mae mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mae.mean(), mae.max(), mae.min())) # mae 越小越好,找出最大mae,也就是找有问题的几帧
for ind in (-mae).argsort()[:10]:
ff.write('%d ' % ind)
print('normal mae done')
with open(osp.join(pred_folder_path, 'mask_iou.txt'), 'w') as ff:
ff.write(' iou\n')
iou = metrics['iou']
for ind, e in enumerate(iou.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('iou mean: %.6f, max: %.6f, min: %.6f, mininds:' % (iou.mean(), iou.max(), iou.min())) # iou 越高越好,找出最小iou,也就是找有问题的几帧
for ind in (iou).argsort()[:10]:
ff.write('%d ' % ind)
print('mask iou done')
with open(osp.join(pred_folder_path, 'mask_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('mask mse done')
# num_temp=end-start
mse_average=mse_total/num_images
psnr_average = psnr_total / num_images
ssim_average = ssim_total / num_images
lpips_average = lpips_total / num_images
mae_average = mae_total / num_images
iou_average = iou_total / num_images
mask_mse_average = mask_mse_total / num_images
print("mse_average:", mse_average,"psnr_average:", psnr_average, " ssim_average:", ssim_average, " lpips_average:", lpips_average,
" mae_average:", mae_average," iou_average:", iou_average," mask_mse_average:", mask_mse_average)
print("End!")
### 测试mask 指标 ###
# iou MAD MSE Grad Conn dtSSD
def compute_mask_metrics():
device = torch.device(0)
folder_path="/mnt/data2/lcj/dataset/deepcap/Antonia1/"
gt_img_folder_path =os.path.join(folder_path, "imgs")
gt_mask_folder_path = os.path.join(folder_path, "masks")
gt_image_files = glob.glob(os.path.join(gt_img_folder_path, "*.png"))
num_images = len(gt_image_files)
print("Number of images in folder:", num_images)
pred_folder_path=os.path.join(folder_path, "finetune_mlp_lbs_p_lpips_1.0")
pred_mask_folder_path = os.path.join(pred_folder_path, "masks1")
print("Compute metrics:")
metrics = {}
metrics['iou'] = -1. * np.ones(num_images)
metrics['mse'] = -1. * np.ones(num_images)
iou_total = 0.
mse_total = 0.
for i in range(num_images):
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
pred_mask_path = osp.join(pred_mask_folder_path, '%d.png' % i)
# 读取mask
gt_mask = cv2.imread(gt_mask_path)/ 255.
pred_mask = cv2.imread(pred_mask_path) / 255.
## 计算metric
# iou
iou_single=compute_iou(pred_mask[:,:,0].reshape(1080,1920),gt_mask[:,:,0].reshape(1080,1920))
# mse #
mse_single = 10000*compare_mse(pred_mask[:, :, 0].reshape(1080, 1920), gt_mask[:, :, 0].reshape(1080, 1920))
# 记录指标 #
metrics['iou'][i] = iou_single
metrics['mse'][i] = mse_single
print("image", i," mask_iou:", iou_single," mask_mse:", mse_single)
iou_total += iou_single
mse_total += mse_single
torch.cuda.empty_cache()
# 分别写进一个txt文件 #
with open(osp.join(pred_folder_path, 'test_mask_iou.txt'), 'w') as ff:
ff.write(' iou\n')
iou = metrics['iou']
for ind, e in enumerate(iou.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('iou mean: %.6f, max: %.6f, min: %.6f, mininds:' % (iou.mean(), iou.max(), iou.min())) # iou 越高越好,找出最小iou,也就是找有问题的几帧
for ind in (iou).argsort()[:10]:
ff.write('%d ' % ind)
print('mask iou done')
with open(osp.join(pred_folder_path, 'test_mask_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('mask mse done')
iou_average = iou_total / num_images
mse_average = mse_total / num_images
print(" iou_average:", iou_average," mse_average:", mse_average)
print("End!")
#compute_metrics()
# humannerf metric
def compute_metrics_humannerf():
device = torch.device(0)
folder_path="/mnt/data2/lcj/humannerf-main/dataset/wild/red/"
gt_img_folder_path =os.path.join(folder_path, "images")
gt_mask_folder_path = os.path.join(folder_path, "masks")
gt_image_files = glob.glob(os.path.join(gt_img_folder_path, "*.png"))
num_images = len(gt_image_files)
print("Number of images in folder:", num_images)
pred_folder_path="/mnt/data2/lcj/humannerf-main/experiments/human_nerf/wild/red/single_gpu/latest/"
pred_img_folder_path = os.path.join(pred_folder_path, "movement")
print("Compute metrics:")
metrics = {}
metrics['mse'] = -1. * np.ones(num_images)
metrics['psnr'] = -1. * np.ones(num_images)
metrics['ssim'] = -1. * np.ones(num_images)
metrics['lpips'] = -1. * np.ones(num_images)
mse_total=0.
psnr_total = 0.
ssim_total = 0.
lpips_total = 0.
lpips = LPIPS(net='vgg').to(device)
set_requires_grad(lpips, requires_grad=False)
W=1284
H=940
for i in range(num_images):
gt_img_path = osp.join(gt_img_folder_path, '%06d.png' % i)
pred_img_path = osp.join(pred_img_folder_path, '%06d.png' % i)
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
# 读取img 转换为浮点数 归一化到[0,1]
gt_img_origin = cv2.imread(gt_img_path) / 255.
pred_img_origin = cv2.imread(pred_img_path) / 255.
# 读取mask
gt_mask = cv2.imread(gt_mask_path)/ 255.
# 只取mask内的(如果是概率mask的话,这样相乘,边缘的颜色会有问题)
# 所以一定要使用二值mask
gt_img=gt_img_origin
gt_img[gt_mask==0] = [1.]
pred_img=pred_img_origin
## 计算metric
# color
mse_single = 10000*compare_mse(pred_img, gt_img)
psnr_single = compare_psnr(pred_img, gt_img)
ssim_single = compare_ssim(pred_img, gt_img,data_range=1.0,channel_axis=-1) # (1080,1920,3)
lpips_single = 1000*compute_lpips(lpips,torch.from_numpy(pred_img).view(H,W,3).float().to(device), torch.from_numpy(gt_img).view(H,W,3).float().to(device))
# 记录指标 #
metrics['mse'][i] = mse_single
metrics['psnr'][i] = psnr_single
metrics['ssim'][i] = ssim_single
metrics['lpips'][i] = lpips_single
print("image", i, " color_mse:", mse_single," color_psnr:", psnr_single, " color_ssim:", ssim_single, " color_lpips:", lpips_single)
mse_total+=mse_single
psnr_total += psnr_single
ssim_total += ssim_single
lpips_total += lpips_single
torch.cuda.empty_cache()
# 分别写进一个txt文件 #
with open(osp.join(pred_folder_path, 'color_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好,找出最大mse,也就是找有问题的几帧
# 在默认情况下,argsort() 返回的是按升序排列的数组的索引,即最小值的索引排在最前面。
# 因此,如果我们要找到最大值的索引,我们可以将待排序数组取负数,然后进行排序,这样就会将最大值变成最小值,再取前几个元素,就可以得到最大值的索引。
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('color mse done')
with open(osp.join(pred_folder_path, 'color_psnr.txt'), 'w') as ff:
ff.write(' psnr\n')
psnr = metrics['psnr']
for ind, e in enumerate(psnr.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('psnr mean: %.6f, max: %.6f, min: %.6f, mininds:' % (psnr.mean(), psnr.max(), psnr.min())) # psnr 越高越好,找出最小psnr,也就是找有问题的几帧
for ind in (psnr).argsort()[:10]:
ff.write('%d ' % ind)
print('color psnr done')
with open(osp.join(pred_folder_path, 'color_ssim.txt'), 'w') as ff:
ff.write(' ssim\n')
ssim = metrics['ssim']
for ind, e in enumerate(ssim.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('ssim mean: %.6f, max: %.6f, min: %.6f, mininds:' % (ssim.mean(), ssim.max(), ssim.min())) # ssim 越高越好
for ind in (ssim).argsort()[:10]:
ff.write('%d ' % ind)
print('color ssim done')
with open(osp.join(pred_folder_path, 'color_lpips.txt'), 'w') as ff:
ff.write(' lpips\n')
lpips = metrics['lpips']
for ind, e in enumerate(lpips.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('lpips mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (lpips.mean(), lpips.max(), lpips.min())) # lpips 越小越好
for ind in (-lpips).argsort()[:10]:
ff.write('%d ' % ind)
print('color lpips done')
mse_average=mse_total/num_images
psnr_average = psnr_total / num_images
ssim_average = ssim_total / num_images
lpips_average = lpips_total / num_images
print("mse_average:", mse_average,"psnr_average:", psnr_average, " ssim_average:", ssim_average, " lpips_average:", lpips_average)
print("End!")
### econ直接可视化比较吧,不比指标了
# 前512*512是gt 中间512*512是pred正面 最后512*512是pred背面
# 分割出gt和pred,normal和gtnormal比较;根据pred得到mask,和gtmask比较
def compute_metrics_econ():
device = torch.device(0)
folder_path = "/mnt/data2/lcj/dataset/deepcap/Antonia1/"
gt_mask_folder_path = os.path.join(folder_path, "masks")
gt_normal_folder_path = os.path.join(folder_path, "normals")
gt_mask_files = glob.glob(os.path.join(gt_mask_folder_path, "*.png"))
num_images = len(gt_mask_files)
print("Number of images in folder:", num_images)
pred_folder_path = os.path.join(folder_path, "econ_results")
pred_econ_folder_path = os.path.join(pred_folder_path, "econ")
pred_econ_img_folder_path = os.path.join(pred_econ_folder_path, "png")
culled_pred_econ_img_folder_path=os.path.join(pred_econ_folder_path, "culled_png")
os.makedirs(culled_pred_econ_img_folder_path,exist_ok=True)
for i in range(num_images):
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
# 读入原始图像
image = cv2.imread(osp.join(pred_econ_img_folder_path, '%06d_crop.png' % i))
# 获取原始图像的尺寸
height, width, _ = image.shape
# 计算要裁剪的中间部分的起始和结束位置
start_x = 512
end_x = start_x + 512
# 裁剪图像
cropped_image = image[:, start_x:end_x]
# 保存裁剪后的图像
cv2.imwrite(osp.join(culled_pred_econ_img_folder_path, '%06d.png' % i),cropped_image)
print("Compute metrics:")
metrics = {}
metrics['mse'] = -1. * np.ones(num_images)
metrics['psnr'] = -1. * np.ones(num_images)
metrics['ssim'] = -1. * np.ones(num_images)
metrics['lpips'] = -1. * np.ones(num_images)
metrics['mae'] = -1. * np.ones(num_images)
metrics['iou'] = -1. * np.ones(num_images)
metrics['mask_mse'] = -1. * np.ones(num_images)
mse_total = 0.
psnr_total = 0.
ssim_total = 0.
lpips_total = 0.
mae_total = 0.
iou_total = 0.
mask_mse_total = 0.
lpips = LPIPS(net='vgg').to(device)
set_requires_grad(lpips, requires_grad=False)
for i in range(num_images):
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
pred_mask_path = osp.join(pred_mask_folder_path, '%d.png' % i)
gt_normal_path = osp.join(gt_normal_folder_path, '%06d.png' % i)
pred_normal_path = osp.join(pred_normal_folder_path, '%d.png' % i)
# 读取mask
gt_mask = cv2.imread(gt_mask_path)/ 255.
pred_mask = cv2.imread(pred_mask_path) / 255.
# 读取normal
gt_normal_origin = cv2.imread(gt_normal_path)[:, :, ::-1]
gt_normal_origin = 2. * gt_normal_origin.astype(np.float32) / 255. - 1.
pred_normal_origin = cv2.imread(pred_normal_path)[:, :, ::-1]
pred_normal_origin = 2. * pred_normal_origin.astype(np.float32) / 255. - 1.
# 只取mask内的(如果是概率mask的话,这样相乘,边缘的颜色会有问题)
# 所以一定要使用二值mask
gt_img=gt_img_origin*pred_mask
pred_img=pred_img_origin*pred_mask
gt_normal=gt_normal_origin*pred_mask
pred_normal = pred_normal_origin * pred_mask
## 计算metric
# color
mse_single = 10000*compare_mse(pred_img, gt_img)
psnr_single = compare_psnr(pred_img, gt_img)
ssim_single = compare_ssim(pred_img, gt_img,multichannel=True)
lpips_single = 1000*compute_lpips(lpips,torch.from_numpy(pred_img).view(1080,1920,3).float().to(device), torch.from_numpy(gt_img).view(1080,1920,3).float().to(device))
# normal #
mae_single = np.mean(np.abs(pred_normal - gt_normal))
# mask #
# pred_mask由pytorch的渲染器渲染mesh生成,因此mash不是0就是1,3个通道的值也是一样的
# gtmask由他人算法得到,范围在[0~1],大小为[1080,1920,3],但是3通道的数值都一样,因为可以直接取某一通道作为该像素点mask的值
# iou
iou_single = compute_iou(pred_mask[:, :, 0].reshape(1080, 1920), gt_mask[:, :, 0].reshape(1080, 1920))
# mse #
mask_mse_single = 10000 * compare_mse(pred_mask[:, :, 0].reshape(1080, 1920), gt_mask[:, :, 0].reshape(1080, 1920))
# 记录指标 #
metrics['mae'][i] = mae_single
metrics['iou'][i] = iou_single
metrics['mask_mse'][i] = mask_mse_single
print("image", i, " normal_mae:", mae_single," mask_iou:", iou_single," mask_mse:", mask_mse_single)
mae_total += mae_single
iou_total += iou_single
mask_mse_total += mask_mse_single
torch.cuda.empty_cache()
with open(osp.join(pred_folder_path, 'normal_mae.txt'), 'w') as ff:
ff.write(' mae\n')
mae = metrics['mae']
for ind, e in enumerate(mae.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mae mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mae.mean(), mae.max(), mae.min())) # mae 越小越好,找出最大mae,也就是找有问题的几帧
for ind in (-mae).argsort()[:10]:
ff.write('%d ' % ind)
print('normal mae done')
with open(osp.join(pred_folder_path, 'mask_iou.txt'), 'w') as ff:
ff.write(' iou\n')
iou = metrics['iou']
for ind, e in enumerate(iou.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('iou mean: %.6f, max: %.6f, min: %.6f, mininds:' % (iou.mean(), iou.max(), iou.min())) # iou 越高越好,找出最小iou,也就是找有问题的几帧
for ind in (iou).argsort()[:10]:
ff.write('%d ' % ind)
print('mask iou done')
with open(osp.join(pred_folder_path, 'mask_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('mask mse done')
mae_average = mae_total / num_images
iou_average = iou_total / num_images
mask_mse_average = mask_mse_total / num_images
print(" mae_average:", mae_average," iou_average:", iou_average," mask_mse_average:", mask_mse_average)
print("End!")
# 前512*512是gt 中间512*512是pred 最后512*512是smpl+衣服
def compute_metrics_scarf():
folder_path = "/mnt/data/lcj/SCARF/exps/mine/male-3-casual/hybrid/visualization/"
pred_folder_path=os.path.join(folder_path, "capture")
pred_files = glob.glob(os.path.join(pred_folder_path, "*.jpg"))
num_images = len(pred_files)//2
print("Number of images in folder:", num_images)
device = torch.device(0)
print("Compute metrics:")
metrics = {}
metrics['mse'] = -1. * np.ones(num_images)
metrics['psnr'] = -1. * np.ones(num_images)
metrics['ssim'] = -1. * np.ones(num_images)
metrics['lpips'] = -1. * np.ones(num_images)
metrics['mae'] = -1. * np.ones(num_images)
metrics['iou'] = -1. * np.ones(num_images)
metrics['mask_mse'] = -1. * np.ones(num_images)
mse_total = 0.
psnr_total = 0.
ssim_total = 0.
lpips_total = 0.
mae_total = 0.
iou_total = 0.
mask_mse_total = 0.
lpips = LPIPS(net='vgg').to(device)
set_requires_grad(lpips, requires_grad=False)
# start=295
# end=345
for i in range(num_images):
# 读入
image = cv2.imread(osp.join(pred_folder_path, '_f%06d.jpg' % (2*i)))
image = 2. * image / 255. - 1.
# mask = cv2.imread(osp.join('/mnt/data/lcj/SCARF/exps/Antonia1/Antonia1/matting/', 'Antonia1_f000000.png'),
# cv2.IMREAD_UNCHANGED)
#
# mask = mask[:, :, 3]
# mask = mask / 255 > 0.5
# import torchvision
# torchvision.utils.save_image(torch.from_numpy(mask).float(), '/mnt/data/lcj/SCARF/a_' +str(i) +'.png')
# 计算要裁剪的中间部分的起始和结束位置
start_x = 512
end_x = start_x + 512
# 裁剪图像
gt_img = image[:, 0:start_x]
pred_img = image[:, start_x:end_x]
cv2.imwrite(osp.join("/mnt/data/lcj/SCARF/", 'gt_img.png'), (gt_img+1)*0.5 * 255)
cv2.imwrite(osp.join("/mnt/data/lcj/SCARF/", 'pred_img.png'), (pred_img+1)*0.5 * 255)
## 计算metric
# color
mse_single = 10000 * compare_mse(pred_img, gt_img)
psnr_single = compare_psnr(pred_img, gt_img)
ssim_single = compare_ssim(pred_img, gt_img,data_range=1.0,channel_axis=-1)
lpips_single = 1000*compute_lpips(lpips,torch.from_numpy(pred_img).view(512,512,3).float().to(device), torch.from_numpy(gt_img).view(512,512,3).float().to(device))
torch.cuda.empty_cache()
# 记录指标 #
metrics['mse'][i] = mse_single
metrics['psnr'][i] = psnr_single
metrics['ssim'][i] = ssim_single
metrics['lpips'][i] = lpips_single
print("image", i*2, " color_mse:", mse_single, " color_psnr:", psnr_single, " color_ssim:", ssim_single,
" color_lpips:", lpips_single)
mse_total += mse_single
psnr_total += psnr_single
ssim_total += ssim_single
lpips_total += lpips_single
torch.cuda.empty_cache()
# 分别写进一个txt文件 #
with open(osp.join(folder_path, 'color_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好,找出最大mse,也就是找有问题的几帧
# 在默认情况下,argsort() 返回的是按升序排列的数组的索引,即最小值的索引排在最前面。
# 因此,如果我们要找到最大值的索引,我们可以将待排序数组取负数,然后进行排序,这样就会将最大值变成最小值,再取前几个元素,就可以得到最大值的索引。
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('color mse done')
with open(osp.join(folder_path, 'color_psnr.txt'), 'w') as ff:
ff.write(' psnr\n')
psnr = metrics['psnr']
for ind, e in enumerate(psnr.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('psnr mean: %.6f, max: %.6f, min: %.6f, mininds:' % (psnr.mean(), psnr.max(), psnr.min())) # psnr 越高越好,找出最小psnr,也就是找有问题的几帧
for ind in (psnr).argsort()[:10]:
ff.write('%d ' % ind)
print('color psnr done')
with open(osp.join(folder_path, 'color_ssim.txt'), 'w') as ff:
ff.write(' ssim\n')
ssim = metrics['ssim']
for ind, e in enumerate(ssim.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('ssim mean: %.6f, max: %.6f, min: %.6f, mininds:' % (ssim.mean(), ssim.max(), ssim.min())) # ssim 越高越好
for ind in (ssim).argsort()[:10]:
ff.write('%d ' % ind)
print('color ssim done')
with open(osp.join(folder_path, 'color_lpips.txt'), 'w') as ff:
ff.write(' lpips\n')
lpips = metrics['lpips']
for ind, e in enumerate(lpips.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('lpips mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (lpips.mean(), lpips.max(), lpips.min())) # lpips 越小越好
for ind in (-lpips).argsort()[:10]:
ff.write('%d ' % ind)
print('color lpips done')
#num_temp=end-start
mse_average=mse_total/num_images
psnr_average = psnr_total / num_images
ssim_average = ssim_total / num_images
lpips_average = lpips_total / num_images
print("mse_average:", mse_average,"psnr_average:", psnr_average, " ssim_average:", ssim_average, " lpips_average:", lpips_average)
print("End!")
def compute_metrics_v2a():
device = torch.device(0)
folder_path="/mnt/data2/lcj/videoavatars-master/data/red/"
gt_img_folder_path =os.path.join(folder_path, "imgs")
gt_mask_folder_path = os.path.join(folder_path, "masks") # mask:v2a的mask不太行 masks:通过rvm获得的gtmask
gt_normal_folder_path = os.path.join(folder_path, "normals") # pifu的normal感觉甚至没有v2a的好
gt_image_files = glob.glob(os.path.join(gt_img_folder_path, "*.png"))
num_images = len(gt_image_files)
num_images=140
print("Number of images in folder:", num_images)
pred_folder_path="/mnt/data2/lcj/videoavatars-master/outputs/Video/red/"
pred_img_folder_path = os.path.join(pred_folder_path, "test_fg_rendering")
pred_mask_folder_path = os.path.join(pred_folder_path, "test_mask")
pred_normal_folder_path = os.path.join(pred_folder_path, "test_normal")
print("Compute metrics:")
metrics = {}
metrics['mse'] = -1. * np.ones(num_images)
metrics['psnr'] = -1. * np.ones(num_images)
metrics['ssim'] = -1. * np.ones(num_images)
metrics['lpips'] = -1. * np.ones(num_images)
metrics['mae'] = -1. * np.ones(num_images)
metrics['iou'] = -1. * np.ones(num_images)
metrics['mask_mse'] = -1. * np.ones(num_images)
mse_total=0.
psnr_total = 0.
ssim_total = 0.
lpips_total = 0.
mae_total=0.
iou_total = 0.
mask_mse_total = 0.
lpips = LPIPS(net='vgg').to(device)
set_requires_grad(lpips, requires_grad=False)
W=1284
H=940
for i in range(num_images):
gt_img_path = osp.join(gt_img_folder_path, '%06d.png' % i)
pred_img_path = osp.join(pred_img_folder_path, '%04d.png' % i)
gt_mask_path = osp.join(gt_mask_folder_path, '%06d.png' % i)
pred_mask_path = osp.join(pred_mask_folder_path, '%04d.png' % i)
gt_normal_path = osp.join(gt_normal_folder_path, '%06d.png' % i)
pred_normal_path = osp.join(pred_normal_folder_path, '%04d.png' % i)
# 读取img 转换为浮点数 归一化到[0,1]
gt_img_origin = cv2.imread(gt_img_path) / 255.
pred_img_origin = cv2.imread(pred_img_path) / 255.
# 读取mask
gt_mask = cv2.imread(gt_mask_path)/ 255.
pred_mask = cv2.imread(pred_mask_path) / 255.
# 读取normal
gt_normal_origin = cv2.imread(gt_normal_path)[:, :, ::-1]
gt_normal_origin = 2. * gt_normal_origin.astype(np.float32) / 255. - 1.
pred_normal_origin = cv2.imread(pred_normal_path)[:, :, ::-1]
pred_normal_origin = 2. * pred_normal_origin.astype(np.float32) / 255. - 1.
# 只取mask内的(如果是概率mask的话,这样相乘,边缘的颜色会有问题)
# 所以一定要使用二值mask
gt_img=gt_img_origin*gt_mask # 和pred_mask相乘还是gt_mask相乘 v2a方法和gt_mask相乘
pred_img=pred_img_origin*pred_mask
gt_normal=gt_normal_origin*gt_mask
pred_normal = pred_normal_origin * pred_mask
## 计算metric
# color
mse_single = 10000*compare_mse(pred_img, gt_img)
psnr_single = compare_psnr(pred_img, gt_img)
ssim_single = compare_ssim(pred_img, gt_img,data_range=1.0,channel_axis=-1)
lpips_single = 1000*compute_lpips(lpips,torch.from_numpy(pred_img).view(H,W,3).float().to(device), torch.from_numpy(gt_img).view(H,W,3).float().to(device))
# normal #
mae_single = np.mean(np.abs(pred_normal - gt_normal))
# mask #
# pred_mask由pytorch的渲染器渲染mesh生成,因此mash不是0就是1,3个通道的值也是一样的
# gtmask由他人算法得到,范围在[0~1],大小为[1080,1920,3],但是3通道的数值都一样,因为可以直接取某一通道作为该像素点mask的值
# iou
iou_single = compute_iou(pred_mask[:, :, 0].reshape(H, W), gt_mask[:, :, 0].reshape(H, W))
# mse #
mask_mse_single = 10000 * compare_mse(pred_mask[:, :, 0].reshape(H, W), gt_mask[:, :, 0].reshape(H, W))
# 记录指标 #
metrics['mse'][i] = mse_single
metrics['psnr'][i] = psnr_single
metrics['ssim'][i] = ssim_single
metrics['lpips'][i] = lpips_single
metrics['mae'][i] = mae_single
metrics['iou'][i] = iou_single
metrics['mask_mse'][i] = mask_mse_single
print("image", i, " color_mse:", mse_single," color_psnr:", psnr_single, " color_ssim:", ssim_single, " color_lpips:", lpips_single,
" normal_mae:", mae_single," mask_iou:", iou_single," mask_mse:", mask_mse_single)
mse_total+=mse_single
psnr_total += psnr_single
ssim_total += ssim_single
lpips_total += lpips_single
mae_total += mae_single
iou_total += iou_single
mask_mse_total += mask_mse_single
torch.cuda.empty_cache()
# 分别写进一个txt文件 #
with open(osp.join(pred_folder_path, 'color_mse.txt'), 'w') as ff:
ff.write(' mse\n')
mse = metrics['mse']
for ind, e in enumerate(mse.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mse mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mse.mean(), mse.max(), mse.min())) # mse 越小越好,找出最大mse,也就是找有问题的几帧
# 在默认情况下,argsort() 返回的是按升序排列的数组的索引,即最小值的索引排在最前面。
# 因此,如果我们要找到最大值的索引,我们可以将待排序数组取负数,然后进行排序,这样就会将最大值变成最小值,再取前几个元素,就可以得到最大值的索引。
for ind in (-mse).argsort()[:10]:
ff.write('%d ' % ind)
print('color mse done')
with open(osp.join(pred_folder_path, 'color_psnr.txt'), 'w') as ff:
ff.write(' psnr\n')
psnr = metrics['psnr']
for ind, e in enumerate(psnr.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('psnr mean: %.6f, max: %.6f, min: %.6f, mininds:' % (psnr.mean(), psnr.max(), psnr.min())) # psnr 越高越好,找出最小psnr,也就是找有问题的几帧
for ind in (psnr).argsort()[:10]:
ff.write('%d ' % ind)
print('color psnr done')
with open(osp.join(pred_folder_path, 'color_ssim.txt'), 'w') as ff:
ff.write(' ssim\n')
ssim = metrics['ssim']
for ind, e in enumerate(ssim.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('ssim mean: %.6f, max: %.6f, min: %.6f, mininds:' % (ssim.mean(), ssim.max(), ssim.min())) # ssim 越高越好
for ind in (ssim).argsort()[:10]:
ff.write('%d ' % ind)
print('color ssim done')
with open(osp.join(pred_folder_path, 'color_lpips.txt'), 'w') as ff:
ff.write(' lpips\n')
lpips = metrics['lpips']
for ind, e in enumerate(lpips.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('lpips mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (lpips.mean(), lpips.max(), lpips.min())) # lpips 越小越好
for ind in (-lpips).argsort()[:10]:
ff.write('%d ' % ind)
print('color lpips done')
with open(osp.join(pred_folder_path, 'normal_mae.txt'), 'w') as ff:
ff.write(' mae\n')
mae = metrics['mae']
for ind, e in enumerate(mae.tolist()):
if e >= 0.:
ff.write('%6d: %.9f\n' % (ind, e))
ff.write('mae mean: %.6f, max: %.6f, min: %.6f, maxinds:' % (mae.mean(), mae.max(), mae.min())) # mae 越小越好,找出最大mae,也就是找有问题的几帧
for ind in (-mae).argsort()[:10]:
ff.write('%d ' % ind)
print('normal mae done')
with open(osp.join(pred_folder_path, 'mask_iou.txt'), 'w') as ff:
ff.write(' iou\n')
iou = metrics['iou']
for ind, e in enumerate(iou.tolist()):