-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
180 lines (153 loc) · 5.41 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
"""Baseline train
- Author: Junghoon Kim
- Contact: [email protected]
"""
import argparse
import os
from datetime import datetime
from typing import Any, Dict, Tuple, Union
import numpy as np
import random
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
import wandb
import pprint
def seed_everything(seed):
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # 멀티 GPU 사용 시
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
os.environ["PYTHONHASHSEED"] = str(seed)
def train(
model_config: Dict[str, Any],
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,
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__":
parser = argparse.ArgumentParser(description="Train model.")
parser.add_argument(
"--model",
default="configs/model/mobilenetv3.yaml",
type=str,
help="model config",
)
parser.add_argument(
"--data", default="configs/data/taco.yaml", type=str, help="data config"
)
parser.add_argument(
"--dir", default="exp", type=str, help="data config"
)
parser.add_argument(
"--wnb_prep", default="#", type=str, help="data config"
)
args = parser.parse_args()
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")
log_dir = os.environ.get("SM_MODEL_DIR", os.path.join(args.dir, 'latest'))
if os.path.exists(log_dir):
modified = datetime.fromtimestamp(os.path.getmtime(log_dir + '/best.pt'))
new_log_dir = os.path.dirname(log_dir) + '/' + modified.strftime("%Y-%m-%d_%H-%M-%S")
os.rename(log_dir, new_log_dir)
os.makedirs(log_dir, exist_ok=True)
seed_everything(data_config["SEED"])
# wandb login and init
wandb_name = ('#'+args.wnb_prep+'-ep:'+str(data_config["EPOCHS"])+'-bs:'+str(data_config["BATCH_SIZE"])+'-lr:'+str(data_config["INIT_LR"])+'-im:'+str(data_config["IMG_SIZE"])+
'-val:'+str(data_config["VAL_RATIO"])+'-FP16:'+str(data_config["FP16"]))
print('WNB name :',wandb_name)
wandb.login()
wandb.init(entity = data_config["WNB_ENTITY"], # 팀 지정
project = data_config["WNB_PROJECT"], # 폴더 지정
name = wandb_name
)
# for wandb logging
print('='*70)
print('Model config')
print('='*70)
pprint.pprint(model_config)
print('='*70)
print('\nData config')
print('='*70)
pprint.pprint(data_config)
print('='*70)
test_loss, test_f1, test_acc = train(
model_config=model_config,
data_config=data_config,
log_dir=log_dir,
fp16=data_config["FP16"],
device=device,
)