Search code examples
pythontensorflowgoogle-colaboratorypythonpath

ModuleNotFoundError: No module named 'nets' on Google Colab


I am trying to run TensorFlow Object Detection API on Google Colab to train SSD-Mobilenet model on a custom dataset. But I am facing this NoModuleError. It is not finding the module 'nets'. I have already found people facing similar problem although they are not running the trining in Google Colab. Following are some of the links:

ImportError: No module named 'nets'

ModuleNotFoundError: No module named 'nets' (TensorFlow)

Everywhere above I found the suggestion of adding PYTHONPATH of slim and research folders and I did them all. Following are the paths I have already added:

! echo $PYTHONPATH

import os
os.environ['PYTHONPATH'] += ":/models"
os.environ['PYTHONPATH'] += ":/models/research"
os.environ['PYTHONPATH'] += ":/models/research/slim"
# I copied the `nets` folder inside models folder and 
# additionally here adding this folder to python path such that it becomes available to `faster_rcnn_inception_resnet_v2_feature_extractor.py` file for importing.
os.environ['PYTHONPATH'] += ":/models/nets" 

! echo $PYTHONPATH

%cd '/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/models/research/'
!python setup.py build
!python setup.py install
%cd '/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD'

But still getting this error. Following is the error I am getting on Colab:

Traceback (most recent call last):
  File "training/train.py", line 26, in <module>
    from object_detection import model_lib
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/model_lib.py", line 28, in <module>
    from object_detection import exporter as exporter_lib
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/exporter.py", line 23, in <module>
    from object_detection.builders import model_builder
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/builders/model_builder.py", line 59, in <module>
    from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor.py", line 30, in <module>
    from nets import inception_resnet_v2
ModuleNotFoundError: No module named 'nets'

As I have noticed the error producing line is from nets import inception_resnet_v2 of the file faster_rcnn_inception_resnet_v2_feature_extractor.py. Hence, I additionally copied the nets folder inside it's scope such that it can find the module. But it is still saying the same although now there should be no point of not finding this module. What else probably went wrong here?


Solution

  • I had the same error, but I found a probable solution. You need to run the code above at slim directory.

    %cd drive/My\ Drive/<path to slim>/slim
    
    !python setup.py build
    !python setup.py install
    

    This code runs setup.py for slim, and in fact it sets all the modules needed.

    You also may need to add path to slim to your environment variable.

    os.environ['PYTHONPATH'] = '/env/python/drive/My Drive/slim'
    

    Or

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

    Here are links that were useful for me.

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

    https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/150

    Hope this will help.