pytorch VGG16 迁移学习服装检索
时间: 2023-07-17 13:04:39 浏览: 118
对于服装检索任务,可以利用迁移学习将预训练的VGG16模型应用于新的数据集。以下是一个基本的迁移学习流程:
1. 导入所需的库和模块:
```python
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, models, transforms
```
2. 定义数据预处理和增强的转换:
```python
data_transforms = {
'train': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(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])
]),
}
```
3. 加载数据集,并将其转换为可用于训练的格式:
```python
data_dir = 'path/to/dataset'
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=64, shuffle=True) for x in ['train', 'val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
class_names = image_datasets['train'].classes
```
4. 加载预训练的VGG16模型:
```python
model = models.vgg16(pretrained=True)
```
5. 冻结卷积层的参数,只训练全连接层:
```python
for param in model.parameters():
param.requires_grad = False
num_features = model.classifier[6].in_features
model.classifier[6] = nn.Linear(num_features, len(class_names))
```
6. 定义损失函数和优化器:
```python
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
```
7. 进行模型训练:
```python
def train_model(model, criterion, optimizer, num_epochs=25):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = model.to(device)
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))
return model
model = train_model(model, criterion, optimizer, num_epochs=25)
```
8. 使用训练好的模型进行预测:
```python
def predict(model, image_path):
image = Image.open(image_path)
image_tensor = data_transforms['val'](image).unsqueeze(0)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
image_tensor = image_tensor.to(device)
model = model.to(device)
model.eval()
with torch.no_grad():
outputs = model(image_tensor)
_, predicted = torch.max(outputs, 1)
return class_names[predicted.item()]
```
以上是一个基本的迁移学习示例,你可以根据自己的需求进行相应的调整和优化。注意,这只是一个示例,并不代表最佳实践。
阅读全文