-
Notifications
You must be signed in to change notification settings - Fork 4
/
train.py
209 lines (164 loc) · 6.46 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
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
from dataset import load_dataset
import easydict
import json
import wandb
import os
import random
from tqdm import tqdm
from importlib import import_module
import pandas as pd
from PIL import Image
import numpy as np
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import models, transforms, utils
from loss import create_criterion
from torch.utils.data import Dataset, DataLoader, random_split, SubsetRandomSampler, WeightedRandomSampler
from sklearn.metrics import f1_score
# 현재 OS 및 라이브러리 버전 체크 체크
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def seed_everything(seed):
"""
동일한 조건으로 학습을 할 때, 동일한 결과를 얻기 위해 seed를 고정시킵니다.
Args:
seed: seed 정수값
"""
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
seed_everything(1004)
def train(args):
# model save 경로 지정하기
save_dir = f"./results/{args.target}"
os.makedirs(save_dir, exist_ok=True)
# 데이터셋 로드하기
trn_dataset = load_dataset(
dataset=args.dataset, target=args.target, train=True)
val_dataset = load_dataset(
dataset=args.dataset, target=args.target, train=False)
trn_transform_original = getattr(import_module(
"dataset"), args.augmentation_original_trn) # 원래 데이터셋에 대한 augmentation
val_transform_original = getattr(import_module(
"dataset"), args.augmentation_original_val) # 원래 데이터셋에 대한 augmentation
trn_transform_aaf = getattr(import_module(
"dataset"), args.augmentation_aaf_trn) # 추가 데이터셋에 대한 augmentation
val_transform_aaf = getattr(import_module(
"dataset"), args.augmentation_aaf_val) # 추가 데이터셋에 대한 augmentation
trn_transform = {
'original_trn': trn_transform_original(),
'aaf_trn': trn_transform_aaf(),
'test_trn': trn_transform_original()
}
val_transform = {
'original_val': val_transform_original(),
'aaf_val': val_transform_aaf(),
'test_val': trn_transform_original()
}
trn_dataset.set_transform(trn_transform)
val_dataset.set_transform(val_transform)
trn_loader = DataLoader(
trn_dataset, batch_size=args.batch_size, shuffle=True, num_workers=2)
val_loader = DataLoader(
val_dataset, batch_size=args.batch_size, shuffle=True, num_workers=2)
num_class = len(trn_dataset.classes)
model_module = getattr(import_module("model"), args.model)
model = model_module(num_class=num_class).to(device)
# Weighted Cross Entroy Loss
weights = [1-n/sum(trn_dataset.count) for n in trn_dataset.count]
weights = torch.FloatTensor(weights).to(device)
criterion = create_criterion(
args.criterion, weight=weights, smoothing=0.1).cuda()
# optimizer
optimizer_module = getattr(import_module("torch.optim"), args.optimizer)
optimizer = optimizer_module(model.parameters(), lr=args.lr)
# Scheduler
scheduler = optim.lr_scheduler.LambdaLR(
optimizer=optimizer, lr_lambda=lambda epoch: 0.95**epoch)
# wandb.init(
# project=args.project,
# entity=args.entity,
# config={
# "learning_rate": args.lr,
# "architecture": args.model,
# "dataset": args.dataset,
# }
# )
best_val_acc = 0.0
best_val_f1 = 0.0
epochs = args.epochs
# Training Start!
print("Start Training!!")
for epoch in range(epochs):
running_loss = 0.0
total = 0
correct = 0
lr = scheduler.get_last_lr()[0]
model.train()
for inputs, labels in tqdm(trn_loader):
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
# Accuracy 계산
_, pred = torch.max(outputs, 1)
total += labels.size(0)
correct += (pred == labels).sum().item()
scheduler.step()
acc = correct/total
print(
f"[TRN]EPOCH:{epoch+1}, LR:{lr}, loss:{running_loss/len(trn_loader):.7f}, acc:{100*acc:.2f}%")
model.eval()
with torch.no_grad():
total = 0
correct = 0
f1 = 0.0
for inputs, labels in val_loader:
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
# Accuracy 계산
_, preds = torch.max(outputs, 1)
total += labels.size(0)
correct += (preds == labels).sum().item()
f1 += f1_score(preds.cpu().numpy(),
labels.cpu().numpy(), average='macro')
val_acc = correct/total
print(
f"[VAL]EPOCH:{epoch+1}, f1:{f1/len(val_loader):.3f}, val_acc:{100*val_acc:.2f}%")
f1 = f1/len(val_loader)
# 모델 저장
# if f1 > best_val_f1:
# print("New Best Model for F1 Score! saving the model...")
# torch.save(model.state_dict(
# ), f"{save_dir}/{args.model}_epoch{epoch:03}_f1_{f1:4.2%}.ckpt")
# best_val_f1 = f1
print("saving the Every model...")
torch.save(model.state_dict(
), f"{save_dir}/{args.model}_epoch{epoch:03}_f1_{f1:4.2%}.ckpt")
if f1 > best_val_f1:
best_val_f1 = f1
# if val_acc > best_val_acc:
# if f1 == best_val_f1:
# continue
# print("New Best Model for Acc Score! saving the model...")
# torch.save(model.state_dict(
# ), f"{save_dir}/{args.model}_epoch{epoch:03}_acc_{val_acc:4.2%}.ckpt")
# best_val_acc = val_acc
wandb.log({"acc": acc, "loss": running_loss /
len(trn_loader), "val_acc": val_acc, "f1": f1})
wandb.finish()
if __name__ == '__main__':
with open('./args.json', 'r') as f:
args = easydict.EasyDict(json.load(f))
train(args)