• 深度学习100例——卷积神经网络(CNN)实现服装图像分类


    活动地址:CSDN21天学习挑战赛

    深度学习100例——卷积神经网络(CNN)服装图像分类

    1. 前期准备工作

    我的环境:

    在这里插入图片描述

    1.1 设置GPU
    import tensorflow as tf
    gpus = tf.config.list_physical_devices("GPU") # tf.config.list_physical_devices# 获得当前主机上某种特定运算设备类型(如 GPU 或 CPU )的列表
    
    if gpus:
        gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPU
        tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用
        tf.config.set_visible_devices([gpu0],"GPU") # 设置可见设备列表
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    当前机器只有一个GPU

    1.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.fashion_mnist.load_data()
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    查看一下数据

    在这里插入图片描述

    (train_images, train_labels) → 训练集

    (test_images, test_labels) → 测试集

    和mnist数据集类似,60000个训练样本,10000个测试样本,且70000张图片均是28 x 28的像素图。

    每张图的像素值介于 0 - 255 之间,标签是整数数组,介于 0 - 9 之间。

    标签对应表:

    标签标签
    0T恤5凉鞋
    1裤子6衬衫
    2套头衫7运动鞋
    3连衣裙8
    4外套9短靴

    在这里插入图片描述

    1.3 数据归一化

    → 将像素的值标准化到 0 - 1的区间内。

    train_images, test_images = train_images / 255.0, test_images / 255.0
    train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
    
    • 1
    • 2

    归一化前:

    在这里插入图片描述

    归一化后:

    在这里插入图片描述

    1.4 调整图片格式
    train_images = train_images.reshape((60000, 28, 28, 1))
    test_images = test_images.reshape((10000, 28, 28, 1))
    
    train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
    
    • 1
    • 2
    • 3
    • 4

    数据维度转换,增加通道数

    在这里插入图片描述

    1.5 可视化查看数据图像
    class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
                   'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
    
    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]])
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    同样只展示0-19索引的样本图像。

    2. 构建CNN网络

    2.1 构建网络
    model = models.Sequential([
        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), #卷积层1,卷积核3 x 3
        layers.MaxPooling2D((2, 2)),                   #池化层1,2 x 2采样
        layers.Conv2D(64, (3, 3), activation='relu'),  #卷积层2,卷积核3 x 3
        layers.MaxPooling2D((2, 2)),                   #池化层2,2 x 2采样
        layers.Conv2D(64, (3, 3), activation='relu'),  #卷积层3,卷积核3 x 3
        
        layers.Flatten(),                      #Flatten层,连接卷积层与全连接层
        layers.Dense(64, activation='relu'),   #全连接层,特征进一步提取
        layers.Dense(10)                       #输出层,输出预期结果
    ])
    
    model.summary()  # 打印网络结构
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    2.2 模型说明

    在这里插入图片描述

    各层的作用:

    • 输入层:将数据输入到训练网络
    • 卷积层:使用卷积核提取图片的特征
    • 池化层:进行下采样,用更高层的抽象表示图像特征
    • Flatten层:将多维的输入一维化,常用在卷积层到全连接层的过渡
    • 全连接层:特征提取器
    • 输出层:输出结果

    3. 编译模型

    model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
    
    • 1

    compile()方法用于设置训练时,使用的优化器optimizer、损失函数loss、准确率评测标准metrics

    SparseCategoricalCrossentropy → 交叉熵损失函数,当from_logits参数为True时,会使用softmax将预测y转换为概率。

    4.训练模型

    history = model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))
    
    • 1

    在这里插入图片描述

    5. 模型预测

    模型的预测结果是一个包含10个数字的数组,代表模型对10种不同服装中每种时装的 置信度

    pre = model.predict(test_images)
    
    • 1

    在这里插入图片描述

    6. 模型评估

    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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    Java:实现FibonacciHeap数据结构算法(附完整源码)
    HarmonyOS(35) @State使用注意事项
    基于粒子群算法和遗传算法优化的高速列车横向悬挂
    C语言 每日一题 11.9 day15
    关于网站安全的一些讨论
    配置FTP站点操作步骤—图解
    南京邮电大学计算机考研资料汇总
    【CSDN竞赛】第八期解题报告
    数字孪生智慧制造生产线项目实施方案,平台认知与概念
    Java 项目-基于微信小程序的自助购药系统
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126133156