基于pytorch水果图像识别系统,数据集使用Fruits 360,并将训练好的模型应用到图片识别界面中,使用GPU加速训练模型,具体代码实现
时间: 2024-06-09 20:07:25 浏览: 183
Python基于tensorflow实现的蔬菜水果图片识别项目源码+数据集+模型
好的,下面是基于PyTorch的水果图像识别系统代码实现,以及如何使用GPU加速训练模型。首先,我们需要准备数据集:
1. 下载Fruits 360数据集:https://www.kaggle.com/moltean/fruits
2. 解压后将数据集放置在项目目录下的“data”文件夹中
3. 安装PyTorch和Torchvision
然后,我们就可以开始编写代码了。
1. 导入必要的库和模块
```python
import torch
import torchvision
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import os
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
```
2. 定义数据转换
```python
# 定义数据转换
transform = transforms.Compose([
transforms.Resize((64, 64)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
```
3. 加载数据集
```python
# 加载数据集
data_dir = 'data/fruits-360/Training'
classes = os.listdir(data_dir)
num_classes = len(classes)
X = []
y = []
for i, c in enumerate(classes):
class_dir = os.path.join(data_dir, c)
for img in os.listdir(class_dir):
img_path = os.path.join(class_dir, img)
X.append(img_path)
y.append(i)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
train_dataset = torchvision.datasets.ImageFolder(root=data_dir, transform=transform)
test_dataset = torchvision.datasets.ImageFolder(root='data/fruits-360/Test', transform=transform)
train_loader = DataLoader(dataset=train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=32, shuffle=False)
```
4. 定义模型
```python
# 定义模型
class FruitNet(torch.nn.Module):
def __init__(self, num_classes):
super(FruitNet, self).__init__()
self.conv1 = torch.nn.Conv2d(3, 32, kernel_size=3)
self.relu1 = torch.nn.ReLU()
self.conv2 = torch.nn.Conv2d(32, 64, kernel_size=3)
self.relu2 = torch.nn.ReLU()
self.conv3 = torch.nn.Conv2d(64, 128, kernel_size=3)
self.relu3 = torch.nn.ReLU()
self.pool = torch.nn.MaxPool2d(kernel_size=2)
self.fc1 = torch.nn.Linear(128 * 14 * 14, 512)
self.relu4 = torch.nn.ReLU()
self.fc2 = torch.nn.Linear(512, num_classes)
def forward(self, x):
x = self.conv1(x)
x = self.relu1(x)
x = self.conv2(x)
x = self.relu2(x)
x = self.conv3(x)
x = self.relu3(x)
x = self.pool(x)
x = x.view(-1, 128 * 14 * 14)
x = self.fc1(x)
x = self.relu4(x)
x = self.fc2(x)
return x
model = FruitNet(num_classes)
```
5. 定义损失函数和优化器
```python
# 定义损失函数和优化器
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
```
6. 训练模型
```python
# 训练模型
num_epochs = 20
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
model.to(device)
for epoch in range(num_epochs):
train_loss = 0.0
train_acc = 0.0
model.train()
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
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.double() / len(train_loader.dataset)
print('Epoch: {}/{} Train Loss: {:.4f} Train Acc: {:.4f}'.format(epoch + 1, num_epochs, train_loss, train_acc))
model.eval()
with torch.no_grad():
test_loss = 0.0
test_acc = 0.0
for i, (images, labels) in enumerate(test_loader):
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
test_loss += loss.item() * images.size(0)
_, preds = torch.max(outputs, 1)
test_acc += torch.sum(preds == labels.data)
test_loss = test_loss / len(test_loader.dataset)
test_acc = test_acc.double() / len(test_loader.dataset)
print('Test Loss: {:.4f} Test Acc: {:.4f}'.format(test_loss, test_acc))
```
7. 使用训练好的模型进行预测
```python
# 使用训练好的模型进行预测
model.eval()
image_path = 'data/fruits-360/Test/Apple Braeburn/0_100.jpg'
image = Image.open(image_path)
plt.imshow(image)
plt.show()
image_tensor = transform(image)
image_tensor = image_tensor.unsqueeze(0)
image_tensor = image_tensor.to(device)
with torch.no_grad():
output = model(image_tensor)
_, preds = torch.max(output, 1)
print(classes[preds.item()])
```
这样,我们就完成了基于PyTorch的水果图像识别系统的开发,并且使用GPU加速了训练模型。
阅读全文