Getting Started with Fastai v1 the Easy Way, Using Python 3.6, Apt and Pip

Chris Fotache
3 min readOct 19, 2018

--

The Fast.ai deep learning library just saw it’s 1.0 release in October 2018! You might be familiar with it from the popular free online course, and I won’t go into details about how easy and fast it is. I’ll just mention it’s based on PyTorch 1.0 and that you may want to listen to the TWiML podcast with Fast.ai creator Jeremy Howard.

Before getting to install Fastai, you might realize you don’t have the required Python 3.6. You’re most likely still using Python 2.7, and if you try installing Python 3 you end up with version 3.5 which is insufficient. Here’s how you can make sure:

python3 -V
Python 3.5.2

So here’s how to install it on Ubuntu. If you don’t want to build it from source and prefer the easy apt-get method, this is the easiest way:

sudo apt-get install software-properties-common python-software-properties
sudo add-apt-repository ppa:jonathonf/python-3.6
sudo apt-get update
sudo apt-get install python3.6

Now you can double check to make sure you got it right. Remember that the python3 command will still call Python 3.5, you must use python3.6 to run this version:

python3.6 -V
Python 3.6.3

Next, you’ll need to get the proper version of pip, in order to install all future packages. Note that once installed, you must call it with pip3.6, not pip3.

python3.6 -m pip install

Now, since you installed Python from the PPA repository, it’s missing some basic components. You might get a “command ‘x86_64-linux-gnu-gcc’ failed with exit status 1” error when trying to install certain modules with pip, so don’t forget to install this as well:

sudo apt-get install python3.6-dev

One more pre-requisite. You’ll probably get this additional error every time you have a runtime error in python: “ModuleNotFoundError: No module named ‘apt_pkg’”. To solve this, you must make sure you have the python_apt package installed in Python 3, and then copy its file so it can be recognized by Python 3.6 as well (note that the file name might be slightly different in your environment):

sudo apt install python3-apt
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.so

And now you’re ready to proceed with the Fastai installation, following the official instructions. The easiest way is with pip. First you need to install PyTorch 1.0 (the dev version), and then Fastai 1.0 itself:

pip install torch_nightly -f https://download.pytorch.org/whl/nightly/cu92/torch_nightly.html
pip install fastai

Note that for the torch installer you must get the correct wheel for your environment. You can get that path from the Pytorch site, using Preview / Linux / Pip / Python3.6 / Your Cuda version. Once you’re done, make sure you got Fastai v1 installed by running pip show fastai.

The only thing remaining now is to install Jupyter Notebook for Python 3.6 so you can take advantage of its benefits in data exploration:

sudo python -m ipykernel install — name Python3.6

If you’re running this on a cloud VM and want to access the notebook from your local browser, you’ll need SSH tunneling. Here are instructions on how to do that, including the Putty version if you’re using Windows like me.

And now you’re ready to try Fastai! You can start with their examples on Github, or go through the documentation. Their free deep learning course is amazing but for now it covers version 0.7.0. An updated v1 edition should be coming soon. The official forums are also very useful.

Here’s how you can train a convolutional neural network (Resnet34) to classify cats and dogs in just a few lines of code (and a couple of minutes or training time):

from fastai import *
from fastai.vision import *
from fastai.docs import *
import torch
untar_data(DOGS_PATH) #comment this out after running the 1st time
data = image_data_from_folder(DOGS_PATH, ds_tfms=get_transforms(), tfms=imagenet_norm, size=224)
learn = ConvLearner(data, tvm.resnet34, metrics=accuracy)
learn.fit_one_cycle(1)
learn.unfreeze()
learn.fit_one_cycle(6, slice(1e-5,3e-4), pct_start=0.05)
print(accuracy(*learn.TTA()))
learn.save("dogscats")

So that’s it! You installed Fastai 1.0 and ran the first CNN classifier! Now it’s time to work on real projects. Feel free to post in comments any cool projects you’re working on.

Chris Fotache is an AI researcher with CYNET.ai based in New Jersey. He covers topics related to artificial intelligence in our life, Python programming, machine learning, computer vision, natural language processing and more.

--

--

Chris Fotache
Chris Fotache

Written by Chris Fotache

AI researcher at CYNET.ai, writing about artificial intelligence, Python programming, machine learning, computer vision, robotics, natural language processing

Responses (2)