帮我写一个基于vgg16猫狗识别的pytorch代码
时间: 2024-02-18 15:36:45 浏览: 155
好的,下面是一个基于vgg16模型的猫狗识别的PyTorch代码:
```python
import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.autograd import Variable
# 定义超参数
num_epochs = 5
batch_size = 10
learning_rate = 0.001
# 定义数据预处理
transform = transforms.Compose(
[transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
# 加载数据集
train_dataset = torchvision.datasets.ImageFolder(root='./data/train', transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_dataset = torchvision.datasets.ImageFolder(root='./data/test', transform=transform)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
# 加载预训练的vgg16模型
vgg16 = torchvision.models.vgg16(pretrained=True)
# 冻结vgg16模型的所有参数
for param in vgg16.parameters():
param.requires_grad = False
# 替换vgg16模型的最后一个全连接层
num_features = vgg16.classifier[6].in_features
vgg16.classifier[6] = nn.Linear(num_features, 2)
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(vgg16.classifier[6].parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
# 前向传播和反向传播
optimizer.zero_grad()
outputs = vgg16(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 输出训练结果
if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, loss.item()))
# 测试模型
vgg16.eval()
correct = 0
total = 0
for images, labels in test_loader:
images = Variable(images)
outputs = vgg16(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))
```
在这个代码中,我们首先定义了超参数,然后使用`transforms`模块对数据进行预处理,包括将图像缩放到224x224像素、在中心进行裁剪、将图像转换为张量和进行归一化。接着,我们通过`ImageFolder`类加载数据集,并使用`DataLoader`类将数据集转换为批处理数据。然后,我们加载预训练的`vgg16`模型,并替换其最后一个全连接层以适应猫狗分类任务。我们还定义了损失函数和优化器。在训练期间,我们首先将数据和标签变量化,然后执行前向传播和反向传播,并更新模型参数。最后,我们测试模型并计算其在测试集上的准确性。
阅读全文