Tutorial on Image Classification Using Keras

    Have you ever thought about how we classify and recognize images and objects around us and if you are interested to implement such mechanism in computer then this post is for you. I’ll try to make this post simple as possible. I’ll use Keras to implement image classification.

    How baby recognize the things around them

    When a baby is born he/she don’t have any prior knowledge about things around them. After certain time they recognize their parents but they don’t recognize at first because human brain needs data to extract information (learn).

    Baby Feeding

    Parents tries to play with baby, calls them with their names, feeds them with baby food and take care of them. When parents appears multiple times in front of baby then human brain extract features such as structure of eye, ear, nose,  mouth and color such as black hair, mustache, etc. as well as facial expression.

    Baby Diaper Change

    After multiple encounters baby recognize their parents but not unknown person because baby brain tries to match extracted information from person to stored information of parents.

    Let’s take another example when baby encounters family pet dog.

    When baby encounters family pet dog for the first time, baby tries to avoid or terrifies. But after successful encounter or when baby is familiar to family dog then baby play with dog.

    Baby with family pet dog

    A few weeks later if baby relatives brings her dog with them, then baby still tries to play with dog.

    Baby hasn’t seen this dog before, still baby tries to play with dog because baby recognize similar features such as body structure, color, ears, walking on 4 legs, long tongue, etc.

    This learning method is known as Image Classification. Image classification can be described as the process of computer vision that can classify or group an image according to its visual context.

    Image classification is an example of supervised learning.

    You can read more about Machine learning in our previous post.

    Machine learning requires three things: –

    • Input data
    • Expected output
    • A way to measure whether our algorithm is doing well or not so that learning can be improved.

    Machine learning algorithms tries to find patterns in input data so that it can be classified as expected output.

    A decade ago programmer used to write hard code to classify images.

    For example to classify between apple and orange, programmer had to write all codes in if else condition such as if the fruit is small and red then it is apple else orange.

     We don’t have to hard code all the features required to classify image. Now machine will identify all features by themselves.

    Image Classification using keras on Fashion Mnist

    In this post we are going to use keras for image classification on fashion mnist.

    Fashion-MNIST is a dataset of Zalando’s article image consisting a training set of 60,000 examples and test set of 10,000 example.

    Fashion-Mnist Sample

    These dataset images are labeled into following categories

    • T-shirt/top
    • Trouser
    • Pullover
    • Dress
    • Coat
    • Sandal
    • Shirt
    • Sneaker
    • Bag
    • Ankle boot

    Preparing datasets for training and testing

    When we have large chuck of data for machine learning then we need to divide data into two parts. One for training and one for testing.

    Training data are responsible to learning purpose as algorithms tries to extract features and learn patterns in data.

    Test data are responsible to tell algorithm that model is correctly classifying data.

    Larger the training data machine will learn quicker and larger the test data machine will learn with high confidence.

    If we have millions of data we can separate 10-20% for test data if not then we must perform cross validation.

    Imp: – if you got 100% accuracy of model on test data double check the dataset because you might be training your model in test data. I’ve faced several times.

    Before we begin to code; you need to install following software package on your machine

    • Python 3
    • Tensorflow
    • Keras
    • Jupyter Noteboook (Optional)

    Code part

    I’m familiar to keras so I’ll be using keras to complete image classification example.

    Step 1: Import necessary modules

    Step 2: Get data and split data into train sets and test sets

    Step 3: Data processing

    Fashion-Mnist contains 60,000 grayscale image sets with 28*28 pixels i.e. tensor of length 784 (28*28). Each pixel values ranges from 0 to 255.

    In computer vision 0 represents perfect black and 255 perfect white. When we go from 0 to above color gets lighter.

    We need to normalized image tensor as processing bigger number will be computationally costly. We divide each pixel by 255 so that new pixel values will ranges from 0 to 1.

    Step 4: Model Formation

    We will build simple sequential model. It will have 784 neurons on input layer as we have input tensor of 28*28 size and 10 neurons on output layers as fashion-mnist is categorized on 10 category.

    It will also have 64 neurons on hidden layers.

    Model Summary

    It helps us to know model details about shape of input and output

    Step 5: Model Compile

    We have used adam optimizer for optimizing model with SparseCategoricalCrossentropy loss function. Read Keras Optimizer and Loss function documentation for more information.

    Step 6: Train the model

    Step 7: Model Evaluation

    You can copy paste following code in your code editor and find the output as above.

    Complete code

    I’ve tried to make this post simple as possible.

    Write down comment if you think I need to cover basic of machine learning using keras.

    Please let me know if you find any difficulty in following this post.


    Recent Articles


    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Get new notification

    Subscribe to our newsletter

    To be updated with all the latest news, offers and special announcements.