Models: cannot import name 'anchor_generator_pb2'

Created on 14 Jul 2017  Â·  30Comments  Â·  Source: tensorflow/models

I have tensorflow-gpu installed in a container. I downloaded models in the tensorflow directory and installed protos 3.3. i tried to execute the last command in the installation section
python object_detection/builders/model_builder_test.py
but it gives me the following error:
"ImportError: cannot import name 'anchor_generator_pb2' "
how to fix this?

Most helpful comment

I ran into the same error.

Steps to resolve:

  1. Read the Installation Instruction. here
  2. Before executing model_builder_test.py, you need to compile proto files as following:
# From tensorflow/models/
protoc object_detection/protos/*.proto --python_out=.

All 30 comments

u need to transform xxx.proto file into xxx_pb2.py file by using"protoc -I=./ --python_out=./ xxx.proto".
But some of the "xxx.proto" file here also have some trouble, u can try this way at first.

i tried that, it gives me the following error even when i use the absolute path
anchor_generator.proto: Import "/usr/local/lib/python3.4/dist-packages/tensorflow/models/object_detection/protos/grid_anchor_generator.proto" was not found or had errors.

anchor_generator.proto: Import "/usr/local/lib/python3.4/dist-packages/tensorflow/models/object_detection/protos/ssd_anchor_generator.proto" was not found or had errors.

anchor_generator.proto:15:5: "GridAnchorGenerator" is not defined.

anchor_generator.proto:16:5: "SsdAnchorGenerator" is not defined.

because all the proto files in ./object_detection/protos should be transformed into python files, i didn't do that successfully yet.

I followed the commands given in the installation guide using tensorflow 1.2, and it worked. I did not find a way to make it work with tensorflow 1.0 though.

The path of grid_anchor_generator.proto and ssd_anchor_generator.proto when you import them in anchor_generator.proto may not be correct. If they are in the same folder, try deleting "object_detection/protos" when you import them. I tried and it worked.

locate at dir : /models/
run in command:
protoc -I=./ --python_out=./ .\object_detection\protos\*.proto

all of mine:
(C:\Program Files\Anaconda3) C:\Program Files\Anaconda3\Lib\site-packages\tensor
flow\models>protoc -I=./ --python_out=./ .\object_detection\protos\*.proto

Could you show me what's in the import section of anchor_generator.proto file? Thanks.

The original one is:
//import "object_detection/protos/grid_anchor_generator.proto";
//import "object_detection/protos/ssd_anchor_generator.proto";

And I changed it to:
import "grid_anchor_generator.proto";
import "ssd_anchor_generator.proto";

I ran into the same error.

Steps to resolve:

  1. Read the Installation Instruction. here
  2. Before executing model_builder_test.py, you need to compile proto files as following:
# From tensorflow/models/
protoc object_detection/protos/*.proto --python_out=.

I have open file ‘anchor_generator.proto’ and find the reason。
in line 5
import "object_detection/protos/grid_anchor_generator.proto";
so ,we should run command "protoc .proto --python_out=." in the parent path of"object_detection " ,just like this "protoc object_detection/protos/.proto --python_out=."

in object_detection protos folder the import line is given
import "object_detection/protos/grid_anchor_generator.proto";

change that to
import "research/object_detection/protos/grid_anchor_generator.proto";

Not a single "solution" has worked so far. Getting tired of trying everything without prevail. This takes way too much time!

Dowloaded protobuf v3.4 for windows: https://github.com/google/protobuf/releases/tag/v3.4.0
Placed protoc.exe in "research" folder
Within "research" folder ran:

protoc object_detection/protos/.proto --python_out=.
gives no error

Run python object_detection/builders/model_builder_test.py:

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

anchor_generator_pb2.py file is located in object_detection/protos

Have tried @navinkeshava's recommendation. Have tried different protobuf versions....
Nothing works!!
Please help!!!!

Add the following code instead of the line... from object-detection.protos import anchor_generator_pb2
//
import sys
sys.path.insert(0,"C:/Users/user/AppData/Local/Programs/Python/Python36/models/research/object_detection/protos")
import anchor_generator_pb2
//

Just change the path to the respective location on your computer

Hi lekshmi17,

thank you for your reply. Unfortunately, that does not change one bit. The solution you propose does not feel quite robust to be honest. Changing all relative paths in all files makes things so messy.

This is what I get after modifying anchor_generator_builder.py according to your suggestion. I added the path but nothing changed. So I changed the path with os.chdir() to .C:/.../research/models/object_detection/protos

>
C:\PythonEnv\WinPython\3.6.1.0\tensorflow\models\research>python object_detection/builders/model_builder_test.py
C:\PythonEnv\WinPython\3.6.1.0\python-3.6.1.amd64\lib\site-packages\h5py__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 21, in
from object_detection.builders import model_builder
File "C:\PythonEnv\WinPython\3.6.1.0\python-3.6.1.amd64\lib\site-packages\slim-0.1-py3.6.egg\object_detection\builders\model_builder.py", line 17, in
from object_detection.builders import anchor_generator_builder
File "C:\PythonEnv\WinPython\3.6.1.0\python-3.6.1.amd64\lib\site-packages\slim-0.1-py3.6.egg\object_detection\builders\anchor_generator_builder.py", line 30, in
import anchor_generator_pb2
ModuleNotFoundError: No module named 'anchor_generator_pb2'

Can you make sense of this?

Much appreciated for your time and effort!

Does anyone know if this works on windows at all? I'm stuck at the same place.

Just in case this might help someone, I did compile all the proto files, while installing. The problem was I was trying to run the code in a new shell and I forgot to export the new PYTHONPATH (On Ubuntu 16.04):

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

Running the above command before running python ... helped.

Also wondering if it's less trouble to install Ubuntu than to try to make this work on Windows :) Stuck in the same place as well.

Edit: I copied the protoc made .py files from my original git cloned models\research\object_detection\protos to Anaconda3\envs\tensorflow\Lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\protos and after that I could run model_builder_test.py successfully.

Win10, Anaconda3, py3.5

Honestly I’ve spent many days going down endless rabbit holes as I continue to try and get this running on Ubuntu. I don’t understand how anyone gets anything to work on Linux. I know millions of people do, but I’m finding myself blindly fumbling all over the internet. Deep Learning seems super simple compared to building Tensorflow from source.

Sent from my iPhone

On Apr 11, 2018, at 6:19 AM, Aki Kutvonen notifications@github.com wrote:

Also wondering if it's less trouble to install Ubuntu than to try to make this work on Windows :) Stuck in the same place as well.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

If you are getting errors as follows when trying to compile with protoc

object_detection/protos/grid_anchor_generator.proto: File not found. object_detection/protos/ssd_anchor_generator.proto: File not found. object_detection/protos/multiscale_anchor_generator.proto: File not found. anchor_generator.proto: Import "object_detection/protos/grid_anchor_generator.proto" was not found or had errors. anchor_generator.proto: Import "object_detection/protos/ssd_anchor_generator.proto" was not found or had errors. anchor_generator.proto: Import "object_detection/protos/multiscale_anchor_generator.proto" was not found or had errors. anchor_generator.proto:13:5: "GridAnchorGenerator" is not defined. anchor_generator.proto:14:5: "SsdAnchorGenerator" is not defined. anchor_generator.proto:15:5: "MultiscaleAnchorGenerator" is not defined.

Turns out the .proto files are trying to import other .proto files found in the same folder. I went through all the .proto files and changed any imports like "import "object_detection/protos/grid_anchor_generator.proto";
to "grid_anchor_generator.proto". I did this to all proto files in the object_detection/protos folder leaving only the filename instead of the full path.

Then I ran
"C:\Program Files\protoc\bin\protoc" *.proto --python_out=.
within the object_detection/protos directory. Once it compile without any error messages and you can see all the *_pb2.py files in the protos folder it should run just fine.
Note: I had placed the protoc exe in my Program Files manually, you might have to change the path to where you placed your protoc.exe file.

If you're in linux 14, first follow this thread and add protoc3_3 to you tensorflow

https://github.com/tensorflow/models/issues/1834

and then if you're still getting the error run something like

/home/ebrahim/tensorflow/protoc_3.3/bin/protoc -I=./ --python_out=./ .\object_detection\protos*.proto

I fixed this issue on WINDOWS by running
python setup.py install in 'tensorflow\models\research' BEFORE to run python object_detection/builders/model_builder_test.py.
In that way all py files are copied from 'tensorflow\models\research' to 'C:\Users\user\AppData\Local\Programs\Python\Python36\Lib\site-packages\object_detection-0.1-py3.6.egg\object_detection'

it's working for me:
you should be on the Tensorflow/models/research to execute, like this:

image

hope can help yous.

This worked for me. From the path: models/research, executed following:

protoc -I=./ --python_out=./ ./object_detection/protos/*.proto
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
# Test
python object_detection/builders/model_builder_test.py

I'm using TF 1.9.0 on Ubuntu 18.04

"python setup.py install in 'tensorflow\models\research' BEFORE to run python object_detection/builders/model_builder_test.py. " WORKS TO ME! thanks @neomarian

Thanks @mangalbhaskar, that worked for me :+1:

This worked for me. From the path: models/research, executed following:

protoc -I=./ --python_out=./ ./object_detection/protos/*.proto
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
# Test
python object_detection/builders/model_builder_test.py

I'm using TF 1.9.0 on Ubuntu 18.04

This helped. I was trying to run it on an ARM. Thanks a lot @mangalbhaskar

I fixed this issue on WINDOWS by running
python setup.py install in 'tensorflow\models\research' BEFORE to run python object_detection/builders/model_builder_test.py.
In that way all py files are copied from 'tensorflow\models\research' to 'C:\Users\user\AppData\Local\Programs\Python\Python36\Lib\site-packages\object_detection-0.1-py3.6.egg\object_detection'

It works for me. Thank you so much!

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

From tensorflow/models/research/

export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim

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

and verify below video
https://youtu.be/nZUxoHPFf4

If its windows, try adding below paths in PYTHONPATH
\research;\research\slim;\research\object_detection

That worked out for me.

Can I not just download the .py files instead of going through this?

@mangalbhaskar Your solution worked for me. Thanks for that

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hanzy123 picture hanzy123  Â·  3Comments

noumanriazkhan picture noumanriazkhan  Â·  3Comments

dsindex picture dsindex  Â·  3Comments

frankkloster picture frankkloster  Â·  3Comments

sun9700 picture sun9700  Â·  3Comments