Ejemplo n.º 1
0
def test_multiple_network():
    """This cases assume that user create network multiple times in a ProtoGraph.
    Because no graph_name() is called to name each network, all computation graph
    operators are collected into a network, it looks like multiple networks
    are merged into a network. In testing time, we only need to feed concatenated
    inputs to this network and check concatenate outputs.
    """
    module_creators = [ModuleCreator(TSTNetNormal(), [(4, 3, 32, 32), (4, 3, 32, 32)]),
                       ModuleCreator(ResUnit(16), [(4, 3, 32, 32)]),
                       ModuleCreator(NestedTestNet(), [(4, 3, 32, 32), (4, 3, 32, 32)])]

    # create graph_def by passing proto_variables as inputs
    with nn.graph_def.graph() as g:
        for module_creator in module_creators:
            module = module_creator.module

            # create proto variables as inputs
            proto_variable_inputs = [nn.ProtoVariable(
                shape) for shape in module_creator.input_shape]

            # generate graph
            outputs = module(*proto_variable_inputs)

    for module_creator, network in zip(module_creators, g.networks.values()):
        # create variable inputs and initialized by random value
        variable_inputs = module_creator.get_variable_inputs()

        # create network by module-like graph_def
        outputs = network(*variable_inputs)

        # create reference network by passing in variable inputs
        ref_outputs = module_creator.module(*variable_inputs)

        # check if outputs are equal
        forward_variable_and_check_equal(outputs, ref_outputs)
Ejemplo n.º 2
0
def test_get_default_graph_def_by_name():
    """This case tests retrieving graph using nn.graph_def.get_default_graph() by
    specifying the name of graph.
    """
    module_creators = [ModuleCreator(TSTNetNormal(), [(4, 3, 32, 32), (4, 3, 32, 32)]),
                       ModuleCreator(ResUnit(16), [(4, 3, 32, 32)]),
                       ModuleCreator(NestedTestNet(), [(4, 3, 32, 32), (4, 3, 32, 32)])]
    network_names = [
        'network1',
        'network2',
        'network3'
    ]

    for module_creator, network_name in zip(module_creators, network_names):
        module = module_creator.module

        # create proto variables as inputs
        proto_variable_inputs = [nn.ProtoVariable(
            shape) for shape in module_creator.input_shape]

        with nn.graph_def.graph_name(network_name):
            # generate graph
            outputs = module(*proto_variable_inputs)

    for module_creator, network_name in zip(module_creators, network_names):
        # create variable inputs and initialized by random value
        variable_inputs = module_creator.get_variable_inputs()

        # get graph from default by name
        g = nn.graph_def.get_default_graph(network_name)

        # create network by module-like graph_def
        outputs = g(*variable_inputs)

        # create reference network by passing in variable inputs
        ref_outputs = module_creator.module(*variable_inputs)

        # check if outputs are equal
        forward_variable_and_check_equal(outputs, ref_outputs)
Ejemplo n.º 3
0
def test_get_graph_def_by_name():
    """This cases assume that user creates multiple networks in a ProtoGraph.
    User may specify the name of network(graph) they created by graph_name().
    """
    module_creators = [
        ModuleCreator(TSTNetNormal(), [(4, 3, 32, 32), (4, 3, 32, 32)]),
        ModuleCreator(ResUnit(16), [(4, 3, 32, 32)]),
        ModuleCreator(NestedTestNet(), [(4, 3, 32, 32), (4, 3, 32, 32)])
    ]
    network_names = ['network1', 'network2', 'network3']

    # create graph_def by passing proto_variables as inputs
    with nn.graph_def.graph() as g:
        for module_creator, network_name in zip(module_creators,
                                                network_names):
            module = module_creator.module

            # create proto variables as inputs
            proto_variable_inputs = [
                nn.ProtoVariable(shape) for shape in module_creator.input_shape
            ]

            with nn.graph_def.graph_name(network_name):
                # generate graph
                outputs = module(*proto_variable_inputs)

    for module_creator, network_name in zip(module_creators, network_names):
        # create variable inputs and initialized by random value
        variable_inputs = module_creator.get_variable_inputs()

        # create network by module-like graph_def
        outputs = g[network_name](*variable_inputs)

        # create reference network by passing in variable inputs
        ref_outputs = module_creator.module(*variable_inputs)

        # check if outputs are equal
        forward_variable_and_check_equal(outputs, ref_outputs)
Ejemplo n.º 4
0
    # create variable inputs and initialized by random value
    variable_inputs = module_creator.get_variable_inputs()

    # create network by module-like graph_def
    outputs = g(*variable_inputs)

    # create reference network by passing in variable inputs
    ref_outputs = module(*variable_inputs)

    # check if outputs are equal
    forward_variable_and_check_equal(outputs, ref_outputs)


@pytest.mark.parametrize("module_creator", [
    ModuleCreator(TSTNetNormal(), [(4, 3, 32, 32), (4, 3, 32, 32)]),
    ModuleCreator(ResUnit(16), [(4, 3, 32, 32)]),
    ModuleCreator(NestedTestNet(), [(4, 3, 32, 32), (4, 3, 32, 32)])
])
def test_simple_create_graph_def(module_creator):
    # get module from test parameters
    module = module_creator.module

    # create proto variables as inputs
    proto_variable_inputs = [
        nn.ProtoVariable(shape) for shape in module_creator.input_shape
    ]

    # create graph_def by passing proto_variables as inputs
    outputs = module(*proto_variable_inputs)

    # find out graph_def in global default graph
Ejemplo n.º 5
0
    # create variable inputs and initialized by random value
    variable_inputs = module_creator.get_variable_inputs()

    # create network by module-like graph_def
    outputs = g(*variable_inputs)

    # create reference network by passing in variable inputs
    ref_outputs = module(*variable_inputs)

    # check if outputs are equal
    forward_variable_and_check_equal(outputs, ref_outputs)


@pytest.mark.parametrize("module_creator", [ModuleCreator(TSTNetNormal(), [(4, 3, 32, 32), (4, 3, 32, 32)]),
                                            ModuleCreator(
                                                ResUnit(16), [(4, 3, 32, 32)]),
                                            ModuleCreator(NestedTestNet(), [(4, 3, 32, 32), (4, 3, 32, 32)])])
def test_simple_create_graph_def(module_creator):
    # get module from test parameters
    module = module_creator.module

    # create proto variables as inputs
    proto_variable_inputs = [nn.ProtoVariable(
        shape) for shape in module_creator.input_shape]

    # create graph_def by passing proto_variables as inputs
    outputs = module(*proto_variable_inputs)

    # find out graph_def in global default graph
    g = nn.graph_def.get_default_graph()