使用深度卷积神经网络,(自己构造或者采用预训练模型 VGG, GoogLeNet, ResNet, DenseNet 等),对图像数据集 CIFAR-10(https://www.cs.toronto.edu/~kriz/cifar.html) 进行分类,该数据集的训练集包含 50000 张图片,测试集包含 10000 张图片。 要求:需要把训练集划分出 10000 张图片作为验证集,用来进行模型选择,可以使用 Kaggle 的免费 GPU 进行训练。需要在测试集上画出 accuracy vs. epochs
时间: 2023-06-12 21:05:01 浏览: 118
好的,这是一个典型的图像分类问题,我们可以使用深度卷积神经网络来解决。我们可以使用 PyTorch 框架来实现。
首先,我们要下载 CIFAR-10 数据集。可以使用以下代码:
```python
import torchvision.datasets as dset
import torchvision.transforms as transforms
# 定义数据预处理
transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=4),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
# 加载数据集
train_set = dset.CIFAR10(root='./data', train=True,
download=True, transform=transform)
test_set = dset.CIFAR10(root='./data', train=False,
download=True, transform=transform)
```
接下来,我们可以将训练集划分为训练集和验证集。
```python
import torch.utils.data as data
# 划分训练集和验证集
train_size = int(0.8 * len(train_set))
val_size = len(train_set) - train_size
train_set, val_set = data.random_split(train_set, [train_size, val_size])
```
然后,我们可以定义模型。这里我们使用一个简单的卷积神经网络。
```python
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.fc1 = nn.Linear(4*4*128, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2)
x = self.conv2(x)
x = self.bn2(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2)
x = self.conv3(x)
x = self.bn3(x)
x = nn.functional.relu(x)
x = nn.functional.max_pool2d(x, 2)
x = x.view(-1, 4*4*128)
x = self.fc1(x)
return x
net = Net()
```
接下来,我们可以定义损失函数和优化器。
```python
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
```
然后,我们可以使用 DataLoader 来加载数据。
```python
train_loader = data.DataLoader(train_set, batch_size=128, shuffle=True)
val_loader = data.DataLoader(val_set, batch_size=128, shuffle=True)
test_loader = data.DataLoader(test_set, batch_size=128, shuffle=False)
```
最后,我们可以开始训练并在测试集上测试模型。
```python
import torch
import time
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net.to(device)
start_time = time.time()
for epoch in range(50):
net.train()
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
net.eval()
val_loss = 0.0
correct = 0
total = 0
with torch.no_grad():
for data in val_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = net(inputs)
loss = criterion(outputs, labels)
val_loss += loss.item()
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Epoch {epoch + 1}, Train Loss: {running_loss / len(train_loader):.3f}, Val Loss: {val_loss / len(val_loader):.3f}, Val Acc: {(correct / total) * 100:.3f}%")
end_time = time.time()
print(f"Training Time: {end_time - start_time}s")
net.eval()
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Test Acc: {(correct / total) * 100:.3f}%")
```
在测试集上的准确率约为 78%,可以根据需要调整模型和超参数来提高准确率。最后,我们可以使用 Matplotlib 绘制 accuracy vs. epochs 图表。
```python
import matplotlib.pyplot as plt
train_acc = []
val_acc = []
net.eval()
with torch.no_grad():
for epoch in range(50):
train_correct = 0
train_total = 0
for data in train_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
train_total += labels.size(0)
train_correct += (predicted == labels).sum().item()
train_acc.append((train_correct / train_total) * 100)
val_correct = 0
val_total = 0
for data in val_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
val_total += labels.size(0)
val_correct += (predicted == labels).sum().item()
val_acc.append((val_correct / val_total) * 100)
plt.plot(train_acc, label="Train Acc")
plt.plot(val_acc, label="Val Acc")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
```
这样就可以得到 accuracy vs. epochs 图表了。
阅读全文