本案例采用CNN卷集神经网络对fashion——mnist数据进行分类
导入包
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
查看tensorflow的版本
print('Tensorflow version: {}'.format(tf.__version__))
Tensorflow version: 2.3
读取数据
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
查看数据图像的shape(维度)
train_images.shape, train_labels.shape
test_images.shape, test_labels.shape
(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)
测试标签的输出格式
test_labels
[9 2 1 ... 8 1 5]
测试数据的数量
len(test_labels)
10000
采用CNN算法进行分类输入的数据的形式是(batch, length, width, channl) 的形式输入训练数据,需要数据维度扩张,在最后一维扩张
train_images = np.expand_dims(train_images, -1)
test_images = np.expand_dims(test_images, -1)
扩张维度后数据图像的shape(维度)
train_images.shape, train_labels.shape
test_images.shape, test_labels.shape
(60000, 28, 28, 1) (60000,)
(10000, 28, 28, 1) (10000,)
扩张维度前数据的维度是(10000, 28, 28),扩张后数据的维度是(10000, 28, 28, 1) 28281说明是一张28*28大小的黑白图片。
建立模型
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, (3, 3), input_shape=(28, 28, 1), activation='relu'))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu'))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu'))
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu'))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(10, activation='softmax'))
模型概述
model.summary()
模型编译:目标值是顺序编码,使用sparse_categorical_crossentropy损失函数
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['acc'])
模型训练
history = model.fit(train_images, train_labels, epochs=30, validation_data=(test_images, test_labels))
Epoch 1/30
1875/1875 [==============================] - 286s 152ms/step - loss: 1.3922 - acc: 0.4926 - val_loss: 0.5285 - val_acc: 0.8145
Epoch 2/30
1875/1875 [==============================] - 283s 151ms/step - loss: 0.5047 - acc: 0.8157 - val_loss: 0.3926 - val_acc: 0.8548
Epoch 3/30
1875/1875 [==============================] - 282s 150ms/step - loss: 0.4276 - acc: 0.8433 - val_loss: 0.3517 - val_acc: 0.8641
Epoch 4/30
.......
模型优化
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(64, (3, 3), input_shape=(28, 28, 1), activation='relu', padding='same'))
model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.MaxPool2D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.Conv2D(512, (3, 3), activation='relu', padding='same'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
模型优化中增加了MaxPool2D层和Dropout层。