Created
February 23, 2019 02:14
-
-
Save vashineyu/7ed4f2b704f9d88776e4d69e4f4db3b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GetDataset(): | |
"""Claim Dataset object for inferencing. | |
Args: | |
slide_name (str): full path to slide | |
f_inputs_preproc (function): preprocessing function to array | |
patch_size (int): patch size of array | |
stride (int): stride of image when get patches | |
level (int): get patch at level | |
Returns: | |
Array | |
""" | |
def __init__(self, slide_name, f_inputs_preproc, patch_size=256, stride=256, level=0): | |
self.slide_name = slide_name | |
self.preproc = f_inputs_preproc | |
self.patch_size = patch_size | |
self.level = level | |
## Init ## | |
self.this_slide = Slide_reader(slide_name=slide_name, rle_file=None, show_info=False) | |
slide_w, slide_h = self.this_slide.slide_info["Width"], self.this_slide.slide_info['Height'] | |
self.croplist = self.compute_croplist(width=slide_w, height=slide_h, stride=stride) | |
self.lowRes_w, self.lowRes_h = slide_w//patch_size, slide_h//patch_size | |
self.counter = 0 | |
def __len__(self): | |
return len(self.croplist) | |
def __getitem__(self, idx): | |
this_coord = self.croplist[idx] | |
arr = self.this_slide.get_patch_at_level(coord=this_coord, | |
sz=(self.patch_size, self.patch_size), | |
level=self.level) | |
if self.preproc is not None: | |
arr = self.preproc(arr) | |
self.counter = (self.counter + 1) % len(self.croplist) | |
return arr | |
def __next__(self): | |
return self.__getitem__(idx=self.counter) | |
@staticmethod | |
def compute_croplist(width, height, stride): | |
x_list = range(0, width, stride) | |
y_list = range(0, height, stride) | |
return list(itertools.product(x_list, y_list)) | |
class Inference_Dataloader(tf.keras.utils.Sequence): | |
"""Compose multiple generators together | |
Args: | |
- slide_obj (class): | |
- batch_size (int): | |
Yield: | |
- Batch of data | |
""" | |
def __init__(self, slide_object, batch_size): | |
self.slide_object = slide_object | |
self.batch_size = batch_size | |
def __len__(self): | |
return math.ceil(len(self.slide_object) / self.batch_size) | |
def __getitem__(self, idx): | |
idx_start = idx * self.batch_size | |
idx_end = (idx+1) * self.batch_size | |
imgs = [] | |
for j in range(idx_start, idx_end): | |
img = self.slide_object[j] | |
imgs.append(img) | |
return np.array(imgs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment