学习笔记--softmax--交叉熵--网络的预测过程--蒸馏--安库

1、理解softmax

import torch
import torch.nn.functional as F

output = torch.randn(2,3)
# 假如这是输出
print(output)

print(F.softmax(output,dim=1))# dim=1使每一行计算之和为1
"""
理解softmax
"""
# tensor([[ 1.5285, -0.1860, -0.4592],
#         [ 1.9413,  0.4243, -1.0071]])
# tensor([[0.7593, 0.1367, 0.1040],
#         [0.7863, 0.1725, 0.0412]])

2、理解交叉熵

import torch
import torch.nn.functional as F

output = torch.tensor(([[1.2,2,3]]))  # 假如是一个样本的预测结果
print(output)
target = torch.tensor([0])  # 这个样本的真实结果
print(target)

log_output = F.log_softmax(output,dim=1) # 先去取对数
print(log_output)
nll_loss = F.nll_loss(log_output,target) # 再去取负数
print(nll_loss)  # 得到的是交叉熵的结果

en = F.cross_entropy(output,target)
print(en)  # 交叉熵的实质
#tensor([[1.2000, 2.0000, 3.0000]])
#tensor([0])
#tensor([[-2.2273, -1.4273, -0.4273]])
#tensor(2.2273)
#tensor(2.2273)

3、神经网络的预测过程

输入图像送入卷积神经网络提取特征
拉伸卷积,送入全连接层
多层全连接层得到logits
经过softmax得到预测概率

4、知识蒸馏

教师传授知识给学生的方法:
1、教师网络经过训练输出一个类别概率分布
2、学生网络以教师网络的输出预测为指导,输出一个类别概率分布
3、设计学生网络的损失函数,最小化两个概率分布之间的差距
实质:训练一个学生网络模型来模仿一个预先训练好的教师网络模型预测输出概率分布

5、蒸馏过程

教师网络训练:

先训练一个层数较深的,提取能力强的网络
得到logits后,利用升温T的softmax得到的预测类别概率分布soft targets

蒸馏教师网络知识到学生网络

构造蒸馏损失(带T的)和学生损失,加权得到最终损失函数
蒸馏损失使得softmax target 和soft predictions越接近越好,这里用交叉熵或KL散度
学生损失使预测和真实标签越来越接近,这里用交叉熵损失在这里插入图片描述

6、模型训练

要保存最好的那个模型,最后输出最好的那个精度

def save_best(loss,accuracy,best_loss,best_acc):
	if best_loss==None:
		best_loss=loss
		best_acc = accuracy
		file= ".pth"
		torch.save(net.state_dict(),file)
	elif loss<best_loss and accuracy>best_acc:
		best_loss=loss
		best_acc=accuracy
		file=".pth"
		torch.save(net.state_dict(),file)
		## 存储模型的参数,如果存储模型,直接net就可以了
	return best_loss,best_acc

教师模型的模型参数就是知识

7、蒸馏

use_cuda = torch.cuda.is_available()
dtype=torch.cuda.FloatTensor if use_cuda else torch.FloatTensor
EPOCH=30
T,lambda_stu=5.0,0.05
teacher=teach()#加载模型
teacher.load_state_dict(torch.load(".pth"))# 加载参数
teacher.eval()
teacher.train(mode=False)
student=stu()
optimizer=optim.SGD(student.parameters(),lr=0.1,momentum=0.9,weight_decay=5e-4)
scheduler=StepLR(optimizer,step_size=10,gamma=0.5)
lossKD=nn.KLDivLoss()
lossCE=nn.CrossEntropyLoss()

留意知识蒸馏中有两个假设

8、取路径

./ # 你的代码在的地方
BASE_DIR = os.path.dirname(os.path.abspath(__file__))# 项目在计算机中的位置

9、

img_Lists = glob.glob(src_img_dir + '/*.png')
# 将一个目录里的图片写进列表
# ['D:\\py_project\\yolo2\\myVCOdevkit\\JPEGImage\\Black_Footed_Albatross_0001_796111.png', 'D:\\py_project\\yolo2\\myVCOdevkit\\JPEGImage\\Black_Footed_Albatross_0002_55.png', 'D:\\py_project\\yolo2\\myVCOdevkit\\JPEGImage\\Black_Footed_Albatross_0003_796136.png']
img_basenames = [] 
for item in img_Lists:
    img_basenames.append(os.path.basename(item))
# ['Black_Footed_Albatross_0001_796111.png', 'Black_Footed_Albatross_0002_55.png', 'Black_Footed_Albatross_0003_796136.png']
img_names = [] 
for item in img_basenames:
    temp1, temp2 = os.path.splitext(item)  # 1、文件名和扩展名分开
    img_names.append(temp1)
# ['Black_Footed_Albatross_0001_796111', 'Black_Footed_Albatross_0002_55', 'Black_Footed_Albatross_0003_796136']
for img in img_names:
    im = Image.open((src_img_dir + '/' + img + '.png'))  # 2、得到图片的尺寸,通道信息
    width, height = im.size 

10 安库必备??反正好多都找不见,他找见了

pip install 库名 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn 库名

11、Could not find a version that satisfies the requirement torch==1.4.0 (from torchvision)

网上各种解释,我的问题是,我想安装torch0.4.0,如果直接pip install torchvision的话可能会默认最新版本,但是最新版本和旧版本的torch并不匹配,所以我就搜了一下和torch0.4.0匹配的torchvision版本
pip install torchvision==0.2.2就安装成功啦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值