当前位置:网站首页>Convolutional Neural Network (CNN) for Clothing Image Classification
Convolutional Neural Network (CNN) for Clothing Image Classification
2022-08-10 05:54:00 【Ape Tongxue】
活动地址:CSDN21天学习挑战赛

In the previous case, it was not mentioned what a convolutional neural network is,接下来介绍一下
什么是卷积?
卷积神经网络(CNN),Generally used to process image data and time series data.其中“卷积”是一种数学运算,A characteristic linear operation,A neural network that uses convolution operations instead of normal matrix multiplication operations in at least one layer of the network.
含义:
After you provide this set of data to the computer,It will output the probability of a particular class describing the image(比如:80%是猫、15%是狗、5%是年).
We humans distinguish cats from dogs by features,Computers are now used to distinguish pictures of cats and dogs,It is necessary for the computer to figure out the characteristics of cats and dogs.Computers can classify pictures by looking for low-level features such as edges and curves,A more abstract concept is then constructed through a series of convolutional layers.这是CNN(卷积神经网络)A general description of how it works.
CNN架构
卷积层 conv2d
non-threaded transform layer relu/sigmoid/tanh
池化层 pooling2d
全连接层 w*x +b
卷积层
三个参数:
ksize 卷积核的大小
strides The stride over which the convolution kernel moves
padding 边缘填充
非线性变换层(也就是激活函数):
relu
sigmoid
tanh
池化层:

Pooling is divided into max pooling,平均池化,L2池化.
全连接层:
Link the final output with all features,We want to use all the features,Make decisions for final classification,最后配合softmax进行分类
整体结构:

一、读取数据
KerasA dataset loading function is provided
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()
查看数据维度
train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
'''
out:((60000, 28, 28, 1), (10000, 28, 28, 1), (60000,), (10000,))
'''You can see that the training set has 60000个28*28的图片,60000个标签,测试集有10000个28*28的图片,10000个标签.
plt.imshow(train_images[0])Check out the picture is a picture

train_images[0]

Then its value range is [0 , 255]之间.
print(np.max(train_images))
print(np.min(train_images))
#255
#0Then its value range is [0 , 255]之间.We'll do normalization next
二、数据预处理
1、在数据预处理时,首先采用reshapeThe function flattens each image matrix into a vector:
#调整数据到我们需要的格式
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
"""
输出:((60000, 28, 28, 1), (10000, 28, 28, 1), (60000,), (10000,))
"""2、数据归一化,将输入值[0,255]归一化为[0,1]的取值范围:
# 将像素的值标准化至0到1的区间内.
train_images, test_images = train_images / 255.0, test_images / 255.03、数据可视化
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(train_labels[i])
plt.show()
三、构建CNN神经网络模型
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),#卷积层1,卷积核3*3
layers.MaxPooling2D((2, 2)), #池化层1,2*2采样
layers.Conv2D(64, (3, 3), activation='relu'), #卷积层2,卷积核3*3
layers.MaxPooling2D((2, 2)), #池化层2,2*2采样
layers.Flatten(), #Flatten层,连接卷积层与全连接层
layers.Dense(64, activation='relu'), #全连接层,特征进一步提取
layers.Dense(10) #输出层,输出预期结果
])
# 打印网络结构
model.summary()
四、确定学习的目标
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])compileThe function sets the goal of learning,其中:
loss:定义了损失函数,
optimizer:The optimization algorithm is specified,
metrics:是评价指标
五:模型训练
This sets the input training dataset(图片及标签)、验证数据集(图片及标签)以及迭代次数epochs
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))六、模型评估
调用evaluateThe function is evaluated on the test set,返回数组score,where the first dimension is the loss value of the model,The second dimension is the accuracy of the model evaluation.

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)
![]()
七、模型的预测
Output the prediction results of the first test set
plt.imshow(test_images[1])
pre = model.predict(test_images) # 对所有测试图片进行预测
pre[1] # 输出第一张图片的预测结果

You can see the subscript in9The position value is the largest,So the prediction isAnkle boot
得到预测结果:
np.argmax([pre[0]])
#9
test_labels[0]
#9View tag names:
import numpy as np
pre = model.predict(test_images)
print(class_names[np.argmax(pre[0])])![]()
Everyone paid attention
>- 本文为[365天深度学习训练营](https://mp.weixin.qq.com/s/k-vYaC8l7uxX51WoypLkTw) The Learning Records Blog
>- 参考文章地址:深度学习100例-卷积神经网络(CNN)服装图像分类 | 第3天
边栏推荐
- Collection set interface
- The submenu of the el-cascader cascade selector is double-clicked to display the selected content
- 国内数字藏品投资价值分析
- A timeout error is reported when connecting to Nacos
- The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
- 树结构——二叉查找树原理与实现
- 智能合约和去中心化应用DAPP
- error in ./node_modules/cesium/Source/ThirdParty/zip.js
- 链读推荐:从瓷砖到生成式 NFT
- Operation table Function usage
猜你喜欢
随机推荐
知识蒸馏论文学习
程序员副业赚钱之道,实现月收入增加20K
I use this recruit let the team to improve the development efficiency of 100%!
微信小程序-小程序的宿主环境
Count down the six weapons of the domestic interface collaboration platform!
LeetCode 1720.解码异或后的数组(简单)
菜谱小程序源码免费分享【推荐】
IDEA的database使用教程(使用mysql数据库)
Analysis of the investment value of domestic digital collections
Content related to ZigBee network devices
Chain Reading Recommendation: From Tiles to Generative NFTs
sqlplus displays the previous command and the available backspace key
作业实验四
事务、存储引擎
cesium rotate image
The latest and most complete digital collection sales calendar-07.27
优先队列
LeetCode 剑指offer 21.调整数组顺序使奇数位于偶数前面(简单)
String common methods
链读精选:星巴克着眼于数字收藏品并更好地吸引客户









