migrate to new convert API (#21323)
This commit is contained in:
parent
c241405c5e
commit
be5c755c32
@ -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]
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user