-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
272 lines (224 loc) · 10.1 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
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
# -*- coding: utf-8 -*-
"""Wasserstein and Standard Turing GAN with Spectral Normalization
"""
from __future__ import print_function
from __future__ import division
__author__ = "Rahul Bhalley"
import torch
import torch.nn as nn
import torch.optim as optim
from config import *
if IMG_DIM == 32:
from t_sn_gan_32 import *
elif IMG_DIM == 256:
from t_sn_gan_256 import *
import torchvision
import torchvision.transforms as transforms
import torchvision.utils as vutils
import os
# Make experiments reproducible
_ = torch.manual_seed(12345)
####################
# Make directories #
# - Samples #
# - Checkpoints #
####################
if not os.path.exists(MODE):
os.mkdir(MODE)
# Directory for samples
if not os.path.exists(os.path.join(MODE, 'samples')):
os.mkdir(os.path.join(MODE, 'samples'))
if not os.path.exists(os.path.join(MODE, 'samples', DATASET)):
os.mkdir(os.path.join(MODE, 'samples', DATASET))
# Directory for checkpoints
if not os.path.exists(os.path.join(MODE, 'checkpoints')):
os.mkdir(os.path.join(MODE, 'checkpoints'))
if not os.path.exists(os.path.join(MODE, 'checkpoints', DATASET)):
os.mkdir(os.path.join(MODE, 'checkpoints', DATASET))
####################
# Load the dataset #
####################
import psutil
cpu_cores = psutil.cpu_count()
transform = transforms.Compose(
[
transforms.Resize(IMG_DIM),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
],
)
root = '/Users/rahulbhalley/.torch/datasets/' + DATASET
if DATASET == 'cifar-10':
trainset = torchvision.datasets.CIFAR10(root=root, train=True, download=True, transform=transform)
elif DATASET == 'cifar-100':
trainset = torchvision.datasets.CIFAR100(root=root, train=True, download=True, transform=transform)
elif DATASET == 'mnist':
trainset = torchvision.datasets.MNIST(root=root, train=True, download=True, transform=transform)
elif DATASET == 'fashion-mnist':
trainset = torchvision.datasets.FashionMNIST(root=root, train=True, download=True, transform=transform)
else:
trainset = torchvision.datasets.ImageFolder(root=root, transform=transform)
dataloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True, num_workers=cpu_cores)
def get_infinite_data(dataloader):
while True:
for images, _ in dataloader:
yield images
data = get_infinite_data(dataloader)
################################################
# Define neural nets, losses, optimizers, etc. #
################################################
# Automatic GPU/CPU device placement
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Create models
decode_model = Decoder().to(device)
encode_model = Encoder().to(device)
critic_model = Critic().to(device)
# Optimizers
decode_optim = optim.Adam(decode_model.parameters(), lr=2e-4, betas=(0.5, 0.999))
encode_optim = optim.Adam(encode_model.parameters(), lr=2e-4, betas=(0.5, 0.999))
critic_optim = optim.Adam(critic_model.parameters(), lr=2e-4, betas=(0.5, 0.999))
############
# Training #
############
def train():
print('Begin training!')
# Try loading the latest existing checkpoints based on `BEGIN_ITER`
try:
# Checkpoints dirs
decode_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'decode_model_' + str(BEGIN_ITER) + '.pth')
encode_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'encode_model_' + str(BEGIN_ITER) + '.pth')
critic_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'critic_model_' + str(BEGIN_ITER) + '.pth')
# Load checkpoints
decode_model.load_state_dict(torch.load(decode_model_dir, map_location='cpu'))
encode_model.load_state_dict(torch.load(encode_model_dir, map_location='cpu'))
critic_model.load_state_dict(torch.load(critic_model_dir, map_location='cpu'))
print('Loaded the latest checkpoints from {}th iteration.')
print('NOTE: Set the begin iteration in accordance to saved checkpoints.')
# Free some memory
del decode_model_dir, encode_model_dir, critic_model_dir
except:
print("Resume: Couldn't load the checkpoints from {}th iteration.".format(BEGIN_ITER))
# Just to see the learning progress
fixed_z = torch.randn(BATCH_SIZE * 2, Z_DIM, 1, 1).to(device)
for i in range(BEGIN_ITER, TOTAL_ITERS+1):
# Just because I'm encountering some problem with
# the batch size of sampled data with `torchvision`.
def safe_sampling():
x_sample = data.next()
if x_sample.size(0) != BATCH_SIZE:
print('Required batch size not equal to x_sample batch size: {} != {} | skipping...'.format(BATCH_SIZE, x_sample.size(0)))
x_sample = data.next()
return x_sample.to(device)
######################
# Train critic_model #
# Train encode_model #
######################
# Gradient computation shut down:
# - decode_model
for param in critic_model.parameters():
param.requires_grad_(True)
for param in encode_model.parameters():
param.requires_grad_(True)
for param in decode_model.parameters():
param.requires_grad_(False)
for j in range(1):
z_sample = torch.randn(BATCH_SIZE, Z_DIM, 1, 1).to(device) # Sample prior from Gaussian distribution
x_sample = safe_sampling()
with torch.no_grad():
x_fake = decode_model(z_sample)
x_real_encoded = encode_model(x_sample)
x_fake_encoded = encode_model(x_fake)
x_real_fake = x_real_encoded - x_fake_encoded
x_fake_real = x_fake_encoded - x_real_encoded
x_real_fake_score = critic_model(x_real_fake)
x_fake_real_score = critic_model(x_fake_real)
# Compute loss for critic, encoder
# Compute gradients
critic_optim.zero_grad()
encode_optim.zero_grad()
# Decide distance for loss
if MODE == 'wgan':
d_loss = - (x_real_fake_score - x_fake_real_score)
elif MODE == 'sgan':
d_loss = - torch.log(x_real_fake_score) - torch.log(1 - x_fake_real_score)
d_loss = d_loss.mean()
d_loss.backward()
# Update encode_model & critic_model
critic_optim.step()
encode_optim.step()
######################
# Train decode_model #
######################
# Gradient computation shut down:
# - critic_model
# - encode_model
for param in critic_model.parameters():
param.requires_grad_(False)
for param in encode_model.parameters():
param.requires_grad_(False)
for param in decode_model.parameters():
param.requires_grad_(True)
for j in range(2):
z_sample = torch.randn(BATCH_SIZE, Z_DIM, 1, 1).to(device) # Sample prior from Gaussian distribution
x_sample = safe_sampling()
x_fake = decode_model(z_sample)
x_real_encoded = encode_model(x_sample)
x_fake_encoded = encode_model(x_fake)
x_real_fake = x_real_encoded - x_fake_encoded
x_fake_real = x_fake_encoded - x_real_encoded
x_real_fake_score = critic_model(x_real_fake)
x_fake_real_score = critic_model(x_fake_real)
# Compute loss for decoder
# Compute gradients
decode_optim.zero_grad()
# Decide distance for loss
if MODE == 'wgan':
g_loss = - (x_fake_real_score - x_real_fake_score)
elif MODE == 'sgan':
g_loss = - torch.log(1 - x_real_fake_score) - torch.log(x_fake_real_score)
g_loss = g_loss.mean()
g_loss.backward()
# Update decode_model
decode_optim.step()
##################
# Log statistics #
##################
if i % ITERS_PER_LOG == 0:
# Print statistics
print('iter: {}, d_loss: {}, g_loss: {}'.format(i, d_loss, g_loss))
# Save image grids of fake and real images
with torch.no_grad():
#z_sample = torch.randn(BATCH_SIZE * 2, Z_DIM, 1, 1)
samples = decode_model(fixed_z)
samples_dir = os.path.join(MODE, 'samples', DATASET, 'test_{}.png'.format(i))
real_samples_dir = os.path.join(MODE, 'samples', DATASET, 'real.png')
vutils.save_image(samples, samples_dir, normalize=True)
vutils.save_image(x_sample, real_samples_dir, normalize=True)
# Checkpoint directories
decode_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'decode_model_' + str(i) + '.pth')
encode_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'encode_model_' + str(i) + '.pth')
critic_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'critic_model_' + str(i) + '.pth')
# Save all the checkpoints
torch.save(decode_model.state_dict(), decode_model_dir)
torch.save(encode_model.state_dict(), encode_model_dir)
torch.save(critic_model.state_dict(), critic_model_dir)
# Free some memory
del decode_model_dir, encode_model_dir, critic_model_dir
print('Finished training!')
def infer(n=1, epoch=100000):
try:
decode_model_dir = os.path.join(MODE, 'checkpoints', DATASET, 'decode_model_' + str(epoch) + '.pth')
decode_model.load_state_dict(torch.load(decode_model_dir, map_location='cpu'))
except:
print('Could not load checkpoint of `decode_model`.')
for i in range(n):
with torch.no_grad():
z_sample = torch.randn(BATCH_SIZE * 2, Z_DIM, 1, 1)
samples = decode_model(z_sample)
samples_dir = os.path.join(MODE, 'samples', DATASET, 'latest_{}.png'.format(i))
vutils.save_image(samples, samples_dir, normalize=True)
print('Saved image: {}'.format(samples_dir))
# Train the Turing GAN
train()
# Sample for Turing GAN
#infer()