-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
284 lines (215 loc) · 9.09 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
from data_loaders_4binover import get_loader
from logger import Logger
from sklearn import metrics as skmet
import argparse, datetime, json
import numpy as np
import pandas as pd
import os, csv, time
import torch
import torch.nn as nn
from ordinal_loss import order_loss, euclidean_loss, order_loss_p
from mean_variance_loss import MeanVarianceLoss
from models import resnet
LAMBDA_1 = 0.05
LAMBDA_2 = 0.2
START_AGE = 10
END_AGE = 95
metrics_f = {
"accuracy": skmet.accuracy_score,
}
loss_functions = {
"ce": nn.CrossEntropyLoss(),
"order": order_loss,
"mv": MeanVarianceLoss(LAMBDA_1, LAMBDA_2, START_AGE, END_AGE),
"mse": nn.MSELoss(),
"euc": euclidean_loss,
}
def get_n_params(model):
pp=0
for p in list(model.parameters()):
nn=1
for s in list(p.size()):
nn = nn*s
pp += nn
return pp
def train(model, dataset, losses, l_d, opt, epoch, device, cls_num=2):
model.train()
data = iter(dataset)
metrics, losses_dict = {}, {}
for it in range(len(dataset)):
try:
x, y = next(data)
except:
data = iter(dataset)
x, y = next(data)
x = x.to(device)
y = y.to(device)
features, y_ = model(x)
for loss in losses:
if loss == "order" or "euc":
losses_dict[loss] = l_d*loss_functions[loss](features, y)
elif loss == "mv":
mean_loss, var_loss = loss_functions[loss](y_, y[:, 0])
losses_dict[loss] = mean_loss + var_loss
else:
losses_dict[loss] = loss_functions[loss](y_, y[:, 0])
closs = sum(losses_dict.values())
opt.zero_grad()
closs.backward()
opt.step()
for x in losses_dict:
if x in metrics:
metrics[x].append(losses_dict[x].item())
else:
metrics[x] = [losses_dict[x].item()]
if "total_loss" in metrics:
metrics["total_loss"].append(closs.item())
else:
metrics["total_loss"] = [closs.item()]
log = f"Epoch {epoch+1}, Iter {it+1}/{len(dataset)}:"
for m in sorted(metrics.keys()):
log += f" {m} = {np.mean(metrics[m])}"
print(log)
return metrics
def val_test(model, dataset, losses, l_d, epoch, device, cls_num=2, mode="val"):
model.eval()
data = iter(dataset)
metrics, losses_dict = {}, {}
gt = []
pred = []
with torch.no_grad():
for it in range(len(dataset)):
try:
x, y = next(data)
except:
data = iter(dataset)
x, y = next(data)
x = x.to(device)
y = y.to(device)
features, y_ = model(x)
for loss in losses:
if loss == "order" or "euc":
losses_dict[loss] = l_d*loss_functions[loss](features, y)
elif loss == "mv":
mean_loss, var_loss = loss_functions[loss](y_, y[:, 0])
losses_dict[loss] = mean_loss + var_loss
else:
losses_dict[loss] = loss_functions[loss](y_, y[:, 0])
closs = sum(losses_dict.values())
for x in losses_dict:
if x in metrics:
metrics[x].append(losses_dict[x].item())
else:
metrics[x] = [losses_dict[x].item()]
if "total_loss" in metrics:
metrics["total_loss"].append(closs.item())
else:
metrics["total_loss"] = [closs.item()]
for x in y.cpu().numpy().squeeze():
gt.append(x)
for y in torch.argmax(y_, dim=1).cpu().numpy():
pred.append(y)
gt_arr = np.array(gt)
pred_arr = np.array(pred)
for mf in metrics_f.keys():
if mf in metrics:
metrics[mf].append( metrics_f[mf](gt_arr, pred_arr) )
else:
metrics[mf] = [ metrics_f[mf](gt_arr, pred_arr) ]
log = f"Testing on {mode.upper()} Dataset at Epoch {epoch+1}:"
log += f" loss = {np.mean(metrics['total_loss'])}"
log += f" accuracy = {np.mean(metrics['accuracy'])}"
print(log)
return metrics
def main():
parser = argparse.ArgumentParser(description='3D Brain Age prediction')
parser.add_argument('--epochs', default=100, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--batch_size', default=4, type=int, metavar='N',
help='number of samples in each batch')
# Data Parameters
parser.add_argument('-d', '--dataset', default='5data', type=str)
parser.add_argument('--augmentation', action='store_true')
# Losses used
parser.add_argument('--losses', default='ce', type=str, nargs='+', metavar='BETA',
help='losses (default: Cross Entropy (ce), add others)')
parser.add_argument('--ld', default=0.1, type=float, metavar='N',
help='l_d for order_loss')
# Model Parameters
parser.add_argument('--model_name', default="resnet18", type=str,
help='name of the classification model')
parser.add_argument('--num_classes', default=86, type=int, metavar='N',
help='number of classes to predict')
# Optimizer parameters
parser.add_argument('--opt', default='adam', type=str, metavar='OPTIMIZER',
help='Optimizer (default: "adam"')
args = parser.parse_args()
print(args)
if "mse" in args.losses:
args.num_classes = 1
timestamp = datetime.datetime.now().strftime("%m%d%y%H%M%S")
training_folder = f"/data/amciilab/jay/diff-agepred-manh/{args.dataset}_{args.model_name}_{timestamp}"
if not os.path.exists(training_folder):
os.makedirs(training_folder)
os.makedirs(f"{training_folder}/weights")
else:
print(f"{training_folder} exists!")
exit()
with open(f'{training_folder}/params.json', 'w') as f:
json.dump(args.__dict__, f, indent=4)
logger = Logger(f'{training_folder}/logs')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if args.model_name == "resnet10":
model = resnet.generate_model(model_depth=10,
n_classes=args.num_classes,
n_input_channels=1,
widen_factor=1)
elif args.model_name == "resnet18":
model = resnet.generate_model(model_depth=18,
n_classes=args.num_classes,
n_input_channels=1,
widen_factor=1)
elif args.model_name == "resnet34":
model = resnet.generate_model(model_depth=34,
n_classes=args.num_classes,
n_input_channels=1,
widen_factor=1)
elif args.model_name == "resnet50":
model = resnet.generate_model(model_depth=50,
n_classes=args.num_classes,
n_input_channels=1,
widen_factor=1)
elif args.model_name == "resnet101":
model = resnet.generate_model(model_depth=101,
n_classes=args.num_classes,
n_input_channels=1,
widen_factor=1)
else:
print(f"Invalid model_name: {args.model_name}!")
exit()
print("Training folder: ", training_folder)
print("Model parameters: ", get_n_params(model))
dataset_train = get_loader(f'./{args.dataset}/HC/train', batch_size=args.batch_size, mode="train")
dataset_val = get_loader(f'./{args.dataset}/HC/val', batch_size=args.batch_size, mode="val")
dataset_test = get_loader(f'./{args.dataset}/HC/test', batch_size=args.batch_size, mode="test")
if args.opt == "adam":
opt = torch.optim.Adam(model.parameters())
else:
print(f"Invalid OPTIMIZER: {args.model_name}!")
exit()
model.to(device)
for e in range(args.epochs):
loss_metrics = train(model, dataset_train, args.losses, args.ld, opt, e, device, args.num_classes)
for tag, value in loss_metrics.items():
for cnt in range(len(value)):
logger.scalar_summary("train/"+tag, value[cnt], e * len(loss_metrics["loss"]) + cnt + 1)
mpath = os.path.join(f"{training_folder}/weights", '{}.ckpt'.format(e+1))
torch.save(model.state_dict(), mpath)
val_loss_metrics = val_test(model, dataset_val, args.losses, args.ld, e, device, args.num_classes, "val")
for tag, value in val_loss_metrics.items():
logger.scalar_summary("val/"+tag, np.mean(value), (e+1))
test_loss_metrics = val_test(model, dataset_test, args.losses, args.ld, e, device, args.num_classes, "test")
for tag, value in test_loss_metrics.items():
logger.scalar_summary("test/"+tag, np.mean(value), (e+1))
if __name__ == '__main__':
main()