-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_vae.py
176 lines (138 loc) · 6.32 KB
/
train_vae.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
import pathlib
import argparse
import json
from pprint import pprint
from tqdm import tqdm
import torch
from torch import optim
from torch.utils.data import DataLoader
try:
from torch.utils.tensorboard import SummaryWriter
except ImportError:
from tensorboardX import SummaryWriter
from torchvision import datasets, transforms
from models.vae import Model, Criterion, ModelV2
from utils import MeterLogger, ImageLogger, set_random_seed
def main(args):
writer = SummaryWriter(args.experiment_log_path)
writer.add_hparams(vars(args), {})
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
transform = transforms.Compose([
transforms.Resize((32, 32), 3),
transforms.ToTensor(),
])
if args.dataset == 'cifar10':
train_dataset = datasets.CIFAR10('data', train=True, download=True, transform=transform)
test_dataset = datasets.CIFAR10('data', train=False, download=True, transform=transform)
args.in_channels = 3
elif args.dataset == 'mnist':
train_dataset = datasets.MNIST('data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST('data', train=False, download=True, transform=transform)
args.in_channels = 1
else:
raise ValueError(f"Invalid dataset: {args.dataset}")
train_dataloader = DataLoader(train_dataset, args.batch_size,
shuffle=True, pin_memory=True, num_workers=4)
test_dataloader = DataLoader(test_dataset, args.batch_size // 4,
pin_memory=True, num_workers=4)
if args.model_type == 'basic':
model = Model(
args.in_channels,
args.hidden_channels,
args.latent_dim,
args.num_latents
)
elif args.model_type == 'resnet':
model = ModelV2(
args.in_channels,
args.hidden_channels,
args.latent_dim
)
else:
raise ValueError(f"Invalid type model: {args.model_type}")
model = model.to(device)
criterion = Criterion(beta=args.beta, likelihood=args.likelihood_type)
optimizer = optim.Adam(model.parameters(), args.lr)
# Initialize Loggers
train_metric_logger = MeterLogger(("total_loss", "reconstruction_loss", "kl"), writer)
val_metric_logger = MeterLogger(("total_loss", "reconstruction_loss", "kl"), writer)
image_logger = ImageLogger(writer)
print(model)
for epoch in tqdm(range(args.num_epoch)):
train_metric_logger.reset()
model.train()
for train_batch in tqdm(train_dataloader):
images, labels = train_batch
images = images.to(device)
reconstruction, proposal_distribution = model(images)
total_loss, reconstruction_loss, kl = \
criterion(images, proposal_distribution, reconstruction)
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
train_metric_logger.update('total_loss', total_loss.item(), train_dataloader.batch_size)
train_metric_logger.update('reconstruction_loss', reconstruction_loss.item(), train_dataloader.batch_size)
train_metric_logger.update('kl', kl.item(), train_dataloader.batch_size)
# Save train metrics
train_metric_logger.write(epoch, 'train')
image_logger.write(images, reconstruction, epoch, 'train')
val_metric_logger.reset()
model.eval()
for test_batch in tqdm(test_dataloader):
images, labels = test_batch
images = images.to(device)
with torch.no_grad():
reconstruction, proposal_distribution = model(images)
total_loss, reconstruction_loss, kl = \
criterion(images, proposal_distribution, reconstruction)
val_metric_logger.update('total_loss', total_loss.item(), test_dataloader.batch_size)
val_metric_logger.update('reconstruction_loss', reconstruction_loss.item(), test_dataloader.batch_size)
val_metric_logger.update('kl', kl.item(), test_dataloader.batch_size)
# Save val metrics
val_metric_logger.write(epoch, 'val')
image_logger.write(images, reconstruction, epoch, 'val')
# Generate
if args.model_type == 'basic':
generated = model.generate(16, device).cpu()
else:
generated = model.generate(16, 8, device).cpu()
writer.add_images('generated', generated, epoch)
# Save checkpoint
checkpoint_path = pathlib.Path(experiment_model_path) / f"{epoch}.pth"
torch.save(model.state_dict(), checkpoint_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Training of VQVAE')
# Common
parser.add_argument('--dataset', type=str, default='cifar10')
parser.add_argument('--experiment-name', type=str)
parser.add_argument('--use-cuda', action='store_true')
parser.add_argument('--seed', type=int, default=987)
# Optimization
parser.add_argument('--batch-size', type=int, default=128)
parser.add_argument('--num-epoch', type=int, default=100)
parser.add_argument('--lr', type=float, default=3e-4)
# Model
parser.add_argument('--model-type', type=str, default='basic')
parser.add_argument('--likelihood-type', type=str, default='bernoulli')
parser.add_argument('--hidden-channels', type=int, default=256)
parser.add_argument('--latent-dim', type=int, default=10)
parser.add_argument('--num-latents', type=int, default=10)
parser.add_argument('--beta', type=float, default=1.0)
args = parser.parse_args()
set_random_seed(args.seed)
experiment_root = pathlib.Path('experiments') / args.experiment_name
args.experiment_root = str(experiment_root)
if not experiment_root.exists():
experiment_root.mkdir()
with open(experiment_root / 'config.json', 'w') as f:
json.dump(vars(args), f, indent=4, sort_keys=True)
experiment_log_path = experiment_root / 'logs'
args.experiment_log_path = str(experiment_log_path)
if not experiment_log_path.exists():
experiment_log_path.mkdir()
experiment_model_path = experiment_root / 'models'
args.experiment_model_path = str(experiment_model_path)
if not experiment_model_path.exists():
experiment_model_path.mkdir()
pprint(vars(args))
main(args)