Models: No module named 'object_detection'

Created on 25 Jul 2017  ·  48Comments  ·  Source: tensorflow/models

System information

  • What is the top-level directory of the model you are using: Nothing yet
  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Nope
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 64bit
  • TensorFlow installed from (source or binary): pip install tensorflow-gpu
  • TensorFlow version (use command below):

b'unknown' 1.2.0

  • Bazel version (if compiling from source):
  • CUDA/cuDNN version:
    8.0 and 5
  • GPU model and memory:
    Geforce GTX 1050ti 4gb ddr5
  • Exact command to reproduce:
    python object_detection/builders/model_builder_test.py

Describe the problem

I followed the README.MD up until the point of having to execute the above command. And when I do I get an error saying that there is no module named object_detection I believe this has something to do with the

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

command. Because when I look at my system environment variables, I don't see anything titled PYTHONPATH.

I'm using Anaconda with a Virtual Environment

awaiting model gardener

Most helpful comment

I also had to go through all this using win10 x64 and summary for the steps that worked from this thread:

  1. in modelsresearch directory run the following:
    python setup.py build python setup.py install
  2. go to model/research/slim and run the following:
    pip install -e .

Am using Anaconda virtual environment (Tensorflow-GPU)

All 48 comments

@jch1 can you look at this, or should it be forwarded to someone who works on Windows support?

Try running pwd on its own; pwd doesn't come with Windows by default, so you may not have it. If you don't have pwd, I recommend defining the environment variable manually. This is what I do; you'll have to adapt the paths to your computer:

set PYTHONPATH=<PATH_TO_MODELS>;<PATH_TO_MODELS>\slim

Also, this should be posted to Stack Overflow.

Well @MaxBareiss I was able to setup pwd correctly using Git Bash on windows
I originally typed source activate py35cv3

I don't have a PYTHONPATH but I do have a PYTHON_HOME So what I did is

echo $PYTHON_HOME
C:\Users\DanMo\Anaconda3
export PYTHON_HOME=$PYTHON_HOME:`pwd`:`pwd`/slim



md5-7d4249cb935db16e9ab54f33e5476134



echo $PYTHON_HOME
C:\Users\DanMo\Anaconda3:/c/Users/DanMo/Anaconda3/envs/py35cv3/lib/site-packages/tensorflow/models:/c/Users/DanMo/Anaconda3/envs/py35cv3/lib/site-packages/tensorflow/models/slim



md5-7d4249cb935db16e9ab54f33e5476134



DanMo@DESKTOP-V1R6HEC MINGW64 ~/Anaconda3/envs/py35cv3/lib/site-packages/tensorflow/models (master)
$ python object_detection/builders/model_builder_test.py
Traceback (most recent call last):
  File "object_detection/builders/model_builder_test.py", line 21, in <module>
    from object_detection.builders import model_builder
ImportError: No module named 'object_detection'

I still get that error

So I didn't realize I also needed to also run

$ python setup.py build
$ python setup.py install

Is there a reason this isn't mentioned in the README?

But now I get the error

$ python object_detection/builders/model_builder_test.py
Traceback (most recent call last):
  File "object_detection/builders/model_builder_test.py", line 21, in <module>
    from object_detection.builders import model_builder
  File "C:\Users\DanMo\Anaconda3\envs\py35cv3\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\builders\model_builder.py", line 30, in <module>
    from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
  File "C:\Users\DanMo\Anaconda3\envs\py35cv3\lib\site-packages\object_detection-0.1-py3.5.egg\object_detection\models\faster_rcnn_inception_resnet_v2_feature_extractor.py", line 29, in <module>
ImportError: No module named 'nets'

Could you print the output of echo $PYTHONPATH?

(py35cv3) C:UsersDanMoAnaconda3envspy35cv3Libsite-packagestensorflowmodels>echo %PYTHONPATH%
"C:UsersDanMoAnaconda3envspy35cv3Libsite-packagestensorflowmodelsslim";"C:UsersDanMoAnaconda3envspy35cv3Libsite-packagestensorflowmodels"

Hi there, I am facing the same issue. I solved it by changing nets to slim.nets. This removed the errors for me. I am using Pycharm and didn't set any environment variables. I added the models and the slim directory as sources root in the project structure. Give it a try and see if it works!

@hari0920 Where did you change nets to slim.nets?

under the models folder in the faster_rcnn_inception_resnet_v2_feature_extractor.py and faster_rcnn_resnet_v1_feature_extractor.py

Still get the same error

(py35cv3) C:UsersDanMoAnaconda3envspy35cv3Libsite-packagestensorflowmodels>python object_detection/builders/model_builder_test.py
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:UsersDanMoAnaconda3envspy35cv3Libsite-packagestensorflowmodelsobject_detectionbuildersmodel_builder.py", line 30, in
from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
File "C:UsersDanMoAnaconda3envspy35cv3Libsite-packagestensorflowmodelsobject_detectionmodelsfaster_rcnn_inception_resnet_v2_feature_extractor.py", line 30, in
slim = tf.contrib.slim.nets
AttributeError: module 'tensorflow.contrib.slim' has no attribute 'nets'

DanMossa, what did you find out?

@roblee357 I honestly have no idea. I didn't try it for a few days, and then I just randomly tried it and it all worked lol

@DanMossa which solution did you use to fix that error message?

Using windows the following worked for me:
Set PYTHONPATH to both models and modelsslim and then add PYTHONPATH
to your Path variable

@jamiii Your solution works for me in Windows 10. But I do not add PYTHONPATH to PATH, it still works.

Nothing in this thread has worked for me. I still get the "ImportError: No module named 'nets'" error.
I went inside "slim" and ran python setup.py install (because thats where he 'nets' folder is).
Then ran "python object_detection/builders/model_builder_test.py" again.
Now I get the error ImportError: No module named 'nets.nasnet'

py object_detection/builders/model_builder_test.py
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:UsersNissanAppDataLocalProgramsPythonPython35libsite-packagesobject_detection-0.1-py3.5.eggobject_detectionbuildersmodel_builder.py", line 32, in
from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_nas
File "C:UsersNissanAppDataLocalProgramsPythonPython35libsite-packagesobject_detection-0.1-py3.5.eggobject_detectionmodelsfaster_rcnn_nas_feature_extractor.py", line 26, in
ImportError: No module named 'nets.nasnet'

Hi @karkinissan. I changed nets to research.slim.nets and it works for me now.

For mac os sierra steps to install. Run from models/research:

sudo chown -R $USER /Library/Frameworks/Python.framework/Versions/3.6/
python setup.py build
python setup.py install

If you use jupyter notebook you need to restart it.

I fixed it
Go to slim and run this

sudo pip install -e .

this will install slim which allows you to use import nets

@crizzy9 Can you please tell which slim? I found 2 in models

@cyberphantom there's one in site packages and the other is in models/research. Choose the second one.

I also had to go through all this using win10 x64 and summary for the steps that worked from this thread:

  1. in modelsresearch directory run the following:
    python setup.py build python setup.py install
  2. go to model/research/slim and run the following:
    pip install -e .

Am using Anaconda virtual environment (Tensorflow-GPU)

I am so happy to modified the problem as the method of @hari0920 and @deaspo . I solved the problem through the steps:
1 add the path of slim and the path of research into the environment variable list
2 in modelsresearch directory run the following:
python setup.py build
python setup.py install
3 change nets to slim.nets in the models folder in the faster_rcnn_inception_resnet_v2_feature_extractor.py and faster_rcnn_resnet_v1_feature_extractor.py
4 restart Anaconda and test “model_builder_test.py”

One possible reason for this might be that you are not running the Jupyter Notebook in the same terminal where you set the $PYTHONPATH value.

Or you can try to add $PYTHONPATH value into your ~/.bashrc file (or ~/.zshrc file if you are using 'oh my zsh')

@deaspo 's answer perfectly clear and worked, it should be published. If you use python 3 just use pip3 instead pip

This is what worked for me in windows 10. All the issue is the pythonpath, so besides pointing it to you pythonxx folder also add from the command prompt the following:

set PythonPath=%pythonpath%;c:mlmodels;c:mlmodelsresearch;c:mlmodelsresearchslim

@jamiii works for me .thanks

works for me!!! Thanks!

import os import cv2 import time import argparse import multiprocessing import numpy as np import tensorflow as tf from matplotlib import pyplot as plt %matplotlib inline from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util

I am using anaconda jupyter on ubuntu 16.04lts

This is my error


ModuleNotFoundError Traceback (most recent call last)
in ()
----> 1 from object_detection.utils import label_map_util
2 from object_detection.utils import visualization_utils as vis_util

ModuleNotFoundError: No module named 'object_detection'

I am getting the same error . When running from python shell

yes i also get this error

On Mon, Jul 30, 2018 at 3:59 PM sanket19189 notifications@github.com
wrote:

I am getting the same error . When running from python shell


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/tensorflow/models/issues/2031#issuecomment-408815462,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMyCUtIpEc_SKpQEEUkUWiV57yCVOJhQks5uLtyjgaJpZM4Oh9HQ
.

Hi, I did the following:

After instructions from deaspo and @SophieApple I moved the complete Archive "nets" and "slim" my home directory in virtualenv.

image

I´m using Anaconda, with Tensorflow 1.10 and windows10. It seems that in TF10 they changed the directores completaly..

faster_rcnn_inception_resnet_v2_feature_extractor.py and faster_rcnn_resnet_v1_feature_extractor.py

4 restart Anaconda and test “model_builder_test.py”

can you clarify what you mean by restarting Anaconda? Do you mean the Anaconda application that is downloaded with the install?

If you are using Anaconda, here is my updated answer. It works for both Linux and Windows.

Hello @DanMossa , I was able to figure out the problem as object_detection library is not installed so please run below command inside the directory models/research

sudo python setup.py install

I hope, this will solve the problem. If such solution does not work, then please execute the below command one by one inside the directory models/research

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
sudo python setup.py install

I still get that error
This will resolve the error. I too faced such error while creating model from export_inference_graph.py. I followed the above procedure, error was gone. I hope this solution may be a help to you.

With Regards
AI Sangam

For Linux:
--> cd inside models/research
Run following commands
--> export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim
-->protoc object_detection/protos/*.proto --python_out=.
-->python setup.py build
-->python setup.py install

I just did as you guys say:

$ python setup.py build
$ python setup.py install

then I run
python object_detection/builders/model_builder_test.py
I got this error:

Traceback (most recent call last):
  File "object_detection/builders/model_builder_test.py", line 23, in <module>
    from object_detection.builders import model_builder
  File "C:\Users\wujinpeng\venv\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\builders\model_builder.py", line 28, in <module>
    from object_detection.builders import region_similarity_calculator_builder as sim_calc
  File "C:\Users\wujinpeng\venv\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\builders\region_similarity_calculator_builder.py", line 19, in <module>
    from object_detection.protos import region_similarity_calculator_pb2
ImportError: cannot import name 'region_similarity_calculator_pb2'

$ python setup.py build
running build
running build_py
creating build/lib
error: could not create 'build/lib': Permission denied

getting this error while executing the setup.py build

$ python setup.py install
running install
running bdist_egg
running egg_info
error: [Errno 13] Permission denied

getting this error while executing the setup.py install

$ python setup.py build
running build
running build_py
creating build/lib
error: could not create 'build/lib': Permission denied

getting this error while executing the setup.py build

Please run the file with sudo permission

This is ridiculous and confusing.

The wiki section is completely empty.
There is zero documentation.

I also had to go through all this using win10 x64 and summary for the steps that worked from this thread:

  1. in modelsresearch directory run the following:
    python setup.py build
    python setup.py install

  2. go to model/research/slim and run the following:
    pip install -e .

Am using Anaconda virtual environment (Tensorflow-GPU)

Just in case this helps anyone, I wanted to confirm that these exact steps worked for me. I'm on macOS 10.15 Beta (Build 19A471t) using Tensorflow CPU.

  • Go to models/research directory
  • Run following commands
python setup.py build
python setup.py install
  • If you are using Anaconda, run these also in same directory
conda develop .
conda develop slim/
  • DONE
    Hope it helps

### pip install tensorflow-object-detection-api

### pip install tensorflow-object-detection-api

This one worked for me.

python setup.py build
python setup.py install

it worked for me, thanks.

For mac os sierra steps to install. Run from models/research:

sudo chown -R $USER /Library/Frameworks/Python.framework/Versions/3.6/
python setup.py build
python setup.py install

If you use jupyter notebook you need to restart it.

Also for macos Catalina fixed the problem. Thanks a lot

Hi, I am getting the following errors.. when I run the main training program..
(tensorflow) C:tensorflowmodelsresearchobject_detection>python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config
Traceback (most recent call last):
File "train.py", line 54, in
from object_detection.builders import model_builder
File "C:tensorflowmodelsresearchobject_detectionbuildersmodel_builder.py", line 20, in
from object_detection.builders import anchor_generator_builder
File "C:tensorflowmodelsresearchobject_detectionbuildersanchor_generator_builder.py", line 23, in
from object_detection.anchor_generators import flexible_grid_anchor_generator
ModuleNotFoundError: No module named 'object_detection.anchor_generators'

Was this page helpful?
0 / 5 - 0 ratings