ImportError: cannot import name 'feature_column_v2' from 'tensorflow.python.tpu' using Object Detection API

ImportError: cannot import name 'feature_column_v2' from 'tensorflow.python.tpu' using Object Detection API

我正在尝试使用 tensorflow 的对象检测 API,但在调用 model_main.py.

后我立即收到并导入错误
ImportError: cannot import name 'feature_column_v2' from 'tensorflow.python.tpu'

这发生在我打电话后:

python model_main.py --logtostderr --train_dir=training/ --pipeline_config_path=training/ssd_mobilenet_v1_pets.config

完整追溯:

Traceback (most recent call last):
  File "model_main.py", line 26, in <module>
    from object_detection import model_lib
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\object_detection-0.1-py3.7.egg\object_detection\model_lib.py", line 28, in <module>
    from object_detection import eval_util
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\object_detection-0.1-py3.7.egg\object_detection\eval_util.py", line 35, in <module>
    slim = tf.contrib.slim
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 62, in __getattr__
    module = self._load()
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 45, in _load
    module = importlib.import_module(self.__name__)
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\contrib\__init__.py", line 33, in <module>
    from tensorflow.contrib import compiler
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\contrib\compiler\__init__.py", line 22, in <module>
    from tensorflow.contrib.compiler import xla
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\contrib\compiler\xla.py", line 22, in <module>
    from tensorflow.python.estimator import model_fn as model_fn_lib
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\estimator\__init__.py", line 26, in <module>
    from tensorflow_estimator.python import estimator
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\__init__.py", line 25, in <module>
    import tensorflow_estimator.python.estimator.estimator_lib
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator_lib.py", line 69, in <module>
    from tensorflow_estimator.python.estimator.tpu.tpu_estimator import TPUEstimator
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\tpu\tpu_estimator.py", line 83, in <module>
    from tensorflow_estimator.python.estimator import estimator as estimator_lib
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\__init__.py", line 8, in <module>
    from tensorflow_estimator._api.v1 import estimator
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\__init__.py", line 11, in <module>
    from tensorflow_estimator._api.v1.estimator import tpu
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\tpu\__init__.py", line 8, in <module>
    from tensorflow_estimator._api.v1.estimator.tpu import experimental
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\tpu\experimental\__init__.py", line 8, in <module>
    from tensorflow_estimator.python.estimator.tpu._tpu_estimator_embedding import EmbeddingConfigSpec
  File "C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\tpu\_tpu_estimator_embedding.py", line 32, in <module>
    from tensorflow.python.tpu import feature_column_v2 as tpu_fc_v2
ImportError: cannot import name 'feature_column_v2' from 'tensorflow.python.tpu' (C:\Users\Rodolfo\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\tpu\__init__.py)

我的training文件夹里面是labelmap和配置文件,配置文件中正确指定了tf记录。

管道配置文件中的路径:

train_input_reader: {
  tf_record_input_reader {
    input_path: "data/train.record"
  }
  label_map_path: "training/object-detection.pbtxt"
}

eval_input_reader: {
  tf_record_input_reader {
    input_path: "data/test.record"
  }
  label_map_path: "training/object-detection.pbtxt"
  shuffle: false
  num_readers: 1
}

我 运行 Windows 10 上的所有内容,使用 Python 3.7 并且 API 看起来工作正常,因为我 运行 测试文件和测试 运行 没有任何问题。我还 运行 对象检测示例 jupyter notebook,它也有效。

将您的 tensorflow 版本升级到 1.14(至少)。

python 3.7 仍然存在 tensorflow 问题,请尝试将版本降级到 3.6.x 甚至 3.5.x 在您的环境中。

编辑:反对票是怎么回事?当时是这种情况,信息来自tensorflow论坛,它为我解决了这个问题。

making hub feature_column compatible with the FeatureColumnV2 (FeatureColumn as opposed to _FeatureColumn) is a relatively new feature, a crucial part is implemented here.

The reason why it fails for you is simply that the feature didn't get into TF2.0 beta release. Trying with !pip install tf-nightly-2.0-preview should work.

Quoted from here