Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fast.ai Install on Google Colab
!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())
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.0.0
!pip install image
%matplotlib inline
from fastai.imports import *
@suraj2596
Copy link

suraj2596 commented Aug 27, 2018

Got an error "ImportError: No module named pathlib"

I guess !pip install pathlib needs to be added before importing fastai library.

@Graystripe17
Copy link

Graystripe17 commented Aug 31, 2018

Thank you for saving 3 hours of my time

@marcospgp
Copy link

marcospgp commented Sep 15, 2018

How did you figure out that Google Colab is using CUDA 7.5? Because if we could use a newer version, then pytorch could be upgraded to 0.4.0 (https://pytorch.org/previous-versions/).

Also, setting pillow to version 4.0.0 gives this warning: torchvision 0.2.1 has requirement pillow>=4.1.1, but you'll have pillow 4.0.0 which is incompatible.

@keisuke-umezawa
Copy link

keisuke-umezawa commented Oct 6, 2018

Should I install fastai by pip install fastai==0.7.0?

@wontheone1
Copy link

wontheone1 commented Oct 6, 2018

now fastai version 1 is out, we can specify the version as @keisuke-umezawa said.

@wontheone1
Copy link

wontheone1 commented Oct 6, 2018

Taking @marcospgp 's and @keisuke-umezawa 's comments into account, the below can be used instead. Although I am still having some error which I will write in the next comment.

!pip install "fastai==0.7.0"
!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
from fastai.imports import *

@wontheone1
Copy link

wontheone1 commented Oct 6, 2018

I have a problem importing fastai.transforms import

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-94ce375e95cb> in <module>()
      1 from fastai.imports import *
----> 2 from fastai.transforms import *
      3 from fastai.conv_learner import *
      4 from fastai.model import *
      5 from fastai.dataset import *

/usr/local/lib/python3.6/dist-packages/fastai/transforms.py in <module>()
     38     return np.clip((im-mu)*c+mu+b,0.,1.).astype(np.float32)
     39 
---> 40 def rotate_cv(im, deg, mode=cv2.BORDER_REFLECT, flags=cv2.INTER_LINEAR):
     41     """ Rotates an image by deg degrees
     42 

NameError: name 'cv2' is not defined

Could anybody help me with this problem ? thank you very much !

@strinkaj
Copy link

strinkaj commented Oct 10, 2018

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?

Thanks in advance,

@der0pa
Copy link

der0pa commented Oct 11, 2018

i am getting the same error as @strinkaj above :

ImportError: cannot import name 'as_tensor'

@strinkaj
Copy link

strinkaj commented Oct 11, 2018

Stupid Workaround Alert:

!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 *

@deropa There are files that are required for fastaiv1 and are in the pip install for fastai 0.7, so I installed and uninstalled fastai 0.7 and now fast ai v1.0.5 works.

The real way to fix this is to go through and find the missing dependancy, and add the install for just that dependancy.

@Mirodil
Copy link

Mirodil commented Oct 23, 2018

in case of fastai version 1.x

!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.4.1-{platform}-linux_x86_64.whl torchvision
import torch
!pip install Pillow==4.1.1
!pip install image

%matplotlib inline
from fastai.imports import *

for nightly version (https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html)

...
!pip install -q https://download.pytorch.org/whl/nightly/{accelerator}/torch_nightly-1.0.0.dev20181022-{platform}-linux_x86_64.whl torchvision
...

@chakming
Copy link

chakming commented Oct 26, 2018

@marcospgp
Copy link

marcospgp commented Nov 4, 2018

Thank you @chakming!

@Nov05
Copy link

Nov05 commented Dec 20, 2018

【Problem Solved】It wasn't caused by the installation.

I successfully installed fastai 0.7.0 for courses on local Windows 10. I have gone through Lesson 1 notebook without any errors. https://github.com/Nov05/fastai

I also successfully installed fastai 0.7.0 in Google Colab by the following code.

!pip install "fastai==0.7.0"
!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
!pip install Pillow>=4.1.1
!pip install image
!pip install torchtext==0.2.3

!pip install seaborn --upgrade matplotlib
!pip install seaborn --upgrade pandas
!pip install seaborn --upgrade pillow
!pip install seaborn --upgrade plotly

However I encountered an error when executing a major code block. Here is the notebook that contains the error. https://github.com/Nov05/fastai/blob/master/colab/lesson1_bcolz_error.ipynb

arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz))
learn = ConvLearner.pretrained(arch, data, precompute=True)
learn.fit(0.01, 2)
bcolz/carray_ext.pyx in bcolz.carray_ext.carray.__cinit__()
bcolz/carray_ext.pyx in bcolz.carray_ext.carray._open_carray()
OSError: data directory does not exist

Haven't found any solutions online. Could anyone kindly help with this issue? Thanks.

@kasperschnack
Copy link

kasperschnack commented Dec 27, 2018

@wontheone1 I'm having the same problem as you mentioned. Did you manage to find a workaround?

@HaleTom
Copy link

HaleTom commented Jan 12, 2019

Given running the first notebooks still seems a problem, I asked on the forum here.

@zeeshansayyed
Copy link

zeeshansayyed commented Jan 24, 2019

Unfortunately, the script posted by OP doesn't work anymore. After a lot of trial and error, I was able to run all the code blocks successfully using just the following.

!pip install torchvision==0.1.9
!pip install fastai==0.7.0
!pip install torchtext==0.2.3

@akashsharma95
Copy link

akashsharma95 commented Jan 30, 2019

!pip install torchvision==0.1.9 !pip install fastai==0.7.0 !pip install torchtext==0.2.3

thanks!! It works for me

@ioanszilagyi
Copy link

ioanszilagyi commented Feb 14, 2019

Check this link : https://course.fast.ai/start_colab.html

Install the necessary packages :
!curl -s https://course.fast.ai/setup/colab | bash

Saving data files

from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
root_dir = "/content/gdrive/My Drive/"
base_dir = root_dir + 'fastai-v3/'

@ddb95
Copy link

ddb95 commented Mar 7, 2019

Thanks @zeeshansayad

@Hamifthi
Copy link

Hamifthi commented May 28, 2019

hello guys, I have a problem and that is when i change runtime type the GPU that comes up is Tesla T4 and this GPU doesn't support cuda versions below 10. on the other hand pytorch version 0.3 doesn't support cuda 10. does anyone have an idea what should i do?

@yaamas
Copy link

yaamas commented Jun 29, 2020

Unfortunately, the script posted by OP doesn't work anymore. After a lot of trial and error, I was able to run all the code blocks successfully using just the following.

!pip install torchvision==0.1.9
!pip install fastai==0.7.0
!pip install torchtext==0.2.3

This works fine for me. I add another line, which fixes sklearn import error as referenced by the official commit to old/fastai (0.7.0) directory. The complete would be:

!pip install torchvision==0.1.9
!pip install torchtext==0.2.3
!pip install fastai==0.7.0

!sed --in-place '4 c\
from sklearn.preprocessing import LabelEncoder, StandardScaler;from sklearn.impute._base import SimpleImputer as Imputer' /usr/local/lib/python3.6/dist-packages/fastai/structured.py

Paste this script in your cell and run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment