spidyDev commented on a change in pull request #11213: [MXNET-533] MXNet-ONNX 
export
URL: https://github.com/apache/incubator-mxnet/pull/11213#discussion_r195505069
 
 

 ##########
 File path: tests/python-pytest/onnx/export/backend.py
 ##########
 @@ -0,0 +1,98 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+"""backend wrapper for onnx test infrastructure"""
+import mxnet as mx
+import numpy as np
+from mxnet.contrib.onnx._import.import_onnx import GraphProto
+from mxnet.contrib.onnx._export.export_onnx import MXNetGraph
+try:
+    from onnx import helper, TensorProto, mapping
+    from onnx.backend.base import Backend
+except ImportError:
+    raise ImportError("Onnx and protobuf need to be installed")
+from backend_rep import MXNetBackendRep
+
+# Using these functions for onnx test infrastructure.
+# Implemented by following onnx docs guide:
+# 
https://github.com/onnx/onnx/blob/master/docs/Implementing%20an%20ONNX%20backend.md
+# MXNetBackend class will take an ONNX model with inputs, perform a 
computation,
+# and then return the output.
+
+class MXNetBackend(Backend):
+    """MXNet backend for ONNX"""
+
+    @staticmethod
+    def perform_import_export(graph_proto, input_shape):
+        """ Import ONNX model to mxnet model and then export to ONNX model
+            and then import it back to mxnet for verifying the result"""
+        graph = GraphProto()
+
+        sym, arg_params, aux_params = graph.from_onnx(graph_proto)
+
+        params = {}
+        params.update(arg_params)
+        params.update(aux_params)
+        # exporting to onnx graph proto format
+        converter = MXNetGraph()
+        graph_proto = converter.create_onnx_graph_proto(sym, params, 
in_shape=input_shape, 
in_type=mapping.NP_TYPE_TO_TENSOR_TYPE[np.dtype('float32')])
+
+        # importing back to MXNET for verifying result.
+        sym, arg_params, aux_params = graph.from_onnx(graph_proto)
+
+        return sym, arg_params, aux_params
+
+
+    @classmethod
+    def prepare(cls, model, device='CPU', **kwargs):
 
 Review comment:
   this method is declared by ONNX. The backends using ONNX test framework 
derives from "backend" class and implements these functions.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to