-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_autoencoder.py
155 lines (114 loc) · 5.1 KB
/
train_autoencoder.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
import torch
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
from tqdm import tqdm
from colorama import Fore, Style
import torch.nn as nn
import models
from layerbuilder import make_layers
from utils.viewer import UniImageViewer, make_grid
import datasets.package as package
import config
import torch.backends.cudnn
import numpy as np
scale = 4
view_in = UniImageViewer('in', screen_resolution=(128 * 2 * scale, 128 * scale))
view_z = UniImageViewer('z', screen_resolution=(128 // 2 * 5 * scale, 128 // 2 * 4 * scale))
def main(args):
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
def log(phase):
writer.add_scalar(f'{phase}_loss', loss.item(), global_step)
if args.display is not None and i % args.display == 0:
recon = torch.cat((reverse_augment(x[0]), reverse_augment(x_[0])), dim=2)
writer.add_image(f'{phase}_recon', recon, global_step)
if args.display:
view_in.render(recon)
if args.model_type != 'fc':
latent = make_grid(z[0].unsqueeze(1), 4, 4)
writer.add_image(f'{phase}_latent', latent.squeeze(0), global_step)
if args.display:
view_z.render(latent)
def nop(x):
return x
def flatten(x):
return x.flatten(start_dim=1)
def reverse_flatten(x):
return x.reshape(1, 28, 28)
torch.cuda.set_device(args.device)
""" reproducibility """
if args.seed is not None:
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(args.seed)
""" variables """
best_loss = 100.0
run_dir = f'data/models/autoencoders/{args.dataset_name}/{args.model_name}/run_{args.run_id}'
writer = SummaryWriter(log_dir=run_dir)
global_step = 0
""" data """
datapack = package.datasets[args.dataset_name]
train, test = datapack.make(args.dataset_train_len, args.dataset_test_len, data_root=args.dataroot)
train_l = DataLoader(train, batch_size=args.batchsize, shuffle=True, drop_last=True, pin_memory=True)
test_l = DataLoader(test, batch_size=args.batchsize, shuffle=True, drop_last=True, pin_memory=True)
""" model """
#encoder, meta = mnn.make_layers(args.model_encoder, type=args.model_type, meta=LayerMetaData(datapack.shape))
#decoder, meta = mnn.make_layers(args.model_decoder, type=args.model_type, meta=meta)
encoder, shape = make_layers(cfg=args.model_encoder, type=args.model_type, input_shape=datapack.shape)
decoder, shape = make_layers(cfg=args.model_decoder, type=args.model_type, input_shape=shape[-1])
auto_encoder = models.AutoEncoder(encoder, decoder).to(args.device)
print(auto_encoder)
augment = flatten if args.model_type == 'fc' else nop
reverse_augment = reverse_flatten if args.model_type == 'fc' else nop
if args.load is not None:
auto_encoder.load_state_dict(torch.load(args.load))
""" optimizer """
optim, scheduler = config.get_optim(args, auto_encoder.parameters())
""" apex mixed precision """
# if args.device != 'cpu':
# model, optimizer = amp.initialize(auto_encoder, optim, opt_level=args.opt_level)
""" loss function """
criterion = nn.MSELoss()
for epoch in range(1, args.epochs + 1):
""" training """
batch = tqdm(train_l, total=len(train) // args.batchsize)
for i, (x, _) in enumerate(batch):
x = augment(x).to(args.device)
optim.zero_grad()
z, x_ = auto_encoder(x)
loss = criterion(x_, x)
if not args.demo:
loss.backward()
optim.step()
batch.set_description(f'Epoch: {epoch} {args.optim_class} LR: {get_lr(optim)} Train Loss: {loss.item()}')
log('train')
if i % args.checkpoint_freq == 0 and args.demo == 0:
torch.save(auto_encoder.state_dict(), run_dir + '/checkpoint')
global_step += 1
""" test """
with torch.no_grad():
ll = 0.0
batch = tqdm(test_l, total=len(test) // args.batchsize)
for i, (images, _) in enumerate(batch):
x = augment(images).to(args.device)
z, x_ = auto_encoder(x)
loss = criterion(x_, x)
ll += loss.item()
ave_loss = ll / (i + 1)
batch.set_description(f'Epoch: {epoch} Test Loss: {ave_loss}')
log('test')
global_step += 1
""" check improvement """
scheduler.step(ave_loss)
best_loss = ave_loss if ave_loss <= best_loss else best_loss
print(f'{Fore.CYAN}ave loss: {ave_loss} {Fore.LIGHTBLUE_EX}best loss: {best_loss} {Style.RESET_ALL}')
""" save if model improved """
if ave_loss <= best_loss and not args.demo:
torch.save(auto_encoder.state_dict(), run_dir + '/best')
return best_loss
if __name__ == '__main__':
args = config.config()
torch.cuda.set_device(args.device)
main(args)