-
Notifications
You must be signed in to change notification settings - Fork 19
/
run.py
865 lines (720 loc) · 33.6 KB
/
run.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
"""run.py
Usage:
run.py [--gpu=<id>] [--mode=<mode>] [--model=<path>] [--input_dir=<path>] [--output_dir=<path>] \
[--cache_dir=<path>] [--batch_size=<n>] [--inf_tile_shape=<n>] [--proc_tile_shape=<n>] \
[--postproc_workers=<n>] [--return_probs]
run.py (-h | --help)
run.py --version
Options:
-h --help Show this string.
--version Show version.
--gpu=<id> GPU list. [default: 0]
--mode=<mode> Inference mode. `tile` or `wsi`. [default: wsi]
--model=<path> Path to model. Use either `pannuke.npz` or `monusac.npz` [default: hovernet.npz]
--input_dir=<path> Directory containing input images/WSIs.
--output_dir=<path> Directory where the output will be saved.
--cache_dir=<path> Cache directory for saving temporary output. [default: cache/]
--batch_size=<n> Batch size. [default: 25]
--inf_tile_shape=<n> Size of tiles for inference (assumes square shape). [default: 15000]
--proc_tile_shape=<n> Size of tiles for post processing (assumes square shape). [default: 2048]
--postproc_workers=<n> Number of workers for post processing. [default: 10]
--return_probs Whether to return the class probabilities for each nucleus
"""
from docopt import docopt
import glob
import math
import os
import sys
import json
import importlib
from collections import deque
from multiprocessing import Pool
import cv2
import numpy as np
import tqdm
from tensorpack.predict import OfflinePredictor, PredictConfig
from tensorpack.tfutils.sessinit import get_model_loader
from tensorpack import logger
logger._getlogger().disabled = True # disable logging of network info
from hover.postproc.process_utils import process
from hover.misc.wsi_handler import get_wsi_handler
from hover.misc.utils import rm_n_mkdir, visualize_instances, get_tissue_mask
from hover.misc.run_utils import (
remove_inst,
assemble_and_flush,
get_tile_info,
get_tile_patch_info,
post_proc_para_wrapper
)
import time
class InferTile(object):
"""Tile inference class
Attributes:
nr_types: number of classes(including BG) for nuclear type classification
patch_input_shape: input dimensions of patch [h, w]
patch_output_shape: output dimensions of patch [h, w]
input_norm (bool): whether to normalise the input between 0 and 1
model_name: which dataset the model was trained on- either `pannuke` or `monusac`
model_path: path to the npz checkpoint file
input_dir: input directory containing WSIs
output_dir: output directory where files will be saved
batch_size (int): batch size to use during inference
input_tensor_names: defines what it the input in the computational graph
output_tensor_names: defines what it the output in the computational graph
"""
def __init__(self,):
self.nr_types = None
self.patch_input_shape = [256, 256]
self.patch_output_shape = [164, 164]
self.input_norm = True
self.input_tensor_names = ["images"]
self.output_tensor_names = ["predmap-coded"]
def _parse_args(self, args):
"""Parse CLI arguments
Args:
args: command line interface arguments
"""
self.model_path = args["--model"]
self.input_dir = args["--input_dir"]
self.output_dir = args["--output_dir"]
self.batch_size = int(args["--batch_size"])
self.return_probs = args['--return_probs']
# get the model name from the checkpoint
model_name = os.path.basename(self.model_path)
self.model_name = model_name.split('.')[0]
if self.model_name == 'pannuke':
self.nr_types = 6
elif self.model_name == 'monusac':
self.nr_types = 5
def get_model(self):
"""Get the model architecture"""
model_constructor = importlib.import_module("hover.model.graph")
model_constructor = model_constructor.Model_NP_HV
return model_constructor # NOTE return alias, not object
def __gen_prediction(self, tile, predictor):
"""Using 'predictor' to generate the prediction of input tile
Args:
tile: input image to be segmented. It will be split into patches
to run the prediction upon before being assembled back
predictor: A predictor built from a given config.
Return:
pred_map: merged probability map
"""
step_size = self.patch_output_shape
msk_size = self.patch_output_shape
win_size = self.patch_input_shape
def get_last_steps(length, msk_size, step_size):
nr_step = math.ceil((length - msk_size) / step_size)
last_step = (nr_step + 1) * step_size
return int(last_step), int(nr_step + 1)
im_h = tile.shape[0]
im_w = tile.shape[1]
last_h, nr_step_h = get_last_steps(im_h, msk_size[0], step_size[0])
last_w, nr_step_w = get_last_steps(im_w, msk_size[1], step_size[1])
diff_h = win_size[0] - step_size[0]
padt = diff_h // 2
padb = last_h + win_size[0] - im_h
diff_w = win_size[1] - step_size[1]
padl = diff_w // 2
padr = last_w + win_size[1] - im_w
tile = np.lib.pad(tile, ((padt, padb), (padl, padr), (0, 0)), "reflect")
sub_patches = []
# generating subpatches from orginal
for row in range(0, last_h, step_size[0]):
for col in range(0, last_w, step_size[1]):
win = tile[row : row + win_size[0], col : col + win_size[1]]
sub_patches.append(win)
pred_list = deque()
while len(sub_patches) > self.batch_size:
mini_batch = sub_patches[: self.batch_size]
sub_patches = sub_patches[self.batch_size :]
batch_output = predictor(mini_batch)[0]
batch_output = np.split(batch_output, self.batch_size, axis=0)
pred_list.extend(batch_output)
if len(sub_patches) != 0:
batch_output = predictor(sub_patches)[0]
batch_output = np.split(batch_output, len(sub_patches), axis=0)
pred_list.extend(batch_output)
output_patch_shape = np.squeeze(pred_list[0]).shape
ch = 1 if len(output_patch_shape) == 2 else output_patch_shape[-1]
# Assemble back into full image
pred_map = np.squeeze(np.array(pred_list))
pred_map = np.reshape(pred_map, (nr_step_h, nr_step_w) + pred_map.shape[1:])
pred_map = (
np.transpose(pred_map, [0, 2, 1, 3, 4])
if ch != 1
else np.transpose(pred_map, [0, 2, 1, 3])
)
pred_map = np.reshape(
pred_map,
(
pred_map.shape[0] * pred_map.shape[1],
pred_map.shape[2] * pred_map.shape[3],
ch,
),
)
pred_map = np.squeeze(pred_map[:im_h, :im_w]) # just crop back to original size
return pred_map
def load_model(self):
"""Loads the model and checkpoints"""
print("Loading Model...")
model_path = self.model_path
model_constructor = self.get_model()
pred_config = PredictConfig(
model=model_constructor(
self.nr_types, self.patch_input_shape, self.patch_output_shape, self.input_norm
),
session_init=get_model_loader(model_path),
input_names=self.input_tensor_names,
output_names=self.output_tensor_names,
)
self.predictor = OfflinePredictor(pred_config)
def process_all_files(self):
"""Process all image files within a directory. The function will:
1) Load the image
2) Extract patches the entire image
3) Run inference
4) Return instance prediction, overlay and JSON files
"""
save_dir = self.output_dir
file_list = glob.glob("%s/*" % self.input_dir)
file_list.sort() # ensure same order
rm_n_mkdir(save_dir)
pbar = tqdm.tqdm(desc='Processing Images', leave=True,
total=len(file_list),
ncols=80, ascii=True, position=0)
for filename in file_list:
filename = os.path.basename(filename)
basename = os.path.splitext(filename)[0]
rm_n_mkdir(save_dir + '/' + basename)
###
img = cv2.imread(self.input_dir + "/" + filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
###
pred_map = self.__gen_prediction(img, self.predictor)
pred_inst, pred_info = process(
pred_map, nr_types=self.nr_types, return_dict=True, return_probs=self.return_probs)
overlaid_output = visualize_instances(img, pred_info, self.model_name)
overlaid_output = cv2.cvtColor(overlaid_output, cv2.COLOR_BGR2RGB)
cv2.imwrite("%s/%s/overlay.png" % (save_dir, basename), overlaid_output)
np.save("%s/%s/instances.npy" % (save_dir, basename), pred_inst)
# save result info as json file
json_dict = {}
for inst_id, inst_info in pred_info.items():
new_inst_info = {}
for info_name, info_value in inst_info.items():
# convert to JSON
if isinstance(info_value, np.ndarray):
info_value = info_value.tolist()
new_inst_info[info_name] = info_value
json_dict[int(inst_id)] = new_inst_info
with open("%s/%s/nuclei_dict.json" % (save_dir, basename), "w") as handle:
json.dump(json_dict, handle)
pbar.update()
pbar.close()
class InferWSI(object):
"""WSI inference class
Attributes:
nr_types: number of classes (including BG) for nuclear type classification
patch_input_shape: input dimensions of patch [h,w]
patch_output_shape: output dimensions of patch [h,w]
input_norm (bool): whether to normalise the input between 0 and 1
model_name: which dataset the model was trained on- either `pannuke` or `monusac`
wsi_proc_lvl: level of wsi pyramid to process (should use level 0)
ambiguous_size: number of pixels from boundary likely to contain 'border nuclei'
wsi_inst_info: dictionary of instance-level results
inf_tile_shape: dimensions of tiles during inference [h,w]
proc_tile_shape: dimensions of tiles during post processing [h,w]
model_path: path to model checkpoints
input_dir: input directory containing WSIs
output_dir: output directory where files will be saved
cache_dir: directory where temporary files will be save (ensure around 100GB space)
return_probs (bool): whether to return the per class probabilities of each nucleus
batch_size (int): batch size to use during inference
postproc_workers (int): number of parallel post processing workers
input_tensor_names: defines what it the input in the computational graph
output_tensor_names: defines what it the output in the computational graph
"""
def __init__(self):
self.nr_types = None
self.patch_input_shape = [256, 256]
self.patch_output_shape = [164, 164]
self.input_norm = True
self.wsi_proc_lvl = 0
self.ambiguous_size = 128
self.wsi_inst_info = {}
self.input_tensor_names = ["images"]
self.output_tensor_names = ["predmap-coded"]
def _parse_args(self, args):
"""Parse CLI arguments
Args:
args: command line interface arguments
"""
# tile inference shape
self.inf_tile_shape = [
int(args["--inf_tile_shape"]),
int(args["--inf_tile_shape"]),
]
# tile post processing shape
self.proc_tile_shape = [
int(args["--proc_tile_shape"]),
int(args["--proc_tile_shape"]),
]
self.model_path = args["--model"]
self.input_dir = os.path.abspath(args["--input_dir"])
self.output_dir = args["--output_dir"]
# temporarily stores probability map as memory map in cache - ensure enough space
self.cache_dir = args["--cache_dir"]
self.return_probs = args["--return_probs"]
self.batch_size = int(args["--batch_size"])
self.postproc_workers = int(args["--postproc_workers"])
# get the model name from the checkpoint
model_name = os.path.basename(self.model_path)
self.model_name = model_name.split('.')[0]
if self.model_name == 'pannuke':
self.nr_types = 6
elif self.model_name == 'monusac':
self.nr_types = 5
def get_model(self):
"""Get the model architecture"""
model_constructor = importlib.import_module("hover.model.graph")
model_constructor = model_constructor.Model_NP_HV
return model_constructor # NOTE return alias, not object
def __run_inference(self, patch_top_left_list, tile_idx, nr_tiles):
"""Get the raw predictions for a set of input patch coordinates
Args:
patch_top_left_list: top left coordinates of patches to process
tile_idx: index of tile that patches have been extracted from
nr_tiles: total number of tiles in the WSI
Return:
pred_list: list of patch-level predictions
"""
cache_tile = np.load("%s/cache_tile.npy" % self.cache_dir)
cache_tile = np.array(cache_tile)
sub_patches = []
patches_info = []
# generating subpatches from orginal
for patch_coord in patch_top_left_list:
win = cache_tile[
patch_coord[0] : patch_coord[0] + self.patch_input_shape_tmp[0],
patch_coord[1] : patch_coord[1] + self.patch_input_shape_tmp[0],
]
if self.factor_40_base > 1:
# cv.INTER_LINEAR is good for zooming
win = cv2.resize(
win, (self.patch_input_shape[1], self.patch_input_shape[0]), cv2.INTER_LINEAR)
patch_coord *= self.factor_40_base
sub_patches.append(win)
patches_info.append(patch_coord)
pred_list = deque()
batch_count = len(sub_patches)
nr_proc = 0 # used for logging number of processed batches
while len(sub_patches) > self.batch_size:
mini_batch = sub_patches[: self.batch_size]
sub_patches = sub_patches[self.batch_size:]
batch_info = patches_info[: self.batch_size]
patches_info = patches_info[self.batch_size:]
batch_output = self.predictor(mini_batch)[0]
batch_output = np.split(batch_output, self.batch_size, axis=0)
batch_info = np.split(np.array(batch_info), self.batch_size, axis=0)
batch_output_combined = list(
zip(batch_info, batch_output))
pred_list.extend(batch_output_combined)
###
nr_proc += self.batch_size
sys.stdout.write("\rProcessing Batch (%d/%d) of Tile (%d/%d)" %
(nr_proc, batch_count, tile_idx+1, nr_tiles))
sys.stdout.flush()
if len(sub_patches) != 0:
batch_output = self.predictor(sub_patches)[0]
batch_output = np.split(batch_output, len(sub_patches), axis=0)
batch_info = np.split(np.array(patches_info), len(patches_info), axis=0)
batch_output_combined = list(
zip(batch_info, batch_output))
pred_list.extend(batch_output_combined)
###
nr_proc += len(sub_patches)
sys.stdout.write("\rProcessing Batch (%d/%d) of Tile (%d/%d)" %
(nr_proc, batch_count, tile_idx+1, nr_tiles))
sys.stdout.flush()
return pred_list
def __select_valid_patches(self, patch_info_list, has_output_info=True):
"""Select valid patches coordinates from a set of input coordinates
Args:
patch_info_list: input patch coordinates
has_output_info: whether output coordintes are available
Return:
sub_patch_info_list: list of valid patch coordinates
"""
down_sample_ratio = self.wsi_mask.shape[0] / self.wsi_proc_shape[0]
selected_indices = []
for idx in range(patch_info_list.shape[0]):
patch_info = patch_info_list[idx]
patch_info = np.squeeze(patch_info)
# get the box at corresponding mag of the mask
if has_output_info:
output_bbox = patch_info[1] * down_sample_ratio
else:
output_bbox = patch_info * down_sample_ratio
output_bbox = np.rint(output_bbox).astype(np.int64)
# coord of the output of the patch (i.e centre regions)
output_roi = self.wsi_mask[
output_bbox[0][0] : output_bbox[1][0],
output_bbox[0][1] : output_bbox[1][1],
]
if np.sum(output_roi) > 0:
selected_indices.append(idx)
sub_patch_info_list = patch_info_list[selected_indices]
return sub_patch_info_list
def __gen_prediction(self, tile_info_list, patch_info_list):
"""Generate prediction for a set of input tiles
Args:
tile_info_list: coordinates of input tiles
patch_info_list: coordinates of input patches
"""
wsi_pred_map_mmap_path = "%s/prob_map.npy" % self.cache_dir
masking = lambda x, a, b: (a <= x) & (x <= b)
nr_tiles = tile_info_list.shape[0]
for idx in range(nr_tiles):
tile_info = tile_info_list[idx]
# select patch basing on top left coordinate of input
start_coord = tile_info[0, 0]
end_coord = tile_info[0, 1] - self.patch_input_shape_tmp
selection = masking(
patch_info_list[:, 0, 0, 0], start_coord[0], end_coord[0]
) & masking(patch_info_list[:, 0, 0, 1], start_coord[1], end_coord[1])
tile_patch_info_list = np.array(patch_info_list[selection])
# further select only the patches within the provided mask
tile_patch_info_list = self.__select_valid_patches(tile_patch_info_list)
# there no valid patches, so flush 0 and skip
if tile_patch_info_list.shape[0] == 0:
assemble_and_flush(wsi_pred_map_mmap_path, tile_info,
self.factor_40_base, None)
continue
# change the coordinates from wrt slide to wrt tile
tile_patch_info_list -= tile_info[:, 0]
tile_data = self.wsi_handler.read_region(
tile_info[0][0][::-1],
self.wsi_proc_lvl,
(tile_info[0][1] - tile_info[0][0])[::-1],
)
tile_data = np.array(tile_data)[..., :3]
np.save("%s/cache_tile.npy" % self.cache_dir, tile_data)
patch_output_list = self.__run_inference(
tile_patch_info_list[:, 0, 0], idx, nr_tiles
)
assemble_and_flush(wsi_pred_map_mmap_path, tile_info,
self.factor_40_base, patch_output_list)
return
def __dispatch_post_processing(self, tile_info_list, callback):
"""Initialise post processing
Args:
tile_info_list: coordinate information of WSI tiles
callback: type of post processing callback used
"""
proc_pool = None
if self.postproc_workers > 0:
proc_pool = Pool(processes=self.postproc_workers)
wsi_pred_map_mmap_path = "%s/prob_map.npy" % self.cache_dir
for idx in list(range(tile_info_list.shape[0])):
tile_tl = tile_info_list[idx][0] * self.factor_40_base
tile_br = tile_info_list[idx][1] * self.factor_40_base
tile_info = (idx, tile_tl, tile_br)
func_kwargs = {
"nr_types": self.nr_types,
"return_dict": True,
"return_probs": self.return_probs,
}
if proc_pool is not None:
proc_pool.apply_async(
post_proc_para_wrapper,
callback=callback,
args=(wsi_pred_map_mmap_path, tile_info, func_kwargs,),
)
else:
results = post_proc_para_wrapper(
wsi_pred_map_mmap_path, tile_info, func_kwargs
)
callback(results)
if proc_pool is not None:
proc_pool.close()
proc_pool.join()
return
def load_wsi(self, filename):
"""Load a WSI and get information"""
wsi_ext = filename.split(".")[-1]
self.wsi_handler = get_wsi_handler(filename, wsi_ext)
self.wsi_ds_lvl = self.wsi_handler.metadata["level_downsamples"][
self.wsi_proc_lvl
]
self.ds_factor_mask = (
self.wsi_handler.metadata["magnification"][self.wsi_proc_lvl] / 1.25
)
base_mag = self.wsi_handler.metadata["base_mag"]
# if scanned at 20x, this will be 2
self.factor_40_base = int(round(40.0 / base_mag))
self.wsi_proc_shape = self.wsi_handler.metadata["level_dims"][self.wsi_proc_lvl]
self.wsi_proc_shape = np.array(self.wsi_proc_shape[::-1]) # to Y, X
def process_wsi(self, filename):
"""Process an individual WSI. This function will:
1) Load the WSI
2) Read and save low resolution thumbnail
3) Generate and save a tissue mask
4) Initialise result memory maps
5) Run inference in tissue region
6) Run post processing
7) Save the results as a JSON file
Args:
filename: WSI path to process
"""
self.full_filename = self.input_dir + "/" + filename
print(self.full_filename)
self.load_wsi(self.full_filename)
# read wsi at low res for tissue seg (fixed at 1.25x obj mag)
self.wsi_lowres = self.wsi_handler.data["src"]["img"]
cv2.imwrite(
"%s/thumbnail.png" % self.output_dir_wsi,
cv2.cvtColor(self.wsi_lowres, cv2.COLOR_BGR2RGB),
)
# get tissue mask
self.wsi_mask = get_tissue_mask(self.wsi_lowres)
cv2.imwrite("%s/mask.png" % self.output_dir_wsi, self.wsi_mask * 255)
# Initialise memory maps to prevent large arrays being stored in RAM
out_ch = self.nr_types + 3 # nr types + instance channels
self.wsi_prob_map_mmap = np.lib.format.open_memmap(
"%s/prob_map.npy" % self.cache_dir,
mode="w+",
shape=tuple(self.wsi_proc_shape*self.factor_40_base) + (out_ch,),
dtype=np.float32,
)
# uncomment below if plan to load prob map straight from cache (for debugging!)
# self.wsi_prob_map_mmap = np.load(
# "%s/prob_map.npy" % self.cache_dir, mmap_mode='r')
self.wsi_inst_map = np.lib.format.open_memmap(
"%s/pred_inst.npy" % self.cache_dir,
mode="w+",
shape=tuple(self.wsi_proc_shape*self.factor_40_base),
dtype=np.int32,
)
# ------------------------------RUN INFERENCE--------------------------------
start = time.perf_counter() # init inference timer
# the WSI is processed tile by tile. Get the coordinates of tiles and patches
patch_input_shape_tmp = np.array(self.patch_input_shape)
self.patch_input_shape_tmp = (
patch_input_shape_tmp/self.factor_40_base).astype('int')
patch_stride = np.array(self.patch_output_shape)
patch_stride = (patch_stride/self.factor_40_base).astype('int')
# make tile smaller if not at 40x. We extract more patches and resize if 20x
inf_tile_input_shape = np.array(
self.inf_tile_shape) / self.factor_40_base
inf_tile_info_list, patch_info_list = get_tile_patch_info(
self.wsi_proc_shape,
inf_tile_input_shape,
self.patch_input_shape_tmp,
patch_stride,
)
# get the predictions of each patch in a tile and save to memory map
self.__gen_prediction(inf_tile_info_list, patch_info_list)
print(". All Tiles Processed!")
end = time.perf_counter()
print("Inference Time:", round(end-start), 'secs')
# --------------------------RUN POST PROCESSING-----------------------------
# * define post processing callbacks - can only receive one argument
def postproc_standard_callback(args):
"""Standard post processing callback.
Args:
args: input to the callback
"""
results, pos_args = args
run_idx, tile_tl, tile_br = pos_args
pred_inst, inst_info_dict = results
if len(inst_info_dict) == 0:
pbar.update()
return
top_left = pos_args[1][::-1]
wsi_max_id = 0
if len(self.wsi_inst_info) > 0:
wsi_max_id = max(self.wsi_inst_info.keys())
for inst_id, inst_info in inst_info_dict.items():
# now correct the coordinate wrt wsi
inst_info["contour"] += (top_left)
inst_info["centroid"] += (top_left)
self.wsi_inst_info[inst_id + wsi_max_id] = inst_info
pred_inst[pred_inst > 0] += wsi_max_id
self.wsi_inst_map[
tile_tl[0]: tile_br[0], tile_tl[1]: tile_br[1]
] = pred_inst
pbar.update()
return
def postproc_fixborder_callback(args):
"""Callback for fixing incorrectly processed nuclei at the boundary.
For this, we define an `ambiguous region`, where predicted nuclei that are
a certain number of pixels from the border are considered `border nuclei`.
This callback replaces border nuclei with valid predictions.
Args:
args: input to the callback
"""
results, pos_args = args
run_idx, tile_tl, tile_br = pos_args
pred_inst, inst_info_dict = results
if len(inst_info_dict) == 0:
pbar.update()
return
top_left = pos_args[1][::-1]
wsi_max_id = 0
if len(self.wsi_inst_info) > 0:
wsi_max_id = max(self.wsi_inst_info.keys())
# * exclude ambiguous output from old prediction map
# check 1 pix of 4 edges to find nuclei split at boundary
roi_inst = self.wsi_inst_map[
tile_tl[0]: tile_br[0], tile_tl[1]: tile_br[1]
]
roi_inst = np.copy(roi_inst)
roi_edge = np.concatenate(
[roi_inst[[0, -1], :].flatten(), roi_inst[:, [0, -1]].flatten()]
)
roi_boundary_inst_list = np.unique(
roi_edge)[1:] # exclude background
roi_inner_inst_list = np.unique(roi_inst)[1:]
roi_inner_inst_list = np.setdiff1d(
roi_inner_inst_list, roi_boundary_inst_list, assume_unique=True
)
roi_inst = remove_inst(roi_inst, roi_inner_inst_list)
self.wsi_inst_map[
tile_tl[0]: tile_br[0], tile_tl[1]: tile_br[1]
] = roi_inst
for inst_id in roi_inner_inst_list:
self.wsi_inst_info.pop(inst_id, None)
# * exclude unambiguous output from new prediction map
# check 1 pix of 4 edges to find nuclei split at boundary
roi_edge = pred_inst[roi_inst > 0] # remove all overlap
boundary_inst_list = np.unique(
roi_edge) # no background to exclude
inner_inst_list = np.unique(pred_inst)[1:]
inner_inst_list = np.setdiff1d(
inner_inst_list, boundary_inst_list, assume_unique=True
)
pred_inst = remove_inst(pred_inst, boundary_inst_list)
# * proceed to overwrite
for inst_id in inner_inst_list:
inst_info = inst_info_dict[inst_id]
# now correct the coordinate wrt to wsi
inst_info["contour"] += (top_left)
inst_info["centroid"] += (top_left)
self.wsi_inst_info[inst_id + wsi_max_id] = inst_info
pred_inst[pred_inst > 0] += wsi_max_id
pred_inst = roi_inst + pred_inst
self.wsi_inst_map[
tile_tl[0]: tile_br[0], tile_tl[1]: tile_br[1]
] = pred_inst
pbar.update()
return
start = time.perf_counter() # init post processing timer
proc_tile_shape = (np.array(self.proc_tile_shape)).astype(np.int64)
proc_tile_coords = get_tile_info(
self.wsi_proc_shape, proc_tile_shape, self.ambiguous_size
)
proc_grid_info, proc_boundary_info, proc_cross_info = proc_tile_coords
proc_grid_info = self.__select_valid_patches(proc_grid_info, False)
proc_boundary_info = self.__select_valid_patches(proc_boundary_info, False)
proc_cross_info = self.__select_valid_patches(proc_cross_info, False)
pbar_creator = lambda x, y: tqdm.tqdm(
desc=y, leave=True, total=int(len(x)), ncols=80, ascii=True, position=0
)
pbar = pbar_creator(proc_grid_info, "Post Proc Phase 1")
self.__dispatch_post_processing(proc_grid_info, postproc_standard_callback)
pbar.close()
pbar = pbar_creator(proc_boundary_info, "Post Proc Phase 2")
self.__dispatch_post_processing(proc_boundary_info, postproc_fixborder_callback)
pbar.close()
pbar = pbar_creator(proc_cross_info, "Post Proc Phase 3")
self.__dispatch_post_processing(proc_cross_info, postproc_fixborder_callback)
pbar.close()
# ! save as JSON because it isn't feasible to save the WSI at highest resolution
json_dict = {}
for inst_id, inst_info in self.wsi_inst_info.items():
new_inst_info = {}
for info_name, info_value in inst_info.items():
# save results at scan resolution
if info_name == 'centroid' or info_name == 'contour':
if self.factor_40_base > 1:
info_value = info_value / self.factor_40_base
info_value = np.round(info_value).astype('int')
# convert to JSON
if isinstance(info_value, np.ndarray):
info_value = info_value.tolist()
new_inst_info[info_name] = info_value
json_dict[int(inst_id)] = new_inst_info
with open(
"%s/nuclei_dict.json" % self.output_dir_wsi, "w"
) as handle:
json.dump(json_dict, handle)
end = time.perf_counter()
print("Post Proc Time:", round(end-start), 'secs')
def load_model(self):
"""Loads the model and checkpoints"""
print("Loading Model...")
model_path = self.model_path
model_constructor = self.get_model()
pred_config = PredictConfig(
model=model_constructor(
self.nr_types,
self.patch_input_shape,
self.patch_output_shape,
self.input_norm,
),
session_init=get_model_loader(model_path),
input_names=self.input_tensor_names,
output_names=self.output_tensor_names,
)
self.predictor = OfflinePredictor(pred_config)
def load_filenames(self):
"""Get the list of all WSI files to process"""
self.file_list = glob.glob("%s/*" % self.input_dir)
self.file_list.sort() # ensure same order
def process_all_files(self):
"""Process each WSI and save results as JSON file"""
if not os.path.exists(self.cache_dir):
rm_n_mkdir(self.cache_dir)
if not os.path.exists(self.output_dir):
rm_n_mkdir(self.output_dir)
for filename in self.file_list:
filename = os.path.basename(filename)
self.basename = os.path.splitext(filename)[0]
self.output_dir_wsi = self.output_dir + "/" + self.basename
# if not os.path.exists(self.output_dir_wsi):
start = time.perf_counter() # init timer
rm_n_mkdir(self.output_dir_wsi)
start_time_total = time.time()
self.process_wsi(filename)
end = time.perf_counter()
print("Overall Time:", round(end-start), 'secs')
# -------------------------------------------------------------------------------------
if __name__ == "__main__":
args = docopt(__doc__, version="HoVer-Net Inference")
print(args)
os.environ["CUDA_VISIBLE_DEVICES"] = args["--gpu"]
# Raise exceptions for invalid / missing arguments
if args["--model"] == None:
raise Exception("A model path must be supplied as an argument with --model.")
if args["--mode"] != "tile" and args["--mode"] != "wsi":
raise Exception('Mode not recognised. Use either "tile" or "wsi"')
if args["--input_dir"] == None:
raise Exception(
"An input directory must be supplied as an argument with --input_dir."
)
if args["--input_dir"] == args["--output_dir"]:
raise Exception(
"Input and output directories should not be the same- otherwise input directory will be overwritten."
)
if args["--mode"] == "tile":
infer = InferTile()
infer._parse_args(args)
infer.load_model()
infer.process_all_files()
elif args["--mode"] == "wsi":
infer = InferWSI()
infer._parse_args(args)
infer.load_model()
infer.load_filenames()
infer.process_all_files()