欢迎关注 “小白玩转Python”,发现更多 “有趣”
引言
图像分类是从图像中提取特征,然后按照特定类别对特征进行分类的技术。对于人类来说,识别图像是一个常识性的问题,但对于机器来说,绝对不是。首先,我们必须用图像来训练机器,然后,当我们用一个新的图像来引入模型,然后基于训练它会给我们带来显著的结果。
在本文中,我们将了解卷积神经网络是如何工作的,以及如何帮助我们改善我们的模型的性能。我们还将介绍如何在 PyTorch 中实现CNN。
目录
PyTorch 概述
什么是 CNN?
在 PyTorch 中实现 CNN
总结
1. PyTorch 概述
PyTorch 是一个基于 Torch 库的开源机器学习库。它主要应用于计算机视觉和自然语言处理等领域。主要由 Facebook 的人工智能研究实验室开发和维护。它是在修改后的 BSD 许可下发布的自由及开放源代码软件。PyTorch 是一个 Python 包,它提供两个高级特性:
1. 可以使用 GPU 加速计算的张量(如 NumPy)
2. 可以自动记录求导过程深层神经网络
2. 什么是 CNN?
卷积神经网络是神经网络的一种类型,最常用于图像处理问题,例如图像中的目标识别,但也可以用于自然语言处理。一旦你通过完整的文章,你将了解为什么 CNN 是最有效的这些快速增长的领域。
CNN 的工作方式不同,因为他们对待空间数据的处理方式不同。在神经网络中,当我们向输入层提供输入时,这一层中的神经元数量等于图像中的像素数量。在 CNN 中,神经元不是连接到上一层的每个神经元,而是只连接到靠近它的神经元。
CNN 中有一些重要的层:
1. 卷积层
2. 池化层
3. 完全连接层
这里我们将理解这两个主要层背后的概念,
(i)卷积层
带(3x3)核的(5x5)矩阵的简单卷积
卷积是指在这种类型的神经网络中发生的过滤过程。在数学上,卷积被描述为两个给定函数通过积分得到的函数,它表示一个函数的形状是如何被另一个函数“映射”的。
卷积层的输出形状受以下因素影响:
Kernel Size:滤波器的大小
Input Dimensions:输入图像大小
Padding:填充,我们可以通过填充的方式给图像周围补0,以保证图像尺寸正常
Strides:滤波器移动时的步长
考虑上面的图像,图像的大小是(5x5) ,滤镜的大小是(3x3)。通过卷积层对输入图像进行处理后,得到尺寸为(3x3)的输出图像。
其中 n_in 表示输入图像的尺寸,f 表示花童窗口大小,s 表示步幅。
The function for convolution in pyTorch is torch.nn.Connv2d().
(ii)池化层
CNN 中的池化层可以降低卷积神经网络中的参数数量。
从上面的图片中,您可以得出结论,有三种类型的池化方法:
Max Pooling:最大池化
Average Pooling:平均池化
Sum Pooling:求和池化
最大池化可以使网络能够集中于少数神经元而不是全部神经元,这对网络具有正则化作用,并且可能过度拟合训练数据。
The function for max-pooling in pyTorch is torch.nn.MaxPool2d().
(iii)全连接层
全连通层是卷积神经网络的重要组成部分,在计算机视觉图像识别和分类中取得了很好的效果。CNN 过程从卷积和池化开始,将图像分解为特征,并独立进行分析。
3. 在 PyTorch 中实现 CNN
我们正在研究 Fashion MNIST 数据集,我们的任务是通过查看数据集中的各种图像来识别服装的类型。
#download the dataset from keras.datasets
from keras.datasets import fashion_mnist
((trainX, trainY), (testX, testY)) = fashion_mnist.load_data()
print(trainX.shape)
print(trainY.shape)
print(testX.shape)
print(testY.shape)
在训练集中有6万张图片,在测试集中有1万张图片。
#Importing the libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#for creating validation set
from sklearn.model_selection import train_test_split
#for evaluating the model
from sklearn.metrics import accuracy_score
#PyTorch libraries and modules
import torch
from torch.autograd import Variable
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential, Conv2d, MaxPool2d, Module, Softmax, BatchNorm2d, Dropout
from torch.optim import Adam, SGD
我们只是导入了数据可视化和预测模型所需的库。让我们可视化训练数据集中的一些图像,
plt.figure(figsize=(10,10))
plt.subplot(221), plt.imshow(trainX[10])
plt.subplot(222), plt.imshow(trainX[20])
对于预处理,我们必须将图像转换为 torch 格式。
train_x, val_x, train_y, val_y = train_test_split(trainX, trainY, test_size = 0.1)
#converting training images into torch format
train_x = train_x.reshape(54000, 1, 28, 28)
train_x = torch.from_numpy(train_x)
#converting the target into torch format
train_y = train_y.astype(int)
train_y = torch.from_numpy(train_y)
#converting validation images into torch format
val_x = val_x.reshape(6000, 1, 28, 28)
val_x = torch.from_numpy(val_x)
#converting the target into torch format
val_y = val_y.astype(int)
val_y = torch.from_numpy(val_y)
我们正在创建一个简单的 CNN 架构,只有2个卷积层,kernel size = 2,stride = 1 & padding = 1,从图像中找到滤波器。
## Architecture
class Net(Module):
def __init__(self):
super(Net, self).__init__()
self.cnn_layers = Sequential(
#Defining a 2D convolution layer
Conv2d(1, 4, kernel_size=2, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2, stride=2),
#Defining another 2D convolution layer
Conv2d(4, 4, kernel_size=2, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2, stride=2)
)
self.linear_layers = Sequential(
Linear(4 * 7 * 7, 10)
)
#Defining the forward pass
def forward(self, x):
x = self.cnn_layers(x)
x = x.view(x.size(0), -1)
x = self.linear_layers(x)
return x
我们去做进一步的处理,
#define the model
model = Net()
#define the optimizer
optimizer = Adam(model.parameters(), lr=0.07)
#defining the loss function
criterion = CrossEntropyLoss()
#checking if GPU is available
if torch.cuda.is_available():
model = model.cuda()
criterion = criterion.cuda()
print(model)
通过开始时 epoch 的值,你可以看到损失是高的,但随着我们的训练过程下来,损失值也在逐步降低。让我们想象一下训练的损失和验证的损失。
#plotting the training and validation loss
plt.plot(train_losses, label='Training loss')
plt.plot(val_losses, label='Validation loss')
plt.legend()
plt.show()
现在我们检查训练集的准确性,
# prediction for training set
with torch.no_grad():
train_x = train_x.float()
output = model(train_x)
softmax = torch.exp(output).cpu()
prob = list(softmax.numpy())
predictions = np.argmax(prob, axis=1)
# accuracy on training set
accuracy_score(train_y, predictions)
现在让我们检查一下验证集的准确性,
# prediction for validation set
with torch.no_grad():
val_x = val_x.float()
output = model(val_x)
softmax = torch.exp(output).cpu()
prob = list(softmax.numpy())
predictions = np.argmax(prob, axis=1)
# accuracy on validation set
accuracy_score(val_y, predictions)
通过结果可以发现:我们使用这种卷积神经网络训练出来的模型在训练集和验证集上的准确率相当。
总结
从这篇文章中,我希望你能理解卷积层和池化层的概念,以及 CNN 对图像分类和目标检测任务的用处,以及如何使用 PyTorch 实现 CNN 架构。
· END ·
HAPPY LIFE