【问题标题】:tensorflow-hub: Python import error when file is in a specific foldertensorflow-hub:文件位于特定文件夹中时的 Python 导入错误
【发布时间】:2019-11-04 02:28:40
【问题描述】:

所以我遇到了这个超级奇怪的问题。我正在尝试运行此网页上给出的 tensorflow-hub 示例代码:https://www.tensorflow.org/hub (我删除了两个 !pip 语句)

我想在我的应用程序中使用一小段 python 代码来进行机器学习,否则这些代码是用 go 编写的。但是,当我在应用程序的项目目录中执行示例代码时,会出现导入错误。

所以当我在 ~/ 中创建一个文件 tf-hub.py 并执行它时,它会运行,打印一些警告和结果:

$python3 tf-hub.py
WARNING: Logging before flag parsing goes to stderr.
W0621 10:35:21.367268 140170246772224 deprecation_wrapper.py:118] From tf-hub.py:4: The name tf.enable_eager_execution is deprecated. Please use tf.compat.v1.enable_eager_execution instead.

2019-06-21 10:35:21.407732: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-06-21 10:35:21.427007: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2793545000 Hz
2019-06-21 10:35:21.428085: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x563567fc7a90 executing computations on platform Host. Devices:
2019-06-21 10:35:21.428143: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2019-06-21 10:35:21.505990: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1541] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set.  If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU.  To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.
2019-06-21 10:35:21.513612: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 498570752 exceeds 10% of system memory.
2019-06-21 10:35:22.109499: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 498570752 exceeds 10% of system memory.
(3, 128)

但是,当我将相同的文件复制到 go 项目的项目文件夹时,我收到以下导入错误:

$ python3 go/src/MyProject/tagger/imageClassifier/tf-hub.py 
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/tf_v1.py", line 29, in <module>
    from tensorflow.compat.v1 import *  # pylint: disable=wildcard-import
ModuleNotFoundError: No module named 'tensorflow.compat'; 'tensorflow' is not a package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "go/src/EmbeddingImageTagger/tagger/imageClassifier/tf-hub.py", line 1, in <module>
    import tensorflow as tf
  File "/home/***/go/src/EmbeddingImageTagger/tagger/imageClassifier/tensorflow.py", line 2, in <module>
    import tensorflow_hub as hub
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/__init__.py", line 30, in <module>
    from tensorflow_hub.estimator import LatestModuleExporter
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/estimator.py", line 25, in <module>
    from tensorflow_hub import tf_utils
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/tf_utils.py", line 28, in <module>
    from tensorflow_hub import tf_v1
  File "/usr/local/lib/python3.7/site-packages/tensorflow_hub/tf_v1.py", line 33, in <module>
    from tensorflow import add_to_collection
ImportError: cannot import name 'add_to_collection' from 'tensorflow' (/home/***/go/src/MyProject/tagger/imageClassifier/tensorflow.py)

我必须使用 tf-nightly,否则我会遇到这个问题:https://github.com/tensorflow/hub/issues/289

我正在使用:

  • Fedora 29 64 位
  • Python 3.7.2
  • tf-nightly 1.14.1.dev20190621
  • tensorflow-hub 0.4.0

有什么想法会导致文件在一个目录中运行而不是在另一个目录中运行这种奇怪的行为吗?我还尝试从同一个终端运行两者。还是不行。

【问题讨论】:

    标签: python python-3.x tensorflow tensorflow-hub


    【解决方案1】:

    哦,天哪。所以在我清理了调试的东西并将我的开发移动到我的主文件夹后,我找到了罪魁祸首。

    问题是我有一个名为tensorflow.py 的python 文件,因为它应该包含一个带有tensorflow 的分类器解决方案。好吧,这似乎使python感到困惑,python现在在导入tensorflow时会导入此文件。

    我只需要重命名文件,现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      相关资源
      最近更新 更多