2222
2323from singa import device
2424from singa import tensor
25- from singa import autograd
2625from singa import sonnx
2726import 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
3029import logging
3130logging .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
7371if __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 ]))
0 commit comments