|
a |
|
b/usage/usage2.py |
|
|
1 |
import os |
|
|
2 |
import time |
|
|
3 |
import glob |
|
|
4 |
import sys |
|
|
5 |
import numpy as np |
|
|
6 |
import openslide |
|
|
7 |
import matplotlib.pyplot as plt |
|
|
8 |
import cv2 |
|
|
9 |
import matplotlib.gridspec as gridspec |
|
|
10 |
from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
from PIL import Image |
|
|
15 |
sys.path.append('..') |
|
|
16 |
from DigiPathAI.Segmentation import getSegmentation |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
digestpath_imgs = ['../examples/colon-cancer-1.tiff'] |
|
|
20 |
|
|
|
21 |
models = ['dense']#, 'inception', 'deeplabv3', 'ensemble', 'epistemic'] |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
for path in digestpath_imgs: |
|
|
25 |
ext = os.path.splitext(path)[1] |
|
|
26 |
base_path = os.path.splitext(path)[0] |
|
|
27 |
|
|
|
28 |
print (ext, base_path, base_path[:-5]) |
|
|
29 |
quick = True |
|
|
30 |
tta_list = ['FLIP_LEFT_RIGHT', 'ROTATE_90'] #, 'ROTATE_180', 'ROTATE_270'] |
|
|
31 |
for model in models: |
|
|
32 |
|
|
|
33 |
print (model, quick, path, "======================================") |
|
|
34 |
if model == 'ensemble': |
|
|
35 |
quick = False |
|
|
36 |
elif model == 'epistemic': |
|
|
37 |
quick = False |
|
|
38 |
tta_list = None |
|
|
39 |
""" |
|
|
40 |
getSegmentation(path, |
|
|
41 |
patch_size = 256, |
|
|
42 |
stride_size = 128, |
|
|
43 |
batch_size = 4, |
|
|
44 |
quick = quick, |
|
|
45 |
tta_list = tta_list, |
|
|
46 |
crf = False, |
|
|
47 |
probs_path = base_path + '-DigiPathAI_{}_probs'.format(model) + '.tiff', |
|
|
48 |
mask_path = base_path + '-DigiPathAI_{}_mask'.format(model) + '.tiff', |
|
|
49 |
uncertainty_path = base_path + '-DigiPathAI_{}_uncertainty'.format(model)+ '.tiff', |
|
|
50 |
status = None, |
|
|
51 |
mask_level = 4, |
|
|
52 |
model = model, |
|
|
53 |
mode = 'colon') |
|
|
54 |
""" |
|
|
55 |
slide = openslide.OpenSlide(path) |
|
|
56 |
level = len(slide.level_dimensions) - 1 |
|
|
57 |
img_dimensions = slide.level_dimensions[-1] |
|
|
58 |
img = np.array(slide.read_region((0,0), level, img_dimensions).convert('RGB')) |
|
|
59 |
|
|
|
60 |
mask = openslide.OpenSlide(base_path + '-DigiPathAI_{}_mask'.format(model) + '.tiff') |
|
|
61 |
level = np.where([1 if ((dim[0] == img_dimensions[0])*(dim[1] == img_dimensions[1])) else 0 for dim in mask.level_dimensions])[0] |
|
|
62 |
mask = np.array(mask.read_region((0,0), level, img_dimensions).convert('L')) |
|
|
63 |
|
|
|
64 |
probs = openslide.OpenSlide(base_path + '-DigiPathAI_{}_probs'.format(model) + '.tiff') |
|
|
65 |
level = np.where([1 if ((dim[0] == img_dimensions[0])*(dim[1] == img_dimensions[1])) else 0 for dim in probs.level_dimensions])[0] |
|
|
66 |
probs = np.array(probs.read_region((0,0), level, img_dimensions).convert('L'))/255.0 |
|
|
67 |
|
|
|
68 |
gt = np.array(Image.open(base_path+ 'gt.jpg').convert('L').resize(img_dimensions)) |
|
|
69 |
|
|
|
70 |
fig, ax = plt.subplots(2, 2, figsize=(14, 20)) |
|
|
71 |
fig.tight_layout() |
|
|
72 |
im_ = ax[0][0].imshow(img) |
|
|
73 |
ax[0][0].set_xticklabels([]) |
|
|
74 |
ax[0][0].set_yticklabels([]) |
|
|
75 |
ax[0][0].set_xticks([]) |
|
|
76 |
ax[0][0].set_yticks([]) |
|
|
77 |
ax[0][0].set_aspect('equal') |
|
|
78 |
ax[0][0].tick_params(bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off' ) |
|
|
79 |
# ax[0][0].title.set_text("WSI Slide") |
|
|
80 |
|
|
|
81 |
gt_ = ax[0][1].imshow(gt,cmap='gray') |
|
|
82 |
ax[0][1].set_xticklabels([]) |
|
|
83 |
ax[0][1].set_yticklabels([]) |
|
|
84 |
ax[0][1].set_xticks([]) |
|
|
85 |
ax[0][1].set_yticks([]) |
|
|
86 |
ax[0][1].set_aspect('equal') |
|
|
87 |
ax[0][1].tick_params(bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off' ) |
|
|
88 |
# ax[0][1].title.set_text("Ground Truth") |
|
|
89 |
|
|
|
90 |
pred_ = ax[1][1].imshow(mask,cmap='gray') |
|
|
91 |
ax[1][1].set_xticklabels([]) |
|
|
92 |
ax[1][1].set_yticklabels([]) |
|
|
93 |
ax[1][1].set_xticks([]) |
|
|
94 |
ax[1][1].set_yticks([]) |
|
|
95 |
ax[1][1].set_aspect('equal') |
|
|
96 |
ax[1][1].tick_params(bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off' ) |
|
|
97 |
# ax[1][1].title.set_text("Ground Truth") |
|
|
98 |
|
|
|
99 |
prob_map_ = ax[1][0].imshow(probs, cmap=plt.cm.jet) |
|
|
100 |
ax[1][0].set_xticklabels([]) |
|
|
101 |
ax[1][0].set_yticklabels([]) |
|
|
102 |
ax[1][0].set_xticks([]) |
|
|
103 |
ax[1][0].set_yticks([]) |
|
|
104 |
ax[1][0].set_aspect('equal') |
|
|
105 |
ax[1][0].tick_params(bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off' ) |
|
|
106 |
# ax[1][0].title.set_text("Probability Map") |
|
|
107 |
|
|
|
108 |
cax = fig.add_axes([ax[1][0].get_position().x1 + 0.01, |
|
|
109 |
ax[1][0].get_position().y0, |
|
|
110 |
0.01, |
|
|
111 |
ax[1][0].get_position().y1-ax[1][0].get_position().y0]) |
|
|
112 |
fig.colorbar(prob_map_, cax=cax) |
|
|
113 |
|
|
|
114 |
plt.savefig('im2.png',bbox_inches = 'tight',pad_inches = 0.1) |