models icon indicating copy to clipboard operation
models copied to clipboard

cannot import name 'anchor_generator_pb2'

Open fib5813 opened this issue 9 years ago • 35 comments

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?

fib5813 avatar Jul 14 '17 21:07 fib5813

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.

Jennyidream0318 avatar Jul 17 '17 06:07 Jennyidream0318

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.

fib5813 avatar Jul 17 '17 15:07 fib5813

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

Jennyidream0318 avatar Jul 18 '17 00:07 Jennyidream0318

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.

fib5813 avatar Jul 18 '17 13:07 fib5813

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.

louiseruiwenli avatar Aug 04 '17 07:08 louiseruiwenli

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";

louiseruiwenli avatar Aug 17 '17 02:08 louiseruiwenli

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=.

datlife avatar Aug 26 '17 19:08 datlife

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=."

mickeybao avatar Sep 05 '17 10:09 mickeybao

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";

navinkeshava avatar Oct 15 '17 16:10 navinkeshava

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!!!!

ghost avatar Mar 29 '18 16:03 ghost

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

lekshmi17 avatar Mar 31 '18 10:03 lekshmi17

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!

ghost avatar Mar 31 '18 19:03 ghost

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

kpierce8 avatar Apr 06 '18 00:04 kpierce8

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.

blumonkey avatar Apr 07 '18 16:04 blumonkey

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

kutvonenaki avatar Apr 11 '18 13:04 kutvonenaki

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 [email protected] 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.

kpierce8 avatar Apr 11 '18 14:04 kpierce8

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.

akapila011 avatar May 05 '18 13:05 akapila011

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

ebimor avatar May 08 '18 14:05 ebimor

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'

neomarian avatar May 16 '18 21:05 neomarian

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

image

hope can help yous.

Paulswith avatar Jun 22 '18 07:06 Paulswith