ModuleNotFoundError: No module named 'object_detection'

Question:

i try to train.py in object_detection in under git url

https://github.com/tensorflow/models/tree/master/research/object_detection

However, the following error occurs.

ModuleNotFoundError: No module named ‘object_detection’

So I tried to solve the problem by writing the following code.

import sys

sys.path.append('/home/user/Documents/imgmlreport/inception/models/research/object_detection')
from object_detection.builders import dataset_builder

This problem has not been solved yet.

The directory structure is shown below.

~/object_detection/train.py

~/object_detection/builders/dataset_bulider.py

and here is full error massage

/home/user/anaconda3/lib/python3.6/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 “train.py”, line 52, in
import trainer

File”/home/user/Documents/imgmlreport/inception/models/research/object_detection/trainer.py”, line 26, in
from object_detection.builders import optimizer_builder

ModuleNotFoundError: No module named ‘object_detection’

how can i import modules?

Asked By: 송준석

||

Answers:

There are a number of modules in the object_detection folder, and I have created setup.py in the parent directory(research folder) to import all of them.

from setuptools import find_packages
from setuptools import setup


REQUIRED_PACKAGES = ['Pillow>=1.0', 'Matplotlib>=2.1', 'Cython>=0.28.1']

setup(
    name='object_detection',
    version='0.1',
    install_requires=REQUIRED_PACKAGES,
    include_package_data=True,
    packages=[p for p in find_packages() if p.startswith('object_detection')],
    description='Tensorflow Object Detection Library',
)
Answered By: 송준석

You need to export the environmental variables every time you open a new terminal in that environment.

Please note that there are are back quotes on each of the pwd in the command as this might not be showing in the command below. Back quote is the same as the tilde key without pressing the shift key (US keyboard).

From tensorflow/models/research/

export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Answered By: juhagat

Cause of this error is installing object_detection library, So one of the solution which can work is running the below command inside models/research


    sudo python setup.py install

If such solution does not work, please execute the below command one by one in the directory models/research


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

I hope this will work. I also faced the same problem while creating model from export_inference_graph.py. It worked for me.

Answered By: Vijay

try this:
python setup.py build
python setup.py install

Answered By: Roco Fernandez

Try install Tensorflow Object Detection Library Packaged

pip install tensorflow-object-detection-api
Answered By: bedna

You did have “sys.path.append()” before you imported the object detection, so I am surprised that you are facing this error!

Please check that the path you have used in sys.path.append() is right.

Well, the only and obvious answer for the error is that the path of the module is not added properly.

Besides the various ways mentioned here, here is a way in which you can add the “object_detection” path permanently to the PYTHONPATH variable.

If you are using a Linux system, here is how you would go about it:

Go to the Home directory. Press Ctrl + H to show hidden files. You will see a file called “.bashrc”. Open this file using a code editor (I used Visual Studio).

In the last line of .bashrc file, add the line:

export PYTHONPATH=/your/module/path:/your/other/module/path:your/someother/module/path

Then press “save” in the code editor. Since “.bashrc” is a “Read-only” file the editor will throw a pop-up saying the same. Also in the pop-up there will be an option that says: “Try with sudo”. Hit this button and now you are good to go.

All your modules are now permanently added to the PYTHONPATH. This means that you need not run sys.path.append every time you open your terminal and start a session!

Below is the screenshot with no error when I followed the said steps:

enter image description here

Try this. I hope it helps.

Answered By: Sushanth

I had to do:
sudo pip3 install -e . (ref)
sudo python3 setup.py install

System:
OS: Ubuntu 16.04, Anaconda (I guess this is why I need to use pip3 and python3 even I made virtual environment with Pyehon 3.8)

Answered By: Cloud Cho

And finally, If you’ve followed all the steps here and are at your wit’s end…make sure the file that you’re running (the one with your source code in it ya know), isn’t named object_detection.py – that would preclude it being searched for as a module.

Certainly I’ve never done anything like this that led me to add an embarrassing answer on Stack Overflow…

Answered By: Darren Hicks
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.