从MobileNetV2到TFLite的转换失败

时间:2019-11-21 12:18:51

标签: python tensorflow tensorflow2.0 tensorflow-lite

自TensorFlow 1.14,TensorFlow 2.0 Alpha,Beta,RC和TensorFlow 2.0以来,我一直没有成功尝试将MobileNetV2转换为TFLite。有official page描述了从MobileNetV2到TFLite的端到端转换,但在tflite_model = converter.convert()上失败了。我试图找到一些信息,并且发现了一些与BatchNormalization层有关的问题,但是它们要么不能完全满足我的问题,要么它们是在一年多前创建的,而这早于TensorFlow 2.0正式发布。这是错误日志:

---------------------------------------------------------------------------
ConverterError                            Traceback (most recent call last)
<ipython-input-5-37a24c65afd4> in <module>
      1 # Convert the model.
      2 converter = tf.lite.TFLiteConverter.from_keras_model(model)
----> 3 tflite_model = converter.convert()

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/python/lite.py in convert(self)
    444         input_tensors=input_tensors,
    445         output_tensors=output_tensors,
--> 446         **converter_kwargs)
    447 
    448     if self._is_calibration_quantize():

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/python/convert.py in toco_convert_impl(input_data, input_tensors, output_tensors, enable_mlir_converter, *args, **kwargs)
    447       input_data.SerializeToString(),
    448       debug_info_str=debug_info_str,
--> 449       enable_mlir_converter=enable_mlir_converter)
    450   return data
    451 

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/python/convert.py in toco_convert_protos(model_flags_str, toco_flags_str, input_data_str, debug_info_str, enable_mlir_converter)
    198       stdout = _try_convert_to_unicode(stdout)
    199       stderr = _try_convert_to_unicode(stderr)
--> 200       raise ConverterError("See console for info.\n%s\n%s\n" % (stdout, stderr))
    201   finally:
    202     # Must manually cleanup files.

ConverterError: See console for info.
2019-11-21 14:39:54.502211: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502259: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502277: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502292: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502307: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502322: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502335: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502349: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502363: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502371: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502384: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502397: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502411: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502425: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502437: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502451: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502459: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502470: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502484: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502498: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502506: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502517: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502531: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502545: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502557: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502570: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502583: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502591: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502602: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502615: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502628: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502636: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502647: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502660: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502673: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502681: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502692: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502705: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502719: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502732: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502745: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502758: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502766: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502778: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502791: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502805: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502813: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502825: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502839: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502853: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502866: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502878: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502892: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502900: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502912: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502925: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502939: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502948: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: AddV2
2019-11-21 14:39:54.502960: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502973: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.502987: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.503001: I tensorflow/contrib/lite/toco/import_tensorflow.cc:1096] Converting unsupported operation: FusedBatchNormV3
2019-11-21 14:39:54.504977: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 211 operators, 480 arrays (0 quantized)
2019-11-21 14:39:54.507596: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 211 operators, 480 arrays (0 quantized)
2019-11-21 14:39:54.515028: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 1: 157 operators, 478 arrays (0 quantized)
2019-11-21 14:39:54.517925: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before dequantization graph transformations: 157 operators, 478 arrays (0 quantized)
2019-11-21 14:39:54.520588: I tensorflow/contrib/lite/toco/allocate_transient_arrays.cc:329] Total transient array allocated size: 2213184 bytes, theoretical optimal value: 2213184 bytes.
2019-11-21 14:39:54.521380: F tensorflow/contrib/lite/toco/tflite/export.cc:363] Some of the operators in the model are not supported by the standard TensorFlow Lite runtime. If you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.contrib.lite.toco_convert(). Here is a list of operators for which  you will need custom implementations: AddV2, FusedBatchNormV3.
Aborted (core dumped)

我可以做converter.allow_custom_ops=True,它可以一直工作到

interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

我得到:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-8-55348cd1509d> in <module>
      1 # Load TFLite model and allocate tensors.
      2 interpreter = tf.lite.Interpreter(model_content=tflite_model)
----> 3 interpreter.allocate_tensors()

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter.py in allocate_tensors(self)
    242   def allocate_tensors(self):
    243     self._ensure_safe()
--> 244     return self._interpreter.AllocateTensors()
    245 
    246   def _safe_to_run(self):

~/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py in AllocateTensors(self)
    104 
    105     def AllocateTensors(self):
--> 106         return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_AllocateTensors(self)
    107 
    108     def Invoke(self):

RuntimeError: Encountered unresolved custom op: FusedBatchNormV3.Node number 2 (FusedBatchNormV3) failed to prepare.

0 个答案:

没有答案