tensorflow object detection API ImportError when generating PASCAL TFRecord files - tensorflow

I'm trying to use the Tensorflow Object Detection API and I've successfully tested the installation,but we I try to generate the PASCAL VOC TFRecord files with the given command
python object_detection/create_pascal_tf_record.py \
--label_map_path=object_detection/data/pascal_label_map.pbtxt \
--data_dir=VOCdevkit --year=VOC2012 --set=train \
--output_path=pascal_train.record
I encountered the following error:
Traceback (most recent call last):
File "object_detection/create_pascal_tf_record.py", line 36, in <module>
from object_detection.utils import dataset_util
ImportError: No module named object_detection.utils
my PYTHONPATH is:
:/usr/local/lib/python2.7/dist-packages/tensorflow/models:/usr/local/lib/python2.7/dist-packages/tensorflow/models/slim
and I'm running the above command in the /models directory,anyone who knows how to fix this problem?

I had the same problem and I solved it by adding :
import os
import sys
sys.path.append(os.path.abspath("./object_detection"))
and
from object_detection.utils import dataset_util
becomes
from utils import dataset_util

It's better for you to add 'object_detection' to the env path, not in the python script.
You can do this in the shell, at the 'object_detection' directory.
export PYTHONPATH=$PYTHONPATH:`pwd`
Or add the path to .bashrc/.zshrc (depend on your shell), so don‘t need to export every time.
echo "export PYTHONPATH=$PYTHONPATH:`pwd` >> ~/.bashrc

Related

Cannot import name 'model_lib_v2' from 'object_detection'

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"

Tensorflow Module Not Found Error when running MNIST TPU

I'm new to tensorflow and was trying to follow the tutorial here https://cloud.google.com/tpu/docs/quickstart to run the MNIST TPU model.
I got error from
python3 mnist_main.py \
--tpu=$TPU_NAME \
--model_dir=$MODEL_DIR \
--data_dir=$DATA_DIR \
--train_epochs=10 \
--distribution_strategy=tpu \
--download
Error message
Traceback (most recent call last):
File "mnist_main.py", line 31, in <module>
from official.vision.image_classification.resnet import common
File "/usr/share/models/official/vision/image_classification/resnet/common.py", line 26, in <module>
import tensorflow_model_optimization as tfmot
ModuleNotFoundError: No module named 'tensorflow_model_optimization'
Any idea about to solve it ? Thank you.
You need to install it using command line (or terminal).
pip install --user --upgrade tensorflow-model-optimization
In Google Colab, if you are running a notebook using qkeras you need to do the following:
!git clone https://github.com/google/qkeras.git
import sys
sys.path.append('qkeras')
!pip install git+https://github.com/keras-team/keras-tuner.git
!pip install tensorflow_model_optimization
import tensorflow_model_optimization as tfmot

tf-1.5 or tf-2.1 when running object detection training

When I tried to run:
From the tensorflow/models/research/ directory
PIPELINE_CONFIG_PATH=/home/paul/objdetect/models/research/object_detection/samples/configs/ssdlite_mobilenet_v3_small_320x320_kitti.config
MODEL_DIR=/home/paul/objdetect/models/research/object_detection/samples/sample_model_ckpt/
NUM_TRAIN_STEPS=5000
SAMPLE_1_OF_N_EVAL_EXAMPLES=1
python object_detection/model_main.py \
--pipeline_config_path=${PIPELINE_CONFIG_PATH} \
--model_dir=${MODEL_DIR} \
--num_train_steps=${NUM_TRAIN_STEPS} \
--sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \
--alsologtostderr
if I use tensorflow-gpu==1.5.0 environment, I got following error:
from nets import inception_resnet_v2
File "/home/paul/objdetect/models/research/slim/nets/inception_resnet_v2.py", line 375, in <module>
batch_norm_updates_collections=tf.compat.v1.GraphKeys.UPDATE_OPS,
AttributeError: 'module' object has no attribute 'v1'
Then I switch to tensorflow-gpu==2.1.0 environment, and run it again, I got the following error:
from object_detection import eval_util
File "/home/paul/objdetect/models/research/object_detection/eval_util.py", line 40, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'
So is there a way to run it at all? what do I need to fix to get it running either in tf-1.5 or tf-2.1?
Anyone could help? Thank you very much in advance.
Sincerely yours,
-Paul
tf.contrib has been deprecated in tf2.x. Similarly tf.compat was introduced in the 1.14.x and 1.15.x for solving compatability between tf 1.x and tf2.x. Try to install tensorflow version 1.15.x and run
from object_detection import eval_util
File "/home/paul/objdetect/models/research/object_detection/eval_util.py", line 40, in <module>
slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'
For this error if you have tensorflow >=2.0 you need to replace slim = tf.contrib.slim with import tf_slim as slim.
This will solve your error

Colab ModuleNotFoundError

I have been following
https://colab.research.google.com/drive/12qC2abKAIAlUM_jNAokGlooKY-idbSxi
pretty exactly but I still cannot access any of my code from other files
My attempt:
!ls /content/gdrive/My\ Drive/Colab\ Notebooks/*.ipynb
!cat '/content/gdrive/My Drive/Colab Notebooks/module_i_want.ipynb'
import sys
sys.path.append('/content/gdrive/My Drive')
import module_i_want as mod
mod.loadData()
I am getting:
ModuleNotFoundError: No module named 'module_i_want'
I have tried:
import os
print(os.path.dirname(os.path.realpath('module_i_want.ipynb')))
print(os.path.dirname(os.path.realpath('module_im_in.ipynb')))
and they both return
/content
I can see both files to the left in my mounted drive (in the same folder) but I can't seem to find any way to import anything
Resolved:
For .ipynb files in Google Colab you need to specifically install
!pip install import_ipynb
and then:
import import_ipynb
in order to deal with this type of file

Cannot use bigquery in appengine : "ImportError: No module named google.cloud.bigquery"

Tried everything possible to fix this bug am not sure where I'm going wrong. I've run pip already and seems like everything is in place, but the lib errors are still there.
Need guidance on what else to check
pip install --system --upgrade -r requirements.txt -t lib
$ cat appengine_config.py
import sys
import os
sys.path.insert(0, 'lib')
# appengine_config.py
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add('lib')
$ cat requirements.txt
GoogleAppEngineCloudStorageClient==1.9.22.1
google-api-python-client
google-cloud
google-cloud-bigquery
#Inside the code
from google.cloud import bigquery
from google.cloud.bigquery import Dataset
ERROR 2018-11-10 16:24:47,981 wsgi.py:263]
Traceback (most recent call last):
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/home/royans/repository/tesmon/tesmonweb/tmon.py", line 32, in <module>
from google.cloud import bigquery
File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1149, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named google.cloud.bigquery