-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
128 lines (111 loc) · 3.98 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
"""Baseline train
- Author: Junghoon Kim
- Contact: placidus36@gmail.com
"""
import argparse
import os
from datetime import datetime
from typing import Any, Dict, Tuple, Union
import torch
import torch.nn as nn
import torch.optim as optim
import yaml
from src.dataloader import create_dataloader
from src.loss import CustomCriterion
from src.model import Model
from src.trainer import TorchTrainer
from src.utils.common import get_label_counts, read_yaml
from src.utils.torch_utils import check_runtime, model_info
def train(
model_config: Dict[str, Any],
model_name: str,
data_config: Dict[str, Any],
log_dir: str,
fp16: bool,
device: torch.device,
) -> Tuple[float, float, float]:
"""Train."""
# save model_config, data_config
with open(os.path.join(log_dir, "data.yml"), "w") as f:
yaml.dump(data_config, f, default_flow_style=False)
with open(os.path.join(log_dir, "model.yml"), "w") as f:
yaml.dump(model_config, f, default_flow_style=False)
model_instance = Model(model_config, verbose=True)
model_path = os.path.join(log_dir, "best.pt")
print(f"Model save path: {model_path}")
if os.path.isfile(model_path):
model_instance.model.load_state_dict(torch.load(model_path, map_location=device))
model_instance.model.to(device)
# Create dataloader
train_dl, val_dl, test_dl = create_dataloader(data_config)
# Create optimizer, scheduler, criterion
optimizer = torch.optim.SGD(
model_instance.model.parameters(), lr=data_config["INIT_LR"], momentum=0.9
)
scheduler = torch.optim.lr_scheduler.OneCycleLR(
optimizer=optimizer,
max_lr=data_config["INIT_LR"],
steps_per_epoch=len(train_dl),
epochs=data_config["EPOCHS"],
pct_start=0.05,
)
criterion = CustomCriterion(
samples_per_cls=get_label_counts(data_config["DATA_PATH"])
if data_config["DATASET"] == "TACO"
else None,
device=device,
)
# Amp loss scaler
scaler = torch.cuda.amp.GradScaler() if fp16 and device != torch.device("cpu") else None
# Create trainer
trainer = TorchTrainer(
model=model_instance.model,
model_name=model_name,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
scaler=scaler,
device=device,
model_path=model_path,
verbose=1,
)
best_acc, best_f1 = trainer.train(
train_dataloader=train_dl,
n_epoch=data_config["EPOCHS"],
val_dataloader=val_dl if val_dl else test_dl,
)
# evaluate model with test set
model_instance.model.load_state_dict(torch.load(model_path))
test_loss, test_f1, test_acc = trainer.test(
model=model_instance.model, test_dataloader=val_dl if val_dl else test_dl
)
return test_loss, test_f1, test_acc
if __name__ == "__main__":
# add command line arguments for training
parser = argparse.ArgumentParser(description="Train model.")
parser.add_argument(
"--model",
default="./model/resnet_18.yaml",
type=str,
help="model config",
)
parser.add_argument("--data", default="./data/taco.yaml", type=str, help="data config")
args = parser.parse_args()
# parse the argument and load configurations
model_name = args.model.split("/")[-1].split(".")[0]
model_config = read_yaml(cfg=args.model)
data_config = read_yaml(cfg=args.data)
data_config["DATA_PATH"] = os.environ.get("SM_CHANNEL_TRAIN", data_config["DATA_PATH"])
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# get time stamp
time_stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = os.environ.get("SM_MODEL_DIR", os.path.join("exp", f"{time_stamp}_{model_name}"))
os.makedirs(log_dir, exist_ok=True)
test_loss, test_f1, test_acc = train(
model_config=model_config,
model_name=model_name,
data_config=data_config,
log_dir=log_dir,
fp16=data_config["FP16"],
device=device,
)