基于预训练的ViT-Base模型,使用包含100种类型水果的数据集进行训练代码实现
时间: 2023-06-24 11:07:53 浏览: 173
水果识别训练模型与数据集
以下是使用PyTorch实现的基于预训练的ViT-Base模型的代码,用于对包含100种水果的数据集进行训练:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from timm.models.vision_transformer import VisionTransformer
# 定义超参数
batch_size = 32
learning_rate = 0.001
num_epochs = 10
# 定义数据增强和数据加载器
transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
train_data = datasets.ImageFolder('./fruits-360/Training', transform=transform)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
test_data = datasets.ImageFolder('./fruits-360/Test', transform=transform)
test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True)
# 定义模型
model = VisionTransformer(img_size=224, patch_size=32, in_chans=3, num_classes=100)
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
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()
# 每100个批次打印一次日志
if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch + 1, num_epochs, i + 1, len(train_loader), loss.item()))
# 测试模型
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print('Test Accuracy of the model on the {} test images: {:.2f}%'.format(total, accuracy))
# 保存模型
torch.save(model.state_dict(), './vit_fruit_classification.pth')
```
在训练完模型后,可以使用测试集对模型进行评估,并将模型保存为.pth文件以备后续使用。
阅读全文