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