class TestXcamWithMultipleIOModel(): @pytest.mark.parametrize("scores,expected_error", [ (None, ValueError), ([None], ValueError), (CategoricalScore(0), ValueError), ([CategoricalScore(0)], ValueError), ([None, None], ValueError), ([CategoricalScore(0), None], ValueError), ([None, BinaryScore(0)], ValueError), ([CategoricalScore(0), BinaryScore(0)], NO_ERROR), ([score_with_tuple, score_with_tuple], NO_ERROR), ([score_with_list, score_with_list], NO_ERROR), ]) @pytest.mark.usefixtures("xcam", "saliency", "mixed_precision") def test__call__if_score_is_(self, scores, expected_error, multiple_io_model): cam = Xcam(multiple_io_model) with assert_raises(expected_error): result = cam(scores, [dummy_sample((1, 8, 8, 3)), dummy_sample((1, 10, 10, 3))]) assert result[0].shape == (1, 8, 8) assert result[1].shape == (1, 10, 10) @pytest.mark.parametrize("seed_input,expected_error", [ (None, ValueError), (dummy_sample((1, 8, 8, 3)), ValueError), ([dummy_sample((1, 8, 8, 3))], ValueError), ([dummy_sample((1, 8, 8, 3)), dummy_sample((1, 10, 10, 3))], NO_ERROR), ]) @pytest.mark.usefixtures("xcam", "saliency", "mixed_precision") def test__call__if_seed_input_is_(self, seed_input, expected_error, multiple_io_model): cam = Xcam(multiple_io_model) with assert_raises(expected_error): result = cam([CategoricalScore(0), BinaryScore(0)], seed_input) assert result[0].shape == (1, 8, 8) assert result[1].shape == (1, 10, 10) @pytest.mark.parametrize("expand_cam", [False, True]) @pytest.mark.usefixtures("xcam", "mixed_precision") def test__call__with_expand_cam(self, expand_cam, multiple_io_model): cam = Xcam(multiple_io_model) result = cam([CategoricalScore(0), BinaryScore(0)], [dummy_sample( (1, 8, 8, 3)), dummy_sample((1, 10, 10, 3))], expand_cam=expand_cam) if expand_cam: assert result[0].shape == (1, 8, 8) assert result[1].shape == (1, 10, 10) else: assert result.shape == (1, 8, 8)
def test__call__with_expand_cam(self, expand_cam, multiple_outputs_model): cam = Xcam(multiple_outputs_model) result = cam([CategoricalScore(0), BinaryScore(0)], [dummy_sample((1, 8, 8, 3))], expand_cam=expand_cam) if expand_cam: assert result[0].shape == (1, 8, 8) else: assert result.shape == (1, 6, 6)
def _test_for_multiple_io(self, model): saliency = Saliency(model) result = saliency( [CategoricalScore(0), BinaryScore(0)], [dummy_sample((1, 8, 8, 3)), dummy_sample((1, 10, 10, 3))]) assert len(result) == 2 assert result[0].shape == (1, 8, 8) assert result[1].shape == (1, 10, 10)
def test__call__if_seed_input_is_(self, seed_input, expected, expected_error, multiple_outputs_model): cam = Xcam(multiple_outputs_model) with assert_raises(expected_error): result = cam([CategoricalScore(0), BinaryScore(0)], seed_input) if type(expected) is list: assert type(result) is list expected = expected[0] result = result[0] assert result.shape == expected
def test__call__(self, target_values, output, expected, expected_error): output = tf.constant(output, tf.float32) score = BinaryScore(target_values) with assert_raises(expected_error): score_value = score(output) assert tf.math.reduce_all(score_value == expected)
def test__init__(self, target_values, expected, expected_error): with assert_raises(expected_error): score = BinaryScore(target_values) assert score.target_values == expected
def test__call__if_seed_input_is_(self, seed_input, expected_error, multiple_io_model): cam = Xcam(multiple_io_model) with assert_raises(expected_error): result = cam([CategoricalScore(0), BinaryScore(0)], seed_input) assert result[0].shape == (1, 8, 8) assert result[1].shape == (1, 10, 10)