-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate128.py
More file actions
88 lines (73 loc) · 2.21 KB
/
generate128.py
File metadata and controls
88 lines (73 loc) · 2.21 KB
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
if __name__ == '__main__':
import os
import random
import argparse
import torch
import torch.nn.parallel
import torch.nn as nn
import torch.utils.data
import torchvision.utils as vutils
from tqdm import tqdm
# Configure
nc = 3
nz = 100
ngf = 32
ndf = 32
class Generator(nn.Module):
def __init__(self, ngpu):
super(Generator, self).__init__()
self.ngpu = ngpu
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d( nz, ngf * 16, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 16),
nn.ReLU(True),
# state size. (ngf*16) x 4 x 4
nn.ConvTranspose2d(ngf * 16, ngf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 8 x 8
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 4),
nn.ReLU(True),
# state size. (ngf*4) x 16 x 16
nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf * 2),
nn.ReLU(True),
# state size. (ngf*2) x 32 x 32
nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False),
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 64 x 64
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 128 x 128
)
def forward(self, input):
return self.main(input)
def positive_count(x):
x = int(x)
if x < 0:
raise argparse.ArgumentTypeError("Minimum count is 1")
return x
parser = argparse.ArgumentParser()
parser.add_argument('--seed', default=42, type=int)
parser.add_argument('--count', default=1, type=positive_count)
parser.add_argument('--output', default='images', type=str)
parser.add_argument('--netG', default='netG.pth', type=str)
parser.add_argument('--cuda', action='store_true')
args = parser.parse_args()
random.seed(args.seed)
torch.manual_seed(args.seed)
try:
os.makedirs(args.output)
except:
pass
device = 'cuda:0' if args.cuda else 'cpu'
netG = torch.load(args.netG)
netG.to(device)
netG.eval()
for i in tqdm(range(args.count)):
noise = torch.randn(1, nz, 1, 1, device=device)
output = netG(noise).cpu()
vutils.save_image(output.detach(), f'{ args.output }/image_{ i :06d}.png', normalize=True)