def execute(self): """ Raw data is clipped or padded for required_shape. """ print(self.image.GetSize()) image_shape = np.array(self.image.GetSize()) required_shape = np.array(self.image.GetSize()) required_shape[0:2] = self.plane_size self.diff = required_shape - image_shape if (self.diff < 0).any(): lower_crop_size = (abs(self.diff) // 2).tolist() upper_crop_size = [rounding(x, 1) for x in abs(self.diff) / 2] self.image = cropping(self.image, lower_crop_size, upper_crop_size) if self.mask is not None: self.mask= cropping(self.mask, lower_crop_size, upper_crop_size) else: lower_pad_size = (self.diff // 2).tolist() upper_pad_size = [rounding(x, 1) for x in self.diff / 2] self.image = padding(self.image, lower_pad_size, upper_pad_size) if self.mask is not None: self.mask = padding(self.mask, lower_pad_size, upper_pad_size) """pad in axial direction. """ self.slide = self.patch_size // np.array((1, 1, self.overlap)) self.axial_lower_pad_size, self.axial_upper_pad_size = caluculatePaddingSize(np.array(self.image.GetSize()), self.patch_size, self.patch_size, self.slide) self.image = padding(self.image, self.axial_lower_pad_size[0].tolist(), self.axial_upper_pad_size[0].tolist()) if self.mask is not None: self.mask= padding(self.mask, self.axial_lower_pad_size[0].tolist(), self.axial_upper_pad_size[0].tolist()) print(self.image.GetSize()) """ Make patch. """ _, _, self.z_length = self.image.GetSize() total = self.z_length // self.slide[2] self.patch_list = [] self.patch_array_list = [] with tqdm(total=total, desc="Clipping images...", ncols=60) as pbar: for z in range(0, self.z_length, self.slide[2]): z_slice = slice(z, z + self.patch_size[2]) if self.mask is not None: patch_mask = sitk.GetArrayFromImage(self.mask[:, :, z_slice]) if (patch_mask == 0).all(): pbar.update(1) continue patch = self.image[:,:, z_slice] patch.SetOrigin(self.image.GetOrigin()) patch_array = sitk.GetArrayFromImage(patch) for _ in range(self.num_rep): self.patch_list.append(patch) self.patch_array_list.append(patch_array) pbar.update(1)
def execute(self): image_patch_size = np.array(self.image.GetSize()) image_patch_size[2] = self.image_patch_width label_patch_size = np.array(self.label.GetSize()) label_patch_size[2] = self.label_patch_width slide = np.array([0, 0, self.z_slide]) image_size = np.array(self.image.GetSize()) """ Caluculate padding size to clip the image correctly. """ self.lower_pad_size, self.upper_pad_size = caluculatePaddingSize( image_size=image_size, image_patch=image_patch_size, label_patch=label_patch_size, slide=slide) """ Pad image and label. """ padded_image = padding(self.image, self.lower_pad_size[0].tolist(), self.upper_pad_size[0].tolist()) # For restoration, add self.. self.padded_label = padding(self.label, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) if self.mask is not None: padded_mask = padding(self.mask, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) """ Clip image, label and mask. """ image_patch_list = self.makePatch(padded_image, self.image_patch_width, self.z_slide) label_patch_list = self.makePatch(self.padded_label, self.label_patch_width, self.z_slide) if self.mask is not None: mask_patch_list = self.makePatch(padded_mask, self.label_patch_width, self.z_slide) """ Check mask. """ self.image_patch_list = [] self.image_patch_array_list = [] self.label_patch_list = [] self.label_patch_array_list = [] assert len(image_patch_list) == len(label_patch_list) for i in range(len(image_patch_list)): if self.mask is not None: mask_patch_array = sitk.GetArrayFromImage(mask_patch_list[i]) if (mask_patch_array == 0).all(): continue image_patch_array = sitk.GetArrayFromImage(image_patch_list[i]) label_patch_array = sitk.GetArrayFromImage(label_patch_list[i]) self.image_patch_list.append(image_patch_list[i]) self.label_patch_list.append(label_patch_list[i]) self.image_patch_array_list.append(image_patch_array) self.label_patch_array_list.append(label_patch_array)
def restore(self, predicted_array_list): predicted_array = np.zeros(self.label.GetSize()[::-1]) z_size = self.label.GetSize()[2] - self.label_patch_width indices = [i for i in range(0, z_size + 1, self.z_slide)] with tqdm(total=len(indices), desc="Restoring image...", ncols=60) as pbar: for pre_array, index in zip(predicted_array_list, indices): z_slice = slice(index, index + self.label_patch_width) predicted_array[z_slice, ...] = pre_array pbar.update(1) predicted = getImageWithMeta(predicted_array, self.label) predicted = cropping(predicted, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) if (self.diff > 0).any(): lower_crop_size = (self.diff // 2).tolist() upper_crop_size = ((self.diff + 1) // 2).tolist() predicted = cropping(predicted, lower_crop_size, upper_crop_size) else: lower_pad_size = (abs(self.diff) // 2).tolist() upper_pad_size = ((abs(self.diff) + 1) // 2).tolist() predicted = padding(predicted, lower_pad_size, upper_pad_size) return predicted
def restore(self, predict_array_list): predicted_array = np.zeros(self.image.GetSize()[::-1]) """ Make patch. """ total = self.z_length // self.slide[2] with tqdm(total=total, desc="Restoring images...", ncols=60) as pbar: for z, predict_array in zip(range(0, self.z_length, self.slide[2]), predict_array_list): z_slice = slice(z, z + self.patch_size[2]) predicted_array[z_slice, ...] = predict_array pbar.update(1) predicted = getImageWithMeta(predicted_array, self.image) predicted = cropping(predicted, self.axial_lower_pad_size[0].tolist(), self.axial_upper_pad_size[0].tolist()) """ Raw data is clipped or padded for required_shape. """ if (self.diff > 0).any(): lower_crop_size = (abs(self.diff) // 2).tolist() upper_crop_size = [rounding(x, 1) for x in abs(self.diff) / 2] predicted = cropping(predicted, lower_crop_size, upper_crop_size) else: lower_pad_size = (abs(self.diff) // 2).tolist() upper_pad_size = [rounding(x, 1) for x in abs(self.diff) / 2] predicted = padding(predicted, lower_pad_size, upper_pad_size) return predicted
def execute(self): """ Clip image and label. """ """ Caluculate each padding size for label and image to clip correctly. """ self.lower_pad_size, self.upper_pad_size = caluculatePaddingSize( np.array(self.label.GetSize()), self.image_patch_size, self.label_patch_size, self.slide) """ Pad image and label. """ self.image = padding(self.image, self.lower_pad_size[0].tolist(), self.upper_pad_size[0].tolist(), mirroring=True) self.label = padding(self.label, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) if self.mask is not None: self.mask = padding(self.mask, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) """ Clip the image and label to patch size. """ image_patch_list = self.makePatch(self.image, self.image_patch_size, self.slide) label_patch_list = self.makePatch(self.label, self.label_patch_size, self.slide) if self.mask is not None: mask_patch_list = self.makePatch(self.mask, self.label_patch_size, self.slide) assert len(image_patch_list) == len(label_patch_list) if self.mask is not None: assert len(image_patch_list) == len(mask_patch_list) """ Check mask. """ self.image_patch_list = [] self.image_patch_array_list = [] self.label_patch_list = [] self.label_patch_array_list = [] for i in range(len(image_patch_list)): if self.mask is not None: mask_patch_array = sitk.GetArrayFromImage(mask_patch_list[i]) if (mask_patch_array == 0).all(): continue image_patch_array = sitk.GetArrayFromImage(image_patch_list[i]) label_patch_array = sitk.GetArrayFromImage(label_patch_list[i]) self.image_patch_list.append(image_patch_list[i]) self.label_patch_list.append(label_patch_list[i]) self.image_patch_array_list.append(image_patch_array) self.label_patch_array_list.append(label_patch_array)
def execute(self): """ Clip or pad raw data for required_shape(=[plane_size, width]) """ print("From {} ".format(self.image.GetSize()), end="") image_size = np.array(self.image.GetSize()) required_shape = np.array(self.image.GetSize()) required_shape[0:2] = self.plane_size self.diff = required_shape - image_size if (self.diff < 0).any(): lower_crop_size = (abs(self.diff) // 2).tolist() upper_crop_size = ((abs(self.diff) + 1) // 2).tolist() self.image = cropping(self.image, lower_crop_size, upper_crop_size) self.label = cropping(self.label, lower_crop_size, upper_crop_size) if self.mask is not None: self.mask = cropping(self.mask, lower_crop_size, upper_crop_size) else: lower_pad_size = (self.diff // 2).tolist() upper_pad_size = ((self.diff + 1) // 2).tolist() self.image = padding(self.image, lower_pad_size, upper_pad_size) self.label = padding(self.label, lower_pad_size, upper_pad_size) if self.mask is not None: self.mask = padding(self.mask, lower_pad_size, upper_pad_size) print("to {}".format(self.image.GetSize())) image_size = np.array(self.image.GetSize()) """ Set image and label patch size. """ image_patch_size = np.array(self.plane_size.tolist() + [self.image_patch_width]) self.label_patch_size = np.array(self.plane_size.tolist() + [self.label_patch_width]) slide = np.array([0, 0, self.z_slide]) """ Caluculate padding size to clip the image correctly. """ self.lower_pad_size, self.upper_pad_size = caluculatePaddingSize( image_size=image_size, image_patch=image_patch_size, label_patch=self.label_patch_size, slide=slide) """ Pad image and label. """ self.image = padding(self.image, self.lower_pad_size[0].tolist(), self.upper_pad_size[0].tolist()) # For restoration, add self.. self.label = padding(self.label, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) if self.mask is not None: self.mask = padding(self.mask, self.lower_pad_size[1].tolist(), self.upper_pad_size[1].tolist()) """ Clip image, label and mask. """ image_patch_list = self.makePatch(self.image, self.image_patch_width, self.z_slide) label_patch_list = self.makePatch(self.label, self.label_patch_width, self.z_slide) if self.mask is not None: mask_patch_list = self.makePatch(self.mask, self.label_patch_width, self.z_slide) """ Check mask. """ self.image_patch_list = [] self.image_patch_array_list = [] self.label_patch_list = [] self.label_patch_array_list = [] for i in range(len(image_patch_list)): if self.mask is not None: mask_patch_array = sitk.GetArrayFromImage(mask_patch_list[i]) if (mask_patch_array == 0).all(): continue image_patch_array = sitk.GetArrayFromImage(image_patch_list[i]) label_patch_array = sitk.GetArrayFromImage(label_patch_list[i]) self.image_patch_list.append(image_patch_list[i]) self.label_patch_list.append(label_patch_list[i]) self.image_patch_array_list.append(image_patch_array) self.label_patch_array_list.append(label_patch_array)
def execute(self): """ Clip image and label. """ """ For restoration. """ self.meta = {} """ Caluculate each padding size for label and image to clip correctly. """ lower_pad_size, upper_pad_size = caluculatePaddingSize(np.array(self.label.GetSize()), self.image_patch_size, self.label_patch_size, self.slide) self.meta["lower_padding_size"] = lower_pad_size[1] self.meta["upper_padding_size"] = upper_pad_size[1] """ Pad image and label. """ padded_image = padding(self.image, lower_pad_size[0], upper_pad_size[0], mirroring=True) padded_label = padding(self.label, lower_pad_size[1], upper_pad_size[1]) if self.mask is not None: padded_mask = padding(self.mask, lower_pad_size[1], upper_pad_size[1]) self.meta["padded_label"] = padded_label self.meta["patch_size"] = self.label_patch_size """ Clip the image and label to patch size. """ self.image_list = [] self.image_array_list = [] self.label_list = [] self.label_array_list = [] izsize, iysize, ixsize = np.array(padded_image.GetSize()) - self.image_patch_size total_image_patch_idx = [i for i in product(range(0, izsize + 1, self.slide[0]), range(0, iysize + 1, self.slide[1]), range(0, ixsize + 1, self.slide[2]))] lzsize, lysize, lxsize = np.array(padded_label.GetSize()) - self.label_patch_size total_label_patch_idx = [i for i in product(range(0, lzsize + 1, self.slide[0]), range(0, lysize + 1, self.slide[1]), range(0, lxsize + 1, self.slide[2]))] self.meta["total_patch_idx"] = total_label_patch_idx if len(total_image_patch_idx) != len(total_label_patch_idx): print("[ERROR] The number of clliped image and label is different.") sys.exit() with tqdm(total=len(total_image_patch_idx), desc="Clipping image and label...", ncols=60) as pbar: for iz, lz in zip(range(0, izsize + 1, self.slide[0]), range(0, lzsize + 1, self.slide[0])): for iy, ly in zip(range(0, iysize + 1, self.slide[1]), range(0, lysize + 1, self.slide[1])): for ix, lx in zip(range(0, ixsize + 1, self.slide[2]), range(0, lxsize + 1, self.slide[2])): """ Set the lower and upper clip index """ image_lower_clip_index = np.array([iz, iy, ix]) image_upper_clip_index = image_lower_clip_index + self.image_patch_size label_lower_clip_index = np.array([lz, ly, lx]) label_upper_clip_index = label_lower_clip_index + self.label_patch_size if self.mask is not None: """ Clip mask image to label patch size. """ clipped_mask = clipping(padded_mask, label_lower_clip_index, label_upper_clip_index) clipped_mask.SetOrigin(self.mask.GetOrigin()) clipped_mask_array = sitk.GetArrayFromImage(clipped_mask) """ If you feed mask image, you check if the image contains the masked part. If not, skip and set False to the check_mask array""" if self.phase == "train" and (clipped_mask_array == 0).all(): pbar.update(1) continue """ Clip label to label patch size. """ clipped_label = clipping(padded_label, label_lower_clip_index, label_upper_clip_index) clipped_label.SetOrigin(self.label.GetOrigin()) clipped_label_array = sitk.GetArrayFromImage(clipped_label) self.label_list.append(clipped_label) self.label_array_list.append(clipped_label_array) clipped_image = clipping(padded_image, image_lower_clip_index, image_upper_clip_index) clipped_image.SetOrigin(self.label.GetOrigin()) clipped_image_array = sitk.GetArrayFromImage(clipped_image) self.image_list.append(clipped_image) self.image_array_list.append(clipped_image_array) pbar.update(1)
def execute(self): # Crop or pad the image for required_shape. image = self.image mask = self.mask print("Image shape : {}".format(image.GetSize())) image_shape = np.array(image.GetSize()) required_shape = np.array(image.GetSize()) required_shape[0:2] = self.plane_size diff = required_shape - image_shape if (diff < 0).any(): lower_crop_size = (abs(diff) // 2).tolist() upper_crop_size = [rounding(x, 1) for x in abs(diff) / 2] image = cropping(image, lower_crop_size, upper_crop_size) if mask is not None: mask = cropping(mask, lower_crop_size, upper_crop_size) else: lower_pad_size = (diff // 2).tolist() upper_pad_size = [rounding(x, 1) for x in diff / 2] image = padding(image, lower_pad_size, upper_pad_size) if mask is not None: mask = padding(mask, lower_pad_size, upper_pad_size) print("Image shape : {}".format(image.GetSize())) image_shape = np.array(image.GetSize()) slide = self.label_patch_size // np.array((1, 1, self.overlap)) lower_pad_size, upper_pad_size = caluculatePaddingSize( image_shape, self.label_patch_size, self.label_patch_size, slide) image = padding(image, lower_pad_size[0].tolist(), upper_pad_size[0].tolist()) if mask is not None: mask = padding(mask, lower_pad_size[0].tolist(), upper_pad_size[0].tolist()) print("Image shape : {}".format(image.GetSize())) # Downsample the image to one in num_down. required_shape = np.array(image.GetSize()) // 2**self.num_down if not self.is_label: image = resampleSize(image, required_shape.tolist(), is_label=False) else: image = resampleSize(image, required_shape.tolist(), is_label=True) if mask is not None: mask = resampleSize(mask, required_shape.tolist(), is_label=True) print("Image shape : {}".format(image.GetSize())) # Crop the image to (label_patch_size / num_down) image_shape = np.array(image.GetSize()) patch_size = self.label_patch_size // 2**self.num_down _, _, z_length = image_shape - patch_size slide = patch_size // np.array((1, 1, self.overlap)) total = z_length // slide[2] + 1 self.patch_list = [] self.patch_array_list = [] with tqdm(total=total, desc="Clipping images...", ncols=60) as pbar: for z in range(0, z_length + 1, slide[2]): z_slice = slice(z, z + patch_size[2]) if mask is not None: patch_mask = sitk.GetArrayFromImage(mask[:, :, z_slice]) if (patch_mask == 0).all(): pbar.update(1) continue patch = image[:, :, z_slice] patch.SetOrigin(self.image.GetOrigin()) patch_array = sitk.GetArrayFromImage(patch) self.patch_list.append(patch) self.patch_array_list.append(patch_array) pbar.update(1) # Load model if self.model_path is not None: with open(self.model_path, "rb") as f: model = cloudpickle.load(f) model.eval() # Make feature map. total = len(self.patch_array_list) is_cuda = torch.cuda.is_available() and True device = torch.device("cuda" if is_cuda else "cpu") self.feature_map_list = [] with tqdm(total=total, desc="Making feature maps...", ncols=60) as pbar: for patch_array in self.patch_array_list: if self.model_path is not None: patch_array = torch.from_numpy(patch_array).to( device, dtype=torch.float)[None, None, ...] feature_map = model.forwardWithoutSegmentation(patch_array) feature_map = feature_map.to("cpu").detach().numpy() feature_map = np.squeeze(feature_map) max_channel = feature_map.shape[0] if self.num_channel == -1: self.num_channel = feature_map.shape[0] for i in range(0, max_channel, self.num_channel): c_slice = slice(i, i + self.num_channel) f_map = feature_map[c_slice, ...] self.feature_map_list.append(f_map) else: for _ in range(self.num_rep): self.feature_map_list.append(patch_array) pbar.update(1)
import tensorflow as tf import functions as func import numpy as np # Loader data_train, data_test = tf.keras.datasets.mnist.load_data() (image_train, label_train) = data_train (image_test, label_test) = data_test x = np.ones((4, 3, 3, 2)) x_pad = func.padding(x, 2) print(x_pad.shape) print(x_pad)
def execute(self): is_cuda = torch.cuda.is_available() and True device = torch.device("cuda" if is_cuda else "cpu") # Load model. with open(self.modelweight_path, "rb") as f: model = cloudpickle.load(f) model.eval() # Read image. print("Image shape : {}".format(self.image.GetSize())) # Pad or crop the image in sagittal and coronal direction to plane_size. required_shape = np.array(self.image.GetSize()) required_shape[0:2] = self.plane_size diff = required_shape - np.array(self.image.GetSize()) if (diff < 0).any(): lower_crop_size = (abs(diff) // 2).tolist() upper_crop_size = [rounding(x, 1) for x in abs(diff) / 2] self.image = cropping(self.image, lower_crop_size, upper_crop_size) if self.mask is not None: self.mask = cropping(self.mask, lower_crop_size, upper_crop_size) else: lower_pad_size = (diff // 2).tolist() upper_pad_size = [rounding(x, 1) for x in diff / 2] self.image = padding(self.image, lower_pad_size, upper_pad_size) if self.mask is not None: self.mask = padding(self.mask, lower_pad_size, upper_pad_size) # Pad the image in axial direction to just make patches. image_shape = np.array(self.image.GetSize()) slide = self.input_size // np.array((1, 1, self.overlap)) lower_pad_size, upper_pad_size = caluculatePaddingSize(image_shape, self.input_size, self.input_size, slide) padded_image = padding(self.image, lower_pad_size[0].tolist(), upper_pad_size[0].tolist()) if self.mask is not None: padded_mask = padding(self.mask, lower_pad_size[0].tolist(), upper_pad_size[0].tolist()) padded_image_array = sitk.GetArrayFromImage(padded_image) print("Padded image shape : {}".format(padded_image.GetSize())) padded_image_shape = np.array(padded_image.GetSize()) - self.input_size self.feature_map_list= [] length = (padded_image_shape[2] // slide[2]) + 1 with tqdm(total=length, desc="Making feature maps...", ncols=60) as pbar: for z in range(0, padded_image_shape[2] + 1, slide[2]): z_slice = slice(z, z + self.input_size[2]) if self.mask is not None: batch_mask = sitk.GetArrayFromImage(padded_mask[:, :, z_slice]) if (batch_mask == 0).all(): pbar.update(1) continue batch = padded_image[:, :, z_slice] batch_array = sitk.GetArrayFromImage(batch) batch_array = torch.from_numpy(batch_array).to(device, dtype=torch.float) batch_array = batch_array[None, None, ...] for c in range(self.output_layer): batch_array, feature_map = model.contracts[c](batch_array) feature_map = feature_map.to("cpu").detach().numpy() feature_map = np.squeeze(feature_map) max_channel = feature_map.shape[0] if self.num_channel == -1: self.num_channel = feature_map.shape[0] for i in range(0, max_channel, self.num_channel): c_slice = slice(i, i + self.num_channel) f_map = feature_map[c_slice, ...] self.feature_map_list.append(f_map) pbar.update(1)