本实例主要参考博客https://blog.csdn.net/cos_lee/article/details/93882520,里面有数据集的下载链接,讲解了加载数据集的方法,请大家阅读,这里不再赘述。唯一不同的是,此博客采用普通的三层全连接神经网络处理数据,而本例采用卷积神经网络。
关于安装tensorflow,推荐大家使用anaconda navigator下载,用字符界面容易出错。
本例中使用的Fashion MINST数据是四个.gz文件,如下图。文件路径根据实际情况设定。
代码如下。卷积神经网络训练时间很长,请耐心等待。
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import gzip
import math
def get_data():
# 文件获取
train_image = r"E:/Data/fashion_minist/train-images-idx3-ubyte.gz"
test_image = r"E:/Data/fashion_minist/t10k-images-idx3-ubyte.gz"
train_label = r"E:/Data/fashion_minist/train-labels-idx1-ubyte.gz"
test_label = r"E:/Data/fashion_minist/t10k-labels-idx1-ubyte.gz" #文件路径
paths = [train_label, train_image, test_label,test_image]
with gzip.open(paths[0], 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[1], 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with gzip.open(paths[2], 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[3], 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
(train_images, train_labels), (test_images, test_labels) = get_data()
if __name__ == '__main__':
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
#数据归一化
train_images = train_images / 255.0
test_images = test_images / 255.0
num_train_examples=len(train_images)
num_test_examples=len(test_images)
'''
#打印其中一个样本
image=test_images[1]
plt.figure()
plt.imshow(image,cmap=plt.cm.binary)
plt.colorbar()
plt.grid(False)
plt.show()
#打印前25个样本
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
#print(train_images[i])
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
'''
#卷积神经网络
model = tf.keras.Sequential([
#卷积层,输出结点32,卷积核3*3,边界补零,激活函数采用relu,
#输入格式28*28*1,图像是28*28像素,1个卷积核,下同
tf.keras.layers.Conv2D(32, (3,3), padding='same', activation=tf.nn.relu,
input_shape=(28, 28, 1)),
#池化层,池化维度2*2,步长为2,下同
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
tf.keras.layers.Conv2D(64, (3,3), padding='same', activation=tf.nn.relu),
tf.keras.layers.MaxPooling2D((2, 2), strides=2),
#将数据压为一维
tf.keras.layers.Flatten(),
#最后两层是全连接层
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
#转换数据格式,与卷积神经网络的输入相适应
train_reshaped=np.reshape(train_images,newshape=(len(train_images),28,28,1))
test_reshaped=np.reshape(test_images,newshape=(len(test_images),28,28,1))
#模型编译
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
#模型训练
model.fit(train_reshaped, train_labels, epochs=50)
#模型测试
test_loss, test_acc = model.evaluate(test_reshaped, test_labels)
print('Test accuracy:', test_acc)