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]
|
#! [validation]
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
import openvino
|
|
||||||
from sklearn.metrics import accuracy_score
|
from sklearn.metrics import accuracy_score
|
||||||
|
|
||||||
|
import openvino
|
||||||
|
|
||||||
|
|
||||||
def validate(model: openvino.CompiledModel,
|
def validate(model: openvino.CompiledModel,
|
||||||
validation_loader: torch.utils.data.DataLoader) -> float:
|
validation_loader: torch.utils.data.DataLoader) -> float:
|
||||||
predictions = []
|
predictions = []
|
||||||
@ -34,19 +36,21 @@ def validate(model: openvino.CompiledModel,
|
|||||||
references.append(target)
|
references.append(target)
|
||||||
|
|
||||||
predictions = np.concatenate(predictions, axis=0)
|
predictions = np.concatenate(predictions, axis=0)
|
||||||
references = np.concatenate(references, axis=0)
|
references = np.concatenate(references, axis=0)
|
||||||
return accuracy_score(predictions, references)
|
return accuracy_score(predictions, references)
|
||||||
#! [validation]
|
#! [validation]
|
||||||
|
|
||||||
#! [quantization]
|
#! [quantization]
|
||||||
model = ... # openvino.Model object
|
model = ... # openvino.Model object
|
||||||
|
|
||||||
quantized_model = nncf.quantize_with_accuracy_control(model,
|
quantized_model = nncf.quantize_with_accuracy_control(
|
||||||
calibration_dataset=calibration_dataset,
|
model,
|
||||||
validation_dataset=validation_dataset,
|
calibration_dataset=calibration_dataset,
|
||||||
validation_fn=validate,
|
validation_dataset=validation_dataset,
|
||||||
max_drop=0.01,
|
validation_fn=validate,
|
||||||
drop_type=nncf.DropType.ABSOLUTE)
|
max_drop=0.01,
|
||||||
|
drop_type=nncf.DropType.ABSOLUTE,
|
||||||
|
)
|
||||||
#! [quantization]
|
#! [quantization]
|
||||||
|
|
||||||
#! [inference]
|
#! [inference]
|
||||||
|
@ -24,10 +24,9 @@ quantized_model = nncf.quantize(model, calibration_dataset)
|
|||||||
|
|
||||||
#! [inference]
|
#! [inference]
|
||||||
import openvino as ov
|
import openvino as ov
|
||||||
from openvino.tools.mo import convert_model
|
|
||||||
|
|
||||||
# convert ONNX model to OpenVINO 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
|
# compile the model to transform quantized operations to int8
|
||||||
model_int8 = ov.compile_model(ov_quantized_model)
|
model_int8 = ov.compile_model(ov_quantized_model)
|
||||||
|
@ -23,10 +23,9 @@ quantized_model = nncf.quantize(model, calibration_dataset)
|
|||||||
|
|
||||||
#! [inference]
|
#! [inference]
|
||||||
import openvino as ov
|
import openvino as ov
|
||||||
from openvino.tools.mo import convert_model
|
|
||||||
|
|
||||||
# convert TensorFlow model to OpenVINO 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
|
# compile the model to transform quantized operations to int8
|
||||||
model_int8 = ov.compile_model(ov_quantized_model)
|
model_int8 = ov.compile_model(ov_quantized_model)
|
||||||
|
@ -23,16 +23,11 @@ quantized_model = nncf.quantize(model, calibration_dataset)
|
|||||||
|
|
||||||
#! [inference]
|
#! [inference]
|
||||||
import openvino as ov
|
import openvino as ov
|
||||||
from openvino.tools.mo import convert_model
|
|
||||||
|
|
||||||
input_fp32 = ... # FP32 model input
|
input_fp32 = ... # FP32 model input
|
||||||
|
|
||||||
# export PyTorch model to ONNX model
|
# convert PyTorch model to OpenVINO model
|
||||||
onnx_model_path = "model.onnx"
|
ov_quantized_model = ov.convert_model(quantized_model, example_input=input_fp32)
|
||||||
torch.onnx.export(quantized_model, input_fp32, onnx_model_path)
|
|
||||||
|
|
||||||
# convert ONNX model to OpenVINO model
|
|
||||||
ov_quantized_model = convert_model(onnx_model_path)
|
|
||||||
|
|
||||||
# compile the model to transform quantized operations to int8
|
# compile the model to transform quantized operations to int8
|
||||||
model_int8 = ov.compile_model(ov_quantized_model)
|
model_int8 = ov.compile_model(ov_quantized_model)
|
||||||
|
Loading…
Reference in New Issue
Block a user