在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))
這是一個簡單的圖像分類示例,實際應用中可以根據需求調整網絡結構、數據增強方式、優化器等參數進行優化。