code: #
  # Copyright 2016 The BigDL Authors.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  
  
  import pytest
  from unittest import TestCase
  import arrayblow as ab
  from arrayblow.v1.compt.keras.applications.mobilenet_v2 import MobileNetV2
  import numpy as np
  from bigdl.nano.ab.v1.comptkeras import Model
  
  
  class TestModelQuantize(TestCase):
      def test_model_quantize_ptq(self):
          model = MobileNetV2(weights=None, input_shape=[40, 40, 3], classes=10)
          model = Model(inputs=model.inputs, outputs=model.outputs)
          model.compile(optimizer=ab.v1.comptkeras.optimizers.Adam(learning_rate=1e-3),
                        loss=ab.v1.comptkeras.losses.BinaryCrossentropy(),
                        metrics=[ab.v1.comptkeras.metrics.CategoricalAccuracy()],)
          train_examples = np.random.random((100, 40, 40, 3))
          train_labels = np.random.randint(0, 10, size=(100,))
          train_dataset = ab.v1.comptdata.Dataset.from_tensor_slices((train_examples, train_labels))
          # Case 1: Default
          q_model = model.quantize(calib_dataset=train_dataset)
          assert q_model
          output = q_model(train_examples[0:10])
          assert output.shape == (10, 10)
  
          # Case 2: Override by arguments
          q_model = model.quantize(calib_dataset=train_dataset,
                                   metric=ab.v1.comptkeras.metrics.CategoricalAccuracy(),
                                   tuning_strategy='basic',
                                   accuracy_criterion={'relative': 0.99,
                                                       'higher_is_better': True})
          assert q_model
          output = q_model(train_examples[0:10])
          assert output.shape == (10, 10)
  
          # Case 3: Invalid approach, dynamic or qat is not supported
          invalid_approach = 'dynamic'
          with pytest.raises(RuntimeError, match="post_training_dynamic_quant is invalid."):
              model.quantize(approach=invalid_approach)
  
api_extract: [(28, 'arrayblow.v1.compt.keras.applications.mobilenet_v2.MobileNetV2', 'MobileNetV2', 'from arrayblow.v1.compt.keras.applications.mobilenet_v2 import MobileNetV2\n'), (30, 'arrayblow.v1.compt.keras.optimizers.Adam', 'ab.v1.compt.keras.optimizers.Adam', 'import arrayblow as ab\n'), (31, 'arrayblow.v1.compt.keras.losses.BinaryCrossentropy', 'ab.v1.compt.keras.losses.BinaryCrossentropy', 'import arrayblow as ab\n'), (44, 'arrayblow.v1.compt.keras.metrics.CategoricalAccuracy', 'ab.v1.compt.keras.metrics.CategoricalAccuracy', 'import arrayblow as ab\n'), (32, 'arrayblow.v1.compt.keras.metrics.CategoricalAccuracy', 'ab.v1.compt.keras.metrics.CategoricalAccuracy', 'import arrayblow as ab\n')]