apeforest commented on a change in pull request #11952: [MXNET-707] Add unit test for mxnet to coreml converter URL: https://github.com/apache/incubator-mxnet/pull/11952#discussion_r213404849
########## File path: tools/coreml/unittest/test_converter_no_pred.py ########## @@ -0,0 +1,970 @@ +# 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. + +import unittest +import mxnet as mx +import numpy as np + +from converter._mxnet_converter import convert +from collections import namedtuple +from converter import utils + +def _mxnet_remove_batch(input_data): + for blob in input_data: + input_data[blob] = np.reshape(input_data[blob], input_data[blob].shape[1:]) + return input_data + + +def _get_mxnet_module(net, data_shapes, mode, label_names, input_names=None): + """ Given a symbolic graph, input shape and the initialization mode, + returns an MXNet module. + """ + mx.random.seed(1993) + + mod = utils.create_module(sym=net, data_shapes=data_shapes, label_shapes=input_names, label_names=label_names) + + if mode == 'random': + mod.init_params( + initializer=mx.init.Uniform(scale=.1) + ) + elif mode == 'zeros': + mod.init_params( + initializer=mx.init.Zero() + ) + elif mode == 'ones': + mod.init_params( + initializer=mx.init.One() + ) + else: + Exception(KeyError("%s is not a valid initialization mode" % mode)) + + return mod + + +class SingleLayerTest(unittest.TestCase): + """ + Unit test class for testing where converter is able to convert individual layers or not. + In order to do so, it converts model and generates preds on both CoreML and MXNet and check they are the same. + """ + def _test_mxnet_model(self, net, input_shape, mode, class_labels=None, coreml_mode=None, label_names=None, delta=1e-3, + pre_processing_args=None, input_name='data'): + """ Helper method that convert the CoreML model into CoreML and compares the predictions over random data. + + Parameters + ---------- + net: MXNet Symbol Graph + The graph that we'll be converting into CoreML. + + input_shape: tuple of ints + The shape of input data. Generally of the format (batch-size, channels, height, width) + + mode: (random|zeros|ones) + The mode to use in order to set the parameters (weights and biases). + + label_names: list of strings + The names of the output labels. Default: None + + delta: float + The maximum difference b/w predictions of MXNet and CoreML that is tolerable. + + input_name: str + The name of the input variable to the symbolic graph. + """ + + data_shapes=[(input_name, input_shape)] + + mod = _get_mxnet_module(net, data_shapes, mode, label_names) + + # Generate some dummy data + input_data = {input_name: np.random.uniform(-10., 10., input_shape)} + Batch = namedtuple('Batch', ['data']) + mod.forward(Batch([mx.nd.array(input_data[input_name])])) + mxnet_preds = mod.get_outputs()[0].asnumpy().flatten() + + # Get predictions from coreml + coreml_model = convert( + model=mod, + class_labels=class_labels, + mode=coreml_mode, + input_shape={input_name: input_shape}, + preprocessor_args=pre_processing_args + ) + + def test_tiny_inner_product_zero_input(self): + np.random.seed(1988) + input_shape = (1, 10) + net = mx.sym.Variable('data') + net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=5) + self._test_mxnet_model(net, input_shape=input_shape, mode='zeros') + + def test_really_tiny_inner_product_ones_input(self): Review comment: Added for all ---------------------------------------------------------------- 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