My Journey using FastAI — Part I

Juan Cruz Alric Cortabarria
LatinXinAI
Published in
4 min readJul 22, 2022

--

On this journey, we will look at FastAI and how we can create powerful Machine Learning models with only a laptop and our brain.

A huge thanks to Jeremy Howards for inspiring me to document my journey so that others can also be inspired.

Step 1

Create a new notebook in Google Colab

Step 2

import the necessary libraries

%%capture
!pip install fastbook
from fastbook import *
from fastai import *

Step 3

We need images to train our model. So we are going to download images of a forest and a forest in fire so that we can help the environment and predict whether a forest is in fire or not.

searches = 'forest', 'forest in fire'
path = Path('fire_or_not')
if not path.exists():
for o in searches:
dest.mkdir(exist_ok=True, parents=True)
results = search_images_ddg(f'{o} photo')
download_images(path/o, urls=results[:400])
try:
resize_images(path/o, max_size=400, dest=path/o)
except:
pass
failed = verify_images(get_image_files(path))
failed.map(Path.unlink);
len(failed)

With only a few lines of code, we can get the images from the internet with:

search_images_ddg(f'{o} photo')

Then with the image URLs, we can download them into our respected folders with:

download_images(path/o, urls=results[:400])

Moreover, we transform the images to be all the same size (we need to do this so that the inputs of our model are always the same) and squish them. There are a lot of different transformations you can use. Please feel free to use any you like.

Finally, we check for broken images and get rid of them:

failed = verify_images(get_image_files(path))
failed.map(Path.unlink);
len(failed)

Step 4

Now let's create our DataBlock and dataloaders

dls = DataBlock(
blocks = (ImageBlock, CategoryBlock),
get_items = get_image_files,
splitter = RandomSplitter(valid_pct=0.2, seed=42),
get_y = parent_label,
item_tfms = [Resize(200, method='squish')]
).dataloaders(path)

The inputs are going to be images “ImageBlock” and the outputs are going to be categories “CategoryBlock”

We can get the items we need by using “get_image_files”

We can specify a splitter, this will retrieve some of the data to use for validation purposes. In this case, we are using 20% of the data for validation.

“get_y” getting the label is as easy as just calling the parent_label function. This will get the labels we created when we downloaded the images

searches = 'forest', 'forest in fire'

Finally, we create a transformation for each input using:

[Resize(200, method='squish')]

Now that we have our DataBlock we need to create batches of our data so that we can feed our model many examples at a time. So that's why we use dataloaders. We simply pass the “path” of where the images are.

.dataloaders(path)

Step 5

We can check out dataloaders and see what’s inside:

dls.show_batch(max_n=20)
images of a forest and a forest in fire

Step 6

learn = cnn_learner(dls, resnet34, metrics=error_rate)

Now we are ready to create our learner. A learner’s just an object that has your data and your model and the metric that we want to check out when training it.

We are using our dataloaders and the resnet34 (pre-trained) model

Step 7

learn.fine_tune(3)

Train the model by fine_tune it.

training process

In a couple of minutes with a CPU, we can train our model to be nearly 98% accurate in the predictions.

WOW, that's something that 7 years ago was impossible to get.

You can now test the model by downloading a new image and testing it using:

is_fire, _, probs = learn.predict(PILImage.create("fire.jpg"))

Step 8

What to do next?

Well, try and get better results :)

  • Try choosing a learning rate
  • Explore the timm package
  • Perform a different transformation
  • Do more epochs
  • Plot a confusion matrix and try to figure out what your model is doing wrong

There are infinite ways we can get the model to perform better.

Hope you enjoy my first approximation to FastAI and enjoy this first journey.

I will appreciate it if you give it a clap. Have a nice day! Please feel free to leave a comment if you didn't understand something I will gladly help you out :).

Added a working URL where you can test the model. Give it a try!

LatinX in AI (LXAI) Logo

Do you identify as Latinx and are working in artificial intelligence or know someone who is Latinx and is working in artificial intelligence?

Don’t forget to hit the 👏 below to help support our community — it means a lot! Thank you :)

--

--

Juan Cruz Alric Cortabarria
LatinXinAI

Machine Learning enthusiast, love video games and working out.