写点什么

tensorflow 实现 cifar10 彩色图像多类别分类

用户头像
AI_robot
关注
发布于: 2021 年 03 月 31 日
tensorflow实现cifar10彩色图像多类别分类

CIFAR10 数据集包含 10 类,共 60000 张彩色图片,每类图片有 6000 张。此数据集中 50000 个样例被作为训练集,剩余 10000 个样例作为测试集。类之间相互度立,不存在重叠的部分。因此,cifar10 分类就是一个图像多分类任务。Keras 另一个好处在于已经集成了很多常见的数据集和模型,在接口里可以直接调用。


代码下载:https://github.com/wennaz/Deep_Learning



import tensorflow as tf
from tensorflow.keras import datasets, layers, modelsimport matplotlib.pyplot as plt#CIFAR10 数据集包含 10 类,共 60000 张彩色图片,每类图片有 6000 张。此数据集中 50000 个样例被作为训练集,剩余 10000 个样例作为测试集。类之间相互度立,不存在重叠的部分。(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# 将像素的值标准化至0到1的区间内。train_images, test_images = train_images / 255.0, test_images / 255.0#我们将测试集的前 25 张图片和类名打印出来,来确保数据集被正确加载。class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], cmap=plt.cm.binary) # 由于 CIFAR 的标签是 array, # 因此您需要额外的索引(index)。 plt.xlabel(class_names[train_labels[i][0]])plt.show()#下方展示的 6 行代码声明了了一个常见卷积神经网络,由几个 Conv2D 和 MaxPooling2D 层组成。model = models.Sequential()model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))model.add(layers.MaxPooling2D((2, 2)))model.add(layers.Conv2D(64, (3, 3), activation='relu'))#在模型的最后,您将把卷积后的输出张量(本例中形状为 (4, 4, 64))传给一个或多个 Dense 层来完成分类。model.add(layers.Flatten())model.add(layers.Dense(64, activation='relu'))model.add(layers.Dense(10))model.summary()
#编译并训练模型model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
#评估模型plt.plot(history.history['accuracy'], label='accuracy')plt.plot(history.history['val_accuracy'], label = 'val_accuracy')plt.xlabel('Epoch')plt.ylabel('Accuracy')plt.ylim([0.5, 1])plt.legend(loc='lower right')plt.show()
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)print(test_acc)
复制代码

Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz170500096/170498071 [==============================] - 17522s 103us/step




_________________________________________________________________Layer (type) Output Shape Param # =================================================================conv2d_1 (Conv2D) (None, 30, 30, 32) 896 _________________________________________________________________max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0 _________________________________________________________________conv2d_2 (Conv2D) (None, 13, 13, 64) 18496 _________________________________________________________________max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0 _________________________________________________________________conv2d_3 (Conv2D) (None, 4, 4, 64) 36928 _________________________________________________________________flatten_1 (Flatten) (None, 1024) 0 _________________________________________________________________dense_14 (Dense) (None, 64) 65600 _________________________________________________________________dense_15 (Dense) (None, 10) 650 =================================================================Total params: 122,570Trainable params: 122,570Non-trainable params: 0_________________________________________________________________Epoch 1/101563/1563 [==============================] - 89s 57ms/step - loss: 1.5250 - accuracy: 0.4467 - val_loss: 1.2551 - val_accuracy: 0.5520Epoch 2/101563/1563 [==============================] - 78s 50ms/step - loss: 1.1560 - accuracy: 0.5923 - val_loss: 1.1427 - val_accuracy: 0.5997Epoch 3/101563/1563 [==============================] - 71s 45ms/step - loss: 1.0224 - accuracy: 0.6403 - val_loss: 1.0141 - val_accuracy: 0.6483Epoch 4/101563/1563 [==============================] - 61s 39ms/step - loss: 0.9337 - accuracy: 0.6744 - val_loss: 0.9356 - val_accuracy: 0.6767Epoch 5/101563/1563 [==============================] - 59s 38ms/step - loss: 0.8591 - accuracy: 0.6997 - val_loss: 1.0341 - val_accuracy: 0.6487Epoch 6/101563/1563 [==============================] - 58s 37ms/step - loss: 0.7981 - accuracy: 0.7228 - val_loss: 0.8725 - val_accuracy: 0.6994Epoch 7/101563/1563 [==============================] - 57s 37ms/step - loss: 0.7518 - accuracy: 0.7376 - val_loss: 0.8625 - val_accuracy: 0.7005Epoch 8/101563/1563 [==============================] - 62s 40ms/step - loss: 0.7073 - accuracy: 0.7527 - val_loss: 0.9021 - val_accuracy: 0.6954Epoch 9/101563/1563 [==============================] - 66s 43ms/step - loss: 0.6669 - accuracy: 0.7657 - val_loss: 0.8610 - val_accuracy: 0.7083Epoch 10/101563/1563 [==============================] - 58s 37ms/step - loss: 0.6353 - accuracy: 0.7771 - val_loss: 0.9132 - val_accuracy: 0.6999
复制代码



用户头像

AI_robot

关注

还未添加个人签名 2021.03.31 加入

Deep Learning从业者

评论

发布
暂无评论
tensorflow实现cifar10彩色图像多类别分类