Ejemplo n.º 1
0
def verify_onnx_forward_impl(model_path, input_data, output_data):
    """Verifies result after inference"""
    print("Converting onnx format to mxnet's symbol and params...")
    sym, params = onnx_mxnet.import_model(model_path)

    # create module
    mod = mx.mod.Module(symbol=sym,
                        data_names=['input_0'],
                        context=mx.cpu(),
                        label_names=None)
    mod.bind(for_training=False,
             data_shapes=[('input_0', input_data.shape)],
             label_shapes=None)
    mod.set_params(arg_params=params,
                   aux_params=params,
                   allow_missing=True,
                   allow_extra=True)
    # run inference
    Batch = namedtuple('Batch', ['data'])

    mod.forward(Batch([mx.nd.array(input_data)]), is_train=False)

    # Run the model with an onnx backend and verify the results
    npt.assert_equal(mod.get_outputs()[0].shape, output_data.shape)
    npt.assert_almost_equal(output_data,
                            mod.get_outputs()[0].asnumpy(),
                            decimal=3)
    print("Conversion Successful")
Ejemplo n.º 2
0
def check_output(model, x):
    with tempfile.NamedTemporaryFile('wb') as fp:
        onnx_chainer.export(model, x, fp)

        sym, params = onnx_mxnet.import_model(fp.name)

        mod = mx.mod.Module(symbol=sym,
                            data_names=['input_0'],
                            context=mx.cpu(),
                            label_names=None)
        mod.bind(for_training=False,
                 data_shapes=[('input_0', x.shape)],
                 label_shapes=None)
        mod.set_params(arg_params=params, aux_params=None, allow_missing=True)

        Batch = namedtuple('Batch', ['data'])
        mod.forward(Batch([mx.nd.array(x)]))

        mxnet_out = mod.get_outputs()[0].asnumpy()

        y = model(x)
        if isinstance(y, dict):
            y = y['prob']
        chainer_out = y.array

        np.testing.assert_almost_equal(chainer_out, mxnet_out, decimal=5)
Ejemplo n.º 3
0
def convert_to_mxnet(model_path):
    onnx_file = validate_onnx(model_path)

    if onnx_file:
        import onnx_mxnet
        model_name = os.path.splitext(os.path.split(onnx_file)[-1])[0]
        sym, params = onnx_mxnet.import_model(onnx_file)
        sym_file = open(os.path.join(model_path,'%s-symbol.json' % model_name), 'w')
        sym_file.write(sym.tojson())
        save_dict = {('arg:%s' % k) : v.as_in_context(mx.cpu()) for k, v in params.items()}
        mx.nd.save(os.path.join(model_path,'%s-0000.params' % model_name), save_dict)
Ejemplo n.º 4
0
def convert_onnx_model(model_path, onnx_file):
    import onnx_mxnet
    model_name = os.path.splitext(os.path.basename(onnx_file))[0]
    symbol_file = '%s-symbol.json' % model_name
    params_file = '%s-0000.params' % model_name

    sym, params = onnx_mxnet.import_model(os.path.join(model_path, onnx_file))
    with open(os.path.join(model_path, symbol_file), 'w') as f:
        f.write(sym.tojson())

    save_dict = {('arg:%s' % k): v.as_in_context(mx.cpu()) for k, v in params.items()}
    mx.nd.save(os.path.join(model_path, params_file), save_dict)
    return symbol_file, params_file
Ejemplo n.º 5
0
def convert_onnx_model(model_path, onnx_file):
    import onnx_mxnet
    model_name = os.path.splitext(os.path.basename(onnx_file))[0]
    symbol_file = '%s-symbol.json' % model_name
    params_file = '%s-0000.params' % model_name

    sym, params = onnx_mxnet.import_model(os.path.join(model_path, onnx_file))
    with open(os.path.join(model_path, symbol_file), 'w') as f:
        f.write(sym.tojson())

    save_dict = {('arg:%s' % k): v.as_in_context(mx.cpu())
                 for k, v in params.items()}
    mx.nd.save(os.path.join(model_path, params_file), save_dict)
    return symbol_file, params_file
Ejemplo n.º 6
0
    def init(self):
        # print ("Face classifier init!")
        self.window = None
        self.Face = {
            "Outline":list(range(0, 16)),
            "LeftEyebrow" : list(range(16, 22)),
            "RightEyebrow" : list(range(22, 28)),
            "RightEyeTop" : [28],
            "LeftEyeTop" : [29],
            "LeftEye" : list(range(30,38)),
            "LeftPupil" : [38],
            "RightPupil" : [39],
            "RightEye" : list(range(40, 48)),
            #"NoseBridge" : list(range(48, 51)),
            "NoseNostrils" : list(range(48, 59)),
            "UpperLip" : list(range(59, 69)),
            "LowerLip" : list(range(69, 77)),
        }

        self.deadZones = None
        self.actions = {
            "MouthOpen": False,
            "Left": False,
            "Right": False,
            "Up": False,
            "Down": False,
        }

        # this won't fail if you give it the wrong file path :/
        self.faceCascade = cv2.CascadeClassifier("support_programs/haarcascade_frontalface_default.xml")

        self.sym, self.arg = onnx_mxnet.import_model('support_programs/NN_V03_01.onnx')

        test_image = np.zeros((350,350))
        # (1, 1, 350, 350)
        # (480, 640, 3)

        self.data_names = [graph_input for graph_input in self.sym.list_inputs()
                    if graph_input not in self.arg]

        self.mod = mx.mod.Module(symbol=self.sym, data_names=self.data_names, context=mx.cpu(), label_names=None)
        self.mod.bind(for_training=False, data_shapes=[(self.data_names[0], test_image.shape)], label_shapes=None)
        self.mod.set_params(arg_params=self.arg, aux_params=self.arg, allow_missing=True, allow_extra=True)

        self.Batch = namedtuple('Batch', ['data'])
        self.output_labels = ["anger", "neutral", "scream", "smile"]
        
        self.nn_results = {}

        self.start = time.clock()
Ejemplo n.º 7
0
def save_as_onnx_then_import_from_mxnet(model, fn):
    x = np.random.randn(1, 3, 224, 224).astype(np.float32)
    chainer_out = model(x)['prob'].array

    onnx_chainer.export(model, x, fn)

    sym, params = onnx_mxnet.import_model(fn)

    mod = mx.mod.Module(
        symbol=sym, data_names=['input_0'], context=mx.cpu(), label_names=None)
    mod.bind(
        for_training=False, data_shapes=[('input_0', x.shape)],
        label_shapes=None)
    mod.set_params(arg_params=params, aux_params=None, allow_missing=True)

    Batch = namedtuple('Batch', ['data'])
    mod.forward(Batch([mx.nd.array(x)]))

    mxnet_out = mod.get_outputs()[0].asnumpy()

    print(mxnet_out.shape)

    np.testing.assert_almost_equal(
        chainer_out, mxnet_out, decimal=5)
Ejemplo n.º 8
0
"""Testing super_resolution model conversion"""
from __future__ import absolute_import as _abs
from __future__ import print_function
from collections import namedtuple
import mxnet as mx
from mxnet.test_utils import download
import numpy as np
from PIL import Image
import onnx_mxnet

model_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_resolution.onnx'

download(model_url, 'super_resolution.onnx')

print("Converting onnx format to mxnet's symbol and params...")
sym, params = onnx_mxnet.import_model('super_resolution.onnx')

# Load test image
input_image_dim = 224
output_image_dim = 672
img_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_res_input.jpg'
download(img_url, 'super_res_input.jpg')
img = Image.open('super_res_input.jpg').resize(
    (input_image_dim, input_image_dim))
img_ycbcr = img.convert("YCbCr")
img_y, img_cb, img_cr = img_ycbcr.split()
x = np.array(img_y)[np.newaxis, np.newaxis, :, :]

# create module
mod = mx.mod.Module(symbol=sym, data_names=['input_0'], label_names=None)
mod.bind(for_training=False, data_shapes=[('input_0', x.shape)])
Ejemplo n.º 9
0
  def __init__(self, model_name, model_base_path, verbose=False):
    """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

    super(OnnxInferenceService, self).__init__()

    self.model_name = model_name
    self.model_base_path = model_base_path
    self.model_version_list = [1]
    self.model_graph_signature = ""
    self.platform = "ONNX"

    # TODO: Import as needed and only once
    import mxnet as mx
    import onnx_mxnet

    # TODO: Select the available version
    epoch_number = 1

    # Load model
    #sym, arg_params, aux_params = mx.model.load_checkpoint(self.model_base_path, epoch_number)
    sym, params = onnx_mxnet.import_model(self.model_base_path)

    # TODO: Support other inputs
    # self.mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None)
    self.mod = mx.mod.Module(
        symbol=sym, data_names=['input_0'], context=mx.cpu(), label_names=None)

    self.has_signature_file = False
    self.signature_input_names = []
    self.signature_output_names = []

    # Load inputs from signature file
    signature_file_path = self.model_base_path + "-signature.json"
    if os.path.exists(signature_file_path) and os.path.isfile(
        signature_file_path):
      self.has_signature_file = True
      data_shapes = []

      with open(signature_file_path) as signature_file:
        signature_dict = json.load(signature_file)
        inputs = signature_dict["inputs"]
        outputs = signature_dict["outputs"]

        for input in inputs:
          input_data_name = input["data_name"]
          input_data_shape = input["data_shape"]

          self.signature_input_names.append(input_data_name)
          data_shapes.append((input_data_name, tuple(input_data_shape)))

        for output in outputs:
          output_data_name = output["data_name"]
          ouput_data_shape = output["data_shape"]

          self.signature_output_names.append(output_data_name)

    else:
      data_shapes = [('data', (1, 2))]
      test_image = np.random.randn(1, 1, 28, 28)
      data_shapes = [('input_0', test_image.shape)]

    self.mod.bind(for_training=False, data_shapes=data_shapes)
    self.mod.set_params(params, params, allow_missing=True, allow_extra=True)
    if self.has_signature_file:
      self.model_graph_signature = "Inputs: {}\nOutputs: {}\n{}".format(
          self.signature_input_names, self.signature_output_names,
          self.mod.symbol.tojson())
    else:
      self.model_graph_signature = "{}".format(self.mod.symbol.tojson())

    self.verbose = verbose
  def __init__(self, model_name, model_base_path, verbose=False):
    """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

    super(OnnxInferenceService, self).__init__()

    self.model_name = model_name
    self.model_base_path = model_base_path
    self.model_version_list = [1]
    self.model_graph_signature = ""
    self.platform = "ONNX"

    # TODO: Import as needed and only once
    import mxnet as mx
    import onnx_mxnet

    # TODO: Select the available version
    epoch_number = 1

    # Load model
    #sym, arg_params, aux_params = mx.model.load_checkpoint(self.model_base_path, epoch_number)
    sym, params = onnx_mxnet.import_model(self.model_base_path)

    # TODO: Support other inputs
    # self.mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None)
    self.mod = mx.mod.Module(
        symbol=sym, data_names=['input_0'], context=mx.cpu(), label_names=None)

    self.has_signature_file = False
    self.signature_input_names = []
    self.signature_output_names = []

    # Load inputs from signature file
    signature_file_path = self.model_base_path + "-signature.json"
    if os.path.exists(signature_file_path) and os.path.isfile(
        signature_file_path):
      self.has_signature_file = True
      data_shapes = []

      with open(signature_file_path) as signature_file:
        signature_dict = json.load(signature_file)
        inputs = signature_dict["inputs"]
        outputs = signature_dict["outputs"]

        for input in inputs:
          input_data_name = input["data_name"]
          input_data_shape = input["data_shape"]

          self.signature_input_names.append(input_data_name)
          data_shapes.append((input_data_name, tuple(input_data_shape)))

        for output in outputs:
          output_data_name = output["data_name"]
          ouput_data_shape = output["data_shape"]

          self.signature_output_names.append(output_data_name)

    else:
      data_shapes = [('data', (1, 2))]
      test_image = np.random.randn(1, 1, 28, 28)
      data_shapes = [('input_0', test_image.shape)]

    self.mod.bind(for_training=False, data_shapes=data_shapes)
    self.mod.set_params(params, params, allow_missing=True, allow_extra=True)
    if self.has_signature_file:
      self.model_graph_signature = "Inputs: {}\nOutputs: {}\n{}".format(
          self.signature_input_names, self.signature_output_names,
          self.mod.symbol.tojson())
    else:
      self.model_graph_signature = "{}".format(self.mod.symbol.tojson())

    self.verbose = verbose
Ejemplo n.º 11
0
import mxnet as mx
import onnx_mxnet
import numpy as np
from matplotlib.pyplot import imshow
from PIL import Image

sym, arg = onnx_mxnet.import_model(
    '../matlab/NeuralNetworks/NN_V03_02_350,350.onnx')
# mx.viz.plot_network(sym, node_attrs={"shape":"oval","fixedsize":"false"})

img = Image.open('out_test/anger/m-daniel-20.png').resize((350, 350))
# # img_ycbcr = img.convert("YCbCr")
# # img_y, img_cb, img_cr = img_ycbcr.split()
test_image = np.array(img)  #[np.newaxis, np.newaxis, :, :]

print(test_image.shape)

data_names = [
    graph_input for graph_input in sym.list_inputs() if graph_input not in arg
]

# print(data_names)

mod = mx.mod.Module(symbol=sym,
                    data_names=data_names,
                    context=mx.cpu(),
                    label_names=None)
mod.bind(for_training=False,
         data_shapes=[(data_names[0], test_image.shape)],
         label_shapes=None)
mod.set_params(arg_params=arg,
Ejemplo n.º 12
0
    def test_convert_and_compare_prediction(self):
        # get data iterators and set basic hyperparams
        num_epochs = 10
        mnist = mx.test_utils.get_mnist()
        batch_size = 1000
        train_iter = mx.io.NDArrayIter(mnist['train_data'],
                                       mnist['train_label'],
                                       batch_size,
                                       shuffle=True)
        val_iter = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'],
                                     batch_size)
        test_iter = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'],
                                      batch_size)
        model_name = 'lenet5'
        model_file = '%s-symbol.json' % model_name
        params_file = '%s-%04d.params' % (model_name, num_epochs)
        onnx_file = "%s.onnx" % model_name
        test_gpu_id = 0
        gpu_id = check_gpu_id(test_gpu_id)
        if not gpu_id:
            print("\nWARNING: GPU id %d is invalid on this machine" %
                  test_gpu_id)
            gpu_id = None

        # If trained model exists, re-use cached version. Otherwise, train model.
        if not (os.path.exists(model_file) and os.path.exists(params_file)):
            print("\n\nTraining LeNet-5 on MNIST data")
            trained_lenet = train_lenet5(num_epochs, gpu_id, train_iter,
                                         val_iter, test_iter, batch_size)
            print("Training finished. Saving model")
            trained_lenet.save_checkpoint(model_name, num_epochs)
            # delete object so we can verify correct loading of the checkpoint from disk
            del trained_lenet
        else:
            print("\n\nTrained model exists. Skipping training.")

        # Load serialized MxNet model (model-symbol.json + model-epoch.params)

        trained_lenet = mx.mod.Module.load(model_name, num_epochs)
        trained_lenet.bind(data_shapes=test_iter.provide_data,
                           label_shapes=None,
                           for_training=False,
                           force_rebind=True)

        # Run inference in MxNet from json/params serialized model
        test_iter = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'],
                                      batch_size)
        pred_softmax = trained_lenet.predict(test_iter).asnumpy()
        pred_classes = np.argmax(pred_softmax, axis=1)

        # Create and save ONNX model
        print("\nConverting trained MxNet model to ONNX")
        model = from_mxnet(model_file,
                           params_file, [1, 1, 28, 28],
                           np.float32,
                           log=True)
        with open(onnx_file, "wb") as f:
            serialized = model.SerializeToString()
            f.write(serialized)
            print("\nONNX file %s serialized to disk" % onnx_file)

        print(
            "\nLoading ONNX file and comparing results to original MxNet output."
        )

        # ONNX load and inference step
        onnx_sym, onnx_params = onnx_mxnet.import_model(onnx_file)
        onnx_mod = mx.mod.Module(symbol=onnx_sym,
                                 data_names=['input_0'],
                                 context=mx.cpu(),
                                 label_names=None)

        # Need to rename data argument from 'data' to 'input_0' because that's how
        # the MxNet ONNX importer expects it by default
        test_iter = mx.io.NDArrayIter(data={'input_0': mnist['test_data']},
                                      label=None,
                                      batch_size=batch_size)

        onnx_mod.bind(data_shapes=test_iter.provide_data,
                      label_shapes=None,
                      for_training=False,
                      force_rebind=True)
        onnx_mod.set_params(arg_params=onnx_params,
                            aux_params=None,
                            allow_missing=True)

        onnx_pred_softmax = onnx_mod.predict(test_iter).asnumpy()
        onnx_pred_classes = np.argmax(pred_softmax, axis=1)

        pred_matches = onnx_pred_classes == pred_classes
        pred_match_ct = pred_matches.sum()
        pred_total_ct = np.size(pred_matches)
        pct_match = 100.0 * pred_match_ct / pred_total_ct

        print(
            "\nOriginal MxNet predictions and ONNX-based predictions after export and re-import:"
        )
        print("Total examples tested: %d" % pred_total_ct)
        print("Matches: %d" % pred_match_ct)
        print("Percent match: %.2f\n" % pct_match)

        assert pred_match_ct == pred_total_ct, "Not all predictions from the ONNX representation match"