Use TensorFlow to create your own handwriting recognition engine

Source: Internet
Author: User
Tags python script

This article is the original translation of the Union, reproduced please indicate the source for the "several league community."

This article describes an easy way to create your own handwriting recognition engine using TensorFlow. The project shown here as an example.

Complete source code can log in GitHub https://github.com/niektemme/tensorflow-mnist-predict/

Introduced

I'm doing a piece of machine learning article writing. It's hard to ignore TensorFlow, a deep learning engine by Google Open source. Depth learning is a branch of machine learning that uses the neural network in the concept of the human brain to solve various problems, such as image and speech recognition (picture 1). This is a difficult problem to solve with "traditional" computers: use a computer as a large calculator.

–image 1:deep Neural Network (source:google)

The fact that TensorFlow was created by Google gives it a lot of traction, especially in the Web technology I use. To learn more about TensorFlow, I attended the local "coffee and coding" gathering in Amsterdam, where they hosted "hands-on involvement in TensorFlow" activities.

In the party we try to experience from the TensorFlow website tutorial. The tutorial itself is clear and well written. It seems to me that these examples are mostly focused on building and validating models, but using the created model is not a priority. An exception is an example of "image recognition". But one of the more complicated examples is that if you're not a machine-learning expert, it's harder to use.

By searching the internet, and perhaps even using some kind of AI from the same company that created TensorFlow, I found that more people were trying to find out how to apply the created model to solving practical problems.

So I set the goal to be more convenient to use the TensorFlow mnist tutorial on handwriting recognition by using the training model.

Goal

The goal of the project is to get my computer to identify one of my own handwritten numbers by using the training model of the Mnist dataset. The Mnist dataset contains a large number of handwritten digits and corresponding labels (the correct number).

This gives you the following tasks:

L train a model with mnist data sets.

L Save the model in step 1 to the file.

L loads the model saved through a different Python script.

L Prepare and load the image of my handwritten handwriting.

L correctly predict the numbers I write.

1. Training a model with Mnist data sets

In the first two tutorials to build a tensorflow.org Web site, there has been a clear explanation of how to train a model. I did not modify any of these examples.

As expected, the model created in the second (expert) tutorial has a good effect on predicting my handwritten numbers correctly.

2. Save the Model

Saving the model is actually very easy. Detailed descriptions have been made in the documentation for TensorFlow saving and restoring variables.

It boils down to adding two lines of code to the Python script explained in the TensorFlow tutorial.

Before initializing the TensorFlow (TF) variable, add:

And the following lines are listed at the bottom of the script:

The document gives a good explanation of how to do this. I created two Python scripts that already contain these lines to create a model.ckpt file.

–create_model_1.py Use beginner mnist Tutorials

–create_model_2.py Use expert mnist tutorials

3. Load a saved model with a different Python script

Load model back to a different Python script is also explicitly described on the same page in the TensorFlow document.

First, you must initialize the TensorFlow variable that you often use to create the model file. You then use the TensorFlow repair function to recover.

4. Prepare and load the image of my handwriting

My handwritten digital image must be formatted in the same way as a normal image into a mnist database. If the image doesn't match, it tries to predict something else.

The Mnist website provides the following information:

– Image normalization, which can be installed in a 20x20-pixel box while preserving its aspect ratio.

– The pictures are all centered in a 28x28 image.

– Sort like a list of masters. Pixel value 0 to 255,0 for background (white) and 255 for foreground (black).

For image processing I use the Python Image Library (PIL). Easy installation Method:

Or look at other installation options for the pillow document.

Gets the pixel value of the image I perform the following steps. The code snippet for the Imageprepare () function shows the code for all the steps.

(1) Load the image of my handwritten number.

(2) Convert the image to black and white (mode "L")

(3) Determine the size of the original image is the largest

(4) Resize the image so that the maximum size (ether height and width) is 20 pixels, and the scale is minimized in the same proportion.

(5) Sharpen the image. This will greatly strengthen the results.

(6) Paste the image on the 28x28 pixel white canvas. Centers the image 4 pixels from the top or side on the largest size. The maximum size is always 20 pixels and 4 + 20 + 4 = 28, the minimum size is positioned at 28 and the size of the scaled image is half the difference between the new sizes.

(7) Gets the pixel value of the new image (Canvas + centered image).

(8) Normalized pixel value to a value between 0 and 1 (this is also done in the TensorFlow mnist tutorial). 0 of which is white and 1 is pure black. The pixel values obtained from step 7 are the opposite, where 255 is white and 0 black, so the values must be reversed. The following formulas include inversion and normalization (255-x) * 1.0/255.0

I did a bit of harm because I cropped the image manually. I have not yet adapted to the automatic cropping function. You can also use a vector-based tool to create handwritten images.

The argv variable is passed through the file path to the Imageprepare () function.

5. Predicting handwritten figures

Predicting numbers using predictive functionality is now relatively straightforward. Just like Pannus at the TensorFlow GitHub Symposium on the prediction of question number 97th.

After recovering the variable's file, loading the model and using the model to prepare the pixel value of the image to predict the integer code do one of the following:

Novice Tutorials Mnist (tutorial 1):

Expert Mnist Tutorials (Tutorial 2):

The difference between Tutorials 1 and 2 is that in the Expert Tutorial model prediction (Model 2), the variable y_conv is used as the predictive label instead of y in Model 1, and the Prediction.eval function using Model 2 requires a different argument keep_prob:1.0

The code snippet below shows the complete predictint () function to predict the correct integer and the main functionality that binds it together (Expert mode). The Predictint () function is a pixel value derived from the Imageprepare () function as input.

These complete scripts can also be used.

–predict_1.py use model to form the first mnist tutorial

–predict_2.py use model to form the second Mnist expert course

Results

Here are some of the numbers I used to test from the Expert Tutorial neural network (Model 2). The result is quite good. Interestingly, it will make mistakes that humans may have (except perhaps by mistaking 7 for 3). I think it needs some fine-tuning.

Right to

Correct forecast for 1

Correct forecast for 3

Correct forecast for 4

Correct forecast for 6

Correct forecast for 7

Not true.

Forecast of 0

Forecast of 2

Forecast of 3

Original link:

https://niektemme.com/2016/02/21/tensorflow-handwriting/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.