Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

python - Tensorflow Object Detection API on Windows - error "ModuleNotFoundError: No module named 'utils'"

I'm attempting to get the TensorFlow Object Detection API

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

working on Windows by following the install instructions

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

Which seem to be for Linux/Mac. I can only get this to work if I put a script in the directory I cloned the above repo to. If I put the script in any other directory I get this error:

ModuleNotFoundError: No module named 'utils'

I suspect that the cause is not properly doing the Windows equivalent of this command listed on the install instructions above:

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

I'm using Windows 10, Python 3.6, and TensorFlow 1.4.0 if that matters. Of course, I've Googled on this concern and found various links, for example, this:

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

But this has not resolved the concern. Any suggestions on how to resolve this?

Here are the steps I've done so far specifically:


EDIT: these steps work now after updating to incorporate RecencyEffect's answer

1) Install TensorFlow and related tools via pip3

2) From an administrative command prompt, run the following:

pip3 install pillow
pip3 install lxml
pip3 install jupyter
pip3 install matplotlib

3) Clone the TensorFlow "models" repository to the Documents folder, in my case

C:UserscdahmsDocumentsmodels

4) Downloaded Google Protobuf https://github.com/google/protobuf Windows v3.4.0 release "protoc-3.4.0-win32.zip" (I tried the most current 3.5.1 and got errors on the subsequent steps, so I tried 3.4.0 per this vid https://www.youtube.com/watch?v=COlbP62-B-U&list=PLQVvvaa0QuDcNK5GeCQnxYnSSaar2tpku&index=1 and the protobuf compile worked)

5) Extract the Protobuf download to Program Files, specifically

"C:Program Filesprotoc-3.4.0-win32"

6) CD into the models esearch directory, specifically

cd C:UserscdahmsDocumentsmodels
esearch

7) Executed the protobuf compile, specifically

“C:Program Filesprotoc-3.4.0-win32inprotoc.exe” object_detection/protos/*.proto --python_out=.

Navigate to:

C:UserscdahmsDocumentsmodels
esearchobject_detectionprotos

and verify the .py files were created successfully as a result of the compile (only the .proto files were there to begin with)

8) cd to the object_detection directory, ex:

cd C:UserscdahmsDocumentsmodels
esearchobject_detection

then enter the following at a command prompt to start the object_detection_tutorial.ipynb Jupyter Notebook

jupyter notebook

9) In the Jupyter Notebook, choose "object_detection_tutorial.ipynb" -> Cell -> Run all, the example should run within the notebook

10) In the Jupyter Notebook, choose “File” -> “Download As” -> “Python”, and save the .py version of the notebook to the same directory, i.e.

C:UserscdahmsDocumentsmodels
esearchobject_detectionobject_detection_tutorial.py

You can now open the script in your chosen Python editor (ex. PyCharm) and run it.


EDIT per RecencyEffect's answer below, if you follow these additional steps you will be able to run the object_detection_tutorial.py script from any directory

11) Move the script to any other directory, then attempt to run it and you will find you will get the error:

ModuleNotFoundError: No module named 'utils'

because we have not yet informed Python how to find the utils directory that these lines use:

from utils import label_map_util
from utils import visualization_utils as vis_util

To resolve this . . .

12) Go to System -> Advanced system settings -> Environment Variables . . . -> New, and add a variable with the name PYTHONPATH and these values:

enter image description here

13) Also under Environment Variables, edit PATH and add %PYTHONPATH% like so:

enter image description here

14) Reboot to make sure these path changes take effect

15) Pull up a command prompt and run the command "set", verify PYTHONPATH is there and PYTHONPATH and PATH contained the values from the previous steps.

16) Now you can copy the "object_detection_tutorial.py" to any other directory and it will run

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As mentioned in the comment, utils is a submodule so you actually need to add object_detection to PYTHONPATH, not object_detection/utils.

I'm glad it worked for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...