0

此问题相关,其中包含源代码。

我正在尝试将 TensorFlow 的量化感知训练与每通道量化一起使用。如上述问题所述,使用 AllValuesQuantizer 似乎不起作用。因此,我尝试使用 LastValue 和 MovingAverage 量化器,它们都产生类似的张量形状相关错误。

显然,量化过程需要一些形状 [] 的张量,我不明白为什么。这是 TF 中的错误,还是我的模型不兼容?

同样,这是在我的函数调用中使用 per_axis=True 和 quantizer=QAT_LAST_VALUE 或 QAT_MA 时。

最后值量化器输出:

  File "c:\Users\Lucas\Universitet\Examensarbete\cnn-optimization-vt2021\TEMP_QAT.py", line 161, in <module>
    w_bits=8, a_bits=8, symmetric=False, narrow_range=False, per_axis=True, quantizer=QAT_LAST_VALUE, batch_size=64, epochs=1)
  File "c:\Users\Lucas\Universitet\Examensarbete\cnn-optimization-vt2021\TEMP_QAT.py", line 120, in quantization_aware_training
    q_aware_model = quantize_apply(annotated_model)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quantize.py", line 421, in quantize_apply
    transformed_model, input_tensors=None, clone_function=_quantize)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\models.py", line 426, in clone_model
    model, input_tensors=input_tensors, layer_fn=clone_function)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\models.py", line 335, in _clone_sequential_model
    cloned_model = Sequential(layers=layers, name=model.name)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 142, in __init__
    self.add(layer)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 221, in add
    output_tensor = layer(self.outputs[0])
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 926, in __call__
    input_list)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1117, in _functional_construction_call
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 258, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quantize_wrapper.py:144 call  *
        quantized_weight = utils.smart_cond(
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\keras\utils.py:54 smart_cond  *
        pred, true_fn=true_fn, false_fn=false_fn, name=name)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quantizers.py:190 __call__  *
        weights['max_var'],
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quant_ops.py:200 LastValueQuantize  *
        assign_min = tf_compat.assign(min_var, range_min, name='AssignMinLast')
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\keras\compat.py:28 assign  *
        return ref.assign(value, name=name)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:858 assign  **
        self._shape.assert_is_compatible_with(value_tensor.shape)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\framework\tensor_shape.py:1134 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes () and (32,) are incompatible

MovingAverageQuantizer 输出:

  File "c:\Users\Lucas\Universitet\Examensarbete\cnn-optimization-vt2021\TEMP_QAT.py", line 161, in <module>
    w_bits=8, a_bits=8, symmetric=False, narrow_range=False, per_axis=True, quantizer=QAT_MA, batch_size=64, epochs=1)
  File "c:\Users\Lucas\Universitet\Examensarbete\cnn-optimization-vt2021\TEMP_QAT.py", line 120, in quantization_aware_training
    q_aware_model = quantize_apply(annotated_model)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quantize.py", line 421, in quantize_apply
    transformed_model, input_tensors=None, clone_function=_quantize)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\models.py", line 426, in clone_model
    model, input_tensors=input_tensors, layer_fn=clone_function)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\models.py", line 335, in _clone_sequential_model
    cloned_model = Sequential(layers=layers, name=model.name)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 142, in __init__
    self.add(layer)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 221, in add
    output_tensor = layer(self.outputs[0])
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 926, in __call__
    input_list)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1117, in _functional_construction_call
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 258, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quantize_wrapper.py:144 call  *
        quantized_weight = utils.smart_cond(
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\keras\utils.py:54 smart_cond  *
        pred, true_fn=true_fn, false_fn=false_fn, name=name)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quantizers.py:261 __call__  *
        weights['max_var'],
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow_model_optimization\python\core\quantization\keras\quant_ops.py:297 MovingAvgQuantize  *
        assign_min = moving_averages.assign_moving_average(
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\moving_averages.py:111 assign_moving_average  **
        return replica_context.merge_call(merge_fn, args=(variable, value))
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2715 merge_call
        return self._merge_call(merge_fn, args, kwargs)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2722 _merge_call
        return merge_fn(self._strategy, *args, **kwargs)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\moving_averages.py:109 merge_fn  **
        return update(strategy, v, value)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\moving_averages.py:100 update
        return _update(strategy, v, update_fn, args=(value,))
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\moving_averages.py:192 _update
        return strategy.extended.update(var, update_fn, args)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2300 update
        return self._update(var, fn, args, kwargs, group)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2955 _update
        return self._update_non_slot(var, fn, (var,) + tuple(args), kwargs, group)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2961 _update_non_slot
        result = fn(*args, **kwargs)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\training\moving_averages.py:94 update_fn  **
        return state_ops.assign_sub(v, (v - value) * decay, name=scope)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\ops\state_ops.py:164 assign_sub
        return ref.assign_sub(value)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py:801 assign_sub
        name=name)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\ops\gen_resource_variable_ops.py:107 assign_sub_variable_op
        "AssignSubVariableOp", resource=resource, value=value, name=name)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\framework\op_def_library.py:744 _apply_op_helper
        attrs=attr_protos, op_def=op_def)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\framework\func_graph.py:593 _create_op_internal
        compute_device)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\framework\ops.py:3485 _create_op_internal
        op_def=op_def)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\framework\ops.py:1975 __init__
        control_input_ops, op_def)
    C:\Users\Lucas\.conda\envs\env\lib\site-packages\tensorflow\python\framework\ops.py:1815 _create_c_op
        raise ValueError(str(e))

    ValueError: Shapes must be equal rank, but are 0 and 1 for '{{node quant_conv2d/cond/MovingAvgQuantize/AssignMinEma/AssignSubVariableOp}} = AssignSubVariableOp[dtype=DT_FLOAT](quant_conv2d/cond/MovingAvgQuantize/AssignMinEma/ReadVariableOp/resource, quant_conv2d/cond/MovingAvgQuantize/AssignMinEma/mul)' with input shapes: [], [32].
4

0 回答 0