From 4eb3763e61fa19581ca6d21ebb8a837bf8293430 Mon Sep 17 00:00:00 2001 From: agnesnatasya Date: Thu, 28 May 2020 00:18:06 +0800 Subject: [PATCH 1/6] Implement Squeezenet using Squeezenet1.1 --- examples/onnx/squeezenet.py | 117 ++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 examples/onnx/squeezenet.py diff --git a/examples/onnx/squeezenet.py b/examples/onnx/squeezenet.py new file mode 100644 index 0000000000..92f9372c8c --- /dev/null +++ b/examples/onnx/squeezenet.py @@ -0,0 +1,117 @@ +# +# 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 th + +import os +import numpy as np +from PIL import Image + +from singa import device +from singa import tensor +from singa import autograd +from singa import sonnx +import onnx +from utils import download_model, update_batch_size, check_exist_or_download + +import logging +logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s') + + +def preprocess(img): + img = img.resize((224, 224)) + img = img.crop((0, 0, 224, 224)) + img = np.array(img).astype(np.float32) / 255. + img = np.rollaxis(img, 2, 0) + for channel, mean, std in zip(range(3), [0.485, 0.456, 0.406], + [0.229, 0.224, 0.225]): + img[channel, :, :] -= mean + img[channel, :, :] /= std + img = np.expand_dims(img, axis=0) + return img + +def get_image_labe(): + # download label + label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' + with open(check_exist_or_download(label_url), 'r') as f: + labels = [l.rstrip() for l in f] + + # download image + image_url = 'https://s3.amazonaws.com/model-server/inputs/kitten.jpg' + img = Image.open(check_exist_or_download(image_url)) + return img, labels + +class Infer: + + def __init__(self, sg_ir): + self.sg_ir = sg_ir + for idx, tens in sg_ir.tensor_map.items(): + # allow the tensors to be updated + tens.requires_grad = True + tens.stores_grad = True + sg_ir.tensor_map[idx] = tens + + def forward(self, x): + return sg_ir.run([x])[0] + + +if __name__ == "__main__": + + url = 'https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.1-7.tar.gz' + download_dir = '/tmp/' + model_path = os.path.join(download_dir, 'squeezenet1.1', 'squeezenet1.1.onnx') + + logging.info("onnx load model...") + download_model(url) + onnx_model = onnx.load(model_path) + + # set batch size + onnx_model = update_batch_size(onnx_model, 1) + + # prepare the model + logging.info("prepare model...") + dev = device.get_default_device() + sg_ir = sonnx.prepare(onnx_model, device=dev) + autograd.training = False + model = Infer(sg_ir) + + # verify the test + from utils import load_dataset + inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0')) + x_batch = tensor.Tensor(device=dev, data=inputs[0]) + outputs = model.forward(x_batch) + for ref_o, o in zip(ref_outputs, outputs): + np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) + + # inference + logging.info("preprocessing...") + img, labels = get_image_labe() + img = preprocess(img) + + logging.info("model running...") + x_batch = tensor.Tensor(device=dev, data=img) + y = model.forward(x_batch) + + logging.info("postprocessing...") + y = tensor.softmax(y) + scores = tensor.to_numpy(y) + scores = np.squeeze(scores) + a = np.argsort(scores)[::-1] + # The output needs to be [1,1000,1,1] + #a = np.expand_dims(a, axis=0) + #print(a.shape) + for i in a[0:5]: + logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) \ No newline at end of file From 5588eeb573658eddef72ca3e494aeba2a87fc853 Mon Sep 17 00:00:00 2001 From: agnesnatasya Date: Thu, 28 May 2020 00:38:16 +0800 Subject: [PATCH 2/6] Modify the comment regarding dimension --- examples/onnx/squeezenet.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/onnx/squeezenet.py b/examples/onnx/squeezenet.py index 92f9372c8c..2bdbec8e6b 100644 --- a/examples/onnx/squeezenet.py +++ b/examples/onnx/squeezenet.py @@ -110,8 +110,8 @@ def forward(self, x): scores = tensor.to_numpy(y) scores = np.squeeze(scores) a = np.argsort(scores)[::-1] - # The output needs to be [1,1000,1,1] - #a = np.expand_dims(a, axis=0) - #print(a.shape) + # The guide stated the output needs to be [1,1000,1,1], but here it is [1000,1,1] + # The dimension can be expanded if [1,1000,1,1] is desired + # a = np.expand_dims(a, axis=0) for i in a[0:5]: logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) \ No newline at end of file From 1efaf4b3033aeb29bef413936f91c01788059bd1 Mon Sep 17 00:00:00 2001 From: agnesnatasya Date: Thu, 28 May 2020 00:48:35 +0800 Subject: [PATCH 3/6] Change device from CPU to GPU --- examples/onnx/squeezenet.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/onnx/squeezenet.py b/examples/onnx/squeezenet.py index 2bdbec8e6b..0246e503c9 100644 --- a/examples/onnx/squeezenet.py +++ b/examples/onnx/squeezenet.py @@ -43,7 +43,7 @@ def preprocess(img): img = np.expand_dims(img, axis=0) return img -def get_image_labe(): +def get_image_label(): # download label label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' with open(check_exist_or_download(label_url), 'r') as f: @@ -83,7 +83,7 @@ def forward(self, x): # prepare the model logging.info("prepare model...") - dev = device.get_default_device() + dev = device.create_cuda_gpu() sg_ir = sonnx.prepare(onnx_model, device=dev) autograd.training = False model = Infer(sg_ir) @@ -98,7 +98,7 @@ def forward(self, x): # inference logging.info("preprocessing...") - img, labels = get_image_labe() + img, labels = get_image_label() img = preprocess(img) logging.info("model running...") From a1cc797d647fb5e4513482ce8dce0b1f088238e8 Mon Sep 17 00:00:00 2001 From: agnesnatasya Date: Sat, 30 May 2020 00:39:43 +0800 Subject: [PATCH 4/6] Formating squeezenet --- examples/onnx/shufflenet.py | 114 ++++++++++++++++++++++++++++++++++ examples/onnx/shufflenet2.py | 116 +++++++++++++++++++++++++++++++++++ examples/onnx/squeezenet.py | 13 ++-- 3 files changed, 237 insertions(+), 6 deletions(-) create mode 100644 examples/onnx/shufflenet.py create mode 100644 examples/onnx/shufflenet2.py diff --git a/examples/onnx/shufflenet.py b/examples/onnx/shufflenet.py new file mode 100644 index 0000000000..c8dc24cc9b --- /dev/null +++ b/examples/onnx/shufflenet.py @@ -0,0 +1,114 @@ +# +# 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 th + +import os +import numpy as np +from PIL import Image + +from singa import device +from singa import tensor +from singa import autograd +from singa import sonnx +import onnx +from utils import download_model, update_batch_size, check_exist_or_download + +import logging +logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s') + + +def preprocess(img): + img = img.resize((256, 256)) + img = img.crop((16, 16, 240, 240)) + img = np.array(img).astype(np.float32) / 255. + img = np.rollaxis(img, 2, 0) + for channel, mean, std in zip(range(3), [0.485, 0.456, 0.406], + [0.229, 0.224, 0.225]): + img[channel, :, :] -= mean + img[channel, :, :] /= std + img = np.expand_dims(img, axis=0) + return img + +def get_image_labe(): + # download label + label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' + with open(check_exist_or_download(label_url), 'r') as f: + labels = [l.rstrip() for l in f] + + # download image + image_url = 'https://s3.amazonaws.com/model-server/inputs/kitten.jpg' + img = Image.open(check_exist_or_download(image_url)) + return img, labels + +class Infer: + + def __init__(self, sg_ir): + self.sg_ir = sg_ir + for idx, tens in sg_ir.tensor_map.items(): + # allow the tensors to be updated + tens.requires_grad = True + tens.stores_grad = True + sg_ir.tensor_map[idx] = tens + + def forward(self, x): + return sg_ir.run([x])[0] + + +if __name__ == "__main__": + + url = 'https://s3.amazonaws.com/download.onnx/models/opset_9/shufflenet.tar.gz' + download_dir = '/tmp/' + model_path = os.path.join(download_dir, 'shufflenet', 'model.onnx') + + logging.info("onnx load model...") + download_model(url) + onnx_model = onnx.load(model_path) + + # set batch size + onnx_model = update_batch_size(onnx_model, 1) + + # prepare the model + logging.info("prepare model...") + dev = device.get_default_device() + sg_ir = sonnx.prepare(onnx_model, device=dev) + autograd.training = False + model = Infer(sg_ir) + + # verifty the test + # from utils import load_dataset + # inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'resnet18v1', 'test_data_set_0')) + # x_batch = tensor.Tensor(device=dev, data=inputs[0]) + # outputs = model.forward(x_batch) + # for ref_o, o in zip(ref_outputs, outputs): + # np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) + + # inference + logging.info("preprocessing...") + img, labels = get_image_labe() + img = preprocess(img) + + logging.info("model running...") + x_batch = tensor.Tensor(device=dev, data=img) + y = model.forward(x_batch) + + logging.info("postprocessing...") + y = tensor.softmax(y) + scores = tensor.to_numpy(y) + scores = np.squeeze(scores) + a = np.argsort(scores)[::-1] + for i in a[0:5]: + logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) \ No newline at end of file diff --git a/examples/onnx/shufflenet2.py b/examples/onnx/shufflenet2.py new file mode 100644 index 0000000000..8b76ad22d1 --- /dev/null +++ b/examples/onnx/shufflenet2.py @@ -0,0 +1,116 @@ +# +# 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 th + +import os +import numpy as np +from PIL import Image + +from singa import device +from singa import tensor +from singa import autograd +from singa import sonnx +import onnx +from utils import download_model, update_batch_size, check_exist_or_download + +import logging +logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s') + + +def preprocess(img): + img = img.resize((256, 256)) + img = img.crop((16, 16, 240, 240)) + img = np.array(img).astype(np.float32) / 255. + img = np.rollaxis(img, 2, 0) + for channel, mean, std in zip(range(3), [0.485, 0.456, 0.406], + [0.229, 0.224, 0.225]): + img[channel, :, :] -= mean + img[channel, :, :] /= std + img = np.expand_dims(img, axis=0) + return img + +def get_image_labe(): + # download label + label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' + with open(check_exist_or_download(label_url), 'r') as f: + labels = [l.rstrip() for l in f] + + # download image + image_url = 'https://s3.amazonaws.com/model-server/inputs/kitten.jpg' + img = Image.open(check_exist_or_download(image_url)) + return img, labels + +class Infer: + + def __init__(self, sg_ir): + self.sg_ir = sg_ir + for idx, tens in sg_ir.tensor_map.items(): + # allow the tensors to be updated + tens.requires_grad = True + tens.stores_grad = True + sg_ir.tensor_map[idx] = tens + + def forward(self, x): + return sg_ir.run([x])[0] + + +if __name__ == "__main__": + + url = 'https://github.com/onnx/models/raw/master/vision/classification/shufflenet/model/shufflenet-v2-10.tar.gz' + download_dir = '/tmp/' + model_path = os.path.join(download_dir, 'model', 'test_shufflenetv2', 'model.onnx') + + logging.info("onnx load model...") + download_model(url) + ''' + onnx_model = onnx.load(model_path) + + # set batch size + onnx_model = update_batch_size(onnx_model, 1) + + # prepare the model + logging.info("prepare model...") + dev = device.get_default_device() + sg_ir = sonnx.prepare(onnx_model, device=dev) + autograd.training = False + model = Infer(sg_ir) + + # verifty the test + # from utils import load_dataset + # inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'resnet18v1', 'test_data_set_0')) + # x_batch = tensor.Tensor(device=dev, data=inputs[0]) + # outputs = model.forward(x_batch) + # for ref_o, o in zip(ref_outputs, outputs): + # np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) + + # inference + logging.info("preprocessing...") + img, labels = get_image_labe() + img = preprocess(img) + + logging.info("model running...") + x_batch = tensor.Tensor(device=dev, data=img) + y = model.forward(x_batch) + + logging.info("postprocessing...") + y = tensor.softmax(y) + scores = tensor.to_numpy(y) + scores = np.squeeze(scores) + a = np.argsort(scores)[::-1] + for i in a[0:5]: + logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) + ''' \ No newline at end of file diff --git a/examples/onnx/squeezenet.py b/examples/onnx/squeezenet.py index 0246e503c9..6b3601eeae 100644 --- a/examples/onnx/squeezenet.py +++ b/examples/onnx/squeezenet.py @@ -43,6 +43,7 @@ def preprocess(img): img = np.expand_dims(img, axis=0) return img + def get_image_label(): # download label label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' @@ -54,6 +55,7 @@ def get_image_label(): img = Image.open(check_exist_or_download(image_url)) return img, labels + class Infer: def __init__(self, sg_ir): @@ -72,7 +74,8 @@ def forward(self, x): url = 'https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.1-7.tar.gz' download_dir = '/tmp/' - model_path = os.path.join(download_dir, 'squeezenet1.1', 'squeezenet1.1.onnx') + model_path = os.path.join(download_dir, 'squeezenet1.1', + 'squeezenet1.1.onnx') logging.info("onnx load model...") download_model(url) @@ -90,7 +93,8 @@ def forward(self, x): # verify the test from utils import load_dataset - inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0')) + inputs, ref_outputs = load_dataset( + os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0')) x_batch = tensor.Tensor(device=dev, data=inputs[0]) outputs = model.forward(x_batch) for ref_o, o in zip(ref_outputs, outputs): @@ -110,8 +114,5 @@ def forward(self, x): scores = tensor.to_numpy(y) scores = np.squeeze(scores) a = np.argsort(scores)[::-1] - # The guide stated the output needs to be [1,1000,1,1], but here it is [1000,1,1] - # The dimension can be expanded if [1,1000,1,1] is desired - # a = np.expand_dims(a, axis=0) for i in a[0:5]: - logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) \ No newline at end of file + logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) From 8f7b4ef976746e8b9d9a3ceaaad0f51d1bfa771c Mon Sep 17 00:00:00 2001 From: agnesnatasya Date: Sat, 30 May 2020 00:43:49 +0800 Subject: [PATCH 5/6] Remove shufflenet files --- examples/onnx/shufflenet.py | 114 ---------------------------------- examples/onnx/shufflenet2.py | 116 ----------------------------------- 2 files changed, 230 deletions(-) delete mode 100644 examples/onnx/shufflenet.py delete mode 100644 examples/onnx/shufflenet2.py diff --git a/examples/onnx/shufflenet.py b/examples/onnx/shufflenet.py deleted file mode 100644 index c8dc24cc9b..0000000000 --- a/examples/onnx/shufflenet.py +++ /dev/null @@ -1,114 +0,0 @@ -# -# 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 th - -import os -import numpy as np -from PIL import Image - -from singa import device -from singa import tensor -from singa import autograd -from singa import sonnx -import onnx -from utils import download_model, update_batch_size, check_exist_or_download - -import logging -logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s') - - -def preprocess(img): - img = img.resize((256, 256)) - img = img.crop((16, 16, 240, 240)) - img = np.array(img).astype(np.float32) / 255. - img = np.rollaxis(img, 2, 0) - for channel, mean, std in zip(range(3), [0.485, 0.456, 0.406], - [0.229, 0.224, 0.225]): - img[channel, :, :] -= mean - img[channel, :, :] /= std - img = np.expand_dims(img, axis=0) - return img - -def get_image_labe(): - # download label - label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' - with open(check_exist_or_download(label_url), 'r') as f: - labels = [l.rstrip() for l in f] - - # download image - image_url = 'https://s3.amazonaws.com/model-server/inputs/kitten.jpg' - img = Image.open(check_exist_or_download(image_url)) - return img, labels - -class Infer: - - def __init__(self, sg_ir): - self.sg_ir = sg_ir - for idx, tens in sg_ir.tensor_map.items(): - # allow the tensors to be updated - tens.requires_grad = True - tens.stores_grad = True - sg_ir.tensor_map[idx] = tens - - def forward(self, x): - return sg_ir.run([x])[0] - - -if __name__ == "__main__": - - url = 'https://s3.amazonaws.com/download.onnx/models/opset_9/shufflenet.tar.gz' - download_dir = '/tmp/' - model_path = os.path.join(download_dir, 'shufflenet', 'model.onnx') - - logging.info("onnx load model...") - download_model(url) - onnx_model = onnx.load(model_path) - - # set batch size - onnx_model = update_batch_size(onnx_model, 1) - - # prepare the model - logging.info("prepare model...") - dev = device.get_default_device() - sg_ir = sonnx.prepare(onnx_model, device=dev) - autograd.training = False - model = Infer(sg_ir) - - # verifty the test - # from utils import load_dataset - # inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'resnet18v1', 'test_data_set_0')) - # x_batch = tensor.Tensor(device=dev, data=inputs[0]) - # outputs = model.forward(x_batch) - # for ref_o, o in zip(ref_outputs, outputs): - # np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) - - # inference - logging.info("preprocessing...") - img, labels = get_image_labe() - img = preprocess(img) - - logging.info("model running...") - x_batch = tensor.Tensor(device=dev, data=img) - y = model.forward(x_batch) - - logging.info("postprocessing...") - y = tensor.softmax(y) - scores = tensor.to_numpy(y) - scores = np.squeeze(scores) - a = np.argsort(scores)[::-1] - for i in a[0:5]: - logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) \ No newline at end of file diff --git a/examples/onnx/shufflenet2.py b/examples/onnx/shufflenet2.py deleted file mode 100644 index 8b76ad22d1..0000000000 --- a/examples/onnx/shufflenet2.py +++ /dev/null @@ -1,116 +0,0 @@ -# -# 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 th - -import os -import numpy as np -from PIL import Image - -from singa import device -from singa import tensor -from singa import autograd -from singa import sonnx -import onnx -from utils import download_model, update_batch_size, check_exist_or_download - -import logging -logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s') - - -def preprocess(img): - img = img.resize((256, 256)) - img = img.crop((16, 16, 240, 240)) - img = np.array(img).astype(np.float32) / 255. - img = np.rollaxis(img, 2, 0) - for channel, mean, std in zip(range(3), [0.485, 0.456, 0.406], - [0.229, 0.224, 0.225]): - img[channel, :, :] -= mean - img[channel, :, :] /= std - img = np.expand_dims(img, axis=0) - return img - -def get_image_labe(): - # download label - label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt' - with open(check_exist_or_download(label_url), 'r') as f: - labels = [l.rstrip() for l in f] - - # download image - image_url = 'https://s3.amazonaws.com/model-server/inputs/kitten.jpg' - img = Image.open(check_exist_or_download(image_url)) - return img, labels - -class Infer: - - def __init__(self, sg_ir): - self.sg_ir = sg_ir - for idx, tens in sg_ir.tensor_map.items(): - # allow the tensors to be updated - tens.requires_grad = True - tens.stores_grad = True - sg_ir.tensor_map[idx] = tens - - def forward(self, x): - return sg_ir.run([x])[0] - - -if __name__ == "__main__": - - url = 'https://github.com/onnx/models/raw/master/vision/classification/shufflenet/model/shufflenet-v2-10.tar.gz' - download_dir = '/tmp/' - model_path = os.path.join(download_dir, 'model', 'test_shufflenetv2', 'model.onnx') - - logging.info("onnx load model...") - download_model(url) - ''' - onnx_model = onnx.load(model_path) - - # set batch size - onnx_model = update_batch_size(onnx_model, 1) - - # prepare the model - logging.info("prepare model...") - dev = device.get_default_device() - sg_ir = sonnx.prepare(onnx_model, device=dev) - autograd.training = False - model = Infer(sg_ir) - - # verifty the test - # from utils import load_dataset - # inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'resnet18v1', 'test_data_set_0')) - # x_batch = tensor.Tensor(device=dev, data=inputs[0]) - # outputs = model.forward(x_batch) - # for ref_o, o in zip(ref_outputs, outputs): - # np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) - - # inference - logging.info("preprocessing...") - img, labels = get_image_labe() - img = preprocess(img) - - logging.info("model running...") - x_batch = tensor.Tensor(device=dev, data=img) - y = model.forward(x_batch) - - logging.info("postprocessing...") - y = tensor.softmax(y) - scores = tensor.to_numpy(y) - scores = np.squeeze(scores) - a = np.argsort(scores)[::-1] - for i in a[0:5]: - logging.info('class=%s ; probability=%f' % (labels[i], scores[i])) - ''' \ No newline at end of file From 1f88218fce4f83d597920ad2ec714ffe680e1e4e Mon Sep 17 00:00:00 2001 From: agnesnatasya Date: Sat, 30 May 2020 14:38:50 +0800 Subject: [PATCH 6/6] Comment out testing part --- examples/onnx/squeezenet.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/onnx/squeezenet.py b/examples/onnx/squeezenet.py index 6b3601eeae..8a6ecf5d0f 100644 --- a/examples/onnx/squeezenet.py +++ b/examples/onnx/squeezenet.py @@ -92,13 +92,13 @@ def forward(self, x): model = Infer(sg_ir) # verify the test - from utils import load_dataset - inputs, ref_outputs = load_dataset( - os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0')) - x_batch = tensor.Tensor(device=dev, data=inputs[0]) - outputs = model.forward(x_batch) - for ref_o, o in zip(ref_outputs, outputs): - np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) + # from utils import load_dataset + # inputs, ref_outputs = load_dataset( + # os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0')) + # x_batch = tensor.Tensor(device=dev, data=inputs[0]) + # outputs = model.forward(x_batch) + # for ref_o, o in zip(ref_outputs, outputs): + # np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4) # inference logging.info("preprocessing...")