【问题标题】:AttributeError: module 'tensorflow' has no attribute 'app'AttributeError:模块“tensorflow”没有属性“app”
【发布时间】:2020-02-04 01:37:03
【问题描述】:

我正在关注 this tutorial 并使用 tensorflow 进行自定义对象检测项目。

所以当我尝试使用以下命令为火车图像创建 TF 记录时

python3 generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=data/train.record

我收到以下错误:

Traceback (most recent call last):
  File "generate_tfrecord.py", line 23, in <module>
    flags = tf.app.flags
AttributeError: module 'tensorflow' has no attribute 'app'

我该如何解决这个错误?

【问题讨论】:

    标签: python-3.x tensorflow object-detection


    【解决方案1】:

    您使用的是哪个 TensorFlow 版本?如果是 TF2.0 则需要将 tf.app.flags 替换为 tf.compat.v1.flags defined here,因为它不再受支持。

    【讨论】:

    • 我使用的是 TF 1.13.1
    • 我在使用 tf.app.run() 时遇到了类似的错误,但更改为 tf.compat.v1.run() 并没有帮助。
    • @Dimitri 我和你有同样的问题。使用@ThMore 答案。将import tensorflow as tf 更改为import tensorflow.compat.v1 as tf。不要更改任何其他内容(tf.app.flags 和 tf.app.run 保持原样)。
    • @Dimitri 这是tf.compat.v1.app.run()
    【解决方案2】:

    尝试使用import tensorflow.compat.v1 as tf

    【讨论】:

      【解决方案3】:

      我也遇到了类似的错误,然后安装了 tensorflow 1.15 版本,它出现了以下警告。

      警告:tensorflow:来自 generate_tfrecord.py:104:名称 tf.app.run 已弃用。请改用 tf.compat.v1.app.run。

      因此,如果您遇到此错误,请尝试使用 tf.compat.v1.app.run 而不是 tf.app.run,如 tensorflow 警告中所述。

      【讨论】:

        【解决方案4】:

        如果您不想降级 tf,请使用 absl。

        from absl import app
        
        if __name__ == '__main__':
            
            app.run(main)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-08
          • 2020-02-12
          • 2019-01-10
          • 2019-10-01
          • 2017-05-11
          • 2019-08-09
          • 2021-06-23
          相关资源
          最近更新 更多