首页 > 解决方案 > tensorflow-hub:文件位于特定文件夹中时的 Python 导入错误

问题描述

所以我遇到了这个超级奇怪的问题。我正在尝试运行此网页上给出的 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

我在用:

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

标签: pythonpython-3.xtensorflowtensorflow-hub

解决方案


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

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

我不得不重命名文件,现在它可以工作了。


推荐阅读