-
Notifications
You must be signed in to change notification settings - Fork 43
/
train.py
156 lines (127 loc) · 4.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
# -*- coding:UTF-8 -*-
"""
training classifying task with CNN
@Cai Yichao 2020_09_18
"""
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torchsummary import summary
from models.resnet import *
from models.resnext import *
from models.densenet import *
from utils.arg_utils import *
from utils.data_utils import *
from utils.progress_utils import progress_bar
from utils.earlystopping import EarlyStopping
import mlflow
mlflow.set_tracking_uri("http://0.0.0.0:5002")
mlflow.set_experiment("train-trial")
"""
arguments
"""
args = fetch_args()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
early_stopping = EarlyStopping(args['patience'], verbose=True, delta=args['delta'])
"""
loading data-set....
"""
print("==> loading data-set...")
train_loader, classes = gen_train_loader(args['train_path'], args['input_size'], args['train_batch_size'])
test_loader, _ = gen_test_loader(args['test_path'], args['input_size'], args['test_batch_size'])
print('Task classes are: ', classes)
num_classes = len(classes)
print(num_classes)
"""
model
"""
print("==> building model...")
# net = ResNet_50(num_classes=2)
# net = resNeXt50_32x4d_SE(num_classes=num_classes)
net = densenet_121(num_classes=num_classes)
net = net.to(device)
summary(net, (3, 224, 224))
# if device is 'cuda':
# net = torch.nn.DataParallel(net)
# cudnn.benchmark = True
"""
training
"""
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=args['learning_rate'], momentum=0.9, weight_decay=5e-4)
def log_scalar(name, value, step):
"""Log a scalar value to both MLflow"""
mlflow.log_metric(name, value)
def train(epoch):
print('\nEpoch:[%d/%d]' % (epoch, args['epochs']))
net.train()
loss, correct, total = 0, 0, 0
for index, (inputs, targets) in enumerate(train_loader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss_curr = criterion(outputs, targets)
loss_curr.backward()
optimizer.step()
loss += loss_curr.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
progress_bar(index, len(train_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (loss / (index + 1), 100. * correct / total, correct, total))
log_scalar("train_loss", loss / (index + 1), epoch)
best_acc = 0
def test(epoch):
global best_acc
net.eval()
loss, correct, total = 0, 0, 0
with torch.no_grad():
for index, (inputs, targets) in enumerate(test_loader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = net(inputs)
loss_curr = criterion(outputs, targets)
loss += loss_curr.item()
_, predicted = outputs.max(1)
print(targets)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
progress_bar(index, len(test_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
%(loss/(index+1), 100.*correct/total, correct, total))
eval_loss = loss/(index+1)
acc = 100.*correct/total
if acc >= best_acc:
print("Saving checkpoints..")
state = {
'net': net.state_dict(),
'acc': acc,
'epoch': epoch,
'eval_loss': eval_loss
}
if not os.path.isdir(args['ckpt_path']):
os.mkdir(args['ckpt_path'])
torch.save(state, args['ckpt_path'] + str('/ckpt_%d_acc%.2f.pt' % (epoch, acc)))
best_acc = acc
log_scalar("eval_loss", eval_loss, epoch)
log_scalar("eval_acc", acc, epoch)
return eval_loss
with mlflow.start_run():
# log parameters into mlflow
mlflow.log_param("learning_rate", args['learning_rate'])
mlflow.log_param("input_size", args['input_size'])
mlflow.log_param("train_batch_size", args['train_batch_size'])
mlflow.log_param("test_batch_size", args['test_batch_size'])
mlflow.log_param("total_epochs", args['epochs'])
mlflow.log_param("earlystop_patience", args['patience'])
mlflow.log_param("earlystop_delta", args['delta'])
for epoch in range(args['epochs']):
train(epoch)
eval_loss = test(epoch)
# early stopping
early_stopping(eval_loss, net)
if early_stopping.early_stop:
print("Early stopping.")
break
mlflow.log_artifacts(args["ckpt_path"])