TensorFlow pip installation issue: cannot import name 'descriptor' - tensorflow

I'm seeing the following error when installing TensorFlow:
ImportError: Traceback (most recent call last):
File ".../graph_pb2.py", line 6, in
from google.protobuf import descriptor as _descriptor
ImportError: cannot import name 'descriptor'

This error signals a mismatch between protobuf and TensorFlow versions.
Take the following steps to fix this error:
Uninstall TensorFlow.
Uninstall protobuf (if protobuf is installed).
Reinstall TensorFlow, which will also install the correct protobuf dependency.

I faced the similar issue, after trial and error, I used the below logic to run the program:
pip install --upgrade --no-deps --force-reinstall tensorflow
This will make sure to uninstall and reinstall the program from fresh. It works!

I would be extra careful before uninstalling/reinstalling other packages such as protobuf. What I think would most likely be the issue is difference in versions. As of writing this, the most recent release of python is 3.7 while tensorflow is only compatible up to 3.6.
If you're using a 3rd party distribution like Anaconda, this can get hidden from you. In this case I would recommend creating a new environment in Anaconda, with python 3.6 and then installing tensorflow: https://conda.io/projects/conda/en/latest/user-guide/getting-started.html#managing-python

Try this:
pip uninstall protobuf
brew install protobuf
mkdir -p
/Users/alexeibendebury/Library/Python/2.7/lib/python/site-packages
echo 'import site;
site.addsitedir("/usr/local/lib/python2.7/site-packages")' >>
/Users/alexeibendebury/Library/Python/2.7/lib/python/site-packages/homebrew.pth

Related

Difficulty updating keras/tensorflow on mac

I am working on a jupyter notebook script which I used last year to train a neural network.
When I try to import the keras tokenizer:
from keras.preprocessing.text import Tokenizer
I receive this error
I have seen other posts which suggest that I need to update tensorflow. My anaconda environment tells me I have 1.13.1 installed. But when I try to update tensorflow-base to 1.15 in the anaconda navigator, I receive this error:
I can update tensorflow from my command line using:
conda install tensorflow=1.15.0
But this doesn't update tensorflow in my anaconda environment and the error persists in my notebook.
Any help would be much appreciated! As you can probably tell, I am a novice python user.
The error says some packages needed to update Tensorflow/Andaconda requires Python 3.11 or newer. Since not all of the error log can be seen however, I would upgrade to python 3.7 to be safe. You can download this from the official page: https://www.python.org/downloads/
If the problem persists, try using pip to update the packages(In bash) :
pip install tensorflow
pip install conda
If you get an error while using pip, try:
pip3 install tensorflow
pip3 install conda
This same method can be used to update keras:
pip install keras
or if that does not work:
pip3 install keras
If pip is not recognized at a command, Python 3.7 is not added to path. I do not have experience with macOS, but this article should go into enough depth.
https://realpython.com/add-python-to-path/#how-to-add-python-to-path-on-linux-and-macos

OSError: libcudart.so.10.2: cannot open shared object file: No such file or directory

For some reason, I am getting this error on Colab, even if I don't use GPU... Any help would be greatly appreciated! Thanks! The error message is as following:
OSError: libcudart.so.10.2: cannot open shared object file: No such file or directory
The reason is a mismatch of CUDA versions. I ran into this issue because the preinstalled version of pytorch did match the default version which I installed using %pip install torchaudio (CUDA 10.2). print(torch.__version__) gives 1.10.0+cu111 (CUDA 11.1).
So I reinstalled pytorch, torchaudio and torch vision with the command stated on the pytorch website
%pip install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
After restarting the environment, it should work.
This method uninstalls pytorch and reinstalls another version, it would be faster to just install the matching version of pytorch, in my case:
%pip install -q torchaudio==0.10.0+cu111 -f https://download.pytorch.org/whl/cu111/torch_stable.html
I don't know if it would be better to install the cu113 variant.
Also, I would suggest to check the error logs to find out the python package that causes the error. In my case, it was generated in torch-cluster and it simply resolved by downgrading torch-cluster to 1.5.9 (recent version is 1.6.0 which is release just couple of weeks back and was installed by default)
I've solved it by replacing the version of torchaudio installed by pip with the one from conda.
pip uninstall torchaudio
conda install torchaudio -c pytorch
Notice the message of conda, it installs the version with bundled CUDA lib:
The following NEW packages will be INSTALLED:
torchaudio pytorch/linux-64::torchaudio-0.11.0-py38_cu113

No module named tensorflow even after installing with pip

I'm trying to follow this guide to test this new algorithm: https://github.com/lalonderodney/SegCaps
I can't do it in my PC, so i'm using another server with Putty. Now I'm connected with the other server.
First of all I installed TensorFlow as indicates in the guide with :
pip install -r requirements.txt
After I wrote this code: ./main.py segcaps.png
in which segcaps.png is the image that i want to use
Finally I wrote python main.py --data_root_dir data
that is the only required parameter with the directory containing imgs and masks folders.
Now it gives me an error:
ModuleNotFoundError: No module named 'tensorflow.python.framework'
I searched it in the directory tensorflow/python/framework and it exists.
So, i don't know how to solve it. Ideas?
If you have multiple Python versions installed, then you'll (most likely) have multiple pip versions installed too. Make sure that the pip command you use installs the package(s) into the Python version you want it to. It may so happen that the package got installed into python2 but you wanted it in python3.
Since using pip did not install the packages in python3, pip3 is most likely to the PyPI for python3. Try
pip3 install -r requirements.txt
and that should work.
In case you have an EnvironmentError you can try this (bad idea):
pip3 install -r requirements.txt --user
This solves the problem most of the times on standalone machines. I'm not sure about the server; insufficient permissions might block this.
Why is the --user flag a bad idea? Read: What is the purpose “pip install --user …”?
You can use pip show tensorflow to see if it is installed or not.
As for ModuleNotFoundError try uninstalling keras and reinstalling an earlier version by pip install keras==2.1.6

How to downgrade tensorflow version in colab?

I am using pip3 install tensorflow==1.8.0, but it doesn't have GPU support.
So I am using pip3 install tensorflow-gpu==1.8.0, but it still raises an exception
libcudart.so.VERSION No such file.
Should I use colab to install tensorflow from source?
After pip3 list:
tensorboard 1.10.0
tensorflow 1.10.0
tensorflow-hub 0.1.1
Google recommends you not to do pip installs!!!!
use this instead: %tensorflow_version 1.x
Restart the Runtime and check if its changed:
import tensorflow
print(tensorflow.__version__)
Here is a link to the main article:
https://colab.research.google.com/notebooks/tensorflow_version.ipynb#scrollTo=8UvRkm1JGUrk
You can downgrade Tensorflow to a previous version without GPU support on Google Colab. I ran:
!pip install tensorflow==1.14.0
import tensorflow as tf
print(tf.__version__)
which initially returned
2.0.0-dev20190130
but when I returned to it after a few hours, I got the version I requested:
1.14.0
Trying to downgrade to a version with GPU support:
!pip install tensorflow-gpu==1.14.0
requires restarting the runtime and fails, as importing import tensorflow as tf returns:
ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
Update
When the import fails you can always downgrade CUDA to version 9.0 using following commands
!wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
!apt-get update
!apt-get install cuda=9.0.176-1
You can check the version of CUDA by running:
!nvcc --version
Second update
This code now seems to fail, see the follow-up question at How to downgrade to tensorflow-gpu version 1.12 in google colab
Google gives quite a simple solution to downgrade to the previously used Colab tf v.1.15.2. Just run the following magic line in Colab:
%tensorflow_version 1.x
Ther recommend "against using pip install to specify a particular TensorFlow version for both GPU and TPU backends. Colab builds TensorFlow from the source to ensure compatibility with our fleet of accelerators. Versions of TensorFlow fetched from PyPI by pip may suffer from performance problems or may not work at all". This means if you need GPU support, use one of the two given TF versions. The other versions will not necessary work I guess even for CPU.
The build process for GPU-enabled tensorflow is involved. In particular, old versions of TensorFlow use (or require) older versions of CUDA, which itself depends on system libraries and configuration beyond the scope of a pip install.
I suspect that downgrading TensorFlow on a VM configured for a newer version is going to be an involved process, perhaps involving downgrades / reinstalls of system libraries.
If it's practical, it might be simpler to update your code to use the latest version of TensorFlow, at least until Colab supports persistent backend enivronments.
It seems that only tensorflow 2 is supported by Colab, but that's not true, you still can use pip to uninstall tensorflow 2 and install a specific version of tf1. !yes|pip uninstall tensorflow, !pip install tensorflow==1.15.5 Maybe you should install other dependencies. So use !pip install -r requirements.txt Attention! You must restart the runtime in order to use newly installed versions.
%tensorflow_version 1.x no longer works.
%tensorflow_version 1.x
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-2-8d2919c1d33c> in <module>
----> 1 get_ipython().run_line_magic('tensorflow_version', '1.x')
1 frames
/usr/local/lib/python3.8/dist-packages/google/colab/_tensorflow_magics.py in _tensorflow_version(line)
33
34 if line.startswith("1"):
---> 35 raise ValueError(
36 # pylint: disable=line-too-long
37 textwrap.dedent("""\
ValueError: Tensorflow 1 is unsupported in Colab.
Your notebook should be updated to use Tensorflow 2.
See the guide at https://www.tensorflow.org/guide/migrate#migrate-from-tensorflow-1x-to-tensorflow-2.

Issue while installing Tensorflow

While installing tensorflow(CPU only) I am facing below error :
>>> import tensorflow as tf
Traceback (most recent call last): File "", line 1, in
ImportError: No module named 'tensorflow'
I have python version 3.5
Did you first try:
pip3 install --upgrade tensorflow
It's maybe due to some missing .py files. Download the package from Github link and paste it on python35/Lib/site-packages
import tensorflow as tf
invokes tensorflow not install it there is great article how to install on tensorflow website.
You have not mentioned platform/OS you working on also steps taken if any before trying this command.
best thing for beginners I feel is Anaconda which is suit of packages required for data science.
Give it a try.
python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl
You can search for the appropriate version you wish to install and update in the above mentioned url.
Try to install an old version of Tensorflow as:
pip install tensorflow==1.5.0
After installing the old version retry with the newest version, for example:
pip install tensorflow==1.9.0