Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Fetch test files by version number. Verify the high resolution example.
Browse files Browse the repository at this point in the history
Signed-off-by: Acharya <[email protected]>
  • Loading branch information
Acharya committed Mar 12, 2018
1 parent c6c8038 commit 1d02490
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ try {
}

stage('Integration Test') {
parallel 'Onnx CPU': {
parallel 'Onnx CPU': {
node('mxnetlinux-cpu') {
ws('workspace/it-onnx-cpu') {
init_git()
Expand Down
4 changes: 2 additions & 2 deletions ci/docker/Dockerfile.build.ubuntu_cpu
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ COPY install/ubuntu_mklml.sh /work/
RUN /work/ubuntu_mklml.sh
COPY install/ubuntu_caffe.sh /work/
RUN /work/ubuntu_caffe.sh
COPY install/ubuntu_onnx.sh /work/
RUN /work/ubuntu_onnx.sh
COPY install/ubuntu_docs.sh /work/
RUN /work/ubuntu_docs.sh
COPY install/ubuntu_adduser.sh /work/
RUN /work/ubuntu_adduser.sh
COPY install/ubuntu_onnx.sh /work/
RUN /work/ubuntu_onnx.sh

COPY runtime_functions.sh /work/

Expand Down
2 changes: 1 addition & 1 deletion ci/docker/runtime_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ unittest_centos7_gpu() {
integrationtest_ubuntu_cpu_onnx() {
set -ex
export PYTHONPATH=./python/
python example/onnx/test_super_resolution.py
python example/onnx/super_resolution.py
pytest tests/python-pytest/onnx/onnx_backend_test.py
pytest tests/python-pytest/onnx/onnx_test.py
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,22 @@
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)

def download_onnx_model():
"""Download the onnx model"""
model_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_resolution.onnx'
download(model_url, 'super_resolution.onnx')

def import_onnx():
"""Import the onnx model into mxnet"""
model_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_resolution.onnx'
download(model_url, 'super_resolution.onnx', version_tag = '"7348c879d16c42bc77e24e270f663524"')

LOGGER.info("Converting onnx format to mxnet's symbol and params...")
sym, params = onnx_mxnet.import_model('super_resolution.onnx')
LOGGER.info("Successfully Converted onnx format to mxnet's symbol and params...")
assert sym is not None
assert params is not None

inputs = sym.list_inputs()
assert len(inputs) == 9
for i, input_param in enumerate(['param_7', 'param_5', 'param_3', 'param_1',
'input_0', 'param_0', 'param_2', 'param_4', 'param_6']):
assert inputs[i] == input_param

assert len(sym.list_outputs()) == 1
assert sym.list_outputs()[0] == 'reshape5_output'

assert len(sym.list_attr()) == 1
assert sym.list_attr()['shape'] == '(1L, 1L, 672L, 672L)'

attrs_keys = sym.attr_dict().keys()
assert len(attrs_keys) == 19
for i, key_item in enumerate(['reshape4', 'param_5', 'param_4', 'param_7',
'param_6', 'param_1', 'param_0', 'param_3',
'param_2', 'reshape2', 'reshape3', 'reshape0',
'reshape1', 'convolution2', 'convolution3',
'convolution0', 'convolution1', 'reshape5',
'transpose0']):
assert attrs_keys[i] == key_item

param_keys = params.keys()
assert len(param_keys) == 8
for i, param_item in enumerate(['param_5', 'param_4', 'param_7', 'param_6',
'param_1', 'param_0', 'param_3', 'param_2']):
assert param_keys[i] == param_item
LOGGER.info("Asserted the result of the onnx model conversion")
return sym, params

def get_test_image():
"""Download and process the test image"""
# Load test image
input_image_dim = 224
img_url = 'https://s3.amazonaws.com/onnx-mxnet/examples/super_res_input.jpg'
download(img_url, 'super_res_input.jpg')
download(img_url, 'super_res_input.jpg', version_tag = '"02c90a7248e51316b11f7f39dd1b226d"')
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()
Expand Down Expand Up @@ -109,7 +76,7 @@ def perform_inference((sym, params), (input_img, img_cb, img_cr)):
assert result_img.size == (output_img_dim, output_img_dim)
LOGGER.info("Super Resolution example success.")
result_img.save("super_res_output.jpg")
return result_img

if __name__ == '__main__':
download_onnx_model()
perform_inference(import_onnx(), get_test_image())
3 changes: 2 additions & 1 deletion python/mxnet/contrib/onnx/_import/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
from .op_translations import reduce_prod, avg_pooling, max_pooling
from .op_translations import argmax, argmin, maximum, minimum

# convert_map defines maps of name to converter functor(callable)
# convert_map defines maps of ONNX operator names to converter functor(callable)
# defined in the op_translations module.
_convert_map = {
# Generator Functions
'Constant' : identity,
Expand Down
3 changes: 2 additions & 1 deletion python/mxnet/contrib/onnx/_import/op_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from . import translation_utils
from .... import symbol

#Generator Functions
# Method definitions for the callable objects mapped in the import_helper module

def identity(attrs, inputs, cls):
"""Returns the identity function of the the input."""
return 'identity', attrs, inputs
Expand Down
10 changes: 8 additions & 2 deletions python/mxnet/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,8 @@ def list_gpus():
pass
return range(len([i for i in re.split('\n') if 'GPU' in i]))

def download(url, fname=None, dirname=None, overwrite=False):

def download(url, fname=None, dirname=None, overwrite=False, version_tag=None):
"""Download an given URL
Parameters
Expand All @@ -1385,6 +1386,8 @@ def download(url, fname=None, dirname=None, overwrite=False):
Default is false, which means skipping download if the local file
exists. If true, then download the url to overwrite the local file if
exists.
version_tag : str, optional
the version tag of the file.
Returns
-------
Expand All @@ -1407,12 +1410,15 @@ def download(url, fname=None, dirname=None, overwrite=False):
if exc.errno != errno.EEXIST:
raise OSError('failed to create ' + dirname)

if not overwrite and os.path.exists(fname):
if not overwrite and os.path.exists(fname) and not version_tag:
logging.info("%s exists, skipping download", fname)
return fname

r = requests.get(url, stream=True)
assert r.status_code == 200, "failed to open %s" % url
if version_tag and r.headers['ETag'] != version_tag:
logging.info("The version tag of the file does not match the expected version. "
+ "Proceeding with the file download...")
with open(fname, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
Expand Down
57 changes: 55 additions & 2 deletions tests/python-pytest/onnx/onnx_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@
# specific language governing permissions and limitations
# under the License.

"""Tests for individual operators"""

"""
Tests for individual operators
This module contains operator tests which currently do not exist on
ONNX backend test framework. Once we have PRs on the ONNX repo and get
those PRs merged, this file will get EOL'ed.
"""
from __future__ import absolute_import
import sys
import os
import unittest
import logging
import numpy as np
import numpy.testing as npt
from onnx import helper
Expand All @@ -29,6 +34,11 @@
sys.path.insert(0, os.path.join(CURR_PATH, '../../python/unittest'))
from common import with_seed

# set up logger
logging.basicConfig()
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)

@with_seed()
def test_reduce_max():
"""Test for ReduceMax operator"""
Expand Down Expand Up @@ -82,5 +92,48 @@ def test_squeeze():
output = mxnet_backend.run_node(node_def, [input1])[0]
npt.assert_almost_equal(output, np.squeeze(input1, axis=[1, 3]))

def test_super_resolution():
"""Test the super resolution example in the example/onnx folder"""
sys.path.insert(0, os.path.join(CURR_PATH, '../../../example/onnx/'))
import super_resolution

sym, params = super_resolution.import_onnx()
assert sym is not None
assert params is not None

inputs = sym.list_inputs()
assert len(inputs) == 9
for i, input_param in enumerate(['param_7', 'param_5', 'param_3', 'param_1',
'input_0', 'param_0', 'param_2', 'param_4', 'param_6']):
assert inputs[i] == input_param

assert len(sym.list_outputs()) == 1
assert sym.list_outputs()[0] == 'reshape5_output'

assert len(sym.list_attr()) == 1
assert sym.list_attr()['shape'] == '(1L, 1L, 672L, 672L)'

attrs_keys = sym.attr_dict().keys()
assert len(attrs_keys) == 19
for i, key_item in enumerate(['reshape4', 'param_5', 'param_4', 'param_7',
'param_6', 'param_1', 'param_0', 'param_3',
'param_2', 'reshape2', 'reshape3', 'reshape0',
'reshape1', 'convolution2', 'convolution3',
'convolution0', 'convolution1', 'reshape5',
'transpose0']):
assert attrs_keys[i] == key_item

param_keys = params.keys()
assert len(param_keys) == 8
for i, param_item in enumerate(['param_5', 'param_4', 'param_7', 'param_6',
'param_1', 'param_0', 'param_3', 'param_2']):
assert param_keys[i] == param_item
LOGGER.info("Asserted the result of the onnx model conversion")

output_img_dim = 672
result_img = super_resolution.perform_inference((sym, params),
super_resolution.get_test_image())
assert result_img.size == (output_img_dim, output_img_dim)

if __name__ == '__main__':
unittest.main()

0 comments on commit 1d02490

Please sign in to comment.