在PaddlePaddle中实现图像分类任务通常使用卷积神经网络(CNN)。以下是一个简单的图像分类示例:
- 导入必要的库和模块:
import paddle
import paddle.nn.functional as F
from paddle.vision import transforms
- 定义一个简单的卷积神经网络模型:
class Net(paddle.nn.Layer):
def __init__(self, num_classes=10):
super(Net, self).__init__()
self.conv1 = paddle.nn.Conv2D(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
self.pool1 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)
self.conv2 = paddle.nn.Conv2D(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.pool2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)
self.fc1 = paddle.nn.Linear(in_features=64*8*8, out_features=128)
self.fc2 = paddle.nn.Linear(in_features=128, out_features=num_classes)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = paddle.flatten(x, start_axis=1)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
- 准备数据和数据增强:
transform = transforms.Compose([
transforms.Resize(size=32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor()
])
train_dataset = paddle.vision.datasets.CIFAR10(mode='train', transform=transform)
train_loader = paddle.io.DataLoader(train_dataset, batch_size=32, shuffle=True)
test_dataset = paddle.vision.datasets.CIFAR10(mode='test', transform=transform)
test_loader = paddle.io.DataLoader(test_dataset, batch_size=32, shuffle=False)
- 训练模型:
model = Net()
optimizer = paddle.optimizer.Adam(parameters=model.parameters())
criterion = paddle.nn.CrossEntropyLoss()
model.train()
for epoch in range(10):
for data in train_loader:
images, labels = data
outputs = model(images)
loss = criterion(outputs, labels)
optimizer.clear_grad()
loss.backward()
optimizer.step()
- 在测试集上评估模型:
model.eval()
accs = []
for data in test_loader:
images, labels = data
outputs = model(images)
acc = paddle.metric.accuracy(outputs, labels)
accs.append(acc.numpy())
print("Test Accuracy: ", sum(accs) / len(accs))
这是一个简单的图像分类示例,实际应用中可以根据需求调整网络结构、数据增强方式、优化器等参数进行优化。