Here is a visual representation of Summer’s normal day:
She loves playing outside in the chilly Canadian weather. Cut scene:
Clearly, she’s having fun.
She also really likes long walks. And by long walks, I mean being carried or put in a stroller.
Summer is 5 pounds overweight and life couldn’t be better for her. Summer is out of this world.
In actuality, when I first got summer, I was told she was a dog. I was also told she will require long walks and lots of ball games. The breeder said Summer will be able to do simple skills like roll over, shake a paw and lay down within a month.
None of this came true. Summer hates walks, she demands to be carried. She could not care less about fetch. And summer was never a fan of rolling over, shaking paws and laying down, instructions aren’t her thing.
Summer likes adventure and exploration. She likes to find very interesting places to nap all around my house. Filing cabinet? Bathtub? Table? Sounds like a bed to Summer.
She is literally the opposite of a dog. She often behaves like an average cat.
I’ve disregarded this issue for a while now.
But recently, I’ve gotten the sudden urge to solve this problem. Maybe the “dog” breeder lied and actually sold me a cat. Time to find out. I wouldn’t return Summer, of course, I’m just want to crack this case.
Is Summer a cat or dog?
As the primary detective on this case, I am personally undecided.
So I decided to ask my side investigators, my mother and father. With less detective training than me (that’s why they’re my side investigators…), unsurprisingly, they weren’t able to come to a consensus either.
My brother was useless as well. We’re out of family members.
Unfortunately, all my detective school friends were busy with less important cases.
It‘s just me, summer and my computer. Solving a massive mystery. Since I don’t know the verdict, and summer certainly doesn’t know, we felt like we were out of options.
But then… I got an idea.
I could use my last resource, my computer, to create a machine learning cat-dog classifier.
I needed to create my own specialist detective since none of my asshole detective school friends were willing to help.
So I did exactly that. Using Machine Learning. Neural Networks. Artificial Intelligence. (All that fun jazz).
Creating a Cat-Dog Classifier, to diagnose my catdog
Using python, I coded the greatest cat-dog classifier of all time. You’ll need to read till the end to find out if Summer’s a cat or a dog.
I promise I’ll keep you on edge.
Now, let’s grab some *healthy snacks*(A cat-dog classifier was already a demanding task, so I’m not going to endorse eating sugar because I don’t want to build an “is-sugar-killing-you” classifier) and get cracking.
What am I even going to do?
I’m going to be using a Convolutional Neural Network to create a machine learning model.
Don’t know what a CNN is? Read here.
Don’t want to read? I’ll summarize.
A CNN is a type of neural network that takes in images as inputs and learns about different features in the images and based on the image features it determines an output. In our case, the output is either cats or dogs.
The neural network is made up of many layers, each of these layers plays a role in understanding different portions of the image. One layer might work to classify edges, another might work to classify fur thickness.
The network studies these images an tries to best learn what is a cat and what is a dog. CNNs also have a set of weights which determine how important a feature is. First, these weights are randomized, but as we feed the model data (pics) it does three things:
- Predict if an image was a cat or dog
- Calculate the error of the prediction
- Train itself to better predict
And we just repeat that many many many times.
First, the model will predict whether an image input is a cat or a dog.
Then it will test how wrong it was by reading the label, which will result in adjusting the weights in the system.
When the model adjusts the weights, it will take in more images, predict their label. Based on the error, the model will adjust the weights “train itself” etc.
Stuff I imported for the Model
Well, First I imported the necessary snacks into my oral cavity, which entered my esophagus and now they’re chilling in my stomach (you should never develop machine learning on an empty stomach).
Then I imported all the necessary packages.
(If you care) The packages I used were:
- cv2 (to help resize the images)
- numpy (to deal with arrays)
- os (to deal with directories)
- Shuffle from random (to shuffle images during training)
- tqdm (nice, pretty, percentage bar to use during tasks like training)
- tflearn (deep learning* library built on top of tensorflow)
*deep learning = a neural network with many layers
Maybe this picture helps?
I created a deep CNN → a network with many layers to improve the accuracy of my model.
All I did was import and install different packages to help the program run.
A package is kinda like a present with a bunch of really useful functions a program can use. When we import these packages, it’s like giving the program some presents. The program can use the stuff within the presents “packages” to do things the program normally can’t do.
→ All you need to know is that whatever we’re importing is helping my computer diagnose Summer’s species.
Let’s prepare the model
I started off by downloading a BUNCH of pictures of cats and dogs. I’m talking thousands. 25,000.
With these (really freaking cute) pics, we can train my computer to become a pro at classifying Cats and Dogs.
I took some of my cat and dog pictures (25,000) and labelled them (like cat.1.jpg, cat.2.jpg, dog.1.jpg, dog.2.jpg… etc, time-consuming, I know. ) This is what I considered my training set.
Then I took a bunch of cat and dog pics (12,500) and set these aside to be my testing pics. These got no labels (1.jpg,2.jpg,3.jpg). Guess what I called this? It’s my testing set.
Do we understand so far??
So here’s how this works:
- We have a training and testing set.
- We start with the training pics and allow the machine to guess if an image is a cat or dog, and over time it just gets better at figuring out what features a cat has and what features a dog has.
- Then we use the testing data to measure how accurate the machine is at classifying cats and dogs
- When it’s accurate enough to please us, we’ll do the big test.
I mean, this is really how humans learn IRL. We learn from experiences and tweak around our tactics based on experiences.
Let’s think of babies. The getup, start walking, crash into a wall, fall down. And then they get back up and try again.
Us Humans learn from experiencing situations and getting better at dealing with them, and so can machines like my computer. Hence the name “machine learning”.
One of our biggest problems in the western world is getting these damn 15, 16 and 17-year-olds off of snapchat and having them do stuff that matters. You’d think since this is such a reoccurring issue we would learn and find a solution… haha nope.
Since we can’t teach every single teenager to deal with problems that actually matter, let’s focus on training my computer to classify cats and dogs.
My computer basically trains on its own (after I’ve written the base code), so while it’s training itself to become a pro-cat-dog classifier, me and summer snuggled up and did some fun stuff.
Ok, enough pictures of my cat-dog.
Here’s what I did:
- imported all the necessary packages
- connected my computer to my data set (training and testing)
- Wrote code to teach my computer how to read labels
- Wrote code to teach my computer how to iterate through my training/testing data
- Wrote code for a convolutional neural network
CNN code looks like this (also, don’t worry, I’ll have a vid out soon explaining the code!!)
Next, I grabbed all my dog and cat training photos and loaded them.
Then it’s time for the magic.
I trained my model on my training set of images.
Guess what I did? Tested the accuracy of my model.
Once I was satisfied with the scores of my model, it was time to plug in a picture of summer.
Real deal time. Get your snacks ready. Drum roll, please.
And the verdict is…
According to the handy dandy CNN model, with 93% accuracy, the machine predicted summer was in fact a…
Kind of surprising, but I guess I just have a really weird dog.
S/O to AI for solving this massive mystery.