hafiz031
hafiz031

Reputation: 2680

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?

Upvotes: 2

Views: 4103

Answers (3)

Ronald M Rodriques
Ronald M Rodriques

Reputation: 1

I just cloned the repository from github and Rerun the code cell where the ModuleNotFoundError occured. Reason:It looks for the file within the specific package,that i cloned,and if not found throws the error.

Upvotes: -2

hafiz031
hafiz031

Reputation: 2680

Alright! I managed to solve it using the following way in Colab. If you think that all the required packages are already installed and are ready to use properly then start from point number 4:

  1. Install model using the following command:

    !git clone --depth 1 https://github.com/tensorflow/models

  2. Also install the following packages in the same directory:

    !apt-get install -qq protobuf-compiler python-pil python-lxml python-tk

    !pip install -q Cython contextlib2 pillow lxml matplotlib

    !pip install -q pycocotools

  3. Now go to research folder to compile .proto files. To do this first go to the research folder by running the following command:

    %cd /content/models/research

    And now compile the .proto files:

    !protoc object_detection/protos/*.proto --python_out=.

  4. Now add the python-path executing the following code:

    import os

    os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'

  5. If you face problem regarding tf-slim than also install the following package:

    !pip install git+https://github.com/google-research/tf-slim

Done!

NB:

  • I have found this notebook helpful to solve the problem.
  • I am working with tensorflow 1.x which is basically tensorflow 1.15.2 provided by Colab.

Upvotes: 2

Harut Hunanyan
Harut Hunanyan

Reputation: 178

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.

Upvotes: 2

Related Questions