def analysis_patchwise_crop(self, sample, data_aug): # If skipping blank patches is active if self.patchwise_skip_blanks: # Slice image and segmentation into patches patches_img = slice_matrix(sample.img_data, self.patch_shape, self.patchwise_overlap, self.data_io.interface.three_dim) patches_seg = slice_matrix(sample.seg_data, self.patch_shape, self.patchwise_overlap, self.data_io.interface.three_dim) # Skip blank patches (only background) for i in reversed(range(0, len(patches_seg))): # IF patch DON'T contain any non background class -> remove it if not np.any(patches_seg[i][...,self.patchwise_skip_class] != 1): del patches_img[i] del patches_seg[i] # Select a random patch pointer = np.random.randint(0, len(patches_img)) img = patches_img[pointer] seg = patches_seg[pointer] # Expand image dimension to simulate a batch with one image img_data = np.expand_dims(img, axis=0) seg_data = np.expand_dims(seg, axis=0) # Pad patches if necessary if img_data.shape[1:-1] != self.patch_shape: img_data = pad_patch(img_data, self.patch_shape, return_slicer=False) seg_data = pad_patch(seg_data, self.patch_shape, return_slicer=False) # Run data augmentation if data_aug: img_data, seg_data = self.data_augmentation.run(img_data, seg_data) # If skipping blank is not active -> random crop else: # Access image and segmentation data img = sample.img_data seg = sample.seg_data # If no data augmentation should be performed # -> create Data Augmentation instance without augmentation methods if not data_aug or self.data_augmentation is None: cropping_data_aug = Data_Augmentation(cycles=1, scaling=False, rotations=False, elastic_deform=False, mirror=False, brightness=False, contrast=False, gamma=False, gaussian_noise=False) else : cropping_data_aug = self.data_augmentation # Configure the Data Augmentation instance to cropping cropping_data_aug.cropping = True cropping_data_aug.cropping_patch_shape = self.patch_shape # Expand image dimension to simulate a batch with one image img_data = np.expand_dims(img, axis=0) seg_data = np.expand_dims(seg, axis=0) # Run data augmentation and cropping img_data, seg_data = cropping_data_aug.run(img_data, seg_data) # Create tuple of preprocessed data ready_data = list(zip(img_data, seg_data)) # Return preprocessed data tuple return ready_data
def analysis_patchwise_grid(self, sample, training, data_aug): # Slice image into patches patches_img = slice_matrix(sample.img_data, self.patch_shape, self.patchwise_overlap, self.data_io.interface.three_dim) if training: # Slice segmentation into patches patches_seg = slice_matrix(sample.seg_data, self.patch_shape, self.patchwise_overlap, self.data_io.interface.three_dim) else: patches_seg = None # Skip blank patches (only background) if training and self.patchwise_skip_blanks: # Iterate over each patch for i in reversed(range(0, len(patches_seg))): # IF patch DON'T contain any non background class -> remove it if not np.any( patches_seg[i][..., self.patchwise_skip_class] != 1): del patches_img[i] del patches_seg[i] # Concatenate a list of patches into a single numpy array img_data = np.stack(patches_img, axis=0) if training: seg_data = np.stack(patches_seg, axis=0) # Pad patches if necessary if img_data.shape[1:-1] != self.patch_shape and training: img_data = pad_patch(img_data, self.patch_shape, return_slicer=False) seg_data = pad_patch(seg_data, self.patch_shape, return_slicer=False) elif img_data.shape[1:-1] != self.patch_shape and not training: img_data, slicer = pad_patch(img_data, self.patch_shape, return_slicer=True) self.cache["slicer_" + str(sample.index)] = slicer # Run data augmentation if data_aug and training: img_data, seg_data = self.data_augmentation.run(img_data, seg_data) elif data_aug and not training: img_data = self.data_augmentation.run_infaug(img_data) # Create tuple of preprocessed data if training: ready_data = list(zip(img_data, seg_data)) else: ready_data = list(zip(img_data)) # Return preprocessed data tuple return ready_data