当前位置:网站首页>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
#0
Then 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.0
3、数据可视化
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]
#9
View 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天
边栏推荐
猜你喜欢
随机推荐
Timer (setInterval) on and off
连接 Nacos 报超时错误
LeetCode 100.相同的树(简单)
cesium add point, move point
pytorch-07.处理多维特征的输入
反射【笔记】
Smart contracts and DAPP decentralized applications
栈和队列
我不喜欢我的代码
卷积神经网络(CNN)实现mnist手写数字识别
智能合约和去中心化应用DAPP
事务、存储引擎
Analysis of the investment value of domestic digital collections
The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
redis常见的面试题
Chain Reading|The latest and most complete digital collection sales calendar-08.02
pytorch-11.卷积神经网络(高级篇)
LeetCode 1351.统计有序矩阵中的负数(简单)
测一测异性的你长什么样?
第二次实验