-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWSI_pytorch_utils.py
180 lines (137 loc) · 6.32 KB
/
WSI_pytorch_utils.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
import os
import sys
import glob
import time
import random
import numpy as np
import copy
from PIL import Image
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import Dataset, DataLoader
from WSI_utils import*
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data
import torchvision.models as models
from torchvision import datasets, models, transforms
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
class WSIDataset(Dataset):
"""Sample from the slides indicated by the wsi.
Switch turning the imgs to batches into the Dataset rather than the dataloader.
Standard pytorch dataloader wants to return one img at a time,
so instead set batch_size=1 and return all the imgs at once.
Set the length to 100 000
Must check if having one batch from the same slide and of one class is a problem
"""
SEED = 101
random.seed(SEED)
def __init__(self, data_loc, normal_nums, tumor_nums, batch_size, length=100000, transforms=None):
"""nums is a list of """
all_data = glob.glob(data_loc+'/**/*.tif', recursive=True)
self.normal_locs = [loc for loc in all_data if any(str(x) in loc for x in normal_nums) and 'normal' in loc.lower()]
self.tumor_locs = [loc for loc in all_data if any(str(x) in loc for x in tumor_nums) and 'tumor' in loc.lower() and 'mask' not in loc.lower()]
# self.tumor_mask_locs = [loc for loc in all_data if any(str(x) in loc for x in tumor_nums) and 'mask' in loc.lower()]
self.all_locs = self.normal_locs + self.tumor_locs
self.batch_size = batch_size
self.length = length
self.transforms = transforms
def __len__(self):
# we tell pytorch we are using a batch size of 1, so need to scale down the length
return int(self.length/self.batch_size)
def __getitem__(self, index):
"""Easiest way is to return half of each batch as tumor and non-tumor.
We don't care about a sampler method, or the indices.
At each call of __getitem__ we randomly select 2 WSIs. There is no iterating over the dataset.
"""
num_tiles = int(self.batch_size/2)
tumor_loc = random.choice(self.tumor_locs)
tumor_wsi = WSI(tumor_loc)
tumor_imgs = tumor_wsi.sample_batch_tumor_region(num_tiles, tile_size=224)
normal_loc = random.choice(self.all_locs)
normal_wsi = WSI(normal_loc)
normal_imgs = normal_wsi.sample_batch_normal_region(num_tiles, tile_size=224)
batch_imgs = tumor_imgs+normal_imgs
labels = [1]*num_tiles + [0]*num_tiles
if self.transforms is not None:
for idx, img in enumerate(batch_imgs):
batch_imgs[idx] = self.transforms(batch_imgs[idx])
## randomize ???
combined = list(zip(batch_imgs, labels))
random.shuffle(combined)
batch_imgs[:], labels[:] = zip(*combined)
labels = torch.LongTensor(labels)
batch_imgs = torch.squeeze(torch.stack(batch_imgs))
# labels = torch.stack(labels, 0)
return torch.squeeze(torch.stack(batch_imgs)), labels
def train_model(model, dataloaders, dataset_sizes, criterion, optimizer, scheduler, num_epochs=5, use_gpu=True):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'valid']:
if phase == 'train':
scheduler.step()
model.train(True) # Set model to training mode
else:
model.train(False) # Set model to evaluate mode
model.eval()
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for data in tqdm(dataloaders[phase]):
# get the inputs
inputs, labels = data
# labels = torch.stack(labels, 0)
# wrap them in Variable
if use_gpu:
inputs = Variable(inputs.cuda())
labels = Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
inputs, labels = torch.squeeze(inputs), torch.squeeze(labels)
# zero the parameter gradients
optimizer.zero_grad()
# forward
outputs = model(inputs)
# for nets that have multiple outputs such as inception
if isinstance(outputs, tuple):
loss = sum((criterion(o,labels) for o in outputs))
else:
loss = criterion(outputs, labels)
# backward + optimize only if in training phase
if phase == 'train':
# _, preds = torch.max(outputs[0].data, 1)
_, preds = torch.max(outputs.data, 1)
loss.backward()
optimizer.step()
else:
_, preds = torch.max(outputs.data, 1)
# statistics
running_loss += loss.data[0] * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
del loss, outputs # Don't know why we need to do this, but some kind of memory leak
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'valid' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best valid Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
return model