def test_with_statement_graph_def_test_name(module_creator): """ This case tests specifying the name of a graph. """ 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 with nn.graph_def.graph(name="test_net") as g: outputs = module(*proto_variable_inputs) # create variable inputs and initialized by random value variable_inputs = module_creator.get_variable_inputs() # create network by module-like graph_def # Access the network directly by network name outputs = g.test_net(*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)
def test_get_graph_def_by_variable(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 graph_def = nn.graph_def.get_default_graph_by_variable(outputs) # create variable inputs and initialized by random value variable_inputs = module_creator.get_variable_inputs() # create network by module-like graph_def outputs = graph_def(*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)
def test_save_load_consistency(module_creator): 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 with nn.graph_def.graph() as g: outputs = module(*proto_variable_inputs) with create_temp_with_dir(nnp_file) as tmp_file: g.save(tmp_file) g = nn.graph_def.load(tmp_file) # 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)
def test_context_priority_hierarchy_testing(module_func, backend): context_backend = '' func, in_shapes = module_func with nn.graph_def.graph() as g: inputs = [nn.ProtoVariable(shape) for shape in in_shapes] outputs = func(*inputs) import nnabla_ext.cpu nn.set_default_context(nnabla_ext.cpu.context()) if backend == 'cpu': ctx = nnabla_ext.cpu.context() elif backend == 'cuda': try: import nnabla_ext.cuda ctx = nnabla_ext.cuda.context() context_backend = 'Cuda' except ImportError: ctx = nnabla_ext.cpu.context() elif backend == 'cudnn': try: import nnabla_ext.cudnn ctx = nnabla_ext.cudnn.context() context_backend = 'Cudnn' except ImportError: ctx = nnabla_ext.cpu.context() g.current_context = ctx g() for f in g.default_graph().forward_sequence(): if context_backend: assert ('Cuda' in f.function_instance.name)
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)
def test_create_graph_def_without_using_call(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 with nn.parameter_scope('', module.parameter_scope): # Here, if user does not call module(), instead, they call a self-defined # function, we should not produce strange result. outputs = module.no_call(*proto_variable_inputs) # find out graph_def in global default graph graph_def = nn.graph_def.get_default_graph() # create variable inputs and initialized by random value variable_inputs = module_creator.get_variable_inputs() # create network by module-like graph_def outputs = graph_def(*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)
def get_proto_variable_inputs(self): if self.proto_variable_inputs is None: proto_variable_inputs = [nn.ProtoVariable( shape) for shape in self.input_shape] self.proto_variable_inputs = proto_variable_inputs return proto_variable_inputs return self.proto_variable_inputs
def get_saved_test_model(module): module_func, module_input_shapes = module with create_temp_with_dir(NNP_FILE) as nnp_file: with nn.graph_def.graph() as g: variables = [ nn.ProtoVariable(shape) for _, shape in module_input_shapes ] outputs = module_func(*variables) g.save(nnp_file) nn.clear_parameters() yield nnp_file
def test_flat_module_with_statement(module_func): '''This case is used to test creating graph_def by passing nn.ProtoVariable() to a flat-style model. ''' func, in_shapes = module_func with nn.graph_def.graph() as g: inputs = [nn.ProtoVariable(shape) for shape in in_shapes] outputs = func(*inputs) g.save(nnp_file) inputs = [nn.Variable(shape) for shape in in_shapes] for i in inputs: i.d = np.random.random(i.shape) outputs = g(*inputs) outputs_ref = func(*inputs) forward_variable_and_check_equal(outputs, outputs_ref)
def test_another_shape_input(module_creator, another_input_shape): # 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() input = nn.Variable(another_input_shape) output = g(input) output.forward()
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)
def test_flat_module_support(module_func): '''This case is used to test creating graph_def by passing nn.ProtoVariable() to a flat-style model. ''' func, in_shapes = module_func inputs = [nn.ProtoVariable(shape) for shape in in_shapes] outputs = func(*inputs) if not isinstance(outputs, tuple): outputs = (outputs, ) g = nn.graph_def.get_default_graph_by_variable(outputs[0]) g.save(nnp_file) g = nn.graph_def.load(nnp_file) inputs = [nn.Variable(shape) for shape in in_shapes] for i in inputs: i.d = np.random.random(i.shape) outputs = g(*inputs) outputs_ref = func(*inputs) forward_variable_and_check_equal(outputs, outputs_ref)
def test_sequential(TSTNet): tst_net = TSTNet() input_shape = (2, 3, 16, 16) input = nn.Variable.from_numpy_array((np.random.random(input_shape))) y = tst_net(input) y.forward() x = nn.ProtoVariable(input_shape) with nn.graph_def.graph() as g1: h = tst_net(x) with create_temp_with_dir(nnp_file) as tmp_file: g1.save(tmp_file) g2 = nn.graph_def.load(tmp_file) for net in g2.networks.values(): out = net(input) out.forward() # should equal assert_allclose(out.d, y.d)
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)
def test_with_statement_graph_def_name(module_creator, network_name): # get module from test module 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 with nn.graph_def.graph(name=network_name) as g: outputs = module(*proto_variable_inputs) # 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(*variable_inputs) # check if outputs are equal forward_variable_and_check_equal(outputs, ref_outputs)
def test_iterator_through_forward_sequence(module_func): func, in_shapes = module_func with nn.graph_def.graph() as g: inputs = [nn.ProtoVariable(shape) for shape in in_shapes] outputs = func(*inputs) inputs = [nn.Variable(shape) for shape in in_shapes] for i in inputs: i.d = np.random.random(i.shape) outputs_ref = func(*inputs) if not isinstance(outputs_ref, tuple): outputs_ref = (outputs_ref, ) output = F.sink(*outputs_ref) forward_sequence = [] def visit_func(f): if f.name != 'Sink': forward_sequence.append(f.name) output.visit(visit_func) for a, b in zip(g.default_graph().forward_sequence(), forward_sequence): assert a.type == b
def save_model_from_graph_def_save(nnp_file, model_def, input_shape, variable_batch_size): with nn.graph_def.graph() as g: x = nn.ProtoVariable(input_shape) y = model_def(x) g.save(nnp_file, variable_batch_size=variable_batch_size)