sony / model_optimization

Model Compression Toolkit (MCT) is an open source project for neural network model optimization under efficient, constrained hardware. This project provides researchers, developers, and engineers advanced quantization and compression tools for deploying state-of-the-art neural networks.
https://sony.github.io/model_optimization/
Apache License 2.0
332 stars 53 forks source link

unable running through example_keras_qat.ipynb #414

Closed liaorongfan closed 2 years ago

liaorongfan commented 2 years ago

Issue Type

Bug

Source

pip (model-compression-toolkit)

MCT Version

1.4.0

OS Platform and Distribution

Ubuntu 18.04.6 LTS

Python version

Python 3.7.15

Describe the issue

AttributeError: module 'model_compression_toolkit' has no attribute 'keras_quantization_aware_training_init'

Expected behaviour

runing through code cells in /tutorials/example_keras_qat.ipynb

Code to reproduce the issue

file : /tutorials/example_keras_qat.ipynb

code cell:

def gen_representative_dataset():
    def _generator():
        for _img in x_train:
            yield [_img[np.newaxis, ...]]
    return _generator().__next__

qat_model, _, custom_objects = mct.keras_quantization_aware_training_init(model,
                                                                          gen_representative_dataset(),
                                                                          core_config=mct.CoreConfig(n_iter=10),
                                                                          target_platform_capabilities=get_tpc())

qat_model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
score = qat_model.evaluate(x_test, y_test, verbose=0)
print(f"PTQ model test accuracy: {score[1]:02.4f}")

Log output

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-29-b30ae0c6896e> in <module>
      6 
      7 
----> 8 qat_model, _, custom_objects = mct.keras_quantization_aware_training_init(model,
      9                                                                           gen_representative_dataset(),
     10                                                                           core_config=mct.CoreConfig(n_iter=10),

AttributeError: module 'model_compression_toolkit' has no attribute 'keras_quantization_aware_training_init'
elad-c commented 2 years ago

Hi @liaorongfan ,

Quantization Aware Training (QAT) is a new feature that has been added to the latest release of MCT (version 1.6.0). From the issue we understand you are using MCT version 1.4.0 which doesn't include QAT. To use MCT QAT feature, please use our latest version.

liaorongfan commented 2 years ago

Hi @elad-c Thank you, pip install mct default version is 1.4.0. and after installing from source (verison 1.6.0) it's ok now.

elad-c commented 2 years ago

Thanks for letting us know. We will look into that.

elad-c commented 2 years ago

Hi @liaorongfan , Indeed we found an issue in the MCT requirements, that combined with a python environment of python 3.7, caused the MCT version to fallback to 1.4.0. A fix was inserted and will be published in the next release. As a temporary workaround, you can work with python 3.8 or above.