-
Notifications
You must be signed in to change notification settings - Fork 20
/
test.py
200 lines (150 loc) · 7.32 KB
/
test.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
# coding=utf-8
# summary:
# author: Jianqiang Ren
# date:
from reshape_base_algos.body_retoucher import BodyRetoucher
import time
import cv2
import argparse
import numpy as np
import glob
import tqdm
import os
import json
import shutil
from utils.eval_util import cal_lpips_and_ssim, psnr
from config.test_config import TESTCONFIG, load_config
import toml
def recurve_search(root_path, all_paths, suffix=[]):
for file in os.listdir(root_path):
target_file = os.path.join(root_path, file)
if os.path.isfile(target_file):
(path, extension) = os.path.splitext(target_file)
if extension in suffix:
all_paths.append(target_file)
else:
recurve_search(target_file, all_paths, suffix)
if __name__ == "__main__":
timestamp = time.strftime("%Y%m%d-%H%M%S", time.localtime())
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='config/test_cvpr_setting.toml', required=True)
args = parser.parse_args()
with open(args.config) as f:
load_config(toml.load(f))
print('TEST CONFIG: \n{}'.format(TESTCONFIG))
print("loading model:{}".format(TESTCONFIG.reshape_ckpt_path))
ret = BodyRetoucher.init(reshape_ckpt_path=TESTCONFIG.reshape_ckpt_path,
pose_estimation_ckpt=TESTCONFIG.pose_estimation_ckpt,
device=0, log_level='error',
log_path='test_log.txt',
debug_level=0)
if ret == 0:
print('init done')
else:
print('init error:{}'.format(ret))
exit(0)
timestamp = time.strftime("%Y%m%d-%H%M%S", time.localtime())
if os.path.exists(TESTCONFIG.save_dir):
shutil.rmtree(TESTCONFIG.save_dir)
os.makedirs(TESTCONFIG.save_dir, exist_ok=True)
shutil.copy(args.config, os.path.join(TESTCONFIG.save_dir, os.path.basename(args.config )))
if os.path.isfile(TESTCONFIG.src_dir):
img_list = [TESTCONFIG.src_dir]
elif os.path.exists(os.path.join(TESTCONFIG.src_dir, "src")):
img_list = glob.glob("{}/*.*g".format(os.path.join(TESTCONFIG.src_dir, "src")))
else:
img_list = []
recurve_search(TESTCONFIG.src_dir, img_list, suffix=['.png', '.jpg', '.jpeg','.JPG'])
img_list = sorted(img_list)
lpips_list = []
ssim_list = []
psnr_list = []
epe_list = []
src_lpips_list = []
src_ssim_list = []
src_psnr_list = []
bad_sample = []
pbar = tqdm.tqdm(img_list)
for src_path in pbar:
print('image_path: {}'.format(src_path))
basename = os.path.basename(src_path)
gt_path = os.path.join(TESTCONFIG.gt_dir, basename)
if os.path.exists(gt_path):
gt = cv2.imread(gt_path)
else:
gt = None
base = os.path.splitext(basename)[0]
src_img = cv2.imread(src_path)
if src_img is None:
print('Error: src_img is None')
continue
t1 = time.time()
pred, flow = BodyRetoucher.reshape_body(src_img, degree=TESTCONFIG.degree)
print('time of BodyRetoucher.run: {}ms/frame'.format(int((time.time() - t1) * 1000)))
if flow is None:
bad_sample.append(src_path)
continue
info = ""
if gt is not None:
if gt.shape[:2] != src_img.shape[:2]:
gt = cv2.resize(gt,(src_img.shape[1], src_img.shape[0]),interpolation=cv2.INTER_LINEAR)
src_rgb = src_img[:,:,::-1]
gt_rgb = gt[:,:,::-1]
pred_rgb = pred[:,:,::-1]
t2 = time.time()
psnr_value = psnr(pred_rgb, gt_rgb)
src_psnr_value = psnr(src_rgb, gt_rgb)
t2 = time.time()
lpips_value, ssim_value = cal_lpips_and_ssim(pred_rgb,gt_rgb)
src_lpips_value, src_ssim_value = cal_lpips_and_ssim(src_rgb, gt_rgb)
lpips_list.append(lpips_value)
ssim_list.append(ssim_value)
psnr_list.append(psnr_value)
src_lpips_list.append(src_lpips_value)
src_ssim_list.append(src_ssim_value)
src_psnr_list.append(src_psnr_value)
pbar.set_description(info + "pred ssim:{:.4},psnr:{:.4},lpips:{:.4}".format(np.mean(ssim_list),np.mean(psnr_list),np.mean(lpips_list)) + "|src ssim:{:.4},psnr:{:.4},lpips:{:.4}".format(np.mean(src_ssim_list), np.mean(src_psnr_list),
np.mean(src_lpips_list)))
cv2.imwrite(os.path.join(TESTCONFIG.save_dir,base + "_warp_{}.jpg".format(os.path.basename(TESTCONFIG.reshape_ckpt_path).split('.')[0])),pred)
cv2.imwrite(os.path.join(TESTCONFIG.save_dir, base + ".jpg"), src_img)
if gt is not None:
cv2.imwrite(os.path.join(TESTCONFIG.save_dir, base + "_gt.jpg"), gt)
if BodyRetoucher._debug_level > 0:
for i in [0,1]:
if os.path.exists('pred_{}.jpg'.format(i)):
os.rename('pred_{}.jpg'.format(i), base+'_pred_{}.jpg'.format(i))
shutil.move(base+'_pred_{}.jpg'.format(i), TESTCONFIG.save_dir)
if os.path.exists('flow_{}.jpg'.format(i)):
os.rename('flow_{}.jpg'.format(i), base + 'flow_{}.jpg'.format(i))
shutil.move(base + 'flow_{}.jpg'.format(i), TESTCONFIG.save_dir)
if os.path.exists('flow_{}.jpg'.format(i)):
os.rename('flow_{}.jpg'.format(i), base + 'flow_{}.jpg'.format(i))
shutil.move(base + 'flow_{}.jpg'.format(i), TESTCONFIG.save_dir)
if os.path.exists('x_fusion_map_{}.jpg'.format(i)):
os.rename('x_fusion_map_{}.jpg'.format(i), base + '_x_fusion_map_{}.jpg'.format(i))
shutil.move(base + '_x_fusion_map_{}.jpg'.format(i), TESTCONFIG.save_dir)
if os.path.exists('y_fusion_map_{}.jpg'.format(i)):
os.rename('y_fusion_map_{}.jpg'.format(i), base + '_y_fusion_map_{}.jpg'.format(i))
shutil.move(base + '_y_fusion_map_{}.jpg'.format(i), TESTCONFIG.save_dir)
os.rename('flow_all.jpg', base + '_flow_all.jpg')
shutil.move(base + '_flow_all.jpg', TESTCONFIG.save_dir)
if gt is not None:
print(f"val count:{len(psnr_list)}")
print(f"pred mean ssim:{np.mean(ssim_list)}")
print(f"pred mean psnr:{np.mean(psnr_list)}")
print(f"pred mean lpips:{np.mean(lpips_list)}")
print(f"src mean ssim:{np.mean(src_ssim_list)}")
print(f"src mean psnr:{np.mean(src_psnr_list)}")
print(f"src mean lpips:{np.mean(src_lpips_list)}")
print(f"bad sample count:{len(bad_sample)}")
print(f"bad samples:{bad_sample}")
cur_time =timestamp
print(cur_time)
with open("record_{}_weight_{}.txt".format(cur_time, os.path.basename(TESTCONFIG.reshape_ckpt_path).split('.')[0]),"w") as f:
f.write("time:{}\n".format(timestamp))
f.write("count :{}\n".format(len(ssim_list)))
f.write("ssim of pred/gt :{}\n".format(np.mean(ssim_list)))
f.write("psnr of pred/gt :{}\n".format(np.mean(psnr_list)))
f.write("lpips of pred/gt :{}\n".format(np.mean(lpips_list)))
BodyRetoucher.release()
print('all done')