人脸识别ArcFace损失函数(代码)

人脸识别ArcFace损失函数(代码)

# 实现方式1
class ArcLoss1(nn.Module):
	def __init__(self, class_num, feature_num, s=10, m=0.1):
		super().__init__()
		self.class_num = class_num
		self.feature_num = feature_num
		self.s = s
		self.m = torch.tensor(m)
		self.w = nn.Parameter(torch.rand(feature_num, class_num))  # 2*10

	def forward(self, feature):
		feature = F.normalize(feature, dim=1)  # 128*2
		w = F.normalize(self.w, dim=0)  # 2*10
		cos_theat = torch.matmul(feature, w) / 10
		sin_theat = torch.sqrt(1.0 - torch.pow(cos_theat, 2))
		cos_theat_m = cos_theat * torch.cos(self.m) - sin_theat * torch.sin(self.m)
		cos_theat_ = torch.exp(cos_theat * self.s)
		sum_cos_theat = torch.sum(torch.exp(cos_theat * self.s), dim=1, keepdim=True) - cos_theat_
		top = torch.exp(cos_theat_m * self.s)
		divide = (top / (top + sum_cos_theat))
		return divide

# 实现方式2
class ArcLoss2(nn.Module):
	def __init__(self, feature_dim=2, cls_dim=10):
		super().__init__()
		self.W = nn.Parameter(torch.randn(feature_dim, cls_dim))

	def forward(self, feature, m=1, s=10):
		x = F.normalize(feature, dim=1)
		w = F.normalize(self.W, dim=0)
		cos = torch.matmul(x, w)/10
		a = torch.acos(cos)
		top = torch.exp(s*torch.cos(a+m))
		down2 = torch.sum(torch.exp(s*torch.cos(a)), dim=1, keepdim=True)-torch.exp(s*torch.cos(a))
		out = torch.log(top/(top+down2))
		return out
代码先放这儿,后面再来解释


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值