from fastai.vision.all import *

This notebook was created inside of Kaggle using the animal-faces dataset, the steps apply to any database, you’ll just have to modify the paths and labeling functions.

Now that we’ve downloaded our dataset, let’s take a look at its structure:

Our training dataset is inside of afhq/train, so we’ll copy that path, and use get_image_files to grab all the images from our subfolders:

Let’s take a look at how many available image files we have:

files = get_image_files("../input/animal-faces/afhq/train")
len(files)
>>> 14630

Next, we need to create our labeling for each image, so that our model can correct train and categorize each one.

To do this, let’s look at the filenames that we have:

“flickr_cat_000002.jpg”

What we want is the “cat” part of the filename, and so we can use regex to grab just this part:

pat = r'^.*_(.*)_\d+.jpg'

This regex will grab everything after the first ”_” and before the second ”_”

Because we use regex so often when defining labels, there’s already a built in method to grab labels using it:

path = "../input/animal-faces/afhq/train"
dls = ImageDataLoaders.from_name_re(path, files, pat, item_tfms=Resize(224))

Now we can use show_batch() to verify that we’re correct:

dls.show_batch()

Now, let’s create our learner using resnet34 and fine_tune() it:

learn = vision_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)

Once we’re done, we can show our results:

learn.show_results()