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 most hot topics around the world. Well, it can even be said as 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 the large amount of data. To know more about Machine learning and its algorithms you can refer to some links that is provided in the Reference sections of this article.
Today, we will create a Image Classifier of our own which 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 is used for Image Classification i.e. Convolutional Neural Network(or CNN).
So basically what is CNN – as we know its 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 fed to the machine. Since its not an article explaining the CNN so I’ll add some links in the end if you guys are interested 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 image given there during training the data set. To do so we can see that name of each image of 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 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 designing 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 convenience and play with the basic hyperparameters like epoch, learning rate, etc to improve the accuracy. I have converted the image to grayscale so that we will only have to deal with 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 or 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 though 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 :
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- R-CNN vs Fast R-CNN vs Faster R-CNN | ML
- Project Idea | Cat vs Dog Image Classifier using CNN implemented using Keras
- ML | Training Image Classifier using Tensorflow Object Detection API
- CNN - Image data pre-processing with generators
- ML | Implementation of KNN classifier using Sklearn
- ML | Voting Classifier using Sklearn
- IBM HR Analytics on Employee Attrition & Performance using Random Forest Classifier
- Random Forest Classifier using Scikit-learn
- CNN | Introduction to Padding
- CNN | Introduction to Pooling Layer
- Selective Search for Object Detection | R-CNN
- VGG-16 | CNN model
- R-CNN | Region Based CNNs
- Mask R-CNN | ML
- Fast R-CNN | ML
- Faster R-CNN | ML
- Understanding GoogLeNet Model - CNN Architecture
- Deploying a TensorFlow 2.1 CNN model on the web with Flask
- Visualizing representations of Outputs/Activations of each CNN layer
- Difference between ANN, CNN and RNN
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mathemagic