-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
308 lines (240 loc) · 17.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torch.optim.lr_scheduler import StepLR
from torchvision import transforms, utils
import os, sys, shutil,itertools,random
import matplotlib.pyplot as plt
from datetime import datetime
import numpy as np
import pandas as pd
from time import time
from tqdm import tqdm
import wandb
from model.models import ResNet
from model.models import BasicBlock
from model.optimizers import get_optimizer
from model.losses import get_loss_function
from model.models import get_model
from modules.schedulers import get_scheduler
from modules.datasets import MaskBaseDataset, MaskSplitByProfileDataset
from modules.metrics import get_metric_function
from modules.datasets import get_dataset_function
from modules.transforms import get_transform_function
from modules.utils import load_yaml,save_yaml
from modules.logger import MetricAverageMeter,LossAverageMeter
prj_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(prj_dir)
import warnings
warnings.filterwarnings('ignore')
if __name__ == "__main__":
train_serial = datetime.now().strftime("%Y%m%d_%H%M%S")
train_result_dir = os.path.join(prj_dir, 'results', 'train', train_serial)
os.makedirs(train_result_dir, exist_ok=True)
#data dir
# Load config
config_path = os.path.join(prj_dir, 'config', 'train.yaml')
config = load_yaml(config_path)
shutil.copy(config_path, os.path.join(train_result_dir,'train.yaml'))
data_dir = config['train_dir']
# data_gen_dir = config['train_gen_dir']
#seed
torch.manual_seed(config['seed'])
torch.cuda.manual_seed(config['seed'])
torch.cuda.manual_seed_all(config['seed']) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(config['seed'])
random.seed(config['seed'])
#wandb
if config['wandb']:
wandb.init(project=config["wandb_project"], config={
"learning_rate": config['optimizer']['args']['lr'],
"architecture": config['model']['architecture'],
"dataset": "MaskDaset",
"notes":config['wandb_note']
},
name = config['wandb_run'])
else:
wandb.init(mode="disabled")
os.environ['CUDA_VISIBLE_DEVICES'] = str(config['gpu_num'])
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("device : ",device)
transform = get_transform_function(config['transform'],config)
print(transform)
if config['dataset'] == "baseDataset":
dataset =get_dataset_function(config['dataset'])
dataset = dataset(data_dir, transform,val_ratio=config['val_size'])
train_dataset, val_dataset = dataset.split_dataset()
train_dataloader = DataLoader(train_dataset, batch_size=config['batch_size'], drop_last=config['drop_last'],num_workers=config['num_workers'])
val_dataloader = DataLoader(val_dataset, batch_size=config['batch_size'], drop_last=config['drop_last'],num_workers=config['num_workers'])
else:
dataset =get_dataset_function(config['dataset'])
dataset = dataset(data_dir, transform,val_ratio=config['val_size'],seed=config['seed'], drop_age_mode=config["drop_age_mode"], drop_age=config["drop_age"])
train_dataset, val_dataset = dataset.split_dataset()
train_sampler = dataset.get_sampler('train')
train_dataloader = DataLoader(train_dataset, batch_size=config['batch_size'], drop_last=config['drop_last'],num_workers=config['num_workers'], sampler=train_sampler)
valid_sampler = dataset.get_sampler('val')
val_dataloader = DataLoader(val_dataset, batch_size=config['batch_size'], drop_last=config['drop_last'],num_workers=config['num_workers'], sampler=valid_sampler)
num_classes = dataset.num_classes
if config['model_custom']:
model = get_model(config['model']['architecture'])
model = model(**config['model']['args'])
else:
model = get_model(config['model']['architecture'])
model = model(config['model']['architecture'], **config['model']['args'])
model = model.to(device)
print(f"Load model architecture: {config['model']['architecture']}")
# model = model(3, 10).to(device)
# model = ResNet1(BasicBlock, [3, 4, 6, 3]).to(device)
wandb.watch(model)
optimizer = get_optimizer(optimizer_str=config['optimizer']['name'])
optimizer = optimizer(model.parameters(), **config['optimizer']['args'])
scheduler = get_scheduler(scheduler_str=config['scheduler']['name'])
scheduler = scheduler(optimizer=optimizer, **config['scheduler']['args'])
loss_func = get_loss_function(loss_function_str=config['loss']['name'])
loss_func = loss_func(**config['loss']['args'])
# loss_func = loss_func()
metric_funcs = {metric_name:get_metric_function(metric_name) for metric_name in config['metrics']}
max_f1_score = 0
model.train()
f1_score_lst = ["acc", "f1_score", "mask_f1_score", "gender_f1_score", "age_f1_score"]
f1_class_score_lst = ["mask_class_f1_score", "gender_class_f1_score", "age_class_f1_score"]
f1_mask_age_lst= ["mask_age_f1_score"]
for epoch_id in range(config['n_epochs']):
tic = time()
train_loss = 0
train_scores = {metric_name: 0 for metric_name, _ in metric_funcs.items() if metric_name in f1_score_lst}
train_class_scores = {metric_name: np.array([0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_class_score_lst}
train_class_cnt = {metric_name: np.array([0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_class_score_lst}
train_mask_class_score = {metric_name: np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_mask_age_lst}
train_mask_class_cnt = {metric_name: np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_mask_age_lst}
for iter, (img, label) in enumerate(tqdm(train_dataloader)):
img = img.to(device)
label = label.to(device)
batch_size = img.shape[0]
pred_value = model(img)
if config['multi_label']:
mask_labels, gender_labels, age_labels = dataset.decode_multi_class(label)
mask_labels, gender_labels, age_labels = mask_labels.to(device), gender_labels.to(device), age_labels.to(device)
(mask_outs, gender_outs, age_outs) = torch.split(pred_value, [3, 2, 3], dim=1)
mask_loss = loss_func(mask_outs, mask_labels)
gender_loss = loss_func(gender_outs, gender_labels)
age_loss = loss_func(age_outs, age_labels)
loss = mask_loss + gender_loss + age_loss
else:
loss = loss_func(pred_value, label)
# Backpropagation
loss.backward()
optimizer.step()
optimizer.zero_grad()
# Accuracy 계산
for metric_name, metric_func in metric_funcs.items():
if metric_name in f1_score_lst:
train_scores[metric_name] += metric_func(pred_value, label) / len(train_dataloader)
elif metric_name in f1_class_score_lst:
score, cnt = metric_func(pred_value, label)
train_class_scores[metric_name] += score
train_class_cnt[metric_name] += cnt
elif metric_name in f1_mask_age_lst:
score, cnt = metric_func(pred_value, label)
train_mask_class_score[metric_name] += score
train_mask_class_cnt[metric_name] += cnt
train_loss += loss.item() / len(train_dataloader)
for metric_name, _ in train_class_scores.items():
for i in range(3):
if train_class_scores[metric_name][i] != 0:
train_class_scores[metric_name][i] = train_class_scores[metric_name][i] / train_class_cnt[metric_name][i]
for metric_name, _ in train_mask_class_score.items():
for i in range(9):
if train_mask_class_score[metric_name][i] != 0:
train_mask_class_score[metric_name][i] = train_mask_class_score[metric_name][i] / train_mask_class_cnt[metric_name][i]
scheduler.step()
# Validation
valid_loss = 0
valid_scores = {metric_name: 0 for metric_name, _ in metric_funcs.items() if metric_name in f1_score_lst}
valid_class_scores = {metric_name: np.array([0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_class_score_lst}
valid_class_cnt = {metric_name: np.array([0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_class_score_lst}
valid_mask_class_score = {metric_name: np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_mask_age_lst}
valid_mask_class_cnt = {metric_name: np.array([0.,0.,0.,0.,0.,0.,0.,0.,0.]) for metric_name, _ in metric_funcs.items() if metric_name in f1_mask_age_lst}
# if (iter % 20 == 0) or (iter == len(qd_train_dataloader)-1):
model.eval()
toc = time()
train_time = toc- tic
for img, label in val_dataloader:
##fill##
img = img.to(device)
label = label.to(device)
batch_size = img.shape[0]
with torch.no_grad():
pred_value = model(img)
if config['multi_label']:
mask_labels, gender_labels, age_labels = dataset.decode_multi_class(label)
mask_labels, gender_labels, age_labels = mask_labels.to(device), gender_labels.to(device), age_labels.to(device)
(mask_outs, gender_outs, age_outs) = torch.split(pred_value, [3, 2, 3], dim=1)
mask_loss = loss_func(mask_outs, mask_labels)
gender_loss = loss_func(gender_outs, gender_labels)
age_loss = loss_func(age_outs, age_labels)
loss = mask_loss + gender_loss + age_loss
else:
loss = loss_func(pred_value, label)
# Accuracy 계산
for metric_name, metric_func in metric_funcs.items():
if metric_name in f1_score_lst:
valid_scores[metric_name] += metric_func(pred_value, label) / len(val_dataloader)
elif metric_name in f1_class_score_lst:
score, cnt = metric_func(pred_value, label)
valid_class_scores[metric_name] += score
valid_class_cnt[metric_name] += cnt
elif metric_name in f1_mask_age_lst:
score, cnt = metric_func(pred_value, label)
valid_mask_class_score[metric_name] += score
valid_mask_class_cnt[metric_name] += cnt
valid_loss += loss.item() / len(val_dataloader)
for metric_name, _ in valid_class_scores.items():
for i in range(3):
if valid_class_scores[metric_name][i] != 0:
valid_class_scores[metric_name][i] = valid_class_scores[metric_name][i] / valid_class_cnt[metric_name][i]
for metric_name, _ in valid_mask_class_score.items():
for i in range(9):
if valid_mask_class_score[metric_name][i] != 0:
valid_mask_class_score[metric_name][i] = valid_mask_class_score[metric_name][i] / valid_mask_class_cnt[metric_name][i]
# print("Epoch [%4d/%4d] | Train Loss %.4f | Train Acc %.4f | Valid Loss %.4f | Valid Acc %.4f" %
# (epoch_id, config['n_epochs'], train_loss, train_acc, valid_loss, valid_acc))
print("Epoch [%4d/%4d] | Train Loss %.4f | Train Acc %.4f | Train F1 %.4f | Valid Loss %.4f | Valid Acc %.4f | Valid F1 %.4f" %
(epoch_id, config['n_epochs'], train_loss, train_scores['acc'], train_scores['f1_score'], valid_loss, valid_scores['acc'], valid_scores['f1_score']))
print(" train_mask_f1_score %.4f | label_0 %.4f | label_1 %.4f | label_2 %.4f" % (train_scores['mask_f1_score'], train_class_scores['mask_class_f1_score'][0], train_class_scores['mask_class_f1_score'][1], train_class_scores['mask_class_f1_score'][2]))
print(" train_gender_f1_score %.4f | label_0 %.4f | label_1 %.4f" % (train_scores['gender_f1_score'], train_class_scores['gender_class_f1_score'][0], train_class_scores['gender_class_f1_score'][1]))
print(" train_age_f1_score %.4f | label_0 %.4f | label_1 %.4f | label_2 %.4f" % (train_scores['age_f1_score'], train_class_scores['age_class_f1_score'][0], train_class_scores['age_class_f1_score'][1], train_class_scores['age_class_f1_score'][2]))
print(" train_mask0_age0_f1_score %.4f | train_mask0_age1_f1_score %.4f | train_mask0_age2_f1_score %.4f | train_mask1_age0_f1_score %.4f | train_mask1_age1_f1_score %.4f | train_mask1_age2_f1_score %.4f | train_mask2_age0_f1_score %.4f | train_mask2_age1_f1_score %.4f | train_mask2_age2_f1_score %.4f" % tuple(x for x in train_mask_class_score['mask_age_f1_score']))
print(" valid_mask_f1_score %.4f | label_0 %.4f | label_1 %.4f | label_2 %.4f" % (valid_scores['mask_f1_score'], valid_class_scores['mask_class_f1_score'][0], valid_class_scores['mask_class_f1_score'][1], valid_class_scores['mask_class_f1_score'][2]))
print(" valid_gender_f1_score %.4f | label_0 %.4f | label_1 %.4f" % (valid_scores['gender_f1_score'], valid_class_scores['gender_class_f1_score'][0], valid_class_scores['gender_class_f1_score'][1]))
print(" valid_age_f1_score %.4f | label_0 %.4f | label_1 %.4f | label_2 %.4f" % (valid_scores['age_f1_score'], valid_class_scores['age_class_f1_score'][0], valid_class_scores['age_class_f1_score'][1], valid_class_scores['age_class_f1_score'][2]))
print(" valid_mask0_age0_f1_score %.4f | valid_mask0_age1_f1_score %.4f | valid_mask0_age2_f1_score %.4f | valid_mask1_age0_f1_score %.4f | valid_mask1_age1_f1_score %.4f | valid_mask1_age2_f1_score %.4f | valid_mask2_age0_f1_score %.4f | valid_mask2_age1_f1_score %.4f | valid_mask2_age2_f1_score %.4f" % tuple(x for x in valid_mask_class_score['mask_age_f1_score']))
new_wandb_metric_dict = {"train_time":train_time,"train_loss":train_loss,"train_acc":train_scores['acc'],"train_f1":train_scores['f1_score'], "valid_loss":valid_loss, "valid_acc":valid_scores['acc'], "valid_f1":valid_scores['f1_score'],
"train_mask_f1_score":train_scores['mask_f1_score'],"train_mask0_f1_score":train_class_scores['mask_class_f1_score'][0],"train_mask1_f1_score":train_class_scores['mask_class_f1_score'][1],"train_mask2_f1_score":train_class_scores['mask_class_f1_score'][2],
"train_age_f1_score":train_scores['age_f1_score'],"train_age0_f1_score":train_class_scores['age_class_f1_score'][0],"train_age1_f1_score":train_class_scores['age_class_f1_score'][1],"train_age2_f1_score":train_class_scores['age_class_f1_score'][2],
"train_gender_f1_score":train_scores['gender_f1_score'],"train_gender0_f1_score":train_class_scores['gender_class_f1_score'][0],"train_gender1_f1_score":train_class_scores['gender_class_f1_score'][1],
"valid_mask_f1_score":valid_scores['mask_f1_score'],"valid_mask0_f1_score":valid_class_scores['mask_class_f1_score'][0],"valid_mask1_f1_score":valid_class_scores['mask_class_f1_score'][1],"valid_mask2_f1_score":valid_class_scores['mask_class_f1_score'][2],
"valid_age_f1_score":valid_scores['age_f1_score'],"valid_age0_f1_score":valid_class_scores['age_class_f1_score'][0],"valid_age1_f1_score":valid_class_scores['age_class_f1_score'][1],"valid_age2_f1_score":valid_class_scores['age_class_f1_score'][2],
"valid_gender_f1_score":valid_scores['gender_f1_score'],"valid_gender0_f1_score":valid_class_scores['gender_class_f1_score'][0],"valid_gender1_f1_score":valid_class_scores['gender_class_f1_score'][1]}
for i in range(9):
new_wandb_metric_dict[f"train_mask{i//3}_age{i%3}_f1_score"] = train_mask_class_score['mask_age_f1_score'][i]
for i in range(9):
new_wandb_metric_dict[f"valid_mask{i//3}_age{i%3}_f1_score"] = valid_mask_class_score['mask_age_f1_score'][i]
wandb.log(new_wandb_metric_dict)
if max_f1_score < valid_scores['f1_score']:
check_point = {
'epoch': epoch_id + 1,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict() if scheduler else None}
torch.save(check_point,os.path.join(train_result_dir,f'model_{epoch_id}.pt'))
torch.save(check_point,os.path.join(train_result_dir,f'best_model.pt'))
early_stopping_count = 0
max_f1_score = valid_scores['f1_score']
else:
early_stopping_count += 1
if early_stopping_count >= config['early_stopping_count']:
exit()
# print(model)