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

17,430

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.

Share:
17,430
cdahms
Author by

cdahms

Updated on June 07, 2022

Comments

  • cdahms
    cdahms almost 2 years

    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:\Users\cdahms\Documents\models
    

    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 Files\protoc-3.4.0-win32"
    

    6) CD into the models\research directory, specifically

    cd C:\Users\cdahms\Documents\models\research
    

    7) Executed the protobuf compile, specifically

    “C:\Program Files\protoc-3.4.0-win32\bin\protoc.exe” object_detection/protos/*.proto --python_out=.
    

    Navigate to:

    C:\Users\cdahms\Documents\models\research\object_detection\protos
    

    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:\Users\cdahms\Documents\models\research\object_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:\Users\cdahms\Documents\models\research\object_detection\object_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