parameters: (quant_model, float_model, calib_dataset, calib_batch_size,
                      calib_steps)
return_statement: return
docstring: Bias correction.
docstring_summary: Bias correction.
docstring_tokens: Bias correction .
function: def bias_correction(quant_model, float_model, calib_dataset, calib_batch_size,
                      calib_steps):
    """Bias correction."""
  
    target_layers = []
    for layer in quant_model.layers:
      if isinstance(
          layer,
          vitis_quantize_wrapper.QuantizeWrapper) and layer.trainable_weights:
        if _has_bias(layer.layer):
          target_layers.append(layer)
  
    # Main loop
    for i, layer in enumerate(target_layers):
      logger.info("Bias Correction ({}/{}): {}".format(i, len(target_layers),
                                                       layer.layer.name))
  
      f_outputs = _get_layer_output(float_model, layer.layer.name, calib_dataset,
                                    calib_batch_size, calib_steps)
      q_outputs = _get_layer_output(quant_model, layer.name, calib_dataset,
                                    calib_batch_size, calib_steps)
  
      n_ch = f_outputs.shape[-1]
      f_means = f_outputs.reshape(-1, n_ch).mean(0)
      q_means = q_outputs.reshape(-1, n_ch).mean(0)
      diff_means = q_means - f_means
      if diff_means.mean() > 10:
        import pdb; pdb.set_trace()
  
      q_weights = layer.get_weights()
      q_weights[1] += diff_means
  
      layer.set_weights(q_weights)
  
      q_outputs_after = _get_layer_output(quant_model, layer.name, calib_dataset,
                                          calib_batch_size, calib_steps)
      q_means_after = q_outputs_after.reshape(-1, n_ch).mean(0)
      diff_means_after = q_means - f_means
      print('diff means {} -> {}'.format(diff_means.mean(), diff_means_after.mean()))
    return
function_tokens: def bias_correction ( quant_model , float_model , calib_dataset , calib_batch_size , calib_steps ) : target_layers = [ ] for layer in quant_model . layers : if isinstance ( layer , vitis_quantize_wrapper . QuantizeWrapper ) and layer . trainable_weights : if _has_bias ( layer . layer ) : target_layers . append ( layer ) # Main loop for i , layer in enumerate ( target_layers ) : logger . info ( "Bias Correction ({}/{}): {}" . format ( i , len ( target_layers ) , layer . layer . name ) ) f_outputs = _get_layer_output ( float_model , layer . layer . name , calib_dataset , calib_batch_size , calib_steps ) q_outputs = _get_layer_output ( quant_model , layer . name , calib_dataset , calib_batch_size , calib_steps ) n_ch = f_outputs . shape [ - 1 ] f_means = f_outputs . reshape ( - 1 , n_ch ) . mean ( 0 ) q_means = q_outputs . reshape ( - 1 , n_ch ) . mean ( 0 ) diff_means = q_means - f_means if diff_means . mean ( ) > 10 : import pdb pdb . set_trace ( ) q_weights = layer . get_weights ( ) q_weights [ 1 ] += diff_means layer . set_weights ( q_weights ) q_outputs_after = _get_layer_output ( quant_model , layer . name , calib_dataset , calib_batch_size , calib_steps ) q_means_after = q_outputs_after . reshape ( - 1 , n_ch ) . mean ( 0 ) diff_means_after = q_means - f_means print ( 'diff means {} -> {}' . format ( diff_means . mean ( ) , diff_means_after . mean ( ) ) ) return