用pytorch写一个猫狗图像识别模型吗,要三分类的,可以识别出猫狗也可以识别出不是猫狗的图像
时间: 2024-05-09 16:19:59 浏览: 75
pytorch CNN网络实现图像二分类问题 猫狗识别 完整代码数据可直接运行
5星 · 资源好评率100%
好的,以下是一个使用PyTorch实现的猫狗图像识别模型,包含三个类别:猫、狗、不是猫狗。
```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
# 定义数据预处理
data_transforms = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
# 加载数据集
train_dataset = ImageFolder('train', transform=data_transforms)
val_dataset = ImageFolder('val', transform=data_transforms)
test_dataset = ImageFolder('test', transform=data_transforms)
# 定义模型
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.relu1 = nn.ReLU(inplace=True)
self.pool1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.relu2 = nn.ReLU(inplace=True)
self.pool2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(64, 128, 3, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.relu3 = nn.ReLU(inplace=True)
self.pool3 = nn.MaxPool2d(2, 2)
self.fc1 = nn.Linear(128 * 28 * 28, 512)
self.relu4 = nn.ReLU(inplace=True)
self.dropout1 = nn.Dropout(0.5)
self.fc2 = nn.Linear(512, 3)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.pool3(x)
x = x.view(-1, 128 * 28 * 28)
x = self.fc1(x)
x = self.relu4(x)
x = self.dropout1(x)
x = self.fc2(x)
return x
# 定义训练函数
def train_model(model, criterion, optimizer, dataloaders, num_epochs=25):
train_loss_history = []
val_loss_history = []
train_acc_history = []
val_acc_history = []
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch + 1, num_epochs))
print('-' * 10)
for phase in ['train', 'val']:
if phase == 'train':
model.train()
else:
model.eval()
running_loss = 0.0
running_corrects = 0
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / len(dataloaders[phase].dataset)
epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)
if phase == 'train':
train_loss_history.append(epoch_loss)
train_acc_history.append(epoch_acc)
else:
val_loss_history.append(epoch_loss)
val_acc_history.append(epoch_acc)
print('{} Loss: {:.4f} Acc: {:.4f}'.format(phase, epoch_loss, epoch_acc))
return model, train_loss_history, val_loss_history, train_acc_history, val_acc_history
# 设置训练参数
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
batch_size = 32
num_epochs = 10
learning_rate = 0.001
# 加载数据
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
test_dataloader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
dataloaders = {'train': train_dataloader, 'val': val_dataloader}
# 初始化模型、损失函数和优化器
model = Net().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
model, train_loss_history, val_loss_history, train_acc_history, val_acc_history = train_model(model, criterion, optimizer, dataloaders, num_epochs=num_epochs)
# 测试模型
model.eval()
test_loss = 0.0
test_corrects = 0
for inputs, labels in test_dataloader:
inputs = inputs.to(device)
labels = labels.to(device)
with torch.set_grad_enabled(False):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
test_loss += loss.item() * inputs.size(0)
test_corrects += torch.sum(preds == labels.data)
test_loss = test_loss / len(test_dataloader.dataset)
test_acc = test_corrects.double() / len(test_dataloader.dataset)
print('Test Loss: {:.4f} Acc: {:.4f}'.format(test_loss, test_acc))
```
在上面的代码中,我们首先定义了数据预处理,然后使用`ImageFolder`加载训练、验证和测试数据集。接下来定义了模型,这里使用了一个简单的卷积神经网络,包含三个卷积层和两个全连接层。然后我们定义了一个训练函数`train_model`,该函数用于训练模型并返回训练过程中的损失和精度。在训练函数中,我们使用了交叉熵损失和Adam优化器。最后,我们训练模型,并在测试集上评估模型的性能。
注意:上述代码中的文件路径需要根据实际情况进行修改。
阅读全文