Exemplo n.º 1
0
def test_keras_model_preprocess():
    num_classes = 1000
    bounds = (0, 255)
    channels = num_classes

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        inputs = Input(shape=(5, 5, channels))
        logits = GlobalAveragePooling2D(data_format="channels_last")(inputs)

        preprocessing = (
            np.arange(num_classes)[None, None],
            np.random.uniform(size=(5, 5, channels)) + 1,
        )

        model1 = KerasModel(Model(inputs=inputs, outputs=logits),
                            bounds=bounds,
                            predicts="logits")

        model2 = KerasModel(
            Model(inputs=inputs, outputs=logits),
            bounds=bounds,
            predicts="logits",
            preprocessing=preprocessing,
        )

        model3 = KerasModel(Model(inputs=inputs, outputs=logits),
                            bounds=bounds,
                            predicts="logits")

        preprocessing = (0, np.random.uniform(size=(5, 5, channels)) + 1)

        model4 = KerasModel(
            Model(inputs=inputs, outputs=logits),
            bounds=bounds,
            predicts="logits",
            preprocessing=preprocessing,
        )

    np.random.seed(22)
    test_images = np.random.rand(2, 5, 5, channels).astype(np.float32)
    test_images_copy = test_images.copy()

    p1 = model1.forward(test_images)
    p2 = model2.forward(test_images)

    # make sure the images have not been changed by
    # the in-place preprocessing
    assert np.all(test_images == test_images_copy)

    p3 = model3.forward(test_images)

    assert p1.shape == p2.shape == p3.shape == (2, num_classes)

    np.testing.assert_array_almost_equal(p1 - p1.max(),
                                         p3 - p3.max(),
                                         decimal=5)

    model4.forward(test_images)
Exemplo n.º 2
0
def test_keras_model_probs(num_classes):
    bounds = (0, 255)
    channels = num_classes

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore", category=DeprecationWarning)
        inputs = Input(shape=(5, 5, channels))
        logits = GlobalAveragePooling2D(data_format="channels_last")(inputs)
        probs = Activation(softmax)(logits)

        model1 = KerasModel(Model(inputs=inputs, outputs=logits),
                            bounds=bounds,
                            predicts="logits")

        model2 = KerasModel(Model(inputs=inputs, outputs=probs),
                            bounds=bounds,
                            predicts="probabilities")

        model3 = KerasModel(Model(inputs=inputs, outputs=probs),
                            bounds=bounds,
                            predicts="probs")

    np.random.seed(22)
    test_images = np.random.rand(2, 5, 5, channels).astype(np.float32)

    p1 = model1.forward(test_images)
    p2 = model2.forward(test_images)
    p3 = model3.forward(test_images)

    assert p1.shape == p2.shape == p3.shape == (2, num_classes)

    np.testing.assert_array_almost_equal(p1 - p1.max(),
                                         p2 - p2.max(),
                                         decimal=1)

    np.testing.assert_array_almost_equal(p2 - p2.max(),
                                         p3 - p3.max(),
                                         decimal=5)