forked from michaal94/torch_DCEC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torch_DCEC.py
383 lines (325 loc) · 15 KB
/
torch_DCEC.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from __future__ import print_function, division
if __name__ == "__main__":
import argparse
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torchvision import datasets, models, transforms
import os
import math
import fnmatch
import nets
import utils
import training_functions
from torch.utils.tensorboard import SummaryWriter
# Translate string entries to bool for parser
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
parser = argparse.ArgumentParser(description='Use DCEC for clustering')
parser.add_argument('--mode', default='train_full', choices=['train_full', 'pretrain'], help='mode')
parser.add_argument('--tensorboard', default=True, type=bool, help='export training stats to tensorboard')
parser.add_argument('--pretrain', default=True, type=str2bool, help='perform autoencoder pretraining')
parser.add_argument('--pretrained_net', default=1, help='index or path of pretrained net')
parser.add_argument('--net_architecture', default='CAE_3', choices=['CAE_3', 'CAE_bn3', 'CAE_4', 'CAE_bn4', 'CAE_5', 'CAE_bn5'], help='network architecture used')
parser.add_argument('--dataset', default='MNIST-train',
choices=['MNIST-train', 'custom', 'MNIST-test', 'MNIST-full'],
help='custom or prepared dataset')
parser.add_argument('--dataset_path', default='data', help='path to dataset')
parser.add_argument('--batch_size', default=256, type=int, help='batch size')
parser.add_argument('--rate', default=0.001, type=float, help='learning rate for clustering')
parser.add_argument('--rate_pretrain', default=0.001, type=float, help='learning rate for pretraining')
parser.add_argument('--weight', default=0.0, type=float, help='weight decay for clustering')
parser.add_argument('--weight_pretrain', default=0.0, type=float, help='weight decay for clustering')
parser.add_argument('--sched_step', default=200, type=int, help='scheduler steps for rate update')
parser.add_argument('--sched_step_pretrain', default=200, type=int,
help='scheduler steps for rate update - pretrain')
parser.add_argument('--sched_gamma', default=0.1, type=float, help='scheduler gamma for rate update')
parser.add_argument('--sched_gamma_pretrain', default=0.1, type=float,
help='scheduler gamma for rate update - pretrain')
parser.add_argument('--epochs', default=1000, type=int, help='clustering epochs')
parser.add_argument('--epochs_pretrain', default=300, type=int, help='pretraining epochs')
parser.add_argument('--printing_frequency', default=10, type=int, help='training stats printing frequency')
parser.add_argument('--gamma', default=0.1, type=float, help='clustering loss weight')
parser.add_argument('--update_interval', default=80, type=int, help='update interval for target distribution')
parser.add_argument('--tol', default=1e-2, type=float, help='stop criterium tolerance')
parser.add_argument('--num_clusters', default=10, type=int, help='number of clusters')
parser.add_argument('--custom_img_size', default=[128, 128, 3], nargs=3, type=int, help='size of custom images')
parser.add_argument('--leaky', default=True, type=str2bool)
parser.add_argument('--neg_slope', default=0.01, type=float)
parser.add_argument('--activations', default=False, type=str2bool)
parser.add_argument('--bias', default=True, type=str2bool)
args = parser.parse_args()
print(args)
if args.mode == 'pretrain' and not args.pretrain:
print("Nothing to do :(")
exit()
board = args.tensorboard
# Deal with pretraining option and way of showing network path
pretrain = args.pretrain
net_is_path = True
if not pretrain:
try:
int(args.pretrained_net)
idx = args.pretrained_net
net_is_path = False
except:
pass
params = {'pretrain': pretrain}
# Directories
# Create directories structure
dirs = ['runs', 'reports', 'nets']
list(map(lambda x: os.makedirs(x, exist_ok=True), dirs))
# Net architecture
model_name = args.net_architecture
# Indexing (for automated reports saving) - allows to run many trainings and get all the reports collected
if pretrain or (not pretrain and net_is_path):
reports_list = sorted(os.listdir('reports'), reverse=True)
if reports_list:
for file in reports_list:
# print(file)
if fnmatch.fnmatch(file, model_name + '*'):
idx = int(str(file)[-7:-4]) + 1
break
try:
idx
except NameError:
idx = 1
# Base filename
name = model_name + '_' + str(idx).zfill(3)
# Filenames for report and weights
name_txt = name + '.txt'
name_net = name
pretrained = name + '_pretrained.pt'
# Arrange filenames for report, network weights, pretrained network weights
name_txt = os.path.join('reports', name_txt)
name_net = os.path.join('nets', name_net)
if net_is_path and not pretrain:
pretrained = args.pretrained_net
else:
pretrained = os.path.join('nets', pretrained)
if not pretrain and not os.path.isfile(pretrained):
print("No pretrained weights, try again choosing pretrained network or create new with pretrain=True")
model_files = [name_net, pretrained]
params['model_files'] = model_files
# Open file
if pretrain:
f = open(name_txt, 'w')
else:
f = open(name_txt, 'a')
params['txt_file'] = f
# Delete tensorboard entry if exist (not to overlap as the charts become unreadable)
try:
os.system("rm -rf runs/" + name)
except:
pass
# Initialize tensorboard writer
if board:
writer = SummaryWriter('runs/' + name)
params['writer'] = writer
else:
params['writer'] = None
# Hyperparameters
# Used dataset
dataset = args.dataset
# Batch size
batch = args.batch_size
params['batch'] = batch
# Number of workers (typically 4*num_of_GPUs)
workers = 4
# Learning rate
rate = args.rate
rate_pretrain = args.rate_pretrain
# Adam params
# Weight decay
weight = args.weight
weight_pretrain = args.weight_pretrain
# Scheduler steps for rate update
sched_step = args.sched_step
sched_step_pretrain = args.sched_step_pretrain
# Scheduler gamma - multiplier for learning rate
sched_gamma = args.sched_gamma
sched_gamma_pretrain = args.sched_gamma_pretrain
# Number of epochs
epochs = args.epochs
pretrain_epochs = args.epochs_pretrain
params['pretrain_epochs'] = pretrain_epochs
# Printing frequency
print_freq = args.printing_frequency
params['print_freq'] = print_freq
# Clustering loss weight:
gamma = args.gamma
params['gamma'] = gamma
# Update interval for target distribution:
update_interval = args.update_interval
params['update_interval'] = update_interval
# Tolerance for label changes:
tol = args.tol
params['tol'] = tol
# Number of clusters
num_clusters = args.num_clusters
# Report for settings
tmp = "Training the '" + model_name + "' architecture"
utils.print_both(f, tmp)
tmp = "\n" + "The following parameters are used:"
utils.print_both(f, tmp)
tmp = "Batch size:\t" + str(batch)
utils.print_both(f, tmp)
tmp = "Number of workers:\t" + str(workers)
utils.print_both(f, tmp)
tmp = "Learning rate:\t" + str(rate)
utils.print_both(f, tmp)
tmp = "Pretraining learning rate:\t" + str(rate_pretrain)
utils.print_both(f, tmp)
tmp = "Weight decay:\t" + str(weight)
utils.print_both(f, tmp)
tmp = "Pretraining weight decay:\t" + str(weight_pretrain)
utils.print_both(f, tmp)
tmp = "Scheduler steps:\t" + str(sched_step)
utils.print_both(f, tmp)
tmp = "Scheduler gamma:\t" + str(sched_gamma)
utils.print_both(f, tmp)
tmp = "Pretraining scheduler steps:\t" + str(sched_step_pretrain)
utils.print_both(f, tmp)
tmp = "Pretraining scheduler gamma:\t" + str(sched_gamma_pretrain)
utils.print_both(f, tmp)
tmp = "Number of epochs of training:\t" + str(epochs)
utils.print_both(f, tmp)
tmp = "Number of epochs of pretraining:\t" + str(pretrain_epochs)
utils.print_both(f, tmp)
tmp = "Clustering loss weight:\t" + str(gamma)
utils.print_both(f, tmp)
tmp = "Update interval for target distribution:\t" + str(update_interval)
utils.print_both(f, tmp)
tmp = "Stop criterium tolerance:\t" + str(tol)
utils.print_both(f, tmp)
tmp = "Number of clusters:\t" + str(num_clusters)
utils.print_both(f, tmp)
tmp = "Leaky relu:\t" + str(args.leaky)
utils.print_both(f, tmp)
tmp = "Leaky slope:\t" + str(args.neg_slope)
utils.print_both(f, tmp)
tmp = "Activations:\t" + str(args.activations)
utils.print_both(f, tmp)
tmp = "Bias:\t" + str(args.bias)
utils.print_both(f, tmp)
# Data preparation
if dataset == 'MNIST-train':
# Uses slightly modified torchvision MNIST class
import mnist
tmp = "\nData preparation\nReading data from: MNIST train dataset"
utils.print_both(f, tmp)
img_size = [28, 28, 1]
tmp = "Image size used:\t{0}x{1}".format(img_size[0], img_size[1])
utils.print_both(f, tmp)
dataset = mnist.MNIST('../data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.1307,), (0.3081,))
]))
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=batch, shuffle=False, num_workers=workers)
dataset_size = len(dataset)
tmp = "Training set size:\t" + str(dataset_size)
utils.print_both(f, tmp)
elif dataset == 'MNIST-test':
import mnist
tmp = "\nData preparation\nReading data from: MNIST test dataset"
utils.print_both(f, tmp)
img_size = [28, 28, 1]
tmp = "Image size used:\t{0}x{1}".format(img_size[0], img_size[1])
utils.print_both(f, tmp)
dataset = mnist.MNIST('../data', train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.1307,), (0.3081,))
]))
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=batch, shuffle=False, num_workers=workers)
dataset_size = len(dataset)
tmp = "Training set size:\t" + str(dataset_size)
utils.print_both(f, tmp)
elif dataset == 'MNIST-full':
import mnist
tmp = "\nData preparation\nReading data from: MNIST full dataset"
utils.print_both(f, tmp)
img_size = [28, 28, 1]
tmp = "Image size used:\t{0}x{1}".format(img_size[0], img_size[1])
utils.print_both(f, tmp)
dataset = mnist.MNIST('../data', full=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
# transforms.Normalize((0.1307,), (0.3081,))
]))
dataloader = torch.utils.data.DataLoader(dataset,
batch_size=batch, shuffle=False, num_workers=workers)
dataset_size = len(dataset)
tmp = "Training set size:\t" + str(dataset_size)
utils.print_both(f, tmp)
else:
# Data folder
data_dir = args.dataset_path
tmp = "\nData preparation\nReading data from:\t./" + data_dir
utils.print_both(f, tmp)
# Image size
custom_size = math.nan
custom_size = args.custom_img_size
if isinstance(custom_size, list):
img_size = custom_size
tmp = "Image size used:\t{0}x{1}".format(img_size[0], img_size[1])
utils.print_both(f, tmp)
# Transformations
data_transforms = transforms.Compose([
transforms.Resize(img_size[0:2]),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# Read data from selected folder and apply transformations
image_dataset = datasets.ImageFolder(data_dir, data_transforms)
# Prepare data for network: schuffle and arrange batches
dataloader = torch.utils.data.DataLoader(image_dataset, batch_size=batch,
shuffle=False, num_workers=workers)
# Size of data sets
dataset_size = len(image_dataset)
tmp = "Training set size:\t" + str(dataset_size)
utils.print_both(f, tmp)
params['dataset_size'] = dataset_size
# GPU check
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
tmp = "\nPerforming calculations on:\t" + str(device)
utils.print_both(f, tmp + '\n')
params['device'] = device
# Evaluate the proper model
to_eval = "nets." + model_name + "(img_size, num_clusters=num_clusters, leaky = args.leaky, neg_slope = args.neg_slope)"
model = eval(to_eval)
# Tensorboard model representation
# if board:
# writer.add_graph(model, torch.autograd.Variable(torch.Tensor(batch, img_size[2], img_size[0], img_size[1])))
model = model.to(device)
# Reconstruction loss
criterion_1 = nn.MSELoss(size_average=True)
# Clustering loss
criterion_2 = nn.KLDivLoss(size_average=False)
criteria = [criterion_1, criterion_2]
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=rate, weight_decay=weight)
optimizer_pretrain = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=rate_pretrain, weight_decay=weight_pretrain)
optimizers = [optimizer, optimizer_pretrain]
scheduler = lr_scheduler.StepLR(optimizer, step_size=sched_step, gamma=sched_gamma)
scheduler_pretrain = lr_scheduler.StepLR(optimizer_pretrain, step_size=sched_step_pretrain, gamma=sched_gamma_pretrain)
schedulers = [scheduler, scheduler_pretrain]
if args.mode == 'train_full':
model = training_functions.train_model(model, dataloader, criteria, optimizers, schedulers, epochs, params)
elif args.mode == 'pretrain':
model = training_functions.pretraining(model, dataloader, criteria[0], optimizers[1], schedulers[1], epochs, params)
# Save final model
torch.save(model.state_dict(), name_net + '.pt')
# Close files
f.close()
if board:
writer.close()