Not Hotdog App with fast.ai
In this post we will be using fast.ai to recreate the "Not Hotdog" app featured in the tv show "Silicon Valley". The fastai library simplifies training fast and accurate neural nets using modern best practices.
The code for this project can be found here.
Imports
from fastai.vision import *
from google_images_download import google_images_download
fasai handles all the dependencies for you. You simply call from fastai.vision import *
and that is it. I also imported a library called google_images_download
which I will use next to download our imageset from google images.
Data Preparation
! googleimagesdownload -cf google_config.json
For our Not Hotdog app to work we are going to need to train it with images of hotdogs and "not hotdogs". To get the images we use the library googleimagesdownload
. The json file above are parameters for the library. We are essentially telling it to Query Google Images once for "hotdog" and download the first 100 images into a folder called "images/hotdog" and then again for "random pictures" and store them in a folder called "images/not_hotdog".
We execute the code by calling ! googleimagesdownload -cf google_config.json
We now have the images we need for our deep learning model.
path = Path('images')
data = ImageDataBunch.from_folder(path,
train=['hotdog', 'not_hotdog'],
valid_pct=0.2,
ds_tfms=get_transforms(),
size=224,
num_workers=0).normalize(imagenet_stats)
fastai uses a "Learner" object to train a model. In order to establish a learner with your data you first need to create a "ImageDataBunch" object.
Above we:
- Set the path for the location of the image folders
- Provide the names of the two folders we are labeling
- Create a validation set from out images using 20% of them as validation
- Transform the Images
- Set the size of the Images
- Normalize the Data
data.show_batch(rows=3, figsize=(7,8))
print(
f'Labels: {data.classes}\n'
f'Training Set Size: {len(data.train_ds)}\n'
f'Validation Set Size: {len(data.valid_ds)}'
)
Now that we have our databunch created data
we can preview our transformed / normalized images. We can also access the Labels that we are trying to predict as well as the train and validation sizes.
Training
learn = cnn_learner(data, models.resnet34, metrics=error_rate, callback_fns=ShowGraph)
learn.fit_one_cycle(4)