Machine Learning Basics
Machine Learning Basics
Machine Learning Basics
K nearest neighbor
Decision tree
This article assumes that you are interested in the technical know-how of machine
learning, image classification in particular!
1 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
assification has been an important task within the field of computer vision.
OpenGenus IQ: Learn Computer Science
mage classification refers to the labeling of images into one of a number of predefined classes.
here are potentially n number of classes in which a given image can be classified. Manually checking and classifying images could be
tedious task especially when they are massive in number (say 10,000) and therefore it will be very useful if we could automate this
ntire process using computer vision.
The advancements in the field of autonomous driving also serve as a great example of the
use of image classification in the real-world. For example, we can build an image
classification model that recognizes various objects, such as other vehicles, pedestrians,
traffic lights, and signposts on the road.
2 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Now that we have a fair idea of what image classification comprises of, let’s start analyzing
the image classification pipeline.
3. Feature extraction and Training- This is a crucial step wherein statistical or deep
learning methods are used to identify the most interesting patterns of the image,
features that might be unique to a particular class and that will, later on, help the
model to differentiate between different classes. This process where the model
learns the features from the dataset is called model training.
Let’s discuss the most crucial step which is image preprocessing, in detail!
Image Pre-processing
Pre-processing is a common name for operations with images at the lowest level of
abstraction — both input and output are intensity images.
3 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Read image
Resize image
Data Augmentation
Reflection
Gaussian Blurring
Histogram Equalization
Rotation
Translation
Step 1
Reading Image
In this step, we simply store the path to our image dataset into a variable and then we
create a function to load folders containing images into arrays so that computers can deal
with it.
4 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Step 2.
Resize image
Some images captured by a camera and fed to our AI algorithm vary in size, therefore, we
should establish a base size for all images fed into our AI algorithms by resizing them.
5 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
1. Gray Scaling
The image will be converted to gray scale (range of gray shades from white to
black) the computer will assign each pixel a value based on how dark it is. All the
numbers are put into an array and the computer does computations on that array.
Sample code to convert an RGB(3 channels) image into a Gray scale image:
RGB Image
6 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Grayscale Image:
2. Reflection/Flip
You can flip images horizontally and vertically. Some frameworks do not provide
function for vertical flips. But, a vertical flip is equivalent to rotating an image by 180
7 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
3. Gaussian Blurring
Gaussian blur (also known as Gaussian smoothing) is the result of blurring an
image by a Gaussian function. It is a widely used effect in graphics software,
typically to reduce image noise.
Sample Code:
8 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
4. Histogram Equalization
Histogram equalization is another image processing technique to increase global
contrast of an image using the image intensity histogram. This method needs no
parameter, but it sometimes results in an unnatural looking image.
Sample Code
9 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
10 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
5. Rotation
This is yet another image augmentation technique. Rotating an image might not
preserve its original dimensions (depending on what angle you choose to rotate it
with )
Sample Code
11 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
6. Translation
Translation just involves moving the image along the X or Y direction (or both).
This method of augmentation is very useful as most objects can be located at
almost anywhere in the image. This forces our feature extractor to look everywhere.
Sample Code
To support their performance analysis, the results from an Image classification task used
to differentiate lymphoblastic leukemia cells from non-lymphoblastic ones have been
provided. The features have been extracted using a convolutional neural network, which
will also be discussed as one of our classifiers. This is because deep learning models
have achieved state of the art results in the feature extraction process.
12 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Different classifiers are then added on top of this feature extractor to classify images.
OpenGenus IQ: Learn Computer Science
Linear Kernel
Gaussian Kernel
Polynomial Kernel
Code Snippet:
13 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
OpenGenus
This isIQ:
theLearn
baseComputer Science
model/feature extractor using Convolutional Neural Network, using Keras
with Tensorflow backend
2. Decision Trees
It is also a supervised machine learning algorithm, which at its core is the tree data
structure only, using a couple of if/else statements on the features selected.
Decision trees are based on a hierarchical rule-based method and permits the acceptance
and rejection of class labels at each intermediary stage/level.
14 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Code
Feature Extractor
15 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
3. K Nearest Neighbor
The k-nearest neighbor is by far the most simple machine learning algorithm.
This algorithm simply relies on the distance between feature vectors and classifies
unknown data points by finding the most common class among the k-closest examples.
16 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Here we can see there are two categories of images and that each of the data points
within each respective category are grouped relatively close together in an n-dimensional
space.
In order to apply the k-nearest Neighbor classification, we need to define a distance metric
or similarity function. Common choices include the Euclidean distance and Manhattan
distance
Code
Base Model/feature extractor
KNN classifier
17 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
18 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
The hidden layers can be thought of as individual feature detectors, recognizing more and
more complex patterns in the data as it is propagated throughout the network. For
example, if the network is given a task to recognize a face, the first hidden layer might act
as a line detector, the second hidden takes these lines as input and puts them together to
form a nose, the third hidden layer takes the nose and matches it with an eye and so on,
until finally the whole face is constructed. This hierarchy enables the network to eventually
recognize very complex objects.
Code
ANN as feature extractor using softmax classifier
19 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
5. Convolutional Neural
Networks
Convolutional neural networks (CNN) is a special architecture of artificial neural networks.
CNNs uses some of its features of visual cortex and have therefore achieved state of the
art results in computer vision tasks.
Convolutional neural networks are comprised of two very simple elements, namely
convolutional layers and pooling layers.
Although simple, there are near-infinite ways to arrange these layers for a given computer
vision problem.
20 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
The elements of a convolutional neural network, such as convolutional and pooling layers,
OpenGenus IQ: Learn Computer Science
are relatively straightforward to understand.
The challenging part of using convolutional neural networks in practice is how to design
model architectures that best use these simple elements.
Code
CNN as feature extractor using softmax classifier
21 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
Performance evaluation
CLASSIFIER ACCURACY PRECISION RECALL ROC
Conclusion
We can conclude from the performance table, that Convolutional Neural networks deliver
the best results in computer vision tasks.
OpenGenus Foundation
22 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
23 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
24 of 25 10/18/2020, 4:25 PM
Basics of Machine Learning Image Classification Techniques https://iq.opengenus.org/basics-of-machine-learning-image-classification...
25 of 25 10/18/2020, 4:25 PM