Machine learning is currently one of the hottest topics in the world. Well, it could even be said to be the new electricity in the modern world. But to be precise, what is machine learning, then this is just one of the ways to teach a machine, providing a large amount of data. To learn more about machine learning and its algorithms, you can refer to some of the links provided in the reference sections of this article.
Today we will create a custom image classifier that can distinguish if a given snapshot is a dog or a cat or something else, depending on your data. To achieve our goal, we will use one of the well-known machine learning algorithms that is used to classify images, i.e. Convolutional Neural Network (or CNN).
So basically what is CNN — since we know its machine learning algorithm for machines to understand the features of the image with foresight and memorize functions to guess if the name of the new image is being fed to the machine. Since this is not an article explaining CNN, so I’ll add some links at the end if you guys are curious about how CNN works and behaves.
So, after going through all these links, let’s see how to create our own cat-vs-dog image classifier. For the dataset, we will use the kaggle cat-vs-dog dataset:
Now after getting the dataset , we need to preprocess the data a bit and provide labels to each image specified there while training the dataset. To do this, we can see that the name of each image of the training dataset starts with "cat" or "dog", so we will use that to our advantage and then 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]
Required libraries:
- TFLearn — a deep learning library with a high-level API for TensorFlow used to create our CNN layers
- tqdm — Instantly have your loops display a smart progress bar, just for design convenience
- numpy — handle image matrices
- open-cv — for image processing such as converting them to grayscale, etc.
- os — To access the file system to read the train image and check the catalog from our machines
- random — shuffle the data to overcome the bias
- matplotlib — to display the result of our predictive result.
- flow tensor — just use a tensor board to compare the loss and Adam’s curve with our result data or the resulting log.
TRAIN_DIR and TEST_DIR should be set according to user experience and play with basic hyperparameters like epoch, learning speed, etc. to improve accuracy. I converted the image to grayscale so that we only have to deal with the 2nd matrix, otherwise the 3D matrix is difficult to apply directly to CNN, especially not recommended for beginners. Below is the code which is carefully commented or you can find it here on my GitHub account at this link .
|
The output image will not be very sharp as the entire image is scaled down to 50X50 so that the machine can process quickly despite the tradeoff between speed and loss.
And to access the tensorboard use the following command in your cmd (windows user)
tensorboard --logdir = foo: C: UsersknapseckDesktopDevCov_Netlog
Output:
Links Links for beginners to machine learning:
- Python.Engineering Machine Learning
- Siraj Raval — YouTube
- Andrew Ng’s Machine Learning Course on Coursera
- Machine Learning: Kevin Murphy’s Probabilistic Approach
- Reddit Community for Machine Learning
Links Links For CNN:
- Jupyter Notebook — Conv_Net
- Wikipedia — Convolutional Neural Networks
- Stanford Course — cs231n