-
Notifications
You must be signed in to change notification settings - Fork 71
/
test_models.py
998 lines (872 loc) · 31.8 KB
/
test_models.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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
"""Tests for code related to model usage."""
from time import time
import os
import pathlib
import shutil
import cv2
import numpy as np
import pytest
import torch
from tiatoolbox import rcParam
from tiatoolbox.models.abc import ModelABC
from tiatoolbox.models.backbone import get_model
from tiatoolbox.models.classification import CNNPatchModel, CNNPatchPredictor
from tiatoolbox.models.dataset import (
DatasetInfoABC,
PatchDatasetABC,
KatherPatchDataset,
PatchDataset,
WSIPatchDataset,
predefined_preproc_func,
)
from tiatoolbox.utils.misc import download_data, unzip_data, imread, imwrite
from tiatoolbox.wsicore.wsireader import get_wsireader
ON_GPU = False
def _get_temp_folder_path():
"""Return unique temp folder path"""
new_dir = os.path.join(
rcParam["TIATOOLBOX_HOME"], f"test_model_patch_{int(time())}"
)
return new_dir
def test_create_backbone():
"""Test for creating backbone."""
backbones = [
"alexnet",
"resnet18",
"resnet34",
"resnet50",
"resnet101",
"resnext50_32x4d",
"resnext101_32x8d",
"wide_resnet50_2",
"wide_resnet101_2",
"densenet121",
"densenet161",
"densenet169",
"densenet201",
"googlenet",
"mobilenet_v2",
"mobilenet_v3_large",
"mobilenet_v3_small",
]
for backbone in backbones:
try:
get_model(backbone, pretrained=False)
except ValueError:
raise AssertionError(f"Model {backbone} failed.")
# test for model not defined
with pytest.raises(ValueError, match=r".*not supported.*"):
get_model("secret_model-kather100k", pretrained=False)
def test_DatasetInfo():
"""Test for kather patch dataset."""
# test defining a subclass of dataset info but not defining
# enforcing attributes - should crash
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(DatasetInfoABC):
def __init__(self):
self.a = "a"
# intentionally create to check error
Proto() # skipcq
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(DatasetInfoABC):
def __init__(self):
self.inputs = "a"
# intentionally create to check error
Proto() # skipcq
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(DatasetInfoABC):
def __init__(self):
self.inputs = "a"
self.labels = "a"
# intentionally create to check error
Proto() # skipcq
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(DatasetInfoABC):
def __init__(self):
self.inputs = "a"
self.label_names = "a"
# intentionally create to check error
Proto() # skipcq
# test kather with default init
dataset = KatherPatchDataset()
# kather with default data path skip download
dataset = KatherPatchDataset()
# pytest for not exist dir
with pytest.raises(
ValueError,
match=r".*not exist.*",
):
_ = KatherPatchDataset(save_dir_path="unknown_place")
# save to temporary location
save_dir_path = _get_temp_folder_path()
# remove previously generated data
if os.path.exists(save_dir_path):
shutil.rmtree(save_dir_path, ignore_errors=True)
url = (
"https://zenodo.org/record/53169/files/"
"Kather_texture_2016_image_tiles_5000.zip"
)
save_zip_path = os.path.join(save_dir_path, "Kather.zip")
download_data(url, save_zip_path)
unzip_data(save_zip_path, save_dir_path)
extracted_dir = os.path.join(save_dir_path, "Kather_texture_2016_image_tiles_5000/")
dataset = KatherPatchDataset(save_dir_path=extracted_dir)
assert dataset.inputs is not None
assert dataset.labels is not None
assert dataset.label_names is not None
assert len(dataset.inputs) == len(dataset.labels)
# to actually get the image, we feed it to PatchDataset
actual_ds = PatchDataset(dataset.inputs, dataset.labels)
sample_patch = actual_ds[100]
assert isinstance(sample_patch["image"], np.ndarray)
assert sample_patch["label"] is not None
# remove generated data
shutil.rmtree(save_dir_path, ignore_errors=True)
shutil.rmtree(rcParam["TIATOOLBOX_HOME"])
def test_PatchDatasetpath_imgs(_sample_patch1, _sample_patch2):
"""Test for patch dataset with a list of file paths as input."""
size = (224, 224, 3)
dataset = PatchDataset([pathlib.Path(_sample_patch1), pathlib.Path(_sample_patch2)])
for _, sample_data in enumerate(dataset):
sampled_img_shape = sample_data["image"].shape
assert (
sampled_img_shape[0] == size[0]
and sampled_img_shape[1] == size[1]
and sampled_img_shape[2] == size[2]
)
def test_PatchDatasetlist_imgs():
"""Test for patch dataset with a list of images as input."""
size = (5, 5, 3)
img = np.random.randint(0, 255, size=size)
list_imgs = [img, img, img]
dataset = PatchDataset(list_imgs)
dataset.preproc_func = lambda x: x
for _, sample_data in enumerate(dataset):
sampled_img_shape = sample_data["image"].shape
assert (
sampled_img_shape[0] == size[0]
and sampled_img_shape[1] == size[1]
and sampled_img_shape[2] == size[2]
)
# test for changing to another preproc
dataset.preproc_func = lambda x: x - 10
item = dataset[0]
assert np.sum(item["image"] - (list_imgs[0] - 10)) == 0
# test for loading npy
save_dir_path = _get_temp_folder_path()
# remove previously generated data
if os.path.exists(save_dir_path):
shutil.rmtree(save_dir_path, ignore_errors=True)
os.makedirs(save_dir_path)
np.save(
os.path.join(save_dir_path, "sample2.npy"), np.random.randint(0, 255, (4, 4, 3))
)
imgs = [
os.path.join(save_dir_path, "sample2.npy"),
]
_ = PatchDataset(imgs)
assert imgs[0] is not None
# test for path object
imgs = [
pathlib.Path(os.path.join(save_dir_path, "sample2.npy")),
]
_ = PatchDataset(imgs)
shutil.rmtree(save_dir_path)
def test_PatchDatasetarray_imgs():
"""Test for patch dataset with a numpy array of a list of images."""
size = (5, 5, 3)
img = np.random.randint(0, 255, size=size)
list_imgs = [img, img, img]
labels = [1, 2, 3]
array_imgs = np.array(list_imgs)
# test different setter for label
dataset = PatchDataset(array_imgs, labels=labels)
an_item = dataset[2]
assert an_item["label"] == 3
dataset = PatchDataset(array_imgs, labels=None)
an_item = dataset[2]
assert "label" not in an_item
dataset = PatchDataset(array_imgs)
for _, sample_data in enumerate(dataset):
sampled_img_shape = sample_data["image"].shape
assert (
sampled_img_shape[0] == size[0]
and sampled_img_shape[1] == size[1]
and sampled_img_shape[2] == size[2]
)
def test_PatchDataset_crash():
"""Test to make sure patch dataset crashes with incorrect input."""
# all below examples below should fail when input to PatchDataset
# not supported input type
imgs = {"a": np.random.randint(0, 255, (4, 4, 4))}
with pytest.raises(
ValueError, match=r".*Input must be either a list/array of images.*"
):
_ = PatchDataset(imgs)
# ndarray of mixed dtype
imgs = np.array([np.random.randint(0, 255, (4, 5, 3)), "Should crash"])
with pytest.raises(ValueError, match="Provided input array is non-numerical."):
_ = PatchDataset(imgs)
# ndarrays of NHW images
imgs = np.random.randint(0, 255, (4, 4, 4))
with pytest.raises(ValueError, match=r".*array of images of the form NHWC.*"):
_ = PatchDataset(imgs)
# list of ndarrays with different sizes
imgs = [
np.random.randint(0, 255, (4, 4, 3)),
np.random.randint(0, 255, (4, 5, 3)),
]
with pytest.raises(ValueError, match="Images must have the same dimensions."):
_ = PatchDataset(imgs)
# list of ndarrays with HW and HWC mixed up
imgs = [
np.random.randint(0, 255, (4, 4, 3)),
np.random.randint(0, 255, (4, 4)),
]
with pytest.raises(
ValueError, match="Each sample must be an array of the form HWC."
):
_ = PatchDataset(imgs)
# list of mixed dtype
imgs = [np.random.randint(0, 255, (4, 4, 3)), "you_should_crash_here", 123, 456]
with pytest.raises(
ValueError,
match="Input must be either a list/array of images or a list of "
"valid image paths.",
):
_ = PatchDataset(imgs)
# list of mixed dtype
imgs = ["you_should_crash_here", 123, 456]
with pytest.raises(
ValueError,
match="Input must be either a list/array of images or a list of "
"valid image paths.",
):
_ = PatchDataset(imgs)
# list not exist paths
with pytest.raises(
ValueError,
match=r".*valid image paths.*",
):
_ = PatchDataset(["img.npy"])
# ** test different extenstion parser
# save dummy data to temporary location
save_dir_path = _get_temp_folder_path()
# remove prev generated data
if os.path.exists(save_dir_path):
shutil.rmtree(save_dir_path, ignore_errors=True)
os.makedirs(save_dir_path)
torch.save({"a": "a"}, os.path.join(save_dir_path, "sample1.tar"))
np.save(
os.path.join(save_dir_path, "sample2.npy"), np.random.randint(0, 255, (4, 4, 3))
)
imgs = [
os.path.join(save_dir_path, "sample1.tar"),
os.path.join(save_dir_path, "sample2.npy"),
]
with pytest.raises(
ValueError,
match=r"Can not load data of .*",
):
_ = PatchDataset(imgs)
shutil.rmtree(rcParam["TIATOOLBOX_HOME"])
# preproc func for not defined dataset
with pytest.raises(
ValueError,
match=r".* preprocessing .* does not exist.",
):
predefined_preproc_func("secret-dataset")
def test_WSIPatchDataset(_sample_wsi_dict):
"""A test for creation and bare output."""
# convert to pathlib Path to prevent wsireader complaint
_mini_wsi_svs = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_svs"])
_mini_wsi_jpg = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_jpg"])
_mini_wsi_msk = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_msk"])
def reuse_init(img_path=_mini_wsi_svs, **kwargs):
"""Testing function."""
return WSIPatchDataset(img_path=_mini_wsi_svs, **kwargs)
def reuse_init_wsi(**kwargs):
"""Testing function."""
return reuse_init(mode="wsi", **kwargs)
# test for ABC validate
with pytest.raises(
ValueError, match=r".*inputs should be a list of patch coordinates.*"
):
# intentionally create to check error
# skipcq
class Proto(PatchDatasetABC):
def __init__(self):
super().__init__()
self.inputs = "CRASH"
self._check_input_integrity("wsi")
# skipcq
def __getitem__(self, idx):
pass
# intentionally create to check error
Proto() # skipcq
# invalid path input
with pytest.raises(ValueError, match=r".*`img_path` must be a valid file path.*"):
WSIPatchDataset(
img_path="aaaa",
mode="wsi",
patch_size=[512, 512],
stride_size=[256, 256],
auto_get_mask=False,
)
# invalid mask path input
with pytest.raises(ValueError, match=r".*`mask_path` must be a valid file path.*"):
WSIPatchDataset(
img_path=_mini_wsi_svs,
mask_path="aaaa",
mode="wsi",
patch_size=[512, 512],
stride_size=[256, 256],
resolution=1.0,
units="mpp",
auto_get_mask=False,
)
# invalid mode
with pytest.raises(ValueError):
reuse_init(mode="X")
# invalid patch
with pytest.raises(ValueError):
reuse_init()
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=[512, 512, 512])
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=[512, "a"])
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=512)
# invalid stride
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=[512, 512], stride_size=[512, "a"])
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=[512, 512], stride_size=[512, 512, 512])
# negative
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=[512, -512], stride_size=[512, 512])
with pytest.raises(ValueError):
reuse_init_wsi(patch_size=[512, 512], stride_size=[512, -512])
# * for wsi
# dummy test for analysing the output
# stride and patch size should be as expected
patch_size = [512, 512]
stride_size = [256, 256]
ds = reuse_init_wsi(
patch_size=patch_size,
stride_size=stride_size,
resolution=1.0,
units="mpp",
auto_get_mask=False,
)
reader = get_wsireader(_mini_wsi_svs)
# tiling top to bottom, left to right
ds_roi = ds[2]["image"]
step_idx = 2 # manually calibrate
start = (step_idx * stride_size[1], 0)
end = (start[0] + patch_size[0], start[1] + patch_size[1])
rd_roi = reader.read_bounds(
start + end, resolution=1.0, units="mpp", coord_space="resolution"
)
correlation = np.corrcoef(
cv2.cvtColor(ds_roi, cv2.COLOR_RGB2GRAY).flatten(),
cv2.cvtColor(rd_roi, cv2.COLOR_RGB2GRAY).flatten(),
)
assert ds_roi.shape[0] == rd_roi.shape[0]
assert ds_roi.shape[1] == rd_roi.shape[1]
assert np.min(correlation) > 0.9, correlation
# test creation with auto mask gen and input mask
ds = reuse_init_wsi(
patch_size=patch_size,
stride_size=stride_size,
resolution=1.0,
units="mpp",
auto_get_mask=True,
)
assert len(ds) > 0
ds = WSIPatchDataset(
img_path=_mini_wsi_svs,
mask_path=_mini_wsi_msk,
mode="wsi",
patch_size=[512, 512],
stride_size=[256, 256],
auto_get_mask=False,
resolution=1.0,
units="mpp",
)
negative_mask = imread(_mini_wsi_msk)
negative_mask = np.zeros_like(negative_mask)
imwrite("negative_mask.png", negative_mask)
with pytest.raises(ValueError, match=r".*No coordinate remain after tiling.*"):
ds = WSIPatchDataset(
img_path=_mini_wsi_svs,
mask_path="negative_mask.png",
mode="wsi",
patch_size=[512, 512],
stride_size=[256, 256],
auto_get_mask=False,
resolution=1.0,
units="mpp",
)
shutil.rmtree("negative_mask.png", ignore_errors=True)
# * for tile
reader = get_wsireader(_mini_wsi_jpg)
tile_ds = WSIPatchDataset(
img_path=_mini_wsi_jpg,
mode="tile",
patch_size=patch_size,
stride_size=stride_size,
auto_get_mask=False,
)
step_idx = 3 # manually calibrate
start = (step_idx * stride_size[1], 0)
end = (start[0] + patch_size[0], start[1] + patch_size[1])
roi2 = reader.read_bounds(
start + end, resolution=1.0, units="baseline", coord_space="resolution"
)
roi1 = tile_ds[3]["image"] # match with step_idx
correlation = np.corrcoef(
cv2.cvtColor(roi1, cv2.COLOR_RGB2GRAY).flatten(),
cv2.cvtColor(roi2, cv2.COLOR_RGB2GRAY).flatten(),
)
assert roi1.shape[0] == roi2.shape[0]
assert roi1.shape[1] == roi2.shape[1]
assert np.min(correlation) > 0.9, correlation
def test_PatchDataset_abc():
# test missing definition for abstract
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(PatchDatasetABC):
# skipcq
def __init__(self):
super().__init__()
# crash due to not define __getitem__
Proto() # skipcq
# skipcq
class Proto(PatchDatasetABC):
# skipcq
def __init__(self):
super().__init__()
# skipcq
def __getitem__(self, idx):
pass
ds = Proto() # skipcq
# test setter and getter
assert ds.preproc_func(1) == 1
ds.preproc_func = lambda x: x - 1
assert ds.preproc_func(1) == 0
assert ds.preproc(1) == 1, "Must be unchanged!"
ds.preproc_func = None
assert ds.preproc_func(2) == 2
# test assign uncallable to preproc_func/postproc_func
with pytest.raises(ValueError, match=r".*callable*"):
ds.preproc_func = 1
def test_model_abc():
"""Test API in model ABC."""
# test missing definition for abstract
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(ModelABC):
# skipcq
def __init__(self):
super().__init__()
# crash due to not define forward and infer_batch
Proto() # skipcq
with pytest.raises(TypeError):
# intentionally create to check error
# skipcq
class Proto(ModelABC):
# skipcq
def __init__(self):
super().__init__()
@staticmethod
# skipcq
def infer_batch():
pass
# crash due to not define forward
Proto() # skipcq
# intentionally create to check error
# skipcq
class Proto(ModelABC):
# skipcq
def __init__(self):
super().__init__()
@staticmethod
def postproc(image):
return image - 2
# skipcq
def forward(self):
pass
@staticmethod
# skipcq
def infer_batch():
pass
model = Proto() # skipcq
# test assign uncallable to preproc_func/postproc_func
with pytest.raises(ValueError, match=r".*callable*"):
model.postproc_func = 1
with pytest.raises(ValueError, match=r".*callable*"):
model.preproc_func = 1
# test setter/getter/inital of preproc_func/postproc_func
assert model.preproc_func(1) == 1
model.preproc_func = lambda x: x - 1
assert model.preproc_func(1) == 0
assert model.preproc(1) == 1, "Must be unchanged!"
assert model.postproc(1) == -1, "Must be unchanged!"
model.preproc_func = None
assert model.preproc_func(2) == 2
# repeat the setter test for postproc
assert model.postproc_func(2) == 0
model.postproc_func = lambda x: x - 1
assert model.postproc_func(1) == 0
assert model.preproc(1) == 1, "Must be unchanged!"
assert model.postproc(2) == 0, "Must be unchanged!"
# coverage setter check
model.postproc_func = None
assert model.postproc_func(2) == 0
def test_predictor_crash():
"""Test for crash when making predictor."""
# without providing any model
with pytest.raises(ValueError, match=r"Must provide.*"):
CNNPatchPredictor()
# provide wrong unknown pretrained model
with pytest.raises(ValueError, match=r"Pretrained .* does not exist"):
CNNPatchPredictor(pretrained_model="secret_model-kather100k")
# provide wrong model of unknown type, deprecated later with type hint
with pytest.raises(ValueError, match=r".*must be a string.*"):
CNNPatchPredictor(pretrained_model=123)
# test predict crash
predictor = CNNPatchPredictor(pretrained_model="resnet18-kather100k", batch_size=32)
with pytest.raises(ValueError, match=r".*not a valid mode.*"):
predictor.predict("aaa", mode="random")
# remove previously generated data
if os.path.exists("output"):
shutil.rmtree("output", ignore_errors=True)
with pytest.raises(ValueError, match=r".*must be a list of file paths.*"):
predictor.predict("aaa", mode="wsi")
# remove previously generated data
if os.path.exists("output"):
shutil.rmtree("output", ignore_errors=True)
with pytest.raises(ValueError, match=r".*masks.*!=.*imgs.*"):
predictor.predict([1, 2, 3], masks=[1, 2], mode="wsi")
with pytest.raises(ValueError, match=r".*labels.*!=.*imgs.*"):
predictor.predict([1, 2, 3], labels=[1, 2], mode="patch")
# remove previously generated data
if os.path.exists("output"):
shutil.rmtree("output", ignore_errors=True)
def test_patch_predictor_api(_sample_patch1, _sample_patch2):
"""Helper function to get the model output using API 1."""
# convert to pathlib Path to prevent reader complaint
inputs = [pathlib.Path(_sample_patch1), pathlib.Path(_sample_patch2)]
predictor = CNNPatchPredictor(pretrained_model="resnet18-kather100k", batch_size=1)
# don't run test on GPU
output = predictor.predict(
inputs,
on_gpu=ON_GPU,
)
assert sorted(list(output.keys())) == ["predictions"]
assert len(output["predictions"]) == 2
output = predictor.predict(
inputs,
labels=[1, "a"],
return_labels=True,
on_gpu=ON_GPU,
)
assert sorted(list(output.keys())) == sorted(["labels", "predictions"])
assert len(output["predictions"]) == len(output["labels"])
assert output["labels"] == [1, "a"]
output = predictor.predict(
inputs,
return_probabilities=True,
on_gpu=ON_GPU,
)
assert sorted(list(output.keys())) == sorted(["predictions", "probabilities"])
assert len(output["predictions"]) == len(output["probabilities"])
output = predictor.predict(
inputs,
return_probabilities=True,
labels=[1, "a"],
return_labels=True,
on_gpu=ON_GPU,
)
assert sorted(list(output.keys())) == sorted(
["labels", "predictions", "probabilities"]
)
assert len(output["predictions"]) == len(output["labels"])
assert len(output["predictions"]) == len(output["probabilities"])
# test saving output, should have no effect
output = predictor.predict(
inputs,
on_gpu=ON_GPU,
save_dir="special_dir_not_exist",
)
assert not os.path.isdir("special_dir_not_exist")
# test loading user weight
pretrained_weight_url = (
"https://tiatoolbox.dcs.warwick.ac.uk/models/pc/resnet18-kather100k.pth"
)
save_dir_path = _get_temp_folder_path()
# remove prev generated data
if os.path.exists(save_dir_path):
shutil.rmtree(save_dir_path, ignore_errors=True)
os.makedirs(save_dir_path)
pretrained_weight = os.path.join(
rcParam["TIATOOLBOX_HOME"],
"tmp_pretrained_weigths",
"resnet18-kather100k.pth",
)
download_data(pretrained_weight_url, pretrained_weight)
predictor = CNNPatchPredictor(
pretrained_model="resnet18-kather100k",
pretrained_weight=pretrained_weight,
batch_size=1,
)
# --- test different using user model
model = CNNPatchModel(backbone="resnet18", num_classes=9)
# test prediction
predictor = CNNPatchPredictor(model=model, batch_size=1, verbose=False)
output = predictor.predict(
inputs,
return_probabilities=True,
labels=[1, "a"],
return_labels=True,
on_gpu=ON_GPU,
)
assert sorted(list(output.keys())) == sorted(
["labels", "predictions", "probabilities"]
)
assert len(output["predictions"]) == len(output["labels"])
assert len(output["predictions"]) == len(output["probabilities"])
def test_wsi_predictor_api(_sample_wsi_dict):
"""Test normal run of wsi predictor."""
# convert to pathlib Path to prevent wsireader complaint
_mini_wsi_svs = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_svs"])
_mini_wsi_jpg = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_jpg"])
_mini_wsi_msk = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_msk"])
patch_size = np.array([224, 224])
predictor = CNNPatchPredictor(pretrained_model="resnet18-kather100k", batch_size=32)
# wrapper to make this more clean
kwargs = dict(
return_probabilities=True,
return_labels=True,
on_gpu=ON_GPU,
patch_size=patch_size,
stride_size=patch_size,
resolution=1.0,
units="baseline",
)
# ! add this test back once the read at `baseline` is fixed
# sanity check, both output should be the same with same resolution read args
wsi_output = predictor.predict(
[_mini_wsi_svs],
masks=[_mini_wsi_msk],
mode="wsi",
**kwargs,
)
tile_output = predictor.predict(
[_mini_wsi_jpg],
masks=[_mini_wsi_msk],
mode="tile",
**kwargs,
)
wpred = np.array(wsi_output[0]["predictions"])
tpred = np.array(tile_output[0]["predictions"])
diff = tpred == wpred
accuracy = np.sum(diff) / np.size(wpred)
assert accuracy > 0.9, np.nonzero(~diff)
# remove previously generated data
save_dir = "model_wsi_output"
if os.path.exists(save_dir):
shutil.rmtree(save_dir, ignore_errors=True)
kwargs = dict(
return_probabilities=True,
return_labels=True,
on_gpu=ON_GPU,
patch_size=patch_size,
stride_size=patch_size,
resolution=0.5,
save_dir=save_dir,
merge_predictions=True, # to test the api coverage
units="mpp",
)
import copy
_kwargs = copy.deepcopy(kwargs)
_kwargs["merge_predictions"] = False
# test reading of multiple whole-slide images
output = predictor.predict(
[_mini_wsi_svs, _mini_wsi_svs],
masks=[_mini_wsi_msk, _mini_wsi_msk],
mode="wsi",
**_kwargs,
)
for output_info in output.values():
assert os.path.exists(output_info["raw"])
assert "merged" not in output_info
if os.path.exists(_kwargs["save_dir"]):
shutil.rmtree(_kwargs["save_dir"], ignore_errors=True)
# coverage test
_kwargs = copy.deepcopy(kwargs)
_kwargs["merge_predictions"] = True
# test reading of multiple whole-slide images
output = predictor.predict(
[_mini_wsi_svs, _mini_wsi_svs],
masks=[_mini_wsi_msk, _mini_wsi_msk],
mode="wsi",
**_kwargs,
)
with pytest.raises(ValueError, match=r".*save_dir.*exist.*"):
_kwargs = copy.deepcopy(kwargs)
predictor.predict(
[_mini_wsi_svs, _mini_wsi_svs],
masks=[_mini_wsi_msk, _mini_wsi_msk],
mode="wsi",
**_kwargs,
)
# remove previously generated data
if os.path.exists(_kwargs["save_dir"]):
shutil.rmtree(_kwargs["save_dir"], ignore_errors=True)
if os.path.exists("output"):
shutil.rmtree("output", ignore_errors=True)
# test reading of multiple whole-slide images
_kwargs = copy.deepcopy(kwargs)
_kwargs["save_dir"] = None # default coverage
_kwargs["return_probabilities"] = False
output = predictor.predict(
[_mini_wsi_svs, _mini_wsi_svs],
masks=[_mini_wsi_msk, _mini_wsi_msk],
mode="wsi",
**_kwargs,
)
assert os.path.exists("output")
for output_info in output.values():
assert os.path.exists(output_info["raw"])
assert "merged" in output_info and os.path.exists(output_info["merged"])
# remove previously generated data
if os.path.exists("output"):
shutil.rmtree("output", ignore_errors=True)
def test_wsi_predictor_merge_predictions(_sample_wsi_dict):
"""Test normal run of wsi predictor with merge predictions option."""
# convert to pathlib Path to prevent reader complaint
_mini_wsi_svs = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_svs"])
_mini_wsi_jpg = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_jpg"])
_mini_wsi_msk = pathlib.Path(_sample_wsi_dict["wsi2_4k_4k_msk"])
predictor = CNNPatchPredictor(pretrained_model="resnet18-kather100k", batch_size=1)
kwargs = dict(
return_probabilities=True,
return_labels=True,
on_gpu=ON_GPU,
patch_size=np.array([224, 224]),
stride_size=np.array([224, 224]),
resolution=1.0,
units="baseline",
merge_predictions=True,
)
# sanity check, both output should be the same with same resolution read args
wsi_output = predictor.predict(
[_mini_wsi_svs],
masks=[_mini_wsi_msk],
mode="wsi",
**kwargs,
)
# mockup to change the preproc func and
# force to use the default in merge function
# stil should have the same resuls
kwargs["merge_predictions"] = False
tile_output = predictor.predict(
[_mini_wsi_jpg],
masks=[_mini_wsi_msk],
mode="tile",
**kwargs,
)
merged_tile_output = predictor.merge_predictions(
_mini_wsi_jpg,
tile_output[0],
resolution=kwargs["resolution"],
units=kwargs["units"],
)
tile_output.append(merged_tile_output)
# first make sure nothing breaks with predictions
wpred = np.array(wsi_output[0]["predictions"])
tpred = np.array(tile_output[0]["predictions"])
diff = tpred == wpred
accuracy = np.sum(diff) / np.size(wpred)
assert accuracy > 0.9, np.nonzero(~diff)
merged_wsi = wsi_output[1]
merged_tile = tile_output[1]
# enure shape of merged predictions of tile and wsi input are the same
assert merged_wsi.shape == merged_tile.shape
# ensure consistent predictions between tile and wsi mode
diff = merged_tile == merged_wsi
accuracy = np.sum(diff) / np.size(merged_wsi)
assert accuracy > 0.9, np.nonzero(~diff)
def _test_predictor_output(
inputs,
pretrained_model,
probabilities_check=None,
predictions_check=None,
on_gpu=ON_GPU,
):
"""Test the predictions of multiple models included in tiatoolbox."""
predictor = CNNPatchPredictor(
pretrained_model=pretrained_model, batch_size=32, verbose=False
)
# don't run test on GPU
output = predictor.predict(
inputs,
return_probabilities=True,
return_labels=False,
on_gpu=ON_GPU,
)
predictions = output["predictions"]
probabilities = output["probabilities"]
for idx, probabilities_ in enumerate(probabilities):
probabilities_max = max(probabilities_)
assert (
np.abs(probabilities_max - probabilities_check[idx]) <= 1e-6
and predictions[idx] == predictions_check[idx]
), (
pretrained_model,
probabilities_max,
probabilities_check[idx],
predictions[idx],
predictions_check[idx],
)
def test_patch_predictor_output(_sample_patch1, _sample_patch2):
"""Test the output of patch prediction models."""
inputs = [pathlib.Path(_sample_patch1), pathlib.Path(_sample_patch2)]
pretrained_info = {
"alexnet-kather100k": [1.0, 0.9999735355377197],
"resnet18-Kather100k": [1.0, 0.9999911785125732],
"resnet34-kather100k": [1.0, 0.9979840517044067],
"resnet50-kather100k": [1.0, 0.9999986886978149],
"resnet101-kather100k": [1.0, 0.9999932050704956],
"resnext50_32x4d-kather100k": [1.0, 0.9910059571266174],
"resnext101_32x8d-kather100k": [1.0, 0.9999971389770508],
"wide_resnet50_2-kather100k": [1.0, 0.9953408241271973],
"wide_resnet101_2-kather100k": [1.0, 0.9999831914901733],
"densenet121-kather100k": [1.0, 1.0],
"densenet161-kather100k": [1.0, 0.9999959468841553],
"densenet169-kather100k": [1.0, 0.9999934434890747],
"densenet201-kather100k": [1.0, 0.9999983310699463],
"mobilenet_v2-kather100k": [0.9999998807907104, 0.9999126195907593],
"mobilenet_v3_large-kather100k": [0.9999996423721313, 0.9999878406524658],
"mobilenet_v3_small-kather100k": [0.9999998807907104, 0.9999997615814209],
"googlenet-kather100k": [1.0, 0.9999639987945557],
}
for pretrained_model, expected_prob in pretrained_info.items():
_test_predictor_output(
inputs,
pretrained_model,
probabilities_check=expected_prob,
predictions_check=[6, 3],
)