Пример #1
0
def test_initialize_model_wrong_weights():
    """Test error raised when weights exist but don't match model"""
    squeeze_weight_path = 'pic2vec/saved_models/squeezenet_weights_tf_dim_ordering_tf_kernels.h5'
    assert os.path.isfile(squeeze_weight_path)

    with pytest.raises(ValueError):
        _initialize_model('vgg16', squeeze_weight_path)
Пример #2
0
def test_initialize_model_weights_not_found():
    """Test error raised when the model can't find weights to load"""
    error_weight = 'htraenoytinutroppodnocesaevahtondideduti\losfosraeyderdnuhenootdenmednocsecar'
    try:
        assert not os.path.isfile(error_weight)
    except AssertionError:
        logging.error(
            'Whoops, that mirage exists. '
            'Change error_weight to a file path that does not exist.')

    with pytest.raises(IOError):
        _initialize_model('squeezenet', error_weight)
Пример #3
0
def test_initialize_model_bad_weights():
    """
    Test error raised when the model finds the weights file,
    but it's not the right format
    """
    bad_weights_file = open('bad_weights_test', 'w')
    bad_weights_file.write('this should fail')
    bad_weights_file.close()
    error_weight = 'bad_weights_test'

    try:
        with pytest.raises(IOError):
            _initialize_model('squeezenet', error_weight)
    finally:
        os.remove(error_weight)
Пример #4
0
def test_initialize_model(model_str, expected_layers, test_size):
    """Test the initializations of each model"""
    model = _initialize_model(model_str)

    if model_str == 'squeezenet':
        try:
            model_downloaded_weights = SqueezeNet()
        except Exception:
            raise AssertionError('Problem loading SqueezeNet weights.')
        check_model_equal(model, model_downloaded_weights)

    # Versions of Keras 2.1.5 and later sometimes use different numbers of layers for these models,
    # without changing any behavior for predictions.
    # This checks that the model uses at least one of the expected numbers of layers.
    assert len(model.layers) in expected_layers

    # Create the test image to be predicted on
    blank_image = np.zeros(test_size)

    # Pre-checked prediction
    existing_test_array = np.load(
        INITIALIZED_MODEL_TEST_ARRAY.format(model_str))

    generated_test_array = model.predict_on_batch(blank_image)

    # Check that each model predicts correctly to see if weights were correctly loaded
    assert np.allclose(generated_test_array, existing_test_array, atol=ATOL)
    del model
Пример #5
0
def test_build_featurizer(depth, autosample, sample_size, expected_size,
                          model_str):
    """Test all of the model iterations"""
    if FEATURIZER_MODEL_DICT[model_str] is None:
        FEATURIZER_MODEL_DICT[model_str] = _initialize_model(model_str)

    model = build_featurizer(depth,
                             autosample,
                             sample_size,
                             model_str=model_str,
                             loaded_model=FEATURIZER_MODEL_DICT[model_str])
    assert model.layers[-1].output_shape == (None, expected_size)
    del model
Пример #6
0
def update_zeros_testing(model):
    """
    This function is used to update arrays in a lower-level part of testing (build_featurizer) than
    the final ImageFeaturizer. This test does not use decapitated models, but rather downloads the
    full Keras pretrained model and checks its baseline predictions on a single blank
    (i.e. all-zeros) image.

    This function initializes the model, and uses it to predict on a single blank image. It logs
    whether the predictions have changed, and then updates the test arrays if necessary.

    Parameters
    ----------
    model : str
        The name of one of pic2vec's supported models

    Returns
    -------
    None
    """

    # Create the test image to be predicted on
    m = _initialize_model(model)

    # Initialize a blank image of the appropriate size for the model
    blank_image = np.zeros(MODEL_TO_IMAGE_SIZE_DICT[model])

    # Compare the generated predictions against the existing test array, and update if necessary
    existing_test_array = np.load(INITIALIZED_MODEL_TEST_ARRAY.format(model))
    generated_array = m.predict_on_batch(blank_image)

    blank_prediction_identical = np.array_equal(generated_array,
                                                existing_test_array)

    logging.INFO("Is a blank image prediction unchanged for {}?".format(model))
    logging.INFO(blank_prediction_identical)

    if not blank_prediction_identical:
        np.save(INITIALIZED_MODEL_TEST_ARRAY.format(model), generated_array)
Пример #7
0
def test_initialize_model(model_str, expected_layers, test_size):
    """Test the initializations of each model"""
    model = _initialize_model(model_str)

    if model_str == 'squeezenet':
        try:
            model_downloaded_weights = SqueezeNet()
        except Exception:
            raise AssertionError('Problem loading SqueezeNet weights.')
        check_model_equal(model, model_downloaded_weights)

    assert len(model.layers) == expected_layers

    # Create the test array to be predicted on
    test_array = np.zeros(test_size)

    # Pre-checked prediction
    check_prediction = np.load(INITIALIZE_ARRAY.format(model_str))

    # Check that each model predicts correctly to see if weights were correctly loaded
    assert np.allclose(model.predict_on_batch(test_array),
                       check_prediction,
                       atol=ATOL)
    del model