Unable to import TorchVision after installation Mac OSX - python

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"

Related

Huggingface Transformers not getting imported in VS Code

My VS Code Editor for Python is not able to import transformers even though I have done a conda install and giving me the following error
Traceback (most recent call last):
File "c:/Users/I323017/Documents/Studies/question_answering_kinnal/src/main.py", line 3, in <module>
import transformers
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\transformers\__init__.py", line 107, in <module>
from .pipelines import (
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\transformers\pipelines.py", line 40, in <module>
from .tokenization_auto import AutoTokenizer
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\transformers\tokenization_auto.py", line 49, in <module>
from .tokenization_flaubert import FlaubertTokenizer
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\transformers\tokenization_flaubert.py", line 23, in <module>
from .tokenization_xlm import XLMTokenizer
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\transformers\tokenization_xlm.py", line 26, in <module>
import sacremoses as sm
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\sacremoses\__init__.py", line 2, in <module>
from sacremoses.tokenize import *
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\sacremoses\tokenize.py", line 10, in <module>
from sacremoses.util import is_cjk
File "C:\Users\I323017\AppData\Local\Continuum\anaconda3\lib\site-packages\sacremoses\util.py", line 11, in <module>
from joblib import Parallel, delayed
ModuleNotFoundError: No module named 'joblib'
May I know the problem here?
The error clearly suggests:
ModuleNotFoundError: No module named 'joblib'
Try pip install joblib
Also make sure you have latest torch and transformers library installed.
This seems to be an installation issue. If you have already installed transformers using conda install -c conda-forge transformers, an additional upgradation from the source using the below resolved my issue.
git clone https://github.com/huggingface/transformers.git
cd transformers
pip install .
As #Vishnukk has stated, this seems like an installation problem.
HuggingFace has now published transformers officially via their own conda channel
Doing conda install transformers -c huggingface should then work after removing the old version of transformers.

install and import tensorflow and keras

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

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.

ImportError libqhull.so.5 when importing pyplot

I'm getting an ImportError whenever I try to import pyplot from matplotlib. Matplotlib is installed via pip in a virtual environment. This is the error I'm getting:
>>> import matplotlib.pyplot
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 36, in <module>
from matplotlib.figure import Figure, figaspect
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/figure.py", line 40, in <module>
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/axes/__init__.py", line 4, in <module>
from ._subplots import *
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/axes/_subplots.py", line 10, in <module>
from matplotlib.axes._axes import Axes
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 38, in <module>
import matplotlib.tri as mtri
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/tri/__init__.py", line 9, in <module>
from .triangulation import *
File "/home/mikefrank/.virtualenvs/pv-hrt/local/lib/python2.7/site-packages/matplotlib/tri/triangulation.py", line 7, in <module>
import matplotlib._qhull as _qhull
ImportError: libqhull.so.5: cannot open shared object file: No such file or directory
This can occur after a system upgrade. In my specific case, I upgraded Ubuntu 14.04 to 16.04. Matplotlib links to specific versions of \*.so, and these go out of date. I presume this is because the different versions are not (always) backward compatible.
(For me, I went from libqhull.so.6 to libqhull.so.7 during the OS upgrade, and matplotlib was linked to libqhull.so.6.)
Firstly, you can probably make sure libqhull exists on your system by using something like:
sudo apt install libqhull-dev
or the equivalent for your system.
(mine was already installed, so nothing changed.)
Since nowadays, pip often installs a wheel, which is already compiled, we'll need to force pip to do the compilation, so it links with the new so file.
I used:
py35 -m pip install --upgrade matplotlib --no-binary matplotlib --force
The options:
--no-binary matplotlib tells pip to not use a wheel for matplotlib (but it may still use wheels for dependencies such as numpy).
--upgrade should be obvious
--force, because we may already be at the latest matplotlib version, and then --upgrade will not do anything.

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