Handwritten digital image recognition is realized using classical fully connected neural network

Library function introduction

  • Numpy: Python third-party library for scientific calculations
  • PIL: Python Image Library, Python Image processing Library
  • Pyplot: A plotting framework for Matplotlib
  • OS: provides rich methods for handling files and directories
Import the required package
import numpy as np
import paddle
import paddle.nn as nn
from PIL import Image
import matplotlib.pyplot as plt
import os
print("This tutorial is based on the Paddle version number:"+paddle.__version__)
Copy the code

Step1: Prepare data.

(1) Introduction of data set

The MNIST dataset contains 60,000 training sets and 10,000 test sets. It is divided into picture and label. The picture is a 28*28 pixel matrix, and the label is 0~9, 10 digits in total.

(2) The transform function defines a normalized standard

(3) train_dataset and test_dataset

Paddle. Vision. Datasets. MNIST () the mode = ‘train’ and mode = ‘test’ respectively for MNIST training set and testing set

Transform =transform parameter is normalized standard

Compose Compose Compose a list of the interfaces used for preprocessing the dataset.
Normalize is used to Normalize images in two ways: 1. The uniform mean and standard deviation are used to normalize each channel of the image. 2. Specify different mean and standard deviation values for each channel for normalization processing.
from paddle.vision.transforms import Compose, Normalize
transform = Compose([Normalize(mean=[127.5],std=[127.5],data_format='CHW')])

Normalize the dataset using transform
print('Download and load training data')
train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)
test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)
print('Load complete')
Copy the code
Download and load training data Cache file/home/aistudio/Cache/paddle/dataset/mnist/train - images - idx3 does - ubyte. Gz not found. downloading https://dataset.bj.bcebos.com/mnist/train-images-idx3-ubyte.gz Begin to download Download finished Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-labels-idx1-ubyte.gz Begin to download ........ Download finished Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-images-idx3-ubyte.gz Begin to download Download finished Cache file  /home/aistudio/.cache/paddle/dataset/mnist/t10k-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-labels-idx1-ubyte.gz Begin to download .. Download finished Loading is completeCopy the code
# output image
train_data0, train_label_0 = train_dataset[0] [0],train_dataset[0] [1]
train_data0 = train_data0.reshape([28.28])
plt.figure(figsize=(2.2))
print(plt.imshow(train_data0, cmap=plt.cm.binary))
print('Train_data0 is labelled:' + str(train_label_0))

Copy the code
AxesImage (18, 19; 111.6x108.72) Train_data0 is labeled as: [5] / opt/conda envs/python35 - paddle120 - env/lib/python3.7 / site - packages/matplotlib/cbook/set py: 2349: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, And in 3.8 it will stop working if isinstance(obj, collections.iterator): / opt/conda envs/python35 - paddle120 - env/lib/python3.7 / site - packages/matplotlib/cbook/set py: 2366: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, And in 3.8 it will stop working return list(data) if isinstance(data, collections.MappingView) else data / opt/conda envs/python35 - paddle120 - env/lib/python3.7 / site - packages/matplotlib/image. The py: 425: DeprecationWarning: Np.asscalar (a) is deprecated since NumPy V1.16, use a.item() instead a_min = np.asscalar(a_min.astype(scaled_dtype)) / opt/conda envs/python35 - paddle120 - env/lib/python3.7 / site - packages/matplotlib/image. The py: 426: DeprecationWarning: Np.asscalar (a) is deprecated since NumPy v1.16, use A. id () instead A_max = Np.asscalar (a_max. Astype (scaled_dtype))Copy the code
# print(train_data0)
Copy the code

Step2. Configure the network

The following code judgment defines a simple fully connected neural network. This example has three layers, two hidden layers of size 128 and one output layer of size 10.

The MNIST dataset is handwritten grayscale images of 0 to 9, with 10 categories, so the final output size is 10.

The network structure of this practice is: input layer –>> hidden layer –>> hidden layer –>> output layer.

Define a fully connected neural network
class Mnist(nn.Layer) :
    def __init__(self) :
        super(Mnist,self).__init__()
        self.fc1 = nn.Linear(in_features = 28*28, out_features = 256)
        self.fc2 = nn.Linear(in_features = 256, out_features =128)
        self.fc3 = nn.Linear(in_features = 128, out_features = 10)


    def forward(self, input) : 
        bsz = input.shape[0]
        x = paddle.reshape(input,[bsz,-1])
        x = nn.functional.relu(self.fc1(x))
        x = nn.functional.relu(self.fc2(x))
        y = self.fc3(x)
        return y
Copy the code
from paddle.metric import Accuracy

Encapsulate a Model with a Model
model = paddle.Model(Mnist())   

# Define the loss function
optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())

# Config model
model.prepare(optim,paddle.nn.CrossEntropyLoss(),Accuracy())

# Train to save and validate the model
model.fit(train_dataset,test_dataset,epochs=2,batch_size=64,save_dir='model.pd',verbose=1)

Copy the code
W1204 13:02:37.934275 128 Device_context. cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1 W1204 13:02:37.939237 128 Device_context.cc :372] 0, cuDNN Version: 7.6. The loss value printed in The log is The current step, and the metric is the average value of previous step. Epoch 1/2 step 30/938 [..............................] - Loss: 0.4677 - ACC: 0.6531 - ETA: 17s - 20ms/st / opt/conda envs/python35 - paddle120 - env/lib/python3.7 / site - packages/paddle/fluid/dataloader/dataloader_iter py: 89: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar Type, use 'Np.bool_' here. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations if isinstance (slot [0], (np) ndarray, np. Bool, Numbers. The Number) : / opt/conda envs/python35 - paddle120 - env/lib/python3.7 / site - packages/paddle/fluid/the layers/utils. Py: 77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, And in 3.8 it will stop working return (isinstance(seq, collections.Sequence) and step 50/938 [>.............................] - loss: 0.6765 - acc: 0.7234 - ETA: 12 s - 14 ms/stepstep 938/938 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - loss: 0.2223 acc: 0.9111-7 ms/step save checkpoint at/home/aistudio/model. The pd / 0 Eval the begin... The loss value printed in the log is the current batch, and the metric is the average value of previous step. step 157/157 [==============================] - loss: 0.0849 acc: 0.9543-6 ms/step Eval samples: 10000 Epoch 2/2 step 938/938 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - loss: 0.0974 acc: 0.9580-6 ms/step save checkpoint at/home/aistudio/model. The pd / 1 Eval the begin... The loss value printed in the log is the current batch, and the metric is the average value of previous step. step 157/157 [==============================] - loss: 0.0055 acc: 0.9571-6 ms/step Eval samples: 10000 save checkpoint at/home/aistudio/model. The pd/finalCopy the code

Step3. Model evaluation

res = model.evaluate(test_dataset,batch_size=64, verbose=1)
print("Test set loss value: {}".format(res["loss"]))
print("Test set Accuracy: {}".format(res["acc"]))
Copy the code
Eval begin... The loss value printed in the log is the current batch, and the metric is the average value of previous step. step 157/157 [==============================] - loss: 0.0055-ACC: 0.9571-6ms/Step Eval samples: 10000 Test set loss value: [0.0054676016] Test set accuracy: 0.9571Copy the code

Step4. Model prediction

Get the first image of the test set
test_data0, test_label_0 = test_dataset[0] [0],test_dataset[0] [1]
test_data0 = test_data0.reshape([28.28])
plt.figure(figsize=(2.2))
# show the first image in the test set
print(plt.imshow(test_data0, cmap=plt.cm.binary))
print(The 'test_data0 'tag is:' + str(test_label_0))
# Model prediction
result = model.predict(test_dataset, batch_size=1)
Print the predicted results of the model
print('test_data0 ': %d' % np.argsort(result[0] [0[])0] [-1])


Copy the code
AxesImage (18, 19; [7] Predict begin... 10000/10000 step [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] 1 ms/step Predict samples: 10000 test_data0 predicted values for: 7Copy the code