Star Wars Classifier
Buiding a star wars character classifier using fastai
Star Wars Classifier
Table of Contents:
- Downloads
- Packages
- Pre-model building
- 3.1 Create folder
- 3.2 Scrape images
- 3.3 Move images
- Data Loaders
- Model Building
- 5.1 Training
- 5.2 Prediction
!pip install icrawler
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
# icrawler
from icrawler.builtin import GoogleImageCrawler
# fastai
from fastai import *
from fastai.vision import *
from fastai.imports import *
from fastai.vision.all import *
# widgets
import ipywidgets as widgets
# ignore warnings
import warnings
warnings.filterwarnings("ignore")
!mkdir yoda
!mkdir luke
!mkdir wookie
3.2 Scrape images
The way icrawler
works is that it creates a folder named /images
from wherever the command is run. So right now we are in the /kaggle/working
directory. Now I'll be going to each directory one at a time and run the crawler to download the images in the /images
folder. So the structure of the folders will be something like
/kaggle/working/yoda/images
/kaggle/working/luke/images
/kaggle/working/wookie/images
After the download, I'll be moving the images from the images folder of each respective label to the label folder itself - so for example from /kaggle/working/yoda/images
to /kaggle/working/yoda
and I'll be deleting all the empty images folder.
If you would like to reproduce the exact same thing, run the command in console first followed by the command in the notebook and so on in the provided order which is as follows.
Run the following in console
After above command, run the following cell
google_crawler = GoogleImageCrawler()
google_crawler.crawl(keyword='baby yoda', max_num=50)
Run the following in console one line at a time
cd luke
After above command, run the following cell
google_crawler = GoogleImageCrawler()
google_crawler.crawl(keyword='luke skywalker', max_num=50)
Run the following in console one line at a time
cd wookie
After above command, run the following cell
google_crawler = GoogleImageCrawler()
google_crawler.crawl(keyword='wookie', max_num=50)
3.3 Move images
Run the following in console one line at a time
Now I'll be moving the images from /images
folders to their respective labels and deleting the /images
folder.
Run the following in console one line at a time
mv -v luke/images/* luke
mv -v wookie/images/* wookie
rmdir yoda/images
rmdir luke/images
rmdir wookie/images
Once done, you may check run pwd
in console to check your current working directory. It must show /kaggle/working
.
path = Path('/kaggle/working/yoda')
dls = ImageDataLoaders.from_folder(path, valid_pct=0.5, batch_size=10, item_tfms=Resize(224))
dls.valid.show_batch(max_n=4, nrows=1)
characters = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=Resize(256))
# Creating the dataloader
path = Path('/kaggle/working')
dls = characters.dataloaders(path)
# checking the images
dls.valid.show_batch(max_n=18, nrows=3)
learn = cnn_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(4)
uploader = widgets.FileUpload()
uploader
def helper():
img = PILImage.create(uploader.data[0])
img.show()
pred,pred_idx,probs = learn.predict(img)
lbl_pred = widgets.Label()
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
print(lbl_pred)
helper()
helper()
helper()