简单的图像分类任务全流程示例(内含代码)

以下是一个简单的示例,展示了如何使用 PyTorch 处理自定义图像分类数据集:

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder

# 数据预处理
transform = transforms.Compose([
    transforms.Resize((64, 64)),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# 创建 ImageFolder 数据集实例
train_dataset = ImageFolder(root='path/to/dataset', transform=transform)

# 创建数据加载器
batch_size = 64
data_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

# 定义简单的卷积神经网络模型
class SimpleCNN(nn.Module):
    def __init__(self):
        super(SimpleCNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
        self.relu = nn.ReLU()
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.flatten = nn.Flatten()
        self.fc1 = nn.Linear(32*32*32, 128)
        self.fc2 = nn.Linear(128, len(train_dataset.classes))  # 类别数根据数据集自动调整

    def forward(self, x):
        x = self.conv1(x)
        x = self.relu(x)
        x = self.pool(x)
        x = self.flatten(x)
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x

# 初始化模型、损失函数和优化器
model = SimpleCNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# 训练模型
num_epochs = 5
for epoch in range(num_epochs):
    for images, labels in data_loader:
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

# 保存模型
torch.save(model.state_dict(), 'custom_classifier_model.pth')

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

print(f'Accuracy on the test images: {100 * correct / total:.2f}%')

这里使用了 ImageFolder 数据集类,它会自动根据文件夹结构为每个类别分配标签。请替换 'path/to/dataset' 为你实际的数据集路径。这样,你就无需手动指定文件路径和标签,代码会自动从文件夹结构中获取这些信息。

你可能感兴趣的:(分类,数据挖掘,人工智能)