-
Notifications
You must be signed in to change notification settings - Fork 1
/
inference.py
257 lines (235 loc) · 11.9 KB
/
inference.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
import sys, os
import torch
import argparse
import timeit
import numpy as np
import scipy.misc as misc
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import shutil
from torch.utils import data
from tqdm import tqdm
from ptsemseg.models import get_model
from ptsemseg.utils import get_logger
from ptsemseg.loader import get_loader, get_data_path
from ptsemseg.utils import convert_state_dict
import cv2
from PIL import Image
from BMPWriter import BMPWriter as bmpw
import pandas
from pdb import set_trace
import openslide
parser = argparse.ArgumentParser(description="Params")
parser.add_argument( "--model_path", nargs="?", type=str, default="models/DMMN-osteosarcoma.pkl")
parser.add_argument( "--out_path", nargs="?", type=str, default="imgs/DMMN-osteosarcoma", help="Path of the output segmap")
args = parser.parse_args()
### Osteosarcoma pretrained model segmentation classes ###
# red: viable tumor
# blue: necrosis with bone
# yellow: necrosis without bone
# green: normal bone
# orange: normal tissue
# brown: cartilage
# gray: background
def test():
bmp = bmpw()
model_file_name = os.path.split(args.model_path)[1]
model_name = {}
model_name['arch'] = "DMMN"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_classes = 8 # the number of tissue subtype classes + 1
test_file = "test_coords.csv" # the list of patch coordinates
with open(test_file) as f1:
test_file_patches = [line.rstrip('\n') for line in f1]
data_path = "testing_images/" # the path where testing whole slide images are located
tile_size = 1024
problem_type = 'tissue'
batch_size = 1
with open(test_file) as f:
file_names = [line.rstrip('\n') for line in f]
model = get_model(model_name, n_classes)
state = convert_state_dict(torch.load(args.model_path)["model_state"])
model.load_state_dict(state)
model.eval()
model.cuda()
if not os.path.exists(str(args.out_path)):
os.makedirs(str(args.out_path))
test_file_patches_split = test_file_patches[0].split(",")
pslide_id = int(test_file_patches_split[0])
filename = os.path.join(os.path.abspath(args.out_path) + "/" + str(pslide_id) + ".svs_data/predictions.bmp")
slide = openslide.OpenSlide(data_path + str(pslide_id) + ".svs")
tilecnt = 0
with torch.no_grad():
for ii in tqdm(range(0,len(test_file_patches))):
test_file_patches_split = test_file_patches[ii].split(",")
slide_id = int(test_file_patches_split[0])
xmin = int(test_file_patches_split[1])
ymin = int(test_file_patches_split[2])
inputs_slide = slide.read_region((xmin,ymin), 0, (tile_size,tile_size)).convert('RGB')
inputs_slide = np.array(inputs_slide)/255.0
inputs_slide = np.expand_dims(np.transpose(inputs_slide, (2,1,0)), axis=0)
inputs = torch.from_numpy(inputs_slide).float().to(device)
inputs = torch.flip(inputs.permute(0,1,3,2),[1])
if tilecnt >= len(test_file_patches):
image_size = slide.dimensions
bmp.writebmp(filename,outfile,int(image_size[0]), int(image_size[1]),palette='standard')
break
filename = os.path.join(os.path.abspath(args.out_path) + "/" + str(slide_id) + ".svs_data/predictions.bmp")
if slide_id != pslide_id:
pfilename = os.path.join(os.path.abspath(args.out_path) + "/" + str(pslide_id) + ".svs_data/predictions.bmp")
image_size = slide.dimensions
bmp.writebmp(pfilename,outfile,int(image_size[0]), int(image_size[1]),palette='standard')
slide = openslide.OpenSlide(data_path + str(slide_id) + ".svs")
if not os.path.isdir(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename), exist_ok=True)
image_size = slide.dimensions
outfile = bmp.makeempty(int(image_size[0]), int(image_size[1]))
xmin = int(test_file_patches_split[1])
ymin = int(test_file_patches_split[2])
if xmin == 0 and ymin == 0:
# tile at outfile[0:128,0:128]
ref_pad = nn.ReflectionPad2d(512)
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
outfile[:128,:64] = t_mask_shrink[128:,64:]
# tile at outfile[0:128,128:384]
ref_pad = nn.ReflectionPad2d((512,512,256,256))
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
outfile[:128,64:192] = t_mask_shrink[128:,:]
# tile at outfile[128:384,0:128]
ref_pad = nn.ReflectionPad2d((256,256,512,512))
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
outfile[128:384,:64] = t_mask_shrink[:,64:]
# tile at outfile[128:384,128:384]
ref_pad = nn.ReflectionPad2d(256)
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
outfile[128:384,64:192] = t_mask_shrink
xmin = int(test_file_patches_split[1])
ymin = int(test_file_patches_split[2])
if xmin == 0:
ymin += 384
# tile at outfile[0:128,ymin:ymin+256]
ref_pad = nn.ReflectionPad2d((512,512,0,0))
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
t2 = int(ymin)
wh2 = int(image_size[1])
if t2 < wh2:
if t2 + 256 > wh2:
outfile[t2:,:64] = t_mask_shrink[0:wh2-t2,64:]
else:
outfile[t2:t2+256,:64] = t_mask_shrink[:,64:]
# tile at outfile[128:384,ymin:ymin+256]
ref_pad = nn.ReflectionPad2d((256,256,0,0))
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
t2 = int(ymin)
wh2 = int(image_size[1])
if t2 < wh2:
if t2 + 256 > wh2:
outfile[t2:,64:192] = t_mask_shrink[0:wh2-t2,:]
else:
outfile[t2:t2+256,64:192] = t_mask_shrink
xmin = int(test_file_patches_split[1])
ymin = int(test_file_patches_split[2])
if ymin == 0:
xmin += 384
# tile at outfile[xmin:xmin+256,0:128]
ref_pad = nn.ReflectionPad2d((0,0,512,512))
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
t1 = bmp.getrowsize(int(xmin))
wh1 = bmp.getrowsize(int(image_size[0]))
if t1 < wh1:
if t1 + 128 > wh1:
outfile[:128,t1:] = t_mask_shrink[128:,0:wh1-t1]
else:
outfile[:128,t1:t1+128] = t_mask_shrink[128:,:]
# tile at outfile[xmin:xmin+256,128:384]
ref_pad = nn.ReflectionPad2d((0,0,256,256))
inputs_new = torch.zeros([1,3,1024,1024]).cuda()
inputs_new = ref_pad(inputs)[:,:,:1024,:1024]
outputs = model(inputs_new[:,:,384:640,384:640],inputs_new[:,:,::2,::2][:,:,128:384,128:384],inputs_new[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
t_mask_shrink = mask_shrink(t_mask)
t1 = bmp.getrowsize(int(xmin))
wh1 = bmp.getrowsize(int(image_size[0]))
if t1 < wh1:
if t1 + 128 > wh1:
outfile[128:384,t1:] = t_mask_shrink[:,0:wh1-t1]
else:
outfile[128:384,t1:t1+128] = t_mask_shrink
xmin = int(test_file_patches_split[1])
ymin = int(test_file_patches_split[2])
outputs = model(inputs[:,:,384:640,384:640],inputs[:,:,::2,::2][:,:,128:384,128:384],inputs[:,:,::4,::4])
t_masks = torch.argmax(outputs, dim=1).cpu().numpy().astype(np.uint8)
t_mask = t_masks[0,:,:]
xmin += 384
ymin += 384
t_mask_shrink = mask_shrink(t_mask)
t1 = bmp.getrowsize(int(xmin))
t2 = int(ymin)
image_size = slide.dimensions
wh1 = bmp.getrowsize(int(image_size[0]))
wh2 = int(image_size[1])
if t1 < wh1 and t2 < wh2:
if t2 + 256 > wh2 and t1 + 128 > wh1:
outfile[t2:,int(int(xmin)/2):] = t_mask_shrink[0:wh2-t2,0:wh1-t1]
elif t2 + 256 > wh2:
outfile[t2:,t1:t1+128] = t_mask_shrink[0:wh2-t2,:]
elif t1 + 128 > wh1:
outfile[t2:t2+256,t1:] = t_mask_shrink[:,0:wh1-t1]
else:
outfile[t2:t2+256,t1:t1+128] = t_mask_shrink
pslide_id = slide_id
tilecnt += 1
image_size = slide.dimensions
bmp.writebmp(filename,outfile,int(image_size[0]), int(image_size[1]),palette='standard')
def mask_shrink(t_mask):
# for color-coding
t_mask[t_mask == 6] = 10
t_mask[t_mask == 5] = 6
t_mask[t_mask == 4] = 5
t_mask[t_mask == 7] = 4
t_mask_shrink = t_mask[:,::2]*17
return t_mask_shrink
if __name__ == "__main__":
test()