install and import tensorflow and keras - python

I am going mad for installing and import tensorflow for 6 weeks. I did everything; I installed it from spyder, Conda prompt and with different commands like pip install tensorflow and conda install tensorflow. my python version is 3.7 and after install when I want to import tensorflow in spyder I get this message:
import tensorflow
Traceback (most recent call last):
File "<ipython-input-4-d6579f534729>", line 1, in <module>
import tensorflow
File "C:\Users\sally\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 27, in <module>
from tensorflow._api.v2 import audio
File "C:\Users\sally\Anaconda3\lib\site-packages\tensorflow\_api\v2\audio\__init__.py", line 8, in <module>
from tensorflow.python.ops.gen_audio_ops import decode_wav
File "C:\Users\sally\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 52, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "C:\Users\sally\Anaconda3\lib\site-packages\tensorflow\core\framework\graph_pb2.py", line 9, in <module>
from google.protobuf import symbol_database as _symbol_database
File "C:\Users\sally\Anaconda3\lib\site-packages\google\protobuf\symbol_database.py", line 184, in <module>
_DEFAULT = SymbolDatabase(pool=descriptor_pool.Default())
AttributeError: module 'google.protobuf.descriptor_pool' has no attribute 'Default

This is a very common issue. I have been there.
First: provide the following information:
- what operating system?
- do you want to run on cpu or gpu?
- if gpu, what gpu do you have?
- are your drivers up to date?
- Are your trying to run tensorflow though an IDE like Pycharm, is it more like Jupyter notebook stuff, or just cmd.
Second: There are some link that might be usefull. Here they are.
https://www.tensorflow.org/install/pip
https://www.tensorflow.org/install/gpu#software_requirements
There is a video that helped me, it might help you as well. With tensorflow it is really important to follow the instructions precisely, not missing any steps, and very important, not assuming that installing something with a higher version is no issue. If it says install 'whatever program'.version.1.1.x you can not install 'whatever program'.version.1.1.y
https://www.youtube.com/watch?v=qrkEYf-YDyI&t=1575s

Related

Unable to import TorchVision after installation Mac OSX

I've installed Pytorch and Torchvision in the way suggested on their website via pip within a virtual environment (env), and whilst no errors occur during installation when I go to import torchvision in my python code the following error occurs.
Traceback (most recent call last):
File "demo.py", line 2, in <module>
import torchvision
File "/Users/QuinceyBee/env/lib/python3.7/site-packages/torchvision/__init__.py", line 2, in <module>
from torchvision import datasets
File "/Users/QuinceyBee/env/lib/python3.7/site-packages/torchvision/datasets/__init__.py", line 9, in <module>
from .fakedata import FakeData
File "/Users/QuinceyBee/env/lib/python3.7/site-packages/torchvision/datasets/fakedata.py", line 3, in <module>
from .. import transforms
File "/Users/QuinceyBee/env/lib/python3.7/site-packages/torchvision/transforms/__init__.py", line 1, in <module>
from .transforms import *
File "/Users/QuinceyBee/env/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 17, in <module>
from . import functional as F
File "/Users/QuinceyBee/env/lib/python3.7/site-packages/torchvision/transforms/functional.py", line 5, in <module>
from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION' from 'PIL' (/Users/QuinceyBee/env/lib/python3.7/site-packages/PIL/__init__.py)
I have tried creating new virtual environments to rebuild from scratch, tried to install via conda within a conda environment, however, neither of these resolved this issue.
I apologise for any format issues, this is the first time posting on here and also I'm relatively new to using python.
Any assistance would be greatly appreciated.
Pillow 7.0.0 removed PILLOW_VERSION, you should use version in your own code instead.
https://pillow.readthedocs.io/en/stable/deprecations.html#pillow-version-constant
If using Torchvision, there is a release planned this week (week 2, 2020) to fix it:
https://github.com/pytorch/vision/issues/1712#issuecomment-570286349
The options are:
wait for the new torchvision release
use the master version of torchvision as given below
pip install -U git+https://github.com/pytorch/vision)
install torchvision from a nightly, which also requires a pytorch from a nightly version
or install Pillow<7
pip install "pillow<7"

Error libtorch_python.so: cannot open shared object file: No such file or directory

I'm trying to implement fastai pretrain language model and it requires torch to work. After run the code, I got some problem about the import torch._C
I run it on my linux, python 3.7.1, via pip: torch 1.0.1.post2, cuda V7.5.17. I'm getting this error:
Traceback (most recent call last):
File "pretrain_lm.py", line 7, in <module>
import fastai
File "/home/andira/anaconda3/lib/python3.7/site-packages/fastai/__init__.py", line 1, in <module>
from .basic_train import *
File "/home/andira/anaconda3/lib/python3.7/site-packages/fastai/basic_train.py", line 2, in <module>
from .torch_core import *
File "/home/andira/anaconda3/lib/python3.7/site-packages/fastai/torch_core.py", line 2, in <module>
from .imports.torch import *
File "/home/andira/anaconda3/lib/python3.7/site-packages/fastai/imports/__init__.py", line 2, in <module>
from .torch import *
File "/home/andira/anaconda3/lib/python3.7/site-packages/fastai/imports/torch.py", line 1, in <module>
import torch, torch.nn.functional as F
File "/home/andira/anaconda3/lib/python3.7/site-packages/torch/__init__.py", line 84, in <module>
from torch._C import *
ImportError: libtorch_python.so: cannot open shared object file: No such file or directory
So I tried to run this line:
from torch._C import *
and got same result
ImportError: libtorch_python.so: cannot open shared object file: No such file or directory
I checked /home/andira/anaconda3/lib/python3.7/site-packages/torch/lib and there are only libcaffe2_gpu.so and libshm.so file, and I can't find libtorch_python.so either. My question is, what is actually libtorch_python.so? I've read some of article and like most of it talked about undefined symbol, not cannot open shared object file: No such file or directory like mine. I'm new at python and torch so I really appreciate your answer.
My problem is solved. I'm uninstalling my torch twice
pip uninstall torch
pip uninstall torch
and then re-installing it back:
pip install torch==1.0.1.post2
Try to use pytorch 1.4.0. For which, upgrade the pytorch library using the following command,
pip install -U torch==1.5
If you are working on Colab then use the following command,
!pip install -U torch==1.5
Still facing challenges on the library then install detectron2 library also.
!pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html
I run into this error when I accidentally overwrite pytorch with a different channel. My original pytorch installation is from pytorch channel and in a later update it was overwritten with the one from conda-forge. I got this error even if the version is the same. After reinstalling pytorch from pytorch channel, the error is gone.

pycharm and tensorflow-gpu; DLL not found

I am trying to get a github repository to work on my pc. This repository is based on tensorflow-gpu. Currently, when I try to run the code, I get the following error:
Traceback (most recent call last):
File "D:/audio/deep-voice-conversion-master/deep-voice-conversion-master/convert.py", line 9, in <module>
from models import Net2
File "D:\audio\deep-voice-conversion-master\deep-voice-conversion-master\models.py", line 4, in <module>
import tensorflow as tf
File "D:\audio\deep-voice-conversion-master\deep-voice-conversion-master\venv\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "D:\audio\deep-voice-conversion-master\deep-voice-conversion-master\venv\lib\site-packages\tensorflow\python\__init__.py", line 59, in <module>
from tensorflow.core.framework.graph_pb2 import *
File "D:\audio\deep-voice-conversion-master\deep-voice-conversion-master\venv\lib\site-packages\tensorflow\core\framework\graph_pb2.py", line 6, in <module>
from google.protobuf import descriptor as _descriptor
File "D:\audio\deep-voice-conversion-master\deep-voice-conversion-master\venv\lib\site-packages\google\protoWbuf\descriptor.py", line 47, in <module>
from google.protobuf.pyext import _message
ImportError: DLL load failed: the specified module could not be found.
I am using pycharm on windows 10, with python 3.6.
I tried to follow these steps, but it seems outdated and I did not get it to work. I also prefer not to use anaconda, as this is a huge package and I only want to get this repository to work. So far, I have done the following:
I have installed all required packages (including the tensorflow and tensorflow-gpu packages) via pip.
I also downloaded CUDA 9.0 and cuDNN.
I have copied the files from cuDNN's bin, include and lib folders into the corresponding folders from CUDA.
I added the path to the cuda folder to the environment PATH from windows
I added the CUDA and cuDNN folder paths to the Content root folders of the project structure in pycharm (probably not helpful but someone somewhere suggested it)
I am new to tensorflow and I have no Idea what I have to do next. Any help will be appreciated.
protobuf seems to be a conda package. Try installing it with
conda install protobuf
Or
pip install protobuf

sklearn "numpy.dtype has the wrong size, try recompiling" in both pycharm and terminal

I got "numpy.dtype has the wrong size, try recompiling" in both pycharm and terminal when compiling Sci-kit learning. I've upgraded all packages(numpy, scikit to the latest), nothing works.Python version is 2.7. Please help. Appreciate!
checking for nltk
Traceback (most recent call last):
File "startup.py", line 6, in <module>
import nltk
File "/Library/Python/2.7/site-packages/nltk/__init__.py", line 128, in <module>
from nltk.chunk import *
File "/Library/Python/2.7/site-packages/nltk/chunk/__init__.py", line 157, in <module>
from nltk.chunk.api import ChunkParserI
File "/Library/Python/2.7/site-packages/nltk/chunk/api.py", line 13, in <module>
from nltk.parse import ParserI
File "/Library/Python/2.7/site-packages/nltk/parse/__init__.py", line 79, in <module>
from nltk.parse.transitionparser import TransitionParser
File "/Library/Python/2.7/site-packages/nltk/parse/transitionparser.py", line 21, in <module>
from sklearn.datasets import load_svmlight_file
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
The error "numpy.dtype has the wrong size, try recompiling" means that sklearn was compiled against a numpy more recent than the numpy version sklearn is now trying to import. To fix this, you need to make sure that sklearn is compiled against the version of numpy that it is now importing, or an earlier version. See ValueError: numpy.dtype has the wrong size, try recompiling for a detailed explanation.
I guess from your paths that you are using the OSX system Python (the one that ships with OSX, at /usr/bin/python). Apple has modified this Python in a way that makes it pick up its own version of numpy rather than any version that you install with pip etc - see https://github.com/MacPython/wiki/wiki/Which-Python#system-python-and-extra-python-packages . I strongly recommend you switch to Python.org or homebrew Python to make it easier to work with packages depending on numpy.
The problem occurs when you are using incompatible versions. Check the versions using:
pip freeze
or, for a specific module
pip freeze | grep Module_Name
I fix my problem by updating all packages:
pip install -U scikit-learn numpy scipy pandas matplotlib
As of Today(30/11/2016). These versions are compatible:
matplotlib==1.5.2
nltk==3.2.1
numpy==1.11.2
pandas==0.19.1
scikit-learn==0.18.1
scipy==0.18.1
textblob==0.11.1

Anaconda with Tensorflow: No module named any_pb2

I have just installed Anaconda in a Ubuntu 14.04. Then I installed tensorflow using
conda install -c https://conda.anaconda.org/jjhelmus tensorflow
Then I installed protobuf
conda install -c https://conda.anaconda.org/anaconda protobuf
However, I am not able to solve this issue:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/daniel/anaconda2/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "/home/daniel/anaconda2/lib/python2.7/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 81, in execfile
builtins.execfile(filename, *where)
File "/home/daniel/Documents/Cursos/UnB/Metodos Computacionais/2016/Notas de Aula/Part II - Aula 1 Regression Linear Models/biasVersusVarianceSeveralData.py", line 8, in <module>
import tensorflow as tf
File "/home/daniel/anaconda2/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in <module>
from tensorflow.python import *
File "/home/daniel/anaconda2/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 69, in <module>
from tensorflow.python.training import training as train
File "/home/daniel/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/training.py", line 149, in <module>
from tensorflow.python.training.saver import generate_checkpoint_state_proto
File "/home/daniel/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 29, in <module>
from google.protobuf.any_pb2 import Any
ImportError: No module named any_pb2
Use the instructions on the TensorFlow site.
TLDR;
I am posting this as an answer because people don't look for answers in the comments.
Here's my comment on the issue from github, hope this helps.
"For anyone else who is running into the same issue, I was also having similar problems when trying to import tensorflow on notebook via jupyter. The modules were installed using conda. After spending two days on it without any success (kept getting the same, no module any_pb2/contrib), it was obvious it was a notebook problem and not tensorflow related since I could import tensorflow from python shell. So I decided to switch back to vanilla python and installed everything manually (tensorflow via pip & got rid of jupyter & installed ipython instead) and now I am able to import tensorflow from my ipython notebook."
Original comment link: https://github.com/tensorflow/tensorflow/issues/1161#issuecomment-188664103
The following worked for me:
conda install -c https://conda.anaconda.org/jjhelmus tensorflow

Categories

Resources