基于神经网络的手写数字识别(模型)

minist数据集

import numpy as np
from PIL import Image


def load_mnist():  # 读取离线的MNIST.npz文件。
    path = r'mnist.npz'  # 放置mnist.py的目录,这里默认跟本代码在同一个文件夹之下。
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)


(train_image, train_label), (test_image, test_label) = load_mnist()
print(train_image.shape)
print(train_label.shape)

# 看第一张图片的数据
print(train_image[0])
# 转化为图片
im = Image.fromarray(train_image[0])
# 看第一张图片的大小
print(im.size)
# 看第一张图片
im.show()

模型训练

# 构建模型
model = keras.Sequential(
    [
        keras.Input(shape=(784,)),  # 这里(784,)的意思是784维向量构成的batch,省略的是batch的大小
        layers.Dense(100, activation="relu"),
        layers.Dense(100, activation="relu"),
        # layers.Dense(100, activation="relu"),
        layers.Dropout(0.3),
        layers.Dense(num_classes, activation="softmax"),
    ]
)

数据预处理

def Preprocess(self):
        """
            预处理
        """
        self.read()
        for image in self.image:
            # 转灰度图
            image = image.convert("L")
            # 如果不是黑底白字,反转灰度图
            # image = ImageOps.invert(image)
            # 重定义大小
            image = image.resize((28, 28))
            # 转化为np数组
            imgdata = np.asarray(image)
            # 转化为数据要求784维
            imgdata = imgdata.reshape([784])
            # 类型转化 + 归一化
            imgdata = imgdata.astype("float32") / 255
            self.imgdata.append(imgdata)
        self.imgdata = np.array(self.imgdata)

载入模型

model = keras.models.load_model('model.h5')

预测

predictResult = model.predict(imgdata)

完整代码

# -*- coding: utf-8 -*-
# @Time    : 2020/9/25 9:44
# @Author  : SanZhi
# @File    : pre.py
# @Software: PyCharm
from PIL import Image
import numpy as np
from tensorflow import keras
import tensorflow as ts
import os
from PIL import ImageOps


class Img:
    def __init__(self, file_dir):
        self.url = file_dir
        self.image = []
        self.imgdata = []
        self.fileName = []

    def read(self):
        """
            读入图像
        """
        for url in os.listdir(path=self.url):
            im = Image.open(self.url + "/" + url)
            self.fileName.append(url)
            self.image.append(im)

    def Preprocess(self):
        """
            预处理
        """
        self.read()
        for image in self.image:
            # 转灰度图
            image = image.convert("L")
            # 如果不是黑底白字,反转灰度图
            # image = ImageOps.invert(image)
            # 重定义大小
            image = image.resize((28, 28))
            # 转化为np数组
            imgdata = np.asarray(image)
            # 转化为数据要求784维
            imgdata = imgdata.reshape([784])
            # 类型转化 + 归一化
            imgdata = imgdata.astype("float32") / 255
            self.imgdata.append(imgdata)
        self.imgdata = np.array(self.imgdata)

    def Predict(self, imgdata):
        """
            载入模型并预测
        """
        model = keras.models.load_model('model.h5')
        predictResult = model.predict(imgdata)
        return predictResult

    def Solove(self):
        Result = self.Predict(self.imgdata)
        out = []
        for index, l in enumerate(Result):
            max_percent = -1
            max_index = -1
            for i, v in enumerate(l):
                if v > max_percent:
                    max_percent = v
                    max_index = i
            out.append({
                "max_index": max_index,
                "max_percent": max_percent
            })
        # print(out)
        for i, v in enumerate(out):
            print(self.fileName[i] + " is " + str(v['max_index']))


Five = Img('./img')
Five.Preprocess()
Five.Solove()

结果

在这里插入图片描述

在这里插入图片描述
有一个数据错了,确实有点难认。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页