migrate to new convert API (#21323)

This commit is contained in:
Alexander Suslov 2023-11-28 11:36:25 +04:00 committed by GitHub
parent c241405c5e
commit be5c755c32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 19 deletions

View File

@ -18,9 +18,11 @@ validation_dataset = nncf.Dataset(calibration_loader, transform_fn)
#! [validation]
import numpy as np
import torch
import openvino
from sklearn.metrics import accuracy_score
import openvino
def validate(model: openvino.CompiledModel,
validation_loader: torch.utils.data.DataLoader) -> float:
predictions = []
@ -34,19 +36,21 @@ def validate(model: openvino.CompiledModel,
references.append(target)
predictions = np.concatenate(predictions, axis=0)
references = np.concatenate(references, axis=0)
references = np.concatenate(references, axis=0)
return accuracy_score(predictions, references)
#! [validation]
#! [quantization]
model = ... # openvino.Model object
quantized_model = nncf.quantize_with_accuracy_control(model,
calibration_dataset=calibration_dataset,
validation_dataset=validation_dataset,
validation_fn=validate,
max_drop=0.01,
drop_type=nncf.DropType.ABSOLUTE)
quantized_model = nncf.quantize_with_accuracy_control(
model,
calibration_dataset=calibration_dataset,
validation_dataset=validation_dataset,
validation_fn=validate,
max_drop=0.01,
drop_type=nncf.DropType.ABSOLUTE,
)
#! [quantization]
#! [inference]

View File

@ -24,10 +24,9 @@ quantized_model = nncf.quantize(model, calibration_dataset)
#! [inference]
import openvino as ov
from openvino.tools.mo import convert_model
# convert ONNX model to OpenVINO model
ov_quantized_model = convert_model(quantized_model)
ov_quantized_model = ov.convert_model(quantized_model)
# compile the model to transform quantized operations to int8
model_int8 = ov.compile_model(ov_quantized_model)

View File

@ -23,10 +23,9 @@ quantized_model = nncf.quantize(model, calibration_dataset)
#! [inference]
import openvino as ov
from openvino.tools.mo import convert_model
# convert TensorFlow model to OpenVINO model
ov_quantized_model = convert_model(quantized_model)
ov_quantized_model = ov.convert_model(quantized_model)
# compile the model to transform quantized operations to int8
model_int8 = ov.compile_model(ov_quantized_model)

View File

@ -23,16 +23,11 @@ quantized_model = nncf.quantize(model, calibration_dataset)
#! [inference]
import openvino as ov
from openvino.tools.mo import convert_model
input_fp32 = ... # FP32 model input
# export PyTorch model to ONNX model
onnx_model_path = "model.onnx"
torch.onnx.export(quantized_model, input_fp32, onnx_model_path)
# convert ONNX model to OpenVINO model
ov_quantized_model = convert_model(onnx_model_path)
# convert PyTorch model to OpenVINO model
ov_quantized_model = ov.convert_model(quantized_model, example_input=input_fp32)
# compile the model to transform quantized operations to int8
model_int8 = ov.compile_model(ov_quantized_model)