-
Notifications
You must be signed in to change notification settings - Fork 1
/
phase_training_100.py
363 lines (280 loc) · 10.9 KB
/
phase_training_100.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import os
from typing import Tuple
import toml
import torch
import xarray as xr
import numpy as np
from datetime import datetime as dt
import torch
from torch.utils.data import DataLoader
from torch.distributed.fsdp import ShardingStrategy
from torch.utils.data.distributed import DistributedSampler
from lightning.pytorch.callbacks import EarlyStopping
from lightning.fabric.loggers import CSVLogger
from lightning.fabric.strategies.fsdp import FSDPStrategy
from lightning.fabric.plugins.environments import MPIEnvironment
from torchmetrics.regression import MeanSquaredError
import Fires
from Fires._datasets.torch_dataset import FireDataset
from Fires._macros.macros import (
CONFIG,
DRIVERS as drivers,
TARGETS as targets,
TRN_YEARS as trn_years,
VAL_YEARS as val_years,
DATA_PATH_100KM,
TORCH_CFG,
DISCORD_CFG,
CHECKPOINTS_DIR,
DATA_DIR,
LOGS_DIR,
NEW_DS_PATH,
RUN_DIR,
SCALER_DIR,
)
from Fires._models.unet import Unet
from Fires._scalers.scaling_maps import StandardMapsPointWise, MinMaxMapsPointWise
from Fires._scalers.standard import StandardScaler
from Fires._scalers.minmax import MinMaxScaler
from Fires._utilities.callbacks import DiscordBenchmark, FabricBenchmark, FabricCheckpoint
from Fires._utilities.cli_args_checker import checker
from Fires._utilities.cli_args_parser import CLIParser
from Fires._utilities.configuration import load_global_config
from Fires._utilities.decorators import debug
from Fires._utilities.logger import Logger as logger
from Fires.trainer import FabricTrainer
# define logger
_log = logger(log_dir=LOGS_DIR).get_logger("Training_on_100km")
@debug(log=_log)
def check_backend() -> str:
"""
Determines the available backend engine for PyTorch computations.
This function checks if the MPS (Metal Performance Shaders) or CUDA backends
are available and sets the appropriate backend accordingly. If neither MPS
nor CUDA is available, it defaults to the CPU backend.
Returns
-------
str
The name of the backend to use for PyTorch computations ('mps', 'cuda', or 'cpu').
"""
# check MPS availability (Use case: code runs on Mac with M-chip and GPUs)
if torch.backends.mps.is_available():
backend:str = 'mps'
# check CUDA availability (Use case: code runs on Supercomputer with GPUs)
elif torch.cuda.is_available():
backend:str = 'cuda'
else: backend:str = 'cpu'
if backend in ['mps', 'cuda']:
# set matricial multiplication
torch.set_float32_matmul_precision(TORCH_CFG.base.matmul_precision)
_log.info(f" | {backend.upper()} available")
return backend
@debug(log=_log)
def get_trainer() -> FabricTrainer:
"""
Creates and configures a FabricTrainer instance for model training.
This function checks the backend, sets up various training parameters
including callbacks, devices, logging, epochs, and more, and then
initializes the FabricTrainer.
Returns
-------
FabricTrainer
An instance of the FabricTrainer class configured with the specified training parameters.
"""
# check backend if MPS, CUDA or CPU
# backend = check_backend()
backend = 'cpu'
# define today's date
today = eval(CONFIG.utils.datetime.today)
_log.info(f" | Today: {today}")
# define csv log name
csv_fname = f'{today}_csv_logs'
_log.info(f" | CSV Filename: {csv_fname}")
# define trainer args
trainer_args = {
# set accelerator type
'accelerator': backend,
# define callbacks
'callbacks':[
DiscordBenchmark(webhook_url=DISCORD_CFG.hooks.webhook_gen, benchmark_csv=os.path.join(RUN_DIR, "fabric_benchmark.csv")),
FabricBenchmark(filename=os.path.join(RUN_DIR, "fabric_benchmark.csv")),
FabricCheckpoint(dst=CHECKPOINTS_DIR),
EarlyStopping('val_loss')
],
# define number of devices (GPUs) that must be used
'devices':TORCH_CFG.trainer.devices,
# define csv logger
'loggers':CSVLogger(root_dir=LOGS_DIR, name=csv_fname),
# define number of epochs
'max_epochs':3, #TORCH_CFG.trainer.epochs,
# define number of nodes used on the cluster
'num_nodes':TORCH_CFG.trainer.num_nodes,
# define trainer accumulation steps
'grad_accum_steps':TORCH_CFG.trainer.accumulation_steps,
# define trainer precision
'precision':TORCH_CFG.trainer.precision,
# define MPI plugin
'plugins':eval(TORCH_CFG.trainer.plugins),
# init distribution strategy
'strategy':eval(TORCH_CFG.model.strategy) if backend in ['mps', 'cuda'] else 'auto',
# set distributed sampler
'use_distributed_sampler':eval(TORCH_CFG.trainer.use_distributed_sampler)
}
_log.info(f" | Trainer arguments: {trainer_args}")
# initialize trainer and its arguments
trainer = FabricTrainer(**trainer_args)
return trainer
@debug(log=_log)
def create_torch_datasets(data_source_path:str) -> Tuple[FireDataset, FireDataset]:
"""
Creates PyTorch datasets for training and validation from the provided data source.
This function checks if the data source path exists, loads the training data,
applies standard scaling, and creates PyTorch datasets for training and validation.
Parameters
----------
data_source_path : str
The path to the data source for the 100km dataset.
Returns
-------
Tuple[FireDataset, FireDataset]
A tuple containing the training and validation datasets as PyTorch FireDataset objects.
Raises
------
ValueError
Check if the data source path exists, if it doesn't exist raise an error.
"""
if not os.path.exists(data_source_path):
raise ValueError(f"Path to 100km dataset doesn't exists: {data_source_path}")
# load training data
data = xr.open_zarr(data_source_path)[drivers+targets]
train_data = data.sel(time=data.time.dt.year.isin(trn_years)).load()
# create standard scaler
mean_std_args = dict(dim=['time','latitude', 'longitude'], skipna=True)
mean_ds = train_data.mean(**mean_std_args)
stdv_ds = train_data.std(**mean_std_args)
x_scaler = StandardScaler(mean_ds=mean_ds, stdv_ds=stdv_ds, features=drivers)
# define pytorch datasets for training and validation
fire_ds_args = dict(src=data_source_path, drivers=drivers, targets=targets)
trn_torch_ds = FireDataset(**fire_ds_args, years=trn_years, scalers=[x_scaler, None])
val_torch_ds = FireDataset(**fire_ds_args, years=val_years, scalers=[x_scaler, None])
return trn_torch_ds, val_torch_ds
@debug(log=_log)
def get_model() -> Unet:
"""
Initializes and configures the UNet model for training.
This function sets up the model configuration, including input shape,
number of classes, depth, and activation function, and then creates an
instance of the UNet model. It also sets the loss function and initializes
the metrics for the model.
Returns
-------
Unet
An instance of the Unet class configured for training.
"""
# define model configuration
# model_config = {
# 'input_shape':(180, 360, 7),
# 'base_filter_dim':32,
# 'activation':torch.nn.modules.activation.Sigmoid()
# }
# define model loss
model.loss = torch.nn.modules.loss.BCELoss()
# define model metrics
model.metrics = []
_log.info(f" | Model: \n\n {model}")
return model
@debug(log=_log)
def main():
"""
Main function to execute the model training pipeline.
This function orchestrates the entire training process by creating datasets,
initializing the trainer and model, setting up data loaders, and starting the
training process. It also logs the training progress and saves the final model
to disk.
"""
# create pytorch datasets for training and validation
trn_torch_ds, val_torch_ds = create_torch_datasets(data_source_path=DATA_PATH_100KM)
# define trainer
trainer = get_trainer()
# define model
model = get_model()
# load dataloader
dloader_args = dict(batch_size=TORCH_CFG.trainer.batch_size, shuffle=True, drop_last=TORCH_CFG.trainer.drop_reminder)
train_loader = DataLoader(trn_torch_ds, **dloader_args)
valid_loader = DataLoader(val_torch_ds, **dloader_args)
for batch in train_loader:
_, y = batch
print(y)
_log.info(f" | Min: {torch.min(y)} Max: {torch.max(y)}")
break # Stampa solo il primo batch
# setup the model and the optimizer
trainer.setup(
model=model,
optimizer_cls=eval(TORCH_CFG.trainer.optim.cls),
optimizer_args=eval(TORCH_CFG.trainer.optim.args),
scheduler_cls=eval(TORCH_CFG.trainer.scheduler.cls),
scheduler_args=eval(TORCH_CFG.trainer.scheduler.args),
checkpoint=eval(TORCH_CFG.trainer.checkpoint.ckpt)
)
# fit the model
trainer.fit(train_loader=train_loader, val_loader=valid_loader)
# save the model to disk
last_model = os.path.join(RUN_DIR,'last_model.pt')
trainer.fabric.save(
path=last_model,
state={
'model':trainer.model,
'optimizer':trainer.optimizer,
'scheduler': trainer.scheduler_cfg
}
)
@debug(log=_log)
def check_unet_args():
"""
Parses and validates command-line arguments for configuring a UNet model for training.
This function displays the program name and description, then parses command-line
arguments related to the UNet model's configuration, such as the base filter dimension
and the activation function for the last layer. It ensures that all required arguments
are provided and sets default values if necessary. Based on the parsed arguments, it
constructs and returns a dictionary containing the model configuration.
Returns
-------
dict
A dictionary containing the UNet model configuration with the following keys:
- 'input_shape': The shape of the input data (fixed as (180, 360, 7)).
- 'base_filter_dim': The base filter dimension for the UNet model, as specified by the user.
- 'activation': The activation function for the last layer, either Sigmoid or ReLU,
based on the user's choice.
"""
PROGRAM_NAME = ""
PROGRAM_DESCRIPTION = "The following script is designed to perform the training of a ML model that must predict Wildfires Burned Areas on global scale."
options = [
[('-bfd', '--base_filter_dim'), dict(type=int, default=32, help='Base filter dimension for Unet (default: 32)')],
[('-afn', '--activation'), dict(type=str, choices=['S', 'R'], default='S', help='Activation function for the last layer: S - Sigmoid (default) | R - ReLU')],
[('-mdl', '--model'), dict(type=str, choices=['unet', 'unetpp'], default='unet', help='Name of the model that must be trained: unet (default) | unetpp - UNet++')],
]
cli_parser = CLIParser(program_name=PROGRAM_NAME, description=PROGRAM_DESCRIPTION)
cli_parser.add_arguments(parser=None, options=options)
cli_args = cli_parser.parse_args()
activation_fn = torch.nn.Sigmoid() if cli_args.activation == 'S' else torch.nn.ReLU()
model_config = {
'input_shape':(180, 360, 7),
'base_filter_dim':cli_args.base_filter_dim,
'activation':activation_fn
}
if cli_args.model == 'unet':
model_class = Fires._models.unet.Unet
elif cli_args.model == 'unetpp':
model_class = Fires._models.unetpp.UnetPlusPlus
else:
raise ValueError(f"Model not supported: {cli_args.model}")
return model_class, model_config
if __name__ == '__main__':
model_class, model_config = check_unet_args()
for k in model_config.keys():
print(f"{k}: {model_config[k]}")
print("\n\n")
global model
model = model_class(**model_config)
print(f"Model: {model}")
main()