如何解决具有加权类别的PyTorch语义分割
我正在研究如何对图像进行语义分割,但是我想为这些类分配权重。我正在使用此Semantic Segmentation PyTorch,在实际的实现中,无法将权重应用于类。 我试图更改util / functional.py,如下面报告的代码一样,但是培训阶段并未提高性能。特别是IOU_Score在0.26的值上振荡,而没有任何良好的性能。
我正在使用example文件夹中报告的笔记本电脑进行多类配置。
任何人都可以帮助我了解如何更改指标或如何使用分配了课程权重的其他指标。
谢谢您的帮助。
def iou(pr,gt,eps=1e-7,threshold=None,ignore_channels=None,weights=None):
"""Calculate Intersection over Union between ground truth and prediction
Args:
pr (torch.Tensor): predicted tensor
gt (torch.Tensor): ground truth tensor
eps (float): epsilon to avoid zero division
threshold: threshold for outputs binarization
Returns:
float: IoU (Jaccard) score
"""
pr = _threshold(pr,threshold=threshold)
pr,gt = _take_channels(pr,ignore_channels=ignore_channels)
#print('PR',pr.shape,type(pr))
#print('GT',gt.shape,type(gt))
pr1 = torch.zeros(pr.shape[0],pr.shape[1],pr.shape[2],pr.shape[3])
gt1 = torch.zeros(gt.shape[0],gt.shape[1],gt.shape[2],gt.shape[3])
pr1[:,:,:] = pr[:,:] / 100
pr1[:,1,:] / 2
pr1[:,2,3,4,:] / 100
gt1[:,:] = gt[:,:] / 100
gt1[:,:] / 2
gt1[:,:] / 100
intersection = torch.sum(gt1 * pr1)
union = torch.sum(gt1) + torch.sum(pr1) - intersection + eps
return (intersection + eps) / union
jaccard = iou
def f_score(pr,beta=1,weights=None):
"""Calculate F-score between ground truth and prediction
Args:
pr (torch.Tensor): predicted tensor
gt (torch.Tensor): ground truth tensor
beta (float): positive constant
eps (float): epsilon to avoid zero division
threshold: threshold for outputs binarization
Returns:
float: F score
"""
pr = _threshold(pr,:] / 100
tp = torch.sum(gt1 * pr1)
fp = torch.sum(pr1) - tp
fn = torch.sum(gt1) - tp
score = ((1 + beta ** 2) * tp + eps) \
/ ((1 + beta ** 2) * tp + beta ** 2 * fn + fp + eps)
return score
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。