def test_initializer(self): X = np.array([[1, 2], [3, 4]]).astype(np.float32) Y = np.array([[1, 2], [3, 4]]).astype(np.float32) weight = np.array([[1, 0], [0, 1]]) graph_def = make_graph( [make_node("Add", ["X", "Y"], ["Z0"]), make_node("Cast", ["Z0"], ["Z"], to="float"), make_node("Mul", ["Z", "weight"], ["W0"]), make_node("Tanh", ["W0"], ["W1"]), make_node("Sigmoid", ["W1"], ["W2"]), make_node("Scale", ["W2"], ["W3"], scale=-1.0)], name="test_initializer", inputs=[ make_tensor_value_info("X", onnx.TensorProto.FLOAT, (2, 2)), make_tensor_value_info("Y", onnx.TensorProto.FLOAT, (2, 2)), make_tensor_value_info("weight", onnx.TensorProto.FLOAT, (2, 2)), ], outputs=[ make_tensor_value_info("W3", onnx.TensorProto.FLOAT, (2, 2)) ], initializer=[make_tensor("weight", onnx.TensorProto.FLOAT, [2, 2], weight.flatten().astype(float))] ) def sigmoid(x): return 1 / (1 + np.exp(-x)) W_ref = -sigmoid(np.tanh((X + Y) * weight)) c2_rep = c2.prepare(make_model(graph_def)) output = c2_rep.run({"X": X, "Y": Y}) np.testing.assert_almost_equal(output["W3"], W_ref)
def caffe2_net_to_onnx_model(cls, *args, **kwargs): model = make_model(cls.caffe2_net_to_onnx_graph(*args, **kwargs)) opset_id = OperatorSetIdProto() opset_id.domain = '' # ONNX opset_id.version = cls._target_opset_version model.opset_import.extend([opset_id]) checker.check_model(model) return model
def test_relu_graph(self): X = np.random.randn(3, 2).astype(np.float32) Y_ref = np.clip(X, 0, np.inf) node_def = make_node( "Relu", ["X"], ["Y"]) output = c2.run_node( node_def, {"X": X}) np.testing.assert_almost_equal(output.Y, Y_ref) graph_def = make_graph( [node_def], name="test", inputs=[make_tensor_value_info("X", onnx.TensorProto.FLOAT, [3, 2])], outputs=[make_tensor_value_info("Y", onnx.TensorProto.FLOAT, [3, 2])]) c2_rep = c2.prepare(make_model(graph_def)) output = c2_rep.run(X) np.testing.assert_almost_equal(output.Y, Y_ref)
def test_onnx_to_caffe2(self): onnx_model = tempfile.NamedTemporaryFile() output = tempfile.NamedTemporaryFile() init_net_output = tempfile.NamedTemporaryFile() node_def = helper.make_node( "Mul", ["X", "W"], ["Y"]) graph_def = helper.make_graph( [node_def], "test", [helper.make_tensor_value_info("X", TensorProto.FLOAT, (2, 3)), helper.make_tensor_value_info("W", TensorProto.FLOAT, (3, 2))], [helper.make_tensor_value_info("Y", TensorProto.FLOAT, (2, 2))], initializer=[helper.make_tensor("W", TensorProto.FLOAT, [3, 2], np.zeros((3, 2)).flatten().astype(float))]) model_def = make_model(graph_def, producer_name='onnx-to-caffe2-test') onnx_model.write(model_def.SerializeToString()) onnx_model.flush() result = self._run_command( onnx_to_caffe2, [ onnx_model.name, '--output', output.name, '--init-net-output', init_net_output.name, ]) caffe2_net = caffe2_pb2.NetDef() caffe2_net.ParseFromString(output.read()) self.assertEqual(len(caffe2_net.op), 1) self.assertEqual(caffe2_net.op[0].type, 'Mul') caffe2_init_net = caffe2_pb2.NetDef() caffe2_init_net.ParseFromString(init_net_output.read()) self.assertEqual(len(caffe2_init_net.op), 1) self.assertEqual(set(sum([list(init_op.output) for init_op in caffe2_init_net.op], [])), {'W'})