keras数字图像识别

aistudio地址:
https://aistudio.baidu.com/aistudio/projectdetail/1484526

keras的数字图像识别

一、加载数据

MNIST数据集预加载到Keras库中,包括4个Numpy数组。
然后使用pyplot显示其中一个数组的图片

因为每次都需要重新下载,可以先手动下载到本地,然后加载文件
wget https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz

from keras.datasets import mnist
import numpy as np

# 使用mnist加载数据
# (train_images, train_labels), (test_images, test_labels) = mnist.load_data()


# 使用本地文件加载数据
train_images = np.load("/home/aistudio/work/mnist/x_train.npy", allow_pickle=True)
train_labels = np.load("/home/aistudio/work/mnist/y_train.npy", allow_pickle=True)
test_images = np.load("/home/aistudio/work/mnist/x_test.npy", allow_pickle=True)
test_labels = np.load("/home/aistudio/work/mnist/y_test.npy", allow_pickle=True)

1.1 查看数据

  • 图像是28x28 NumPy数组,像素值介于0到255之间。
  • 标签是一个整数数组,范围从0到9.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg


print(train_images.shape)
print(train_labels)
print(test_images.shape)
print(test_labels)

# 25 * 25的grid显示125张图片
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(train_labels[i])
plt.show()
(60000, 28, 28)
[5 0 4 ... 5 6 8]
(10000, 28, 28)
[7 2 1 ... 4 5 6]
output_3_1.png

二、数据预处理

2.1 图片数据三维转二维

# 三维转二维train_images

train_images_re = train_images.reshape((60000, 28 * 28))
test_images_re = test_images.reshape((10000, 28 * 28))
print(train_images_re.shape)

train_images_re = train_images_re.astype('float32') / 255
test_images_re = test_images_re.astype('float32') / 255

(60000, 784)

2.2 标签分类编码

改成one hot编码。
一个二维数组,数字5转成0. 0. 0. 0. 0. 1. 0. 0. 0. 0.,第五个元素为1.

from keras.utils import to_categorical

train_labels_re = to_categorical(train_labels)
test_labels_re = to_categorical(test_labels)

print('原始: ', train_labels)
print('转化后 - one hot: ')
for i in range(10):
    print(train_labels_re[i])



原始:  [5 0 4 ... 5 6 8]
转化后 - one hot: 
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

三、构建网络

3.1添加层

from keras import models
from keras import layers

network = models.Sequential()
# 第一层定义
# 输出,第一维大小:512
# 输入,第一维大小:28 * 28
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28, )))

# 第二层定义
network.add(layers.Dense(10, activation='softmax'))

3.1 编译

添加损失函数、优化器、监控指标

network.compile(
    optimizer='rmsprop',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

四、拟合模型

network.fit(
    train_images_re,
    train_labels_re,
    epochs=5,
    batch_size=128
)
Epoch 1/5
469/469 [==============================] - 16s 33ms/step - loss: 0.4357 - accuracy: 0.87
Epoch 2/5
469/469 [==============================] - 14s 30ms/step - loss: 0.1135 - accuracy: 0.96
Epoch 3/5
469/469 [==============================] - 15s 31ms/step - loss: 0.0691 - accuracy: 0.97
Epoch 4/5
469/469 [==============================] - 15s 33ms/step - loss: 0.0452 - accuracy: 0.98
Epoch 5/5
469/469 [==============================] - 14s 29ms/step - loss: 0.0352 - accuracy: 0.98






五、验证模型

精确度:accuracy
损失度:loss

test_loss, test_acc = network.evaluate(test_images_re, test_labels_re)
print('test_loss', test_loss)
print('test_acc', test_acc)
313/313 [==============================] - 1s 2ms/step - loss: 0.0707 - accuracy: 0.97
test_loss 0.07070968300104141
test_acc 0.9790999889373779

六、预测模型

  • 使用predict()方法进行预测,返回样本属于每一个类别的概率
  • 使用numpy.argmax()方法找到样本以最大概率所属的类别作为样本的预测标签。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

predictions = network.predict(test_images_re)

# 显示预测结果 
plt.figure(figsize=(10,10))
for i in range(25):
    pre_label = np.argmax(predictions[i])
    pre_percent = round(predictions[i][np.argmax(predictions[i])] * 100, 2)
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(test_images[i], cmap=plt.cm.binary)
    plt.xlabel(str(pre_percent) + '%: ' + str(pre_label))
plt.show()

output_17_0.png

你可能感兴趣的:(keras数字图像识别)