ONE - On-device Neural Engine
Loading...
Searching...
No Matches
session.py
Go to the documentation of this file.
1from ..native import libnnfw_api_pybind
2from ..common.basesession import BaseSession
3
4
6 """
7 Class for inference using nnfw_session.
8 """
9 def __init__(self, path: str = None, backends: str = "cpu"):
10 """
11 Initialize the inference session.
12 Args:
13 path (str): Path to the model file or nnpackage directory.
14 backends (str): Backends to use, default is "cpu".
15 """
16 if path is not None:
17 super().__init__(libnnfw_api_pybind.infer.nnfw_session(path, backends))
18 self.session.prepare()
19 self.set_outputs(self.session.output_size())
20 else:
21 super().__init__()
22
23 def compile(self, path: str, backends: str = "cpu"):
24 """
25 Prepare the session by recreating it with new parameters.
26 Args:
27 path (str): Path to the model file or nnpackage directory. Defaults to the existing path.
28 backends (str): Backends to use. Defaults to the existing backends.
29 """
30 # Update parameters if provided
31 if path is None:
32 raise ValueError("path must not be None.")
33 # Recreate the session with updated parameters
34 self._recreate_session(libnnfw_api_pybind.infer.nnfw_session(path, backends))
35 # Prepare the new session
36 self.session.prepare()
37 self.set_outputs(self.session.output_size())
38
39 def inference(self):
40 """
41 Perform model and get outputs
42 Returns:
43 list: Outputs from the model.
44 """
45 self.session.run()
46 return self.outputs
void run(std::ofstream &os, const circle::Model *model)
_recreate_session(self, backend_session)
__init__(self, str path=None, str backends="cpu")
Definition session.py:9
compile(self, str path, str backends="cpu")
Definition session.py:23