Artificial intelligence (Pytorch) building model 6-Using Pytorch to build a convolutional neural network ResNet model

Hello everyone, I am Wei Xue AI. Today I will introduce to you the artificial intelligence (Pytorch) model building 6-Using Pytorch to build a convolutional neural network ResNet model. In this article, we will learn how to use PyTorch to build a convolutional neural network ResNet model. , and train and test on the generated fake data. This article will cover these contents: Introduction to ResNet model, ResNet model structure, generating fake data, implementing ResNet model, training and testing models.

1. Introduction to ResNet model

The ResNet (residual network) model is a deep convolutional neural network proposed by He Yuming et al. in 2015. Its main innovation is the introduction of a residual structure, through which ResNet can effectively solve the problem that deep neural networks are difficult to train. ResNet has achieved very good results on several image classification tasks, including the ImageNet Large-Scale Visual Recognition Challenge. The ResNet model makes the convolutional neural network not limited by the number of layers, and solves the problem that the deeper the number of layers, the worse the prediction result of the model.

2. ResNet model structure

The core idea of ​​ResNet is to introduce a residual block. The input of the residual block is not only directly passed to the next layer, but also directly connected to the subsequent layer through a skip connection (Skip Connection). This structure can effectively alleviate the problem of gradient disappearance, so that the network can be effectively trained.

A typical residual block contains two convolutional layers, two activation functions and one skip connection. The number of layers of the ResNet network can be achieved by stacking different numbers of residual blocks, such as the common ResNet-18, ResNet-34, ResNet-50, ResNet-101, and ResNet-152.

59448bf4644146d3961ad57b50d4a5e7.png

3. Generate fake data

To demonstrate model training and testing, we will use generated fake data throughout this article. We will generate a dataset of 1000 3-channel 32x32 images, use random numbers to represent image pixel values, and assign each image a class label between 0 and 9.

Fourth, implement the ResNet model

Next, we will implement a simplified version of the ResNet-18 model using the PyTorch framework. First, we need to import the required libraries:

import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader, TensorDataset
import numpy as np

class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1):
        super(ResidualBlock, self).__init__()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(out_channels)

        self.skip_connection = nn.Sequential()
        if stride != 1 or in_channels != out_channels:
            self.skip_connection = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(out_channels)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        out += self.skip_connection(x)
        out = F.relu(out)
        return out

class ResNet(nn.Module):
    def __init__(self, block, num_classes=10):
        super(ResNet, self).__init__()
        self.in_channels = 64
        self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, stride=1)
        self.layer2 = self._make_layer(block, 128, stride=2)
        self.layer3 = self._make_layer(block, 256, stride=2)
        self.layer4 = self._make_layer(block, 512, stride=2)
        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512, num_classes)

    def _make_layer(self, block, out_channels, stride):
        layers = [block(self.in_channels, out_channels, stride)]
        self.in_channels = out_channels
        layers.append(block(out_channels, out_channels))
        return nn.Sequential(*layers)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avg_pool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x

Residual connection graph: 

8c9a04aad4174dd1bafd61d10dfd9cb8.png

5. Training and testing model

Now we create the dataset, model, loss function and optimizer, and train:

# 生成假数据
num_samples = 1000
image_data = np.random.rand(num_samples, 3, 32, 32).astype(np.float32)
labels = np.random.randint(0, 10, size=num_samples, dtype=np.int64)

# 创建数据集和数据加载器
train_data = TensorDataset(torch.from_numpy(image_data), torch.from_numpy(labels))
train_loader = DataLoader(train_data, batch_size=64, shuffle=True)

# 创建模型、损失函数和优化器
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = ResNet(ResidualBlock).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

# 训练模型
num_epochs = 10
for epoch in range(num_epochs):
    model.train()
    running_loss = 0.0
    for i, (images, labels) in enumerate(train_loader):
        images = images.to(device)
        labels = labels.to(device)

        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()

    print(f"Epoch [{epoch + 1}/{num_epochs}], Loss: {running_loss / (i + 1)}")

# 测试模型
model.eval()
with torch.no_grad():
    correct = 0
    total = 0
    for images, labels in train_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

    print(f"Accuracy of the model on the {total} test images: {100 * correct / total}%")

6. Summary

The article details how to use PyTorch to build a convolutional neural network ResNet model, and train and test on the generated fake data. By implementing a simplified version of the ResNet-18 model, we understand the structure and principle of the residual block, and how to use the residual structure to effectively train a deep neural network. In practical applications, the performance of the model can be further improved by adjusting the model structure and parameters, and using real data sets.

 

Guess you like

Origin blog.csdn.net/weixin_42878111/article/details/130873253