再战卷积,实现Fashion-MNIST分类

315 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

之前已经接触过卷积了,对于概念就不再详细叙述,前几篇博客讲述的都是直接定义网络结构,相当于一个基础的构建,主要是为了学习和体会如何构建自己的网络这个过程,今天使用tensorflow提供的高阶API实现一个网络的模型搭建与训练过程,废话不多说,直接上代码,在代码的注释中详细的体会其中的不同。

# Fashion-MNIST数据集与MNIST数据集差不多,也是具有10类,输入都是28x28的灰度图
from tensorflow.keras import estimator
import tensorflow as tf
from tensorflow.keras import datasets, models, layers, optimizers
import numpy as np
import matplotlib.pyplot as plt

# 导入数据集
(train_x, train_y), (test_x, test_y) = datasets.fashion_mnist.load_data()
NUM_CLASS = 10
SHUFFLE_SIZE = 100
BATCH_SIZE = 512
EPOCH = 100
# 查看一下数据集内容
# plt.imshow(train_x[0])
# plt.show()
# print(train_y[0])
# 数据集和训练集大小
train_size = len(train_x)
test_size = len(test_x)
# 处理数据集,归一化
train_x = np.array(train_x, dtype=np.float32) / 255
train_x = np.expand_dims(train_x, axis=3)
test_x = np.array(test_x, dtype=np.float32) / 255
test_x = np.expand_dims(test_x, axis=3)

train_y = tf.keras.utils.to_categorical(train_y, num_classes=NUM_CLASS)
test_y = tf.keras.utils.to_categorical(test_y, num_classes=NUM_CLASS)

train_y = train_y.astype(np.float32)
test_y = test_y.astype(np.float32)

# 使用tensorflow提供的API构建网络
input_data = tf.keras.Input(shape=(28, 28, 1))
model = layers.Convolution2D(filters=32, kernel_size=(3, 3), activation='relu')(input_data)
model = layers.MaxPooling2D(pool_size=(2, 2), strides=2)(model)
model = layers.Convolution2D(filters=64, kernel_size=(3, 3), activation='relu')(model)
model = layers.MaxPooling2D(pool_size=(2, 2), strides=2)(model)
model = layers.Convolution2D(filters=64, kernel_size=(3, 3), activation='relu')(model)
model = layers.Flatten()(model)
model = layers.Dense(64, activation='relu')(model)
predicts = layers.Dense(NUM_CLASS, activation='softmax')(model)
out = tf.keras.Model(input_data, predicts)
out.summary()

optimizer = optimizers.SGD()
out.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

# 定义策略
strategy = tf.distribute.MirroredStrategy()
config = tf.estimator.RunConfig(train_distribute=strategy)
# 将模型转化为便捷的tetimator
estimator = tf.keras.estimator.model_to_estimator(out, config=config)


# 定义输入函数
def in_funtion(in_image, label, epochs, batch_size):
    dataset = tf.data.Dataset.from_tensor_slices((in_image, label))
    # 随机打乱数据集
    dataset = dataset.shuffle(SHUFFLE_SIZE).repeat(epochs).batch(batch_size)
    dataset = dataset.prefetch(None)
    return dataset


# 训练
rest = estimator.train(input_fn=lambda:in_funtion(train_x, train_y, epochs=EPOCH, batch_size=BATCH_SIZE))
print(rest)
# 评估
estimator.evaluate(lambda:in_funtion(test_x, test_y, epochs=1, batch_size=BATCH_SIZE))

代码也只是做一些参考,至于具体的内容,也没有什么需要特别注意的地方,前面已经说过很多遍了,这次只是数据集名字换了一下,内容不同了,格式还是一模一样。