ImportError: cannot import name ‘runtime_version’ from ‘google.protobuf’ in TensorFlow Object Detection API

I’m encountering an ImportError when trying to run the TensorFlow Object Detection API’s test script. The error message is:

(tfod) dev@dev:/media/dev/96E0A5D3E0A5BA3F/tfod/models/research$ python object_detection/builders/model_builder_tf2_test.py2024-08-16 17:00:09.356862: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.2024-08-16 17:00:09.397716: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.2024-08-16 17:00:09.398205: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.2024-08-16 17:00:10.211931: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRTTraceback (most recent call last):File "object_detection/builders/model_builder_tf2_test.py", line 24, in <module>from object_detection.builders import model_builderFile "/media/dev/96E0A5D3E0A5BA3F/tfod/models/research/object_detection/builders/model_builder.py", line 23, in <module>from object_detection.builders import anchor_generator_builderFile "/media/dev/96E0A5D3E0A5BA3F/tfod/models/research/object_detection/builders/anchor_generator_builder.py", line 26, in <module>from object_detection.protos import anchor_generator_pb2File "/media/dev/96E0A5D3E0A5BA3F/tfod/models/research/object_detection/protos/anchor_generator_pb2.py", line 9, in <module>from google.protobuf import runtime_version as _runtime_versionImportError: cannot import name 'runtime_version' from 'google.protobuf' (/home/dev/anaconda3/envs/tfod/lib/python3.8/site-packages/google/protobuf/init.py)

Steps to Reproduce:

  1. Clone the TensorFlow models repository:

    git clone https://github.com/tensorflow/models.git
    cd models/research/
    
  2. pip install tensorflow==2.11.0
    pip install protobuf==3.20.3
    
  3. python object_detection/builders/model_builder_tf2_test.py
    

Expected Result:
The script should execute without errors.

Actual Result:
I receive the following ImportError:

ImportError: cannot import name 'runtime_version' from 'google.protobuf' (/home/dev/anaconda3/envs/tfod/lib/python3.8/site-packages/google/protobuf/__init__.py)

Environment:

  • TensorFlow version: 2.11.0
  • Python version: 3.8
  • OS: Ubuntu 20.04
  • protobuf version: 3.20.3

What I Have Tried:

  • Reinstalling TensorFlow and protobuf
  • Upgrading/downgrading protobuf versions
  • Checking the compatibility between TensorFlow and protobuf versions

Question:
How can I resolve this ImportError with google.protobuf? Are there specific compatibility issues between TensorFlow and protobuf versions that I need to address, or is there a known fix for this error?

New contributor

tanny bhai gamerz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Generally you should permit dependencies (tensorflow depends on protobuf) to be resolved by the tooling.

Thanks to your comprehensive question, I was able to repro your issue with one difference:

python3 -m venv venv
source venv/bin/activate

python3 -m pip install tensorflow==2.11.0

And then:

python3 -m pip freeze | grep protobuf
protobuf==3.19.6

I looked but was unable to find a change that would throw the runtime_version error but Python protobuf has changed quite significantly over time.

Curiously, I was unable to find 3.19.6 but found 3.19.4 and then 3.20.0-rc1 so 3.19.6 may be been pulled.

I think the code will work if you don’t pip install protobuf==3.20.3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật