JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyNetKSNN.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
7from ksnn.api import KSNN
8from ksnn.types import *
9
10## Simple DNN network running on NPU and invoked from the Khadas KSNN library in python
11#
12# @author Laurent Itti
13#
14# @email itti\@usc.edu
15# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
16# @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
17# @mainurl http://jevois.org
18# @supporturl http://jevois.org/doc
19# @otherurl http://iLab.usc.edu
20# @license GPL v3
21# @distribution Unrestricted
22# @restrictions None
23# @ingroup pydnn
25 # ###################################################################################################
26 ## [Optional] Constructor
27 def __init__(self):
28 self.net = None
29
30 # ###################################################################################################
31 ## [Optional] JeVois parameters initialization
32 def init(self):
33 pc = jevois.ParameterCategory("DNN Network Options", "")
34
35 self.dataroot = jevois.Parameter(self, 'dataroot', 'str',
36 "Root directory to use when config or model parameters are relative paths.",
37 pyjevois.share, pc) # pyjevois.share contains '/jevois[pro]/share'
38
39 self.model = jevois.Parameter(self, 'model', 'str',
40 "Path to a binary file of model contains trained weights with .onnx extension. " +
41 "If path is relative, it will be prefixed by dataroot.",
42 "", pc);
43
44 self.library = jevois.Parameter(self, 'library', 'str',
45 "Path to a library .so file of model support code, obtained during conversion to NPU. " +
46 "If path is relative, it will be prefixed by dataroot.",
47 "", pc);
48
49 # Note: input shapes are stored in the network already; however, here we require them as a parameter that the
50 # pre-processor will be able to query to create the input tensors. In the future, we may add some callback
51 # function here to allow the pre-processor to get the input shapes directly from the ONNX net
52 self.intensors = jevois.Parameter(self, 'intensors', 'str',
53 "Specification of input tensors",
54 '', 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.net = KSNN('VIM3')
67 self.net.nn_init(model = self.dataroot.get() + '/' + self.model.get(),
68 library = self.dataroot.get() + '/' + self.library.get(), level = 0)
69 jevois.LINFO("Model ready.")
70
71 # ###################################################################################################
72 ## [Required] Main processing function: process input blobs through network and return output blobs
73 ## blobs is a list of numpy arrays for the network's outputs
74 ## Should return a tuple with (list of output blobs, list of info strings), where the info strings
75 ## could contain some information about the network
76 def process(self, blobs):
77 if self.net is None:
78 raise RuntimeError("Cannot process because no loaded network")
79
80 if len(blobs) != len(self.inputs):
81 raise ValueError(f"{len(blobs)} inputs received but network wants {len(self.inputs)}")
82
83 # Create a dictionary to associate one blob to each network input:
84 ins = { }
85 for i in range(len(blobs)): ins[self.inputs[i].name] = blobs[i]
86
87 # Run the network:
88 outs = self.net.nn_inference(blobs, platform = 'ONNX', input_tensor = len(blobs), reorder = '0 1 2',
89 output_tensor = numouts,
90 output_format = output_format.OUT_FORMAT_FLOAT32)
91
92 # Some simple info strings that will be shown along with preproc/postproc info:
93 info = [ "* Network", "Forward pass OK" ]
94
95 # Return outs and info:
96 return (outs, info)
Simple DNN network running on NPU and invoked from the Khadas KSNN library in python.
Definition PyNetKSNN.py:24
process(self, blobs)
[Required] Main processing function: process input blobs through network and return output blobs blob...
Definition PyNetKSNN.py:76
load(self)
[Required] Load the network from disk
Definition PyNetKSNN.py:65
init(self)
[Optional] JeVois parameters initialization
Definition PyNetKSNN.py:32
__init__(self)
[Optional] Constructor
Definition PyNetKSNN.py:27
freeze(self, doit)
[Optional] Freeze some parameters that should not be changed at runtime
Definition PyNetKSNN.py:58