[Introduction to deep learning case 1] Handwritten digital image recognition based on Keras

Article directory

 1. Tools and environment

2. Construction of deep learning environment

1. Install Anaconda

2. Create a virtual environment

Step 1: Open Anaconda's command window, Anaconda Prompt

 Step 2: Use the command to create a python environment of the specified version (here the name of the py36 command environment is used as an example)

3. Switch environment

4. Download the toolkit required for the project

 5. Set the environment of Pycharm as the created environment

3. Operation and testing of Keras-based handwritten digital image recognition project

1. Core code

2. Collection of handwritten pictures for recognition

3. Basic principles of recognition

4. Run and test

 4. Analysis and Summary

 reference article


 1. Tools and environment

  • Pycharm 2022.1.4
  • conda version : 4.5.4
  • python version : 3.6.5.final.0
  • platform : win-64

2. Construction of deep learning environment

1. Install Anaconda

        Anaconda is an open source Python distribution, used to manage Python-related packages , using anaconda can easily switch to different python environments , and use different deep learning frameworks to develop projects, which is very efficient!

For specific download and installation steps, please refer to the article of this big guy

The most complete Anaconda installation and usage tutorial in history icon-default.png?t=N4P3https://blog.csdn.net/wq_ocean_/article/details/103889237?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168594896716800180669116%252 2%252C%2522scm%2522%253A %252220140713.130102334..%2522%257D&request_id=168594896716800180669116&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-1 03889237-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term =Anaconda%E5%AE%89%E8%A3%85&spm=1018.2226.3001.4187

2. Create a virtual environment

Step 1: Open Anaconda's command window, Anaconda Prompt

Anaconda will be attached after the installation is successful, in the start bar

 Step 2: Use the command to create a python environment of the specified version (here the name of the py36 command environment is used as an example)

conda create -n py36 python=3.6

3. Switch environment

        The default environment is base. We need to create a specified version of the environment for us to use. It can be understood as the specified environment that needs to be created to develop a project, so that the environments between different projects are isolated from each other . Use the command activate + environment name to switch the environment.

activate py36

After switching to the py36 environment, enter the conda info command, and the following information appears, indicating that the surface environment is created successfully!

4. Download the toolkit required for the project

        After successfully switching to py36 (the environment created by yourself) , execute the following commands in Anaconda Prompt to download the required toolkits. Note: Since the default download source is abroad, the download speed will be very slow. We can use domestic Some mirrors to download, very fast. Just add -i mirror address after each of the following commands.

I am using the mirror source of Tsinghua University:  -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplolib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install keras==2.1.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow==1.14.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

 5. Set the environment of Pycharm as the created environment

Click Add Interpreter in the lower right corner of Pycharm

As shown in the figure below, select conda.exe under the Scripts folder in the Anaconda installation directory

(Some will also appear in the root directory. You can also choose conda.exe in the root directory. There will be differences according to different versions of Pycharm)

 

 After Pycharm switches the interpreter, so far, the environment preparation is completed


3. Operation and testing of Keras-based handwritten digital image recognition project

1. Core code

train_cnn.py

#步骤01 入所需要的模块
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
import numpy as np
np.random.seed(10)

#步骤02 下载mnist数据集、读取数据集
(x_Train, y_Train) , (x_Test, y_Test) = mnist.load_data()

#步骤03 将图像特征值转化为6000,28,28,1的4维矩阵
x_Train4D = x_Train.reshape(x_Train.shape[0], 28, 28,1).astype('float32')
x_Test4D = x_Test.reshape(x_Test.shape[0], 28, 28, 1).astype('float32')

#步骤04 进行标准化
#将像素范围设置在【0,1】
x_Train4D_normalize = x_Train4D / 255
x_Test4D_normalize = x_Test4D / 255

#步骤05 label进行一位有效编码转换
#将标签转成读热码
y_TrainHot = np_utils.to_categorical(y_Train)
y_TestHot = np_utils.to_categorical(y_Test)

#=>建立模型
#步骤01 定义模型
model = Sequential()

#步骤02 建立卷积层1
model.add(Conv2D(filters=16, # filter = 16 建立16个滤镜
# kernel_size = (5,5) 每一个滤镜是5 × 5的大小
kernel_size=(5,5), 
    # padding = 'same' 设置卷积运算产生的图像大小不变
padding='same', 
#输入的图像形状为28*28,1代表单色灰度,3代表RGB
input_shape= (28, 28, 1),
# activation设置激活函数为relu建立池化层1
activation='relu'))

#步骤03 建立池化层1
model.add(MaxPooling2D(pool_size=(2,2)))#缩减采样,输出16个14*14图像

#步骤04建立卷积层2
model.add(Conv2D(filters=36,#建立36个滤镜
kernel_size=(5,5),#每一个滤镜是5 × 5的大小
padding='same',#Convolution完成后的图像大小不变
activation='relu'#输出36个14*14的图像
))

#步骤05 建立池化层2,加入Dropout避免Overfitting
model.add(MaxPooling2D(pool_size=(2,2)))#图像大小变为7*7
# 加入DropOut(0.25),每次训练时,会在神经网络中随机放弃25%的神经元,避免过拟合建立神经网络(平坦层,隐藏层,输出层)建立平坦层
model.add(Dropout(0.25))

#步骤06 建立平坦层
model.add(Flatten())#长度是36*7*7个神经元

#步骤07 建立隐藏层
model.add(Dense(128, activation='relu'))
# 把DropOut加入模型中,DropOut(0.5)在每次迭代时候会随机放弃50%的神经元,避免过拟合
model.add(Dropout(0.5))
# 建立输出层,一共10个单元,对应0-9一共10个数字。使用softmax进行激活
model.add(Dense(10, activation='softmax'))
# 查看模型摘要
print(model.summary())

#=>进行训练
#步骤01 定义训练方式
# 定义训练方式compile
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#步骤02 开始训练
train_history = model.fit(x = x_Train4D_normalize, y = y_TrainHot,
validation_split=0.2,
#将80%作为训练数据,20%作为测试数据
epochs=10,#执行10个训练周期
batch_size=300,#每一批300项数据
verbose=2#参数为2表示显示训练过程
)

train_cnn_model.py

from keras.datasets import mnist
from keras.utils import np_utils
import numpy as np
np.random.seed(10)

(x_Train, y_Train) , (x_Test, y_Test) = mnist.load_data()

x_Train4D = x_Train.reshape(x_Train.shape[0], 28, 28,1).astype('float32')
x_Test4D = x_Test.reshape(x_Test.shape[0], 28, 28, 1).astype('float32')

x_Train4D_normalize = x_Train4D / 255
x_Test4D_normalize = x_Test4D / 255
y_TrainHot = np_utils.to_categorical(y_Train)
y_TestHot = np_utils.to_categorical(y_Test)

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D

model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(5,5), padding='same', input_shape = (28, 28, 1), activation='relu'))
# 参数说明
# filter = 16 建立16个滤镜
# kernel_size = (5,5) 每一个滤镜是5 × 5的大小
# padding = 'same' 设置卷积运算产生的图像大小不变
# input_shape = (28, 28, 1) 第一二维代表输入图像的形状是28 × 28,第三维因为是单色灰度图像,所以最后维数值是1
# activation设置激活函数为relu建立池化层1
model.add(MaxPooling2D(pool_size=(2,2)))
# 输入参数为pool_size=(2,2),执行第一次缩减采样,将16个28 ×28的图像缩小为16个14 × 14的图像建立卷积层2,将16个图像转化为36个图像,不改变图像大小,仍为14 × 14
model.add(Conv2D(filters=36, kernel_size=(5,5), padding='same', activation='relu'))
# 加入池化层2,并加入DropOut避免过拟合
model.add(MaxPooling2D(pool_size=(2,2)))
# 执行第二次缩减采样,将14 × 14图像转换为7 × 7图像
model.add(Dropout(0.25))
# 加入DropOut(0.25),每次训练时,会在神经网络中随机放弃25%的神经元,避免过拟合建立神经网络(平坦层,隐藏层,输出层)建立平坦层
model.add(Flatten())
# 将之前步骤建立的池化层2,一共有36个7 × 7的图像转化为一维向量,长度是36 × 7 × 7 = 1764, 也就是1764个float数,对应1764个神经元建立隐藏层,一共128个神经元
model.add(Dense(128, activation='relu'))
# 把DropOut加入模型中,DropOut(0.5)在每次迭代时候会随机放弃50%的神经元,避免过拟合
model.add(Dropout(0.5))
# 建立输出层,一共10个单元,对应0-9一共10个数字。使用softmax进行激活
model.add(Dense(10, activation='softmax'))
# 查看模型摘要
print(model.summary())
# # 进行训练
# 定义训练方式

# # 加载之前训练的模型
try:
    # model.load_weights("SaveModel/minist_model.h5")
    model.load_weights("SaveModel/minist_model_graphic.h5")
    print("加载模型成功!继续训练模型")
except :
    print("加载模型失败!开始训练一个新模型")

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

# 开始训练
train_history = model.fit(x = x_Train4D_normalize, y = y_TrainHot,
                          validation_split=0.2, epochs=10, batch_size=300, verbose=2)

# model.save_weights("SaveModel/minist_model.h5")
model.save("SaveModel/minist_model_graphic.h5")

print("Saved model to disk")

predict_one_img.py

import cv2
from keras.models import load_model

img = cv2.imread("./pic/6.jpg")
print(img.shape)
grey_img = img[:,:,0:1]
print(grey_img.shape)
shape_img= (grey_img.reshape(1, 28, 28, 1)).astype('float32')/255

# model = load_model('SaveModel/minist_model.h5')  #选取自己的.h模型名称
model = load_model('SaveModel/minist_model_graphic.h5')  #选取自己的.h模型名称
prediction = model.predict_classes(shape_img)
print('该手写数字图像为:'+'%d'%prediction[0])

2. Collection of handwritten pictures for recognition

 

3. Basic principles of recognition

        The dataset used is the MNIST dataset. Each picture in this dataset consists of 28x28 pixels , and each pixel is represented by a gray value. The following is an example of the number 1. Our purpose is to make a model, input these 784 values ​​into the model, and then its output result is 1.

4. Run and test

  • Read the dataset first

  •  training data model

  • Recognize handwritten digit images

 4. Analysis and Summary

        This time, based on the classic case of Keras handwritten digital image recognition , I have initially learned another important branch of machine learning - the content of deep learning. The data set used is the MNIST data set . Each picture in this dataset consists of 28x28 pixels , and each pixel is represented by a gray value. The first is to use the pip command line to download the toolkit to be imported by the program. At the same time, use the activate py36 command to switch the python environment to the environment where we created and imported the relevant toolkit. The compiler uses pycharm to run the program. First of all, it is necessary to train the data set, and then use the trained model to recognize the handwritten digital image 6.jpg, and the console prints out the recognition results.

        After the study of this introductory case, I have a strong interest in the field of machine learning. In essence, it uses massive data as input to let the computer train and analyze a model. We can regard it as a function, a " "Black box", and then use it to identify the specified data and give the analysis results. This process is analogous to the process of the human brain summarizing past experience and predicting the future to guide actions. It is very wonderful! !

 reference article

In-depth learning example - Keras realizes handwritten digit recognition icon-default.png?t=N4P32522 %252C%2522scm%2522 %253A%252220140713.130102334..%2522%257D&request_id=168594730916800222827149&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default -1-103811112-null-null.142^v88^control_2,239^v2 ^insert_chatgpt&utm_term=Keras%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E5%9B%BE%E5%83%8F%E8%AF%86%E5%88 %AB&spm=1018.2226.3001.4187 [Deep learning practice - 1]: Handwritten digit recognition based on Keras (very detailed, open source code)icon-default.png?t=N4P3https://blog.csdn.net/qq_42856191/article/details/121420268?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168594730916800222827149%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168594730916800222827149&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-121420268-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=Keras%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E5%9B%BE%E5%83%8F%E8%AF%86%E5%88%AB&spm=1018.2226.3001.4187

Guess you like

Origin blog.csdn.net/qq_52487066/article/details/131048466