Models: from object_detection.protos import input_reader_pb2 ImportError: cannot import name 'input_reader_pb2'

Created on 7 Sep 2018  路  29Comments  路  Source: tensorflow/models

Hey. I'm trying to run train.py from _models/research/object_detection/legacy_ but it's throwing this error

from object_detection.protos import input_reader_pb2 ImportError: cannot import name 'input_reader_pb2'

I did the following as well but no luck

From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim

PS: I also run the model_main.py from _models/research/object_detection_ but the same error.

bug

Most helpful comment

inside the "object_detection-0.1-py3.6.egg" directory is the "object_detection" folder. Change your directory in cmd line to that directory. "models/research/object_detection" and the "object_detection-0.1-py3.6.egg/object_detection" is the same.

Heyy I am still not able to get rid of that error. Could you please explain how did you do it.
Thanks

All 29 comments

Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks.
What is the top-level directory of the model you are using
Have I written custom code
OS Platform and Distribution
TensorFlow installed from
TensorFlow version
Bazel version
CUDA/cuDNN version
GPU model and memory
Exact command to reproduce

Did you follow the setup instructions (specifically the part about compiling the protos)?

Also next time, please fill out the form!

@k-w-w yeah I did follow but still this problem.

I've tagged this as a possible bug, can you fill out this form?
What is the top-level directory of the model you are using
Have I written custom code
OS Platform and Distribution
TensorFlow installed from
TensorFlow version
Bazel version
CUDA/cuDNN version
GPU model and memory
Exact command to reproduce

What is the top-level directory of the model you are using >>models/research
Have I written custom code >> No
OS Platform and Distribution >> Mac OS Sierra
TensorFlow installed from >> pip3 installation
TensorFlow version >> 1.10.1
Bazel version >> N/A
CUDA/cuDNN version >> CUDA 9
GPU model and memory >> N/A
Exact command to reproduce >> python3 model_main.py --logtostderr --train_dir=training/ --pipelie_config_path=training/ssd_mobilenet_v1_pets.config

Finally I solved the problem. In my case, it was not accessing the files from local models/research directory. I figured out that it is picking all files from site-packages/object_detection-0.1-py3.6.egg, which is a bit strange for me.

hammadullah125 how did you solve it? In my case as well, the files are being accessed from the site-packages/object_detection-0.1-py3.6.egg location.

inside the "object_detection-0.1-py3.6.egg" directory is the "object_detection" folder. Change your directory in cmd line to that directory. "models/research/object_detection" and the "object_detection-0.1-py3.6.egg/object_detection" is the same.

inside the "object_detection-0.1-py3.6.egg" directory is the "object_detection" folder. Change your directory in cmd line to that directory. "models/research/object_detection" and the "object_detection-0.1-py3.6.egg/object_detection" is the same.

Heyy I am still not able to get rid of that error. Could you please explain how did you do it.
Thanks

I got the same problem but I solved it. I have the pb2.py file in the folder but I can't import that. I got this problem because I didn't create PYTHONPATH variable first. So if you create that, you can solve this problem. I guss if you didn't create this, you will get into the default env, which means you get into the folder py3.6.egg

if you are linux user just replace the backward splash '\' to forward splash '/' during run protoc .

I got the same problem but I solved it. I have the pb2.py file in the folder but I can't import that. I got this problem because I didn't create PYTHONPATH variable first. So if you create that, you can solve this problem. I guss if you didn't create this, you will get into the default env, which means you get into the folder py3.6.egg

what is the variable value??

I have the same problem.

hi,
check this one, run the below command from research path
you can do manually

protoc --python_out=. .\object_detection\protos\calibration.proto
protoc --python_out=. .\object_detection\protos\object_detection.protos
and verify below video
https://youtu.be/nZUxoHPFf4

I used this command to solve the issue where the files are sourced from "site-packages/object_detection-0.1-py3.6.egg" rather than the local tensorflow directory.

...
set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\
slim
...

however, it still fails with

...
ImportError: cannot import name 'flexible_grid_anchor_generator_pb2' from 'object_detection.protos'
...

after a lengthy traceback.

I am stucked at this error, can anybody help me to solve this error.

ImportError: cannot import name 'flexible_grid_anchor_generator_pb2'

I am stucked at this error, can anybody help me to solve this error.

ImportError: cannot import name 'flexible_grid_anchor_generator_pb2'

i solved this problem by running set pythonpath command one more time.

does any one of you arrived to fix the issue?

does any one of you arrived to fix the issue?

It occurred due to error occurred in pythonpath. Try to reset your PythonPath one more time. Most Probably it occurred due to incorrect PythonPath command.
correct command example:

(tensorflow1) C:\> set PYTHONPATH=C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim

(Note: Every time the "tensorflow1" virtual environment is exited, the PYTHONPATH variable is reset and needs to be set up again.)

I am stucked at this error, can anybody help me to solve this error.

ImportError: cannot import name 'flexible_grid_anchor_generator_pb2'

I had the same issue. My problem was that I forgot to compile the proto file flexible_grid_anchor_generator.proto .

I compiled the file (from the folder models/research) with :

(tensorflow1) C:\tensorflow1\models\research>protoc --python_out=. .\object_detection\protos\flexible_grid_anchor_generator.proto

and after that I run the scripts:

(tensorflow1) C:\tensorflow1\models\research>python setup.py build

and

(tensorflow1) C:\tensorflow1\models\research>python setup.py install

After these steps the error ImportError: cannot import name 'flexible_grid_anchor_generator_pb2' was gone

inside the "object_detection-0.1-py3.6.egg" directory is the "object_detection" folder. Change your directory in cmd line to that directory. "models/research/object_detection" and the "object_detection-0.1-py3.6.egg/object_detection" is the same.

Hi, can explain more how to do it ??

From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

^^This command only works on linux. On windows, you must compile each proto manually.

Something like:

protoc object_detection/protos/calibration.proto --python_out=. .\object_detection\protos\calibration.proto
protoc object_detection/protos/object_detection.proto --python_out=. .\object_detection\protos\object_detection.protos

and so on for every .proto file in that directory. there may be a way to shorten that command, but windows won't play nice with wildcard search.

Also, forget the "C:\ > set PYTHONPATH" command. Go to environment variables, create a new entry called PYTHONPATH, with the entry being the path to the relevant folders.

i.e.: C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim

From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

^^This command only works on linux. On windows, you must compile each proto manually.

Something like:

protoc object_detection/protos/calibration.proto --python_out=. .\object_detection\protos\calibration.proto
protoc object_detection/protos/object_detection.proto --python_out=. .\object_detection\protos\object_detection.protos

and so on for every .proto file in that directory. there may be a way to shorten that command, but windows won't play nice with wildcard search.

Also, forget the "C:\ > set PYTHONPATH" command. Go to environment variables, create a new entry called PYTHONPATH, with the entry being the path to the relevant folders.

i.e.: C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim

actually, i' have already compiled all proto files manually and tensorflow api is working right,
now i'm trying to train SSD mobilnet for a custom detection but python is crushing and i don't know for what reason , and this the error i get:

Windows fatal exception: access violation

Current thread 0x00000238 (most recent call first):
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 84 in _preread_check
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 122 in read
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\object_detection-0.1-py3.7.egg\object_detection\utils\config_util.py", line 99 in get_configs_from_pipeline_file
File "train.py", line 93 in main
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\tensorflow\python\util\deprecation.py", line 324 in new_func
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\absl\app.py", line 251 in _run_main
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\absl\app.py", line 300 in run
File "C:\Users\medali\Anaconda3\envs\gpu\lib\site-packages\tensorflow\python\platform\app.py", line 40 in run
File "train.py", line 184 in

after that message python stops . :/

That's not one I've seen before, but I would try 2 things:

  1. run anaconda as administrator
  2. use python without anaconda

From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

^^This command only works on linux. On windows, you must compile each proto manually.
Something like:
protoc object_detection/protos/calibration.proto --python_out=. .\object_detection\protos\calibration.proto
protoc object_detection/protos/object_detection.proto --python_out=. .\object_detection\protos\object_detection.protos

and so on for every .proto file in that directory. there may be a way to shorten that command, but windows won't play nice with wildcard search.
Also, forget the "C:\ > set PYTHONPATH" command. Go to environment variables, create a new entry called PYTHONPATH, with the entry being the path to the relevant folders.
i.e.: C:\tensorflow1\models;C:\tensorflow1\models\research;C:\tensorflow1\models\research\slim

This is absolutely correct.
I had to do the compilation again. I used the code given here
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html

What I did is -
changed directory to TensorFlow/models/research/

and then used the following cmd

for /f %i in ('dir /b object_detection\protos*.proto') do protoc object_detection\protos\%i --python_out=.

After that the error has gone for me.
Thanks @isademigod

1- open the terminal in model/research and copy this:
protoc --python_out=. .\object_detection\protos\anchor_generator.proto .\object_detection\protos\argmax_matcher.proto .\object_detection\protos\bipartite_matcher.proto .\object_detection\protos\box_coder.proto .\object_detection\protos\box_predictor.proto .\object_detection\protos\eval.proto .\object_detection\protos\faster_rcnn.proto .\object_detection\protos\faster_rcnn_box_coder.proto .\object_detection\protos\grid_anchor_generator.proto .\object_detection\protos\hyperparams.proto .\object_detection\protos\image_resizer.proto .\object_detection\protos\input_reader.proto .\object_detection\protos\losses.proto .\object_detection\protos\matcher.proto .\object_detection\protos\mean_stddev_box_coder.proto .\object_detection\protos\model.proto .\object_detection\protos\optimizer.proto .\object_detection\protos\pipeline.proto .\object_detection\protos\post_processing.proto .\object_detection\protos\preprocessor.proto .\object_detection\protos\region_similarity_calculator.proto .\object_detection\protos\square_box_coder.proto .\object_detection\protos\ssd.proto .\object_detection\protos\ssd_anchor_generator.proto .\object_detection\protos\string_int_label_map.proto .\object_detection\protos\train.proto .\object_detection\protos\keypoint_box_coder.proto .\object_detection\protos\multiscale_anchor_generator.proto .\object_detection\protos\graph_rewriter.proto .\object_detection\protos\calibration.proto .\object_detection\protos\flexible_grid_anchor_generator.proto .\object_detection\protos\center_net.proto .\object_detection\protos\fpn.proto

  • then press enter
    3- build the setup.py : python setup.py build then enter
    4- then install setup.py python setup.py install then enter
    and it will solved Inshallah
    assure that you oppend the terminal into model/research directory before running the commands

I'm getting this error now while running the train.py.
ImportError: cannot import name 'fpn_pb2'
any solution?

I'm getting this error now while running the train.py.
ImportError: cannot import name 'fpn_pb2'
any solution?

I have the same error. I'm pretty sure it's due to a failure during 'python setup.py install' command. I get the following error during the install.

Extracting object_detection-0.1-py3.5.egg to c:\users\mckay\anaconda3_64\envs\tensorflow\lib\site-packages
  File "c:\users\mckay\anaconda3_64\envs\tensorflow\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\utils\colab_utils.py", line 463
    boxes: List[np.ndarray] = box_storage_pointer
         ^
SyntaxError: invalid syntax

The setup script still finishes without notifying you of the error at the end, which may leave you to believe it completely successfully. so I'm uncertain if this error is negligible or if it's the cause of fpn_pb2 not being generated.

Any ideas anyone?

I'm getting this error now while running the train.py.
ImportError: cannot import name 'fpn_pb2'
any solution?

I have the same error. I'm pretty sure it's due to a failure during 'python setup.py install' command. I get the following error during the install.

Extracting object_detection-0.1-py3.5.egg to c:\users\mckay\anaconda3_64\envs\tensorflow\lib\site-packages
  File "c:\users\mckay\anaconda3_64\envs\tensorflow\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\utils\colab_utils.py", line 463
    boxes: List[np.ndarray] = box_storage_pointer
         ^
SyntaxError: invalid syntax

The setup script still finishes without notifying you of the error at the end, which may leave you to believe it completely successfully. so I'm uncertain if this error is negligible or if it's the cause of fpn_pb2 not being generated.

Any ideas anyone?

Okay. I've realized that the output from that script tells us that the generated files were placed in the wrong location. Namely, in site-packages. @hasanudoy We need to figure out why the generated files are going to the wrong location. Verify your PYTHONPATH. My python path is set correctly and it didn't resolve my issue. However, setting your python path as mentioned above may solve the problem for you.

Was this page helpful?
0 / 5 - 0 ratings