-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_coop.py
149 lines (123 loc) · 4.92 KB
/
train_coop.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
import os
import yaml
import sys
import torch
import argparse
from loguru import logger
from IPython import embed
from pytorch_lightning import Trainer, seed_everything
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import CSVLogger
from src.datamodule import datamodule
from src.models import models
def get_next_version(path_results,name):
version = 0
while os.path.exists(os.path.join(path_results, name, 'version_{}'.format(version))):
version += 1
return version
def get_current_version(path_results,name):
version = 0
while os.path.exists(os.path.join(path_results, name, 'version_{}'.format(version))):
version += 1
return version - 1
def run(params):
logger.info('Running the model')
# Initialize seeding
if params['seed'][0]:
seed_everything(params['seed'][1], workers=True)
which = params['which']
which_data = params['which_data']
# Initialize the paths
#path_root = '/develop'
#params['paths']['path_root'] = path_root
path_root = params['paths']['path_root']
path_results = params['paths']['path_results']
path_data = params['paths']['path_data']
path_checkpoints = params['paths']['path_checkpoints']
path_lens_phase = params['paths']['path_lens_phase']
path_results = os.path.join(path_root, path_results)
path_data = os.path.join(path_root, path_data)
name = 'coop_{}_{}'.format(which, which_data)
if params['resume_training']:
version = get_current_version(path_results, name)
version = 0
else:
version = get_next_version(path_results, name)
# Initialize the directory for saving lens phase
params['paths']['path_lens_phase'] = os.path.join(path_results, name, f'version_{version}', path_lens_phase)
lens_phase_dir = os.path.join(params['paths']['path_lens_phase'])
os.makedirs(lens_phase_dir, exist_ok=True)
# Initialize the CSV logger
save_dir = os.path.join(path_results, name)
print(save_dir)
csv_logger = CSVLogger(save_dir = save_dir, version = 'logs', name = 'version_{}'.format(version))
# Initialize the model checkpoint
checkpoint_dir = os.path.join(path_results, name, f'version_{version}', path_checkpoints)
os.makedirs(checkpoint_dir, exist_ok=True)
model_checkpoint = ModelCheckpoint(
dirpath = checkpoint_dir,
filename = 'model-{epoch:02d}',
save_last = True,
verbose = False,
save_on_train_epoch_end = True,
monitor = 'loss_train',
save_top_k = 2,
)
model = models.select_new_model(params)
# Initialize the datamodel
dm = datamodule.select_data(params)
# Get the training params
gpu_list = params['gpu_config'][1]
num_epochs = params['num_epochs']
# Dump the config
yaml.dump(params, open(os.path.join(path_root, path_results,name,f'version_{version}', 'config.yaml'), 'w'))
if params['resume_training']:
resume_from_checkpoint = os.path.join(checkpoint_dir, 'last.ckpt')
else:
resume_from_checkpoint = None
# Initialize the PyTorch Lightning Trainer
if params['gpu_config'][0] and torch.cuda.is_available():
logger.info('Using GPUs')
trainer = Trainer(
accelerator = 'cuda',
num_nodes = 1,
devices = gpu_list,
max_epochs = num_epochs,
deterministic = True,
enable_progress_bar=True,
enable_model_summary=True,
log_every_n_steps = 1,
default_root_dir = path_results,
num_sanity_val_steps = 1,
check_val_every_n_epoch = 1,
callbacks = [model_checkpoint],
logger = csv_logger,
)
else:
logger.info('Using CPU')
trainer = Trainer(
accelerator = "cpu",
max_epochs = num_epochs,
deterministic = True,
enable_progress_bar=True,
enable_model_summary=True,
log_every_n_steps = 1,
default_root_dir = path_results,
num_sanity_val_steps = 1,
check_val_every_n_epoch = 1,
callbacks = [model_checkpoint],
logger = csv_logger,
)
# Fit the model
trainer.fit(model, dm, ckpt_path=resume_from_checkpoint)
if __name__ == "__main__":
# Load the parameters
params = yaml.load(open('config_coop.yaml', 'r'), Loader=yaml.FullLoader)
params['paths']['path_root'] = os.getcwd()
argparser = argparse.ArgumentParser()
argparser.add_argument("--which_data", type=str, default='resampled_sample', help="Which element of the data to use")
args = argparser.parse_args()
params['which_data'] = args.which_data
print("Running with the following parameters:")
print(f"which_data: {params['which_data']}")
run(params)