forked from EIDOSLAB/torchstain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
107 lines (86 loc) · 2.18 KB
/
example.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
import cv2
import matplotlib.pyplot as plt
import torchstain
import torch
from torchvision import transforms
import time
size = 1024
target = cv2.resize(cv2.cvtColor(cv2.imread("./data/target.png"), cv2.COLOR_BGR2RGB), (size, size))
to_transform = cv2.resize(cv2.cvtColor(cv2.imread("./data/source.png"), cv2.COLOR_BGR2RGB), (size, size))
normalizer = torchstain.normalizers.MacenkoNormalizer(backend='numpy')
normalizer.fit(target)
T = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: x*255)
])
torch_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='torch')
torch_normalizer.fit(T(target))
tf_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='tensorflow')
tf_normalizer.fit(T(target))
t_to_transform = T(to_transform)
t_ = time.time()
norm, H, E = normalizer.normalize(I=to_transform, stains=True)
print("numpy runtime:", time.time() - t_)
plt.figure()
plt.suptitle('numpy normalizer')
plt.subplot(2, 2, 1)
plt.title('Original')
plt.axis('off')
plt.imshow(to_transform)
plt.subplot(2, 2, 2)
plt.title('Normalized')
plt.axis('off')
plt.imshow(norm)
plt.subplot(2, 2, 3)
plt.title('H')
plt.axis('off')
plt.imshow(H)
plt.subplot(2, 2, 4)
plt.title('E')
plt.axis('off')
plt.imshow(E)
plt.show()
t_ = time.time()
norm, H, E = torch_normalizer.normalize(I=t_to_transform, stains=True)
print("torch runtime:", time.time() - t_)
plt.figure()
plt.suptitle('torch normalizer')
plt.subplot(2, 2, 1)
plt.title('Original')
plt.axis('off')
plt.imshow(to_transform)
plt.subplot(2, 2, 2)
plt.title('Normalized')
plt.axis('off')
plt.imshow(norm)
plt.subplot(2, 2, 3)
plt.title('H')
plt.axis('off')
plt.imshow(H)
plt.subplot(2, 2, 4)
plt.title('E')
plt.axis('off')
plt.imshow(E)
plt.show()
t_ = time.time()
norm, H, E = tf_normalizer.normalize(I=t_to_transform, stains=True)
print("tf runtime:", time.time() - t_)
plt.figure()
plt.suptitle('tensorflow normalizer')
plt.subplot(2, 2, 1)
plt.title('Original')
plt.axis('off')
plt.imshow(to_transform)
plt.subplot(2, 2, 2)
plt.title('Normalized')
plt.axis('off')
plt.imshow(norm)
plt.subplot(2, 2, 3)
plt.title('H')
plt.axis('off')
plt.imshow(H)
plt.subplot(2, 2, 4)
plt.title('E')
plt.axis('off')
plt.imshow(E)
plt.show()