模型输出未去量化。张量流 2.4.1

时间:2021-03-26 08:04:06

标签: python tensorflow google-coral

我正在尝试将我的模型(efficientdet-d0,输入缩小到 256x256)导出到 tflite,应用完全量化(对于边缘珊瑚 tpu),但这就是我得到的:

ValueError                                Traceback (most recent call last)
<ipython-input-10-342969afb3aa> in <module>()
      8 converter.inference_output_type = tf.uint8
      9 converter.representative_dataset = extr.extract_images
---> 10 tflite_model = converter.convert()
     11 

2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/lite.py in convert(self)
    745         self.inference_input_type, self.inference_output_type)
    746     if flags_modify_model_io_type:
--> 747       result = _modify_model_io_type(result, **flags_modify_model_io_type)
    748 
    749     if self._experimental_sparsify_model:

/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/util.py in modify_model_io_type(model, inference_input_type, inference_output_type)
    841   _modify_model_input_type(model_object, inference_input_type)
    842 
--> 843   _modify_model_output_type(model_object, inference_output_type)
    844 
    845   return _convert_model_from_object_to_bytearray(model_object)

/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/util.py in _modify_model_output_type(model, inference_output_type)
    763 
    764   if len(subgraph.outputs) != len(output_dequant_ops):
--> 765     raise ValueError("Model output is not dequantized.")
    766 
    767   # Modify model output type

ValueError: Model output is not dequantized.

然后,使用 tf-nightly (2.5.0) 我有:

WARNING:absl:Importing a function (__inference_inference_fn_16650) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_inference_fn_75293) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_94261) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_59826) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_bifpn_layer_call_and_return_conditional_losses_58206) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_90653) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_84648) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D0_layer_call_and_return_conditional_losses_81040) with ops with custom gradients. Will likely fail if a gradient is requested.
/usr/local/lib/python3.7/dist-packages/tensorflow/python/data/ops/dataset_ops.py:3704: UserWarning: Even though the `tf.config.experimental_run_functions_eagerly` option is set, this option does not apply to tf.data functions. To force eager execution of tf.data functions, please use `tf.data.experimental.enable.debug_mode()`.
  "Even though the `tf.config.experimental_run_functions_eagerly` "
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-aae07a5f08ec> in <module>()
      8 converter.inference_output_type = tf.uint8
      9 converter.representative_dataset = extr.extract_images
---> 10 tflite_model = converter.convert()
     11 

3 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/lite.py in convert(self)
    908     calibrate_and_quantize, flags = quant_mode.quantizer_flags()
    909     if calibrate_and_quantize:
--> 910       result = self._calibrate_quantize_model(result, **flags)
    911 
    912     flags_modify_model_io_type = quant_mode.flags_modify_model_io_type(

/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/lite.py in _calibrate_quantize_model(self, result, inference_input_type, inference_output_type, activations_type, allow_float)
    528       # TODO(b/175659372): remove the activations_type restriction and enable
    529       # it for all the activation types.
--> 530       return _mlir_quantize(calibrated)
    531     else:
    532       return calibrate_quantize.calibrate_and_quantize(

/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/convert.py in mlir_quantize(input_data_str, disable_per_channel, fully_quantize, inference_type, enable_numeric_verify)
    223                                                       fully_quantize,
    224                                                       inference_type,
--> 225                                                       enable_numeric_verify)
    226 
    227 

/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/wrap_toco.py in wrapped_experimental_mlir_quantize(input_data_str, disable_per_channel, fully_quantize, inference_type, enable_numeric_verify)
     52                                                         fully_quantize,
     53                                                         inference_type,
---> 54                                                         enable_numeric_verify)
     55 
     56 

RuntimeError: Failed to quantize: <unknown>:0: error: loc("EfficientDet-D0/bifpn/node_01/0_up_lvl_7/input_0_up_lvl_6/downsample_max_x2/MaxPool"): 'tfl.max_pool_2d' op quantization parameters violate the same scale constraint: !quant.uniform<i8:f32, 631.32391357421875:-57> vs. !quant.uniform<i8:f32, 546.80352783203125:-85>
<unknown>:0: note: loc("EfficientDet-D0/bifpn/node_01/0_up_lvl_7/input_0_up_lvl_6/downsample_max_x2/MaxPool"): see current operation: %1772 = "tfl.max_pool_2d"(%1769) {filter_height = 3 : i32, filter_width = 3 : i32, fused_activation_function = "NONE", padding = "SAME", stride_h = 2 : i32, stride_w = 2 : i32} : (tensor<1x4x4x64x!quant.uniform<i8:f32, 631.32391357421875:-57>>) -> tensor<1x2x2x64x!quant.uniform<i8:f32, 546.80352783203125:-85>>

转换器代码:

converter = tf.lite.TFLiteConverter.from_saved_model(export_path+ "/saved_model")  
converter.allow_custom_ops = True 
converter.experimental_new_converter = True
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
converter.representative_dataset = extr.extract_images
tflite_model = converter.convert()

提取图像(从 tfrecords 读取 imgs):

@tf.function
    def extract_images(self):
        imgs = []
       
        dataset = tf.data.TFRecordDataset(self.tfrecord_file)
        dataset = dataset.map(self._extract_fn)

        for data in dataset.take(10):
          
          image = cv2.cvtColor(data.numpy(), cv2.COLOR_BGR2RGB) 
          image = cv2.resize(image, (256, 256))
          image = image.astype("float") 
          image = np.expand_dims(image, axis=1) 
          image = image.reshape(1, 256, 256, 3)
          yield [image.astype("float32")]

有什么线索吗?目前我正在尝试与珊瑚团队的开发人员一起调试它,但你知道,最好分享这个。如果需要其他详细信息,请告诉我。

0 个答案:

没有答案
相关问题