Ejemplo n.º 1
0
def randomify_neuropod(output_path, input_spec, output_spec):
    """Uses neuropod input and output specs to automatically generate a neuropod package that complies to the spec and
    produces random outputs.

    This neuropod can be used as a stub, for testing purposes. A Tensorflow engine is used in the neuropod generated.

    :param output_path: Output path where the neuropod will be written to.
    :param input_spec: Neuropod input spec structure (a list of dictionaries)
    :param output_spec: Neuropod output spec structure (a list of dictionaries)
    :return:
    """
    g = tf.Graph()
    with g.as_default():
        # Create a placeholder of a corresponding shape for each of the inputs in the input spec
        node_name_mapping = _placeholdes_from_input_spec(input_spec)

        # For each output, we create a random matrix generator of an appropriate type and size.
        node_name_mapping.update(_random_from_output_spec(output_spec))

    graph_def = g.as_graph_def()

    create_tensorflow_neuropod(
        neuropod_path=output_path,
        model_name="random_model",
        graph_def=graph_def,
        node_name_mapping=node_name_mapping,
        input_spec=input_spec,
        output_spec=output_spec,
    )

    return output_path
    def package_simple_addition_model(self, do_fail=False, **kwargs):
        with TemporaryDirectory() as test_dir:
            neuropod_path = os.path.join(test_dir, "test_neuropod")

            # Get the input/output spec along with test data
            kwargs.update(get_addition_model_spec(do_fail=do_fail))

            # `create_tensorflow_neuropod` runs inference with the test data immediately
            # after creating the neuropod. Raises a ValueError if the model output
            # does not match the expected output.
            create_tensorflow_neuropod(
                neuropod_path=neuropod_path,
                model_name="addition_model",
                trackable_obj=create_tf_addition_model(),
                **kwargs)

            # Run some additional checks
            check_addition_model(neuropod_path)
    def package_simple_addition_model(self, do_fail=False):
        with TemporaryDirectory() as test_dir:
            neuropod_path = os.path.join(test_dir, "test_neuropod")

            # `create_tensorflow_neuropod` runs inference with the test data immediately
            # after creating the neuropod. Raises a ValueError if the model output
            # does not match the expected output.
            create_tensorflow_neuropod(
                neuropod_path=neuropod_path,
                model_name="addition_model",
                graph_def=create_tf_addition_model(self.custom_op_path),
                node_name_mapping={
                    "x": "some_namespace/in_x:0",
                    "y": "some_namespace/in_y:0",
                    "out": "some_namespace/out:0",
                },
                custom_ops=[self.custom_op_path, self.second_custom_op],
                # Get the input/output spec along with test data
                **get_addition_model_spec(do_fail=do_fail))
Ejemplo n.º 4
0
    def package_accumulator_model(self, neuropod_path, init_op_name_as_list):
        graph_def, init_op_name = create_tf_accumulator_model()

        # `create_tensorflow_neuropod` runs inference with the test data immediately
        # after creating the neuropod. Raises a ValueError if the model output
        # does not match the expected output.
        create_tensorflow_neuropod(
            neuropod_path=neuropod_path,
            model_name="accumulator_model",
            graph_def=graph_def,
            node_name_mapping={
                "x": "some_namespace/in_x:0",
                "out": "some_namespace/out:0",
            },
            input_spec=[
                {
                    "name": "x",
                    "dtype": "float32",
                    "shape": ()
                },
            ],
            output_spec=[
                {
                    "name": "out",
                    "dtype": "float32",
                    "shape": ()
                },
            ],
            init_op_names=[init_op_name]
            if init_op_name_as_list else init_op_name,
            test_input_data={
                "x": np.float32(5.0),
            },
            test_expected_out={
                "out": np.float32(5.0),
            },
        )
Ejemplo n.º 5
0
    def package_simple_addition_model(self, do_fail=False, **kwargs):
        with TemporaryDirectory() as test_dir:
            neuropod_path = os.path.join(test_dir, "test_neuropod")

            # Get the input/output spec along with test data
            kwargs.update(get_addition_model_spec(do_fail=do_fail))

            # `create_tensorflow_neuropod` runs inference with the test data immediately
            # after creating the neuropod. Raises a ValueError if the model output
            # does not match the expected output.
            create_tensorflow_neuropod(
                neuropod_path=neuropod_path,
                model_name="addition_model",
                graph_def=create_tf_addition_model(),
                node_name_mapping={
                    "x": "some_namespace/in_x:0",
                    "y": "some_namespace/in_y:0",
                    # The `:0` is optional
                    "out": "some_namespace/out",
                },
                **kwargs)

            # Run some additional checks
            check_addition_model(neuropod_path)