-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpcnn_train.py
244 lines (211 loc) · 11.3 KB
/
pcnn_train.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
import time
import os
import torch
import torch.optim as optim
from torch.optim import lr_scheduler
from torchvision import datasets, transforms
import wandb
from utils import *
from model import *
from dataset import *
from tqdm import tqdm
from pprint import pprint
import argparse
from pytorch_fid.fid_score import calculate_fid_given_paths
def train_or_test(model, data_loader, optimizer, loss_op, device, args, epoch, mode = 'training'):
if mode == 'training':
model.train()
else:
model.eval()
deno = args.batch_size * np.prod(args.obs) * np.log(2.)
loss_tracker = mean_tracker()
for batch_idx, item in enumerate(tqdm(data_loader)):
model_input, _ = item
model_input = model_input.to(device)
model_output = model(model_input)
loss = loss_op(model_input, model_output)
loss_tracker.update(loss.item()/deno)
if mode == 'training':
optimizer.zero_grad()
loss.backward()
optimizer.step()
if args.en_wandb:
wandb.log({mode + "-Average-BPD" : loss_tracker.get_mean()})
wandb.log({mode + "-epoch": epoch})
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--en_wandb', type=bool, default=False,
help='Enable wandb logging')
parser.add_argument('-t', '--tag', type=str, default='default',
help='Tag for this run')
# sampling
parser.add_argument('-c', '--sampling_interval', type=int, default=5,
help='sampling interval')
# data I/O
parser.add_argument('-i', '--data_dir', type=str,
default='data', help='Location for the dataset')
parser.add_argument('-o', '--save_dir', type=str, default='models',
help='Location for parameter checkpoints and samples')
parser.add_argument('-sd', '--sample_dir', type=str, default='samples',
help='Location for saving samples')
parser.add_argument('-d', '--dataset', type=str,
default='cpen455', help='Can be either cifar|mnist|cpen455')
parser.add_argument('-st', '--save_interval', type=int, default=10,
help='Every how many epochs to write checkpoint/samples?')
parser.add_argument('-r', '--load_params', type=str, default=None,
help='Restore training from previous model checkpoint?')
parser.add_argument('--obs', type=tuple, default=(3, 32, 32),
help='Observation shape')
# model
parser.add_argument('-q', '--nr_resnet', type=int, default=5,
help='Number of residual blocks per stage of the model')
parser.add_argument('-n', '--nr_filters', type=int, default=160,
help='Number of filters to use across the model. Higher = larger model.')
parser.add_argument('-m', '--nr_logistic_mix', type=int, default=10,
help='Number of logistic components in the mixture. Higher = more flexible model')
parser.add_argument('-l', '--lr', type=float,
default=0.0002, help='Base learning rate')
parser.add_argument('-e', '--lr_decay', type=float, default=0.999995,
help='Learning rate decay, applied every step of the optimization')
parser.add_argument('-b', '--batch_size', type=int, default=64,
help='Batch size during training per GPU')
parser.add_argument('-sb', '--sample_batch_size', type=int, default=32,
help='Batch size during sampling per GPU')
parser.add_argument('-x', '--max_epochs', type=int,
default=5000, help='How many epochs to run in total?')
parser.add_argument('-s', '--seed', type=int, default=1,
help='Random seed to use')
args = parser.parse_args()
pprint(args.__dict__)
check_dir_and_create(args.save_dir)
# reproducibility
torch.manual_seed(args.seed)
np.random.seed(args.seed)
model_name = 'pcnn_' + args.dataset + "_"
model_path = args.save_dir + '/'
if args.load_params is not None:
model_name = model_name + 'load_model'
model_path = model_path + model_name + '/'
else:
model_name = model_name + 'from_scratch'
model_path = model_path + model_name + '/'
job_name = "PCNN_Training_" + "dataset:" + args.dataset + "_" + args.tag
if args.en_wandb:
# start a new wandb run to track this script
wandb.init(
# set entity to specify your username or team name
# entity="qihangz-work",
# set the wandb project where this run will be logged
project="CPEN455HW",
# group=Group Name
name=job_name,
)
wandb.config.current_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
wandb.config.update(args)
#set device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
kwargs = {'num_workers':1, 'pin_memory':True, 'drop_last':True}
# set data
if "mnist" in args.dataset:
ds_transforms = transforms.Compose([transforms.Resize((32, 32)), transforms.ToTensor(), rescaling, replicate_color_channel])
train_loader = torch.utils.data.DataLoader(datasets.MNIST(args.data_dir, download=True,
train=True, transform=ds_transforms), batch_size=args.batch_size,
shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(datasets.MNIST(args.data_dir, train=False,
transform=ds_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)
elif "cifar" in args.dataset:
ds_transforms = transforms.Compose([transforms.ToTensor(), rescaling])
if args.dataset == "cifar10":
train_loader = torch.utils.data.DataLoader(datasets.CIFAR10(args.data_dir, train=True,
download=True, transform=ds_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(datasets.CIFAR10(args.data_dir, train=False,
transform=ds_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)
elif args.dataset == "cifar100":
train_loader = torch.utils.data.DataLoader(datasets.CIFAR100(args.data_dir, train=True,
download=True, transform=ds_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(datasets.CIFAR100(args.data_dir, train=False,
transform=ds_transforms), batch_size=args.batch_size, shuffle=True, **kwargs)
else:
raise Exception('{} dataset not in {cifar10, cifar100}'.format(args.dataset))
elif "cpen455" in args.dataset:
ds_transforms = transforms.Compose([transforms.Resize((32, 32)), rescaling])
train_loader = torch.utils.data.DataLoader(CPEN455Dataset(root_dir=args.data_dir,
mode = 'train',
transform=ds_transforms),
batch_size=args.batch_size,
shuffle=True,
**kwargs)
test_loader = torch.utils.data.DataLoader(CPEN455Dataset(root_dir=args.data_dir,
mode = 'test',
transform=ds_transforms),
batch_size=args.batch_size,
shuffle=True,
**kwargs)
val_loader = torch.utils.data.DataLoader(CPEN455Dataset(root_dir=args.data_dir,
mode = 'validation',
transform=ds_transforms),
batch_size=args.batch_size,
shuffle=True,
**kwargs)
else:
raise Exception('{} dataset not in {mnist, cifar, cpen455}'.format(args.dataset))
args.obs = (3, 32, 32)
input_channels = args.obs[0]
loss_op = lambda real, fake : discretized_mix_logistic_loss(real, fake)
sample_op = lambda x : sample_from_discretized_mix_logistic(x, args.nr_logistic_mix)
model = PixelCNN(nr_resnet=args.nr_resnet, nr_filters=args.nr_filters,
input_channels=input_channels, nr_logistic_mix=args.nr_logistic_mix)
model = model.to(device)
if args.load_params:
model.load_state_dict(torch.load(args.load_params))
print('model parameters loaded')
optimizer = optim.Adam(model.parameters(), lr=args.lr)
scheduler = lr_scheduler.StepLR(optimizer, step_size=1, gamma=args.lr_decay)
for epoch in tqdm(range(args.max_epochs)):
train_or_test(model = model,
data_loader = train_loader,
optimizer = optimizer,
loss_op = loss_op,
device = device,
args = args,
epoch = epoch,
mode = 'training')
# decrease learning rate
scheduler.step()
train_or_test(model = model,
data_loader = test_loader,
optimizer = optimizer,
loss_op = loss_op,
device = device,
args = args,
epoch = epoch,
mode = 'test')
train_or_test(model = model,
data_loader = val_loader,
optimizer = optimizer,
loss_op = loss_op,
device = device,
args = args,
epoch = epoch,
mode = 'val')
if epoch % args.sampling_interval == 0:
print('......sampling......')
sample_t = sample(model, args.sample_batch_size, args.obs, sample_op)
sample_t = rescaling_inv(sample_t)
save_images(sample_t, args.sample_dir)
sample_result = wandb.Image(sample_t, caption="epoch {}".format(epoch))
gen_data_dir = args.sample_dir
ref_data_dir = args.data_dir +'/test'
paths = [gen_data_dir, ref_data_dir]
try:
fid_score = calculate_fid_given_paths(paths, 32, device, dims=192)
print("Dimension {:d} works! fid score: {}".format(192, fid_score))
except:
print("Dimension {:d} fails!".format(192))
if args.en_wandb:
wandb.log({"samples": sample_result,
"FID": fid_score})
if (epoch + 1) % args.save_interval == 0:
if not os.path.exists("models"):
os.makedirs("models")
torch.save(model.state_dict(), 'models/{}_{}.pth'.format(model_name, epoch))