基于预训练的ViT-Base模型,使用包含100种类型水果的数据集进行训练,微调代码
时间: 2023-06-24 16:07:52 浏览: 152
以下是使用PyTorch和torchvision库微调预训练的ViT-Base模型的示例代码:
```python
import torch
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
from torch.optim import lr_scheduler
import torch.nn as nn
import time
import os
# 定义数据集路径和批次大小
data_dir = './fruits-360_dataset/fruits-360'
batch_size = 32
# 定义数据预处理
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])])
# 加载数据集
image_datasets = torchvision.datasets.ImageFolder(os.path.join(data_dir, 'Training'), transform=transform)
dataloaders = torch.utils.data.DataLoader(image_datasets, batch_size=batch_size, shuffle=True, num_workers=4)
# 定义模型
model = torchvision.models.vit_base_patch16_224(pretrained=True)
num_ftrs = model.head.in_features
model.head = nn.Linear(num_ftrs, len(image_datasets.classes))
# 定义优化器
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
# 定义学习率调度器
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
# 定义损失函数
criterion = nn.CrossEntropyLoss()
# 定义设备
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
# 训练模型
num_epochs = 10
for epoch in range(num_epochs):
print('Epoch {}/{}'.format(epoch+1, num_epochs))
print('-' * 10)
running_loss = 0.0
running_corrects = 0
for inputs, labels in dataloaders:
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
scheduler.step()
epoch_loss = running_loss / len(image_datasets)
epoch_acc = running_corrects.double() / len(image_datasets)
print('Loss: {:.4f} Acc: {:.4f}'.format(epoch_loss, epoch_acc))
```
其中,我们使用了预训练的ViT-Base模型,并将其头部修改为与数据集分类数相同的线性层。然后,我们定义了优化器、学习率调度器和损失函数,并将模型移动到GPU上进行训练。在训练过程中,我们使用了交叉熵损失函数和随机梯度下降优化器,并使用了学习率调度器来动态调整学习率。最后,我们计算了每个epoch的损失和准确率,并输出结果。
阅读全文