-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
executable file
·188 lines (144 loc) · 7.01 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
import argparse
import os
import random
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from brainiac.utils import *
from brainiac.train_utils import *
from brainiac.log_helper import *
import logging
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str,
required=True, help='Network archetecture')
parser.add_argument('--data_path', type=str,
default='../ADNI-processed', help='Full path to a data .csv file')
parser.add_argument('--classes', type=str,
default="['CN', 'AD']", help='Classes for experiment')
parser.add_argument('--use_regression', type=str2bool,
default=False, help='Train regression model')
parser.add_argument('--num_epoch', type=int,
default=200, help='Number of epoch')
parser.add_argument('--batch_size', type=int,
default=4, help='Batch size')
parser.add_argument('--optimizer', type=str,
default='Adam', help='Optimizer',
choices=['Adam', 'SGD', 'RMSprop'])
parser.add_argument('--lr', type=float,
default=3e-5, help='Learning rate')
parser.add_argument('--weight_decay', type=float,
default=1e-3, help='Weight decay')
parser.add_argument('--use_augmentation', type=str2bool,
default=True, help='Use or not augmentation')
parser.add_argument('--use_sampling', type=str2bool,
default=True, help='Use sampling or not')
parser.add_argument('--sampling_type', type=str,
default='over', help='Type of sampling (over and under)')
parser.add_argument('--train_print_every', type=int,
default=1, help='Interval of logging in train')
parser.add_argument('--test_print_every', type=int,
default=1, help='Interval of logging in test')
parser.add_argument('--use_scheduler', type=str2bool,
default=True, help='Use scheduler or not')
parser.add_argument('--scheduler_step', type=str,
default='[50, 100, 150]', help='Scheduler\'s steps')
parser.add_argument('--scheduler_gamma', type=float,
default=0.1, help='Scheduler\'s gamma')
parser.add_argument('--device', type=str,
default='cuda', help='Computing device')
parser.add_argument('--use_pretrain', type=str2bool,
default=False, help='Use pretrain model')
parser.add_argument('--path_pretrain', type=str,
default=None, help='Path to pretrain model')
parser.add_argument('--pretrain_head', type=int,
default=2, help='Num classes of pretrain model')
parser.add_argument('--seed', type=int, default=42)
args = parser.parse_args()
save_dir = 'trained_model/{}/{}-classes-{}_optim-{}_aug-{}_sampling-{}_lr-{}_scheduler-{}_pretrain-{}/'.format(
args.model, args.data_path.split('/')[-1], '-'.join([str(i) for i in eval(args.classes)]),
args.optimizer, int(args.use_augmentation), int(args.use_sampling),
args.lr, int(args.use_scheduler), int(args.use_pretrain))
args.save_dir = save_dir
print(save_dir)
assert False
args.images_path = args.data_path + '/images/'
args.data_path = args.data_path + '/data.csv'
if args.device < 'cpu':
args.device = 'cuda:' + args.device
return args
def train_epoch(epoch, model, data_loader, optimizer, args):
model.train()
y_true = []
y_pred = []
for batch_idx, (data, target) in enumerate(data_loader):
optimizer.zero_grad()
output = model(data.to(args.device))
loss = F.cross_entropy(output, target.to(args.device))
loss.backward()
optimizer.step()
y_true.append(target.detach().cpu().numpy())
y_pred.append(output.detach().cpu().numpy())
if batch_idx % args.train_print_every == 0:
logging.info('Train Epoch: {:04d} | Iter: [{:04d} / {:04d} ({:02d}%)] | Loss: {:.6f}'.format(
epoch, batch_idx * len(data), data_loader.sampler.num_samples,
int(100. * batch_idx / len(data_loader)), loss.item()))
y_true = np.concatenate(y_true)
y_pred = np.concatenate(y_pred)
num_samples = len(y_true)
correct, acc, roc_auc = get_metrics(y_true, y_pred)
logging.info('Train Epoch: {:04d} | Accuracy: {}/{} ({:.3f}) | RocAuc: {:.3f}'.format(
epoch, correct, num_samples, acc, roc_auc))
def test_epoch(epoch, model, data_loader, args):
model.eval()
test_loss = 0
y_true = []
y_pred = []
with torch.no_grad():
for data, target in data_loader:
output = model(data.to(args.device))
test_loss += F.cross_entropy(output, target.to(args.device), reduction='sum').item() # sum up batch loss
y_true.append(target.cpu().numpy())
y_pred.append(output.cpu().numpy())
y_true = np.concatenate(y_true)
y_pred = np.concatenate(y_pred)
num_samples = len(y_true)
correct, acc, roc_auc = get_metrics(y_true, y_pred)
test_loss /= num_samples
logging.info('Test Epoch: {:04d} | Average loss: {:.4f} | Accuracy: {}/{} ({:.3f}) | RocAuc: {:.3f}'.format(
epoch, test_loss, correct, num_samples, acc, roc_auc))
return acc
def train(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
log_save_id = create_log_id(args.save_dir)
logging_config(folder=args.save_dir, name='log{:d}'.format(log_save_id), no_console=False)
logging.info(args)
logging.info('-'*20 + 'Data' + '-'*20)
train_loader, test_loader, num_classes = make_data(args)
logging.info('-'*20 + 'Model' + '-'*20)
model, init_epoch = make_model(args, num_classes)
optimizer = get_optimizer(model, args)
scheduler = get_scheduler(optimizer, args)
logging.info(model)
logging.info('-'*20 + 'Train' + '-'*20)
model.to(args.device)
best_acc = 0
best_epoch = -1
for epoch in range(init_epoch, init_epoch + args.num_epoch):
train_epoch(epoch, model, train_loader, optimizer, args)
if scheduler:
scheduler.step()
logging.info('Learning rate: {}'.format(optimizer.state_dict()['param_groups'][0]['lr']))
if epoch % args.test_print_every == 0:
current_acc = test_epoch(epoch, model, test_loader, args)
if current_acc > best_acc:
save_model_epoch(model, args.save_dir, epoch, best_epoch)
best_acc, best_epoch = current_acc, epoch
logging.info('Save model at {} epoch'.format(epoch))
save_model_epoch(model, args.save_dir, epoch)
if __name__ == '__main__':
args = parse_args()
train(args)