-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__crossval_dataloader.py
230 lines (132 loc) · 5.46 KB
/
__crossval_dataloader.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
import torch
from copy import deepcopy
import numpy as np
import pathlib as pl
import pandas as pd
import bioformats
import javabridge as jb
import tifffile
import zarr
import os
import matplotlib
from matplotlib_scalebar.scalebar import ScaleBar
from resize.pytorch import resize
import albumentations as A
# def flip_aug(a,b,c ):
# """Randomly flips along x-axis, y-axis, or both, or neither."""
# if np.random.random() < 0.5:
# a = torch.flip(a, dims=(1,))
# b = torch.flip(b, dims=(1,))
# c = torch.flip(c, dims=(1,))
# if np.random.random() < 0.5:
# a = torch.flip(a, dims=(2,))
# b = torch.flip(b, dims=(2,))
# c = torch.flip(c, dims=(2,))
# return a, b, c
# def rot_aug(a,b,c):
# """Randomly rotates in multiples of 90 degrees"""
# k = np.random.choice([0, 1, 2, 3])
# a = torch.rot90(a, k=k, dims=(1,2))
# b = torch.rot90(b, k=k, dims=(1,2))
# c = torch.rot90(c, k=k, dims=(1,2))
# return a, b, c
def flip_aug(a ):
"""Randomly flips along x-axis, y-axis, or both, or neither."""
if np.random.random() < 0.5:
a = torch.flip(a, dims=(1,))
if np.random.random() < 0.5:
a = torch.flip(a, dims=(2,))
return a
def rot_aug(a):
"""Randomly rotates in multiples of 90 degrees"""
k = np.random.choice([0, 1, 2, 3])
a = torch.rot90(a, k=k, dims=(1,2))
return a
def inform(a):
print(a.shape, a.dtype, type(a))
def clip(a, _min, _max):
a[a < _min] = _min
a[a > _max] = _max
class Dataset(torch.utils.data.Dataset):
def __init__(self, dataframe, n_items,
mapping_level_2, reverse_mapping_level_2,
mapping_level_1, reverse_mapping_level_1,
mapping_level_0, reverse_mapping_level_0,
patch_size, training):
self.dataframe = deepcopy( dataframe )
self.n_items = n_items
self.mapping_level_2 = mapping_level_2
self.reverse_mapping_level_2 = reverse_mapping_level_2
self.patch_size = patch_size
self.training = training
def __len__(self):
return self.n_items
def __getitem__(self, _index):
if self.training:
# Undersampling - even probability of looking at a patch with any of the labels
# Uniformly pull a cell label, this is a string of the cell name
cur_class = np.random.choice( list( self.mapping_level_2.values() ) )
# Randomly grab a row in the dataframe with that label
index = np.random.choice( self.dataframe[self.dataframe["level_2_label_string"] == cur_class].index.tolist() )
else:
index = _index
# select relevant df row
# datapoint = deepcopy( self.dataframe.loc[ index ] )
datapoint = self.dataframe.loc[ index ]
# get new patch from centroid
centroid_row = int( datapoint["centroid_row"] )
centroid_col = int( datapoint["centroid_col"] )
start_row = centroid_row - (self.patch_size // 2)
start_col = centroid_col - (self.patch_size // 2)
#=========================
# Read vH&E
# correct a missing intial slash
vhe_path = "/" + datapoint["vhe"]
if os.path.exists(vhe_path):
store = tifffile.imread(vhe_path, aszarr=True)
else:
print("path doesn't exist:", vhe_path)
return
z = zarr.open(store, mode='r')
start_row = centroid_row - (self.patch_size // 2)
start_col = centroid_col - (self.patch_size // 2)
X = z[start_row:start_row+self.patch_size, start_col:start_col+self.patch_size].astype(np.float32)
X = np.moveaxis(X, -1, 0) # put channels first
#=========================
# normalize x
X /= 255.0
X = torch.tensor(X)
# Y = torch.tensor(Y)
# mask = torch.tensor(mask)
if self.training:
# X, Y, mask = flip_aug( X, Y, mask )
# X, Y, mask = rot_aug( X, Y, mask )
X = flip_aug( X )
X = rot_aug( X )
label_string_level_2 = datapoint["level_2_label_string"]
label_level_2 = self.reverse_mapping_level_2[label_string_level_2]
X = resize( X.unsqueeze(0), dxyz=(1.56, 1.56), order=3 )[0] # order 3 in cubic interp
clip(X, 0, 1)
if self.training:
#==============================================================
# realistic H&E color augmentation based on observation
transform = A.Compose([
A.HueSaturationValue(
hue_shift_limit=0.4,
sat_shift_limit=0.4,
val_shift_limit=0.2,
always_apply=False,
p=0.5,),
])
# move channels to the back before color augmentation. it must be numpy for this library's augmentation
X = torch.moveaxis( X, 0, -1 ).numpy()
# color augment
transformed = transform( image = X )
transformed_image = transformed["image"]
# put it back in the batch
X = transformed_image
# convert back to torch tensor and move channels back
X = torch.tensor(X).float()
X = torch.moveaxis(X, -1, 0)
#==============================================================
return X, label_level_2, torch.tensor([0]), torch.tensor([0])