-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_model.py
77 lines (63 loc) · 2.36 KB
/
train_model.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
import argparse
import os
import time
import torch
from metrics.accuracy import Accuracy
from utils.training_config_parser import TrainingConfigParser
if __name__ == '__main__':
# Define and parse arguments
parser = argparse.ArgumentParser(
description='Training a target classifier')
parser.add_argument('-c',
'--config',
default=None,
type=str,
dest="config",
help='Config .json file path (default: None)')
args = parser.parse_args()
if not args.config:
print(
"Configuration file is missing. Please check the provided path. Execution is stopped."
)
exit()
# Load json config file
config = TrainingConfigParser(args.config.strip())
# Set seeds and make deterministic
seed = config.seed
torch.manual_seed(seed)
# Create the target model architecture
target_model = config.create_model()
if torch.__version__.startswith('2.'):
print('Compiling model with torch.compile')
target_model.model = torch.compile(target_model.model)
# Build the datasets
train_set, valid_set, test_set = config.create_datasets()
criterion = torch.nn.CrossEntropyLoss()
metric = Accuracy
# Set up optimizer and scheduler
optimizer = config.create_optimizer(target_model)
lr_scheduler = config.create_lr_scheduler(optimizer)
# Create and start RTPT object
rtpt = config.create_rtpt()
rtpt.start()
# Modify the save_path such that subfolders with a timestamp and the name of the run are created
time_stamp = time.strftime("%Y%m%d_%H%M%S")
save_path = os.path.join(config.training['save_path'],
f"{config.model['architecture']}_{time_stamp}")
# Start training
target_model.fit(
training_data=train_set,
validation_data=valid_set,
test_data=test_set,
criterion=criterion,
metric=metric,
optimizer=optimizer,
lr_scheduler=lr_scheduler,
rtpt=rtpt,
config=config,
batch_size=config.training['batch_size'],
num_epochs=config.training['num_epochs'],
dataloader_num_workers=config.training['dataloader_num_workers'],
logging=config.wandb['logging'],
save_base_path=save_path,
config_file=args.config)