【无标题】

语言环境:Python3.9.0
编译器:jupyter notebook
深度学习环境:TensorFlow2.12.0

  • 目录

一、前期工作
2. 设置GPU
3. 导入数据
4. 归一化
5. 可视化
二、构建CNN网络
三、编译
四、训练模型
五、预测
六、模型评估

前期工作

  1. 设置GPU
import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")
if gpus:
    gpu0 = gpus[0]
    tf.config.experimental.set_memory_growth(gpu0,True)
    tf.config.set_visible_devices([gpu0],"GPU")

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()
#导入数据,数据内容分别是:训练集图片、训练集标签、测试集图片、测试集标签

3.图像数据归一化:

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()

输出结果:
在这里插入图片描述
4.进行训练数据的初步可视化

class_names = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','tiruck']
#将数据集前20个图片数据可视化显示
#图像大小:20*10
plt.figure(figsize=(20,10))
for i in range(20):
    #将整个figure分成2行10列
    plt.subplot(2,10,i+1)
    #设置不显示X轴刻度
    plt.xticks([])
    #设置不显示Y轴刻度
    plt.yticks([])
    #设置不显示子图网格线
    plt.grid(False)
    #图像展示,cmap为颜色图谱,“plt.cm.binar”为matplotlib.cm中的色表
    plt.imshow(train_images[i],cmap=plt.cm.binary)
    #设置X轴标签显示为图片集对应的标签名
    plt.xlabel(class_names[train_labels[i][0]])
    #显示图片
plt.show()

输出
在这里插入图片描述

二、构建CNN模型

#构建CNN网络模型
#创建并设置卷积神经网络
#卷积层、池化层、全连接层
model = models.Sequential([
#设置二维卷积层1,设置32个3*3的卷积核,activation参数将激活函数设置为Relu函数,
#input_shape参数将图层的输入形状设置为(32,32,3)
#Relu函数作为激活函数可以增强判定函数和整个神经网络的非线性特性,而本身并不会改变卷积层
#相比于其他函数来说,Relu函数更受青睐,这是因为它可以将神经网络的训练速度提升数倍,而不会对模型的泛化准确度造成影响。
    
    layers.Conv2D(32,(3,3),activation='relu',input_shape=(32,32,3)),
    #池化层1,2*2采样
    layers.MaxPooling2D((2,2)),
    #设置二维卷积层2,设置64个3*3卷积核,activation参数将激活函数设置为Relu
    layers.Conv2D(64,(3,3),activation='relu'),
    #池化层2,2*2采样
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64,(3,3),activation='relu'),
    #Flatten层,连接卷积层与全连接层
    layers.Flatten(),
    #全连接层,特征进一步提取,64为输出空间的维数,activation参数将激活函数设置为Relu函数
    layers.Dense(64,activation='relu'),
    #输出层,输出预期结果,10为输出空间的维数
    layers.Dense(10)
])
model.summary()

OUT:
在这里插入图片描述
编译

#设置优化器、损失函数以及metrics
#model.compile()方法用于在配置训练方法时,告知训练时用的优化器、损失函数和准确率测评标准
model.compile(
    #设置优化器为Adam优化器
              optimizer='adam',
    #设置损失函数为交叉熵损失函数(tf.keras.losses.SparseCategoricalCrossentropy())
    #参数:from_logits,为True时,会将y_pred转化为概率(用softmax),否则不进行转换,通常情况下用True更稳定
              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))

训练过程
在这里插入图片描述
输出预测的第一张图片结果
在这里插入图片描述
模型评估

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)

在这里插入图片描述

思考与总结

1.在进行图像的归一化处理时,输出的结果是什么含义?
在这里插入图片描述
2.在这里插入图片描述
本意是分成5行10列,怎么出现的是两行10
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值