forked from egorzakharov/PerceptualGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
64 lines (43 loc) · 1.54 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
import argparse
import torch
from PIL import Image
from torchvision import transforms as T
from torch.autograd import Variable
import os
import numpy as np
# Options specification
parser = argparse.ArgumentParser(conflict_handler='resolve')
# Dataset options
parser.add_argument(
'--input_path', default='', type=str,
help='path to input images')
parser.add_argument(
'--img_list', default='', type=str,
help='*optional* path to txt list with image names subset')
parser.add_argument(
'--img_size', default=-1, type=int,
help='rescale input image to that size')
parser.add_argument(
'--net_path', default='', type=str,
help='path to the .pkl file with the network')
parser.add_argument(
'--output_path', default='', type=str,
help='path to the output directory')
opt, _ = parser.parse_known_args()
net = torch.load(opt.net_path).cuda()
transforms = []
if opt.img_size > 0:
transforms += [T.Scale(opt.img_size)]
transforms += [T.ToTensor(), T.Normalize([0.5]*3, [0.5]*3)]
in_t = T.Compose(transforms)
out_t = T.ToPILImage()
if opt.img_list:
names = np.loadtxt(opt.img_list, dtype=str).tolist()
else:
names = os.listdir(opt.input_path)
for name in names:
in_img = Image.open(os.path.join(opt.input_path, name))
out_img = out_t(((net(Variable(in_t(in_img)[None].cuda()))[0] + 1) / 2).detach().cpu())
output_path = os.path.join(opt.output_path, name)
in_img.save(os.path.join(opt.output_path, name.split('.')[0] + '_original.jpg'))
out_img.save(os.path.join(opt.output_path, name.split('.')[0] + '_result.jpg'))