Example #1
0
    def _reconstruct(self, predictions, ids):
        counter = 0
        for (prediction, id) in predictions:
#             prediction = 255- (prediction*255)
#             prediction = prediction * 255
            img_ref = next((x for x in self.test_img_refs if x.probe_file_id == id), None)
            try:
                if self._is_keras_img_model():
                    pred = 255 - np.array(MinMaxScaler((0, 255)).fit_transform(prediction[0].reshape(-1, 1))).flatten()
                    img = pred.reshape(self.patch_shape, self.patch_shape)
                else:
                    pred = 255 - np.array(MinMaxScaler((0, 255)).fit_transform(prediction.reshape(-1, 1))).flatten()
                    if self.config['data_type']=="image":
                        img = pred.reshape(
                            img_ref.img_height//self.image_downscale_factor, 
                            img_ref.img_width//self.image_downscale_factor)
                    elif self.config['data_type'] =="csv":
                        img = pred.reshape(
                            img_ref.img_height, 
                            img_ref.img_width)
                img_original_size = cv2.resize(
                    img, (img_ref.img_orig_width, img_ref.img_orig_height))
            except:
                counter +=1
                img_original_size = np.zeros((img_ref.img_orig_width, img_ref.img_orig_height))

            file_name = f'{img_ref.probe_file_id}.png'
            file_path = self.output_dir + file_name
            ImageUtils.save_image(img_original_size, file_path)
        print(f'Number of errors in reconstruction {counter}') 
    def _read_indicators(self, img_ref):
        #        indicators = []
        shrunken_height, shrunken_width = ImageUtils.get_shrunken_dimensions(
            img_ref.img_height, img_ref.img_width, self.image_downscale_factor)
        indicators = np.empty([
            shrunken_height * shrunken_width,
            len(self.indicator_directories)
        ])
        for i, indicator_name in enumerate(self.indicator_directories):
            indicator_path = self.indicators_path + indicator_name + "/mask/" + img_ref.probe_file_id + ".png"
            try:
                img = ImageUtils.read_image(indicator_path)
                img = ImageUtils.shrink_image(img, self.image_downscale_factor)
                indicator_img = 255 - img
            except FileNotFoundError as err:
                indicator_img = np.zeros([shrunken_height, shrunken_width])
#           indicators.append(indicator_img.ravel())
            try:
                raveled = indicator_img.ravel()
                indicators[:, i] = raveled
            except ValueError as err:
                print(
                    f'img ref dims {(img_ref.img_height//self.image_downscale_factor)* (img_ref.img_width//self.image_downscale_factor)}'
                )
                print(
                    f'img ref original dims {img_ref.img_orig_height* img_ref.img_orig_width}'
                )
                print(f'index value {self.index}')
                print(
                    f'indicators shape {indicators.shape} current indicator shape {raveled.shape}'
                )

#        return np.column_stack(indicators)
        return indicators
Example #3
0
    def get_average_score(self, data):
        scores = 0
        counter = 0
        c = 0
        for i, d in enumerate(data):
            try:
                sys_image = ImageUtils.read_image(d.sys)
            except FileNotFoundError as err:
                print(
                    f'scoring, couldnt read img with id {d.sys} at index {i}')
                continue
            try:
                #                 bw = ImageUtils.get_black_and_white_image(d.ref)
                bw = ImageUtils.read_image(d.ref)
            except FileNotFoundError:
                c += 1
                counter += 1
                continue
#                bw = np.zeros(sys_image.shape)

#             normalized_ref = self.flip(bw)
#             ImageUtils.display(sys_image,bw)
            normalized_ref = bw
            noscore_img = self.get_noscore_image(normalized_ref)
            score = self.get_image_score(noscore_img.ravel(),
                                         normalized_ref.ravel(),
                                         sys_image.ravel())
            scores += score
            counter += 1
#             print(f'running average {round(scores/(i+1),5)} current {round(score,5)}')
        print(f'ref imgs not found {c}')
        return scores / counter
Example #4
0
    def _reconstruct(self, predictions, ids):
        for (prediction, id), patch_img_ref in zip(predictions,
                                                   self.test_img_refs):
            prediction = 255 - (prediction * 255)
            #             prediction = prediction * 255
            img = self._reconstruct_image_from_patches(prediction,
                                                       patch_img_ref)
            img_original_size = cv2.resize(
                img,
                (patch_img_ref.img_orig_width, patch_img_ref.img_orig_height))

            file_name = f'{patch_img_ref.probe_file_id}.png'
            file_path = self.output_dir + file_name
            ImageUtils.save_image(img_original_size, file_path)
 def _get_img_patches_by_id(self, dir_path, probe_file_id):
     imgs = []
     img_names = self._get_img_file_names(dir_path, probe_file_id)
     for name in img_names:
         img_path = os.path.join(dir_path, name)
         img = ImageUtils.read_image(img_path)
         imgs.append(img)
     return imgs
 def _read_images_from_directory(self, dir_path, starting_index,
                                 ending_index):
     img_names = os.listdir(dir_path)[starting_index:ending_index]
     imgs = []
     for name in img_names:
         img_path = os.path.join(dir_path, name)
         img = ImageUtils.read_image(img_path)
         imgs.append(img)
     return imgs
Example #7
0
    def _reconstruct_image_from_patches(self, prediction, patch_img_ref):
        img_from_patches = PatchUtils.get_image_from_patches(
            prediction, patch_img_ref.bordered_img_shape,
            patch_img_ref.patch_window_shape)
        img_without_border = ImageUtils.remove_border(
            img_from_patches, patch_img_ref.border_top,
            patch_img_ref.border_left)

        return img_without_border
    def _read_images_from_directory(self, dir_path, img_refs):
        #         imgs = [None]*len(img_refs)
        imgs = np.empty((len(img_refs), ), dtype=object)
        for (i, img_ref) in enumerate(img_refs):
            img_path = os.path.join(dir_path,
                                    img_ref.probe_mask_file_name + ".png")
            try:
                img = ImageUtils.read_image(img_path)
                img = ImageUtils.shrink_image(img, self.image_downscale_factor)
                img_raveled = img.ravel()
                flipped_img = 255 - img_raveled
                thresholded_img = np.where(flipped_img > 127, 1, 0)
                imgs[i] = thresholded_img
#                 imgs.append(thresholded_img)
            except FileNotFoundError as err:
                self.missing_probe_file_ids.append(img_ref.probe_file_id)
        if len(self.missing_probe_file_ids):
            print(f'img refs length {len(img_refs)}')
            print(f'missing probe file ids {len(self.missing_probe_file_ids)}')
        return imgs
Example #9
0
    def _create_img_patch(self, img_ref, targets_path):
        target_image_path = os.path.join(targets_path, "manipulation", "mask",
                                         img_ref.probe_mask_file_name) + ".png"

        border_value = [255, 255, 255]
        if self.tuning['black_border_y'] is True or self.tuning[
                'dilate_y_black_border_y'] is True:
            border_value = [0, 0, 0]

        bordered_img, border_top, border_left, original_img_shape = ImageUtils.get_image_with_border(
            target_image_path,
            self.patch_shape,
            self.img_downscale_factor,
            border_value=border_value)
        if self.tuning["dilate_y"] is True:
            diluted_target = ImageUtils.dilate(bordered_img)
        target_image_out_dir = self.output_dir + 'target_image/'
        bordered_image_patches, patch_window_shape = PatchUtils.get_patches(
            bordered_img, self.patch_shape)
        for i, patch in enumerate(bordered_image_patches):
            path = f'{target_image_out_dir}{img_ref.probe_file_id}_{i}.png'
            ImageUtils.save_image(patch, path)
        patch_img_ref = PatchImageRefFactory.create_img_ref(
            img_ref.probe_file_id, bordered_img.shape, patch_window_shape,
            img_ref.probe_mask_file_name, original_img_shape, border_top,
            border_left)

        indicators = None
        for indicator_dir in self.indicator_directories:
            indicator_out_path = self.output_dir + indicator_dir + '/'
            img_path = os.path.join(self.indicators_path, indicator_dir,
                                    "mask", img_ref.probe_file_id) + ".png"
            try:
                img, _, _, _ = ImageUtils.get_image_with_border(
                    img_path,
                    self.patch_shape,
                    self.img_downscale_factor,
                    top=border_top,
                    left=border_left)
            except FileNotFoundError as err:
                img = self._handle_missing_indicator_image(bordered_img.shape)
            finally:
                img_patches, _ = PatchUtils.get_patches(img, self.patch_shape)
                for i, patch in enumerate(img_patches):
                    path = f'{indicator_out_path}{img_ref.probe_file_id}_{i}.png'
                    ImageUtils.save_image(patch, path)

        return patch_img_ref
 def _read_images_from_directory(self, dir_path, img_refs):
     all_imgs = [None] * len(img_refs)
     for (i, img_ref) in enumerate(img_refs):
         num_images = img_ref.patch_window_shape[
             0] * img_ref.patch_window_shape[1]
         imgs = [None] * num_images
         for j in range(num_images):
             img_path = f'{dir_path}/{img_ref.probe_file_id}_{j}.png'
             try:
                 img = ImageUtils.read_image(img_path)
                 flipped_img = 255 - img
                 thresholded_img = np.where(flipped_img > 127, 1, 0)
                 imgs[j] = thresholded_img.reshape(self.patch_shape,
                                                   self.patch_shape, 1)
             except FileNotFoundError as err:
                 print(
                     f'could not find file with id {img_ref.probe_file_id}')
                 self.missing_probe_file_ids.append(img_ref.probe_file_id)
         all_imgs[i] = imgs
     return all_imgs
    def _read_indicators(self, img_ref):

        num_images = img_ref.patch_window_shape[
            0] * img_ref.patch_window_shape[1]
        all_indicators = [None] * num_images
        for j in range(num_images):
            #indicators = []
            indicators = np.empty([
                self.patch_shape, self.patch_shape,
                len(self.indicator_directories)
            ])
            for i, indicator_name in enumerate(self.indicator_directories):
                indicator_path = f'{self.indicators_path}{indicator_name}/{img_ref.probe_file_id}_{j}.png'
                try:
                    img = ImageUtils.read_image(indicator_path)
                    ind_img = 255 - img
                except FileNotFoundError as err:
                    ind_img = np.zeros([self.patch_shape, self.patch_shape])
                indicators[:, :, i] = ind_img
            all_indicators[j] = indicators
        return all_indicators
    def __getitem__(self, index):
        starting_index = index * self.batch_size
        ending_index = (index + 1) * self.batch_size

        indicator_imgs = []
        for indicator_name in self.indicator_directories:
            indicator_path = self.patches_path + indicator_name
            indicator_patches = self._read_images_from_directory(
                indicator_path, starting_index, ending_index)
            indicator_imgs.append(indicator_patches)

        target_imgs = []
        target_imgs_path = self.patches_path + 'target_image'
        target_imgs = self._read_images_from_directory(target_imgs_path,
                                                       starting_index,
                                                       ending_index)

        if self.patch_tuning['dilate_y']:
            for img in target_imgs:
                target_imgs.append(ImageUtils.dilate(img))
        elif self.patch_tuning['patch_black']:
            target_imgs = self.patch_black(target_imgs)
        return indicator_imgs, target_imgs
Example #13
0
                diff.append(i)
        f = plt.figure()
        f.add_subplot(1, 2, 1)
        plt.plot(range(len(binary_mcc)), list(sk_mcc))
        f.add_subplot(1, 2, 2)
        plt.plot(range(len(binary_mcc)), binary_mcc)
        plt.show()


if __name__ == '__main__':
    m = MccBinarized()

    ground_truth = [0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0]
    predictions = [0, 5, 10, 2, 18, 118, 50, 75, 20] * 2

    a = ImageUtils.read_image("x.png")
    b = ImageUtils.read_image("y.png")
    #     c = ImageUtils.read_image("c.png")
    #     b = ImageUtils.resize_image(c, tuple(np.flip(a.shape)))

    #     mean = 50.0   # some constant
    #     std = 10.0    # some constant (standard deviation)
    #     noisy_img = a + np.random.normal(mean, std, a.shape)
    #     n = np.clip(noisy_img, 0, 255)
    #     ImageUtils.display(a,n)

    ground_truth = a.ravel()
    predictions = b.ravel()

    t1 = time.perf_counter()
    example_mcc = MccBinarized.compute(predictions, ground_truth)
Example #14
0
 def _save_image(self,img,base_path, file_name):
     path = f'{base_path}{file_name}.png'
     ImageUtils.save_image(img,path)