Image Classifier using CNN
The article is about creating an Image classifier for identifying cat-vs-dogs using TFLearn in Python. The problem is here hosted on kaggle.
Machine Learning is now one of the hottest topics around the world. Well, it can even be said of the new electricity in today’s world. But to be precise what is Machine Learning, well it’s just one way of teaching the machine by feeding a large amount of data. To know more about Machine learning and its algorithms you can refer to some links that are provided in the Reference sections of this article.
Today, we will create an Image Classifier of our own that can distinguish whether a given pic is of a dog or cat or something else depending upon your fed data. To achieve our goal, we will use one of the famous machine learning algorithms out there which are used for Image Classification i.e. Convolutional Neural Network(or CNN).
So basically what is CNN – as we know it’s a machine learning algorithm for machines to understand the features of the image with foresight and remember the features to guess whether the name of the new image is fed to the machine. Since it’s not an article explaining CNN so I’ll add some links in the end if you guys are interested in how CNN works and behaves.
So after going through all those links let us see how to create our very own cat-vs-dog image classifier. For the dataset we will use the Kaggle dataset of cat-vs-dog:
Now after getting the data set, we need to preprocess the data a bit and provide labels to each of the images given there during training the data set. To do so we can see that name of each image of the training data set is either start with “cat” or “dog” so we will use that to our advantage then we use one hot encoder for the machine to understand the labels(cat[1, 0] or dog[0, 1]).
def label_img(img): word_label = img.split('.')[-3] # DIY One hot encoder if word_label == 'cat': return [1, 0] elif word_label == 'dog': return [0, 1]
Libraries Required :
- TFLearn – Deep learning library featuring a higher-level API for TensorFlow used to create layers of our CNN
- tqdm – Instantly make your loops show a smart progress meter, just for simple design sake
- numpy – To process the image matrices
- open-cv – To process the image like converting them to grayscale and etc.
- os – To access the file system to read the image from the train and test directory from our machines
- random – To shuffle the data to overcome the biasing
- matplotlib – To display the result of our predictive outcome.
- TensorFlow – Just to use the tensorboard to compare the loss and adam curve our result data or obtained log.
TRAIN_DIR and TEST_DIR should be set according to the user’s convenience and play with the basic hyperparameters like an epoch, learning rate, etc to improve the accuracy. I have converted the image to grayscale so that we will only have to deal with a 2-d matrix otherwise 3-d matrix is tough to directly apply CNN to, especially not recommended for beginners. Below here is the code which is heavily commented on otherwise you can find the code here in my GitHub account from this link.
The output image will not be very clear since all the image is reduced to 50X50 for a machine to process fast through the tradeoff between speed and loss.
And to access the tensorboard use the following command in your cmd(Windows user).
Reference Links for beginner to Machine Learning:
- Machine Learning GeeksforGeeks
- Siraj Raval – YouTube
- Andrew Ng Machine Learning Course on Coursera
- Machine Learning : A probabilistic Approach by Kevin Murphy
- Reddit community for Machine Learning.
Reference Links for CNN :