-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_image.py
165 lines (140 loc) · 5.49 KB
/
test_image.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
import argparse
import os
import torch
import torch.nn as nn
from PIL import Image
from os.path import basename, splitext
from torchvision import transforms
from torchvision.utils import save_image
from function import calc_mean_std, normal, coral
import net as net
import numpy as np
import cv2
import torch.backends.cudnn as cudnn
from tqdm import tqdm
import imageio
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# read image files
def get_files(img_dir):
files = os.listdir(img_dir)
paths = []
for x in files:
if x.endswith('.jpg') or x.endswith('.jpeg') or x.endswith('.png'):
paths.append(os.path.join(img_dir, x))
# return [os.path.join(img_dir,x) for x in files]
return paths
#loading the images
def load_images(content_dir, style_dir):
if os.path.isdir(content_dir):
content_paths = get_files(content_dir)
else: # Single image file
content_paths = [content_dir]
if os.path.isdir(style_dir):
style_paths = get_files(style_dir)
else: # Single image file
style_paths = [style_dir]
return content_paths, style_paths
def test_transform(size, crop):
transform_list = []
if size != 0:
transform_list.append(transforms.Resize(size))
if crop:
transform_list.append(transforms.CenterCrop(size))
transform_list.append(transforms.ToTensor())
transform = transforms.Compose(transform_list)
return transform
def style_transform(ori_size):
transform_list = []
max_s = int (np.max(ori_size))
thresh = 512
if max_s > thresh:
ratio = max_s / thresh
current_size = (int(ori_size[0] / ratio), int(ori_size[1] / ratio))
transform_list.append(transforms.Resize(current_size))
transform_list.append(transforms.ToTensor())
transform = transforms.Compose(transform_list)
return transform
def content_transform(ori_size):
resized = 0
transform_list = []
max_s = int (np.max(ori_size))
thresh = 512
if max_s > thresh:
resized = 1
ratio = max_s / thresh
current_size = (int(ori_size[0] / ratio), int(ori_size[1] / ratio))
transform_list.append(transforms.Resize(current_size))
transform_list.append(transforms.ToTensor())
transform = transforms.Compose(transform_list)
return transform, resized
def resize_transform(ori_size):
transform_list = [
transforms.Resize(ori_size)
]
transform = transforms.Compose(transform_list)
return transform
def image_process(network, content, style):
C_size = content.size[::-1]
S_size = style.size[::-1]
content_tf1, resized = content_transform(C_size)
content_frame = content_tf1(content)
style_tf1 = style_transform(S_size)
style = style_tf1(style)
style = style.to(device).unsqueeze(0)
content = content_frame.to(device).unsqueeze(0)
with torch.no_grad():
output = network(content, style)
output = output.squeeze(0)
if resized:
resize_tf = resize_transform(C_size)
output = resize_tf(output)
return output.cpu()
def process_image(network, content_path, style_path, outfile):
image_name = outfile + '/{:s}_stylized_{:s}.jpg'.format(
splitext(basename(content_path))[0], splitext(basename(style_path))[0])
content = Image.open(content_path).convert("RGB")
style = Image.open(style_path).convert("RGB")
output = image_process(network, content, style)
save_image(output, image_name)
def test_image(network, content_paths, style_paths, output_path):
pbar = tqdm(total = len(content_paths)*len(style_paths))
for style_path in style_paths:
for content_path in content_paths:
outfile = output_path + '/' + splitext(basename(content_path))[0] + '/'
if not os.path.exists(outfile):
os.makedirs(outfile)
process_image(network, content_path, style_path, outfile)
pbar.update(1)
def create_args():
parser = argparse.ArgumentParser()
parser.add_argument('--KC', type=int, default=4)
parser.add_argument('--KS', type=int, default=-10)
parser.add_argument('--content_dir', type=str,
help='Directory path to a batch of content images')
parser.add_argument('--style_dir', type=str,
help='Directory path to a batch of style images')
parser.add_argument('--output_dir', type=str, default='./results',
help='Directory to save the output image(s)')
parser.add_argument('--csbnet_path', type=str, default='./models/csbnet.pth',
help='The path of the csbnet pretrained model')
parser.add_argument('--vgg_path', type=str, default='./models/vgg_normalised.pth',
help='The path of the pretained vgg-net model')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = create_args()
if not os.path.exists(args.output_dir):
os.mkdir(args.output_dir)
#load pre-trained vgg-net
vgg = net.vgg
vgg.to(device)
vgg.load_state_dict(torch.load(args.vgg_path, map_location=device))
vgg.eval()
#load CSBNet
network = net.Net(vgg, KC=args.KC, KS=args.KS)
network.to(device)
network.csbnet.load_state_dict(torch.load(args.csbnet_path, map_location=device))
network.eval()
#inference
content_paths, style_paths = load_images(args.content_dir, args.style_dir)
test_image(network, content_paths, style_paths, args.output_dir)