简体   繁体   English

如何使用 Keras 模型进行手写字符识别预测

[英]How to make predictions with Keras model for Handwritten Character Recognition

We have already the trained model but we can't write the part of the program that makes the predictions.我们已经有了经过训练的模型,但我们无法编写进行预测的程序部分。 We can open the picture but we can't process it with TensorFlow.我们可以打开图片,但是我们不能用 TensorFlow 处理它。

Any help would be appreciated.任何帮助,将不胜感激。 Here is what we already have.这是我们已经拥有的。

 from __future__ import absolute_import, division, print_function, unicode_literals

# Install TensorFlow

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='tanh'),    #relu, softmax, tanh
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='tanh')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test,  y_test, verbose=2)

You can use the model object's .predict() method as follows:您可以使用model对象的.predict()方法如下:

import numpy as np

predictions = model.predict([x_test]) # Make prediction
print(np.argmax(predictions[1000])) # Print out the number

Anyway I advice you to study a nice, simple example of how to use tensorflow for classifying handwritten digits by Abdelhakim Ouafi.无论如何,我建议你学习一个很好的、简单的例子,说明如何使用 tensorflow 对 Abdelhakim Ouafi 的手写数字进行分类。 I'm including the main parts here for future readers for the case, if the link would be available:如果链接可用,我将在此处为案例的未来读者提供主要部分:

Description of the MNIST Handwritten Digit. MNIST 手写数字的描述。

The MNIST Handwritten Digit is a dataset for evaluating machine learning and deep learning models on the handwritten digit classification problem, it is a dataset of 60,000 small square 28×28 pixel grayscale images of handwritten single digits between 0 and 9. MNIST Handwritten Digit 是一个用于评估机器学习和深度学习模型在手写数字分类问题上的数据集,它是一个包含 60,000 张 0 到 9 之间的手写单个数字的 60,000 个小正方形 28×28 像素灰度图像的数据集。

Import the TensorFlow library导入 TensorFlow 库

import tensorflow as tf # Import tensorflow library
import matplotlib.pyplot as plt # Import matplotlib library

Create a variable named mnist, and set it to an object of the MNIST dataset from the Keras library and we're gonna unpack it to a training dataset (x_train, y_train) and testing dataset (x_test, y_test) :创建一个名为 mnist 的变量,并将其设置为 Keras 库中 MNIST 数据集的对象,然后我们将其解包为训练数据集(x_train, y_train)和测试数据集(x_test, y_test)

mnist = tf.keras.datasets.mnist # Object of the MNIST dataset
(x_train, y_train),(x_test, y_test) = mnist.load_data() # Load data

Preprocess the data预处理数据

To make sure that our data was imported correctly, we are going to plot the first image from the training dataset using matplotlib:为了确保我们的数据被正确导入,我们将使用 matplotlib 绘制训练数据集中的第一张图像:

plt.imshow(x_train[0], cmap="gray") # Import the image
plt.show() # Plot the image

在此处输入图片说明

Before we feed the data into the neural network we need to normalize it by scaling the pixels value in a range from 0 to 1 instead of being from 0 to 255 and that make the neural network needs less computational power:在我们将数据输入神经网络之前,我们需要通过在 0 到 1 的范围内缩放像素值而不是从 0 到 255 来对其进行归一化,这使得神经网络需要更少的计算能力:

# Normalize the train dataset
x_train = tf.keras.utils.normalize(x_train, axis=1)
# Normalize the test dataset
x_test = tf.keras.utils.normalize(x_test, axis=1)

Build the model Now, we are going to build the model or in other words the neural network that will train and learn how to classify these images.构建模型现在,我们将构建模型,即训练和学习如何对这些图像进行分类的神经网络。

It worth noting that the layers are the most important thing in building an artificial neural network since it will extract the features of the data.值得注意的是,层是构建人工神经网络最重要的部分,因为它将提取数据的特征。

First and foremost, we start by creating a model object that lets you add the different layers.首先,我们首先创建一个模型对象,让您可以添加不同的层。

Second, we are going to flatten the data which is the image pixels in this case.其次,在这种情况下,我们将展平数据,即图像像素。 So the images are 28×28 dimensional we need to make it 1×784 dimensional so the input layer of the neural network can read it or deal with it.所以图像是 28×28 维的,我们需要将其设为 1×784 维,以便神经网络的输入层可以读取或处理它。 This is an important concept you need to know.这是您需要了解的一个重要概念。

Third, we define input and a hidden layer with 128 neurons and an activation function which is the relu function.第三,我们定义输入和一个具有 128 个神经元的隐藏层和一个激活函数,即 relu 函数。

And the Last thing we create the output layer with 10 neurons and a softmax activation function that will transform the score returned by the model to a value so it will be interpreted by humans.最后,我们创建了具有 10 个神经元和 softmax 激活函数的输出层,该函数将模型返回的分数转换为一个值,以便人类对其进行解释。

#Build the model object
model = tf.keras.models.Sequential()
# Add the Flatten Layer
model.add(tf.keras.layers.Flatten())
# Build the input and the hidden layers
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# Build the output layer
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

Compile the model Since we finished building the neural network we need to compile the model by adding some few parameters that will tell the neural network how to start the training process.编译模型由于我们完成了神经网络的构建,我们需要通过添加一些告诉神经网络如何开始训练过程的参数来编译模型。

First, we add the optimizer which will create or in other word update the parameter of the neural network to fit our data.首先,我们添加优化器,它将创建或换句话说更新神经网络的参数以适应我们的数据。

Second, the loss function that will tell you the performance of your model.其次,损失函数将告诉您模型的性能。

Third, the Metrics which give indicative tests of the quality of the model.第三,指标对模型的质量进行指示性测试。

# Compile the model
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

Train the model We are ready to train our model, we call the fit subpackage and feed it with the training data and the labeled data that correspond to the training dataset and how many epoch should run or how many times should make a guess.训练模型我们准备好训练我们的模型,我们调用 fit 子包并为其提供训练数据和与训练数据集相对应的标记数据,以及应该运行多少个 epoch 或应该进行多少次猜测。

model.fit(x=x_train, y=y_train, epochs=5) # Start training process

在此处输入图片说明 Evaluate the model Let's see how the model performs after the training process has finished.评估模型让我们看看模型在训练过程完成后的表现。

# Evaluate the model performance
test_loss, test_acc = model.evaluate(x=x_test, y=y_test)
# Print out the model accuracy 
print('\nTest accuracy:', test_acc)

Evaluating the Model Performance It shows that the neural network has reached 97.39% accuracy which is pretty good since we train the model just with 5 epochs.评估模型性能它表明神经网络已经达到了 97.39% 的准确率,这是相当不错的,因为我们只用 5 个 epochs 训练了模型。

Make predictions Now, we will start making a prediction by importing the test dataset images.进行预测现在,我们将通过导入测试数据集图像开始进行预测。

predictions = model.predict([x_test]) # Make prediction

We are going to make a prediction for numbers or images that the model has never seen before.我们将对模型以前从未见过的数字或图像进行预测。

For instance, we try to predict the number that corresponds to the image number 1000 in the test dataset:例如,我们尝试预测测试数据集中与图像编号 1000 对应的数字:

print(np.argmax(predictions[1000])) # Print out the number

预言

As you see, the prediction is number nine but how we can make sure that this prediction was true?如您所见,预测是第 9 位,但我们如何确保该预测是正确的? well, we need to plot the image number 1000 in the test dataset using matplotlib:好吧,我们需要使用 matplotlib 在测试数据集中绘制图像编号 1000:

plt.imshow(x_test[1000], cmap="gray") # Import the image
plt.show() # Show the image

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用顺序 Keras model 和多处理并行进行预测? - How to make predictions in parallel using sequential Keras model and multiprocessing? 使用Keras进行无分段的手写文本识别 - Segmentation-free Handwritten Text Recognition with Keras 如何使用保存在 HDF5 文件中的 Keras 训练模型进行预测? - How can I use a Keras trained model saved in a HDF5 file to make predictions? 如何使用训练有素的Keras模型进行新的预测? - How can I get use of trained Keras model to make new predictions? 使用经过训练的 Keras 模型对新的 csv 数据进行预测 - Using a trained Keras model to make predictions on new csv data 使用keras模型中的张量流图进行预测 - Make predictions using a tensorflow graph from a keras model 由于 CUDA 错误,无法从 Keras model 进行预测 - Unable to make predictions from Keras model due to CUDA errors 如何在手写文本识别中对数字和单词进行分类 - How to classify digits and words in Handwritten Text recognition 简单的(有效的)手写数字识别:如何改进? - Simple (working) handwritten digit recognition: how to improve it? 如何在分类模型中对单个图像进行预测? - How to make predictions for a single image in a classification model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM