Pytorch框架——知识蒸馏(Distillation)分类实战
·
蒸馏神经网络取名为蒸馏(Distill),其实是一个非常形象的过程。
我们把数据结构信息和数据本身当作一个混合物,分布信息通过概率分布被分离出来。首先,T值很大,相当于用很高的温度将关键的分布信息从原有的数据中分离,之后在同样的温度下用新模型融合蒸馏出来的数据分布,最后恢复温度,让两者充分融合。这也可以看成Prof. Hinton将这一个迁移学习过程命名为蒸馏的原因。
蒸馏神经网络想做的事情,本质上更接近于迁移学习(Transfer Learning),当然也可从模型压缩(Model Compression)的角度取理解蒸馏神经网络。
详细的推道过程和理论,可以参见我的另外一篇博客:知识蒸馏(Distillation)简介_自蒸馏算法大神-CSDN博客
talk is cheap show me code
"""
Function:knowledge distillation
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
torch.manual_seed(0)
# torch.cuda.manual_seed(0)
# 定义教师网络
class TeacherNet(nn.Module):
def __init__(self):
super(TeacherNet, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.3)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x,2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
output = self.fc2(x)
return output
# 训练过程
def train_teacher(model, device, train_loader, optimizer, epoch):
# 启用 BatchNormalization 和 Dropout
model.train()
trained_samples = 0
for batch_idx, (data, target) in enumerate(train_loader):
# 搬到指定gpu或者cpu设备上运算
data, target = data.to(device), target.to(device)
# 梯度清零
optimizer.zero_grad()
# 前向传播
output = model(data)
# 计算误差
loss = F.cross_entropy(output, target)
# 误差反向传播
loss.backward()
# 梯度更新一步
optimizer.step()
# 统计已经训练的数据量
trained_samples += len(data)
progress = math.ceil(batch_idx / len(train_loader) * 50)
print('\rTrain epoch: {} {}/{} [{}]{}%'.format(epoch, trained_samples, len(train_loader.dataset), '-'*progress+'>', progress*2), end='')
# 测试过程
def test_teacher(model, device, test_loader):
# 不启用 BatchNormalization 和 Dropout
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.cross_entropy(output, target, reduction='sum').item()
# 输出预测类别
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest: average loss: {:.4f}, accuracy:{}/{},({:.0f}%)'.format(
test_loss, correct,len(test_loader.dataset), 100* correct /len(test_loader.dataset)
))
return test_loss, correct / len(test_loader.dataset)
def teacher_main():
epochs = 10
batch_size = 64
torch.manual_seed(0)
mnist_path = 'MNIST'
# 动态设置硬件设备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_loader = torch.utils.data.DataLoader(
datasets.MNIST(mnist_path, train=True, download=True,
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))
])),
batch_size=batch_size,
shuffle=True
)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(mnist_path, train=False, download=True,
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))
])),
batch_size=1000, shuffle=True
)
# 实例化模型
model = TeacherNet().to(device)
# 选取优化器
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
teacher_history = []
for epoch in range(1, epochs+1):
print(epoch)
train_teacher(model, device, train_loader, optimizer, epoch)
loss, acc = test_teacher(model, device, test_loader)
teacher_history.append((loss, acc))
# 保存模型,state_dict:Returns a dictionary containing a whole state of the module.
torch.save(model.state_dict(), 'model/teacher.pt')
return model, teacher_history
# 构建学生网络
class Student(nn.Module):
def __init__(self):
super(Student, self).__init__()
self.fc1 = nn.Linear(28*28, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)
def forward(self, x):
x = torch.flatten(x, 1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
output = F.relu(self.fc3(x))
return output
def distillation(y, labels, teacher_scores, temp, alpha):
pass
# 训练学生网络
def train_student_kd(model, teacher_model, device, train_loader, optimizer, epoch):
model.train()
trained_samples = 0
for batch_idx, (data, target) in enumerate(train_loader):
# 搬到指定gpu或者cpu设备上运算
data, target = data.to(device), target.to(device)
# 梯度清零
optimizer.zero_grad()
# 前向传播
output = model(data)
# 老师输出
teacher_output = teacher_model(data)
# 计算误差
loss = distillation(output, target, teacher_output, temp=5., alpha=.7)
# 误差反向
loss.backward()
# 梯度更新一步
optimizer.step()
# 统计已经训练的数据量
trained_samples += len(data)
progress = math.ceil(batch_idx / len(train_loader) * 50)
print('\rTrain epoch: {} {}/{} [{}]{}%'.format(epoch, trained_samples, len(train_loader.dataset), '-'*progress+'>', progress*2), end='')
# 测试学生网络
def test_student_kd(model, device, test_loader):
# 不启用 BatchNormalization 和 Dropout
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.cross_entropy(output, target, reduction='sum').item()
# 输出预测类别
pred = output.argmax(dim=1, keepdim=True)
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest: average loss: {:.4f}, accuracy:{}/{},({:.0f}%)'.format(
test_loss, correct,len(test_loader.dataset), 100* correct /len(test_loader.dataset)
))
return test_loss, correct / len(test_loader.dataset)
def student_kd_main(teacher_m):
epochs = 10
batch_size = 64
torch.manual_seed(0)
mnist_path = 'MNIST'
# 动态设置硬件设备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_loader = torch.utils.data.DataLoader(
datasets.MNIST(mnist_path, train=True, download=False,
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))
])),
batch_size=batch_size,
shuffle=True
)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(mnist_path, train=False, download=False,
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,),(0.3081,))
])),
batch_size=1000, shuffle=True
)
# 实例化模型
model = Student().to(device)
# 选取优化器
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
student_history = []
for epoch in range(1, epochs+1):
print(epoch)
train_student_kd(model, teacher_m, device, train_loader, optimizer, epoch)
loss, acc = test_student_kd(model, device, test_loader)
student_history.append((loss, acc))
# 保存模型,state_dict:Returns a dictionary containing a whole state of the module.
torch.save(model.state_dict(), 'model/student.pt')
return model, student_history
# 让学生自己学,不使用KD
def train_student(model, device, train_loader, optimizer, epoch):
model.train()
trained_samples = 0
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.cross_entropy(output, target)
loss.backward()
optimizer.step()
trained_samples += len(data)
progress = math.ceil(batch_idx / len(train_loader) * 50)
print('\rTrain epoch: {} {}/{} [{}]{}%'.format(epoch, trained_samples, len(train_loader.dataset), '-'*progress+'>', progress*2), end='')
def test_student(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.cross_entropy(output, target, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest: average loss: {:.4f}, accuracy: {}/{} ({:.0f}%)'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
return test_loss, correct / len(test_loader.dataset)
def student_main():
epochs = 10
batch_size = 64
torch.manual_seed(0)
mnist_path = 'MNIST'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_loader = torch.utils.data.DataLoader(
datasets.MNIST(mnist_path, train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(mnist_path, train=False, download=True, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=1000, shuffle=True)
model = Student().to(device)
optimizer = torch.optim.Adadelta(model.parameters())
student_history = []
for epoch in range(1, epochs + 1):
train_student(model, device, train_loader, optimizer, epoch)
loss, acc = test_student(model, device, test_loader)
student_history.append((loss, acc))
torch.save(model.state_dict(), "student.pt")
return model, student_history
if __name__ == '__main__':
pass
代码运行之后的结果,很明显,经过知识蒸馏的结果好:

【源码下载】GitCode,关键词【Pytorch知识蒸馏】
整套项目源码内容包含
有训练代码和训练好的模型以及训练过程,提供数据,提供GUI界面代码
更多推荐
所有评论(0)