Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
    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)
Esempio n. 4
0
    def restore(self, predict_array_list):
        predict_array = np.zeros_like(sitk.GetArrayFromImage(self.label))

        size = np.array(self.label.GetSize()) - self.label_patch_size
        indices = [
            i for i in product(range(0, size[0] + 1, self.slide[0]),
                               range(0, size[1] + 1, self.slide[1]),
                               range(0, size[2] + 1, self.slide[2]))
        ]

        with tqdm(total=len(predict_array_list),
                  desc="Restoring image...",
                  ncols=60) as pbar:
            for pre_array, idx in zip(predict_array_list, indices):
                x_slice = slice(idx[0], idx[0] + self.label_patch_size[0])
                y_slice = slice(idx[1], idx[1] + self.label_patch_size[1])
                z_slice = slice(idx[2], idx[2] + self.label_patch_size[2])

                predict_array[z_slice, y_slice, x_slice] = pre_array
                pbar.update(1)

        predict = getImageWithMeta(predict_array, self.label)
        predict = cropping(predict, self.lower_pad_size[1].tolist(),
                           self.upper_pad_size[1].tolist())
        predict.SetOrigin(self.label.GetOrigin())

        return predict
Esempio n. 5
0
    def restore(self, predict_array_list):
        predict_array = np.zeros_like(sitk.GetArrayFromImage(self.meta["padded_label"]))

        with tqdm(total=len(predict_array_list), desc="Restoring image...", ncols=60) as pbar:
            for pre_array, idx in zip(predict_array_list, self.meta["total_patch_idx"]):
                z_slice = slice(idx[0], idx[0] + self.meta["patch_size"][0])
                y_slice = slice(idx[1], idx[1] + self.meta["patch_size"][1])
                x_slice = slice(idx[2], idx[2] + self.meta["patch_size"][2])


                predict_array[x_slice, y_slice, z_slice] = pre_array
                pbar.update(1)


        predict = getImageWithMeta(predict_array, self.label)
        predict = cropping(predict, self.meta["lower_padding_size"], self.meta["upper_padding_size"])
        predict.SetOrigin(self.label.GetOrigin())
        

        return predict
Esempio n. 6
0
    def restore(self, predicted_array_list):
        predicted_array = np.zeros(self.padded_label.GetSize()[::-1])
        z_size = self.padded_label.GetSize()[2] - self.label_patch_width
        patch_size = np.array(self.padded_label.GetSize())
        patch_size[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 + patch_size[2])
                predicted_array[z_slice, ...] = pre_array

                pbar.update(1)

        predicted = getImageWithMeta(predicted_array, self.padded_label)

        predicted = cropping(predicted, self.lower_pad_size[1].tolist(),
                             self.upper_pad_size[1].tolist())

        return predicted
Esempio n. 7
0
    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)
Esempio n. 8
0
    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)
Esempio n. 9
0
    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)