TensorFlow2.0 搭建手写数字识别网络

参考: https://www.bilibili.com/video/BV16A41157LW?p=17
视频及课件来源 北京大学 曹建

1, 获取数据:

import tensorflow as tf
import numpy as np

def get_mnist_data():
    # 参考: https://www.codenong.com/53310656/
    # 获取数据 return  (x_train, y_train), (x_test, y_test)
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
    # 将0-255直接的数值变成 0或1
    x_train, x_test = (x_train > 128).astype(int), (x_test > 128).astype(int)
    return (x_train, y_train), (x_test, y_test)

2, 用 Sequential搭建网络

def train_data(x_train, y_train, x_test, y_test, model_path):
    '''
    用 Sequential搭建网络
    tf.keras.layers.Flatten()  # 拉直层 输入特征拉直成一维数据  原本数据 28 * 28 矩阵  拉直后的数据为 1 * 784 数组
    tf.keras.layers.Dense(128, activation='relu'), # 定义第一层网络有128个神经元 激活函数维 relu
    tf.keras.layers.Dense(10, activation='softmax') # 定义第二层网络有10个神经元 激活函数 softmax
    :rtype: object
    '''
    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),  # 拉直层  将数据拉直成1维
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dense(10, activation='softmax')
    ])
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                  metrics=['sparse_categorical_accuracy'])

    # 每次喂入32组数据    数据集迭代5次    alidation_data=(x_test, y_test): 测试集数据  validation_freq: 每迭代数据一次 执行一次测试集评测
    model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
    model.summary()
    # 保存模型
    model.save(model_path, save_format="tf")
    del model  # 删除现有模型

3 加载模型,然后使用模型识别图片

def local_image_demo():
    # 参考 https://blog.csdn.net/great_yzl/article/details/120776341
    img = Image.open('./data/image/2.png')  # 载入自己的图片
    img = img.resize((28, 28))  # 设置图片大小
    gray_img = img.convert('L')
    mun_img = np.array(gray_img)
    # 将图片转为和训练图片一样的黑底白字
    mun_img = (mun_img > 64).astype(int)
    x_test__reshape = mun_img.reshape(1, 28, 28)
    print(x_test__reshape)
    model_path = './data/tf_model_number_model'
    model_loaded = tf.keras.models.load_model(model_path)
    loaded_evaluate = model_loaded.predict(x_test__reshape)
    print(loaded_evaluate)
    prediction = np.argmax(loaded_evaluate, axis=1)  # 找出最大值
    print('预测结果:', prediction)

代码比较多
所以
还可以使用类搭建神经网络结构

# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
from PIL import Image

model_path = './data/tf_model_class'


def get_mnist_data():
    # 获取数据
    # 参考: https://www.codenong.com/53310656/
    # 获取数据 return  (x_train, y_train), (x_test, y_test)
    (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
    # 将0-255直接的数值变成 0或1
    x_train, x_test = x_train / 255.0, x_test/255.0
    return (x_train, y_train), (x_test, y_test)


class MyMnistModel(tf.keras.Model):
    '''
     网络模型搭建
    '''
    def __init__(self):
        super(MyMnistModel, self).__init__()
        self.flatten = tf.keras.layers.Flatten()
        self.d1 = tf.keras.layers.Dense(128, activation='relu')
        self.d2 = tf.keras.layers.Dense(10, activation='softmax')

    def call(self, x):
        x = self.flatten(x)
        x = self.d1(x)
        y = self.d2(x)
        return y


def train_model():
    '''
    训练
    :return:
    '''
    (x_train, y_train), (x_test, y_test) = get_mnist_data()
    model = MyMnistModel()

    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                  metrics=['sparse_categorical_accuracy'])

    model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
    model.summary()
    # 保存模型
    model.save(model_path, save_format="tf")
    del model  # 删除现有模型


def local_image_demo():
    # 本地手写图片识别
    # 参考 https://blog.csdn.net/great_yzl/article/details/120776341
    img = Image.open('./data/image/8.jpg')  # 载入自己的图片
    img = img.resize((28, 28))  # 设置图片大小
    gray_img = img.convert('L')
    mun_img = np.array(gray_img)
    # convert.show()
    mun_img = mun_img / 255.0
    x_test__reshape = mun_img.reshape(1, 28, 28)
    print(x_test__reshape)

    model_loaded = tf.keras.models.load_model(model_path)
    loaded_evaluate = model_loaded.predict(x_test__reshape)
    print(loaded_evaluate)
    prediction = np.argmax(loaded_evaluate, axis=1)  # 找出最大值
    print('预测结果:', prediction)


if __name__ == '__main__':
    # train_model()
    local_image_demo()

用于验证的手写数字图片如下
TensorFlow2.0 搭建手写数字识别网络_第1张图片
TensorFlow2.0 搭建手写数字识别网络_第2张图片
请添加图片描述
请添加图片描述

但是有一个问题,
对于自己手写数字的识别率并不是很高,我现在也不知道哪里出了问题,
后面慢慢探究这些东西

你可能感兴趣的:(Machine,Learning,网络,深度学习,keras)