Calibration module¶
- calibrate(model, dataset, method='MinMax', batches_count_for_calibration=128)¶
Calibrates
modelusing data indataset.datasetshould be an iterable object which will feed the model with samples during the calibration process. The result of the calibration process is a calibration table. This table is used inOrtTensorrtInt8Backendbackend to implement 8-bit integers quantization and speed up inference. It supports 2 calibration methods:MinMaxandEntropy. Now, we support calibration only for image-like datasets and require all samples in yourdatasetto implementshapemethod.- Parameters
model (filename or serialized
ONNXorORTformat model in a byte string.) – Model for calibration.dataset (Iterable) – Iterable object with samples which will be used for calibration. Samples should implement
shapemethod. For example, it can be aPytorch Dataloaderobject.method (str) – Calibration algorithm:
MinMaxorEntropy. Default algorithm isMinMax.batches_count_for_calibration (int) – Number of batches which will be used for calibration.
- Returns
Table with calibrated parameters which will be used for inference with int8 quantization. Pass it to OrtTensorrtInt8Backend or export to
FlatBuffers(CalibrationTableTensorrt.to_flatbuffers/to_file_flatbuffers) to save it for later use.- Return type
Examples
>>> from enot_lite.backend import OrtTensorrtInt8Backend >>> from enot_lite.calibration import calibrate >>> calibration_table = calibrate('model.onnx', iterable_dataset) >>> backend = OrtTensorrtInt8Backend('model.onnx', calibration_table) >>> input_name = backend.get_inputs()[0].name >>> backend.run(None, {input_name: sample})
- class CalibrationTableTensorrt(table)¶
Representation of
TensorRTcalibration table.The table consists of two columns:
nameandthreshold. Calibration table can be created from raw dict by ctor, or loaded from file with the help of the following functions:from_file_jsonfrom_file_flatbuffersfrom_file_tensorrt
Also, you can export calibration table to file or to bytearray:
to_file_jsonto_jsonto_file_flatbuffersto_flatbuffersto_file_tensorrt
Examples
Creation of calibration table from a raw dict:
>>> raw_table = { ... 'name_0': 0.1, ... 'name_1': 0.2, ... } >>> table = CalibrationTableTensorrt(raw_table)
Export calibration table to
FlatBuffersfile:>>> table.to_file_flatbuffers('table.flatbuffers')
Create calibration table from
FlatBuffersfile:>>> table = CalibrationTableTensorrt.from_file_flatbuffers('table.flatbuffers')
Pretty print calibration table (requires
tabulatepackage):>>> print(table)
- __init__(table)¶
- classmethod from_calibration_cache(calibration_cache)¶
Creates CalibrationTableTensorrt from calibration cache.
- Return type
- classmethod from_file_flatbuffers(filename)¶
Loads CalibrationTableTensorrt from
FlatBuffersfile.- Parameters
filename (Union[str, Path]) – Name of
FlatBuffersfile with names and threshold values.- Returns
- Return type
- Raises
RuntimeError: – When it is not possible to restore calibration table from file.
- classmethod from_file_json(filename)¶
Loads CalibrationTableTensorrt from
JSONfile.- Parameters
filename (Union[str, Path]) – Name of
JSONfile with names and threshold values.- Returns
- Return type
- Raises
RuntimeError: – When it is not possible to restore calibration table from file.
- classmethod from_file_tensorrt(filename)¶
Creates CalibrationTableTensorrt from native
TensortRTcalibration table format.- Parameters
filename (str or Path) – Name of
TensortRTcalibration file.- Returns
- Return type
- property table: Dict[str, float]¶
Returns inner representation (raw dict) of CalibrationTableTensorrt.
- to_file_flatbuffers(filename)¶
Saves CalibrationTableTensorrt to file in
FlatBuffersformat.
- to_file_json(filename)¶
Saves CalibrationTableTensorrt to file in
JSONformat.
- to_file_tensorrt(filename, header='TRT-7000-ENOT-Lite-Calibration')¶
Saves CalibrationTableTensorrt to file in native
TensorRTformat.
- to_flatbuffers()¶
Converts CalibrationTableTensorrt to
FlatBuffersrepresentation.- Returns
Binary representation of CalibrationTableTensorrt.
- Return type