没有合适的资源?快使用搜索试试~ 我知道了~
首页pytorch实现focal loss的两种方式小结
pytorch实现focal loss的两种方式小结
16 下载量 15 浏览量
更新于2023-05-04
收藏 49KB PDF 举报
我就废话不多说了,直接上代码吧! import torch import torch.nn.functional as F import numpy as np from torch.autograd import Variable ''' pytorch实现focal loss的两种方式(现在讨论的是基于分割任务) 在计算损失函数的过程中考虑到类别不平衡的问题,假设加上背景类别共有6个类别 ''' def compute_class_weights(histogram): classWeights = np.ones(6, dtype=np.float32) normHist =
资源详情
资源推荐
pytorch实现实现focal loss的两种方式小结的两种方式小结
我就废话不多说了,直接上代码吧!我就废话不多说了,直接上代码吧!
import torch
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
'''
pytorch实现focal loss的两种方式(现在讨论的是基于分割任务)
在计算损失函数的过程中考虑到类别不平衡的问题,假设加上背景类别共有6个类别
'''
def compute_class_weights(histogram):
classWeights = np.ones(6, dtype=np.float32)
normHist = histogram / np.sum(histogram)
for i in range(6):
classWeights[i] = 1 / (np.log(1.10 + normHist[i]))
return classWeights
def focal_loss_my(input,target):
'''
:param input: shape [batch_size,num_classes,H,W] 仅仅经过卷积操作后的输出,并没有经过任何激活函数的作用
:param target: shape [batch_size,H,W] :return:
'''
n, c, h, w = input.size()
target = target.long()
input = input.transpose(1, 2).transpose(2, 3).contiguous().view(-1, c)
target = target.contiguous().view(-1)
number_0 = torch.sum(target == 0).item()
number_1 = torch.sum(target == 1).item()
number_2 = torch.sum(target == 2).item()
number_3 = torch.sum(target == 3).item()
number_4 = torch.sum(target == 4).item()
number_5 = torch.sum(target == 5).item()
frequency = torch.tensor((number_0, number_1, number_2, number_3, number_4, number_5), dtype=torch.float32)
frequency = frequency.numpy()
classWeights = compute_class_weights(frequency)
'''
根据当前给出的ground truth label计算出每个类别所占据的权重
'''
# weights=torch.from_numpy(classWeights).float().cuda()
weights = torch.from_numpy(classWeights).float()
focal_frequency = F.nll_loss(F.softmax(input, dim=1), target, reduction='none')
'''
上面一篇博文讲过
F.nll_loss(torch.log(F.softmax(inputs, dim=1),target)的函数功能与F.cross_entropy相同
可见F.nll_loss中实现了对于target的one-hot encoding编码功能,将其编码成与input shape相同的tensor
然后与前面那一项(即F.nll_loss输入的第一项)进行 element-wise production
相当于取出了 log(p_gt)即当前样本点被分类为正确类别的概率
现在去掉取log的操作,相当于 focal_frequency shape [num_samples] 即取出ground truth类别的概率数值,并取了负号
'''
focal_frequency += 1.0#shape [num_samples] 1-P(gt_classes)
focal_frequency = torch.pow(focal_frequency, 2) # torch.Size([75])
focal_frequency = focal_frequency.repeat(c, 1)
'''
进行repeat操作后,focal_frequency shape [num_classes,num_samples] '''
focal_frequency = focal_frequency.transpose(1, 0)
loss = F.nll_loss(focal_frequency * (torch.log(F.softmax(input, dim=1))), target, weight=None,
reduction='elementwise_mean')
return loss
def focal_loss_zhihu(input, target):
'''
:param input: 使用知乎上面大神给出的方案 https://zhuanlan.zhihu.com/p/28527749
:param target:
:return:
weixin_38593723
- 粉丝: 5
- 资源: 919
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功