当前位置: 网站首页 技术文章 正文

人工智能算法快速入门:CNN教程

来源:互联网 发布时间:2024-08-21 10:34:02
今天给大家介绍一个强大的算法模型,CNN

卷积神经网络 (CNN) 是一类深度学习算法,主要用于处理和分析视觉数据。

它们彻底改变了计算机视觉领域,使图像识别、物体检测和各种其他应用取得了突破。

什么是卷积神经网络?

卷积神经网络 (CNN) 是一种人工神经网络,专门用于处理结构化网格数据(例如图像)。

与将输入视为平面像素阵列的传统神经网络不同,CNN 利用图像的空间结构来提取分层特征。这种能力使 CNN 在图像分类、对象检测和分割等任务中特别有效。

图片图片

关键组件

1.卷积层

卷积层是 CNN 的核心组件,用于提取输入数据的特征。

卷积层通过滤波器(卷积核)在输入数据上滑动,进行卷积操作,生成特征图(Feature Map)。每个卷积核负责捕捉图像的不同特征,如边缘、纹理等。

图片图片

 

  • 卷积核
    检测特定特征(如边缘、纹理或图案)的小矩阵。常见尺寸为 3x3 或 5x5。
  • 步幅
    卷积核在输入图像上移动的步长。步幅为 1 表示卷积核每次移动一个像素。
  • 填充
    在输入图像的边框周围添加额外的像素,以确保卷积核正确适配。

图片图片

以下是使用 NumPy 的简单实现。

import numpy as np

def convolve(image, kernel, stride=1, padding=0):
    # Add padding to the input image
    image_padded = np.pad(image, [(padding, padding), (padding, padding)], mode='constant', constant_values=0)
    
    # Calculate output dimensions
    output_height = (image.shape[0] - kernel.shape[0] + 2 * padding) // stride + 1
    output_width = (image.shape[1] - kernel.shape[1] + 2 * padding) // stride + 1
    
    # Initialize output
    output = np.zeros((output_height, output_width))
    
    # Perform convolution
    for i in range(0, output_height, stride):
        for j in range(0, output_width, stride):
            output[i, j] = np.sum(image_padded[i:i+kernel.shape[0], j:j+kernel.shape[1]] * kernel)
    
    return output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
 

2.池化层

池化层通常用于降低特征图的空间尺寸(高度和宽度),减少参数数量和计算复杂性,同时使特征检测更加鲁棒。

池化操作主要有两种类型:

  • 最大池化:从特征图的每个块中获取最大值。
  • 平均池化:从特征图的每个块中取平均值。

图片图片

def max_pooling(image, size=2, stride=2):
    output_height = (image.shape[0] - size) // stride + 1
    output_width = (image.shape[1] - size) // stride + 1
    output = np.zeros((output_height, output_width))
    
    for i in range(0, output_height, stride):
        for j in range(0, output_width, stride):
            output[i, j] = np.max(image[i:i+size, j:j+size])
    
    return output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
 

3.全连接层

经过几个卷积层和池化层之后,神经网络中的高级推理通过全连接层完成。

这些层将扁平化的特征图作为输入,并将其用于最终分类。

图片图片

CNN 的执行过程

让我们通过一个例子来了解 CNN 如何处理图像:

  1. 输入图像
    考虑 32x32x3 RGB 图像(高度、宽度、深度)。
  2. 卷积层
    对图像应用多个过滤器(例如 3x3),从而产生多个特征图。假设我们使用 10 个过滤器;输出将为 32x32x10。
  3. 池化层
    使用 2x2 最大池化,步长为 2,以减少空间维度。输出将为 16x16x10。
  4. 额外的卷积和池化层
    根据需要重复卷积、激活和池化操作。
  5. 全连接层
    将最后一个池化层的输出展平(例如,8x8x10 变为 640 维向量)并将其连接到密集层以进行分类。
  6. 输出层
    应用softmax函数获取类别的概率分布。

下面我们将使用 TensorFlow 构建一个简单的 CNN,用于 MNIST 数据集(手写数字数据集)的图像分类。

首先我们导入必要的库

import tensorflow as tf
from tensorflow.keras import layers, models
import matplotlib.pyplot as plt
  • 1.
  • 2.
  • 3.
 

然后加载和预处理数据

# Load the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

# Normalize the images
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
 

构建 CNN 模型

model = models.Sequential([
    layers.Conv2D(32, (3, 3), activatinotallow='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activatinotallow='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activatinotallow='relu'),
    layers.Flatten(),
    layers.Dense(64, activatinotallow='relu'),
    layers.Dense(10, activatinotallow='softmax')
])
model.summary()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
 

编译并训练模型

model.compile(optimizer='adam', 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=5, 
                    validation_data=(test_images, test_labels))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
 

评估模型

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f"Test accuracy: {test_acc}")

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label='val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  •  
 

相关教程