迁移学习:使用 torchvision.models
,可以下载预训练的网络。猫狗照片数据集(从 Kaggle 上可以获得):https://www.kaggle.com/c/dogs-vs-cats
模块加载
%matplotlib inline
%config InlineBackend.figure_format = 'retina'import matplotlib.pyplot as pltimport torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
数据准备
data_dir = 'Cat_Dog_data'# TODO: Define transforms for the training data and testing data
train_transforms = transforms.Compose([transforms.RandomRotation(30),transforms.RandomResizedCrop(224),transforms.RandomHorizontalFlip(),transforms.ToTensor(),transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])])test_transforms = transforms.Compose([transforms.Resize(255),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])])# Pass transforms in here, then run the next cell to see how the transforms look
train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)
test_data = datasets.ImageFolder(data_dir + '/test', transform=test_transforms)trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
testloader = torch.utils.data.DataLoader(test_data, batch_size=64)
迁移学习的模型加载
model = models.densenet121(pretrained=True)
model
修改模型的classifier:模型由两部分组成-特征和分类器。特征部分由一堆卷积层组成,整体作为特征检测器传入分类器中。分类器是一个单独的全连接层 (classifier): Linear(in_features=1024, out_features=1000)
。这个层级是用 ImageNet 数据集训练过的层级,因此无法解决我们的问题。意味着我们需要替换分类器。但是特征就完全没有问题。你可以把预训练的网络看做是效果很好地的特征检测器,可以用作简单前馈分类器的输入。
# Freeze parameters so we don't backprop through them
for param in model.parameters():param.requires_grad = False from collections import OrderedDict
classifier = nn.Sequential(OrderedDict([('fc1', nn.Linear(1024, 500)),('relu', nn.ReLU()),('fc2', nn.Linear(500, 2)),('output', nn.LogSoftmax(dim=1))]))model.classifier = classifier
模型导入GPU
# Use GPU if it's available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")model = models.densenet121(pretrained=True) #下载densenet网咯# Freeze parameters so we don't backprop through them
for param in model.parameters():param.requires_grad = Falsemodel.classifier = nn.Sequential(nn.Linear(1024, 256), #修改下载模型的classifier,nn.ReLU(),nn.Dropout(0.2),nn.Linear(256, 2),nn.LogSoftmax(dim=1))criterion = nn.NLLLoss()# Only train the classifier parameters, feature parameters are frozen
optimizer = optim.Adam(model.classifier.parameters(), lr=0.003)model.to(device); #模型导入GPU
训练新模型
epochs = 1
steps = 0
running_loss = 0
print_every = 5
for epoch in range(epochs):for inputs, labels in trainloader:steps += 1# Move input and label tensors to the default deviceinputs, labels = inputs.to(device), labels.to(device) #将输入与标签导入GPUoptimizer.zero_grad() #梯度清0logps = model.forward(inputs) #导入输入,获得输出loss = criterion(logps, labels) #损失误差loss.backward() #反向传播optimizer.step() #更新权重running_loss += loss.item()if steps % print_every == 0:test_loss = 0accuracy = 0model.eval() #取消Dropoutwith torch.no_grad():for inputs, labels in testloader:inputs, labels = inputs.to(device), labels.to(device)logps = model.forward(inputs)batch_loss = criterion(logps, labels)test_loss += batch_loss.item()# Calculate accuracyps = torch.exp(logps)top_p, top_class = ps.topk(1, dim=1)equals = top_class == labels.view(*top_class.shape)accuracy += torch.mean(equals.type(torch.FloatTensor)).item()print(f"Epoch {epoch+1}/{epochs}.. "f"Train loss: {running_loss/print_every:.3f}.. "f"Test loss: {test_loss/len(testloader):.3f}.. "f"Test accuracy: {accuracy/len(testloader):.3f}")running_loss = 0model.train()