Contents

Getting started with fastai by creating a simple football player classifier

Introduction

This is part of fast.ai course: Practical Deep Learning for Coders. This was done at the end of first lecture. I got insights into how things work with fastai.

In this work, I decided to make a simple football player classifier using the concepts taught. Pretrained weights from Resnet18 were used and fine tuning was done to suit this particular use case.

Let’s jump into it…

Code

!pip install -Uqq fastai duckduckgo_search

In the above cell, we are installing and upgrading fastai and duckduckgo libraries.

 

# importing libraries
from duckduckgo_search import ddg_images
from fastcore.all import *

 

# creating a function to retrieve images from duckduckgo
def get_images(term, max_images=30):
    print(f'getting images of {term}')
    return L(ddg_images(term,max_results=max_images)).itemgot('image')

We have created a function to to get images of term and if the number of images to retrieve is not specified, we will get 30 images from duckduckgo.

 

from fastdownload import download_url
from fastai.vision.all import *
from time import sleep

# Images which we want to retireve
sources=['Messi','Cristiano Ronaldo','Neymar','Benzema','Robert Lewandowski']
path=Path('players')

# Downloading source images and putting them in respective folders
# We are using sleep here because we don't want to over burden duckduckgo
for source in sources:
    
    dest=(path/source)
    
    dest.mkdir(exist_ok=True,parents=True)
    
    download_images(dest,urls=get_images(f'{source} photos'))
    
    sleep(10)
    
    download_images(dest,urls=get_images(f'{source} match photos'))
    
    sleep(10)
    
    download_images(dest,urls=get_images(f'{source} young photos'))
    
    resize_images(path/source, max_size=400, dest=path/source)
getting images of Messi photos
getting images of Messi match photos
getting images of Messi young photos
getting images of Cristiano Ronaldo photos
getting images of Cristiano Ronaldo match photos
getting images of Cristiano Ronaldo young photos
getting images of Neymar photos
getting images of Neymar match photos
getting images of Neymar young photos
getting images of Benzema photos
getting images of Benzema match photos
getting images of Benzema young photos
getting images of Robert Lewandowski photos
getting images of Robert Lewandowski match photos
getting images of Robert Lewandowski young photos

 


# Downloading an image to test
# Chances are that this image might already be present in the data which we intially gathered
download_url(get_images('Cristino Ronaldo award',max_images=1)[0],'ronaldo.jpg')
im=Image.open('ronaldo.jpg')
im.to_thumb(256,256)
getting images of Cristino Ronaldo award

/ronaldo.png

 

# Verifying if we have properly downloaded images and deleting those which have not downloaded properly
failed_images=verify_images(get_image_files(path))
failed_images.map(Path.unlink)
len(failed_images)
0

 

# Creating a datablock
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(192,method='squish')]
).dataloaders(path,bs=32)

dls.show_batch(max_n=10)

/batch.png

 

# Using resnet18 model and finetuning to our dataset
learn=vision_learner(dls,resnet18,metrics=error_rate)
learn.fine_tune(5)

 

# Predicting on our test image
player,_,proba=learn.predict(PILImage.create('ronaldo.jpg'))
print(f'This is {player}')
print(f'Probability is {proba[1]}')
This is Cristiano Ronaldo
Probability is 0.7899865508079529

Conclusion

This was done as part of an introduction to fastai library and is a result of lecture one learnings from Practical Deep Learning for Coders