-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.py
1345 lines (1229 loc) · 55.7 KB
/
utils.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 os
import random
import numpy as np
import csv
import cv2
import pdb
from collections import defaultdict
import sys
from tqdm import tqdm
from PIL import Image
import torch.nn as nn
import torchvision.transforms as T
import torch
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.cm as cm
import math
import imageio
from skimage import io, img_as_float32
from skimage.color import gray2rgb
from sklearn.model_selection import train_test_split
from imageio import mimread,imsave
import pandas as pd
from evaluation.extract import cmp_akd, cmp_aed,cmp_aed_corss,extract_face_id,extract_arcface_id,cmp_CSIM_corss
from glob import glob
import random
def count_test_video(path):
vis = {video[:video.find('#',8)] for video in
os.listdir(path)}
print(vis)
print(len(vis))
def create_same_id_test_set(path):
vis = os.listdir(path)
videos = np.random.choice(vis, replace=False, size=100)
f = open('./data/vox_evaluation.csv','w',encoding='utf-8')
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in range(2083):
v = np.random.choice(videos, replace=False, size=1)
imgs = os.listdir(os.path.join(path,v[0]))
pair = np.random.choice(imgs, replace=False, size=2)
src = os.path.join(path,v[0],pair[0])
dst = os.path.join(path,v[0],pair[1])
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)
f.close()
def modify_same_id_voxceleb():
f = open('./data/vox_evaluation_v2.csv','w',encoding='utf-8')
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","best_frame"])
pairs = pd.read_csv('data/vox_evaluation.csv')
source = pairs['source'].tolist()
driving = pairs['driving'].tolist()
best_frame = pairs['driving'].tolist()
source = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
anchor = np.array(best_frame).reshape(-1,1)
content = np.concatenate((source,driving,anchor),1)
csv_writer.writerows(content)
def create_cross_id_test_set(path):
vis = os.listdir(path)
ids2video = defaultdict(list)
num = len('id10283')
for vi in vis:
ids2video[vi[:num]].append(vi)
ids = list(ids2video.keys())
videos = np.random.choice(vis, replace=False, size=100)
f = open('./data/vox_cross_id_evaluation.csv','w',encoding='utf-8')
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in range(2083):
id = np.random.choice(ids, replace=False, size=1)
vis = np.random.choice(ids2video[id[0]], replace=False, size=1)
imgs = os.listdir(os.path.join(path,vis[0]))
img = np.random.choice(imgs, replace=False, size=1)
src = os.path.join(path,vis[0],img[0])
other_id = list(set(ids).difference(set(id)))
id = np.random.choice(other_id, replace=False, size=1)
vis = np.random.choice(ids2video[id[0]], replace=False, size=1)
imgs = os.listdir(os.path.join(path,vis[0]))
img = np.random.choice(imgs, replace=False, size=1)
dst = os.path.join(path,vis[0],img[0])
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)
f.close()
def concate_compared_results(resust_path,cp_path):
imgs = os.listdir(resust_path)
for im in tqdm(imgs):
ours = cv2.imread(os.path.join(resust_path,im))
fomm = cv2.imread(os.path.join(cp_path,im))
rst = np.concatenate((ours,fomm),1).astype(np.uint8)
cv2.imwrite(os.path.join('compare',im),rst)
def render(path):
depth_encoder = depth.ResnetEncoder(18, False).cuda()
depth_decoder = depth.DepthDecoder(num_ch_enc=depth_encoder.num_ch_enc, scales=range(4)).cuda()
loaded_dict_enc = torch.load('depth/models/weights_19/encoder.pth')
loaded_dict_dec = torch.load('depth/models/weights_19/depth.pth')
filtered_dict_enc = {k: v for k, v in loaded_dict_enc.items() if k in depth_encoder.state_dict()}
depth_encoder.load_state_dict(filtered_dict_enc)
depth_decoder.load_state_dict(loaded_dict_dec)
depth_encoder.eval()
depth_decoder.eval()
cvimg = cv2.resize(cv2.imread(path),(256,256))
img = Image.open(path).convert('RGB').resize((256,256))
tensor_img = T.ToTensor()(img).unsqueeze(0).cuda()
outputs = depth_decoder(depth_encoder(tensor_img))
depth_source = outputs[("disp", 0)][0]
depth_source = depth_source.permute(1,2,0).detach().cpu().numpy()
heatmap = depth_source/np.max(depth_source)
heatmap = np.uint8(255 * heatmap)
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img1 = heatmap*0.6+cvimg
cv2.imwrite('{}.jpg'.format(path),superimposed_img1)
def depth_gray(path):
depth_encoder = depth.ResnetEncoder(18, False).cuda()
depth_decoder = depth.DepthDecoder(num_ch_enc=depth_encoder.num_ch_enc, scales=range(4)).cuda()
loaded_dict_enc = torch.load('depth/models/weights_19/encoder.pth')
loaded_dict_dec = torch.load('depth/models/weights_19/depth.pth')
filtered_dict_enc = {k: v for k, v in loaded_dict_enc.items() if k in depth_encoder.state_dict()}
depth_encoder.load_state_dict(filtered_dict_enc)
depth_decoder.load_state_dict(loaded_dict_dec)
depth_encoder.eval()
depth_decoder.eval()
img = Image.open(path).convert('RGB').resize((256,256))
tensor_img = T.ToTensor()(img).unsqueeze(0).cuda()
outputs = depth_decoder(depth_encoder(tensor_img))
depth_source = outputs[("disp", 0)][0]
depth_source = depth_source.permute(1,2,0).detach().cpu().numpy()*depth_source.permute(1,2,0).detach().cpu().numpy()
heatmap = 1-depth_source/np.max(depth_source)
heatmap = np.uint8(255 * heatmap)
cv2.imwrite('heatmap.jpg',heatmap)
def depth_rgb(path):
depth_encoder = depth.ResnetEncoder(18, False).cuda()
depth_decoder = depth.DepthDecoder(num_ch_enc=depth_encoder.num_ch_enc, scales=range(4)).cuda()
loaded_dict_enc = torch.load('depth/models/weights_19/encoder.pth')
loaded_dict_dec = torch.load('depth/models/weights_19/depth.pth')
filtered_dict_enc = {k: v for k, v in loaded_dict_enc.items() if k in depth_encoder.state_dict()}
depth_encoder.load_state_dict(filtered_dict_enc)
depth_decoder.load_state_dict(loaded_dict_dec)
depth_encoder.eval()
depth_decoder.eval()
img = Image.open(path).convert('RGB').resize((256,256))
tensor_img = T.ToTensor()(img).unsqueeze(0).cuda()
outputs = depth_decoder(depth_encoder(tensor_img))
disp = outputs[("disp", 0)]
# Saving colormapped depth image
disp_resized = torch.nn.functional.interpolate(disp, (256, 256), mode="bilinear", align_corners=False)
disp_resized_np = disp_resized.squeeze().detach().cpu().numpy()
vmax = np.percentile(disp_resized_np, 95)
normalizer = mpl.colors.Normalize(vmin=disp_resized_np.min(), vmax=vmax)
mapper = cm.ScalarMappable(norm=normalizer, cmap='rainbow')
colormapped_im = (mapper.to_rgba(disp_resized_np)[:, :, :3] * 255).astype(np.uint8)
plt.axis('off')
plt.imshow(colormapped_im)
# plt.colorbar(mapper)
plt.savefig(path+'.pdf')
# plt.savefig(path+'.png')
plt.clf()
def process_celeV(path):
train_path = os.path.join(path,'train')
test_path = os.path.join(path,'test')
ids = os.listdir(path)
f = open('./data/celeV_cross_id_evaluation.csv','w',encoding='utf-8')
# sample 2000 image sets from each identity
# if not os.path.exists(train_path):
# os.makedirs(train_path)
# if not os.path.exists(test_path):
# os.makedirs(test_path)
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in range(2083):
src_id = np.random.choice(ids, replace=False, size=1)
imgs = os.listdir(os.path.join(path,src_id[0],'Image'))
src_imgs = np.random.choice(imgs, replace=False, size=1)
src = os.path.join(path,src_id[0],'Image',src_imgs[0])
res_ids = list(set(ids).difference(set(src_id)))
dst_id = np.random.choice(res_ids, replace=False, size=1)
imgs = os.listdir(os.path.join(path,dst_id[0],'Image'))
dst_imgs = np.random.choice(imgs, replace=False, size=1)
dst = os.path.join(path,dst_id[0],'Image',dst_imgs[0])
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)
f.close()
def compare():
x2face = '/data/fhongac/workspace/gitrepo/X2Face/UnwrapMosaic/FID/celebv'
fomm = '/data/fhongac/workspace/gitrepo/first-order-model/FID/celebv'
osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/FID/celebv'
dagan = '/data/fhongac/workspace/src/parallel-fom-rgbd/log/vox-adv-256rgbd_kp_num15_rgbd_attnv2/celebv/concate'
imgs = os.listdir(x2face)
for i in tqdm(range(len(imgs))):
im = imgs[i]
img_x2face = os.path.join(x2face,im)
img_x2face = cv2.imread(img_x2face)
img_fomm = os.path.join(fomm,im)
img_fomm = cv2.imread(img_fomm)
img_osfv = os.path.join(osfv,im)
img_osfv = cv2.imread(img_osfv)
img_dagan = os.path.join(dagan,im)
img_dagan = cv2.imread(img_dagan)
img = np.vstack((img_x2face, img_fomm,img_osfv,img_dagan))
cv2.imwrite('FID/multiMethod/{}.jpg'.format(i),img)
def aus(path):
import cv2
frame = cv2.imread(path)
from feat import Detector
detector = Detector()
# image_prediction = detector.detect_image(path)
out1 = detector.detect_image('FID/source/0.jpg')
out1.plot_aus(12, muscles={'all': "heatmap"}, gaze = None)
plt.savefig('a.jpg')
out2 = detector.detect_image('FID/source/1.jpg')
p1 = out1.facepose().values
p2 = out2.facepose().values
# landmarks = detector.detect_landmarks(frame, face)
# score = detector.detect_aus(frame,landmarks[0])
def evaluate_CSIM_PRMSE_AUCON(source_fold,gt_fold,generate_fold):
from feat import Detector
# from feat.utils import read_pictures
detector = Detector()
# detector = Detector(
# face_model="retinaface",
# landmark_model="mobilefacenet",
# au_model='svm',
# emotion_model="resmasknet",
# facepose_model="img2pose",
# )
# x2face = '/data/fhongac/workspace/gitrepo/X2Face/UnwrapMosaic/FID'
# fomm = '/data/fhongac/workspace/gitrepo/first-order-model/FID'
# osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/FID'
# dagan = '/data/fhongac/workspace/src/parallel-fom-rgbd/FID'
# test = dagan
# path = sys.argv[1]
imgs = os.listdir(generate_fold)
PRMSE = 0
AUCON = 0
counter = 1e-9
CSIM = 0
csim_counter = 1e-9
##########################################CSIM##############################################################
# from facenet_pytorch import MTCNN, InceptionResnetV1
# # If required, create a face detection pipeline using MTCNN:
# mtcnn = MTCNN(image_size=256, margin=0).cuda()
# # Create an inception resnet (in eval mode):
# resnet = InceptionResnetV1(pretrained='vggface2').eval().cuda()
#####################################################################################################################
for im in tqdm(imgs):
gt = os.path.join(gt_fold,im)
gen = os.path.join(generate_fold,im)
try:
out_gt = detector.detect_image(gt)
out_generat = detector.detect_image(gen)
gt_aus = out_gt.aus.values
generate_aus = out_generat.aus.values
gt_pose = out_gt.facepose.values
generate_pose = out_generat.facepose.values
row,num = generate_aus.shape
prmse=np.sqrt(np.power(gt_pose-generate_pose,2).sum()/3)
if math.isnan(prmse):
print(im)
raise RuntimeError('NaN')
PRMSE+=prmse
generate_aus = generate_aus>0.5
gt_aus = gt_aus>0.5
rst = ~ (generate_aus^gt_aus)
correct = rst.sum()
AUCON+=(correct/num)
counter+=1
except Exception as e:
print(e)
# try:
# source = Image.open(os.path.join(source_fold,im))
# generate = Image.open(os.path.join(generate_fold,im))
# # Get cropped and prewhitened image tensor
# img_cropped = mtcnn(source,save_path='src.jpg')
# # img_cropped = T.ToTensor()(source).cuda()
# # Calculate embedding (unsqueeze to add batch dimension)
# source_emb = resnet(img_cropped.unsqueeze(0))
# # Get cropped and prewhitened image tensor
# img_cropped = mtcnn(generate,save_path='dst.jpg')
# # img_cropped = T.ToTensor()(generate).cuda()
# # Calculate embedding (unsqueeze to add batch dimension)
# generate_emb = resnet(img_cropped.unsqueeze(0))
# CSIM+=torch.cosine_similarity(source_emb,generate_emb).item()
# csim_counter+=1
# except Exception as e:
# print(e)
print(' PRMSE: {}, AUCON : {}, CSIM: {}'.format(PRMSE/counter, AUCON/counter,CSIM/csim_counter))
def mergeimgs(paths, save_name):
pth = paths[0]
imgps = os.listdir(pth)
if not os.path.exists('Compare/{}'.format(save_name)):
os.makedirs('Compare/{}'.format(save_name))
for i in tqdm(range(len(imgps))):
imgp = imgps[i]
cats = []
for idx, pth in enumerate(paths):
img = os.path.join(pth,imgp)
img = cv2.imread(img)
# if idx!=0:
# img = cv2.resize(img,(256,256))
cats.append(img)
img = np.hstack(cats)
cv2.imwrite('Compare/{}/{}.jpg'.format(save_name,i),img)
def create_animate_pair():
f = open('./data/vox_cross_id_animate.csv','w',encoding='utf-8')
csv_writer = csv.writer(f)
csv_writer.writerow(["source_frame","driving_video"])
pairs = pd.read_csv('data/vox_cross_id_evaluation.csv')
source = pairs['source'].tolist()
driving = pairs['driving'].tolist()
source_frames = []
driving_videos = []
for src, dst in zip(source,driving):
video = os.path.dirname(dst).replace('vox1_frames','vox1')
source_frames.append(src)
driving_videos.append(video)
source_frames = np.array(source_frames).reshape(-1,1)
driving_videos = np.array(driving_videos).reshape(-1,1)
content = np.concatenate((source_frames,driving_videos),1)
csv_writer.writerows(content)
f.close()
def merge_abla_imgs(paths):
pth = paths[0]
imgps = os.listdir(pth)
for i in tqdm(range(len(imgps))):
imgp = imgps[i]
cats = []
for pth in paths:
img = os.path.join(pth,imgp)
img = cv2.imread(img)
cats.append(img)
img = np.vstack(cats)
cv2.imwrite('FID/abla/{}.jpg'.format(i),img)
def mergevideos():
videos_path1 = 'animation'
videos_path2 = '/data/fhongac/workspace/gitrepo/first-order-model/animation'
videos = os.listdir(videos_path1)
save_path = 'merge_animation'
for vi in tqdm(videos):
fomm = np.array(mimread('{}/{}'.format(videos_path2,vi),memtest=False))
ours = np.array(mimread('{}/{}'.format(videos_path1,vi),memtest=False))
reader = imageio.get_reader('{}/{}'.format(videos_path2,vi))
fps = reader.get_meta_data()['fps']
if len(fomm.shape) == 3:
fomm = np.array([gray2rgb(frame) for frame in fomm])
if fomm.shape[-1] == 4:
fomm = fomm[..., :3]
if len(ours.shape) == 3:
ours = np.array([gray2rgb(frame) for frame in ours])
if ours.shape[-1] == 4:
ours = ours[..., :3]
fomm = fomm[:,:,-256:,:]
src_dst = ours[:,:,:512,:]
ours = ours[:,:,-256:,:]
merge = np.concatenate((src_dst,fomm,ours),2)
imageio.mimsave('{}/{}'.format(save_path,vi), merge, fps=fps)
def extractFrames():
videos_pairs = pd.read_csv('data/vox_cross_id_animate.csv')
source = videos_pairs['source_frame'].tolist()
driving = videos_pairs['driving_video'].tolist()
frame_pairs = pd.read_csv('data/vox_cross_id_evaluation.csv')
# source = videos_pairs['source_frame'].tolist()
driving_frame = frame_pairs['driving'].tolist()
concate = 'FID/video_cross_id'
generate = 'FID/video_generate'
videos = 'animation'
for i, (src, dst,number) in tqdm(enumerate(zip(source,driving,driving_frame))):
video = np.array(mimread('{}/{}.mp4'.format(videos,i),memtest=False))
if len(video.shape) == 3:
video = np.array([gray2rgb(frame) for frame in video])
if video.shape[-1] == 4:
video = video[..., :3]
num = int(os.path.basename(number)[:7])
video_array = img_as_float32(video)
frame = (video_array[num]*255).astype(np.uint8)
imsave('{}/{}.jpg'.format(concate,i),frame)
imsave('{}/{}.jpg'.format(generate,i),frame[:,-256:,:])
class depth_network(nn.Module):
def __init__(self):
super(depth_network, self).__init__()
self.depth_encoder = depth.ResnetEncoder(18, False).cuda()
self.depth_decoder = depth.DepthDecoder(num_ch_enc=self.depth_encoder.num_ch_enc, scales=range(4)).cuda()
def forward(self,x):
outputs = self.depth_decoder(self.depth_encoder(x))
return outputs
def viewNetworkStructure():
network = depth_network().cuda()
print(network)
import hiddenlayer as h
vis_graph = h.build_graph(network, torch.zeros([1,3,256,256]).cuda()) # 获取绘制图像的对象
vis_graph.theme = h.graph.THEMES["blue"].copy() # 指定主题颜色
vis_graph.save("network_graph/depth_network.png") # 保存图像的路径
def drawKPline():
kp10 = [2.292730636,0.870793269,0.719648837]
kp15 = [2.335680558,0.872849592,0.7229482939818654]
kp20 = [2.268743373,0.882716346, 0.67557838]
kp25 = [3.395401378,0.827983638,0.662669217]
data = np.array([kp10,kp15,kp20,kp25])
x=[0,1,2,3]
fig, ax = plt.subplots()
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
l1=plt.plot(x,data[:,0],'r--',label='PRMSE')
l2=plt.plot(x,data[:,1],'g--',label='AUCON')
l3=plt.plot(x,data[:,2],'b--',label='CSIM')
plt.plot(x,data[:,0],'ro-',x,data[:,1],'g+-',x,data[:,2],'b^-')
plt.grid(linestyle=':')
# ax.tick_params(bottom=False)
plt.xticks(x,["kp=10","kp=15","kp=20","kp=25"]) #去掉横坐标值
# plt.yticks([]) #去掉纵坐标值
# plt.setp(ax.get_xticklabels(), visible=False)
# plt.setp(ax.get_yticklabels(), visible=False)
plt.legend()
plt.savefig('network_graph/kp.pdf')
def all_depth(path):
imgs = os.listdir(path+'/gt')
depth_encoder = depth.ResnetEncoder(18, False).cuda()
depth_decoder = depth.DepthDecoder(num_ch_enc=depth_encoder.num_ch_enc, scales=range(4)).cuda()
loaded_dict_enc = torch.load('depth/models/weights_19/encoder.pth')
loaded_dict_dec = torch.load('depth/models/weights_19/depth.pth')
filtered_dict_enc = {k: v for k, v in loaded_dict_enc.items() if k in depth_encoder.state_dict()}
depth_encoder.load_state_dict(filtered_dict_enc)
depth_decoder.load_state_dict(loaded_dict_dec)
depth_encoder.eval()
depth_decoder.eval()
for im in tqdm(imgs):
driving = os.path.join(path,'gt',im)
source = os.path.join(path,'generate',im)
img = Image.open(path).convert('RGB').resize((256,256))
tensor_img = T.ToTensor()(img).unsqueeze(0).cuda()
outputs = depth_decoder(depth_encoder(tensor_img))
disp = outputs[("disp", 0)]
# Saving colormapped depth image
disp_resized = torch.nn.functional.interpolate(disp, (256, 256), mode="bilinear", align_corners=False)
disp_resized_np = disp_resized.squeeze().detach().cpu().numpy()
vmax = np.percentile(disp_resized_np, 95)
normalizer = mpl.colors.Normalize(vmin=disp_resized_np.min(), vmax=vmax)
mapper = cm.ScalarMappable(norm=normalizer, cmap='rainbow')
colormapped_im = (mapper.to_rgba(disp_resized_np)[:, :, :3] * 255).astype(np.uint8)
plt.axis('off')
plt.imshow(colormapped_im)
# plt.colorbar(mapper)
plt.savefig(path+'.pdf')
# plt.savefig(path+'.png')
plt.clf()
def changevideos():
# videos_path1 = 'animation'
# videos_path2 = '/data/fhongac/workspace/gitrepo/first-order-model/animation'
# videos = os.listdir(videos_path1)
# save_path = 'merge_animation'
# for vi in tqdm(videos):
# fomm = np.array(mimread('{}/{}'.format(videos_path2,vi),memtest=False))
# ours = np.array(mimread('{}/{}'.format(videos_path1,vi),memtest=False))
# reader = imageio.get_reader('{}/{}'.format(videos_path2,vi))
# fps = reader.get_meta_data()['fps']
# if len(fomm.shape) == 3:
# fomm = np.array([gray2rgb(frame) for frame in fomm])
# if fomm.shape[-1] == 4:
# fomm = fomm[..., :3]
# if len(ours.shape) == 3:
# ours = np.array([gray2rgb(frame) for frame in ours])
# if ours.shape[-1] == 4:
# ours = ours[..., :3]
# fomm = fomm[:,:,-256:,:]
# src_dst = ours[:,:,:512,:]
# ours = ours[:,:,-256:,:]
# merge = np.concatenate((src_dst,fomm,ours),2)
# imageio.mimsave('{}/{}'.format(save_path,vi), merge, fps=fps)
# 155
# path = 'merge_animation/155.mp4'
# disp = '/data/fhongac/workspace/src/depthEstimate/7PbDDjXgYzU/id10287#bP0bKbQQlzc#003638#003940_disp.mp4'
# osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/155.mp4'
# save = 'FID/animation/155.mp4'
# path = 'merge_animation/523.mp4'
# disp = '/data/fhongac/workspace/src/depthEstimate/7PbDDjXgYzU/id10287#4oOmqI1myzY#000381#000729_disp.mp4'
# osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/523.mp4'
# save = 'FID/animation/523.mp4'
# path = 'merge_animation/705.mp4'
# disp = '/data/fhongac/workspace/src/depthEstimate/705.mp4'
# osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/705.mp4'
# save = 'FID/animation/705.mp4'
# path = 'merge_animation/2062.mp4'
# disp = '/data/fhongac/workspace/src/depthEstimate/2062.mp4'
# osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/2062.mp4'
# save = 'FID/animation/2062.mp4'
# path = 'merge_animation/1841.mp4'
# disp = '/data/fhongac/workspace/src/depthEstimate/1841.mp4'
# osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/1841.mp4'
# save = 'FID/animation/1841.mp4'
path = 'merge_animation/1758.mp4'
disp = '/data/fhongac/workspace/src/depthEstimate/1758.mp4'
osfv = '/data/fhongac/workspace/gitrepo/One-Shot_Free-View_Neural_Talking_Head_Synthesis/1758.mp4'
save = 'FID/animation/1758.mp4'
video = np.array(mimread('{}'.format(path),memtest=False))
reader = imageio.get_reader('{}'.format(path))
fps = reader.get_meta_data()['fps']
video = np.array([gray2rgb(frame) for frame in video])
disp = np.array(mimread('{}'.format(disp),memtest=False))
disp = np.array([gray2rgb(frame) for frame in disp])
osfv = np.array(mimread('{}'.format(osfv),memtest=False))
osfv = np.array([gray2rgb(frame) for frame in osfv])
bz,h,w,c = video.shape
up_video = np.concatenate((video[:,:,:int(w/2),:],disp),2)
down_video = np.concatenate((video[:,:,int(w/2):int(w/4)*3,:],osfv,video[:,:,int(w/4)*3:,:]),2)
up_zeros = np.ones((bz,20,256*3,3))*255
mid_zeros = np.ones((bz,40,256*3,3))*255
down_zeros = np.ones((bz,40,256*3,3))*255
video = np.concatenate((up_zeros,up_video, mid_zeros, down_video,down_zeros),1)
imageio.mimsave('{}'.format(save), video, fps=fps)
print('aa')
def mergevideo(paths,save_name):
pth = paths[0]
vps = os.listdir(pth)
if not os.path.exists('Compare/{}'.format(save_name)):
os.makedirs('Compare/{}'.format(save_name))
for i in tqdm(range(len(vps))):
imgp = vps[i]
cats = []
fps = None
for pth in paths:
vp = os.path.join(pth,imgp)
video = np.array(mimread('{}'.format(vp),memtest=False))
if not fps:
reader = imageio.get_reader('{}'.format(vp))
fps = reader.get_meta_data()['fps']
video = np.array([gray2rgb(frame) for frame in video])
cats.append(video)
cats = np.concatenate(cats,1)
imageio.mimsave('Compare/{}/{}.mp4'.format(save_name,i), cats, fps=fps*2)
# cv2.imwrite('Compare/{}/{}.jpg'.format(save_name,i),img)
def vec_sten():
from sklearn.manifold import TSNE
import pandas as pd
import seaborn as sns
# We want to get TSNE embedding with 2 dimensions
generated = 'log/Unet_Baseline/vox_cross_id/generate'
# generated = 'log/ExpendMemoryUnitV54_kp15_Unet_Generator_keypoint_aware/vox_cross_id/generate'
df = extract_arcface_id(False,generated, (256,256), 0)
id_maps, ids = id_collect()
feats = np.array(df['value'].values)
# pdb.set_trace()
X = np.stack(feats,0)
n_components = 2
tsne = TSNE(n_components,init='pca', random_state=501)
tsne_result = tsne.fit_transform(X)
tsne_result_df = pd.DataFrame({'tsne_1': tsne_result[:,0], 'tsne_2': tsne_result[:,1], 'label': ids})
fig, ax = plt.subplots(1)
sns.scatterplot(x='tsne_1', y='tsne_2', hue='label', data=tsne_result_df, ax=ax,s=20)
lim = (tsne_result.min()-5, tsne_result.max()+5)
ax.set_xlim(lim)
ax.set_ylim(lim)
ax.set_aspect('equal')
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)
plt.savefig('baseline_cross.jpg')
def id_collect():
idx = len('/data/fhongac/origDataset/vox1_frames/test/')
# pairs = pd.read_csv('data/vox_evaluation_v2.csv')
pairs = pd.read_csv('data/vox_cross_id_evaluation_best_frame.csv')
sources = pairs['source'].values
n = len(sources)
maps = {}
ids = []
for i in range(n):
addr = sources[i]
iden = addr[idx:idx+7]
ids.append(iden)
if iden in maps:
maps[iden].append(i)
else:
maps[iden] = []
maps[iden].append(i)
return maps ,ids
def Video_construction(ids, ours, others):
for id_ in ids:
mcnet = os.path.join(ours,str(id_)+'.mp4')
reader = imageio.get_reader('{}'.format(mcnet))
fps = reader.get_meta_data()['fps']
video = np.array(mimread('{}'.format(mcnet),memtest=False))
video = np.array([gray2rgb(frame) for frame in video])
num, h,w,c=video.shape
idx = w//3*2
src_and_dst = video[:,:,:idx,:]
mcnet = video[:,:,idx:,:]
result = []
result.append(src_and_dst)
for method in others:
mp = os.path.join(method,str(id_)+'.mp4')
video = np.array(mimread('{}'.format(mp),memtest=False))
video = np.array([gray2rgb(frame) for frame in video])
mds = video[:,:,idx:,:]
result.append(mds)
result.append(mcnet)
cats = np.concatenate(result,2)
imageio.mimsave('Compare/select_video/{}.mp4'.format(id_), cats, fps=fps*2)
print('Compare/select_video/{}.mp4'.format(id_))
# cv2.imwrite('Compare/{}/{}.jpg'.format(save_name,i),img)
def clipVideo(path):
reader = imageio.get_reader('{}'.format(path))
fps = reader.get_meta_data()['fps']
video = np.array(mimread('{}'.format(path),memtest=False))
video = np.array([gray2rgb(frame) for frame in video])
num, h,w,c=video.shape
# num, h,w,c=video.shape
bound = np.ones((num,224//3,w,c))*255
video = np.concatenate((bound,video,bound),1)
# interval = num//9
# video=video[interval*2:,...]
imageio.mimsave(path, video, fps=fps)
#串连所有的memory bank
def mergeMetaMb(path,name, rows_num=16, columns_num=32, start=0):
rows_num = rows_num
columns_num = columns_num
whole_img = []
column_barria = np.zeros((32,2,3))
for i in range(rows_num):
row = []
for j in range(columns_num):
num = i*columns_num+j + start
pth = os.path.join(path,'{}.jpg'.format(num))
img = cv2.imread(pth)
row.append(img)
row.append(column_barria)
row = np.concatenate(row[:-1],1)
h,w,c= row.shape
row_barria = np.zeros((2,w,3))
whole_img.append(row)
whole_img.append(row_barria)
whole_img = np.concatenate(whole_img[:-1],0)
cv2.imwrite(name,whole_img)
def mergeMetaMb_sub(path,name):
mbs = [3,9,33,13,23,459,108,149,196,171,418,369]
rows_num = 2
columns_num = 6
whole_img = []
column_barria = np.zeros((32,2,3))
for i in range(rows_num):
row = []
for j in range(columns_num):
num = i*columns_num+j
idx = mbs[num]
pth = os.path.join(path,'{}.jpg'.format(idx))
img = cv2.imread(pth)
row.append(img)
row.append(column_barria)
row = np.concatenate(row[:-1],1)
h,w,c= row.shape
row_barria = np.zeros((2,w,3))
whole_img.append(row)
whole_img.append(row_barria)
whole_img = np.concatenate(whole_img[:-1],0)
cv2.imwrite(name,whole_img)
# 画柱状图
def AKD():
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
name_list = ['Baseline','MCNet w/o F','MCNet w/o kp','MCNet']
num_list = [0.1236,0.1097, 0.1087,0.1065]
plt.bar(name_list, num_list, color=['aquamarine','darkseagreen', 'dodgerblue','crimson'], width=0.7)
plt.ylim((0.1,0.13))
# plt.yticks([])
ax = plt.gca()
ax.axes.yaxis.set_ticklabels([])
# plt.ylabel('Number of Patents')
plt.xticks([])
# plt.legend()
plt.grid(color = 'black', linestyle = '--', linewidth = 0.5)
plt.savefig('AKD.pdf')
plt.clf()
def AED():
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
name_list = ['Baseline','MCNet w/o F','MCNet w/o kp','MCNet']
num_list = [1.303,1.237, 1.227,1.203]
plt.bar(name_list, num_list, color=['aquamarine','darkseagreen', 'dodgerblue','crimson'], width=0.7)
plt.ylim((1.115,1.35))
ax = plt.gca()
ax.axes.yaxis.set_ticklabels([])
# plt.ylabel('Number of Patents')
plt.xticks([])
# plt.legend()
plt.grid(color = 'black', linestyle = '--', linewidth = 0.5)
plt.savefig('AED.pdf')
plt.clf()
def l1_bar():
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
name_list = ['Baseline','MCNet w/o F','MCNet w/o kp','MCNet']
num_list = [0.0356,0.0336, 0.0333,0.0331]
plt.bar(name_list, num_list, color=['aquamarine','darkseagreen', 'dodgerblue','crimson'], width=0.7)
plt.ylim((0.0330,0.0358))
# plt.ylabel('Number of Patents')
ax = plt.gca()
ax.axes.yaxis.set_ticklabels([])
plt.xticks([])
# plt.legend()
plt.grid(color = 'black', linestyle = '--', linewidth = 0.5)
plt.savefig('l1.pdf')
plt.clf()
def LPIPS_bar():
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
name_list = ['Baseline','MCNet w/o F','MCNet w/o kp','MCNet']
num_list = [0.182,0.175,0.175,0.174]
plt.bar(name_list, num_list, color=['aquamarine','darkseagreen', 'dodgerblue','crimson'], width=0.7)
plt.ylim((0.173,0.183))
# plt.ylabel('Number of Patents')
ax = plt.gca()
ax.axes.yaxis.set_ticklabels([])
plt.xticks([])
# plt.legend()
plt.grid(color = 'black', linestyle = '--', linewidth = 0.5)
plt.savefig('LPIPS.pdf')
plt.clf()
def PSNR_bar():
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
name_list = ['Baseline','MCNet w/o F','MCNet w/o kp','MCNet']
num_list = [31.701,31.896,31.932,31.942]
plt.bar(name_list, num_list, color=['aquamarine','darkseagreen', 'dodgerblue','crimson'], width=0.7)
plt.ylim((31.600,31.943))
# plt.ylabel('Number of Patents')
ax = plt.gca()
ax.axes.yaxis.set_ticklabels([])
plt.xticks([])
# plt.legend()
plt.grid(color = 'black', linestyle = '--', linewidth = 0.5)
plt.savefig('PSNR.pdf')
plt.clf()
def SSIM_bar():
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
name_list = ['Baseline','MCNet w/o F','MCNet w/o kp','MCNet']
num_list = [81.1,82.3,82.3,82.5]
plt.bar(name_list, num_list, color=['aquamarine','darkseagreen', 'dodgerblue','crimson'], width=0.7)
plt.ylim((81.0,82.6))
ax = plt.gca()
ax.axes.yaxis.set_ticklabels([])
# plt.ylabel('Number of Patents')
plt.xticks([])
# plt.legend()
plt.grid(color = 'black', linestyle = '--', linewidth = 0.5)
plt.savefig('SSIM.pdf')
plt.clf()
def Bars():
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
sns.set_theme(style="white", context="talk")
rs = np.random.RandomState(8)
# Set up the matplotlib figure
f, (ax1, ax2, ax3, ax4) = plt.subplots(1,4, figsize=(10,3), sharex=True)
# Generate some sequential data
x = np.array(list("ABCDEFGHIJ"))
y1 = np.arange(1, 11)
sns.barplot(x=x, y=y1, palette="rocket", ax=ax1)
ax1.axhline(0, color="k", clip_on=False)
ax1.set_ylabel("Sequential")
# Center the data to make it diverging
y2 = y1 - 5.5
sns.barplot(x=x, y=y2, palette="vlag", ax=ax2)
ax2.axhline(0, color="k", clip_on=False)
ax2.set_ylabel("Diverging")
# Randomly reorder the data to make it qualitative
y3 = rs.choice(y1, len(y1), replace=False)
sns.barplot(x=x, y=y3, palette="deep", ax=ax3)
ax3.axhline(0, color="k", clip_on=False)
ax3.set_ylabel("Qualitative")
# Finalize the plot
sns.despine(bottom=True)
plt.setp(f.axes, yticks=[])
plt.tight_layout(h_pad=2)
def sum_meta(path):
rows_num = 16
columns_num = 32
total=[]
for i in range(rows_num):
for j in range(columns_num):
num = i*columns_num+j
pth = os.path.join(path,'{}.jpg'.format(num))
img = cv2.imread(pth)
total.append(img)
pdb.set_trace()
total = np.stack(total).mean(0).astype(np.uint8)
# total = (total/(columns_num*rows_num)).astype(np.uint8)
print(total)
cv2.imwrite('mb_sum.jpg',total)
def cropVideo(path):
vps = os.listdir(path)
for vp in vps:
if '.mp4' in vp:
reader = imageio.get_reader('{}'.format(os.path.join(path,vp)))
fps = reader.get_meta_data()['fps']
video = np.array(mimread('{}'.format(os.path.join(path,vp)),memtest=False))
video = np.array([gray2rgb(frame) for frame in video])
num, h,w,c=video.shape
# num, h,w,c=video.shape
video = video[:,:,-w//3:,:]
imageio.mimsave('single_demo/{}'.format(vp), video, fps=fps)
print(vp)
def create_same_id_train_set(path):
vis = os.listdir(path)
videos = np.random.choice(vis, replace=False, size=100)
f = open('./data/vox_train_evaluation.csv','w',encoding='utf-8')
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in range(2083):
v = np.random.choice(videos, replace=False, size=1)
imgs = os.listdir(os.path.join(path,v[0]))
pair = np.random.choice(imgs, replace=False, size=2)
src = os.path.join(path,v[0],pair[0])
dst = os.path.join(path,v[0],pair[1])
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)
f.close()
def create_vox2_same_id_train_set(path):
path = '/data/fhongac/origDataset/Voxceleb2/vox2_train_frames/mp4/'
videos = sorted(glob(path+"/*/*/*"))
videos = np.random.choice(videos, replace=False, size=100)
f = open('./data/vox2_train_evaluation.csv','w',encoding='utf-8')
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in range(2083):
v = np.random.choice(videos, replace=False, size=1)[0]
imgs = sorted(glob(v+"/*.jpg"))
# imgs = os.listdir(os.path.join(path,v[0]))
pair = np.random.choice(imgs, replace=False, size=2)
src = pair[0]
dst = pair[1]
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)
f.close()
def create_HDTF_same_id_train_set():
path = '/ssddata/fhongac/origDataset/HDTF/frames_split/test'
videos = sorted(glob(path+"/*"))
# videos = np.random.choice(videos, replace=False, size=100)
f = open('./data/HDTF_test_evaluation_new.csv','w',encoding='utf-8')
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in tqdm(range(2083)):
v = np.random.choice(videos, replace=False, size=1)[0]
print(v)
imgs = sorted(glob(v+"/*.jpg"))
if len(imgs)<2:
continue
# imgs = os.listdir(os.path.join(path,v[0]))
pair = np.random.choice(imgs, replace=False, size=2)
src = pair[0]
dst = pair[1]
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)
f.close()
def create_HDTF_HHFQ_test_set(path):
ffhq = '/ssddata/fhongac/gitrepo/GRAM/datasets/ffhq'
ffhq = sorted(glob(ffhq+"/*.png"))
kids = '/ssddata/fhongac/origDataset/kids_face'
kids = sorted(glob(kids+"/*.png"))
# videos = np.random.choice(videos, replace=False, size=100)
f = open('./data/HDTF_HHFQ_test_evaluation.csv','w',encoding='utf-8')
source = []
driving = []
csv_writer = csv.writer(f)
csv_writer.writerow(["source","driving","frame"])
for i in tqdm(range(2083)):
adult = np.random.choice(ffhq, replace=False, size=1)[0]
kid = np.random.choice(kids, replace=False, size=1)[0]
pair = [adult,kid]
if random.random() < 0.5:
pair = pair[::-1]
src = pair[0]
dst = pair[1]
source.append(src)
driving.append(dst)
sources = np.array(source).reshape(-1,1)
driving = np.array(driving).reshape(-1,1)
content = np.concatenate((sources,driving),1)
csv_writer.writerows(content)