-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
53 lines (42 loc) · 1.6 KB
/
sample.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
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 - Random Sampling')
parser.add_argument('--run_name', required=True)
parser.add_argument('--height', type=int, required=True)
parser.add_argument('--width', type=int, required=True)
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)
if args.not_pretrained:
# load the single training image
train_img_path = os.path.join('data', args.img)
train_img = imread(train_img_path)
# 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)
train_img = singan.train_img
# get the size of the img for later
img_size = train_img.shape[:-1]
# random sampling for different but fixed sizes
size = (args.height, args.width)
x = singan.test(target_size=size)
imwrite(f'samples/{logger.run_name}/size_{size[0]}x{size[1]}.jpg', x)