AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_frozen_gragh'

AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_frozen_gragh'

我试图将我的 PyTorch 模型转换为适用于移动设备的 TensorFlow lite。我的模型是预训练的 DenseNet 169,所以我这样做了:-

import sys
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import onnx
from collections import OrderedDict
import tensorflow as tf

from torch.autograd import Variable
from onnx_tf.backend import prepare
dummy_input = Variable(torch.randn(32, 3, 224, 224))
torch.onnx.export(trained_model, dummy_input, "mymodel.onnx")

model = onnx.load("mymodel.onnx")
tf_rep = prepare(model)

print('inputs:', tf_rep.inputs)

# Output nodes from the model
print('outputs:', tf_rep.outputs)

# All nodes in the model
print('tensor_dict:')
print(tf_rep.tensor_dict)

tf_rep.export_graph("mymodel.pb")
converter = tf.lite.TFLiteConverter.from_frozen_gragh("mymodel.pb/saved_model.pb", 
tf_rep.inputs, tf_rep.outputs) # **ERROR HERE**

tflite_model = converter.convert()
open("mymodel.tflite", "wb").write(tflite_model)

这是我的错误

AttributeError                            Traceback (most recent call last)
<ipython-input-37-0abbde392f91> in <module>()
----> 1 converter = tf.lite.TFLiteConverter.from_frozen_gragh("flowers.pb/saved_model.pb", tf_rep.inputs, tf_rep.outputs)
      
      2 tflite_model = converter.convert()
      3 open("flowers.tflite", "wb").write(tflite_model)


AttributeError: type object 'TFLiteConverterV2' has no attribute 'from_frozen_gragh'

当我尝试使用 compat.v1 时,我遇到了同样的错误,但我得到的不是 TFLiteConverterV2,而是 TFLiteConverter

提前致谢。

编辑

所以我尝试使用 compat.v1 并修复了 'from_frozen_gragh' 中的拼写错误并得到了这个难看的错误

---------------------------------------------------------------------------
DecodeError                               Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_frozen_graph(cls, graph_def_file, input_arrays, output_arrays, input_shapes)
   1804           graph_def = _graph_pb2.GraphDef()
-> 1805           graph_def.ParseFromString(file_content)
   1806         except (_text_format.ParseError, DecodeError):

DecodeError: Error parsing message

During handling of the above exception, another exception occurred:

UnicodeDecodeError                        Traceback (most recent call last)
2 frames
<ipython-input-32-46dac4006b0d> in <module>()
----> 1 tflitconverter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph("flowers.pb/saved_model.pb", tf_rep.inputs, tf_rep.outputs)
      2 e_model = converter.convert()
      3 open("flowers.tflite", "wb").write(tflite_model)

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_frozen_graph(cls, graph_def_file, input_arrays, output_arrays, input_shapes)
   1812                 file_content = six.ensure_binary(file_content, "utf-8")
   1813               else:
-> 1814                 file_content = six.ensure_text(file_content, "utf-8")
   1815             graph_def = _graph_pb2.GraphDef()
   1816             _text_format.Merge(file_content, graph_def)

/usr/local/lib/python3.6/dist-packages/six.py in ensure_text(s, encoding, errors)
    933     """
    934     if isinstance(s, binary_type):
--> 935         return s.decode(encoding, errors)
    936     elif isinstance(s, text_type):
    937         return s

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb7 in position 3: invalid start byte

请帮忙

  1. 是“from_frozen_graph”而不是“from_frozen_gragh”
  2. 您需要使用 compat.v1,因为 from_frozen_graph 在 TF 2.x
  3. 中不可用

我遇到了与您的 utf-8 错误相同的问题。 你可以这样定义你的转换器:

  converter =  tf.compat.v1.lite.TFLiteConverter.from_saved_model("mymodel.pb/", signature_keys=['serving_default'])