def test_spacetodepth(): n, c, h, w = shape = (1, 1, 4, 6) input1 = np.random.rand(n, c, h, w).astype("float32") blocksize = 2 inputs = [ helper.make_tensor_value_info("input1", TensorProto.FLOAT, shape=shape) ] outputs = [ helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=(1, 4, 2, 3)) ] nodes = [ helper.make_node("SpaceToDepth", ["input1"], ["output"], block_size=blocksize) ] graph = helper.make_graph(nodes, "spacetodepth_test", inputs, outputs) spacetodepth_model = helper.make_model(graph) bkd_rep = backend.prepare(spacetodepth_model) output = bkd_rep.run([input1]) tmp = np.reshape( input1, [n, c, h // blocksize, blocksize, w // blocksize, blocksize]) tmp = np.transpose(tmp, [0, 3, 5, 1, 2, 4]) numpy_op = np.reshape( tmp, [n, c * (blocksize**2), h // blocksize, w // blocksize]) npt.assert_almost_equal(output[0], numpy_op)
def test_spacetodepth(): n, c, h, w = shape = (1, 1, 4, 6) input1 = np.random.rand(n, c, h, w).astype("float32") blocksize = 2 inputs = [helper.make_tensor_value_info("input1", TensorProto.FLOAT, shape=shape)] outputs = [helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=(1, 4, 2, 3))] nodes = [helper.make_node("SpaceToDepth", ["input1"], ["output"], block_size=blocksize)] graph = helper.make_graph(nodes, "spacetodepth_test", inputs, outputs) spacetodepth_model = helper.make_model(graph) bkd_rep = backend.prepare(spacetodepth_model) output = bkd_rep.run([input1]) tmp = np.reshape(input1, [n, c, h // blocksize, blocksize, w // blocksize, blocksize]) tmp = np.transpose(tmp, [0, 3, 5, 1, 2, 4]) numpy_op = np.reshape(tmp, [n, c * (blocksize**2), h // blocksize, w // blocksize]) npt.assert_almost_equal(output[0], numpy_op)
def test_import_export(self): def get_input_tensors(input_data): input_tensor = [] input_names = [] input_sym = [] for idx, ip in enumerate(input_data): name = "input" + str(idx + 1) input_sym.append(mx.sym.Variable(name)) input_names.append(name) input_tensor.append( helper.make_tensor_value_info(name, TensorProto.FLOAT, shape=np.shape(ip))) return input_names, input_tensor, input_sym def get_onnx_graph(testname, input_names, inputs, output_name, output_shape, attr): outputs = [ helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=output_shape) ] nodes = [ helper.make_node(output_name, input_names, ["output"], **attr) ] graph = helper.make_graph(nodes, testname, inputs, outputs) model = helper.make_model(graph) return model for test in test_cases: test_name, mxnet_op, onnx_name, inputs, attrs, mxnet_specific, fix_attrs, check_value, check_shape = test with self.subTest(test_name): names, input_tensors, inputsym = get_input_tensors(inputs) test_op = mxnet_op(*inputsym, **attrs) mxnet_output = forward_pass(test_op, None, None, names, inputs) outputshape = np.shape(mxnet_output) if mxnet_specific: onnxmodelfile = onnx_mxnet.export_model( test_op, {}, [np.shape(ip) for ip in inputs], np.float32, onnx_name + ".onnx") onnxmodel = load_model(onnxmodelfile) else: onnx_attrs = _fix_attributes(attrs, fix_attrs) onnxmodel = get_onnx_graph(test_name, names, input_tensors, onnx_name, outputshape, onnx_attrs) bkd_rep = backend.prepare(onnxmodel, operation='export') output = bkd_rep.run(inputs) if check_value: npt.assert_almost_equal(output[0], mxnet_output) if check_shape: npt.assert_equal(output[0].shape, outputshape)
def test_import_export(self): for test in test_cases: test_name, mxnet_op, onnx_name, inputs, attrs, mxnet_specific, fix_attrs, check_value, check_shape = test with self.subTest(test_name): names, input_tensors, inputsym = get_input_tensors(inputs) if inputs: test_op = mxnet_op(*inputsym, **attrs) mxnet_output = forward_pass(test_op, None, None, names, inputs) outputshape = np.shape(mxnet_output) else: test_op = mxnet_op(**attrs) shape = attrs.get('shape', (1,)) x = mx.nd.zeros(shape, dtype='float32') xgrad = mx.nd.zeros(shape, dtype='float32') exe = test_op.bind(ctx=mx.cpu(), args={'x': x}, args_grad={'x': xgrad}) mxnet_output = exe.forward(is_train=False)[0].asnumpy() outputshape = np.shape(mxnet_output) if mxnet_specific: onnxmodelfile = onnx_mxnet.export_model(test_op, {}, [np.shape(ip) for ip in inputs], np.float32, onnx_name + ".onnx") onnxmodel = load_model(onnxmodelfile) else: onnx_attrs = _fix_attributes(attrs, fix_attrs) onnxmodel = get_onnx_graph(test_name, names, input_tensors, onnx_name, outputshape, onnx_attrs) bkd_rep = backend.prepare(onnxmodel, operation='export') output = bkd_rep.run(inputs) if check_value: npt.assert_almost_equal(output[0], mxnet_output) if check_shape: npt.assert_equal(output[0].shape, outputshape) input1 = get_rnd((1, 10, 2, 3)) ipsym = mx.sym.Variable("input1") for test in test_scalar_ops: if test == 'Add': outsym = 2 + ipsym if test == "Sub": outsym = ipsym - 2 if test == "rSub": outsym = ipsym.__rsub__(2) if test == "Mul": outsym = 2 * ipsym if test == "Div": outsym = ipsym / 2 if test == "Pow": outsym = ipsym ** 2 forward_op = forward_pass(outsym, None, None, ['input1'], input1) converted_model = onnx_mxnet.export_model(outsym, {}, [np.shape(input1)], np.float32, onnx_file_path=outsym.name + ".onnx") sym, arg_params, aux_params = onnx_mxnet.import_model(converted_model) result = forward_pass(sym, arg_params, aux_params, ['input1'], input1) npt.assert_almost_equal(result, forward_op)
def test_import_export(self): for test in test_cases: test_name, mxnet_op, onnx_name, inputs, attrs, mxnet_specific, fix_attrs, check_value, check_shape = test with self.subTest(test_name): names, input_tensors, inputsym = get_input_tensors(inputs) if inputs: test_op = mxnet_op(*inputsym, **attrs) mxnet_output = forward_pass(test_op, None, None, names, inputs) outputshape = np.shape(mxnet_output) else: test_op = mxnet_op(**attrs) shape = attrs.get('shape', (1,)) x = mx.nd.zeros(shape, dtype='float32') xgrad = mx.nd.zeros(shape, dtype='float32') exe = test_op.bind(ctx=mx.cpu(), args={'x': x}, args_grad={'x': xgrad}) mxnet_output = exe.forward(is_train=False)[0].asnumpy() outputshape = np.shape(mxnet_output) if mxnet_specific: onnxmodelfile = onnx_mxnet.export_model(test_op, {}, [np.shape(ip) for ip in inputs], np.float32, onnx_name + ".onnx") onnxmodel = load_model(onnxmodelfile) else: onnx_attrs = _fix_attributes(attrs, fix_attrs) onnxmodel = get_onnx_graph(test_name, names, input_tensors, onnx_name, outputshape, onnx_attrs) bkd_rep = backend.prepare(onnxmodel, operation='export') output = bkd_rep.run(inputs) if check_value: npt.assert_almost_equal(output[0], mxnet_output) if check_shape: npt.assert_equal(output[0].shape, outputshape) input1 = get_rnd((1, 10, 2, 3)) ipsym = mx.sym.Variable("input1") for test in test_scalar_ops: if test == 'Add': outsym = 2 + ipsym if test == "Sub": outsym = ipsym - 2 if test == "rSub": outsym = ipsym.__rsub__(2) if test == "Mul": outsym = 2 * ipsym if test == "Div": outsym = ipsym / 2 if test == "Pow": outsym = ipsym ** 2 forward_op = forward_pass(outsym, None, None, ['input1'], input1) converted_model = onnx_mxnet.export_model(outsym, {}, [np.shape(input1)], np.float32, onnx_file_path=outsym.name + ".onnx") sym, arg_params, aux_params = onnx_mxnet.import_model(converted_model) result = forward_pass(sym, arg_params, aux_params, ['input1'], input1) npt.assert_almost_equal(result, forward_op)
def test_ops(op_name, inputs, input_tensors, numpy_op): outputs = [helper.make_tensor_value_info("output", TensorProto.FLOAT, shape=np.shape(inputs[0]))] nodes = [helper.make_node(op_name, ["input"+str(i+1) for i in range(len(inputs))], ["output"])] graph = helper.make_graph(nodes, op_name + "_test", input_tensors, outputs) model = helper.make_model(graph) bkd_rep = backend.prepare(model) output = bkd_rep.run(inputs) npt.assert_almost_equal(output[0], numpy_op)
def test_imports(self): for test in import_test_cases: test_name, onnx_name, inputs, np_op, attrs = test with self.subTest(test_name): names, input_tensors, inputsym = get_input_tensors(inputs) np_out = [np_op(*inputs, **attrs)] output_shape = np.shape(np_out) onnx_model = get_onnx_graph(test_name, names, input_tensors, onnx_name, output_shape, attrs) bkd_rep = backend.prepare(onnx_model, operation='import') mxnet_out = bkd_rep.run(inputs) npt.assert_almost_equal(np_out, mxnet_out)
def test_imports(self): for test in import_test_cases: test_name, onnx_name, inputs, np_op, attrs = test with self.subTest(test_name): names, input_tensors, inputsym = get_input_tensors(inputs) np_out = [np_op(*inputs, **attrs)] output_shape = np.shape(np_out) onnx_model = get_onnx_graph(test_name, names, input_tensors, onnx_name, output_shape, attrs) bkd_rep = backend.prepare(onnx_model, operation='import') mxnet_out = bkd_rep.run(inputs) npt.assert_almost_equal(np_out, mxnet_out, decimal=4)
def convert(infile, outfile, **kwargs): """Convert pb. Args: infile: Input path. outfile: Output path. **kwargs: Other args for converting. Returns: None. """ common.logger.setLevel(kwargs.get("logging_level", "INFO")) common.logger.info("Start converting onnx pb to tf pb:") onnx_model = onnx.load(infile) tf_rep = backend.prepare(onnx_model, **kwargs) tf_rep.export_graph(outfile) common.logger.info("Converting completes successfully.")
import onnx import backend import numpy as np model = onnx.load("Bisenet_nearest.onnx") engine = backend.prepare(model, device='CUDA:1') input_data = np.random.random(size=(32, 3, 224, 224)).astype(np.float32) output_data = engine.run(input_data)[0] print(output_data) print(output_data.shape)