mnist手写数字识别

1、训练模型

import tensorflow as tf
from tensorflow.keras import models, layers, optimizers
import matplotlib.pyplot as plt

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0  #归一化
#print(x_train.shape,x_test.shape,y_train.shape,y_test.shape)
y=tf.one_hot(y_train,depth=10)  #depth ont-hot 编码的长度
#print(y)

#######将图片和对应的号码显示出来
# def show_single_image(img_arr, label):
#     plt.imshow(img_arr, cmap='binary')
#     plt.title('%i' % label)
#     plt.show()
#
#
# image_id = 8
# show_single_image(x_train[image_id], y_train[image_id])
model = models.Sequential([layers.Conv2D(filters=6, kernel_size=3, strides=1, input_shape=(28, 28, 1)),
                           layers.MaxPooling2D(pool_size=2, strides=2),
                           layers.ReLU(),
                           layers.Conv2D(filters=16, kernel_size=3, strides=1),
                           layers.MaxPooling2D(pool_size=2, strides=2),
                           layers.ReLU(),
                           layers.Flatten(),
                           layers.Dense(120, activation='relu'),
                           layers.Dropout(0.5),
                           layers.Dense(84, activation='relu'),
                           layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
                  metrics=['sparse_categorical_accuracy'])
model.fit(x_train, y_train, epochs=5,validation_data=(x_test, y_test),validation_freq=1)
model.summary()
test_loss, test_acc=model.evaluate(x_test,  y_test)

#print(test_loss, test_acc)
model.save('my_mnist.h5')

2、测试自己手写数字

import tensorflow as tf
import numpy as np


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


def preprocess_image(image):
    image = tf.image.decode_jpeg(image, channels=1)
    image = tf.image.resize(image, [28, 28])
    image /= 255.0  # normalize to [0,1] range
    image = tf.reshape(image, [28, 28, 1])
    return image


def load_and_preprocess_image(path):
    image = tf.io.read_file(path)
    return preprocess_image(image)


filepath = 'hh.jpg'
test_my_img = load_and_preprocess_image(filepath)
test_my_img = (np.expand_dims(test_my_img, 0))
my_result = model.predict(test_my_img)
print('自己的图片预测值 =', np.argmax(my_result))


在这里插入图片描述
输出:自己的图片预测值 = 6

你可能感兴趣的:(机器学习,python,tensorflow,深度学习)