-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint2image.py
72 lines (55 loc) · 2.1 KB
/
paint2image.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
import torch
import os
from imageio import imread, imwrite
from singan import SinGAN
from log import TensorboardLogger
import argparse
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(description='SinGAN - Scale Injections')
parser.add_argument('--run_name', required=True)
parser.add_argument('--paint', required=True)
parser.add_argument('--target_height', type=int, default=None)
parser.add_argument('--target_width', type=int, default=None)
parser.add_argument('--not_pretrained', action='store_true')
parser.add_argument('--img')
parser.add_argument('--N', type=int, default=0)
parser.add_argument('--steps_per_scale', type=int, default=2000)
args = parser.parse_args()
# get the available device
if torch.cuda.is_available():
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
# instantiate the logger and the SinGAN
logger = TensorboardLogger(f'singan_{args.run_name}')
singan = SinGAN(N=args.N, logger=logger, device=device)
img_size = None
if args.not_pretrained:
# load the single training image
train_img_path = os.path.join('data', args.img)
train_img = imread(train_img_path)
# get the size of the img for later
img_size = train_img.shape[:-1]
# fit SinGAN to it
singan.fit(img=train_img, steps_per_scale=args.steps_per_scale)
# after training, save the model in a checkpoint
singan.save_checkpoint()
else:
# load the existing checkpoint if possible
singan.load_checkpoint(logger.run_name)
# load the single training image
paint_img_path = os.path.join('data', 'paint', args.paint)
paint_img = imread(paint_img_path)
# get the size of the img for later
paint_size = paint_img.shape[:-1]
if img_size:
assert img_size == paint_size
if args.target_height is None:
target_size = paint_size
else:
assert args.target_width is not None
target_size = (args.target_height, args.target_width)
# scale injections
for scale in range(singan.N):
x = singan.test(target_size=target_size, start_at_scale=scale, injection=paint_img)
imwrite(f'samples/{logger.run_name}/paint_{args.paint.replace(".jpg", "")}_{scale}.jpg', x)