JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyNetURetinex.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 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)
Simple DNN network invoked from ONNX-Runtime in python for URetinex-Net.
load(self)
[Required] Load the network from disk
init(self)
[Optional] JeVois parameters initialization
process(self, blobs)
[Required] Main processing function: process input blobs through network and return output blobs blob...
__init__(self)
[Optional] Constructor
freeze(self, doit)
[Optional] Freeze some parameters that should not be changed at runtime