# My first CNN: Is it a dog or a fox?

I've been fascinated by ML & deep learning for a while now, so I started going through the [fast.ai course](https://course.fast.ai/). In chapter 2, the example was a CNN that could tell different types of bears apart. I thought I'd try something bigger: one model that could recognize all the animals in my dataset.

I got a [dataset from Kaggle](https://www.kaggle.com/datasets/iamsouravbanerjee/animal-image-dataset-90-different-animals) with 90 different animals and started training. The error rate was high. I opened the confusion matrix to see which animals it was mixing up, but with 90 rows and 90 columns, I had no idea where to start.

So I figured, let's keep it simple. Can it tell a dog from a fox? The photos were already in folders named after each animal, so I just picked those two:

```python
from fastai.vision.all import *

labels = ['dog', 'fox']

def get_animal_images(path):
    return [p for p in get_image_files(path) if p.parent.name in labels]
```

That gave me 120 photos. I kept 24 aside to see how the model did. When I looked through the rest, I found copies of photos I'd set aside. Two files were even shots of the same fox on the same fence. Different filenames, same fox.

I removed 18 training photos and had 78 left. Like in the bear example, I started with a pretrained ResNet-18 and fine-tuned it on my photos. Here's the training code from [my Kaggle notebook](https://www.kaggle.com/code/rahulmfg/dog-vs-fox?scriptVersionId=365648934):

```python
set_seed(42, reproducible=True)
clean_dls = ImageDataLoaders.from_folder(
    work, train='train', valid='valid',
    item_tfms=RandomResizedCrop(224, min_scale=0.5),
    batch_tfms=aug_transforms(),
)
clean_learn = vision_learner(clean_dls, resnet18, metrics=error_rate)
clean_learn.fine_tune(4)
```

And boom, I had my first classifier. It got **22 of the 24 photos right**, about 92% validation accuracy. Before I removed the copies, it got 23 right. I trust the 22 more.

The cover shows what happened when I tried the fox example. [Try it with your own dog or fox photo](https://huggingface.co/spaces/rahulmfg/dog_vs_fox) and see what it says.
