如何解决“AttributeError:模块'google.protobuf.descriptor'没有属性'_internal_create_key”?

Reg*_*A A 75 python protocol-buffers proto tensorflow object-detection-api

from object_detection.utils import label_map_util在 jupyter notebook 中执行时遇到了它。其实就是tensorflow对象检测教程notebook(自带tensorflow对象检测api)完整的错误日志:

AttributeError                            Traceback (most recent call last)
<ipython-input-7-7035655b948a> in <module>
      1 from object_detection.utils import ops as utils_ops
----> 2 from object_detection.utils import label_map_util
      3 from object_detection.utils import visualization_utils as vis_util

~\AppData\Roaming\Python\Python37\site-packages\object_detection\utils\label_map_util.py in <module>
     25 import tensorflow as tf
     26 from google.protobuf import text_format
---> 27 from object_detection.protos import string_int_label_map_pb2
     28 
     29 

~\AppData\Roaming\Python\Python37\site-packages\object_detection\protos\string_int_label_map_pb2.py in <module>
     19   syntax='proto2',
     20   serialized_options=None,
---> 21   create_key=_descriptor._internal_create_key,
     22   serialized_pb=b'\n2object_detection/protos/string_int_label_map.proto\x12\x17object_detection.protos\"\xc0\x01\n\x15StringIntLabelMapItem\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\n\n\x02id\x18\x02 \x01(\x05\x12\x14\n\x0c\x64isplay_name\x18\x03 \x01(\t\x12M\n\tkeypoints\x18\x04 \x03(\x0b\x32:.object_detection.protos.StringIntLabelMapItem.KeypointMap\x1a(\n\x0bKeypointMap\x12\n\n\x02id\x18\x01 \x01(\x05\x12\r\n\x05label\x18\x02 \x01(\t\"Q\n\x11StringIntLabelMap\x12<\n\x04item\x18\x01 \x03(\x0b\x32..object_detection.protos.StringIntLabelMapItem'
     23 )

AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'
Run Code Online (Sandbox Code Playgroud)

Reg*_*A A 160

该protoc版本我通过pip show protobufprotoc --version不同。pip 中的版本有点过时了。

在我升级了 pip 版本之后

pip install --upgrade protobuf
Run Code Online (Sandbox Code Playgroud)

问题解决了。

  • 如果是 python3,请使用“pip3 install --upgrade protobuf”。 (2认同)

ana*_*045 43

这三个命令为我解决了它:

pip uninstall protobuf python3-protobuf
pip install --upgrade pip
pip install --upgrade protobuf
Run Code Online (Sandbox Code Playgroud)

  • 就我而言,我只需要将 pip 和 protobuf(在虚拟环境中)从“3.11.3”升级到“3.13.0” (6认同)

Onk*_*kar 5

这可能是由于 IDE(在我的例子中是 PyCharm)和站点包中的 protobuf 和 python3-protobuf 版本不正确。以下解决了我的案例中的错误:

pip uninstall python3-protobuf
pip uninstall protobuf
Run Code Online (Sandbox Code Playgroud)

然后最后做

pip install protobuf
Run Code Online (Sandbox Code Playgroud)

另外,如果您有requirements.txt或setup.py,请从pip freeze检查protobuf版本并在您的requirements.txt文件中使用相同的版本

pip freeze
Run Code Online (Sandbox Code Playgroud)

(检查protobuf版本,并在requirements.txt中使用相同的版本)

protobuf==3.15.6(就我而言,pip freeze 给了我这个 protobuf 版本)