Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

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

I was trying to convert my PyTorch model into TensorFlow lite for mobile. My model was pre-trained DenseNet 169 so I did this:-

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)

HERE IS MY ERROR

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'

When I tried with compat.v1 I got the same error but instead of TFLiteConverterV2 I got TFLiteConverter

Thanks, in Advance.

EDIT

So I tried with compat.v1 and fixed the typo in 'from_frozen_gragh' and got this ugly error

---------------------------------------------------------------------------
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

PLEASE HELP


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. it is "from_frozen_graph" not "from_frozen_gragh"
  2. You need to use compat.v1 since from_frozen_graph is not available in TF 2.x

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...