- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊 | 接辅导、项目定制
- 🚀 文章来源:K同学的学习圈子
CIFAR10是CIFAR-10是一个更接近普适物体的彩色图像数据集。CIFAR-10 是由Hinton 的学生Alex Krizhevsky 和Ilya Sutskever 整理的一个用于识别普适物体的小型数据集。一共包含10 个类别的RGB 彩色图片:飞机( airplane )、汽车( automobile )、鸟类( bird )、猫( cat )、鹿( deer )、狗( dog )、蛙类( frog )、马( horse )、船( ship )和卡车( truck )。每个图片的尺寸为32 × 32 ,每个类别有6000个图像,数据集中一共有50000 张训练图片和10000 张测试图片。
与MNIST 数据集中目比, CIFAR-10 真高以下不同点
(1)、CIFAR-10 是3 通道的彩色RGB 图像,而MNIST 是灰度图像。
(2)、CIFAR-10 的图片尺寸为32 × 32 , 而MNIST 的图片尺寸为28 × 28 ,比MNIST 稍大。
(3)、相比于手写字符, CIFAR-10 含有的是现实世界中真实的物体,不仅噪声很大,而且物体的比例、特征都不尽相同,这为识别带来很大困难。
下面本文以CIFAR10数据集为例实现一个两层卷积神经网络的简单模型。电脑配置
Current time: 2023-11-03 19:07:10.120599
Python version: 3.6.15 (default, Dec 3 2021, 18:25:24) [MSC v.1916 64 bit (AMD64)]
Tensorflow version 2.6.2
Matplotlib version 3.3.4
Device: CPU
一 前期准备
1. 设置GPU
在anaconda prompt中新建名称为tensorflow_cpu的环境,并安装tensorflow,matplotlib的包。同时脚本代码为ipy文件,需要安装ipykernel。
conda create -n tensorflow_cpu python=3.6
conda activate tensorflow_cpu
pip install tensorflow
pip install matplotlib
pip install ipykernel
输出使用的电脑配置。
from datetime import datetime
import sys
import matplotlib
print("Current time:", datetime.today())
print("Python version:", sys.version)
print("Tensorflow version", tf.__version__)
print("Matplotlib version", matplotlib.__version__)
gpus = tf.config.list_physical_devices("GPU")
if gups:
gpu0 = gpus[0]
tf.config.experimental.set_memory_growth(gpu0, True)
tf.config.set_visible_devices([gpu0],"GPU")
print("Device: GPU")
else:
print("Device: CPU")
2. 导入数据
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import matplotlib.pyplot as plt
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
print(len(train_images))
print(len(test_images))
50000
10000
3. 归一化
train_images, test_images = train_images/255, test_images/255
train_images.shape,test_images.shape, train_labels.shape,test_labels.shape
((50000, 32, 32, 3), (10000, 32, 32, 3), (50000, 1), (10000, 1))
4. 可视化
class_names = ['airplane', 'automobile','bird','cat','deer','dog','frog','horse','ship','truck']
plt.figure(figsize=(20,10))
for i in range(20):
plt.subplot(5,10, i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i][0]])
plt.show()
二 构建网络模型
1. 搭建模型
构建三层卷积、两层全连接的神经网络。
model = models.Sequential([
layers.Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3)),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64,(3,3),activation='relu'),
layers.MaxPooling2D((2,2)),
layers.Conv2D(64, (3,3), activation='relu'),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10)
])
model.summary()
模型结构如下所示
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 30, 30, 32) 896
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 13, 13, 64) 18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 4, 4, 64) 36928
_________________________________________________________________
flatten (Flatten) (None, 1024) 0
_________________________________________________________________
dense (Dense) (None, 64) 65600
_________________________________________________________________
dense_1 (Dense) (None, 10) 650
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
三 编译
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))
训练过程如下
Epoch 1/10
1563/1563 [==============================] - 32s 20ms/step - loss: 1.1760 - accuracy: 0.5820 - val_loss: 1.2314 - val_accuracy: 0.5684
Epoch 2/10
1563/1563 [==============================] - 30s 19ms/step - loss: 1.0230 - accuracy: 0.6395 - val_loss: 1.0207 - val_accuracy: 0.6404
Epoch 3/10
1563/1563 [==============================] - 27s 17ms/step - loss: 0.9278 - accuracy: 0.6748 - val_loss: 0.9497 - val_accuracy: 0.6683
Epoch 4/10
1563/1563 [==============================] - 27s 17ms/step - loss: 0.8613 - accuracy: 0.6992 - val_loss: 0.9768 - val_accuracy: 0.6634
Epoch 5/10
1563/1563 [==============================] - 27s 17ms/step - loss: 0.8050 - accuracy: 0.7179 - val_loss: 0.9045 - val_accuracy: 0.6842
Epoch 6/10
1563/1563 [==============================] - 26s 17ms/step - loss: 0.7548 - accuracy: 0.7341 - val_loss: 0.8928 - val_accuracy: 0.6960
Epoch 7/10
1563/1563 [==============================] - 27s 17ms/step - loss: 0.7123 - accuracy: 0.7500 - val_loss: 0.8862 - val_accuracy: 0.6960
Epoch 8/10
1563/1563 [==============================] - 27s 17ms/step - loss: 0.6743 - accuracy: 0.7613 - val_loss: 0.8551 - val_accuracy: 0.7043
Epoch 9/10
1563/1563 [==============================] - 27s 17ms/step - loss: 0.6401 - accuracy: 0.7732 - val_loss: 0.8911 - val_accuracy: 0.6996
Epoch 10/10
1563/1563 [==============================] - 30s 19ms/step - loss: 0.6060 - accuracy: 0.7849 - val_loss: 0.8726 - val_accuracy: 0.7102
五 预测
plt.imshow(test_images[1])
import numpy as np
pre = model.predict(test_images)
print(class_names[np.argmax(pre[1])])
ship
![在这里插入图片描述](https://img-blog.csdnimg.cn/cfebf30b0b254833a5c12e238fa72ca4.png#pic_center
六 模型评估
import matplotlib.pyplot as plt
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)
test_loss
test_acc