用pytorch写一个关于猫狗图像识别代码
时间: 2024-05-12 19:20:28 浏览: 109
以下是一个简单的猫狗图像识别代码,使用PyTorch框架。
首先,我们需要导入必要的库和模块:
```python
import torch
import torchvision
from torchvision import transforms, datasets
import matplotlib.pyplot as plt
import numpy as np
```
接下来,我们定义数据集的路径和一些超参数:
```python
train_path = 'train/'
test_path = 'test/'
batch_size = 32
num_epochs = 10
```
然后,我们使用PyTorch的`transforms`模块定义数据集的预处理方式:
```python
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
```
在这里,我们将图像大小调整为`224x224`,随机进行水平翻转,将图像转换为张量,并对每个通道进行归一化。
接下来,我们使用`datasets.ImageFolder`类加载数据集:
```python
train_data = datasets.ImageFolder(train_path, transform=transform)
test_data = datasets.ImageFolder(test_path, transform=transform)
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False)
```
我们使用`DataLoader`类将数据集加载到内存中,并指定批处理大小和是否随机洗牌数据集。
接下来,我们定义一个卷积神经网络模型:
```python
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = torch.nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
self.conv2 = torch.nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.conv3 = torch.nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = torch.nn.Linear(256*28*28, 512)
self.fc2 = torch.nn.Linear(512, 2)
def forward(self, x):
x = self.conv1(x)
x = torch.nn.functional.relu(x)
x = self.pool(x)
x = self.conv2(x)
x = torch.nn.functional.relu(x)
x = self.pool(x)
x = self.conv3(x)
x = torch.nn.functional.relu(x)
x = self.pool(x)
x = x.view(-1, 256*28*28)
x = self.fc1(x)
x = torch.nn.functional.relu(x)
x = self.fc2(x)
x = torch.nn.functional.softmax(x, dim=1)
return x
model = Net()
```
在这里,我们定义了一个包含3个卷积层和2个全连接层的模型。我们使用了ReLU激活函数和softmax激活函数。
最后,我们定义损失函数和优化器:
```python
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
```
在每个epoch中,我们将训练模型并计算损失。然后,我们在测试集上评估模型的准确性:
```python
for epoch in range(num_epochs):
train_loss = 0.0
train_acc = 0.0
for i, (images, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item() * images.size(0)
_, preds = torch.max(outputs, 1)
train_acc += torch.sum(preds == labels.data)
train_loss = train_loss / len(train_loader.dataset)
train_acc = train_acc / len(train_loader.dataset)
print('Epoch: {} \tTraining Loss: {:.6f} \tTraining Accuracy: {:.6f}'.format(
epoch+1, train_loss, train_acc))
test_acc = 0.0
for i, (images, labels) in enumerate(test_loader):
outputs = model(images)
_, preds = torch.max(outputs, 1)
test_acc += torch.sum(preds == labels.data)
test_acc = test_acc / len(test_loader.dataset)
print('Testing Accuracy: {:.6f}'.format(test_acc))
```
最后,我们可以使用以下代码加载并显示一张测试图像:
```python
test_image = datasets.ImageFolder(test_path, transform=transform)
img, label = test_image[0]
plt.imshow(np.transpose(img.numpy(), (1, 2, 0)))
plt.show()
```
我们可以使用以下代码来使用我们的模型对该图像进行预测:
```python
output = model(torch.unsqueeze(img, 0))
_, pred = torch.max(output, 1)
if pred == 0:
print('Cat')
else:
print('Dog')
```
希望这个示例代码可以帮助你开始使用PyTorch进行图像分类任务!
阅读全文