Python and deep learning (7): CNN and fashion_mnist

1. Description

This article is another example of CNN, fashion_mnist, which is a dataset of clothes.
The same modular neural network as that used for handwritten digit recognition can be built to train the model.

2. Fashion_mnist in action

2.1 Import related libraries

The following third-party libraries are python-specific libraries for deep learning

# 导入tensorflow
import tensorflow as tf
# 导入keras
from tensorflow import keras
from keras.datasets import fashion_mnist
# 引入绘制acc和loss曲线的库
import matplotlib.pyplot as plt
# 引入ANN的必要的类
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten
from keras.models import Sequential
from keras import optimizers, losses

2.2 Load data

Load the fashion_mnist dataset

"1.加载数据"
"""
x_train是fashion_mnist训练集图片,大小的28*28的,y_train是对应的标签是数字
x_test是fashion_mnist测试集图片,大小的28*28的,y_test是对应的标签是数字
"""
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()  # 加载fashion_mnist数据集
print('mnist_data:', x_train.shape, y_train.shape, x_test.shape, y_test.shape)  # 打印训练数据和测试数据的形状

2.3 Data preprocessing

(1) Normalize the input picture and transform it from 0-255 to 0-1;
(2) Convert the shape of the input picture (60000,28,28) into (60000,28,28,1), which is quite (3) Perform one-hot encoding on
the label y, because the output of the neural network is 10 probability values, and y is 1 number, which cannot be calculated correspondingly when calculating the loss, so the y is independently encoded into a row vector of 10 numbers, and then the calculation of loss is performed. One-hot encoding: For example, the one-hot encoding of the 10 categories of value 1 is [0 1 0 0 0 0 0 0 0 0, that is, the position of 1 is 1 , and the remaining positions are 0.

"2.数据预处理"


def preprocess(x, y):  # 数据预处理函数
    x = tf.cast(x, dtype=tf.float32) / 255.  # 将输入的图片进行归一化,从0-255变换到0-1
    x = tf.reshape(x, [28, 28, 1])
    """
    # 将输入图片的形状(60000,28,28)转换成(60000,28,28,1),
    相当于将图片拉直,便于输入给神经网络
    """
    y = tf.cast(y, dtype=tf.int32)  # 将输入图片的标签转换为int32类型
    y = tf.one_hot(y, depth=10)
    """
    # 将标签y进行独热编码,因为神经网络的输出是10个概率值,而y是1个数,
    计算loss时无法对应计算,因此将y进行独立编码成为10个数的行向量,然后进行loss的计算
    独热编码:例如数值1的10分类的独热编码是[0 1 0 0 0 0 0 0 0 0,即1的位置为1,其余位置为0
    """
    return x, y

2.4 Data processing

After the data is loaded into the memory, it needs to be converted into a Dataset object in order to take advantage of various convenient functions provided by TensorFlow.
Through Dataset.from_tensor_slices, the data image x and label y of the training part can be converted into Dataset objects

batchsz = 128  # 每次输入给神经网络的图片数
"""
数据加载进入内存后,需要转换成 Dataset 对象,才能利用 TensorFlow 提供的各种便捷功能。
通过 Dataset.from_tensor_slices 可以将训练部分的数据图片 x 和标签 y 都转换成Dataset 对象
"""
db = tf.data.Dataset.from_tensor_slices((x_train, y_train))  # 构建训练集对象
db = db.map(preprocess).shuffle(60000).batch(batchsz)  # 将数据进行预处理,随机打散和批量处理
ds_val = tf.data.Dataset.from_tensor_slices((x_test, y_test))  # 构建测试集对象
ds_val = ds_val.map(preprocess).batch(batchsz)  # 将数据进行预处理,随机打散和批量处理

2.5 Building a network model

Two convolutional layers, two pooling layers, then a flattening layer (straightening the two-dimensional feature map into the fully connected layer) are constructed, followed by three fully connected layers.

"3.构建网络模型"
model = Sequential([Conv2D(filters=6, kernel_size=(5, 5), activation='relu'),
                    MaxPool2D(pool_size=(2, 2), strides=2),
                    Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
                    MaxPool2D(pool_size=(2, 2), strides=2),
                    Flatten(),
                    Dense(120, activation='relu'),
                    Dense(84, activation='relu'),
                    Dense(10,activation='softmax')])

model.build(input_shape=(None, 28, 28, 1))  # 模型的输入大小
model.summary()  # 打印网络结构

2.6 Model compilation

The optimizer of the model is Adam, another optimization method, the learning rate is 0.01, the
loss function is losses.CategoricalCrossentropy, multi-categorical cross entropy, and
the performance index is accuracy.

"4.模型编译"
model.compile(optimizer='Adam',
              loss=losses.CategoricalCrossentropy(from_logits=False),
              metrics=['accuracy']
                )
"""
模型的优化器是Adam
损失函数是losses.CategoricalCrossentropy,
性能指标是正确率accuracy
"""

2.7 Model training

The number of model training is 20, and the test is performed every 1 cycle

"5.模型训练"
history = model.fit(db, epochs=20, validation_data=ds_val, validation_freq=1)
"""
模型训练的次数是20,每1次循环进行测试
"""

2.8 Model saving

Save the model in .h5 file format

"6.模型保存"
model.save('cnn_fashion.h5')  # 以.h5文件格式保存模型

2.9 Model Evaluation

Get the correct rate of the test set

"7.模型评价"
model.evaluate(ds_val)  # 得到测试集的正确率

2.10 Model testing

Test the model

"8.模型测试"
sample = next(iter(ds_val))  # 取一个batchsz的测试集数据
x = sample[0]  # 测试集数据
y = sample[1]  # 测试集的标签
pred = model.predict(x)  # 将一个batchsz的测试集数据输入神经网络的结果
pred = tf.argmax(pred, axis=1)  # 每个预测的结果的概率最大值的下标,也就是预测的数字
y = tf.argmax(y, axis=1)  # 每个标签的最大值对应的下标,也就是标签对应的数字
print(pred)  # 打印预测结果
print(y)  # 打印标签数字

2.11 Visualization of model training results

Visualize the training results of the model

"9.模型训练时的可视化"
# 显示训练集和验证集的acc和loss曲线
acc = history.history['accuracy']  # 获取模型训练中的accuracy
val_acc = history.history['val_accuracy']  # 获取模型训练中的val_accuracy
loss = history.history['loss']  # 获取模型训练中的loss
val_loss = history.history['val_loss']  # 获取模型训练中的val_loss
# 绘值acc曲线
plt.figure(1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
# 绘制loss曲线
plt.figure(2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()  # 将结果显示出来

3. CNN model visualization results of fashion_mnist

Epoch 1/20
469/469 [==============================] - 13s 21ms/step - loss: 0.6610 - accuracy: 0.7600 - val_loss: 0.5067 - val_accuracy: 0.8097
Epoch 2/20
469/469 [==============================] - 12s 24ms/step - loss: 0.4408 - accuracy: 0.8375 - val_loss: 0.4202 - val_accuracy: 0.8491
Epoch 3/20
469/469 [==============================] - 12s 24ms/step - loss: 0.3844 - accuracy: 0.8595 - val_loss: 0.3868 - val_accuracy: 0.8605
Epoch 4/20
469/469 [==============================] - 13s 27ms/step - loss: 0.3507 - accuracy: 0.8707 - val_loss: 0.3924 - val_accuracy: 0.8533
Epoch 5/20
469/469 [==============================] - 13s 27ms/step - loss: 0.3272 - accuracy: 0.8805 - val_loss: 0.3621 - val_accuracy: 0.8674
Epoch 6/20
469/469 [==============================] - 14s 27ms/step - loss: 0.3106 - accuracy: 0.8859 - val_loss: 0.3436 - val_accuracy: 0.8728
Epoch 7/20
469/469 [==============================] - 14s 28ms/step - loss: 0.2923 - accuracy: 0.8923 - val_loss: 0.3429 - val_accuracy: 0.8732
Epoch 8/20
469/469 [==============================] - 14s 29ms/step - loss: 0.2821 - accuracy: 0.8962 - val_loss: 0.3268 - val_accuracy: 0.8802
Epoch 9/20
469/469 [==============================] - 14s 28ms/step - loss: 0.2714 - accuracy: 0.8994 - val_loss: 0.3208 - val_accuracy: 0.8832
Epoch 10/20
469/469 [==============================] - 14s 28ms/step - loss: 0.2621 - accuracy: 0.9031 - val_loss: 0.3187 - val_accuracy: 0.8822
Epoch 11/20
469/469 [==============================] - 12s 24ms/step - loss: 0.2517 - accuracy: 0.9059 - val_loss: 0.3154 - val_accuracy: 0.8870
Epoch 12/20
469/469 [==============================] - 12s 25ms/step - loss: 0.2418 - accuracy: 0.9098 - val_loss: 0.3058 - val_accuracy: 0.8928
Epoch 13/20
469/469 [==============================] - 12s 25ms/step - loss: 0.2344 - accuracy: 0.9129 - val_loss: 0.3182 - val_accuracy: 0.8885
Epoch 14/20
469/469 [==============================] - 12s 24ms/step - loss: 0.2277 - accuracy: 0.9150 - val_loss: 0.3212 - val_accuracy: 0.8824
Epoch 15/20
469/469 [==============================] - 12s 24ms/step - loss: 0.2190 - accuracy: 0.9177 - val_loss: 0.2903 - val_accuracy: 0.8981
Epoch 16/20
469/469 [==============================] - 12s 24ms/step - loss: 0.2141 - accuracy: 0.9198 - val_loss: 0.3071 - val_accuracy: 0.8895
Epoch 17/20
469/469 [==============================] - 12s 25ms/step - loss: 0.2091 - accuracy: 0.9211 - val_loss: 0.3042 - val_accuracy: 0.8897
Epoch 18/20
469/469 [==============================] - 13s 26ms/step - loss: 0.2018 - accuracy: 0.9239 - val_loss: 0.2985 - val_accuracy: 0.8973
Epoch 19/20
469/469 [==============================] - 13s 26ms/step - loss: 0.1942 - accuracy: 0.9275 - val_loss: 0.2867 - val_accuracy: 0.9026
Epoch 20/20
469/469 [==============================] - 12s 25ms/step - loss: 0.1887 - accuracy: 0.9286 - val_loss: 0.3019 - val_accuracy: 0.9005
79/79 [==============================] - 1s 9ms/step - loss: 0.3019 - accuracy: 0.9005

insert image description here
insert image description here
From the above results, it can be seen that the accuracy of the model reached 90%.

4. Complete code

# python练习
# 重新学习时间:2023/5/2 14:46
# 导入tensorflow
import tensorflow as tf
# 导入keras
from tensorflow import keras
from keras.datasets import fashion_mnist
# 引入绘制acc和loss曲线的库
import matplotlib.pyplot as plt
# 引入ANN的必要的类
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten
from keras.models import Sequential
from keras import optimizers, losses

"1.加载数据"
"""
x_train是fashion_mnist训练集图片,大小的28*28的,y_train是对应的标签是数字
x_test是fashion_mnist测试集图片,大小的28*28的,y_test是对应的标签是数字
"""
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()  # 加载fashion_mnist数据集
print('mnist_data:', x_train.shape, y_train.shape, x_test.shape, y_test.shape)  # 打印训练数据和测试数据的形状

"2.数据预处理"


def preprocess(x, y):  # 数据预处理函数
    x = tf.cast(x, dtype=tf.float32) / 255.  # 将输入的图片进行归一化,从0-255变换到0-1
    x = tf.reshape(x, [28, 28, 1])
    """
    # 将输入图片的形状(60000,28,28)转换成(60000,28,28,1),
    相当于将图片拉直,便于输入给神经网络
    """
    y = tf.cast(y, dtype=tf.int32)  # 将输入图片的标签转换为int32类型
    y = tf.one_hot(y, depth=10)
    """
    # 将标签y进行独热编码,因为神经网络的输出是10个概率值,而y是1个数,
    计算loss时无法对应计算,因此将y进行独立编码成为10个数的行向量,然后进行loss的计算
    独热编码:例如数值1的10分类的独热编码是[0 1 0 0 0 0 0 0 0 0,即1的位置为1,其余位置为0
    """
    return x, y


batchsz = 128  # 每次输入给神经网络的图片数
"""
数据加载进入内存后,需要转换成 Dataset 对象,才能利用 TensorFlow 提供的各种便捷功能。
通过 Dataset.from_tensor_slices 可以将训练部分的数据图片 x 和标签 y 都转换成Dataset 对象
"""
db = tf.data.Dataset.from_tensor_slices((x_train, y_train))  # 构建训练集对象
db = db.map(preprocess).shuffle(60000).batch(batchsz)  # 将数据进行预处理,随机打散和批量处理
ds_val = tf.data.Dataset.from_tensor_slices((x_test, y_test))  # 构建测试集对象
ds_val = ds_val.map(preprocess).batch(batchsz)  # 将数据进行预处理,随机打散和批量处理

"3.构建网络模型"
model = Sequential([Conv2D(filters=6, kernel_size=(5, 5), activation='relu'),
                    MaxPool2D(pool_size=(2, 2), strides=2),
                    Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
                    MaxPool2D(pool_size=(2, 2), strides=2),
                    Flatten(),
                    Dense(120, activation='relu'),
                    Dense(84, activation='relu'),
                    Dense(10,activation='softmax')])

model.build(input_shape=(None, 28, 28, 1))  # 模型的输入大小
model.summary()  # 打印网络结构

"4.模型编译"
model.compile(optimizer='Adam',
              loss=losses.CategoricalCrossentropy(from_logits=False),
              metrics=['accuracy']
                )
"""
模型的优化器是Adam
损失函数是losses.CategoricalCrossentropy,
性能指标是正确率accuracy
"""

"5.模型训练"
history = model.fit(db, epochs=20, validation_data=ds_val, validation_freq=1)
"""
模型训练的次数是20,每1次循环进行测试
"""
"6.模型保存"
model.save('cnn_fashion.h5')  # 以.h5文件格式保存模型

"7.模型评价"
model.evaluate(ds_val)  # 得到测试集的正确率

"8.模型测试"
sample = next(iter(ds_val))  # 取一个batchsz的测试集数据
x = sample[0]  # 测试集数据
y = sample[1]  # 测试集的标签
pred = model.predict(x)  # 将一个batchsz的测试集数据输入神经网络的结果
pred = tf.argmax(pred, axis=1)  # 每个预测的结果的概率最大值的下标,也就是预测的数字
y = tf.argmax(y, axis=1)  # 每个标签的最大值对应的下标,也就是标签对应的数字
print(pred)  # 打印预测结果
print(y)  # 打印标签数字

"9.模型训练时的可视化"
# 显示训练集和验证集的acc和loss曲线
acc = history.history['accuracy']  # 获取模型训练中的accuracy
val_acc = history.history['val_accuracy']  # 获取模型训练中的val_accuracy
loss = history.history['loss']  # 获取模型训练中的loss
val_loss = history.history['val_loss']  # 获取模型训练中的val_loss
# 绘值acc曲线
plt.figure(1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.legend()
# 绘制loss曲线
plt.figure(2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.legend()
plt.show()  # 将结果显示出来

Guess you like

Origin blog.csdn.net/qq_47598782/article/details/131905674