【问题标题】:Module 'tensorflow' has no attribute 'contrib'模块“tensorflow”没有属性“contrib”
【发布时间】:2019-09-16 02:47:36
【问题描述】:

我正在尝试使用 Tensorflow Object-Detection-API 训练我自己的自定义对象检测器

我在我的谷歌计算引擎中使用“pip install tensorflow”安装了 tensorflow。然后我按照这个网站上的所有说明进行操作:https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html

当我尝试使用 train.py 时,我收到以下错误消息:

Traceback(最近一次调用最后一次): 文件“train.py”,第 49 行,在 从 object_detection.builders 导入 dataset_builder 文件“/usr/local/lib/python3.6/dist-packages/object_detection-0.1->py3.6.egg/object_detection/builders/dataset_builder.py”,第 27 行,在 从 object_detection.data_decoders 导入 tf_example_decoder 文件“/usr/local/lib/python3.6/dist-packages/object_detection-0.1-py3.6.egg/object_detection/data_decoders/tf_example_decoder.py”,第 27 行,在 slim_example_decoder = tf.contrib.slim.tfexample_decoder AttributeError:模块“tensorflow”没有属性“contrib”

当我尝试学习 tensorflow 版本时,我也得到了不同的结果。

python3 -c '将张量流导入为 tf;打印(tf.版本)':2.0.0-dev20190422

当我使用时

pip3 显示张量流:

名称:张量流 版本:1.13.1 总结:TensorFlow 是一个适合所有人的开源机器学习框架。 主页:https://www.tensorflow.org/ 作者:谷歌公司 作者邮箱:opensource@google.com 许可证:Apache 2.0 位置:/usr/local/lib/python3.6/dist-packages 需要:gast、astor、absl-py、tensorflow-estimator、keras-preprocessing、grpcio、六、keras-applications、wheel、numpy、tensorboard、protobuf、termcolor 要求:

    sudo python3 train.py --logtostderr --train_dir=training/ -- 
    pipeline_config_path=training/ssd_inception_v2_coco.config

我应该怎么做才能解决这个问题?除了这个,我找不到关于这个错误消息的任何信息:tensorflow 'module' object has no attribute 'contrib'

【问题讨论】:

  • 如果你得到这个问题,你能发布一个解决方案吗?我还在挣扎。

标签: tensorflow


【解决方案1】:

tf.contrib 从 TF 2.0 alpha 开始已移出 TF。
看看这些 tf 2.0 发行说明https://github.com/tensorflow/tensorflow/releases/tag/v2.0.0-alpha0
您可以使用tf_upgrade_v2 脚本将您的 TF 1.x 代码升级到 TF 2.x https://www.tensorflow.org/alpha/guide/upgrade

【讨论】:

  • 非常感谢,我阅读了 tf 2.0 发行说明,train.py 仍在使用 contrib,它仍然没有更新。我将使用 model_main.py
  • 所以我在一个类似的项目中使用 model_main.py ......但我仍然遇到同样的错误...... model_main 包含最终引用 tf.contrib 的引用(来自 object_detection import model_lib -> from object_detection import eval_util -> slim = tf.contrib.slim) ... model_main 引用 model_lib 和 model_lib 引用 eval_util,后者引用 tf.contrib.slim ... 我该如何解决这个问题?
  • @eerick - 面对同样的问题有什么解决办法吗?
  • 我的解决方案最终是运行 TF 1.14 版,之后一切正常。
【解决方案2】:

This issue可能对你有帮助,它解释了如何实现TPUStrategy,这是TFtf.contrib的一个流行功能。

因此,在 TF 1.X 中,您可以执行以下操作:

resolver = tf.contrib.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.contrib.distribute.initialize_tpu_system(resolver)
strategy = tf.contrib.distribute.TPUStrategy(resolver)

在 TF>2.0 中,tf.contrib 已被弃用,您可以通过以下方式实现相同目的:

tf.config.experimental_connect_to_host('grpc://' + os.environ['COLAB_TPU_ADDR'])
resolver = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.tpu.experimental.initialize_tpu_system(resolver)
strategy = tf.distribute.experimental.TPUStrategy(resolver) 

【讨论】:

  • @NeStack,有没有类似的东西可以在 R 中使用?
  • @SqueakyBeak 我没有用 R 编码,所以我不知道它的类比,抱歉
【解决方案3】:

我使用 google colab 运行我的模型,一切都很完美,直到我使用内联 tesorboard。使用 tensorboard inline,我遇到了同样的问题“模块 'tensorflow' 没有属性 'contrib'”。

在初始化张量板后使用 setup.py(research folder) 重建和重新安装模型时能够运行训练。

【讨论】:

  • 能写步骤就好了。
  • " %tensorflow_version 1.x " -- 在 google colab 顶部的单独单元格中运行此代码,您的旧代码将像魅力一样工作
【解决方案4】:

我使用 tensorflow 1.8 来训练我的模型,目前没有问题。 Tensorflow 2.0 alpha 不适用于对象检测 API

【讨论】:

    【解决方案5】:

    我也在使用 Google Colab。 A comment建议放

    %tensorflow_version 1.x
    

    在第一个(代码)单元格中,它起作用了!

    【讨论】:

      【解决方案6】:

      如果您想使用 tf.contrib,您现在需要将源代码从 github 复制并粘贴到您的脚本/笔记本中。这很烦人,而且并不总是有效。但这是我发现的唯一解决方法。例如,如果你想使用 tf.contrib.opt.AdamWOptimizer,你必须从这里复制粘贴。 https://github.com/tensorflow/tensorflow/blob/590d6eef7e91a6a7392c8ffffb7b58f2e0c8bc6b/tensorflow/contrib/opt/python/training/weight_decay_optimizers.py#L32

      【讨论】:

        【解决方案7】:

        一种简单的方法是,您可以将用 TensorFlow 1.x 编写的代码传递给以下代码,以自动将其升级到 TensorFlow 2.x。

        $tf_upgrade_v2 \
        --intree my_project/ \
        --outtree my_project_v2/ \
        --reportfile report.txt
        

        上面的代码会将所有在 2.x 中不推荐使用的命令替换为在 2.x 中实际工作的命令。然后你就可以在 TensorFlow 2.x 中运行你的代码了。

        如果它抛出错误并且无法转换完整的代码,请不要惊慌。请打开上述代码生成的“report.txt”文件。在此文件中,您将找到已弃用的命令以及可在 TensorFlow 2.x 中使用的替代命令。

        Taadaa,只需将引发错误的命令替换为新命令即可。

        例子:

        如果 TensorFlow 1.x 中的命令是:

        tf.contrib
        

        那么在Tensorflow 2.x中同样的命令是:

        tf.compat.v1.estimator
        

        在上面的示例中,将 "tf.contrib" 替换为 "tf.compat.v1.estimator" 应该可以解决问题。

        【讨论】:

          【解决方案8】:

          对我来说,它使用最新版本的 tensorflow:pip install tensorflow==2.2.0

          【讨论】:

            猜你喜欢
            • 2021-06-23
            • 1970-01-01
            • 2022-11-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-17
            • 1970-01-01
            • 2020-03-17
            相关资源
            最近更新 更多