代码拉取完成,页面将自动刷新
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
# import ohem_cpp
# class OhemCELoss(nn.Module):
#
# def __init__(self, thresh, lb_ignore=255):
# super(OhemCELoss, self).__init__()
# self.score_thresh = thresh
# self.lb_ignore = lb_ignore
# self.criteria = nn.CrossEntropyLoss(ignore_index=lb_ignore, reduction='mean')
#
# def forward(self, logits, labels):
# n_min = labels[labels != self.lb_ignore].numel() // 16
# labels = ohem_cpp.score_ohem_label(
# logits, labels, self.lb_ignore, self.score_thresh, n_min).detach()
# loss = self.criteria(logits, labels)
# return loss
class OhemCELoss(nn.Module):
def __init__(self, thresh, lb_ignore=255):
super(OhemCELoss, self).__init__()
self.thresh = -torch.log(torch.tensor(thresh, requires_grad=False, dtype=torch.float)).cuda()
self.lb_ignore = lb_ignore
self.criteria = nn.CrossEntropyLoss(ignore_index=lb_ignore, reduction='none')
def forward(self, logits, labels):
n_min = labels[labels != self.lb_ignore].numel() // 16
loss = self.criteria(logits, labels).view(-1)
loss_hard = loss[loss > self.thresh]
if loss_hard.numel() < n_min:
loss_hard, _ = loss.topk(n_min)
return torch.mean(loss_hard)
if __name__ == '__main__':
pass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。