Exemplo n.º 1
0
 def test_larq_zoo_models(self):
     with context.eager_mode():
         model = lqz.sota.QuickNet(weights=None)
         convert_keras_model(model)
     mocked_converter.assert_called_once_with(
         mock.ANY, ["input_1"], ["DT_FLOAT"], [[1, 224, 224, 3]], ["Identity"],
     )
Exemplo n.º 2
0
def test_simple_model(model_cls):
    model = model_cls(weights="imagenet")
    model_lce = convert_keras_model(model)

    # Test on the flowers dataset
    dataset = (tfds.load(
        "oxford_flowers102",
        split="validation").map(preprocess).shuffle(256).batch(10).take(1))
    inputs = next(tfds.as_numpy(dataset))

    outputs = model(inputs).numpy()
    for input, output in zip(inputs, outputs):
        for actual_output in run_model(model_lce, list(input.flatten())):
            np.testing.assert_allclose(actual_output,
                                       output,
                                       rtol=0.001,
                                       atol=0.25)

    # Test on some random inputs
    input_shape = (10, *model.input.shape[1:])
    inputs = np.random.uniform(-1, 1, size=input_shape).astype(np.float32)
    outputs = model(inputs).numpy()
    for input, output in zip(inputs, outputs):
        for actual_output in run_model(model_lce, list(input.flatten())):
            np.testing.assert_allclose(actual_output,
                                       output,
                                       rtol=0.001,
                                       atol=0.25)
Exemplo n.º 3
0
def convert_model(outfile):
    model_lce = convert_keras_model(
        model_fn(), experimental_enable_bitpacked_activations=True
    )
    with open(outfile, "wb") as f:
        f.write(model_lce)

    click.secho(f"TFLite flatbuffer saved to '{outfile}'.")
Exemplo n.º 4
0
 def test_larq_zoo_models(self):
     with context.eager_mode():
         model = lqz.sota.QuickNet(weights=None)
         convert_keras_model(model)
     if version.parse(tf.__version__) < version.parse("2.2"):
         mocked_graphdef_converter.assert_called_once_with(
             mock.ANY,
             ["input_1"],
             ["DT_FLOAT"],
             [[1, 224, 224, 3]],
             ["Identity"],
             False,
             "arm",
             None,
             False,
         )
     else:
         mocked_saved_model_converter.assert_called_once_with(
             mock.ANY, ["serve"], ["serving_default"], 1, "arm", None,
             False)
Exemplo n.º 5
0
def test_int8_input_output(model_cls, inference_input_type, inference_output_type):
    model_lce = convert_keras_model(
        model_cls(),
        inference_input_type=inference_input_type,
        inference_output_type=inference_output_type,
        experimental_default_int8_range=(-6.0, 6.0) if model_cls == toy_model else None,
    )
    interpreter = tf.lite.Interpreter(model_content=model_lce)
    input_details = interpreter.get_input_details()
    assert len(input_details) == 1
    assert input_details[0]["dtype"] == inference_input_type.as_numpy_dtype
    output_details = interpreter.get_output_details()
    assert len(output_details) == 1
    assert output_details[0]["dtype"] == inference_output_type.as_numpy_dtype
Exemplo n.º 6
0
def test_strip_lcedequantize_ops(
    model_cls,
    inference_input_type,
    inference_output_type,
    experimental_enable_bitpacked_activations,
):
    model_lce = convert_keras_model(
        model_cls(),
        inference_input_type=inference_input_type,
        inference_output_type=inference_output_type,
        experimental_enable_bitpacked_activations=
        experimental_enable_bitpacked_activations,
    )
    model_lce = strip_lcedequantize_ops(model_lce)
    interpreter = tf.lite.Interpreter(model_content=model_lce)
    output_details = interpreter.get_output_details()
    assert len(output_details) == 1
    assert output_details[0]["dtype"] == tf.int32.as_numpy_dtype
Exemplo n.º 7
0
    def test_target_arg(self):
        with context.eager_mode():
            model = lqz.sota.QuickNet(weights=None)

            # These should work
            convert_keras_model(model, target="arm")
            convert_keras_model(model, target="xcore")

            # Anything else shouldn't
            with self.assertRaises(
                    ValueError,
                    msg='Expected `target` to be "arm" or "xcore"'):
                convert_keras_model(model, target="x86")
Exemplo n.º 8
0
def test_simple_model(model_cls):
    model = model_cls(weights="imagenet")
    model_lce = convert_keras_model(
        model, experimental_enable_bitpacked_activations=True)

    # Test on the flowers dataset
    dataset = (tfds.load(
        "tf_flowers", split="train",
        try_gcs=True).map(preprocess).shuffle(100).batch(10).take(1))
    inputs = next(tfds.as_numpy(dataset))

    outputs = model(inputs).numpy()
    assert_model_output(model_lce, inputs, outputs)

    # Test on some random inputs
    input_shape = (10, *model.input.shape[1:])
    inputs = np.random.uniform(-1, 1, size=input_shape).astype(np.float32)
    outputs = model(inputs).numpy()
    assert_model_output(model_lce, inputs, outputs)
Exemplo n.º 9
0
def test_simple_model(dataset, model_cls):
    model = model_cls(weights="imagenet")

    # For the untrained models, do a very small amount of training so that the
    # batch norm stats (and, less importantly, the weights) have sensible
    # values.
    if model_cls != lqz.sota.QuickNetSmall:
        model.compile(
            optimizer=tf.keras.optimizers.Adam(1e-4),
            loss="sparse_categorical_crossentropy",
        )
        model.fit(dataset, epochs=1)

    model_lce = convert_keras_model(
        model, experimental_enable_bitpacked_activations=True
    )

    if model_cls == lqz.sota.QuickNetSmall:
        # Since QuickNetSmall is a deep model we are more tolerant of error.
        rtol = 0.05
        atol = 0.2
    elif model_cls == toy_model_int8:
        # 0.025 atol is chosen because it allows an off-by-one error in the int8
        # model (with +-3 scales) but not off-by-two.
        rtol = 0.01
        atol = 0.025
    else:
        rtol = 0.001
        atol = 0.001

    # Test on a single batch of images
    inputs = next(iter(dataset.take(1)))[0]
    outputs = model(inputs).numpy()
    assert_model_output(model_lce, inputs.numpy(), outputs, rtol, atol)

    # Test on some random inputs
    input_shape = (10, *model.input.shape[1:])
    inputs = np.random.uniform(-1, 1, size=input_shape).astype(np.float32)
    outputs = model(inputs).numpy()
    assert_model_output(model_lce, inputs, outputs, rtol, atol)
Exemplo n.º 10
0
 def test_wrong_arg(self):
     with self.assertRaises(ValueError):
         convert_keras_model("./model.h5")
Exemplo n.º 11
0
def test_larq_zoo_models(eager_mode, model_cls):
    model = model_cls(weights=None)
    convert_keras_model(model)