-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIL_train.py
335 lines (300 loc) · 12.5 KB
/
MIL_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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import sys
import os
import json
import numpy as np
import argparse
import random
#import openslide
import cv2
import PIL.Image as Image
import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
import torch.utils.data as data
import torchvision.transforms as transforms
import torchvision.models as models
from efficientnet_pytorch import EfficientNet
from torch.optim.lr_scheduler import StepLR
from PIL import Image
parser = argparse.ArgumentParser(description='MIL-nature-medicine-2019 tile classifier training script')
parser.add_argument('--train_lib', type=str, default='', help='path to train MIL library binary')
parser.add_argument('--val_lib', type=str, default='', help='path to validation MIL library binary. If present.')
parser.add_argument('--output', type=str, default='.', help='name of output file')
parser.add_argument('--batch_size_train', type=int, default=512, help='mini-batch size (default: 512)')
parser.add_argument('--batch_size_val', type=int, default=512, help='mini-batch size (default: 512)')
parser.add_argument('--nepochs', type=int, default=100, help='number of epochs')
parser.add_argument('--workers', default=4, type=int, help='number of data loading workers (default: 4)')
parser.add_argument('--test_every', default=10, type=int, help='test on val every (default: 10)')
parser.add_argument('--weights', default=0.5, type=float, help='unbalanced positive class weight (default: 0.5, balanced classes)')
parser.add_argument('--k', default=1, type=float, help='top k tiles are assumed to be of the same class as the slide (default: 1, standard MIL)')
parser.add_argument('--previous_checkpoint', default=None, type=str, help='Path to the previous checkopoint if the training has been interupted')
best_acc = 0
def main():
global args, best_acc
args = parser.parse_args()
#cudnn
if args.previous_checkpoint is None:
model = models.resnet34(True)
model.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3,
bias=False)
model.fc = nn.Linear(model.fc.in_features, 2)
## Alexnet
# model = models.alexnet(args.previous_checkpoint)
# model.features [0] = nn.Conv2d(1, 64, kernel_size=11, stride=4, padding=2)
# model.classifier[6] = nn.Linear(4096,2)
## My classifier
#model = ConvNet()
#model.classifier[6] = nn.Linear(4096,2)
#model.fc = nn.Linear(model.fc.in_features, 2)
else:
model = models.resnet34(args.previous_checkpoint)
model.fc = nn.Linear(model.fc.in_features, 2)
# model = models.alexnet(args.previous_checkpoint)
# model.classifier[6] = nn.Linear(4096,2)
#model.fc = nn.Linear(model.fc.in_features, 2)
model.cuda()
if args.weights==0.5:
criterion = nn.CrossEntropyLoss().cuda()
else:
w = torch.Tensor([1-args.weights,args.weights])
criterion = nn.CrossEntropyLoss(w).cuda()
lr_ = 1e-1
optimizer = optim.Adam(model.parameters(), lr=lr_, weight_decay=1e-4)
scheduler = StepLR(optimizer, step_size=10, gamma=0.1)
cudnn.benchmark = True
#normalization
#normalize = transforms.Normalize(mean=[0.5,0.5,0.5],std=[0.1,0.1,0.1])
color = transforms.ColorJitter(brightness=0.1, contrast=0.2, saturation=0.3, hue=0.02)
##trans = transforms.Compose([transforms.ToTensor(), color])
trans = transforms.Compose([ color, transforms.ToTensor()])
#load data
train_dset = MILdataset(args.train_lib, trans)
train_loader = torch.utils.data.DataLoader(
train_dset,
batch_size=args.batch_size_train, shuffle=False,
num_workers=args.workers, pin_memory=False)
if args.val_lib:
val_dset = MILdataset(args.val_lib, trans)
val_loader = torch.utils.data.DataLoader(
val_dset,
batch_size=args.batch_size_val, shuffle=False,
num_workers=args.workers, pin_memory=False)
#open output file
fconv = open(os.path.join(args.output,'convergence.csv'), 'w')
fconv.write('epoch,metric,value\n')
fconv.close()
#loop throuh epochs
for epoch in range(args.nepochs):
if epoch > 3 and epoch <= 6:
lr_ = 1e-2
optimizer.param_groups[0]['lr'] = lr_
elif epoch > 6 and epoch <= 18:
lr_ = 1e-3
optimizer.param_groups[0]['lr'] = lr_
else:
scheduler.step()
print('Epoch:', epoch,'LR:', scheduler.get_lr())
train_dset.setmode(1)
probs = inference(epoch, train_loader, model, 'train')
maxs = group_max(np.array(train_dset.slideIDX), probs, len(train_dset.targets))
pred = [1 if x >= 0.5 else 0 for x in maxs]
err,fpr,fnr = calc_err(pred, train_dset.targets)
fconv = open(os.path.join(args.output, 'convergence.csv'), 'a')
fconv.write('{},Training_error,{}\n'.format(epoch+1, err))
fconv.write('{},Training_fpr,{}\n'.format(epoch+1, fpr))
fconv.write('{},Training_fnr,{}\n'.format(epoch+1, fnr))
fconv.write('{},Training_fnr,{}\n'.format(epoch+1, str(optimizer.param_groups[0]['lr'])))
fconv.close()
topk = group_argtopk(np.array(train_dset.slideIDX), probs, args.k)
train_dset.maketraindata(topk)
train_dset.shuffletraindata()
train_dset.setmode(2)
loss = train(epoch, train_loader, model, criterion, optimizer)
print('Training\tEpoch: [{}/{}]\tLoss: {}'.format(epoch+1, args.nepochs, loss))
fconv = open(os.path.join(args.output, 'convergence.csv'), 'a')
fconv.write('{},loss,{}\n'.format(epoch+1,loss))
fconv.close()
#Validation
if args.val_lib : # and (epoch+1) % args.test_every == 0
val_dset.setmode(1)
probs = inference(epoch, val_loader, model, 'eval')
maxs = group_max(np.array(val_dset.slideIDX), probs, len(val_dset.targets))
pred = [1 if x >= 0.5 else 0 for x in maxs]
err,fpr,fnr = calc_err(pred, val_dset.targets)
print('Validation\tEpoch: [{}/{}]\tError: {}\tFPR: {}\tFNR: {}'.format(epoch+1, args.nepochs, err, fpr, fnr))
fconv = open(os.path.join(args.output, 'convergence.csv'), 'a')
fconv.write('{},error,{}\n'.format(epoch+1, err))
fconv.write('{},fpr,{}\n'.format(epoch+1, fpr))
fconv.write('{},fnr,{}\n'.format(epoch+1, fnr))
fconv.close()
#Save best model
err = (fpr+fnr)/2.
if 1-err >= best_acc:
best_acc = 1-err
obj = {
'epoch': epoch+1,
'state_dict': model.state_dict(),
'best_acc': best_acc,
'optimizer' : optimizer.state_dict()
}
torch.save(obj, os.path.join(args.output,'checkpoint_best_{}.pth'.format(str( epoch+1))))
def inference(run, loader, model, eval_train):
model.eval()
probs = torch.FloatTensor(len(loader.dataset))
with torch.no_grad():
for i, input in enumerate(loader):
input = input.cuda()
output = F.softmax(model(input), dim=1)
if eval_train == 'train':
probs[i*args.batch_size_train:i*args.batch_size_train+input.size(0)] = output.detach()[:,1].clone()
else:
probs[i*args.batch_size_val:i*args.batch_size_val+input.size(0)] = output.detach()[:,1].clone()
return probs.cpu().numpy()
def train(run, loader, model, criterion, optimizer):
model.train()
running_loss = 0.
for i, (input, target) in enumerate(loader):
input = input.cuda()
target = target.cuda()
output = model(input)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()*input.size(0)
return running_loss/len(loader.dataset)
def calc_err(pred,real):
pred = np.array(pred)
real = np.array(real)
neq = np.not_equal(pred, real)
err = float(neq.sum())/pred.shape[0]
fpr = float(np.logical_and(pred==1,neq).sum())/(real==0).sum()
fnr = float(np.logical_and(pred==0,neq).sum())/(real==1).sum()
return err, fpr, fnr
def group_argtopk(groups, data,k=1):
k = int(len(groups) * k)
order = np.lexsort((data, groups))
groups = groups[order]
data = data[order]
index = np.empty(len(groups), 'bool')
index[-k:] = True
index[:-k] = groups[k:] != groups[:-k]
return list(order[index])
def group_max(groups, data, nmax):
out = np.empty(nmax)
out[:] = np.nan
order = np.lexsort((data, groups))
groups = groups[order]
data = data[order]
index = np.empty(len(groups), 'bool')
index[-1] = True
index[:-1] = groups[1:] != groups[:-1]
out[groups[index]] = data[index]
return out
class Attention(nn.Module):
def __init__(self):
super(Attention, self).__init__()
self.L = 500
self.D = 128
self.K = 1
self.feature_extractor_part1 = nn.Sequential(
nn.Conv2d(1, 20, kernel_size=5),
nn.ReLU(),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(20, 50, kernel_size=5),
nn.ReLU(),
nn.MaxPool2d(2, stride=2)
)
self.feature_extractor_part2 = nn.Sequential(
nn.Linear(50 * 4 * 4, self.L),
nn.ReLU(),
)
self.attention = nn.Sequential(
nn.Linear(self.L, self.D),
nn.Tanh(),
nn.Linear(self.D, self.K)
)
self.classifier = nn.Sequential(
nn.Linear(self.L*self.K, 1),
nn.Sigmoid()
)
def forward(self, x):
x = x.squeeze(0)
H = self.feature_extractor_part1(x)
H = H.view(-1, 50 * 4 * 4)
H = self.feature_extractor_part2(H) # NxL
A = self.attention(H) # NxK
A = torch.transpose(A, 1, 0) # KxN
A = F.softmax(A, dim=1) # softmax over N
M = torch.mm(A, H) # KxL
Y_prob = self.classifier(M)
Y_hat = torch.ge(Y_prob, 0.5).float()
return Y_prob, Y_hat, A
class MILdataset(data.Dataset):
def __init__(self, libraryfile='', transform=None):
with open(libraryfile) as json_file:
lib = json.load(json_file)
slides = lib['Slides']
# for i,name in enumerate(lib['slides']):
# sys.stdout.write('Opening SVS headers: [{}/{}]\r'.format(i+1, len(lib['slides'])))
# sys.stdout.flush()
# slides.append(openslide.OpenSlide(name))
#Flatten grid
tiles_full = []
slideIDX = []
print(len(lib['Tiles']))
for i,g in enumerate(lib['Tiles']):
#print('g' , g)
tiles_full.extend(g)
slideIDX.extend([i]*len(g))
print('Number of tiles: {}'.format(len(tiles_full)))
print('Length ', len(tiles_full), len(slideIDX))
self.slidenames = lib['Slides']
self.targets = lib['Targets']
self.tiles = lib['Tiles']
self.tiles_full = tiles_full
self.slideIDX = slideIDX
self.transform = transform
self.mode = None
def setmode(self,mode):
print('mode ', mode)
self.mode = mode
def maketraindata(self, idxs):
self.t_data = [(self.slideIDX[x],self.tiles_full[x],self.targets[self.slideIDX[x]]) for x in idxs]
def shuffletraindata(self):
self.t_data = random.sample(self.t_data, len(self.t_data))
def __getitem__(self,index):
if self.mode == 1:
slideIDX = self.slideIDX[index]
tiles_path = self.tiles_full[index]
##img = cv2.imread(tiles_path)
img = Image.open(tiles_path)
width = 512
height = 512
img = img.resize((width, height))
if self.transform is not None:
img = self.transform(img)
return img
elif self.mode == 2:
slideIDX, coord, target = self.t_data[index]
tiles_path = self.tiles_full[index]
try:
img = Image.open(tiles_path)
width = 512
height = 512
img = img.resize((width, height))
#img = cv2.imread(tiles_path)
except:
print('ERROR ', tiles_path)
if self.transform is not None:
img = self.transform(img)
return img, target
def __len__(self):
if self.mode == 1:
return len(self.tiles_full)
elif self.mode == 2:
return len(self.t_data)
if __name__ == '__main__':
main()