Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FastAI library v1 with Google Colab

I am trying to install the FastAi library and use it with Google Colab. I am

Using:

!pip3 install fastai
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4-{platform}-linux_x86_64.whl torchvision
import torch
!pip install Pillow==4.1.1
!pip install image
%matplotlib inline

imports/ installs everything, but when I run

from fastai.imports import *

The last line gives an error that reads:

ImportError: cannot import name 'as_tensor'

Does anyone know what I am doing wrong/why it is not importing fastai library when it is the first thing installed?

The full error I receive is as follows:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-10-9f9378ae0f2a> in <module>()
----> 1 from fastai.imports import *

/usr/local/lib/python3.6/dist-packages/fastai/__init__.py in <module>()
----> 1 from .basic_train import *
  2 from .callback import *
  3 from .callbacks import *
  4 from .core import *
  5 from .data import *

/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in <module>()
  1 "Provides basic training and validation with `Learner`"
----> 2 from .torch_core import *
  3 from .data import *
  4 from .callback import *
  5 

/usr/local/lib/python3.6/dist-packages/fastai/torch_core.py in <module>()
  1 "Utility functions to help deal with tensors"
----> 2 from .imports.torch import *
  3 from .core import *
  4 
  5 AffineMatrix = Tensor

/usr/local/lib/python3.6/dist-packages/fastai/imports/__init__.py in 
<module>()
  1 from .core import *
----> 2 from .torch import *

/usr/local/lib/python3.6/dist-packages/fastai/imports/torch.py in <module>()
  1 import torch, torch.nn.functional as F
  2 from torch import ByteTensor, DoubleTensor, FloatTensor, HalfTensor, 
LongTensor, ShortTensor, Tensor
----> 3 from torch import nn, optim, as_tensor, tensor
  4 from torch.utils.data import BatchSampler, DataLoader, Dataset, Sampler, 
TensorDataset

ImportError: cannot import name 'as_tensor'
like image 875
Joe S Avatar asked Oct 11 '18 14:10

Joe S


4 Answers

You are using previous version of torch torch-0.3.0.post4 which didn't have as_tensor.

So you can use other version of torch like torch-0.4.1 which have this method.

After running following snippet:

!pip3 install fastai
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
# !pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4-{platform}-linux_x86_64.whl torchvision
!pip3 install http://download.pytorch.org/whl/cpu/torch-0.4.1-cp36-cp36m-linux_x86_64.whl

import torch
!pip install Pillow==4.1.1
!pip install image
%matplotlib inline

You can use :

from fastai.imports import *
like image 60
Krishna Avatar answered Oct 19 '22 19:10

Krishna


from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
!pip3 install torch_nightly -f https://download.pytorch.org/whl/nightly/{accelerator}/torch_nightly.html


import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.backends.cudnn.enabled)

If this part is okay, as of today you should see the following output:

1.0.0.dev20181019
True
True

Then move on to installing the latest release of fastai:

!pip3 install fastai

and test with:

from fastai.imports import *
like image 34
aslisabanci Avatar answered Oct 19 '22 19:10

aslisabanci


I was missing dependencies. I have made an awkward workaround that installs the previous version of the library (with dependancies), then uninstalls the library and replaces it with the updated version.

Code as follows.

!pip install "fastai==0.7.0"
!pip uninstall "fastai==0.7.0"
!pip install fastai 
!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
!apt update && apt install -y libsm6 libxext6

accelerator = 'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'

!pip install -q http://download.pytorch.org/whl/{accelerator}/torch-0.3.0.post4- 
{platform}-linux_x86_64.whl torchvision
import torch
!pip install Pillow==4.1.1
!pip install image

%matplotlib inline
from fastai.imports import *
like image 1
Joe S Avatar answered Oct 19 '22 18:10

Joe S


At the moment Google Colab comes with FastAi and you don't need to install it separately. You can start using it right away and import modules like this

from fastai.vision import *
from fastai.metrics import *
like image 1
Khondoker Zahidul Hossain Avatar answered Oct 19 '22 19:10

Khondoker Zahidul Hossain