-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
120 lines (93 loc) · 3.67 KB
/
main.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
import os
import cv2
import argparse
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torchvision as tv
from models import Generator
from models import Discriminator
from torchvision import datasets
from torch.utils.data import DataLoader
root = os.path.expanduser('~/.cache/torch/datasets')
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
parser = argparse.ArgumentParser()
parser.add_argument('--data', type=str, default=root)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--lr', type=float, default=0.0002)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--in_channels', type=int, default=64)
parser.add_argument('--out_channels', type=int, default=1)
cfg = parser.parse_args()
print(cfg)
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.xavier_normal_(m.weight)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight, 1.0, 0.02)
nn.init.constant_(m.bias, 0)
net_G = Generator(cfg.in_channels, cfg.out_channels)
net_G = net_G.to(device)
net_G.apply(weights_init)
net_D = Discriminator(cfg.out_channels)
net_D = net_D.to(device)
net_D.apply(weights_init)
dataset = datasets.MNIST(cfg.data, transform=tv.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=cfg.batch_size, shuffle=True, drop_last=True)
fixed_noises = torch.randn(cfg.batch_size, cfg.in_channels, 1, 1, device=device)
criterion = nn.MSELoss() # V(D, G) = log(D(x)) + log(1 - D(G(z)))
optimizer_G = torch.optim.Adam(net_G.parameters(), lr=0.0002, betas=(0.5, 0.999))
optimizer_D = torch.optim.Adam(net_D.parameters(), lr=0.0002, betas=(0.5, 0.999))
plt.ion()
for epoch in range(cfg.epochs):
print('Epoch: {}/{}'.format(epoch + 1, cfg.epochs))
for i, data in enumerate(dataloader):
noises = torch.randn_like(fixed_noises, device=device)
# -------------------
# Train Discriminator
# -------------------
optimizer_D.zero_grad()
# Update net_D: maximize D(x), minimize D(G(z)) -> maximize V(D, G)
# train with real
real = data[0].to(device)
pred_real = net_D(real)
loss_real = criterion(pred_real, torch.ones_like(pred_real, device=device))
D_x = pred_real.mean().item()
# train with fake
fake = net_G(noises)
pred_fake = net_D(fake.detach()) # not calc net_G's grad
loss_fake = criterion(pred_fake, torch.zeros_like(pred_fake, device=device))
D_G_z1 = pred_fake.mean().item()
loss_D = loss_real + loss_fake
loss_D.backward()
optimizer_D.step()
# ---------------
# Train Generator
# ---------------
optimizer_G.zero_grad()
# Update net_G: maximize D(G(z)) -> minimize V(D, G)
pred_fake = net_D(fake)
loss_G = criterion(pred_fake, torch.ones_like(pred_fake, device=device))
D_G_z2 = pred_fake.mean().item()
loss_G.backward()
optimizer_G.step()
# ---------
# Print Log
# ---------
print(
'[{}/{}]'.format(epoch + 1, cfg.epochs) +
'[{}/{}]'.format(i + 1, len(dataloader)) + ', ' +
'loss_D: {:.4f}, loss_G: {:.4f}'.format(loss_D.item(), loss_G.item()) + ', ' +
'D(x): {:.2f}, D(G(z)): {:.2f}/{:.2f}'.format(D_x, D_G_z1, D_G_z2)
)
if i % 100 == 99:
fake = net_G(fixed_noises)
fake = fake.detach().cpu()
fake = tv.utils.make_grid(fake)
fake = fake.numpy().transpose(1, 2, 0)
plt.imshow(fake)
plt.pause(0.1)
plt.ioff()
plt.show()