python学习笔记——手写数字识别

一.使用数据集进行手写数字识别

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
import matplotlib.pyplot as plt

# 一些参数
batch_size = 128
epochs = 1
num_classes = 10

# 获取数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()  # 加载mnist数据集

# 归一化,0-255不太方便神经网络进行计算,因此将范围缩小到0—1
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# 改变数据形状,格式为(n_samples, vector),将其转化为向量
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)

# 控制台打印输出样本数量信息
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# 样本标签转化为one-hot编码格式(one hot编码是将变量转换为机器学习算法易于利用的一种形式的过程)
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test  = keras.utils.to_categorical(y_test, num_classes)

# 创建一个神经网络训练模型
model = Sequential()
# 添加一个全连接层;神经元units=512;该层使用的激活函数activation=relu;输入格式input_dim=784
model.add(Dense(units=512, activation='relu', input_dim=784))
# Dropout通过随机(概率0.2)断开神经网络之间的连接,减少每次训练时实际参与计算的模型的参数量,
# 从而减少了模型的实际容量,来防止过拟合。
model.add(Dropout(rate=0.2))
# 添加一个全连接层;神经元units=512;该层使用的激活函数activation=relu
model.add(Dense(units=512, activation='relu'))
# Dropout通过随机(概率0.2)断开神经网络之间的连接,减少每次训练时实际参与计算的模型的参数量,
# 从而减少了模型的实际容量,来防止过拟合
model.add(Dropout(rate=0.2))
# 添加一个全连接层;将最终的输出结果分成10类
model.add(Dense(num_classes, activation='softmax'))
# 在控制台输出模型参数信息
model.summary()
# 配置训练时各指标的评测标准;损失函数loss:多分类交叉熵损失函数,RMS优化器,
# 评价函数 metrics:真实值与预测值皆为int型
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(), metrics=['accuracy'])
# 训练模型:batch_size:一次训练所取样本数,epochs:迭代次数,verbose:
# 进度条模式的日志展示(整数),validation_data:真实值.
model.fit(x_train, y_train,batch_size=batch_size,  epochs=epochs,verbose=1,validation_data=(x_test, y_test))
# 预测
n = 4   # 给出需要预测的图片数量,为了方便,只取前4张图片
print(x_test[:n])
print('/n')
predicted_number = model.predict(x_test[:n], n)
# 画图
plt.figure(figsize=(10, 3))
for i in range(n):
    plt.subplot(1, n, i + 1)
    t = x_test[i].reshape(28, 28)   # 向量需要reshape为矩阵
    plt.imshow(t, cmap='gray')      # 以灰度图显示
    plt.subplots_adjust(wspace=2)   # 调整子图间的间距,挨太紧了不好看
    # 第一个数字是真实标签,第二个数字是预测数值
    # 如果预测正确,绿色显示,否则红色显示
    # 预测结果是one-hot编码,需要转化为数字
    if y_test[i].argmax() == predicted_number[i].argmax():
        plt.title('%d,%d' % (y_test[i].argmax(), predicted_number[i].argmax()), color='green')
    else:
        plt.title('%d,%d' % (y_test[i].argmax(), predicted_number[i].argmax()), color='red')
    plt.xticks([])  # 取消x轴刻度
    plt.yticks([])
plt.show()

二.自定义数据集进行手写字体识别

import tensorflow as tf
from tensorflow.keras import layers ,models
from tensorflow.examples.tutorials.mnist import input_data

# 读取模型
mnist = input_data.read_data_sets("MNIST/", one_hot=False)
# 创建网络
model = models.Sequential()
# 第一个卷积层 28x28x1 change to 24x24x32
model.add(layers.Conv2D(32,kernel_size = [5,5],activation='relu',input_shape=(28,28,1)))
# 第一个池化层层24x24x32 change to 12x12x32
model.add(layers.MaxPooling2D([2,2]))
# 第二个卷积层 12x12x32 change to 8x8x64
model.add(layers.Conv2D(64,kernel_size = [5,5],activation='relu'))
# 第二个池化层 8x8x64 change to 4x4x64
model.add(layers.MaxPooling2D([2,2]))
# 全连接层 4*4*64(每一个特征图4*4,共有64个),变化成一行4*4*64,便于全连接
model.add(layers.Flatten())
# 这个就是全连接层的计算 [1,4x4x64] change to [1, 1024]
model.add(layers.Dense(1024,activation = 'relu'))
model.add(layers.Dense(10, activation = 'softmax'))
# [1,1024] change to [1, 10]
model.summary()

# 把最有最优算的参数
check_path = './1.hdf5'
# period 每隔5epoch保存一次
save_model_cb = tf.keras.callbacks.ModelCheckpoint(
     check_path,save_best_only=False ,monitor='val_acc',save_weights_only=False, verbose=1, period=1)
# 设置模型的优化类型
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
# 填充数据,总共循环10次
model.fit(mnist.train.images.reshape((55000,28,28,1)),mnist.train.labels, epochs = 2, callbacks = [save_model_cb])

# 根据算的权值,计算准确率
test_loss, test_acc = model.evaluate(mnist.test.images.reshape(10000,28,28,1)
            , mnist.test.labels)
print('\n')
print('测试集的准确率:')
print("准确率: %.4f,共测试了%d张图片 " % (test_acc,len(mnist.test.images)))

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from PIL import Image
import numpy as np

def produceImage(file_in,file_out):
    image = Image.open(file_in)
    resized_image = image.resize((28, 28), Image.ANTIALIAS)
    resized_image.save(file_out)

if __name__ == '__main__':
    file_in1 = './number/1.png'
    file_in2 = './number/2.png'
    file_in5 = './number/5.png'
    file_out1 = './number/1_1.png'
    file_out2 = './number/2_1.png'
    file_out5 = './number/5_1.png'
    produceImage(file_in1, file_out1)
    produceImage(file_in2, file_out2)
    produceImage(file_in5, file_out5)
    im1 = Image.open('./number/1_1.png')
    im2 = Image.open('./number/2_1.png')
    im5 = Image.open('./number/5_1.png')
    L1 = im1.convert("L")
    L2 = im2.convert("L")
    L5 = im5.convert("L")
    L1.save('./number/1_2.png')
    L2.save('./number/2_2.png')
    L5.save('./number/5_2.png')

mnist = input_data.read_data_sets("MNIST/", one_hot=False)
im_1 = Image.open('./number/1_2.png')
im_1 = np.reshape(im_1, [1,28,28,1])
im_2 = Image.open('./number/2_2.png')
im_2 = np.reshape(im_2, [1,28,28,1])
im_5 = Image.open('./number/5_2.png')
im_5 = np.reshape(im_5, [1,28,28,1])
# 调用模型
new_model =tf.keras.models.load_model('./1.hdf5')
# 进行预测
pe_1 = new_model.predict(im_1)
pe_2 = new_model.predict(im_2)
pe_5 = new_model.predict(im_5)
# 把最大的坐标找到,因为new_model.predict返回的是[0,0,1,0,0,0,0,0,0,0]这种格式,
# 所以需要转换为张量的格式
pe_1 = tf.argmax(pe_1 ,1)
pe_2 = tf.argmax(pe_2 ,1)
pe_5 = tf.argmax(pe_5 ,1)
print(pe_1)
print(pe_2)
print(pe_5)


使用到的数据链接:

链接:https://pan.baidu.com/s/1fjg2t5mGkeTTffnl_ltFlw 
提取码:1234

推荐的参考文章链接:

(1).

tensorflow 自己制作Mnist数据集,用训练好的模型来测试准确度。手把手教你实现!教你怎么调用自己已经训练好的模型!清晰易懂!加深你对卷积CNN的理解_尚码哥123的博客-CSDN博客

(2).

基于tensorflow2的手写中文数字识别(自己创建数据集)_红杏枝头的博客-CSDN博客

(3).

Tensorflow 报错No module named 'tensorflow.examples.tutorials'解决办法 - 萍2樱释 - 博客园

  • 2
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值