-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
163 lines (127 loc) · 5.67 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
import os
import argparse
import warnings
import multiprocessing
import numpy as np
from importlib import import_module
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from dataset.dataset import get_dataset
from utils.collator import MixCollator
from utils.criterion import create_criterion, MixCriterion
from utils.fold import train_kfold
from utils.metric import validation
from utils.scheduler import get_scheduler
from utils.util import seed_everything, save_model, increment_path
def train(model, optimizer, train_loader, test_loader, scheduler,
device, saved_dir, args):
model.to(device)
criterion = create_criterion(args.criterion).to(device)
if args.cutmix or args.mixup:
criterion = MixCriterion(criterion)
if not args.no_valid:
val_criterion = create_criterion(args.criterion).to(device)
best_score = 0
patience = args.early_stopping if args.early_stopping > 0 else 9999
for epoch in range(1, args.epochs + 1):
model.train()
train_loss = []
for img, label in tqdm(iter(train_loader)):
img = img.float().to(device)
if args.cutmix or args.mixup:
targets1, targets2, lam = label
label = (targets1.to(device), targets2.to(device), lam)
else:
label = label.to(device)
optimizer.zero_grad()
model_pred = model(img)
loss = criterion(model_pred, label)
loss.backward()
optimizer.step()
train_loss.append(loss.item())
tr_loss = np.mean(train_loss)
if args.no_valid:
print(f'Epoch [{epoch}], Train Loss : [{tr_loss:.5f}]')
else:
val_loss, val_score = validation(model, val_criterion, test_loader, device)
print(f'Epoch [{epoch}], Train Loss : [{tr_loss:.5f}] Val Loss : [{val_loss:.5f}] Val F1 Score : [{val_score:.5f}]')
if best_score < val_score:
best_score = val_score
file_name = f'{args.model}_Epoch_{epoch}_F1_{best_score:.5f}'
save_model(model, saved_dir, file_name)
if args.early_stopping > 0:
patience = args.early_stopping
elif args.early_stopping > 0:
patience -= 1
if args.early_stopping > 0 and patience < 1:
print('Early stopping ...')
break
if scheduler is not None:
scheduler.step()
if epoch == args.epochs:
save_model(model, saved_dir)
def parse_arg():
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('--data_dir', type=str, default='data/')
parser.add_argument('--model', type=str, default='BaseModel')
parser.add_argument('--epochs', type=int, default=20)
parser.add_argument('--early_stopping', type=int, default=0)
parser.add_argument('--optimizer', type=str, default='Adam')
parser.add_argument('--scheduler', type=str, default=None)
parser.add_argument('--criterion', type=str, default='cross_entropy')
parser.add_argument('--augmentation', type=str, default='BaseAugmentation')
parser.add_argument('--resize', type=int, default=480)
parser.add_argument('--crop_size', type=int, default=224)
parser.add_argument('--lr', type=float, default=3e-4)
parser.add_argument('--batch_size', type=int, default=64)
parser.add_argument('--seed', type=int, default=41)
parser.add_argument('--cutmix', action='store_true')
parser.add_argument('--mixup', action='store_true')
parser.add_argument('--alpha', type=float, default=1.0)
parser.add_argument('--name', type=str, default='exp', help='model save at {name}')
parser.add_argument('--no_valid', action='store_true')
# KFold arguments
parser.add_argument('--kfold', action='store_true')
parser.add_argument('--stratified', action='store_true')
parser.add_argument('--n_splits', type=int, default=7)
parser.add_argument('--tta', action='store_true')
parser.add_argument('--oof', action='store_true')
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_arg()
print(args)
warnings.filterwarnings('ignore')
device = "cuda" if torch.cuda.is_available() else "cpu"
saved_dir = increment_path(os.path.join('./output/model', args.name))
seed_everything(args.seed)
num_workers = multiprocessing.cpu_count() // 2
if args.cutmix:
collate_fn = MixCollator(alpha=args.alpha, mode='cutmix')
elif args.mixup:
collate_fn = MixCollator(alpha=args.alpha, mode='mixup')
else:
collate_fn = None
if args.kfold:
train_kfold(device, saved_dir, num_workers, collate_fn, args)
else:
# Dataset
train_dataset, val_dataset = get_dataset(args)
train_loader = DataLoader(train_dataset, batch_size=args.batch_size,
shuffle=True, num_workers=num_workers,
collate_fn=collate_fn)
if args.no_valid:
val_loader = None
else:
val_loader = DataLoader(val_dataset, batch_size=args.batch_size,
shuffle=False, num_workers=num_workers)
# Train model
model_module = getattr(import_module("models.model"), args.model)
model = model_module(num_classes=50)
model.eval()
optimizer_module = getattr(import_module('torch.optim'), args.optimizer)
optimizer = optimizer_module(model.parameters(), lr=args.lr)
scheduler = get_scheduler(args.scheduler, optimizer, args.epochs)
train(model, optimizer, train_loader, val_loader, scheduler,
device, saved_dir, args)