基于pytorch的Vgg16进行遥感影像分类并引入注意力机制,并绘制训练结果和测试结果图
时间: 2024-06-09 19:09:45 浏览: 171
很高兴能为您提供帮助。首先,我们需要准备遥感影像分类数据集,然后利用pytorch框架搭建Vgg16深度学习模型,最后引入注意力机制并进行训练和测试。
以下是详细步骤:
1. 下载遥感影像分类数据集,例如UCMerced_LandUse数据集,该数据集包含21个类别的遥感影像,每个类别有100张图像,每张图像的大小为256x256像素。您可以从以下链接下载该数据集:http://weegee.vision.ucmerced.edu/datasets/landuse.html
2. 安装pytorch和torchvision库,这是搭建Vgg16模型的必要库。您可以使用以下命令进行安装:
```
pip install torch torchvision
```
3. 加载和预处理数据集。我们可以使用torchvision库中的transforms进行图像预处理,例如随机裁剪、标准化等。还需要使用torch.utils.data.Dataset和torch.utils.data.DataLoader来加载数据集。
```python
import torch
import torchvision
from torchvision import transforms, datasets
# 设置数据集路径
data_path = 'UCMerced_LandUse/Images'
# 定义数据预处理
data_transform = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 加载数据集
train_dataset = datasets.ImageFolder(root=data_path+'/train', transform=data_transform)
test_dataset = datasets.ImageFolder(root=data_path+'/test', transform=data_transform)
# 定义数据加载器
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)
```
4. 搭建Vgg16模型。我们可以使用torchvision库中的Vgg16模型进行搭建。由于我们需要引入注意力机制,因此需要对模型进行修改。这里我们可以使用PyTorch自定义模型进行修改。
```python
import torch.nn as nn
import torch.nn.functional as F
# 自定义模型
class VGG16Attention(nn.Module):
def __init__(self):
super(VGG16Attention, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(256, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, kernel_size=3, padding=1),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.attention = nn.Sequential(
nn.Conv2d(512, 1, kernel_size=1),
nn.Sigmoid()
)
self.classifier = nn.Sequential(
nn.Linear(512, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 21)
)
def forward(self, x):
x = self.features(x)
att = self.attention(x)
x = F.adaptive_avg_pool2d(att * x, 1).reshape(x.size(0), -1)
x = self.classifier(x)
return x
```
5. 训练和测试模型。我们可以使用Adam优化器和交叉熵损失函数进行训练,并在测试集上进行评估。
```python
import torch.optim as optim
# 定义模型、优化器、损失函数
model = VGG16Attention().cuda()
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
# 训练模型
for epoch in range(50):
running_loss = 0.0
for i, (inputs, labels) in enumerate(train_loader):
inputs, labels = inputs.cuda(), labels.cuda()
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('Epoch %d loss: %.3f' % (epoch+1, running_loss/len(train_loader)))
# 测试模型
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in test_loader:
inputs, labels = inputs.cuda(), labels.cuda()
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test accuracy: %.2f%%' % (100 * correct / total))
```
6. 绘制训练结果和测试结果图。我们可以使用matplotlib库绘制训练和测试结果图。
```python
import matplotlib.pyplot as plt
# 绘制训练和测试结果图
plt.plot(range(1, 51), train_loss, label='Training loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Training Loss')
plt.legend()
plt.show()
plt.plot(range(1, 51), test_acc, label='Test accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Test Accuracy')
plt.legend()
plt.show()
```
这样,我们就完成了基于pytorch的Vgg16进行遥感影像分类并引入注意力机制,并绘制训练结果和测试结果图。
阅读全文