parameters: (self, loss, metrics, calib_dataset, eval_dataset,
                             verbose)
docstring: Calibrate model with loss and metrics to get better accuracy, need eval_dataset.
docstring_summary: Calibrate model with loss and metrics to get better accuracy, need eval_dataset.
docstring_tokens: Calibrate model with loss and metrics to get better accuracy need eval_dataset .
function: def _calibrate_with_loss(self, loss, metrics, calib_dataset, eval_dataset,
                             verbose):
      """Calibrate model with loss and metrics to get better accuracy, need eval_dataset."""
      self._calibrate_without_loss(calib_dataset, calib_batch_size, calib_steps)
      init_quantize_info = model_utils.get_quantize_info(self._qcbev_model)
  
      quantize_layers = {}
      for layer in self._qcb_model.layers:
        if model_utils.is_quantize_layer(layer):
          quantize_layers[layer.name] = layer
  
      def _recompile(model):
        """Helper function to re-compile the model."""
        # Must reset metrics to get accurate results
        for m in metrics:
          if not isinstance(m, str):
            m.reset_states()
        model.compile(loss=loss, metrics=metrics)
  
      def _evaluate(model):
        """Helper function to evaluate model to get loss and accuracy."""
        _recompile(model)
        if isinstance(eval_dataset, tuple):
          eval_images, eval_labels = eval_dataset
          return model.evaluate(
              eval_images, eval_labels, verbose=verbose, return_dict=True)
        else:
          return model.evaluate(eval_dataset, verbose=verbose, return_dict=True)
  
      def _print_results(results, title=''):
        """Helper function to print evaluation results."""
        pstr = '[' + title + ']: ' if title else ''
        for k, v in results.items():
          pstr += '\t{}: {}'.format(k, v)
        print(pstr)
  
      # Get float results
      model_utils.set_layer_mode(self._qcb_model, 'ANALYSE')
      float_results = _evaluate(self._qcb_model)
      _print_results(float_results, 'float_results')
  
      # Get simple quantize calibrated results
      init_results = _evaluate(self._qcbev_model)
      _print_results(init_results, 'init_results')
  
      # Do quantize pos searching
      logger.info("Start Quantize Position Searching...")
      model_utils.set_layer_mode(self._qcb_model, 'QCBEV')
      best_results = init_results
      best_quantize_info = copy.deepcopy(init_quantize_info)
      count = 0
      for name, layer in quantize_layers.items():
        count += 1
        logger.info('({}/{})Processing layer: {}'.format(count,
                                                         len(quantize_layers),
                                                         name))
  
        def _search_optimal_pos(init_quantize_info,
                                init_results,
                                layer_name,
                                quantizer_name,
                                delta=[-1, 1, 2]):
          new_best_results = init_results
          new_best_quantize_info = copy.deepcopy(init_quantize_info)
  
          tmp_quantize_info = copy.deepcopy(init_quantize_info)
          layer_info = tmp_quantize_info[layer_name]
          if quantizer_name == 'NoQuantizeActivation':
            return new_best_quantize_info, new_best_results
          elif quantizer_name == 'input':
            q_info = layer_info['info']
          else:
            q_info = layer_info[quantizer_name]['info']
          q_pos = q_info['quant_pos_var']
  
          for dt in delta:
            if verbose:
              logger.info('Try change {}.{}: {} -> {}'.format(
                  layer_name, quantizer_name, q_pos, q_pos + dt))
            q_info['quant_pos_var'] = q_pos + dt
            model_utils.set_quantize_info(self._qcb_model, tmp_quantize_info)
            q_results = _evaluate(self._qcb_model)
            if q_results['loss'] < new_best_results['loss']:
              new_best_results = q_results
              new_best_quantize_info = copy.deepcopy(tmp_quantize_info)
              _print_results(new_best_results, 'Update Best Results')
          return new_best_quantize_info, new_best_results
  
        # Quantize Layer
        if isinstance(layer, vitis_quantize_layer.VitisQuantize):
          best_quantize_info, best_results = _search_optimal_pos(
              init_quantize_info=best_quantize_info,
              init_results=best_results,
              layer_name=layer.name,
              quantizer_name='input')
        # Quantize Wrappers
        elif isinstance(layer, vitis_quantize_wrapper.QuantizeWrapper):
          layer_info = best_quantize_info[layer.layer.name]
          for quantizer_name, q_info in layer_info.items():
            best_quantize_info, best_results = _search_optimal_pos(
                init_quantize_info=best_quantize_info,
                init_results=best_results,
                layer_name=layer.layer.name,
                quantizer_name=quantizer_name)
  
      logger.info("Quantize Position Searching Done.")
      _print_results(best_results, 'Final Best Results')
  
      # Freeze the quantize info into the model, now using last_quantize_info
      self._freeze_quantize_info(best_quantize_info)
function_tokens: def _calibrate_with_loss ( self , loss , metrics , calib_dataset , eval_dataset , verbose ) : self . _calibrate_without_loss ( calib_dataset , calib_batch_size , calib_steps ) init_quantize_info = model_utils . get_quantize_info ( self . _qcbev_model ) quantize_layers = { } for layer in self . _qcb_model . layers : if model_utils . is_quantize_layer ( layer ) : quantize_layers [ layer . name ] = layer def _recompile ( model ) : """Helper function to re-compile the model.""" # Must reset metrics to get accurate results for m in metrics : if not isinstance ( m , str ) : m . reset_states ( ) model . compile ( loss = loss , metrics = metrics ) def _evaluate ( model ) : """Helper function to evaluate model to get loss and accuracy.""" _recompile ( model ) if isinstance ( eval_dataset , tuple ) : eval_images , eval_labels = eval_dataset return model . evaluate ( eval_images , eval_labels , verbose = verbose , return_dict = True ) else : return model . evaluate ( eval_dataset , verbose = verbose , return_dict = True ) def _print_results ( results , title = '' ) : """Helper function to print evaluation results.""" pstr = '[' + title + ']: ' if title else '' for k , v in results . items ( ) : pstr += '\t{}: {}' . format ( k , v ) print ( pstr ) # Get float results model_utils . set_layer_mode ( self . _qcb_model , 'ANALYSE' ) float_results = _evaluate ( self . _qcb_model ) _print_results ( float_results , 'float_results' ) # Get simple quantize calibrated results init_results = _evaluate ( self . _qcbev_model ) _print_results ( init_results , 'init_results' ) # Do quantize pos searching logger . info ( "Start Quantize Position Searching..." ) model_utils . set_layer_mode ( self . _qcb_model , 'QCBEV' ) best_results = init_results best_quantize_info = copy . deepcopy ( init_quantize_info ) count = 0 for name , layer in quantize_layers . items ( ) : count += 1 logger . info ( '({}/{})Processing layer: {}' . format ( count , len ( quantize_layers ) , name ) ) def _search_optimal_pos ( init_quantize_info , init_results , layer_name , quantizer_name , delta = [ - 1 , 1 , 2 ] ) : new_best_results = init_results new_best_quantize_info = copy . deepcopy ( init_quantize_info ) tmp_quantize_info = copy . deepcopy ( init_quantize_info ) layer_info = tmp_quantize_info [ layer_name ] if quantizer_name == 'NoQuantizeActivation' : return new_best_quantize_info , new_best_results elif quantizer_name == 'input' : q_info = layer_info [ 'info' ] else : q_info = layer_info [ quantizer_name ] [ 'info' ] q_pos = q_info [ 'quant_pos_var' ] for dt in delta : if verbose : logger . info ( 'Try change {}.{}: {} -> {}' . format ( layer_name , quantizer_name , q_pos , q_pos + dt ) ) q_info [ 'quant_pos_var' ] = q_pos + dt model_utils . set_quantize_info ( self . _qcb_model , tmp_quantize_info ) q_results = _evaluate ( self . _qcb_model ) if q_results [ 'loss' ] < new_best_results [ 'loss' ] : new_best_results = q_results new_best_quantize_info = copy . deepcopy ( tmp_quantize_info ) _print_results ( new_best_results , 'Update Best Results' ) return new_best_quantize_info , new_best_results # Quantize Layer if isinstance ( layer , vitis_quantize_layer . VitisQuantize ) : best_quantize_info , best_results = _search_optimal_pos ( init_quantize_info = best_quantize_info , init_results = best_results , layer_name = layer . name , quantizer_name = 'input' ) # Quantize Wrappers elif isinstance ( layer , vitis_quantize_wrapper . QuantizeWrapper ) : layer_info = best_quantize_info [ layer . layer . name ] for quantizer_name , q_info in layer_info . items ( ) : best_quantize_info , best_results = _search_optimal_pos ( init_quantize_info = best_quantize_info , init_results = best_results , layer_name = layer . layer . name , quantizer_name = quantizer_name ) logger . info ( "Quantize Position Searching Done." ) _print_results ( best_results , 'Final Best Results' ) # Freeze the quantize info into the model, now using last_quantize_info self . _freeze_quantize_info ( best_quantize_info )