forked from SashaMalysheva/Pytorch-VAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·70 lines (59 loc) · 2.42 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
#!/usr/bin/env python3
import argparse
import torch
import torchvision
from model import VAE
from data import TRAIN_DATASETS, DATASET_CONFIGS
from train import train_model
parser = argparse.ArgumentParser('VAE PyTorch implementation')
parser.add_argument('--dataset', default='mnist',
choices=list(TRAIN_DATASETS.keys()))
parser.add_argument('--kernel-num', type=int, default=128)
parser.add_argument('--z-size', type=int, default=128)
parser.add_argument('--epochs', type=int, default=10)
parser.add_argument('--batch-size', type=int, default=32)
parser.add_argument('--sample-size', type=int, default=32)
parser.add_argument('--lr', type=float, default=5e-03)
parser.add_argument('--weight-decay', type=float, default=1e-03)
parser.add_argument('--loss-log-interval', type=int, default=100)
parser.add_argument('--image-log-interval', type=int, default=500)
parser.add_argument('--resume', action='store_true')
parser.add_argument('--checkpoint-dir', type=str, default='./checkpoints')
parser.add_argument('--sample-dir', type=str, default='./samples')
parser.add_argument('--no-gpus', action='store_false', dest='cuda')
main_command = parser.add_mutually_exclusive_group(required=True)
main_command.add_argument('--test', action='store_false', dest='train')
main_command.add_argument('--train', action='store_true')
if __name__ == '__main__':
args = parser.parse_args()
cuda = args.cuda and torch.cuda.is_available()
dataset_config = DATASET_CONFIGS[args.dataset]
dataset = TRAIN_DATASETS[args.dataset]
vae = VAE(
label=args.dataset,
image_size=dataset_config['size'],
channel_num=dataset_config['channels'],
kernel_num=args.kernel_num,
z_size=args.z_size,
)
# move the model parameters to the gpu if needed.
if cuda:
vae.cuda()
# run a test or a training process.
if args.train:
train_model(
vae, dataset=dataset,
epochs=args.epochs,
batch_size=args.batch_size,
sample_size=args.sample_size,
lr=args.lr,
weight_decay=args.weight_decay,
checkpoint_dir=args.checkpoint_dir,
loss_log_interval=args.loss_log_interval,
image_log_interval=args.image_log_interval,
resume=args.resume,
cuda=cuda,
)
else:
images = vae.sample(args.sample_size)
torchvision.utils.save_image(images, args.sample_dir)