rnn循环神经网络模型实现猫狗分类的代码
时间: 2023-09-16 22:14:33 浏览: 176
基于Python实现猫狗图像分类(CNN-DNN-RNN)【100011892】
5星 · 资源好评率100%
以下是使用RNN模型实现猫狗分类的示例代码,其中使用了LSTM作为RNN的基本单元:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder
from torchvision.transforms import transforms
# 定义数据预处理
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
])
# 加载数据集
train_dataset = ImageFolder(root="train", transform=transform)
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
# 定义模型
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.num_layers = num_layers
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
out, _ = self.lstm(x, (h0, c0))
out = self.fc(out[:, -1, :])
return out
# 训练模型
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = RNN(input_size=3*64*64, hidden_size=128, num_layers=2, num_classes=2)
model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
for epoch in range(10):
for images, labels in train_loader:
images = images.reshape(-1, 3*64*64).to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
print(f"Epoch [{epoch+1}/10], Loss: {loss.item():.4f}")
# 测试模型
model.eval()
test_dataset = ImageFolder(root="test", transform=transform)
test_loader = DataLoader(test_dataset, batch_size=16, shuffle=False)
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, 3*64*64).to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print(f"Accuracy: {100*correct/total:.2f}%")
```
在这个例子中,我们使用了一个包含两层LSTM的循环神经网络,并在最后加入了一个全连接层进行分类。代码中使用了PyTorch框架,并对数据进行了预处理和归一化。在训练过程中,我们使用交叉熵作为损失函数,Adam优化器进行参数更新。最后在测试集上计算模型的准确率。
阅读全文