示例#1
0
def main():

    file_path = "Simple.json"
    data = convert_states_to_stateful_parameters("../" + file_path)
    # print(data)
    with open("Translated_" + file_path, "w") as fp:
        json.dump(data, fp, indent=4)

    if "-run" in sys.argv:

        verbose = True

        mod_graph = load_mdf("Translated_%s" % file_path).graphs[0]
        eg = EvaluableGraph(mod_graph, verbose)

        mod_graph_old = load_mdf("../" + file_path).graphs[0]
        eg_old = EvaluableGraph(mod_graph_old, verbose)

        format = FORMAT_NUMPY

        eg.evaluate(array_format=format)

        eg_old.evaluate(array_format=format)

        print(
            "New file output value>>>",
            eg.enodes["processing_node"].evaluable_outputs["output_1"].
            curr_value,
        )

        print(
            "Old file output value>>>",
            eg_old.enodes["processing_node"].evaluable_outputs["output_1"].
            curr_value,
        )
示例#2
0
def main():

    dt = 0.01
    file_path = "States.json"
    data = convert_states_to_stateful_parameters("../" + file_path, dt)

    with open("Translated_" + file_path, "w") as fp:
        json.dump(data, fp, indent=4)

    if "-run" in sys.argv:

        verbose = True

        mod_graph = load_mdf("Translated_%s" % file_path).graphs[0]
        eg = EvaluableGraph(mod_graph, verbose)

        mod_graph_old = load_mdf("../" + file_path).graphs[0]
        eg_old = EvaluableGraph(mod_graph_old, verbose)

        duration = 2
        t = 0
        recorded = {}
        times = []
        s = []
        s_old = []
        while t <= duration:

            print("======   Evaluating at t = %s  ======" % (t))

            # levels.append(eg.enodes['sine_node'].evaluable_stateful_parameters['level'].curr_value)
            # t+=args.dt

            # print("time first>>>",type(t))
            t = float(
                eg.enodes["sine_node"].evaluable_parameters["time"].curr_value)

            # times.append(float(eg.enodes['sine_node'].evaluable_parameters['time'].curr_value))

            times.append(t)

            if t == 0:
                eg_old.evaluate()  # replace with initialize?
            else:
                eg_old.evaluate(time_increment=dt)
            s_old.append(eg_old.enodes["sine_node"].
                         evaluable_outputs["out_port"].curr_value)
            eg.evaluate()

            s.append(eg.enodes["sine_node"].evaluable_outputs["out_port"].
                     curr_value)
            # t+=dt

        print(s_old[:10], s[:10], times[:10])
        import matplotlib.pyplot as plt

        plt.plot(times, s)

        plt.show()
        plt.savefig("translated_levelratesineplot.jpg")
示例#3
0
def main(example_file: str,
         array_format: str = FORMAT_NUMPY,
         verbose: bool = False):
    """
    Main entry point for execution engine.

    Args:
        example_file: The MDF file to execute.
        array_format: The format of arrays to use. Allowed values: 'numpy' or 'tensorflow'.
        verbose: Whether to print output to standard out during execution.

    """

    from modeci_mdf.utils import load_mdf, print_summary

    mod_graph = load_mdf(example_file).graphs[0]

    if verbose:
        print("Loaded Graph:")
        print_summary(mod_graph)

        print("------------------")
    eg = EvaluableGraph(mod_graph, verbose)
    eg.evaluate(array_format=array_format)

    return eg
示例#4
0
文件: exporter.py 项目: kmantel/MDF
def convert_mdf_file_to_onnx(input_file: str):
    """
    Converter from MDF to ONNX. Takes in a JSON/ONNX file and generates ONNX files.

    Args:
        input_file: The input file path to the MDF file. Output files are generated in same
            directory with -m2o.onnx extensions.

    Returns:
        NoneType
    """

    import os

    # Load the MDF model from file - this is not used anymore
    mdf_model = load_mdf(input_file)
    # print("Loaded MDF file from ", mdf_model_file)

    onnx_models = mdf_to_onnx(mdf_model)

    for onnx_model in onnx_models:
        out_filename = (
            f"{os.path.splitext(input_file)[0]}_{onnx_model.graph.name}-m2o.onnx"
        )
        onnx.save(onnx_model, out_filename)
        print("ONNX output saved in ", out_filename)
示例#5
0
def test_abc():
    base_path = Path(__file__).parent

    filename = "examples/ONNX/abc_basic-mdf.json"
    file_path = (base_path / "../../.." / filename).resolve()

    # Load the MDF model
    mdf_model = load_mdf(str(file_path))

    # Test input
    test_input = np.array([[0, 0, 0], [1, 1, 1]], dtype=np.float32)

    # Get the result of MDF execution
    mdf_executable = EvaluableGraph(mdf_model.graphs[0], verbose=False)
    mdf_executable.evaluate(initializer={"input": test_input})
    mdf_output = mdf_executable.enodes["Cos_2"].evaluable_outputs[
        "_3"].curr_value

    # Get the translated ONNX model
    onnx_models = mdf_to_onnx(mdf_model)

    # Bluffing onnx that our model is 13 when it is actually 15. This is needed for older onnxruntime
    # installations to run this model. See https://github.com/onnx/onnx/issues/3205
    onnx_models[0].opset_import[0].version = 13

    # Get the result of running the ONNX model
    session = backend.prepare(onnx_models[0])
    onnx_output = session.run(
        test_input)  # run returns a list with the actual result and type
    onnx_res_output = np.array(onnx_output[0])

    assert np.array_equal(onnx_res_output, mdf_output)
示例#6
0
def test_ab():
    base_path = Path(__file__).parent

    filename = "examples/ONNX/ab.json"
    file_path = (base_path / "../../.." / filename).resolve()

    # Load the MDF model
    mdf_model = load_mdf(str(file_path))

    # Test input
    test_input = np.array([[0, 0, 0], [1, 1, 1]], dtype=np.float32)

    # Get the result of MDF execution
    mdf_executable = EvaluableGraph(mdf_model.graphs[0], verbose=False)
    # TODO: the int type cast is necessaryf or now because the nodes' parameters are constants and inputs must have
    #  the same type
    mdf_executable.evaluate(initializer={"input": test_input.astype(int)})
    mdf_output = mdf_executable.enodes["Mul_3"].evaluable_outputs[
        "_4"].curr_value

    # Get the translated ONNX model
    onnx_models = mdf_to_onnx(mdf_model)

    # Bluffing onnx that our model is 13 when it is actually 15. This is needed for older onnxruntime
    # installations to run this model. See https://github.com/onnx/onnx/issues/3205
    onnx_models[0].opset_import[0].version = 13

    # Get the result of running the ONNX model
    session = backend.prepare(onnx_models[0])
    onnx_output = session.run(
        test_input)  # run returns a list with the actual result and type
    onnx_res_output = np.array(onnx_output[0])
    # print(f"Output calculated by onnxruntime: {onnx_res_output} and MDF: {mdf_output.astype(float)}")

    assert np.array_equal(onnx_res_output, mdf_output)
示例#7
0
文件: test_model.py 项目: kmantel/MDF
def test_no_input_ports_to_json(tmpdir):
    """Test the edge case of exporting a model to JSON when it has a node with no input ports"""

    mod = Model(id="ABCD")
    mod_graph = Graph(id="abcd_example")
    mod.graphs.append(mod_graph)

    input_node = Node(id="input0")
    input_node.parameters.append(Parameter(id="input_level", value=10.0))
    op1 = OutputPort(id="out_port")
    op1.value = "input_level"
    input_node.output_ports.append(op1)
    mod_graph.nodes.append(input_node)

    tmpfile = f"{tmpdir}/test.json"
    mod.to_json_file(tmpfile)

    # FIXME: Doesn't seem like we have any methods for deserialization. Just do some quick and dirty checks
    # This should really be something like assert mod_graph == deserialized_mod_graph
    import json

    with open(tmpfile) as f:
        data = json.load(f)
    print(data)

    assert (data["ABCD"]["graphs"]["abcd_example"]["nodes"]["input0"]
            ["parameters"]["input_level"]["value"] == 10.0)

    mod_graph2 = load_mdf(tmpfile)

    print(mod_graph2)
    assert mod_graph2.graphs[0].nodes[0].parameters[0].value == 10.0
示例#8
0
def _generate_scripts_from_json(model_input):
    """
    Helper function to parse MDF objects from MDF json representation as well
    as load the h5 weights for large weight matrices. Uses MDF scheduler to
    determine proper ordering of nodes, and calls `build_script`.

    Returns dictionary of scripts where key = name of mdf model, value is string
    representation of script.
    """
    file_dir = os.path.dirname(model_input)

    model = load_mdf(model_input)
    scripts = {}

    for graph in model.graphs:
        nodes = graph.nodes
        # Read weights.h5 if exists
        if "weights.h5" in os.listdir(file_dir):
            weight_dict = h5py.File(os.path.join(file_dir, "weights.h5"), "r")

            # Hack to fix problem with HDF5 parameters
            for node in graph.nodes:
                if node.parameters:
                    for param in node.parameters:
                        param_key = param.id
                        param_val = param.value
                        if param_key in ["weight", "bias"
                                         ] and type(param_val) == str:
                            # Load and reassign
                            array = weight_dict[param_val][:]
                            # np.set_printoptions(threshold=sys.maxsize)
                            param.value = array

        evaluable_graph = EvaluableGraph(graph, verbose=False)
        enodes = evaluable_graph.enodes
        edges = evaluable_graph.ordered_edges
        try:
            conditions = evaluable_graph.conditions
        except AttributeError:
            conditions = {}

        # Use edges and nodes to construct execution order
        execution_order = []
        for idx, edge in enumerate(edges):
            if idx == 0:
                execution_order.append(edge.sender)
            execution_order.append(edge.receiver)

        # Build script
        script = build_script(nodes, execution_order, conditions=conditions)
        scripts[graph.id] = script

    return scripts
示例#9
0
文件: test_model.py 项目: kmantel/MDF
def test_graph_types(tmpdir):
    r"""
    Test whether types saved in parameters are the same after reloading
    """
    mod = Model(id="Test0")
    mod_graph = Graph(id="test_example")
    mod.graphs.append(mod_graph)
    node0 = Node(id="node0")
    mod_graph.nodes.append(node0)

    p_int = 2
    node0.parameters.append(Parameter(id="p_int", value=p_int))
    p_float = 2.0
    node0.parameters.append(Parameter(id="p_float", value=p_float))
    p_bool = False
    node0.parameters.append(Parameter(id="p_bool", value=p_bool))
    p_str = "p_int + p_float"
    node0.parameters.append(Parameter(id="p_str", value=p_str))
    p_str2 = "2"
    node0.parameters.append(Parameter(id="p_str2", value=p_str2))
    p_list = ["2", 2, "two"]
    node0.parameters.append(Parameter(id="p_list", value=p_list))

    p_dict = {"a": 3, "w": {"b": 3, "x": True, "y": [2, 2, 2, 2]}}
    node0.parameters.append(Parameter(id="p_dict", value=p_dict))

    p_dict_tuple = {"y": (4, 44)}  # will change to {'y': [4, 44]}
    node0.parameters.append(Parameter(id="p_dict_tuple", value=p_dict_tuple))

    print(mod)
    tmpfile = f"{tmpdir}/test.json"
    mod.to_json_file(tmpfile)
    mod_graph2 = load_mdf(tmpfile)
    print(f"Saved to {tmpfile}: {mod_graph2}")
    new_node0 = mod_graph2.graphs[0].nodes[0]

    for p in [p.id for p in node0.parameters]:
        print("Testing {}, is {} = {}?".format(
            p,
            new_node0.get_parameter(p).value, eval(p)))

        assert type(new_node0.get_parameter(p).value) == type(eval(p))
        # Type will be same for tuple containing dict, but tuple will have been converetd to dict...
        if not p == "p_dict_tuple":
            assert new_node0.get_parameter(p).value == eval(p)
示例#10
0
def main():
    """Takes count.lisp, converts to MDF, and runs using the scheduler."""
    file_name = os.path.dirname(os.path.realpath(__file__)) + "/count.lisp"
    print(file_name)
    mod = actr_to_mdf(file_name)
    mdf_graph = load_mdf(file_name[:-5] + ".json").graphs[0]
    eg = EvaluableGraph(graph=mdf_graph, verbose=False)
    term = False
    goal = {}
    retrieval = {}
    while not term:
        eg.evaluate(initializer={"goal_input": goal, "dm_input": retrieval})
        term = (eg.enodes["check_termination"].
                evaluable_outputs["check_output"].curr_value)
        goal = (eg.enodes["fire_production"].
                evaluable_outputs["fire_prod_output_to_goal"].curr_value)
        retrieval = (
            eg.enodes["fire_production"].
            evaluable_outputs["fire_prod_output_to_retrieval"].curr_value)
    print("Final Goal:")
    print(eg.enodes["goal_buffer"].evaluable_outputs["goal_output"].curr_value)
示例#11
0
文件: exporter.py 项目: kmantel/MDF
    from modeci_mdf.utils import load_mdf, print_summary

    verbose = True

    if len(sys.argv) < 3:
        print(
            "Usage:\n\n  python graphviz.py MDF_JSON_FILE level [%s]\n\n" %
            NO_VIEW +
            "where level = 1, 2 or 3. Include %s to supress viewing generated graph on render\n"
            % NO_VIEW)
        exit()

    example = sys.argv[1]
    view = NO_VIEW not in sys.argv

    model = load_mdf(example)

    mod_graph = model.graphs[0]

    print("------------------")
    print("Loaded Graph:")
    print_summary(mod_graph)

    print("------------------")
    # nmllite_file = example.replace('.json','.nmllite.json')
    mdf_to_graphviz(mod_graph,
                    engine=engines["d"],
                    view_on_render=view,
                    level=int(sys.argv[2]))
示例#12
0
def main():

    verbose = True
    dt = 5e-05
    file_path = "mlp_pure_mdf.json"
    data = convert_states_to_stateful_parameters(file_path, dt)
    # print(data)
    with open("Translated_" + file_path, "w") as fp:
        json.dump(data, fp, indent=4)

    test_all = "-test" in sys.argv

    mod_graph = load_mdf("Translated_%s" % file_path).graphs[0]

    # mdf_to_graphviz(mod_graph,view_on_render=not test_all, level=3)

    from modelspec.utils import FORMAT_NUMPY, FORMAT_TENSORFLOW

    format = FORMAT_TENSORFLOW if "-tf" in sys.argv else FORMAT_NUMPY

    eg = EvaluableGraph(mod_graph, verbose=False)
    eg.evaluate(array_format=format)

    print("Finished evaluating graph using array format %s" % format)

    for n in [
            "mlp_input_layer",
            "mlp_relu_1",
            "mlp_hidden_layer_with_relu",
            "mlp_output_layer",
    ]:
        out = eg.enodes[n].evaluable_outputs["out_port"].curr_value
        print(f"Final output value of node {n}: {out}, shape: {out.shape}")

    if "-graph" in sys.argv:
        mod.to_graph_image(
            engine="dot",
            output_format="png",
            view_on_render=False,
            level=2,
            filename_root="mlp_pure_mdf",
            only_warn_on_fail=
            True,  # Makes sure test of this doesn't fail on Windows on GitHub Actions
        )

    if test_all:
        # Iterate on training data, feed forward and log accuracy
        imgs = np.load("example_data/imgs.npy")
        labels = np.load("example_data/labels.npy")

        import torch.nn

        matches = 0
        imgs_to_test = imgs[:300]

        start = time.time()
        for i in range(len(imgs_to_test)):
            ii = imgs[i, :, :]
            target = labels[i]
            img = torch.Tensor(ii).view(-1, 14 * 14).numpy()
            # plot_img(img, 'Post_%i (%s)'%(i, img.shape))
            print("***********\nTesting image %i (label: %s): %s\n%s" %
                  (i, target, np.array2string(img, threshold=5,
                                              edgeitems=2), img.shape))
            # print(mod_graph.nodes[0].parameters['input'])
            mod_graph.nodes[0].get_parameter("input").value = img
            eg = EvaluableGraph(mod_graph, verbose=False)
            eg.evaluate(array_format=format)
            for n in ["mlp_output_layer"]:
                out = eg.enodes[n].evaluable_outputs["out_port"].curr_value
                print("Output of evaluated graph: %s %s (%s)" %
                      (out, out.shape, type(out).__name__))
                prediction = np.argmax(out)

            match = target == int(prediction)
            if match:
                matches += 1
            print(
                f"Target: {target}, prediction: {prediction}, match: {match}")
        t = time.time() - start
        print(
            "Matches: %i/%i, accuracy: %s%%. Total time: %.4f sec (%.4fs per run)"
            % (
                matches,
                len(imgs_to_test),
                (100.0 * matches) / len(imgs_to_test),
                t,
                t / len(imgs_to_test),
            ))
示例#13
0
def main():

    dt = 5e-05
    file_path = "FN.mdf.json"
    data = convert_states_to_stateful_parameters("../" + file_path, dt)
    # print(data)
    with open("Translated_" + file_path, "w") as fp:
        json.dump(data, fp, indent=4)

    if "-run" in sys.argv:

        verbose = True

        mod_graph = load_mdf("Translated_%s" % file_path).graphs[0]
        eg = EvaluableGraph(mod_graph, verbose)

        mod_graph_old = load_mdf("../" + file_path).graphs[0]
        eg_old = EvaluableGraph(mod_graph_old, verbose)

        duration = 0.1
        t = 0
        recorded = {}
        times = []
        s = []
        vv = []
        ww = []

        vv_old = []
        ww_old = []

        while t <= duration + dt:
            print("======   Evaluating at t = %s  ======" % (t))

            vv.append(
                float(
                    eg.enodes["FNpop_0"].evaluable_parameters["V"].curr_value))
            ww.append(
                float(
                    eg.enodes["FNpop_0"].evaluable_parameters["W"].curr_value))

            # levels.append(eg.enodes['sine_node'].evaluable_stateful_parameters['level'].curr_value)

            # print("time first>>>",type(t))
            t = float(
                eg.enodes["FNpop_0"].evaluable_parameters["time"].curr_value)
            times.append(t)

            if t == 0:

                eg_old.evaluate()  # replace with initialize?
            else:

                eg_old.evaluate(time_increment=dt)

            vv_old.append(
                eg_old.enodes["FNpop_0"].evaluable_parameters["V"].curr_value)
            ww_old.append(
                eg_old.enodes["FNpop_0"].evaluable_parameters["W"].curr_value)

            eg.evaluate()

        print("Times>>>", times[:10])
        print("Translated file W and V>>>", ww[:10], vv[:10])

        print("Old file W and V>>>", ww_old[:10], vv_old[:10])

        import matplotlib.pyplot as plt

        plt.plot(times, vv, label="V")
        plt.plot(times, ww, label="W")
        plt.legend()
        plt.show()
        plt.savefig("translated_FN_stateful_vw_plot.jpg")
示例#14
0
文件: FNrun.py 项目: kmantel/MDF
def execute(multi=False):

    mdf_model = load_mdf("FN.mdf.yaml")
    mod_graph = mdf_model.graphs[0]

    dt = 0.00005
    duration = 0.1

    if not multi:

        fn_node = mod_graph.nodes[0]
        fn_node.get_parameter("initial_v").value = [-1.0]
        fn_node.get_parameter("initial_w").value = [0.0]
        input = np.array([0])

    else:
        size = 15
        max_amp = 0.5
        input = np.array(
            [max_amp * (-1 + 2 * i / size) for i in range(size + 1)])
        # input = [-0.4,-0.2, 0.,0.2,0.4]
        input_node = Node(id="input_node", parameters={"input_level": input})

        op1 = OutputPort(id="out_port", value="input_level")
        input_node.output_ports.append(op1)
        mod_graph.nodes.append(input_node)

        fn_node = mod_graph.nodes[0]
        fn_node.get_parameter("initial_v").value = np.array([1.0] * len(input))
        fn_node.get_parameter("initial_w").value = np.array([0.0] * len(input))

        print(fn_node)

        e1 = Edge(
            id="input_edge",
            sender=input_node.id,
            sender_port=op1.id,
            receiver="FNpop_0",
            receiver_port="INPUT",
        )

        mod_graph.edges.append(e1)

        mdf_model.to_graph_image(
            engine="dot",
            output_format="png",
            view_on_render=False,
            level=3,
            filename_root="FNmulti",
            only_warn_on_fail=
            True,  # Makes sure test of this doesn't fail on Windows on GitHub Actions
        )

        duration = 0.1

    eg = EvaluableGraph(mod_graph, verbose)
    # duration= 2
    t = 0

    times = []
    vv = {}
    ww = {}

    format = FORMAT_TENSORFLOW if "-tf" in sys.argv else FORMAT_NUMPY

    while t < duration + 0.00005:
        times.append(t)
        print("======   Evaluating at t = %s  ======" % (t))
        if t == 0:
            eg.evaluate(array_format=format)  # replace with initialize?
        else:
            eg.evaluate(array_format=format, time_increment=dt)

        for i in range(
                len(eg.enodes["FNpop_0"].evaluable_parameters["V"].curr_value)
        ):
            if not i in vv:
                vv[i] = []
                ww[i] = []
            v = eg.enodes["FNpop_0"].evaluable_parameters["V"].curr_value[i]
            w = eg.enodes["FNpop_0"].evaluable_parameters["W"].curr_value[i]
            vv[i].append(v)
            ww[i].append(w)
            if i == 0:
                print(f"    Value at {t}: v={v}, w={w}")
        t += dt

    import matplotlib.pyplot as plt

    for vi in vv:
        plt.plot(times, vv[vi], label="V %.3f" % input[vi])
        plt.plot(times, ww[vi], label="W %.3f" % input[vi])
    plt.legend()

    if not multi:
        plt.savefig("MDFFNrun.png", bbox_inches="tight")

    if not "-nogui" in sys.argv:
        plt.show()