-
Notifications
You must be signed in to change notification settings - Fork 1
/
dc_gan.py
276 lines (232 loc) · 9.35 KB
/
dc_gan.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
from tqdm import tqdm
import utils.data as data
import utils.graphics as graphics
import utils.loss as loss
from models import gan, utils
seed = 42
np.random.seed(seed)
_ = torch.manual_seed(seed)
################################################################################
#################################### Config ####################################
################################################################################
experiment_name = f"gan_v1"
# Hyperparameters
learning_rate = 0.0002
num_epochs = 25
batch_size = 64
num_dataloader_workers = 0
beta1 = 0.5 # Beta1 hyperparam for Adam optimizers
# GAN Config
num_output_channels = 3
latent_dim = 100
generator_num_filters = 64
discriminator_num_filters = 64
# Data Config
image_size = 64
use_noise_images = True
data_prefix = "data\\Pokemon\\final\\standard"
output_prefix = f"data\\{experiment_name}"
train_data_folder = os.path.join(data_prefix, "train")
val_data_folder = os.path.join(data_prefix, "val")
test_data_folder = os.path.join(data_prefix, "test")
output_dir = os.path.join(output_prefix, "generated")
model_save_dir = os.path.join(output_prefix, "models")
loss_output_path = output_prefix
animation_output_path = os.path.join(output_prefix, "animation.mp4")
animation_sample_image_name = os.path.join(output_prefix, "animation_base.jpg")
test_sample_input_name = os.path.join(output_dir, "test_sample_input.jpg")
################################################################################
##################################### Setup ####################################
################################################################################
# Setup Device
gpu = torch.cuda.is_available()
device = torch.device("cuda" if gpu else "cpu")
print(gpu, device)
# Create Output Paths
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not os.path.exists(model_save_dir):
os.makedirs(model_save_dir)
################################################################################
################################## Data Setup ##################################
################################################################################
# Preprocess & Create Data Loaders
transform = data.image2tensor_resize(image_size)
dataset = data.EverythingDataset(
train_data_folder, val_data_folder, test_data_folder, transform, use_noise_images
)
dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_dataloader_workers,
pin_memory=gpu,
)
# Create batch of latent vectors that we will use to visualize
# the progression of the generator
sample = torch.randn((64, latent_dim, 1, 1), device=device)
################################################################################
##################################### Model ####################################
################################################################################
# Create the model
netG = gan.DCGANGenerator(
latent_dim=latent_dim,
num_filters=generator_num_filters,
num_output_channels=num_output_channels,
).to(device)
netD = gan.DCGANDiscriminator(
num_filters=discriminator_num_filters, num_output_channels=num_output_channels
).to(device)
# Apply the weights_init function to randomly initialize all weights
# to mean=0, stdev=0.02.
netG.apply(utils.weights_init)
netD.apply(utils.weights_init)
# Print the model
print(netG)
print(netD)
# Initialize BCELoss function
criterion = nn.BCELoss()
# Setup Adam optimizers for both G and D
optimizerD = optim.Adam(netD.parameters(), lr=learning_rate, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters(), lr=learning_rate, betas=(beta1, 0.999))
################################################################################
################################### Training ###################################
################################################################################
# Establish convention for real and fake labels during training
real_label = 1.0
fake_label = 0.0
# Training Loop
# Lists to keep track of progress
training_samples = []
all_generator_loss = []
all_discriminator_loss = []
iters = 0
print("Starting Training Loop...")
# For each epoch
for epoch in range(num_epochs):
netG.train()
netD.train()
generator_loss = 0
discriminator_loss = 0
# For each batch in the dataloader
for iteration, batch in enumerate(tqdm(dataloader)):
############################
# (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
###########################
# Zero Grad
netD.zero_grad()
# Format batch
_, batch = batch
real_cpu = batch.to(device)
b_size = real_cpu.size(0)
## Train with all-real batch
label = torch.full((b_size,), real_label, dtype=torch.float, device=device)
# Forward pass real batch through D
output = netD(real_cpu).view(-1)
# Calculate loss on all-real batch
errD_real = criterion(output, label)
# Calculate gradients for D in backward pass
errD_real.backward()
D_x = output.mean().item()
## Train with all-fake batch
# Generate batch of latent vectors
noise = torch.randn(b_size, latent_dim, 1, 1, device=device)
# Generate fake image batch with G
fake = netG(noise)
label.fill_(fake_label)
# Classify all fake batch with D
output = netD(fake.detach()).view(-1)
# Calculate D's loss on the all-fake batch
errD_fake = criterion(output, label)
# Calculate the gradients for this batch, accumulated (summed) with previous gradients
errD_fake.backward()
D_G_z1 = output.mean().item()
# Compute error of D as sum over the fake and the real batches
errD = errD_real + errD_fake
# Update D
optimizerD.step()
############################
# (2) Update G network: maximize log(D(G(z)))
###########################
netG.zero_grad()
label.fill_(real_label) # fake labels are real for generator cost
# Since we just updated D, perform another forward pass of all-fake batch through D
output = netD(fake).view(-1)
# Calculate G's loss based on this output
errG = criterion(output, label)
# Calculate gradients for G
errG.backward()
D_G_z2 = output.mean().item()
# Update G
optimizerG.step()
# Save Losses for plotting later
generator_loss += errG.item()
discriminator_loss += errD.item()
# Check how the generator is doing by saving G's output on fixed_noise
if (iters % 500 == 0) or (
(epoch == num_epochs - 1) and (iteration == len(dataloader) - 1)
):
with torch.no_grad():
fake = netG(sample).detach().cpu()
training_samples.append(vutils.make_grid(fake, padding=2, normalize=True))
iters += 1
generator_loss = generator_loss / len(dataloader)
discriminator_loss = discriminator_loss / len(dataloader)
all_generator_loss.append(generator_loss)
all_discriminator_loss.append(discriminator_loss)
# Output training stats
print(
f"[{epoch+1}/{num_epochs}]\tLoss_D: {errD.item():.4f}\tLoss_G: {errG.item():.4f}\tD(x): {D_x:.4f}\tD(G(z)): {D_G_z1:.4f} / {D_G_z2:.4f}"
)
# Save Model
torch.save(
{
"epoch": epoch,
"generator_model_state_dict": netG.state_dict(),
"generator_optimizer_state_dict": optimizerG.state_dict(),
"generator_loss": all_generator_loss,
"discriminator_model_state_dict": netD.state_dict(),
"discriminator_optimizer_state_dict": optimizerD.state_dict(),
"discriminator_loss": all_discriminator_loss,
},
os.path.join(model_save_dir, f"epoch_{epoch}_model.pt"),
)
netG.eval()
netD.eval()
# Generate a batch of fake images
with torch.no_grad():
# Generate batch of latent vectors
noise = torch.randn(batch_size, latent_dim, 1, 1, device=device)
# Generate fake image batch with G
generated = netG(noise).detach().cpu()
# Plot fake images
fig, axis = graphics.make_grid(("Test Sample", generated), 4, 4)
plt.savefig(os.path.join(output_dir, f"epoch_{epoch}_test_sample_output.jpg"))
################################################################################
################################## Save & Test #################################
################################################################################
# Save output graphs
graphics.draw_loss(
all_generator_loss, all_discriminator_loss, loss_output_path, mode="gan"
)
# Create & Save Animation
anim = graphics.make_gan_animation(training_samples)
anim.save(animation_output_path)
# Pick a couple of sample images for an Input v Output comparison
sample = data.get_samples_from_data(dataset, 16)
# Plot A Set of Test Images
fig, axis = graphics.make_grid(("Test Sample", sample), 4, 4)
plt.savefig(test_sample_input_name)