Exemplo n.º 1
0
def test__call__if_smoothing_is_active(model):
    saliency = Saliency(model)
    result = saliency(CategoricalScore(1, 2),
                      np.random.sample((1, 8, 8, 3)),
                      smooth_samples=1)
    assert result.shape == (1, 8, 8)
    result = saliency(CategoricalScore(1, 2),
                      np.random.sample((1, 8, 8, 3)),
                      smooth_samples=2)
    assert result.shape == (1, 8, 8)
def test__call__with_mutiple_outputs_model_but_losses_is_too_many(
        multiple_outputs_model):
    activation_maximization = ActivationMaximization(multiple_outputs_model)
    with pytest.raises(ValueError):
        activation_maximization([
            CategoricalScore(1, 2),
            CategoricalScore(1, 2),
            CategoricalScore(1, 2)
        ],
                                steps=1,
                                input_modifiers=None)
Exemplo n.º 3
0
def test__call__if_penultimate_layer_is_no_exist_name(model):
    scorecam = ScoreCAM(model)
    with pytest.raises(ValueError):
        scorecam(CategoricalScore(1, 2),
                 np.random.sample((1, 8, 8, 3)),
                 penultimate_layer='hoge',
                 max_N=3)
def test__call__with_mutiple_inputs_model(multiple_inputs_model):
    activation_maximization = ActivationMaximization(multiple_inputs_model)
    result = activation_maximization(CategoricalScore(1, 2),
                                     steps=1,
                                     input_modifiers=None)
    assert result[0].shape == (1, 8, 8, 3)
    assert result[1].shape == (1, 10, 10, 3)
Exemplo n.º 5
0
def test__call__if_penultimate_layer_is_None(model):
    scorecam = ScoreCAM(model)
    result = scorecam(CategoricalScore(1, 2),
                      np.random.sample((1, 8, 8, 3)),
                      penultimate_layer=None,
                      max_N=3)
    assert result.shape == (1, 8, 8)
Exemplo n.º 6
0
def test__call__if_expand_cam_is_False(model):
    scorecam = ScoreCAM(model)
    result = scorecam(CategoricalScore(1, 2),
                      np.random.sample((1, 8, 8, 3)),
                      expand_cam=False,
                      max_N=3)
    assert result.shape == (1, 6, 6)
Exemplo n.º 7
0
def test__call__if_seed_input_shape_is_invalid(model):
    gradcam = Gradcam(model)
    try:
        gradcam(CategoricalScore(1, 2), np.random.sample((8, )))
        assert False
    except (ValueError, tf.errors.InvalidArgumentError):
        # TF became to raise InvalidArgumentError from ver.2.0.2.
        assert True
Exemplo n.º 8
0
def test__call__if_model_has_multiple_inputs(multiple_inputs_model):
    saliency = Saliency(multiple_inputs_model)
    result = saliency(
        CategoricalScore(1, 2),
        [np.random.sample((1, 8, 8, 3)),
         np.random.sample((1, 10, 10, 3))])
    assert len(result) == 2
    assert result[0].shape == (1, 8, 8)
    assert result[1].shape == (1, 10, 10)
Exemplo n.º 9
0
def test__call__if_model_has_multiple_io(multiple_io_model):
    gradcam = Gradcam(multiple_io_model)
    result = gradcam(
        [CategoricalScore(1, 2), lambda x: x],
        [np.random.sample((1, 8, 8, 3)),
         np.random.sample((1, 10, 10, 3))])
    assert len(result) == 2
    assert result[0].shape == (1, 8, 8)
    assert result[1].shape == (1, 10, 10)
Exemplo n.º 10
0
def test__call__if_expand_cam_is_False_and_model_has_multiple_inputs(
        multiple_inputs_model):
    gradcam = Gradcam(multiple_inputs_model)
    result = gradcam(
        CategoricalScore(1, 2),
        [np.random.sample((1, 8, 8, 3)),
         np.random.sample((1, 10, 10, 3))],
        expand_cam=False)
    assert result.shape == (1, 8, 8)
Exemplo n.º 11
0
def test__call__if_model_has_multiple_io_when_batchsize_is_2(
        multiple_io_model):
    gradcam = Gradcam(multiple_io_model)
    result = gradcam(
        [CategoricalScore([1, 0], 2), lambda x: x],
        [np.random.sample((2, 8, 8, 3)),
         np.random.sample((2, 10, 10, 3))])
    assert len(result) == 2
    assert result[0].shape == (2, 8, 8)
    assert result[1].shape == (2, 10, 10)
def test__call__with_callback(model):
    activation_maximization = ActivationMaximization(model)
    mock = MockCallback()
    result = activation_maximization(CategoricalScore(1, 2),
                                     steps=1,
                                     callbacks=mock)
    assert result.shape == (1, 8, 8, 3)
    assert mock.on_begin_was_called
    assert mock.on_call_was_called
    assert mock.on_end_was_called
Exemplo n.º 13
0
def test__call__if_model_has_multiple_inputs(multiple_inputs_model):
    scorecam = ScoreCAM(multiple_inputs_model)
    result = scorecam(
        CategoricalScore(1, 2),
        [np.random.sample((1, 8, 8, 3)),
         np.random.sample((1, 10, 10, 3))],
        max_N=3)
    assert len(result) == 2
    assert result[0].shape == (1, 8, 8)
    assert result[1].shape == (1, 10, 10)
def test__call__with_mutiple_outputs_model(multiple_outputs_model):
    activation_maximization = ActivationMaximization(multiple_outputs_model)
    result = activation_maximization(CategoricalScore(1, 2),
                                     steps=1,
                                     input_modifiers=None)
    assert result.shape == (1, 8, 8, 3)
    activation_maximization = ActivationMaximization(multiple_outputs_model)
    result = activation_maximization(
        [CategoricalScore(1, 2),
         CategoricalScore(1, 2)],
        steps=1,
        input_modifiers=None)
    assert result.shape == (1, 8, 8, 3)
    activation_maximization = ActivationMaximization(multiple_outputs_model)
    result = activation_maximization(
        [CategoricalScore(1, 2),
         CategoricalScore(1, 2)],
        steps=1,
        input_modifiers=None,
        regularizers=[TotalVariation(10.), L2Norm(10.)])
    assert result.shape == (1, 8, 8, 3)
Exemplo n.º 15
0
def test__call__if_expand_cam_is_False(model):
    gradcam = Gradcam(model)
    result = gradcam(CategoricalScore(1, 2),
                     np.random.sample((1, 8, 8, 3)),
                     expand_cam=False)
    assert result.shape == (1, 6, 6)
Exemplo n.º 16
0
def test__call__if_model_has_multiple_outputs(multiple_outputs_model):
    gradcam = Gradcam(multiple_outputs_model)
    result = gradcam([CategoricalScore(1, 2), lambda x: x],
                     np.random.sample((1, 8, 8, 3)))
    assert result.shape == (1, 8, 8)
Exemplo n.º 17
0
def test__call__if_model_has_only_dense_layer(dense_model):
    gradcam = Gradcam(dense_model)
    with pytest.raises(ValueError):
        gradcam(CategoricalScore(1, 2), np.random.sample((1, 3)))
def test__call__with_seed_input(model):
    activation_maximization = ActivationMaximization(model)
    result = activation_maximization(CategoricalScore(1, 2),
                                     seed_input=np.random.sample((8, 8, 3)),
                                     steps=1)
    assert result.shape == (1, 8, 8, 3)
Exemplo n.º 19
0
def test__call__if_seed_input_has_not_batch_dim(model):
    scorecam = ScoreCAM(model)
    result = scorecam(CategoricalScore(1, 2),
                      np.random.sample((8, 8, 3)),
                      max_N=3)
    assert result.shape == (1, 8, 8)
def test__call__with_gradient_modifier(model):
    activation_maximization = ActivationMaximization(model)
    result = activation_maximization(CategoricalScore(1, 2),
                                     steps=1,
                                     gradient_modifier=lambda x: x)
    assert result.shape == (1, 8, 8, 3)
Exemplo n.º 21
0
def test__call__(model):
    gradcam = Gradcam(model)
    result = gradcam(CategoricalScore(1, 2), np.random.sample((1, 8, 8, 3)))
    assert result.shape == (1, 8, 8)
Exemplo n.º 22
0
def test__call__if_model_has_multiple_outputs(multiple_outputs_model):
    scorecam = ScoreCAM(multiple_outputs_model)
    result = scorecam([CategoricalScore(1, 2), lambda x: x],
                      np.random.sample((1, 8, 8, 3)),
                      max_N=3)
    assert result.shape == (1, 8, 8)
def test__call__(model):
    activation_maximization = ActivationMaximization(model)
    result = activation_maximization(CategoricalScore(1, 2), steps=1)
    assert result.shape == (1, 8, 8, 3)
Exemplo n.º 24
0
def test__call__(model):
    scorecam = ScoreCAM(model)
    result = scorecam(CategoricalScore(1, 2),
                      np.random.sample((1, 8, 8, 3)),
                      max_N=3)
    assert result.shape == (1, 8, 8)
Exemplo n.º 25
0
def test__call__if_seed_input_is_None(model):
    gradcam = Gradcam(model)
    with pytest.raises(ValueError):
        gradcam(CategoricalScore(1, 2), None)
Exemplo n.º 26
0
def test__call__if_penultimate_layer_is_no_exist_index(model):
    gradcam = Gradcam(model)
    with pytest.raises(ValueError):
        gradcam(CategoricalScore(1, 2),
                np.random.sample((1, 8, 8, 3)),
                penultimate_layer=100000)
Exemplo n.º 27
0
def test__call__if_seed_input_has_not_batch_dim(model):
    gradcam = Gradcam(model)
    result = gradcam(CategoricalScore(1, 2), np.random.sample((8, 8, 3)))
    assert result.shape == (1, 8, 8)
def test__call__if_loss_is_list(model):
    activation_maximization = ActivationMaximization(model)
    result = activation_maximization([CategoricalScore(1, 2)], steps=1)
    assert result.shape == (1, 8, 8, 3)
Exemplo n.º 29
0
def test__call__if_penultimate_layer_is_None(model):
    gradcam = Gradcam(model)
    result = gradcam(CategoricalScore(1, 2),
                     np.random.sample((1, 8, 8, 3)),
                     penultimate_layer=None)
    assert result.shape == (1, 8, 8)
Exemplo n.º 30
0
def test__call__if_seed_input_is_None(model):
    scorecam = ScoreCAM(model)
    with pytest.raises(ValueError):
        scorecam(CategoricalScore(1, 2), None, max_N=3)