图像数据集存放在文件夹中,请帮我搭建pytorch卷积神经网络对文件夹中的图像数据进行识别
时间: 2024-05-10 09:14:37 浏览: 120
好的,以下是一个基本的pytorch卷积神经网络对图像数据集进行识别的代码:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import torchvision.datasets as datasets
import torchvision.transforms as transforms
# 定义数据集路径、批次大小、学习率、训练轮数等超参数
data_path = 'image_dataset'
batch_size = 32
learning_rate = 0.001
num_epochs = 10
# 加载数据集并进行数据增强
train_dataset = datasets.ImageFolder(
root=data_path,
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))
])
)
# 使用DataLoader进行批量加载数据
train_loader = DataLoader(
dataset=train_dataset,
batch_size=batch_size,
shuffle=True
)
# 定义卷积神经网络模型
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, padding=1),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.layer3 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2)
)
self.fc1 = nn.Linear(64 * 28 * 28, 256)
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.reshape(out.size(0), -1)
out = self.fc1(out)
out = self.dropout(out)
out = self.fc2(out)
return out
# 实例化模型并定义损失函数和优化器
model = CNN()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
outputs = model(images)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# 保存模型
torch.save(model.state_dict(), 'cnn_model.pth')
```
在上述代码中,我们使用了`datasets.ImageFolder`类将图像数据集加载到内存中,并使用`transforms`进行数据增强。然后使用`DataLoader`将数据集分批次进行加载。接着,我们定义了一个卷积神经网络模型,并使用`nn.CrossEntropyLoss`作为损失函数,使用`optim.Adam`作为优化器。最后,我们使用`torch.save`保存了训练好的模型。
阅读全文