This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
forked from Yukariin/CSA_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
66 lines (50 loc) · 1.87 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
import argparse
from PIL import Image
import torch
from torchvision import transforms
from torchvision.utils import save_image
from model import InpaintNet
def norm(x):
return 2. * x - 1. # [0,1] -> [-1,1]
def denorm(x):
out = (x + 1) / 2 # [-1,1] -> [0,1]
return out.clamp_(0, 1)
parser = argparse.ArgumentParser()
parser.add_argument('--image', type=str,
help='The filename of image to be completed.')
parser.add_argument('--mask', type=str,
help='The filename of mask, value 255 indicates mask.')
parser.add_argument('--output', default='output.png', type=str,
help='Where to write output.')
parser.add_argument('--checkpoint', type=str,
help='The filename of pickle checkpoint.')
if __name__ == "__main__":
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
device = torch.device('cuda' if use_cuda else 'cpu')
g_model = InpaintNet().to(device)
g_checkpoint = torch.load(args.checkpoint, map_location=device)
g_model.load_state_dict(g_checkpoint)
g_model.eval()
to_tensor = transforms.ToTensor()
img = Image.open(args.image).convert('RGB')
mask = Image.open(args.mask).convert('RGB')
img = to_tensor(img)
mask = to_tensor(mask)
_, h, w = img.shape
grid = 256
img = img[:, :h//grid*grid, :w//grid*grid]
mask = mask[:, :h//grid*grid, :w//grid*grid]
img = img.unsqueeze_(0) # CHW -> BCHW
mask = mask.unsqueeze_(0) # CHW -> BCHW
img = norm(img) # [0,1] -> [-1,1]
mask = mask[:, 0:1, :, :] #Bx3xHxW -> Bx1xHxW
img = img * (1. - mask)
img = img.to(device)
mask = mask.to(device)
print(img.shape)
import time
start_time = time.time()
_, result, _, _ = g_model(img, mask)
print("Done in %.3f seconds!" % (time.time() - start_time))
save_image(denorm(result), args.output)