-
Notifications
You must be signed in to change notification settings - Fork 44
/
test.py
270 lines (226 loc) · 8.96 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
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
from collections import defaultdict
from itertools import islice
from multiprocessing.pool import ThreadPool as Pool
import os
from pathlib import Path
import argparse
import cv2
import numpy as np
import torch
import tqdm
from utils import list2nparray, gen_miss, merge_imgs
from model import DFNet
class Tester:
def __init__(self, model_path, input_size, batch_size):
self.model_path = model_path
self._input_size = input_size
self.batch_size = batch_size
self.init_model(model_path)
@property
def input_size(self):
if self._input_size > 0:
return (self._input_size, self._input_size)
elif "celeba" in self.model_path:
return (256, 256)
else:
return (512, 512)
def init_model(self, path):
if torch.cuda.is_available():
self.device = torch.device("cuda")
print("Using gpu.")
else:
self.device = torch.device("cpu")
print("Using cpu.")
self.model = DFNet().to(self.device)
checkpoint = torch.load(path, map_location=self.device)
self.model.load_state_dict(checkpoint)
self.model.eval()
print("Model %s loaded." % path)
def get_name(self, path):
return ".".join(Path(path).name.split(".")[:-1])
def results_path(self, output, img_path, mask_path, prefix="result"):
img_name = self.get_name(img_path)
mask_name = self.get_name(mask_path)
return {
"result_path": self.sub_dir("result").joinpath(
"result-{}-{}.png".format(img_name, mask_name)
),
"raw_path": self.sub_dir("raw").joinpath(
"raw-{}-{}.png".format(img_name, mask_name)
),
"alpha_path": self.sub_dir("alpha").joinpath(
"alpha-{}-{}.png".format(img_name, mask_name)
),
}
def inpaint_instance(self, img, mask):
"""Assume color image with 3 dimension. CWH"""
img = img.view(1, *img.shape)
mask = mask.view(1, 1, *mask.shape)
return self.inpaint_batch(img, mask).squeeze()
def inpaint_batch(self, imgs, masks):
"""Assume color channel is BGR and input is NWHC np.uint8."""
imgs = np.transpose(imgs, [0, 3, 1, 2])
masks = np.transpose(masks, [0, 3, 1, 2])
imgs = torch.from_numpy(imgs).to(self.device)
masks = torch.from_numpy(masks).to(self.device)
imgs = imgs.float().div(255)
masks = masks.float().div(255)
imgs_miss = imgs * masks
results = self.model(imgs_miss, masks)
if type(results) is list:
results = results[0]
results = results.mul(255).byte().data.cpu().numpy()
results = np.transpose(results, [0, 2, 3, 1])
return results
def _process_file(self, output, img_path, mask_path):
item = {"img_path": img_path, "mask_path": mask_path}
item.update(self.results_path(output, img_path, mask_path))
self.path_pair.append(item)
def process_single_file(self, output, img_path, mask_path):
self.path_pair = []
self._process_file(output, img_path, mask_path)
def process_dir(self, output, img_dir, mask_dir):
img_dir = Path(img_dir)
mask_dir = Path(mask_dir)
imgs_path = sorted(list(img_dir.glob("*.jpg")) + list(img_dir.glob("*.png")))
masks_path = sorted(list(mask_dir.glob("*.jpg")) + list(mask_dir.glob("*.png")))
n_img = len(imgs_path)
n_mask = len(masks_path)
n_pair = min(n_img, n_mask)
self.path_pair = []
for i in range(n_pair):
img_path = imgs_path[i % n_img]
mask_path = masks_path[i % n_mask]
self._process_file(output, img_path, mask_path)
def get_process(self, input_size):
def process(pair):
img = cv2.imread(str(pair["img_path"]), cv2.IMREAD_COLOR)
mask = cv2.imread(str(pair["mask_path"]), cv2.IMREAD_GRAYSCALE)
if input_size:
img = cv2.resize(img, input_size)
mask = cv2.resize(mask, input_size)
img = np.ascontiguousarray(img.transpose(2, 0, 1)).astype(np.uint8)
mask = np.ascontiguousarray(np.expand_dims(mask, 0)).astype(np.uint8)
pair["img"] = img
pair["mask"] = mask
return pair
return process
def _file_batch(self):
pool = Pool()
n_pair = len(self.path_pair)
n_batch = (n_pair - 1) // self.batch_size + 1
for i in tqdm.trange(n_batch, leave=False):
_buffer = defaultdict(list)
start = i * self.batch_size
stop = start + self.batch_size
process = self.get_process(self.input_size)
batch = pool.imap_unordered(process, islice(self.path_pair, start, stop))
for instance in batch:
for k, v in instance.items():
_buffer[k].append(v)
yield _buffer
def batch_generator(self):
generator = self._file_batch
for _buffer in generator():
for key in _buffer:
if key in ["img", "mask"]:
_buffer[key] = list2nparray(_buffer[key])
yield _buffer
def to_numpy(self, tensor):
tensor = tensor.mul(255).byte().data.cpu().numpy()
tensor = np.transpose(tensor, [0, 2, 3, 1])
return tensor
def process_batch(self, batch, output):
imgs = torch.from_numpy(batch["img"]).to(self.device)
masks = torch.from_numpy(batch["mask"]).to(self.device)
imgs = imgs.float().div(255)
masks = masks.float().div(255)
imgs_miss = imgs * masks
result, alpha, raw = self.model(imgs_miss, masks)
result, alpha, raw = result[0], alpha[0], raw[0]
result = imgs * masks + result * (1 - masks)
result = self.to_numpy(result)
alpha = self.to_numpy(alpha)
raw = self.to_numpy(raw)
for i in range(result.shape[0]):
cv2.imwrite(str(batch["result_path"][i]), result[i])
cv2.imwrite(str(batch["raw_path"][i]), raw[i])
cv2.imwrite(str(batch["alpha_path"][i]), alpha[i])
@property
def root(self):
return Path(self.output)
def sub_dir(self, sub):
return self.root.joinpath(sub)
def prepare_folders(self, folders):
for folder in folders:
Path(folder).mkdir(parents=True, exist_ok=True)
def inpaint(self, output, img, mask, merge_result=False):
self.output = output
self.prepare_folders(
[self.sub_dir("result"), self.sub_dir("alpha"), self.sub_dir("raw")]
)
if os.path.isfile(img) and os.path.isfile(mask):
if img.endswith((".png", ".jpg", ".jpeg")):
self.process_single_file(output, img, mask)
_type = "file"
else:
raise NotImplementedError()
elif os.path.isdir(img) and os.path.isdir(mask):
self.process_dir(output, img, mask)
_type = "dir"
else:
print("Img: ", img)
print("Mask: ", mask)
raise NotImplementedError("img and mask should be both file or directory.")
print("# Inpainting...")
print("Input size:", self.input_size)
for batch in self.batch_generator():
self.process_batch(batch, output)
print("Inpainting finished.")
if merge_result:
miss = self.sub_dir("miss")
merge = self.sub_dir("merge")
print("# Preparing input images...")
gen_miss(img, mask, miss)
print("# Merging...")
merge_imgs(
[
miss,
self.sub_dir("raw"),
self.sub_dir("alpha"),
self.sub_dir("result"),
img,
],
merge,
res=self.input_size[0],
)
print("Merging finished.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-m",
"--model",
default="./model/model_places2.pth",
help="Select a checkpoint.",
)
parser.add_argument(
"-i", "--input_size", default=0, type=int, help="Batch size for testing."
)
parser.add_argument(
"-b", "--batch_size", default=8, type=int, help="Batch size for testing."
)
parser.add_argument(
"--img", default="./samples/places2/img", help="Image or Image folder."
)
parser.add_argument(
"--mask", default="./samples/places2/mask", help="Mask or Mask folder."
)
parser.add_argument("--output", default="./output/places2", help="Output dir")
parser.add_argument(
"--merge",
action="store_true",
help="Whether merge input and results for better viewing.",
)
args = parser.parse_args()
tester = Tester(args.model, args.input_size, args.batch_size)
tester.inpaint(args.output, args.img, args.mask, merge_result=args.merge)