ModuleNotFoundError: No module named 'nets' on Google Colab - python

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?

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.

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:
Install model using the following command:
!git clone --depth 1 https://github.com/tensorflow/models
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
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=.
Now add the python-path executing the following code:
import os
os.environ['PYTHONPATH'] += ':/content/models/research/:/content/models/research/slim/'
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.

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.

Related

error: cannot import name 'model_lib_v2' from 'object_detection' [duplicate]

I'm trying to train TF2 for object detection. When I run model_main_tf2.py, I get the following error:
Traceback (most recent call last):
File "C:\Python_venv\trained_models\model_main_tf2.py", line 32, in <module>
from object_detection import model_lib_v2
ImportError: cannot import name 'model_lib_v2' from 'object_detection' (c:\Python_venv\tensorflow\lib\site-packages\object_detection\__init__.py)
How do I install model_lib_v2?
I tried reinstalling TF and reinstalling TensorFlow-object-detection-API but no luck. I went all over the internet looking for answers.
I found:
https://github.com/tensorflow/models/issues/7920
But they don't say how to install model_lib_v2
Unfortunately I cannot use TF1, the goal is to use TF2.
see https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md
git clone https://github.com/tensorflow/models.git
## Python Package Installation
cd models/research
### Compile protos.
protoc object_detection/protos/*.proto --python_out=.
### Install TensorFlow Object Detection API.
cp object_detection/packages/tf2/setup.py .
python3 -m pip install --user --use-feature=2020-resolver .
You should be able to just go to line 32 in "C:\Python_venv\trained_models\model_main_tf2.py" and remove "from object_detection", since you are already inside the package.
replace:
"from object_detection import model_lib_v2"
by:
"import model_lib_v2"

How to install tensorflow without pip in the right way?

So I have installed tensorflow with pip using pip install tensorflow==1.15 a few days ago, today I wanted to search for my models-master file and I didn't find it. I uninstalled tensorflow using pip uninstall tensorflow and then I installed it from the github and added it to the folder of the project I am working on like, it is in the same folder as the .py files of my original project. This is because I need to use protoc on the files because I got error messages before. I think I got the protoc part down now but I can't test it, because whenever I want to run my main file, I get this error message:
Traceback (most recent call last):
File "C:\Users\user\Desktop\captcha solver\main_.py", line 1, in <module>
from CAPTCHA_object_detection import *
File "C:\Users\user\Desktop\captcha solver\CAPTCHA_object_detection.py", line 18, in <module>
from object_detection.utils import ops as utils_ops
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection\utils\ops.py", line 291, in <module>
dtype=tf.float32):
AttributeError: module 'tensorflow' has no attribute 'float32'
I think this is because of the way I installed tensorflow, but I don't know what I did wrong, I have never installed modules without pip though.

cannot import name 'string_int_label_map_pb2' ,even after run 'protoc object_detection/protos/*.proto --python_out=.'

I didn't find any useful information from stackoverflow to correct the error, that's why this question is here.
I'm using tensorflow object detection .But getting the following error
Traceback (most recent call last):
File "C:/Users/Sam.CP/PycharmProjects/Tensorflow/tensor_demo.py", line 20, in <module>
from object_detection.utils import label_map_util
File "C:\Users\Sam.CP\PycharmProjects\Tensorflow\object_detection\utils\label_map_util.py", line 21, in <module>
from object_detection.protos import string_int_label_map_pb2
ImportError: cannot import name 'string_int_label_map_pb2
'
Hence i have done a small research about this issue and i found this answer and i used the protoc object_detection/protos/*.proto --python_out=. command in cmd ,then the py files are created in the protos folder. I'm getting the same error even after the files are created.
Python - 3.6.0,
tensorflow- 1.6.0,
Protobuf- 3.4.0
The solution that works for me is as follows:
If you are creating a Virtual environment:
Run the command after you create the environment to change the Google file .protp to .py:
protoc object_detection/protos/*.proto --python_out=.
However, if you are still facing an error, you can simply run the code on a google colab notebook:
Protos Conversion to Python
%%cd /content/drive/MyDrive/TFOD1.x/models/research
!protoc object_detection/protos/*.proto --python_out=.
get the file in protoc folder, then copy it into the >models>research>protoc
And (import step):
Copy the folder to your environment path:
For example:
C:\Users\x04xx18\Anaconda3\envs\tfod1.x\libs\protos
This should fix the error for you!

Error in importing openface

I installed openface using the link
But when i am importing openface in python it is giving an error
./util/align-dlib.py ./home/admin1/Dhoni/ align outerEyesAndNose ./aligned-images/ --size 96
Traceback (most recent call last):
File "./util/align-dlib.py", line 24, in <module>
import openface
ImportError: No module named openface
Please help me!!!!!
If you are on Linux,
Clone the project :https://github.com/cmusatyalab/openface
cd openface
python setup.py install
Then create an empty file named __init__.py inside the openface/ directory
Try running the demo command : ./demos/compare.py images/examples/{lennon*,clapton*}
Note: If the demo fails due to dependency issue, try to install those dependencies. Like in my case I had to install following as well
luarocks install torch
luarocks install nn
luarocks install dpnn
Do this in your terminal:
cd /home/your_username/openface
python setup.py install
This should help you.

ImportError: No module named dns.exception

I am trying to use the dnspython in my code but when it gets to the import statement, I get the following error:
>>> import DNS.resolver
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python27\lib\site-packages\DNS\resolver.py", line 31, in <module>
import dns.exception
ImportError: No module named dns.exception
I have tried installing with pip, easy_install and manually from the git repo but nothing seems to do the trick. Any ideas??
Code:
import DNS.resolver
if __name__ == "__main__":
domain = 'hotmail.com'
for x in dns.resolver.query(domain, 'MX'):
print x.to_text()
Another thing you can do to fix problems with this package is to install python-dnspython again using Synaptic. (Whether no having previous problems about duplicity)
Sometimes if you try to install this package using pip could appear some problems like that:
Command python setup.py egg_info failed with error code 1 in
/tmp/pip_build_root/dnspython Storing debug log for failure in
/root/.pip/pip.log
Using Synaptic cleans the old files and install the new ones, from that package. I know this issue is solved but I wanted to add more information about this :)
That's because package and module names are lowercase (see PEP 8). This works just fine :
import dns.resolver
import dns.exception
You should also be careful that none of your own *.py filename conflicts with the dns package. Your *.py file should not be named dns.py. Pay also attention to the *.pyc files.
First, your code should be : import dns.resolver.
There seems to be an issue installing dnspython using pip, you should clone the dnspython repository using git and run the file setup.py as follows:
git clone https://github.com/rthalley/dnspython
cd dnspython\
python setup.py install
If you don't have git installed on your machine, you can just download the repository manually .

Categories