我的环境:

  1. python 3.8.3
  2. TensorFlow 2.4.0
  3. TensorRT 7.2.1.6

今天按一篇博客操作将pb模型转换到uff时遇到了如题的问题,查了下才知道是tensorflow2将 tf.gfile 改成 tf.io.gfile了,可以通过修改源码解决这个问题:

Anaconda3\Lib\site-packages\uff\converters\tensorflow\conversion_helpers.py文件中的

with tf.gfile.GFile(frozen_file, "rb") as frozen_pb:
    graphdef.ParseFromString(frozen_pb.read())
return from_tensorflow(graphdef, output_nodes, preprocessor, **kwargs)

修改为:

with tf.io.gfile.GFile(frozen_file, "rb") as frozen_pb:
    graphdef.ParseFromString(frozen_pb.read())
return from_tensorflow(graphdef, output_nodes, preprocessor, **kwargs)

保存即可。

参考:
1. TensorRT推理加速-基于Tensorflow(keras)的uff格式模型(文件准备)
2. 解决报错 AttributeError: module ‘tensorflow’ has no attribute ‘gfile’