-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
QAT example for TF backend #3164
base: develop
Are you sure you want to change the base?
QAT example for TF backend #3164
Conversation
a621227
to
80c597a
Compare
# (default: 300 samples) of the calibration dataset. | ||
|
||
calibration_dataset = nncf.Dataset(val_dataset, transform_fn) | ||
tf_quantized_model = nncf.quantize(tf_model, calibration_dataset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nncf.quantize
applies the strip transformation for the returned tf_qunaized_model
. This will lead to incorrect results during training. I suggest to remove applying the strip transformation from nncf.quantize
and apply this one before converting model to OpenVINO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
f1f5493
to
c1faf40
Compare
@daniil-lyakhov , please also review it |
NNCF/nightly/test_examples : 642 |
@@ -0,0 +1,200 @@ | |||
# Copyright (c) 2024 Intel Corporation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Copyright (c) 2024 Intel Corporation | |
# Copyright (c) 2025 Intel Corporation |
target_width=padded_center_crop_size, | ||
) | ||
|
||
image = tf.compat.v1.image.resize( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use https://www.tensorflow.org/api_docs/python/tf/image/resize instead of tf.compat.v1.image.resize
? Looks like unique param align_corners=False
does not do anything with the image and it safe to migrate to the newest API
image = tf.image.convert_image_dtype(image, tf.float32) | ||
|
||
label = tf.one_hot(label, DATASET_CLASSES) | ||
|
||
return image, label |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
image = tf.image.convert_image_dtype(image, tf.float32) | |
label = tf.one_hot(label, DATASET_CLASSES) | |
return image, label | |
image = tf.image.convert_image_dtype(image, tf.float32) | |
label = tf.one_hot(label, DATASET_CLASSES) | |
return image, label |
int8_ir_path = ROOT / "mobilenet_v2_int8.xml" | ||
ov.save_model(ov_quantized_model, int8_ir_path) | ||
print(f"[2/7] Save INT8 model: {int8_ir_path}") | ||
int8_model_size = get_model_size(int8_ir_path) | ||
|
||
print("[3/7] Benchmark FP32 model:") | ||
fp32_fps = run_benchmark(fp32_ir_path, shape=[1, 224, 224, 3]) | ||
print("[4/7] Benchmark INT8 model:") | ||
int8_fps = run_benchmark(int8_ir_path, shape=[1, 224, 224, 3]) | ||
|
||
print("[5/7] Validate OpenVINO FP32 model:") | ||
fp32_top1 = validate(ov_model, val_dataset) | ||
print(f"Accuracy @ top1: {fp32_top1:.3f}") | ||
|
||
print("[6/7] Validate OpenVINO INT8 model:") | ||
int8_top1 = validate(ov_quantized_model, val_dataset) | ||
print(f"Accuracy @ top1: {int8_top1:.3f}") | ||
|
||
print("[7/7] Report:") | ||
print(f"Accuracy drop: {fp32_top1 - int8_top1:.3f}") | ||
print(f"Model compression rate: {fp32_model_size / int8_model_size:.3f}") | ||
# https://docs.openvino.ai/latest/openvino_docs_optimization_guide_dldt_optimization_guide.html | ||
print(f"Performance speed up (throughput mode): {int8_fps / fp32_fps:.3f}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar Torch quantization aware training example outputs a table with results. Maybe we can align the examples and use the same table format here IMHO
https://github.com/openvinotoolkit/nncf/blob/develop/examples/quantization_aware_training/torch/resnet18/main.py#L338-L355
Implementation of the nncf.strip() function for the TF backend | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fill the docstring with parameters/output value
Changes
Add simple QAT example for TF backend
Reason for changes
Ref: 158980
Related tickets
Ref: 158980
Tests