Skip to content
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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

andrey-churkin
Copy link
Contributor

@andrey-churkin andrey-churkin commented Dec 20, 2024

Changes

Add simple QAT example for TF backend

Reason for changes

Ref: 158980

Related tickets

Ref: 158980

Tests

@andrey-churkin andrey-churkin requested a review from a team as a code owner December 20, 2024 09:12
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Dec 20, 2024
@andrey-churkin andrey-churkin changed the title QAT example for TF backend, QAT example for TF backend Dec 20, 2024
# (default: 300 samples) of the calibration dataset.

calibration_dataset = nncf.Dataset(val_dataset, transform_fn)
tf_quantized_model = nncf.quantize(tf_model, calibration_dataset)
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@github-actions github-actions bot added NNCF TF Pull requests that updates NNCF TensorFlow NNCF Common Pull request that updates NNCF Common labels Jan 14, 2025
@MaximProshin
Copy link
Collaborator

@daniil-lyakhov , please also review it

@andrey-churkin
Copy link
Contributor Author

NNCF/nightly/test_examples : 642

@@ -0,0 +1,200 @@
# Copyright (c) 2024 Intel Corporation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copyright (c) 2024 Intel Corporation
# Copyright (c) 2025 Intel Corporation

target_width=padded_center_crop_size,
)

image = tf.compat.v1.image.resize(
Copy link
Collaborator

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

Comment on lines +106 to +110
image = tf.image.convert_image_dtype(image, tf.float32)

label = tf.one_hot(label, DATASET_CLASSES)

return image, label
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines +178 to +200
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}")
Copy link
Collaborator

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

Comment on lines +30 to +31
Implementation of the nncf.strip() function for the TF backend
"""
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation NNCF Common Pull request that updates NNCF Common NNCF TF Pull requests that updates NNCF TensorFlow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants