-
Notifications
You must be signed in to change notification settings - Fork 0
/
randstainna.py
219 lines (180 loc) · 6.95 KB
/
randstainna.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
import cv2
import numpy as np
from skimage import color
from typing import Optional, Dict
import yaml
class Dict2Class(object):
# ToDo: Wrap into RandStainNA
def __init__(self, my_dict: Dict):
self.my_dict = my_dict
for key in my_dict:
setattr(self, key, my_dict[key])
def get_yaml_data(yaml_file):
# ToDo: Wrap into RandStainNA
file = open(yaml_file, "r", encoding="utf-8")
file_data = file.read()
file.close()
# str->dict
data = yaml.load(file_data, Loader=yaml.FullLoader)
return data
class RandStainNA(object):
# ToDo: support downloading yaml file from online if the path is not provided.
def __init__(
self,
yaml_file: str,
std_hyper: Optional[float] = 0,
distribution: Optional[str] = "normal",
probability: Optional[float] = 1.0,
is_train: Optional[bool] = True,
):
# true:training setting/false: demo setting
assert distribution in [
"normal",
"laplace",
"uniform",
], "Unsupported distribution style {}.".format(distribution)
self.yaml_file = yaml_file
cfg = get_yaml_data(self.yaml_file)
c_s = cfg["color_space"]
self._channel_avgs = {
"avg": [
cfg[c_s[0]]["avg"]["mean"],
cfg[c_s[1]]["avg"]["mean"],
cfg[c_s[2]]["avg"]["mean"],
],
"std": [
cfg[c_s[0]]["avg"]["std"],
cfg[c_s[1]]["avg"]["std"],
cfg[c_s[2]]["avg"]["std"],
],
}
self._channel_stds = {
"avg": [
cfg[c_s[0]]["std"]["mean"],
cfg[c_s[1]]["std"]["mean"],
cfg[c_s[2]]["std"]["mean"],
],
"std": [
cfg[c_s[0]]["std"]["std"],
cfg[c_s[1]]["std"]["std"],
cfg[c_s[2]]["std"]["std"],
],
}
self.channel_avgs = Dict2Class(self._channel_avgs)
self.channel_stds = Dict2Class(self._channel_stds)
self.color_space = cfg["color_space"]
self.p = probability
self.std_adjust = std_hyper
self.color_space = c_s
self.distribution = distribution
self.is_train = is_train
def _getavgstd(self, image: np.ndarray, isReturnNumpy: Optional[bool] = True):
avgs = []
stds = []
num_of_channel = image.shape[2]
for idx in range(num_of_channel):
avgs.append(np.mean(image[:, :, idx]))
stds.append(np.std(image[:, :, idx]))
if isReturnNumpy:
return (np.array(avgs), np.array(stds))
else:
return (avgs, stds)
def _normalize(
self,
img: np.ndarray,
img_avgs: np.ndarray,
img_stds: np.ndarray,
tar_avgs: np.ndarray,
tar_stds: np.ndarray,
) -> np.ndarray:
img_stds = np.clip(img_stds, 0.0001, 255)
img = (img - img_avgs) * (tar_stds / img_stds) + tar_avgs
if self.color_space in ["LAB", "HSV"]:
img = np.clip(img, 0, 255).astype(np.uint8)
return img
def augment(self, img):
# img:is_train:false——>np.array()(cv2.imread()) #BGR
# img:is_train:True——>PIL.Image #RGB
if self.is_train == False:
image = img
else:
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
num_of_channel = image.shape[2]
# color space transfer
if self.color_space == "LAB":
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
elif self.color_space == "HSV":
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
elif self.color_space == "HED":
image = color.rgb2hed(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
std_adjust = self.std_adjust
# virtual template generation
tar_avgs = []
tar_stds = []
if self.distribution == "uniform":
# three-sigma rule for uniform distribution
for idx in range(num_of_channel):
tar_avg = np.random.uniform(
low=self.channel_avgs.avg[idx] - 3 * self.channel_avgs.std[idx],
high=self.channel_avgs.avg[idx] + 3 * self.channel_avgs.std[idx],
)
tar_std = np.random.uniform(
low=self.channel_avgs.avg[idx] - 3 * self.channel_avgs.std[idx],
high=self.channel_avgs.avg[idx] + 3 * self.channel_avgs.std[idx],
)
tar_avgs.append(tar_avg)
tar_stds.append(tar_std)
else:
if self.distribution == "normal":
np_distribution = np.random.normal
elif self.distribution == "laplace":
np_distribution = np.random.laplace
for idx in range(num_of_channel):
tar_avg = np_distribution(
loc=self.channel_avgs.avg[idx],
scale=self.channel_avgs.std[idx] * (1 + std_adjust),
)
tar_std = np_distribution(
loc=self.channel_stds.avg[idx],
scale=self.channel_stds.std[idx] * (1 + std_adjust),
)
tar_avgs.append(tar_avg)
tar_stds.append(tar_std)
tar_avgs = np.array(tar_avgs)
tar_stds = np.array(tar_stds)
img_avgs, img_stds = self._getavgstd(image)
image = self._normalize(
img=image,
img_avgs=img_avgs,
img_stds=img_stds,
tar_avgs=tar_avgs,
tar_stds=tar_stds,
)
if self.color_space == "LAB":
image = cv2.cvtColor(image, cv2.COLOR_LAB2BGR)
elif self.color_space == "HSV":
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
elif self.color_space == "HED":
nimg = color.hed2rgb(image)
imin = nimg.min()
imax = nimg.max()
rsimg = (255 * (nimg - imin) / (imax - imin)).astype(
"uint8"
) # rescale to [0,255]
image = cv2.cvtColor(rsimg, cv2.COLOR_RGB2BGR)
return image
def __call__(self, img):
if np.random.rand(1) < self.p:
return self.augment(img)
else:
return img
def __repr__(self):
format_string = self.__class__.__name__ + "("
format_string += f"methods=Reinhard"
format_string += f", colorspace={self.color_space}"
format_string += f", mean={self._channel_avgs}"
format_string += f", std={self._channel_stds}"
format_string += f", std_adjust={self.std_adjust}"
format_string += f", distribution={self.distribution}"
format_string += f", p={self.p})"
return format_string