-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtrain_and_eval_pytorch.py
305 lines (241 loc) · 11.7 KB
/
train_and_eval_pytorch.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
import argparse
import os
import time
from collections import OrderedDict
from copy import deepcopy
import medmnist
import numpy as np
import PIL
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import torchvision.transforms as transforms
from medmnist import INFO, Evaluator
from models import ResNet18, ResNet50
from tensorboardX import SummaryWriter
from torchvision.models import resnet18, resnet50
from tqdm import trange
def main(data_flag, output_root, num_epochs, gpu_ids, batch_size, size, download, model_flag, resize, as_rgb, model_path, run):
lr = 0.001
gamma=0.1
milestones = [0.5 * num_epochs, 0.75 * num_epochs]
info = INFO[data_flag]
task = info['task']
n_channels = 3 if as_rgb else info['n_channels']
n_classes = len(info['label'])
DataClass = getattr(medmnist, info['python_class'])
str_ids = gpu_ids.split(',')
gpu_ids = []
for str_id in str_ids:
id = int(str_id)
if id >= 0:
gpu_ids.append(id)
if len(gpu_ids) > 0:
os.environ["CUDA_VISIBLE_DEVICES"]=str(gpu_ids[0])
device = torch.device('cuda:{}'.format(gpu_ids[0])) if gpu_ids else torch.device('cpu')
output_root = os.path.join(output_root, data_flag, time.strftime("%y%m%d_%H%M%S"))
if not os.path.exists(output_root):
os.makedirs(output_root)
print('==> Preparing data...')
if resize:
data_transform = transforms.Compose(
[transforms.Resize((224, 224), interpolation=PIL.Image.NEAREST),
transforms.ToTensor(),
transforms.Normalize(mean=[.5], std=[.5])])
else:
data_transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize(mean=[.5], std=[.5])])
train_dataset = DataClass(split='train', transform=data_transform, download=download, as_rgb=as_rgb, size=size)
val_dataset = DataClass(split='val', transform=data_transform, download=download, as_rgb=as_rgb, size=size)
test_dataset = DataClass(split='test', transform=data_transform, download=download, as_rgb=as_rgb, size=size)
train_loader = data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
train_loader_at_eval = data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=False)
val_loader = data.DataLoader(dataset=val_dataset,
batch_size=batch_size,
shuffle=False)
test_loader = data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
print('==> Building and training model...')
if model_flag == 'resnet18':
model = resnet18(pretrained=False, num_classes=n_classes) if resize else ResNet18(in_channels=n_channels, num_classes=n_classes)
elif model_flag == 'resnet50':
model = resnet50(pretrained=False, num_classes=n_classes) if resize else ResNet50(in_channels=n_channels, num_classes=n_classes)
else:
raise NotImplementedError
model = model.to(device)
train_evaluator = medmnist.Evaluator(data_flag, 'train', size=size)
val_evaluator = medmnist.Evaluator(data_flag, 'val', size=size)
test_evaluator = medmnist.Evaluator(data_flag, 'test', size=size)
if task == "multi-label, binary-class":
criterion = nn.BCEWithLogitsLoss()
else:
criterion = nn.CrossEntropyLoss()
if model_path is not None:
model.load_state_dict(torch.load(model_path, map_location=device)['net'], strict=True)
train_metrics = test(model, train_evaluator, train_loader_at_eval, task, criterion, device, run, output_root)
val_metrics = test(model, val_evaluator, val_loader, task, criterion, device, run, output_root)
test_metrics = test(model, test_evaluator, test_loader, task, criterion, device, run, output_root)
print('train auc: %.5f acc: %.5f\n' % (train_metrics[1], train_metrics[2]) + \
'val auc: %.5f acc: %.5f\n' % (val_metrics[1], val_metrics[2]) + \
'test auc: %.5f acc: %.5f\n' % (test_metrics[1], test_metrics[2]))
if num_epochs == 0:
return
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=milestones, gamma=gamma)
logs = ['loss', 'auc', 'acc']
train_logs = ['train_'+log for log in logs]
val_logs = ['val_'+log for log in logs]
test_logs = ['test_'+log for log in logs]
log_dict = OrderedDict.fromkeys(train_logs+val_logs+test_logs, 0)
writer = SummaryWriter(log_dir=os.path.join(output_root, 'Tensorboard_Results'))
best_auc = 0
best_epoch = 0
best_model = deepcopy(model)
global iteration
iteration = 0
for epoch in trange(num_epochs):
train_loss = train(model, train_loader, task, criterion, optimizer, device, writer)
train_metrics = test(model, train_evaluator, train_loader_at_eval, task, criterion, device, run)
val_metrics = test(model, val_evaluator, val_loader, task, criterion, device, run)
test_metrics = test(model, test_evaluator, test_loader, task, criterion, device, run)
scheduler.step()
for i, key in enumerate(train_logs):
log_dict[key] = train_metrics[i]
for i, key in enumerate(val_logs):
log_dict[key] = val_metrics[i]
for i, key in enumerate(test_logs):
log_dict[key] = test_metrics[i]
for key, value in log_dict.items():
writer.add_scalar(key, value, epoch)
cur_auc = val_metrics[1]
if cur_auc > best_auc:
best_epoch = epoch
best_auc = cur_auc
best_model = deepcopy(model)
print('cur_best_auc:', best_auc)
print('cur_best_epoch', best_epoch)
state = {
'net': best_model.state_dict(),
}
path = os.path.join(output_root, 'best_model.pth')
torch.save(state, path)
train_metrics = test(best_model, train_evaluator, train_loader_at_eval, task, criterion, device, run, output_root)
val_metrics = test(best_model, val_evaluator, val_loader, task, criterion, device, run, output_root)
test_metrics = test(best_model, test_evaluator, test_loader, task, criterion, device, run, output_root)
train_log = 'train auc: %.5f acc: %.5f\n' % (train_metrics[1], train_metrics[2])
val_log = 'val auc: %.5f acc: %.5f\n' % (val_metrics[1], val_metrics[2])
test_log = 'test auc: %.5f acc: %.5f\n' % (test_metrics[1], test_metrics[2])
log = '%s\n' % (data_flag) + train_log + val_log + test_log
print(log)
with open(os.path.join(output_root, '%s_log.txt' % (data_flag)), 'a') as f:
f.write(log)
writer.close()
def train(model, train_loader, task, criterion, optimizer, device, writer):
total_loss = []
global iteration
model.train()
for batch_idx, (inputs, targets) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(inputs.to(device))
if task == 'multi-label, binary-class':
targets = targets.to(torch.float32).to(device)
loss = criterion(outputs, targets)
else:
targets = torch.squeeze(targets, 1).long().to(device)
loss = criterion(outputs, targets)
total_loss.append(loss.item())
writer.add_scalar('train_loss_logs', loss.item(), iteration)
iteration += 1
loss.backward()
optimizer.step()
epoch_loss = sum(total_loss)/len(total_loss)
return epoch_loss
def test(model, evaluator, data_loader, task, criterion, device, run, save_folder=None):
model.eval()
total_loss = []
y_score = torch.tensor([]).to(device)
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(data_loader):
outputs = model(inputs.to(device))
if task == 'multi-label, binary-class':
targets = targets.to(torch.float32).to(device)
loss = criterion(outputs, targets)
m = nn.Sigmoid()
outputs = m(outputs).to(device)
else:
targets = torch.squeeze(targets, 1).long().to(device)
loss = criterion(outputs, targets)
m = nn.Softmax(dim=1)
outputs = m(outputs).to(device)
targets = targets.float().resize_(len(targets), 1)
total_loss.append(loss.item())
y_score = torch.cat((y_score, outputs), 0)
y_score = y_score.detach().cpu().numpy()
auc, acc = evaluator.evaluate(y_score, save_folder, run)
test_loss = sum(total_loss) / len(total_loss)
return [test_loss, auc, acc]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='RUN Baseline model of MedMNIST2D')
parser.add_argument('--data_flag',
default='pathmnist',
type=str)
parser.add_argument('--output_root',
default='./output',
help='output root, where to save models and results',
type=str)
parser.add_argument('--num_epochs',
default=100,
help='num of epochs of training, the script would only test model if set num_epochs to 0',
type=int)
parser.add_argument('--size',
default=28,
help='the image size of the dataset, 28 or 64 or 128 or 224, default=28',
type=int)
parser.add_argument('--gpu_ids',
default='0',
type=str)
parser.add_argument('--batch_size',
default=128,
type=int)
parser.add_argument('--download',
action="store_true")
parser.add_argument('--resize',
help='resize images of size 28x28 to 224x224',
action="store_true")
parser.add_argument('--as_rgb',
help='convert the grayscale image to RGB',
action="store_true")
parser.add_argument('--model_path',
default=None,
help='root of the pretrained model to test',
type=str)
parser.add_argument('--model_flag',
default='resnet18',
help='choose backbone from resnet18, resnet50',
type=str)
parser.add_argument('--run',
default='model1',
help='to name a standard evaluation csv file, named as {flag}_{split}_[AUC]{auc:.3f}_[ACC]{acc:.3f}@{run}.csv',
type=str)
args = parser.parse_args()
data_flag = args.data_flag
output_root = args.output_root
num_epochs = args.num_epochs
size = args.size
gpu_ids = args.gpu_ids
batch_size = args.batch_size
download = args.download
model_flag = args.model_flag
resize = args.resize
as_rgb = args.as_rgb
model_path = args.model_path
run = args.run
main(data_flag, output_root, num_epochs, gpu_ids, batch_size, size, download, model_flag, resize, as_rgb, model_path, run)