torch使用vgg16搭建图片识别模型,并使用本地的数据集
时间: 2023-12-25 15:05:27 浏览: 82
好的,我可以帮你解决这个问题。首先,你需要安装PyTorch和TorchVision,然后下载VGG16预训练模型。
以下是一个使用VGG16模型进行图像分类的基本示例代码,其中使用了本地的数据集:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, models, transforms
# 设置数据集路径
data_dir = "/path/to/your/data"
# 定义数据转换
data_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
# 加载数据集
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
data_transforms[x])
for x in ['train', 'val']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=4,
shuffle=True, num_workers=4)
for x in ['train', 'val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
class_names = image_datasets['train'].classes
# 加载VGG16模型
model = models.vgg16(pretrained=True)
# 冻结卷积层的参数
for param in model.features.parameters():
param.requires_grad = False
# 替换分类层
model.classifier = nn.Sequential(
nn.Linear(25088, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, 4096),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(4096, len(class_names))
)
# 训练模型
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.classifier.parameters(), lr=0.001, momentum=0.9)
# 训练模型
num_epochs = 10
for epoch in range(num_epochs):
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 / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# 保存模型
torch.save(model.state_dict(), 'vgg16_model.pth')
```
这个示例代码中定义了一个数据转换函数,用来对图像进行预处理。然后使用`ImageFolder`类加载本地数据集,接着使用`vgg16`函数加载预训练模型。接着冻结卷积层的参数,以便只训练全连接层。最后训练模型并保存模型权重。
阅读全文