-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
executable file
·96 lines (78 loc) · 3.29 KB
/
run.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
import os
import yaml
import argparse
import numpy as np
from pathlib import Path
from models import *
from experiment import VAEXperiment
import torch.backends.cudnn as cudnn
from pytorch_lightning import Trainer
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning import seed_everything
# from pytorch_lightning.utilities.seed import seed_everything
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
#from dataset import VAEDataset
from f3_dataloader_lightning import VAEDataset
from pytorch_lightning.strategies import DDPStrategy
# from pytorch_lightning.plugins import DDPPlugin
parser = argparse.ArgumentParser(description='Generic runner for VAE models')
parser.add_argument('--config', '-c',
dest="filename",
metavar='FILE',
help = 'path to the config file',
default='configs/cvae.yaml')
args = parser.parse_args()
with open(args.filename, 'r') as file:
try:
config = yaml.safe_load(file)
except yaml.YAMLError as exc:
print(exc)
tb_logger = TensorBoardLogger(save_dir=config['logging_params']['save_dir'],
name=config['model_params']['name'],)
# For reproducibility
seed_everything(config['exp_params']['manual_seed'], True)
model = vae_models[config['model_params']['name']](**config['model_params'])
experiment = VAEXperiment(model,
config['exp_params'])
data = VAEDataset(**config["data_params"], pin_memory=len(config['trainer_params']['gpus']) != 0)
data.setup()
tb_logger = TensorBoardLogger("./logs/",
name="ConditionalVAE",)
runner = Trainer(logger=tb_logger,
callbacks=[
LearningRateMonitor(),
ModelCheckpoint(save_top_k=2,
dirpath =os.path.join(tb_logger.log_dir , "checkpoints"),
monitor= "val_loss",
save_last= True),
],
strategy=DDPStrategy(find_unused_parameters=False),
accelerator="gpu", max_epochs = config["trainer_params"]["max_epochs"] )
Path(f"{tb_logger.log_dir}/Samples").mkdir(exist_ok=True, parents=True)
Path(f"{tb_logger.log_dir}/Test_Input").mkdir(exist_ok=True, parents=True)
Path(f"{tb_logger.log_dir}/Test_Label").mkdir(exist_ok=True, parents=True)
Path(f"{tb_logger.log_dir}/Reconstructions").mkdir(exist_ok=True, parents=True)
Path(f"{tb_logger.log_dir}/mean_image").mkdir(exist_ok=True, parents=True)
Path(f"{tb_logger.log_dir}/std_image").mkdir(exist_ok=True, parents=True)
print(f"======= Training {config['model_params']['name']} =======")
runner.fit(experiment, datamodule=data)
# !!!!! Check Later !!!!!
# For reproducibility
# seed_everything(1265, True)
# model = vae_models['ConditionalVAE']( in_channels=1,
# num_classes= 10,
# latent_dim= 128,
# img_size=28)
# experiment = VAEXperiment(model,
# params={
# "LR": 0.005,
# "weight_decay": 0.0,
# # "scheduler_gamma": 0.95,
# "kld_weight": 0.00025,
# "manual_seed": 1265
# })
# data = VAEDataset( data_path= 'E:/celeba/',
# train_batch_size= 64,
# val_batch_size= 64,
# patch_size= 64,
# num_workers= 4, pin_memory=True)