def test_should_apply_grey_patch_on_image(): input_image = np.zeros((10, 20, 3)) output = apply_grey_patch(input_image, 0, 0, 10) expected_output = np.concatenate( [127.5 * np.ones( (10, 10, 3)), np.zeros((10, 10, 3))], axis=1) np.testing.assert_almost_equal(output, expected_output)
def get_sensitivity_map(self, model, image, class_index, patch_size): """ Compute sensitivity map on a given image for a specific class index. Args: model (tf.keras.Model): tf.keras model to inspect image: class_index (int): Index of targeted class patch_size (int): Size of patch to apply on the image Returns: np.ndarray: Sensitivity map with shape (H, W, 3) """ sensitivity_map = np.zeros(( math.ceil(image.shape[0] / patch_size), math.ceil(image.shape[1] / patch_size), )) coordinates = [ (index_y, index_x) for index_x in range(sensitivity_map.shape[1] # pylint: disable=unsubscriptable-object ) for index_y in range(sensitivity_map.shape[0] # pylint: disable=unsubscriptable-object ) ] target_class_predictions = [] base_confidence = model.predict(np.array([image]), batch_size=1)[0][class_index] print(f"Base_confidence: {base_confidence}") with tqdm(total=math.ceil(image.shape[0] * image.shape[1] / patch_size**2), position=0, leave=True) as pbar: for index_y, top_left_y in enumerate( range(0, image.shape[1], patch_size)): for index_x, top_left_x in enumerate( range(0, image.shape[0], patch_size)): patch = np.array([ apply_grey_patch(image, top_left_x, top_left_y, patch_size) ]) prediction = model.predict(patch, batch_size=1)[0][class_index] target_class_predictions.append(prediction) triggered = pbar.update(1) print(f"Confidence scores: {target_class_predictions}") for (index_y, index_x), confidence in zip(coordinates, target_class_predictions): sensitivity_map[index_y, index_x] = base_confidence - confidence return cv2.resize(sensitivity_map, image.shape[0:2])
def get_sensitivity_map(self, model, image, class_index, patch_size): """ Compute sensitivity map on a given image for a specific class index. Args: model (tf.keras.Model): tf.keras model to inspect image: class_index (int): Index of targeted class patch_size (int): Size of patch to apply on the image Returns: np.ndarray: Sensitivity map with shape (H, W, 3) """ sensitivity_map = np.zeros( ( math.ceil(image.shape[0] / patch_size), math.ceil(image.shape[1] / patch_size), ) ) patches = [ apply_grey_patch(image, top_left_x, top_left_y, patch_size) for index_x, top_left_x in enumerate(range(0, image.shape[0], patch_size)) for index_y, top_left_y in enumerate(range(0, image.shape[1], patch_size)) ] coordinates = [ (index_y, index_x) for index_x in range( sensitivity_map.shape[1] # pylint: disable=unsubscriptable-object ) for index_y in range( sensitivity_map.shape[0] # pylint: disable=unsubscriptable-object ) ] predictions = model.predict(np.array(patches), batch_size=self.batch_size) target_class_predictions = [ prediction[class_index] for prediction in predictions ] for (index_y, index_x), confidence in zip( coordinates, target_class_predictions ): sensitivity_map[index_y, index_x] = 1 - confidence return cv2.resize(sensitivity_map, image.shape[0:2])