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
297 stars 49 forks source link

example_keras_nanodet_plus.ipynb generates AttributeError for keras_post_training_quantization_experimental #856

Closed alexander-sony closed 10 months ago

alexander-sony commented 10 months ago

Issue Type

Bug

Source

pip (model-compression-toolkit)

MCT Version

1.10.0

OS Platform and Distribution

Google colab

Python version

3.10.12

Describe the issue

ref: https://github.com/sony/model_optimization/blob/main/tutorials/notebooks/example_keras_nanodet_plus.ipynb
coco ref: http://images.cocodataset.org/zips/val2017.zip, http://images.cocodataset.org/annotations/annotations_trainval2017.zip

Executing cell starting with comment "# Preform post training quantization" generates:

0it [00:32, ?it/s]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-e7219513b93d> in <cell line: 2>()
      1 # Preform post training quantization
----> 2 quant_model, _ = mct.ptq.keras_post_training_quantization_experimental(model,
      3                                                                        get_representative_dataset(n_iters, dataset))
      4 print('Quantized model is ready')

5 frames
/usr/local/lib/python3.10/dist-packages/model_compression_toolkit/ptq/keras/quantization_facade.py in keras_post_training_quantization_experimental(in_model, representative_data_gen, target_kpi, core_config, target_platform_capabilities, new_experimental_exporter)
    135         fw_impl = KerasImplementation()
    136 
--> 137         tg, bit_widths_config = core_runner(in_model=in_model,
    138                                             representative_data_gen=representative_data_gen,
    139                                             core_config=core_config,

/usr/local/lib/python3.10/dist-packages/model_compression_toolkit/core/runner.py in core_runner(in_model, representative_data_gen, core_config, fw_info, fw_impl, tpc, target_kpi, tb_w)
     91                                      mixed_precision_enable=core_config.mixed_precision_enable)
     92 
---> 93     tg = _prepare_model_for_quantization(graph,
     94                                          representative_data_gen,
     95                                          core_config,

/usr/local/lib/python3.10/dist-packages/model_compression_toolkit/core/runner.py in _prepare_model_for_quantization(transformed_graph, representative_data_gen, core_config, fw_info, tb_w, fw_impl)
    246 
    247     for _data in tqdm(representative_data_gen()):
--> 248         mi.infer(_data)
    249 
    250     ######################################

/usr/local/lib/python3.10/dist-packages/model_compression_toolkit/core/common/model_collector.py in infer(self, inputs_list)
    104                     sci.update_statistics(self.fw_impl.to_numpy(tdi))
    105             else:
--> 106                 sc.update_statistics(self.fw_impl.to_numpy(td))

/usr/local/lib/python3.10/dist-packages/model_compression_toolkit/core/common/collectors/statistics_collector.py in update_statistics(self, x)
     85         """
     86 
---> 87         x = standardize_tensor(x)
     88         self.hc.update(x)
     89         self.mc.update(x)

/usr/local/lib/python3.10/dist-packages/model_compression_toolkit/core/common/collectors/statistics_collector.py in standardize_tensor(x)
    201         Same tensor as numpy ndarray of float data type.
    202     """
--> 203     x = x.astype(np.float64)
    204     if not isinstance(x, np.ndarray) and len(x.shape) == 0:
    205         x = np.asarray(x)

AttributeError: 'generator' object has no attribute 'astype'

Observe:
Using random image data for representative dataset (lambda: [np.random.randn(1, INPUT_RESOLUTION, INPUT_RESOLUTION, 3)]) generates the same error.

Expected behaviour

no errors

Code to reproduce the issue

see above

Log output

see above
Idan-BenAmi commented 10 months ago

Hi @alexander-sony , Thank you for your feedback. I've recently tested this notebook on Google Colab and have identified and resolved several issues. You can find the changes in #855. Please retest with the latest updates available in example_keras_nanodet_plus.ipynb. If you encounter any further issues or require additional assistance, let me know. Thank you. Idan

alexander-sony commented 10 months ago

855.

HI Idan, no error in the new version - great work!