深度学习之手写数字识别小探之细节点记录

深度学习之手写数字识别小探之细节点记录
1.自己写一张图片去做判断时,底色完全黑色,形成二值图最好,如果底色是浅黑色,判断会有误差
如下两张图的底色差别
在这里插入图片描述
在这里插入图片描述

图片格式为png导入,准确率高一些,貌似:)

层数经验
以28*28为例

输入层784,
隐藏层
第一层,520
第二层,320
第三层,240
第四层,120

输出层
10

在这里插入图片描述

训练时,四千多个样本数据,用全梯度下降,训练16次左右,以784为一组输入训练

训练前数据处理

取灰度图,归一化

训练数据时,预测值需要0-9热编码

import os
os.environ["CUDA_VISIBLE_DEVICES" ] ="-1"

import cv2 as cv
# 数值计算
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 划分训练集和测试集
from sklearn.model_selection import train_test_split
# 用于模型搭建
from tensorflow.keras.models import Sequential
# 构建模型的层和激活方法
from tensorflow.keras.layers import Dense, Activation
# 数据处理的辅助工具
from tensorflow.keras import utils

train = pd.read_csv("./zuoyetupian/1128/trainshouxie.csv")

train_image = train.iloc[:, 1:]
train_label = train.iloc[:, 0]

def to_plot(n):
    num = train_image.iloc[n,].values.reshape(28, 28)

    plt.imshow(num)
    plt.axis("off")
    plt.show()


# 对数据特征值归一化处理
print(train_image.shape)
train_image = train_image.values / 255
print(train_image)
train_label = train_label.values

train_X, test_X, train_y, test_y = train_test_split(train_image, train_label, train_size = 0.8, random_state=0)
print(train_X.shape, test_X.shape)

# 进行热编码
def one_hot_encode_object_array(arr):
    # 去重获取全部的类别
    uniques, ids = np.unique(arr, return_inverse=True)
    # 返回热编码的结果
    return utils.to_categorical(ids, len(uniques))

# 训练集热编码
train_y_ohe = one_hot_encode_object_array(train_y)
# 测试集热编码
test_y_ohe = one_hot_encode_object_array(test_y)

# 利用sequential方式构建模型
model = Sequential([
  # 隐藏层1,激活函数是relu,输入大小有input_shape指定
  Dense(520, activation="relu", input_shape=(784,)),
  # 隐藏层2,激活函数是relu
  Dense(320, activation="relu"),
  Dense(240, activation="relu"),
  Dense(120, activation="relu"),

  # 输出层
  Dense(10,activation="softmax")
])

# 模型结构展示
print(model.summary())
print(utils.plot_model(model, show_shapes=True))


# 设置模型的相关参数:优化器,损失函数和评价指标
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=["accuracy"])

# 类型准换
train_x = np.array(train_X,dtype= np.float32)
test_x = np.array(test_X,dtype=np.float32)

# 模型训练:epochs,训练样本送入到网络中的次数,batch_size:每次训练的送入到网络中的样本个数
model.fit(train_x, train_y_ohe, epochs=16, batch_size=784, verbose=1)

# 计算模型的损失和准确率
loss, accuracy = model.evaluate(test_x, test_y_ohe, verbose=1)
print("Accuracy = {:.2f}".format(accuracy))

# 真实预测一条数据,手写一张图片做预测
# 1.读取图像
img = cv.imread('./zuoyetupian/1128/shouxieshuzi0606.png')
gray= cv.cvtColor(img,cv.COLOR_BGR2GRAY)
print(gray.shape)

#数据归一化
test_xlg001=gray.reshape(1, 784)
test_xlg001=test_xlg001/255

# 类型准换
test_xlg003 = np.array(test_xlg001,dtype= np.float32)
print(f'准备的预测数据:\n{test_xlg003}')
aa=model.predict(test_xlg003,verbose=0)
# 打印结果,输出输出层的结果,寻找最大值下标输出
print(aa)
print(np.argmax(np.array(aa)))

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值