-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest_enhance_dir_align.py
62 lines (51 loc) · 2.4 KB
/
test_enhance_dir_align.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
import os
from options.test_options import TestOptions
from data import create_dataset
from models import create_model
from utils import utils
from PIL import Image
from tqdm import tqdm
import torch
import time
import numpy as np
if __name__ == '__main__':
opt = TestOptions().parse() # get test options
opt.num_threads = 0 # test code only supports num_threads = 1
opt.batch_size = 4 # test code only supports batch_size = 1
opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed.
opt.no_flip = True
dataset = create_dataset(opt) # create a dataset given opt.dataset_mode and other options
model = create_model(opt) # create a model given opt.model and other options
model.load_pretrain_models()
save_dir = opt.results_dir
os.makedirs(save_dir, exist_ok=True)
print('creating result directory', save_dir)
netP = model.netP
netG = model.netG
model.eval()
max_size = 9999
os.makedirs(os.path.join(save_dir, 'sr'), exist_ok=True)
for i, data in tqdm(enumerate(dataset), total=len(dataset)//opt.batch_size):
inp = data['LR']
with torch.no_grad():
parse_map, _ = netP(inp)
parse_map_sm = (parse_map == parse_map.max(dim=1, keepdim=True)[0]).float()
output_SR = netG(inp, parse_map_sm)
img_path = data['LR_paths'] # get image paths
for i in tqdm(range(len(img_path))):
inp_img = utils.batch_tensor_to_img(inp)
output_sr_img = utils.batch_tensor_to_img(output_SR)
ref_parse_img = utils.color_parse_map(parse_map_sm)
save_path = os.path.join(save_dir, 'lq', os.path.basename(img_path[i]))
os.makedirs(os.path.join(save_dir, 'lq'), exist_ok=True)
save_img = Image.fromarray(inp_img[i])
save_img.save(save_path)
save_path = os.path.join(save_dir, 'hq', os.path.basename(img_path[i]))
os.makedirs(os.path.join(save_dir, 'hq'), exist_ok=True)
save_img = Image.fromarray(output_sr_img[i])
save_img.save(save_path)
save_path = os.path.join(save_dir, 'parse', os.path.basename(img_path[i]))
os.makedirs(os.path.join(save_dir, 'parse'), exist_ok=True)
save_img = Image.fromarray(ref_parse_img[i])
save_img.save(save_path)
if i > max_size: break