minst数据集的读取、训练和预测

首先是基于本地mnist图像数据集来进行训练

  • 笔记

首先是不管是数据集还是标签集,它都接收的是np数组,标签集接收的是int类型
关于它的输入数据的格式,nx28x28,标签的格式不是one—hot(这个看编译模型时的损失函数)。
整个流程是:1、处理数据(将其处理为模型需要的格式)。2、网络设计(也就是特征提取)。3、进行编译。4、训练。5预测和评估。

import numpy as np
from PIL import Image
import os

# print(type(train_images))
#读取本地mnist图片进行训练
#训练集
# 递归函数,用于获取文件夹内所有PNG图片的文件名
def get_png_files(folder_path):
    png_files = []
    labels=[]
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.png'):
                png_files.append(os.path.join(root, file))
                labels.append(int(os.path.basename(root)))
    return png_files,labels

# 图片文件夹路径
folder_path = 'E:\subject\pycharm_subject\\bp_cnn_recurent\mnist_train'

# 获取文件夹内所有PNG图片的文件名
png_files,train_labels = get_png_files(folder_path)
train_labels=np.array(train_labels)
# 读取每张PNG图片,并将其转换为NumPy数组
image_array_list = []
for file_path in png_files:
    # 使用Pillow库加载图片
    image = Image.open(file_path)
    # 将图片转换为NumPy数组,并添加到列表中
    image_array_list.append(np.array(image))

# 将列表中的NumPy数组堆叠为一个三维数组(或四维数组,取决于图片的通道数)
train_images = np.stack(image_array_list)
train_images=train_images/255.0

#测试集
# 图片文件夹路径
test_path = 'E:\subject\pycharm_subject\\bp_cnn_recurent\mnist_test'

# 获取文件夹内所有PNG图片的文件名
test_files,test_labels = get_png_files(test_path)
test_labels=np.array(test_labels)
# 读取每张PNG图片,并将其转换为NumPy数组
image_array_list = []
for file_path in test_files:
    # 使用Pillow库加载图片
    image = Image.open(file_path)
    # 将图片转换为NumPy数组,并添加到列表中
    image_array_list.append(np.array(image))

# 将列表中的NumPy数组堆叠为一个三维数组(或四维数组,取决于图片的通道数)
test_images = np.stack(image_array_list)
test_images=test_images/255.0

# print(type(train_images))

# 2. 网络设计
model = models.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(10, activation='softmax')
])

# 3. 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 4. 训练模型
model.fit(train_images, train_labels, epochs=5)

# 5. 模型评估
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

# 6. 预测
predictions = model.predict(test_images)

# 打印预测结果
for i in range(5):
    print("Predicted label:", np.argmax(predictions[i]))
    print("True label:", test_labels[i])

然后是基于tensorflow的mnist训练


import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
from PIL import Image
import os

# 1. 数据读取
def load_mnist():
    (train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
    # 归一化
    train_images = train_images / 255.0
    test_images = test_images / 255.0
    return (train_images, train_labels), (test_images, test_labels)

(train_images, train_labels), (test_images, test_labels) = load_mnist()

# 2. 网络设计
model = models.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(10, activation='softmax')
])

# 3. 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 4. 训练模型
model.fit(train_images, train_labels, epochs=5)

# 5. 模型评估
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)

# 6. 预测
predictions = model.predict(test_images)

# 打印预测结果
for i in range(5):
    print("Predicted label:", np.argmax(predictions[i]))
    print("True label:", test_labels[i])

posted @ 2024-05-04 16:02  玩意  阅读(4)  评论(0编辑  收藏  举报