-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain.py
271 lines (227 loc) · 11.3 KB
/
train.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Weather4cast 2022 Starter Kit
#
# Copyright (C) 2022
# Institute of Advanced Research in Artificial Intelligence (IARAI)
# This file is part of the Weather4cast 2022 Starter Kit.
#
# The Weather4cast 2022 Starter Kit is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# The Weather4cast 2022 Starter Kit is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Contributors: Aleksandra Gruca, Pedro Herruzo, David Kreil, Stephen Moran
import argparse
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.plugins import DDPPlugin
from pytorch_lightning.plugins.environments import SLURMEnvironment
from torch.utils.data import DataLoader
from pytorch_lightning import loggers as pl_loggers
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
import datetime
import os
import torch
from models.unet_lightning import UNet_Lightning as UNetModel
from utils.data_utils import load_config
from utils.data_utils import get_cuda_memory_usage
from utils.data_utils import tensor_to_submission_file
from utils.w4c_dataloader import RainData
class DataModule(pl.LightningDataModule):
""" Class to handle training/validation splits in a single object
"""
def __init__(self, params, training_params, mode):
super().__init__()
self.params = params
self.training_params = training_params
if mode in ['train']:
print("Loading TRAINING/VALIDATION dataset -- as test")
self.train_ds = RainData('training', **self.params)
self.val_ds = RainData('validation', **self.params)
if self.params.get('add_val_to_train', False):
print("Adding validation to training")
self.train_ds = torch.utils.data.ConcatDataset([self.train_ds, self.val_ds])
print(f"Training dataset size: {len(self.train_ds)}")
if mode in ['val']:
print("Loading VALIDATION dataset -- as test")
self.val_ds = RainData('validation', **self.params)
if mode in ['predict']:
print("Loading PREDICTION/TEST dataset -- as test")
self.test_ds = RainData('test', **self.params)
if mode in ['heldout']:
print("Loading HELD-OUT dataset -- as test")
self.test_ds = RainData('heldout', **self.params)
def __load_dataloader(self, dataset, shuffle=True, pin=True):
dl = DataLoader(dataset,
batch_size=self.training_params['batch_size'],
num_workers=self.training_params['n_workers'],
shuffle=shuffle,
pin_memory=pin, prefetch_factor=2,
persistent_workers=False)
return dl
def train_dataloader(self):
return self.__load_dataloader(self.train_ds, shuffle=True, pin=True)
def val_dataloader(self):
return self.__load_dataloader(self.val_ds, shuffle=False, pin=True)
def test_dataloader(self):
return self.__load_dataloader(self.test_ds, shuffle=False, pin=True)
def load_model(Model, params, checkpoint_path=''):
""" loads a model from a checkpoint or from scratch if checkpoint_path='' """
p = {**params['experiment'], **params['dataset'], **params['train'], 'predict': params['predict']}
if checkpoint_path == '':
print('-> Modelling from scratch! (no checkpoint loaded)')
model = Model(params['model'], p)
else:
print(f'-> Loading model checkpoint: {checkpoint_path}')
model = Model.load_from_checkpoint(checkpoint_path, UNet_params=params['model'], params=p, strict=True)
return model
def get_trainer(gpus,params):
""" get the trainer, modify here its options:
- save_top_k
"""
max_epochs=params['train']['max_epochs'];
print("Trainig for",max_epochs,"epochs");
checkpoint_callback = ModelCheckpoint(monitor='val_loss_epoch', save_top_k=3, save_last=True,
filename='{epoch:02d}-{val_loss_epoch:.6f}')
parallel_training = None
ddpplugin = None
if gpus[0] == -1:
gpus = None
elif len(gpus) > 1:
parallel_training = 'ddp'
## ddpplugin = DDPPlugin(find_unused_parameters=True)
print(f"====== process started on the following GPUs: {gpus} ======")
date_time = datetime.datetime.now().strftime("%m%d-%H:%M")
version = params['experiment']['name']
version = version + '_' + date_time
#SET LOGGER
if params['experiment']['logging']:
tb_logger = pl_loggers.TensorBoardLogger(save_dir=params['experiment']['experiment_folder'],name=params['experiment']['sub_folder'], version=version, log_graph=True)
else:
tb_logger = False
if params['train']['early_stopping']:
early_stop_callback = EarlyStopping(monitor="val_loss_epoch",
patience=params['train']['patience'],
mode="min")
callback_funcs = [checkpoint_callback, early_stop_callback]
else:
callback_funcs = [checkpoint_callback]
if 'SLURM_JOB_ID' in os.environ and False:
print("SLURM JOB ID detected, using SLURMEnvironment")
plugins = [SLURMEnvironment(auto_requeue=False)]
else:
plugins = None
trainer = pl.Trainer(devices=gpus, max_epochs=max_epochs,
gradient_clip_val=params['model']['gradient_clip_val'],
gradient_clip_algorithm=params['model']['gradient_clip_algorithm'],
accelerator="gpu",
plugins=plugins,
callbacks=callback_funcs,logger=tb_logger,
profiler='simple',precision=params['experiment']['precision'],
strategy="ddp_find_unused_parameters_false", # ddp
)
return trainer
def do_predict(trainer, model, predict_params, test_data):
scores = trainer.predict(model, dataloaders=test_data)
scores = torch.cat(scores)
tensor_to_submission_file(scores,predict_params)
def do_test(trainer, model, test_data):
scores = trainer.test(model, dataloaders=test_data)
def train(params, gpus, mode, checkpoint_path, model=UNetModel):
""" main training/evaluation method
"""
# ------------
# model & data
# ------------
get_cuda_memory_usage(gpus)
data = DataModule(params['dataset'], params['train'], mode)
model = load_model(model, params, checkpoint_path)
# ------------
# Add your models here
# ------------
# ------------
# trainer
# ------------
trainer = get_trainer(gpus, params)
get_cuda_memory_usage(gpus)
# ------------
# train & final validation
# ------------
if mode == 'train':
print("------------------")
print("--- TRAIN MODE ---")
print("------------------")
if params['train'].get('restore_full_training', False) and checkpoint_path:
print(f"-> restoring all training (optim, lr, ...) states from {checkpoint_path}")
else:
checkpoint_path = None
trainer.fit(model, data, ckpt_path=checkpoint_path)
if mode == "val":
# ------------
# VALIDATE
# ------------
print("---------------------")
print("--- VALIDATE MODE ---")
print("---------------------")
do_test(trainer, model, data.val_dataloader())
if mode == 'predict' or mode == 'heldout':
# ------------
# PREDICT
# ------------
print("--------------------")
print("--- PREDICT MODE ---")
print("--------------------")
print("REGIONS!:: ", params["dataset"]["regions"], params["predict"]["region_to_predict"])
if params["predict"]["region_to_predict"] not in params["dataset"]["regions"]:
print("EXITING... \"regions\" and \"regions to predict\" must indicate the same region name in your config file.")
else:
do_predict(trainer, model, params["predict"], data.test_dataloader())
get_cuda_memory_usage(gpus)
def update_params_based_on_args(options):
config_p = os.path.join('models/configurations',options.config_path)
params = load_config(config_p)
if options.name != '':
print(params['experiment']['name'])
params['experiment']['name'] = options.name
return params
def set_parser():
""" set custom parser """
parser = argparse.ArgumentParser(description="")
parser.add_argument("-f", "--config_path", type=str, required=False, default='./configurations/config_basline.yaml',
help="path to config-yaml")
parser.add_argument("-g", "--gpus", type=int, nargs='+', required=False, default=1,
help="specify gpu(s): 1 or 1 5 or 0 1 2 (-1 for no gpu)")
parser.add_argument("-m", "--mode", type=str, required=False, default='train',
help="choose mode: train (default) / val / predict")
parser.add_argument("-c", "--checkpoint", type=str, required=False, default='',
help="init a model from a checkpoint path. '' as default (random weights)")
parser.add_argument("-n", "--name", type=str, required=False, default='',
help="Set the name of the experiment")
return parser
def main():
parser = set_parser()
options = parser.parse_args()
params = update_params_based_on_args(options)
train(params, options.gpus, options.mode, options.checkpoint)
if __name__ == "__main__":
main()
""" examples of usage:
1) train from scratch on one GPU
python train.py --gpus 2 --mode train --config_path config_baseline.yaml --name baseline_train
2) train from scratch on four GPUs
python train.py --gpus 0 1 2 3 --mode train --config_path config_baseline.yaml --name baseline_train
3) fine tune a model from a checkpoint on one GPU
python train.py --gpus 1 --mode train --config_path config_baseline.yaml --checkpoint "lightning_logs/PATH-TO-YOUR-MODEL-LOGS/checkpoints/YOUR-CHECKPOINT-FILENAME.ckpt" --name baseline_tune
4) evaluate a trained model from a checkpoint on two GPUs
python train.py --gpus 0 1 --mode val --config_path config_baseline.yaml --checkpoint "lightning_logs/PATH-TO-YOUR-MODEL-LOGS/checkpoints/YOUR-CHECKPOINT-FILENAME.ckpt" --name baseline_validate
5) generate predictions (plese note that this mode works only for one GPU)
python train.py --gpus 1 --mode predict --config_path config_baseline.yaml --checkpoint "lightning_logs/PATH-TO-YOUR-MODEL-LOGS/checkpoints/YOUR-CHECKPOINT-FILENAME.ckpt"
6) generate predictions for the held-out dataset (plese note that this mode works only for one GPU)
python train.py --gpus 1 --mode heldout --config_path config_baseline.yaml --checkpoint "lightning_logs/PATH-TO-YOUR-MODEL-LOGS/checkpoints/YOUR-CHECKPOINT-FILENAME.ckpt"
"""