Cannot import name 'model_lib_v2' from 'object_detection' - tensorflow

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"

Related

pipenv installed pip does not work with specified python version

On a Raspberry Pi OS Bullseye system, I tried to install numpy with pipenv using a specific python version and got this:
$ pipenv --python /opt/python/3.7/bin/python3 install numpy --verbose
Creating a virtualenv for this project…
Using /opt/python/3.7/bin/python3 (3.7.9) to create virtualenv…
⠋created virtual environment CPython3.7.9.final.0-32 in 410ms
creator CPython3Posix(dest=/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/pi/.local/share/virtualenv)
added seed packages: pip==20.3.4, pkg_resources==0.0.0, setuptools==44.1.1, wheel==0.34.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Virtualenv location: /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC
Installing numpy…
⠙Installing 'numpy'
$ "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip" install --verbose "numpy" -i https://pypi.org/simple --exists-action w
⠙
Error: An error occurred while installing numpy!
Traceback (most recent call last):
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip", line 5, in <module>
from pip._internal.cli.main import main
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 10, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
from pip._internal.cli import cmdoptions
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
from pip._vendor.packaging.utils import canonicalize_name
ModuleNotFoundError: No module named 'pip._vendor.packaging'
Looking at the verbose output i see that the path to pip used by pipenv is /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip.
Calling this pip directly indeed leads to the same error:
$ /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip --version
Traceback (most recent call last):
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip", line 5, in <module>
from pip._internal.cli.main import main
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 10, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
from pip._internal.cli import cmdoptions
File "/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
from pip._vendor.packaging.utils import canonicalize_name
ModuleNotFoundError: No module named 'pip._vendor.packaging'
Which python is used in that case? Looking at the shebang line it would seem it's the one I passed to pipenv initially:
$ head -n 1 /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip
#!/home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/python
$ ls -l /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/python
lrwxrwxrwx 1 pi pi 27 Dec 11 11:00 /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/python -> /opt/python/3.7/bin/python3
But when I explicitly use that exact interpreter there is no error:
$ /opt/python/3.7/bin/python3 /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/bin/pip --version
pip 20.1.1 from /opt/python/3.7/lib/python3.7/site-packages/pip (python 3.7)
The difference seems to be that in the case it goes wrong, the pip installation in /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip is used while in the working case it's the one in /opt/python/3.7/lib/python3.7/site-packages/pip.
But why? My understanding of the shebang is that it points to the interpreter that's to be used. In the working example all i do is call that interpreter explicitly myself. Why is there a difference in behaviour?
And also, why did pipenv even install its own pip in /home/pi/.local/share/virtualenvs/deep-dregs-eaJke9eC/lib/python3.7/site-packages/pip ? Why didn't it reuse the pip that comes with the python version I passed? And if that's just how pipenv works, why is its pip broken? What's going on? And how can I fix it?
EDIT
When i use my system python 3.9 installation it works fine.

Tensorflow docker installation problem. (undefined symbol: _ZN10tensorflow8OpKernel11TraceStringEPNS_15OpKernelContextEb)

I am trying to learn more about tensorflow and building a custom model with it.
I follow an instruction in which i install a docker container with the following steps:
git clone https://github.com/tensorflow/models.git
cd models
docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od .
docker run -it od
All works ok.
Next step is to run a test in the container script:
python object_detection/builders/model_builder_tf2_test.py
This fails and outputs:
Traceback (most recent call last):
File "object_detection/builders/model_builder_tf2_test.py", line 22, in <module>
import tensorflow.compat.v1 as tf
File "/home/tensorflow/.local/lib/python3.6/site-packages/tensorflow/__init__.py", line 438, in <module>
_ll.load_library(_main_dir)
File "/home/tensorflow/.local/lib/python3.6/site-packages/tensorflow/python/framework/load_library.py", line 154, in load_library
py_tf.TF_LoadLibrary(lib)
tensorflow.python.framework.errors_impl.NotFoundError: /usr/local/lib/python3.6/dist-packages/tensorflow/core/kernels/libtfkernel_sobol_op.so: undefined symbol: _ZN10tensorflow8OpKernel11TraceStringEPNS_15OpKernelContextEb
So there is a question about this in stackoverflow see:
Also a followup at:
It says, if i understand correctly, to uninstall and install tensorflow again, but it does not work.
The version is 2.6.1. If i try to downgrade i run into all kind of other problems.
I am stuck ;-(, any clues ?
I am working on a VPS, is it possible to run the tensorflow docker on 'normal' hardware ?

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

How can I install the tensorflow object detection API on Google colab?

I tried running tensorflow object detection API on Colab according to
Inline Link
I got such an error at the first Install required packages.
How can I solve it?
Background : Python2 , GPU
/root
fatal: destination path 'models' already exists and is not an empty directory.
/root/models/research
Traceback (most recent call last):
File "object_detection/builders/model_builder_test.py", line 23, in <module>
from object_detection.builders import model_builder
ImportError: No module named object_detection.builders
I'm not clear about from which directory you are executing the command.
if you executing it from content directory then go to model and then to research directory.
%cd ~/models/research
!python object_detection/builders/model_builder_test.py
If you don't have model directory clone it by using
!git clone --quiet https://github.com/tensorflow/models.git

Sklearn will not run/compile due to numpy errors

I would not be posting this question if I had not researched this problem thoroughly. I run python server.py (it uses sklearn). Which gives me
Traceback (most recent call last):
File "server.py", line 34, in <module>
from lotusApp.lotus import lotus
File "/Users/natumyers/Desktop/.dev/qq/lotusApp/lotus.py", line 2, in <module>
from sklearn import datasets
File "/Library/Python/2.7/site-packages/sklearn/__init__.py", line 57, in <module>
from .base import clone
File "/Library/Python/2.7/site-packages/sklearn/base.py", line 11, in <module>
from .utils.fixes import signature
File "/Library/Python/2.7/site-packages/sklearn/utils/__init__.py", line 10, in <module>
from .murmurhash import murmurhash3_32
File "numpy.pxd", line 155, in init sklearn.utils.murmurhash (sklearn/utils/murmurhash.c:5029)
ValueError: numpy.dtype has the wrong size, try recompiling
I next do everything I can, nothing helps.
I ran:
sudo -H pip uninstall numpy
sudo -H pip uninstall pandas
sudo -H pip install numpy
sudo -H pip install pandas
All which give me errors such as OSError: [Errno 1] Operation not permitted:
I try sudo -H easy_install --upgrade numpy
I get a list of errors like
_configtest.c:13:5: note: 'ctanl' is a builtin with type '_Complex long double (_Complex long double)'
_configtest.c:14:5: warning: incompatible redeclaration of library function 'cpowl' [-Wincompatible-library-redeclaration]
int cpowl (void);
^
Edit: Perhaps part of the issue was that I wasn't running the virtual environment. So I get that going, and when I type python server.py, I get error:
from sklearn import datasets
ImportError: No module named sklearn
sudo -H pip install -U scikit-learn Doesn't install because of another error....
I was using depreciated python. I updated everything to python 3, and used pip3.