行人属性识别Loss计算
代码
class Weighted_BCELoss(object):
"""
Weighted_BCELoss was proposed in "Multi-attribute learning for pedestrian attribute recognition in surveillance scenarios".
"""
def __init__(self, experiment):
super(Weighted_BCELoss, self).__init__()
self.weights = None
if experiment == 'pa100k':
self.weights = torch.Tensor([0.460444444444,
0.0134555555556,
0.924377777778,
0.0621666666667,
0.352666666667,
0.294622222222,
0.352711111111,
0.0435444444444,
0.179977777778,
0.185,
0.192733333333,
0.1601,
0.00952222222222,
0.5834,
0.4166,
0.0494777777778,
0.151044444444,
0.107755555556,
0.0419111111111,
0.00472222222222,
0.0168888888889,
0.0324111111111,
0.711711111111,
0.173444444444,
0.114844444444,
0.006]).cuda()
elif experiment == 'rap':
self.weights = torch.Tensor([0.311434,
0.009980,
0.430011,
0.560010,
0.144932,
0.742479,
0.097728,
0.946303,
0.048287,
0.004328,
0.189323,
0.944764,
0.016713,
0.072959,
0.010461,
0.221186,
0.123434,
0.057785,
0.228857,
0.172779,
0.315186,
0.022147,
0.030299,
0.017843,
0.560346,
0.000553,
0.027991,
0.036624,
0.268342,
0.133317,
0.302465,
0.270891,
0.124059,
0.012432,
0.157340,
0.018132,
0.064182,
0.028111,
0.042155,
0.027558,
0.012649,
0.024504,
0.294601,
0.034099,
0.032800,
0.091812,
0.024552,
0.010388,
0.017603,
0.023446,
0.128917]).cuda()
elif experiment == 'peta':
self.weights = torch.Tensor([0.5016,
0.3275,
0.1023,
0.0597,
0.1986,
0.2011,
0.8643,
0.8559,
0.1342,
0.1297,
0.1014,
0.0685,
0.314,
0.2932,
0.04,
0.2346,
0.5473,
0.2974,
0.0849,
0.7523,
0.2717,
0.0282,
0.0749,
0.0191,
0.3633,
0.0359,
0.1425,
0.0454,
0.2201,
0.0178,
0.0285,
0.5125,
0.0838,
0.4605,
0.0124]).cuda()
def forward(self, output, target, epoch):
if self.weights is not None:
cur_weights = torch.exp(target + (1 - target * 2) * self.weights)
loss = cur_weights * (target * torch.log(output + EPS)) + ((1 - target) * torch.log(1 - output + EPS))
else:
loss = target * torch.log(output + EPS) + (1 - target) * torch.log(1 - output + EPS)
return torch.neg(torch.mean(loss))
理解
用法: torch.neg(input, *, out=None) → Tensor 参数: input(Tensor) -输入张量。 关键字参数: out(Tensor,可选的) -输出张量。 返回具有 input 元素的负数的新张量。 out=?1×input
例子:
>>> a = torch.randn(5)
>>> a
tensor([ 0.0090, -0.2262, -0.0682, -0.2866, 0.3940])
>>> torch.neg(a)
tensor([-0.0090, 0.2262, 0.0682, 0.2866, -0.3940])
torch.mean(input) → Tensor
返回 input 张量中所有元素的平均值。
Parameters input(Tensor)–输入张量。
Example:
>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.2294, -0.5481, 1.3288]])
>>> torch.mean(a)
tensor(0.3367)
torch.mean(input, dim, keepdim=False, *, out=None) → Tensor
返回给定维度 dim 中 input 张量的每一行的平均值。如果 dim 是尺寸列表,请缩小所有尺寸。 如果 keepdim 为 True ,则输出张量的大小与 input 大小相同,只是尺寸为1的 dim 尺寸除外。否则,将 dim 压缩(请参见 torch.squeeze() ),从而使输出张量具有尺寸减少1(或 len(dim) )。
Parameters input(Tensor)–输入张量。 dim(python:ints的int或元组)–要减小的尺寸。 keepdim(bool)–输出张量是否保持 dim 。 關鍵字參數 out(Tensor ,可选)–输出张量。
Example:
>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.3841, 0.6320, 0.4254, -0.7384],
[-0.9644, 1.0131, -0.6549, -1.4279],
[-0.2951, -1.3350, -0.7694, 0.5600],
[ 1.0842, -0.9580, 0.3623, 0.2343]])
>>> torch.mean(a, 1)
tensor([-0.0163, -0.5085, -0.4599, 0.1807])
>>> torch.mean(a, 1, True)
tensor([[-0.0163],
[-0.5085],
[-0.4599],
[ 0.1807]])
|