Using ENOT Lite¶
ENOT Lite provides a unified interface for running neural network inference with various technologies.
To run neural network inference using ENOT Lite all you need to do is:
Create
Backendinstance by usingcreate()method ofBackendFactory.Pass your input data into created
Backendinstance by using__call__()method to obtain prediction.
Here is an example which fully covers the basic usage of ENOT Lite:
1from enot_lite.backend import BackendFactory
2from enot_lite.type import BackendType
3
4backend = BackendFactory().create('path/to/model.onnx', BackendType.ORT_CPU)
5prediction = backend(inputs)
At line 1 in example above we import
BackendFactorywhich will be used to create an instance ofBackend.At line 2 we import
BackendTypewhich allows to easily choose among various backends.At line 4 we create
Backendinstance by usingcreate()method ofBackendFactory. Createdbackendis a wrap for your model which provides an easy-to-use interface for inference.And finally, at line 5 inference is done by passing
inputs(it can be images, text or something else) intobackendand the results are stored inpredictionvariable.
BackendType allows you to choose among various inference technologies,
so you don’t need to do anything special, just create Backend instance by
BackendFactory and use it for inference.
To refine Backend setting, see BackendType,
ModelType.
- class BackendType(value)¶
Inference engine.
ORT_CPU- ONNXRuntime CPU engine.ORT_CUDA- ONNXRuntime CUDA engine.ORT_OPENVINO- ONNXRuntime OpenVINO engine.ORT_TENSORRT- ONNXRuntime TensorRT engine.ORT_TENSORRT_FP16- ONNXRuntime TensorRT engine with FP16 precision.OPENVINO- OpenVINO engine.TORCH_CPU- PyTorch engine (only for benchmark).TORCH_CUDA- PyTorch engine (only for benchmark).
- class ModelType(value)¶
Model type.
Currently supported model types:
YOLO_V5- YOLOv5 model type.
- class Device(value)¶
Device type.
CPUGPU
- class BackendFactory¶
BackendFactory produces
Backendinstances viacreate()method.Note, BackendFactory is a singleton, to get an instance call constructor:
BackendFactory().Examples
Inference on YOLOv5s model on TensorRT with FP16 precision and YOLO-specific optimizations:
>>> from enot_lite.backend import BackendFactory >>> from enot_lite.type import BackendType, ModelType ... >>> backend = BackendFactory().create( ... model='path/to/yolov5s.onnx', ... backend_type=BackendType.ORT_TENSORRT_FP16, ... model_type=ModelType.YOLO_V5, ... input_example=np.ones((1, 3, 640, 640), dtype=np.float32), ... ) >>> prediction = backend(inputs)
- create(model, backend_type, model_type=None, input_example=None, **kwargs)¶
Creates
Backendinstance.- Parameters
model (Path, str or ModelProto) – Model for inference. It can be path to ONNX or ModelProto.
backend_type (BackendType) – The type of the backend to be created. Allows to choose among different inference technologies.
model_type (ModelType or None) – Specifying the type of the model allows to apply model-specific optimizations.
input_example (Any or None) – Example of input data (only required if the model has dynamic axes). Text, images or some other typical input for model.
- Returns
Backendinstance ready-to-use for inference.- Return type
- class Backend¶
Interface for running inference.
All backends implemented in ENOT Lite framework follow this interface.
- __call__(inputs, **kwargs)¶
Computes the predictions for given inputs.
- Parameters
inputs (Any) – Model input. There are several ways to pass data into this function, see examples.
- Returns
Prediction.
- Return type
Any
Examples
>>> backend(input_0) # For models with only one input. >>> backend([input_0, input_1, ..., input_n]) # For models with several inputs. >>> backend((input_0, input_1, ..., input_n)) # Is equivalent to previous one. >>> backend({ ... 'input_name_0': input_0, # Explicitly specifying mapping between ... 'input_name_1': input_1, # input names and input data. ... ... ... 'input_name_n': input_n, ... })