Skip to content

Commit 0af1736

Browse files
authored
Merge branch 'master' into superresolution
2 parents 0b72251 + f92e459 commit 0af1736

8 files changed

Lines changed: 116 additions & 136 deletions

File tree

examples/onnx/159008.jpg

76.6 KB
Loading

examples/onnx/densenet121.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
from singa import device
2424
from singa import tensor
25-
from singa import autograd
2625
from singa import sonnx
2726
import onnx
28-
from utils import download_model, update_batch_size, check_exist_or_download
27+
from utils import download_model, check_exist_or_download
2928

3029
import logging
3130
logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s')
@@ -44,7 +43,7 @@ def preprocess(img):
4443
return img
4544

4645

47-
def get_image_labe():
46+
def get_image_label():
4847
# download label
4948
label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt'
5049
with open(check_exist_or_download(label_url), 'r') as f:
@@ -56,61 +55,57 @@ def get_image_labe():
5655
return img, labels
5756

5857

59-
class Infer:
58+
class MyModel(sonnx.SONNXModel):
6059

61-
def __init__(self, sg_ir):
62-
self.sg_ir = sg_ir
63-
for idx, tens in sg_ir.tensor_map.items():
64-
# allow the tensors to be updated
65-
tens.requires_grad = True
66-
tens.stores_grad = True
67-
sg_ir.tensor_map[idx] = tens
60+
def __init__(self, onnx_model):
61+
super(MyModel, self).__init__(onnx_model)
6862

69-
def forward(self, x):
70-
return sg_ir.run([x])[0]
63+
def forward(self, *x):
64+
y = super(MyModel, self).forward(*x)
65+
return y[0]
66+
67+
def train_one_batch(self, x, y):
68+
pass
7169

7270

7371
if __name__ == "__main__":
72+
73+
download_dir = '/tmp'
7474
url = 'https://s3.amazonaws.com/download.onnx/models/opset_9/densenet121.tar.gz'
75-
download_dir = '/tmp/'
7675
model_path = os.path.join(download_dir, 'densenet121', 'model.onnx')
7776

7877
logging.info("onnx load model...")
7978
download_model(url)
8079
onnx_model = onnx.load(model_path)
8180

82-
# set batch size
83-
onnx_model = update_batch_size(onnx_model, 1)
81+
# inference demo
82+
logging.info("preprocessing...")
83+
img, labels = get_image_label()
84+
img = preprocess(img)
85+
# sg_ir = sonnx.prepare(onnx_model) # run without graph
86+
# y = sg_ir.run([img])
8487

85-
# prepare the model
86-
logging.info("prepare model...")
88+
logging.info("model compling...")
8789
dev = device.create_cuda_gpu()
88-
sg_ir = sonnx.prepare(onnx_model, device=dev)
89-
autograd.training = False
90-
model = Infer(sg_ir)
90+
x = tensor.Tensor(device=dev, data=img)
91+
model = MyModel(onnx_model)
92+
model.compile([x], is_train=False, use_graph=True, sequential=True)
9193

9294
# verifty the test
9395
# from utils import load_dataset
94-
# inputs, ref_outputs = load_dataset(
95-
# os.path.join('/tmp', 'densenet121', 'test_data_set_0'))
96+
# inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'densenet121', 'test_data_set_0'))
9697
# x_batch = tensor.Tensor(device=dev, data=inputs[0])
97-
# outputs = model.forward(x_batch)
98+
# outputs = sg_ir.run([x_batch])
9899
# for ref_o, o in zip(ref_outputs, outputs):
99100
# np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4)
100101

101-
# inference
102-
logging.info("preprocessing...")
103-
img, labels = get_image_labe()
104-
img = preprocess(img)
105-
106102
logging.info("model running...")
107-
x_batch = tensor.Tensor(device=dev, data=img)
108-
y = model.forward(x_batch)
103+
y = model.forward(x)
109104

110105
logging.info("postprocessing...")
111106
y = tensor.softmax(y)
112107
scores = tensor.to_numpy(y)
113108
scores = np.squeeze(scores)
114109
a = np.argsort(scores)[::-1]
115110
for i in a[0:5]:
116-
logging.info('class=%s ; probability=%f' % (labels[i], scores[i]))
111+
logging.info('class=%s ; probability=%f' % (labels[i], scores[i]))

examples/onnx/shufflenetv1.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@
2323

2424
from singa import device
2525
from singa import tensor
26-
from singa import autograd
2726
from singa import sonnx
2827
import onnx
29-
from utils import download_model
30-
from utils import update_batch_size
31-
from utils import check_exist_or_download
28+
from utils import download_model, check_exist_or_download
3229

3330
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
3431

@@ -56,51 +53,53 @@ def get_image_label():
5653
return img, labels
5754

5855

59-
class Infer:
56+
class MyModel(sonnx.SONNXModel):
6057

61-
def __init__(self, sg_ir):
62-
self.sg_ir = sg_ir
63-
for idx, tens in sg_ir.tensor_map.items():
64-
tens.require_grad = True
65-
tens.store_grad = True
66-
sg_ir.tensor_map[idx] = tens
58+
def __init__(self, onnx_model):
59+
super(MyModel, self).__init__(onnx_model)
6760

68-
def forward(self, x):
69-
return sg_ir.run([x])[0]
61+
def forward(self, *x):
62+
y = super(MyModel, self).forward(*x)
63+
return y[0]
64+
65+
def train_one_batch(self, x, y):
66+
pass
7067

7168

7269
if __name__ == '__main__':
70+
download_dir = '/tmp'
7371
url = 'https://github.com/onnx/models/raw/master/vision/classification/shufflenet/model/shufflenet-9.tar.gz'
74-
download_dir = "/tmp/"
7572
model_path = os.path.join(download_dir, 'shufflenet', 'model.onnx')
76-
logging.info("onnx load model....")
73+
74+
logging.info("onnx load model...")
7775
download_model(url)
7876
onnx_model = onnx.load(model_path)
79-
# setting batch size
80-
onnx_model = update_batch_size(onnx_model, 1)
81-
# preparing the model
82-
logging.info("preparing model...")
83-
dev = device.create_cuda_gpu()
84-
sg_ir = sonnx.prepare(onnx_model, device=dev)
85-
autograd.training = False
86-
model = Infer(sg_ir)
87-
88-
# verifying the test dataset
89-
#from utils import load_dataset
90-
#inputs,ref_outputs = load_dataset(os.path.join('/tmp','shufflenet','test_data_set_0'))
91-
#x_batch = tensor.Tensor(device = dev,data=inputs[0])
92-
#outputs = model.forward(x_batch)
93-
# for ref_o,o in zip(ref_outputs,outputs):
94-
# np.testing.assert_almost_equal(ref_o,tensor.to_numpy(o),4)
95-
96-
# inference
77+
78+
# inference demo
9779
logging.info("preprocessing...")
9880
img, labels = get_image_label()
9981
img = preprocess(img)
100-
x_batch = tensor.Tensor(device=dev, data=img)
101-
logging.info("model running....")
102-
y = model.forward(x_batch)
103-
logging.info("postprocessing....")
82+
# sg_ir = sonnx.prepare(onnx_model) # run without graph
83+
# y = sg_ir.run([img])
84+
85+
logging.info("model compling...")
86+
dev = device.create_cuda_gpu()
87+
x = tensor.Tensor(device=dev, data=img)
88+
model = MyModel(onnx_model)
89+
model.compile([x], is_train=False, use_graph=True, sequential=True)
90+
91+
# verifty the test
92+
# from utils import load_dataset
93+
# inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'shufflenet', 'test_data_set_0'))
94+
# x_batch = tensor.Tensor(device=dev, data=inputs[0])
95+
# outputs = sg_ir.run([x_batch])
96+
# for ref_o, o in zip(ref_outputs, outputs):
97+
# np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4)
98+
99+
logging.info("model running...")
100+
y = model.forward(x)
101+
102+
logging.info("postprocessing...")
104103
y = tensor.softmax(y)
105104
scores = tensor.to_numpy(y)
106105
scores = np.squeeze(scores)

examples/onnx/shufflenetv2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def preprocess(img):
4343
return img
4444

4545

46-
def get_image_labe():
46+
def get_image_label():
4747
# download label
4848
label_url = 'https://s3.amazonaws.com/onnx-model-zoo/synset.txt'
4949
with open(check_exist_or_download(label_url), 'r') as f:
@@ -81,7 +81,7 @@ def train_one_batch(self, x, y):
8181

8282
# inference
8383
logging.info("preprocessing...")
84-
img, labels = get_image_labe()
84+
img, labels = get_image_label()
8585
img = preprocess(img)
8686
# sg_ir = sonnx.prepare(onnx_model) # run without graph
8787
# y = sg_ir.run([img])

examples/onnx/squeezenet.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
from singa import device
2424
from singa import tensor
25-
from singa import autograd
2625
from singa import sonnx
2726
import onnx
28-
from utils import download_model, update_batch_size, check_exist_or_download
27+
from utils import download_model, check_exist_or_download
2928

3029
import logging
3130
logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s')
@@ -56,58 +55,52 @@ def get_image_label():
5655
return img, labels
5756

5857

59-
class Infer:
58+
class MyModel(sonnx.SONNXModel):
6059

61-
def __init__(self, sg_ir):
62-
self.sg_ir = sg_ir
63-
for idx, tens in sg_ir.tensor_map.items():
64-
# allow the tensors to be updated
65-
tens.requires_grad = True
66-
tens.stores_grad = True
67-
sg_ir.tensor_map[idx] = tens
60+
def __init__(self, onnx_model):
61+
super(MyModel, self).__init__(onnx_model)
6862

69-
def forward(self, x):
70-
return sg_ir.run([x])[0]
63+
def forward(self, *x):
64+
y = super(MyModel, self).forward(*x)
65+
return y[0]
7166

67+
def train_one_batch(self, x, y):
68+
pass
7269

73-
if __name__ == "__main__":
7470

71+
if __name__ == '__main__':
72+
download_dir = '/tmp'
7573
url = 'https://github.com/onnx/models/raw/master/vision/classification/squeezenet/model/squeezenet1.1-7.tar.gz'
76-
download_dir = '/tmp/'
7774
model_path = os.path.join(download_dir, 'squeezenet1.1',
7875
'squeezenet1.1.onnx')
7976

8077
logging.info("onnx load model...")
8178
download_model(url)
8279
onnx_model = onnx.load(model_path)
8380

84-
# set batch size
85-
onnx_model = update_batch_size(onnx_model, 1)
81+
# inference demo
82+
logging.info("preprocessing...")
83+
img, labels = get_image_label()
84+
img = preprocess(img)
85+
# sg_ir = sonnx.prepare(onnx_model) # run without graph
86+
# y = sg_ir.run([img])
8687

87-
# prepare the model
88-
logging.info("prepare model...")
88+
logging.info("model compling...")
8989
dev = device.create_cuda_gpu()
90-
sg_ir = sonnx.prepare(onnx_model, device=dev)
91-
autograd.training = False
92-
model = Infer(sg_ir)
90+
x = tensor.Tensor(device=dev, data=img)
91+
model = MyModel(onnx_model)
92+
model.compile([x], is_train=False, use_graph=True, sequential=True)
9393

94-
# verify the test
94+
# verifty the test
9595
# from utils import load_dataset
96-
# inputs, ref_outputs = load_dataset(
97-
# os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0'))
96+
# inputs, ref_outputs = load_dataset(os.path.join('/tmp', 'squeezenet1.1', 'test_data_set_0'))
9897
# x_batch = tensor.Tensor(device=dev, data=inputs[0])
99-
# outputs = model.forward(x_batch)
98+
# outputs = sg_ir.run([x_batch])
10099
# for ref_o, o in zip(ref_outputs, outputs):
101100
# np.testing.assert_almost_equal(ref_o, tensor.to_numpy(o), 4)
102101

103-
# inference
104-
logging.info("preprocessing...")
105-
img, labels = get_image_label()
106-
img = preprocess(img)
107-
108102
logging.info("model running...")
109-
x_batch = tensor.Tensor(device=dev, data=img)
110-
y = model.forward(x_batch)
103+
y = model.forward(x)
111104

112105
logging.info("postprocessing...")
113106
y = tensor.softmax(y)

examples/onnx/superresolution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import os
2020
import numpy as np
2121
from PIL import Image
22-
# pip install python-resize-image
2322
from resizeimage import resizeimage
2423

2524
from singa import device

0 commit comments

Comments
 (0)