Exemplo n.º 1
0
def load_backselect_subsets(dataset,
                            dataset_name,
                            pixels_to_keep,
                            sis_results_dir,
                            fully_masked_image,
                            max_num=None):
    bs_masks = []
    bs_masked_images = []

    num_images = len(dataset)
    if max_num:
        num_images = min(max_num, len(dataset))
    for i in range(num_images):
        image, _ = dataset[i]
        sis_file = os.path.join(sis_results_dir,
                                '%s_%d.npz' % (dataset_name, i))
        sr = sis_util.load_sis_result(sis_file)
        bs_mask = backselect_mask_from_sis_result(sr, pixels_to_keep)
        img_masked = sis.produce_masked_inputs(image.numpy(),
                                               fully_masked_image,
                                               [bs_mask])[0]
        bs_masks.append(bs_mask)
        bs_masked_images.append(img_masked)

    bs_masks = np.array(bs_masks)
    bs_masked_images = np.array(bs_masked_images)

    return bs_masks, bs_masked_images
 def _initialize_masked_images(self):
     masked_images = []
     for i in tqdm(range(len(self.original_dataset))):
         image, _ = self.original_dataset[i]
         image = image.cpu().numpy()
         matched_files = glob.glob(
             os.path.join(self.sis_dir, '*_%d.npz' % i))
         assert len(matched_files) == 1
         sr = sis_util.load_sis_result(matched_files[0])
         bs_mask = sis_analysis_util.backselect_mask_from_sis_result(
             sr, self.pixels_to_keep)
         img_masked = sis.produce_masked_inputs(image,
                                                self.fully_masked_image,
                                                [bs_mask])[0]
         masked_images.append(img_masked)
     masked_images = np.array(masked_images)
     self.masked_images = masked_images
Exemplo n.º 3
0
def load_sis_results(dataset,
                     dataset_name,
                     model,
                     sis_results_dir,
                     fully_masked_image,
                     sis_threshold,
                     max_num=None):
    """Load data and create masks and masked images."""
    sis_results = []
    sis_image_idxs = []
    sis_pred_class = []
    sis_is_correct_class = []
    sis_masked_images = []
    original_confidences = []

    num_images = len(dataset)
    if max_num:
        num_images = min(max_num, len(dataset))
    for i in range(num_images):
        image, label = dataset[i]

        # Check if original prediction >= threshold.
        original_preds = inference_util.predict(model,
                                                image.unsqueeze(0).cuda(),
                                                add_softmax=True)
        original_confidence = float(original_preds.max())
        original_label = int(original_preds.argmax())
        if original_confidence < sis_threshold:
            continue  # No SIS exists.

        # Compute SIS from backselect data.
        sis_file = os.path.join(sis_results_dir,
                                '%s_%d.npz' % (dataset_name, i))

        backselect_sr = sis_util.load_sis_result(sis_file)
        sis_result = find_sis_from_backselect_result(backselect_sr,
                                                     sis_threshold)
        sis_masked_image = sis.produce_masked_inputs(image.numpy(),
                                                     fully_masked_image,
                                                     [sis_result.mask])[0]

        sis_results.append(sis_result)
        sis_image_idxs.append(i)
        sis_pred_class.append(original_label)
        sis_is_correct_class.append((original_label == label))
        sis_masked_images.append(sis_masked_image)
        original_confidences.append(original_confidence)

    sis_image_idxs = np.array(sis_image_idxs)
    sis_pred_class = np.array(sis_pred_class)
    sis_is_correct_class = np.array(sis_is_correct_class)
    sis_masked_images = np.array(sis_masked_images)
    original_confidences = np.array(original_confidences)

    return LoadSISResults(
        sis_results=sis_results,
        sis_image_idxs=sis_image_idxs,
        sis_pred_class=sis_pred_class,
        sis_is_correct_class=sis_is_correct_class,
        sis_masked_images=sis_masked_images,
        original_confidences=original_confidences,
    )
Exemplo n.º 4
0
 def test_produce_masked_inputs_raises_error(
     self, input_to_mask, fully_masked_input, batch_of_masks):
   with self.assertRaises(TypeError):
     _ = sis.produce_masked_inputs(input_to_mask, fully_masked_input,
                                   batch_of_masks)
Exemplo n.º 5
0
 def test_produce_masked_inputs(self, input_to_mask, fully_masked_input,
                                batch_of_masks, expected_masked_inputs):
   actual_masked_inputs = sis.produce_masked_inputs(
       input_to_mask, fully_masked_input, batch_of_masks)
   np.testing.assert_array_equal(actual_masked_inputs, expected_masked_inputs)
Exemplo n.º 6
0
def create_masked_input(x, sis_result, fully_masked_input):
    return sis.produce_masked_inputs(x.cpu().numpy(), fully_masked_input,
                                     [sis_result.mask])[0]