JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyNetORT.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4
5import numpy as np
6import cv2
7import onnxruntime as rt
8
9## Simple DNN network invoked from ONNX-Runtime in python
10#
11# @author Laurent Itti
12#
13# @email itti\@usc.edu
14# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
15# @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
16# @mainurl http://jevois.org
17# @supporturl http://jevois.org/doc
18# @otherurl http://iLab.usc.edu
19# @license GPL v3
20# @distribution Unrestricted
21# @restrictions None
22# @ingroup pydnn
24 # ###################################################################################################
25 ## [Optional] Constructor
26 def __init__(self):
27 self.session = None # we do not have a network yet, we will load it after parameters have been set
28
29 # ###################################################################################################
30 ## [Optional] JeVois parameters initialization
31 def init(self):
32 pc = jevois.ParameterCategory("DNN Network Options", "")
33
34 self.dataroot = jevois.Parameter(self, 'dataroot', 'str',
35 "Root directory to use when config or model parameters are relative paths.",
36 pyjevois.share, pc) # pyjevois.share contains '/jevois[pro]/share'
37
38 self.model = jevois.Parameter(self, 'model', 'str',
39 "Path to a binary file of model contains trained weights with .onnx extension. " +
40 "If path is relative, it will be prefixed by dataroot.",
41 "", pc);
42
43 # Note: input shapes are stored in the network already; however, here we require them as a parameter that the
44 # pre-processor will be able to query to create the input tensors. In the future, we may add some callback
45 # function here to allow the pre-processor to get the input shapes directly from the ONNX net
46 self.intensors = jevois.Parameter(self, 'intensors', 'str',
47 "Specification of input tensors",
48 '', pc)
49
50 # ###################################################################################################
51 ## [Optional] Freeze some parameters that should not be changed at runtime
52 def freeze(self, doit):
53 self.dataroot.freeze(doit)
54 self.model.freeze(doit)
55 self.intensors.freeze(doit)
56
57 # ###################################################################################################
58 ## [Required] Load the network from disk
59 def load(self):
60 self.session = rt.InferenceSession(self.dataroot.get() + '/' + self.model.get(),
61 providers = rt.get_available_providers())
62
63 # Get a list of input specs to be used during inference:
64 self.inputs = self.session.get_inputs()
65
66 # ###################################################################################################
67 ## [Required] Main processing function: process input blobs through network and return output blobs
68 ## blobs is a list of numpy arrays for the network's outputs
69 ## Should return a tuple with (list of output blobs, list of info strings), where the info strings
70 ## could contain some information about the network
71 def process(self, blobs):
72 if self.session is None:
73 raise RuntimeError("Cannot process because no loaded network")
74
75 if len(blobs) != len(self.inputs):
76 raise ValueError(f"{len(blobs)} inputs received but network wants {len(self.inputs)}")
77
78 # Create a dictionary to associate one blob to each network input:
79 ins = { }
80 for i in range(len(blobs)): ins[self.inputs[i].name] = blobs[i]
81
82 # Run the network:
83 outs = self.session.run(None, ins)
84
85 # Some simple info strings that will be shown along with preproc/postproc info:
86 info = [ "* Network", "Forward pass OK" ]
87
88 # Return outs and info:
89 return (outs, info)
Simple DNN network invoked from ONNX-Runtime in python.
Definition PyNetORT.py:23
freeze(self, doit)
[Optional] Freeze some parameters that should not be changed at runtime
Definition PyNetORT.py:52
load(self)
[Required] Load the network from disk
Definition PyNetORT.py:59
__init__(self)
[Optional] Constructor
Definition PyNetORT.py:26
init(self)
[Optional] JeVois parameters initialization
Definition PyNetORT.py:31
process(self, blobs)
[Required] Main processing function: process input blobs through network and return output blobs blob...
Definition PyNetORT.py:71