Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Convolutional Neural Network: Detect Handwriting

    Why it’s great to master Convolutional Neural Networks (CNN)?

    Mastering Convolutional Neural Networks (CNNs) offers several advantages in the field of computer vision and image recognition:

    1. Image recognition capabilities: CNNs are specifically designed to excel in image classification tasks. They have demonstrated remarkable accuracy and efficiency in recognizing patterns, objects, and features within images, making them a crucial tool for various applications such as autonomous driving, medical imaging, and facial recognition.
    2. Hierarchical feature learning: CNNs employ a hierarchical architecture that automatically learns relevant features at different levels of abstraction. By leveraging convolutional and pooling layers, CNNs can capture local patterns and gradually build complex representations, enabling them to effectively understand and interpret visual information.
    3. Transfer learning potential: CNNs trained on large-scale datasets, such as ImageNet, have learned generic features that can be transferred and fine-tuned for specific tasks with smaller datasets. This transfer learning capability reduces the need for extensive labeled data and accelerates the development of accurate and robust models.

    What will be covered in this tutorial?

    In this tutorial on Convolutional Neural Networks (CNNs), we will cover the following topics:

    • Understanding CNNs: Exploring the fundamental concepts and principles behind Convolutional Neural Networks, including convolutional layers, pooling layers, and the concept of parameter sharing. This foundation will help you grasp the core mechanisms of CNNs and their unique strengths in image analysis.
    • The strength of CNNs: Investigating the key advantages of CNNs in image recognition tasks, such as their ability to capture spatial relationships, handle varying input sizes, and extract meaningful features automatically. Understanding these strengths will reinforce the motivation behind utilizing CNNs for visual recognition tasks.
    • Using CNNs for handwriting detection: Applying CNNs to the task of handwriting detection, where you will learn how to preprocess handwriting images, design and train a CNN model, and evaluate its performance on handwritten character recognition. This hands-on experience will showcase the practical application of CNNs in a real-world scenario.
    • Feature extraction from images: Understanding the concept of feature extraction in CNNs, where you will explore how CNNs learn and extract meaningful features from images. You will gain insights into convolutional operations, pooling operations, and the flattening process, which collectively contribute to the extraction of discriminative image representations.
    • Creating a CNN: Step-by-step guidance on constructing a CNN architecture from scratch, including the configuration of convolutional layers, pooling layers, fully connected layers, and activation functions. You will learn the essential components and design principles necessary to build an effective CNN model.

    By mastering Convolutional Neural Networks and completing this tutorial, you will acquire the knowledge and skills to leverage CNNs for image recognition tasks. This expertise will empower you to effectively detect and classify objects, features, and patterns within images, opening up exciting opportunities in computer vision, artificial intelligence, and various domains that rely on visual data analysis.

    Watch tutorial

    Step 1: What is Computer Vision?

    Computational methods for analyzing and understanding digital images.

    An example could be detecting handwriting.

    Assuming familiarity with Deep Neural Network a naive approach would be to map one pixel to the input network, have some hidden layers, then detect.

    If you are new to Artificial Neural Network or Deep Neural Network.

    A Deep Neural Network could be given for images.

    As follows.

    But actually, we are not (the network) is not interested into any specifics of the pixels in the image. Also, what if the images are moved 1 pixel to the left, then this would influence the network. Hence, this approach seems not to be very good.

    Step 2: What is Image Convolution?

    Image Convolution is applying a filter that adds each pixel value of an image to its neighbors, weighted according to a kernel matrix.

    A few techniques are given here.


    • Reducing the size of an input by sampling from regoins in the input
    • Bascially reducing the size of the image


    • Pooling by choosing the maximum value in each region

    Step 3: What is Convolutional Neural Network (CNN)?

    Convolutional Neural Network (CNN) is a Neural Networks that use convolution for analyzing images.

    Idea of CNN is as follows.

    • We have an input image
    • Apply Convolution – possible several to get several features of the image (feature maps)
    • Apply pooling (this reduces the input)
    • Then flatten it out to traditional network

    Step 4: Handwriting detection with CNN

    We will use the MNIST data base, which is a classical large datasets of handwritten digits.

    Here is the code given below with some comments.

    import tensorflow as tf
    from tensorflow.keras.utils import to_categorical
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
    # https://en.wikipedia.org/wiki/MNIST_database
    mnist = tf.keras.datasets.mnist
    # Read the data
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    # Scale it to values 0 - 1
    x_train = x_train / 255.0
    x_test = x_test / 255.0
    y_train = to_categorical(y_train)
    y_test = to_categorical(y_test)
    x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
    x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
    # Creating a model
    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dense(128, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
    model.fit(x_train, y_train, epochs=10)
    model.evaluate(x_test, y_test)

    Which gives an accuracy on 98%.

    Want to learn more?

    Want to compare your result with a model using PyTorch?

    In the next lesson you will learn to use PyTorch Model to Detect Handwriting.

    This is part of a FREE 10h Machine Learning course with Python.

    • 15 video lessons – which explain Machine Learning concepts, demonstrate models on real data, introduce projects and show a solution (YouTube playlist).
    • 30 JuPyter Notebooks – with the full code and explanation from the lectures and projects (GitHub).
    • 15 projects – with step guides to help you structure your solutions and solution explained in the end of video lessons (GitHub).

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment