You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.5 KiB
70 lines
2.5 KiB
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
import math
|
|
|
|
import paddle
|
|
import paddle.nn as nn
|
|
import paddle.nn.functional as F
|
|
|
|
|
|
class AngularMargin(nn.Layer):
|
|
def __init__(self, margin=0.0, scale=1.0):
|
|
super(AngularMargin, self).__init__()
|
|
self.margin = margin
|
|
self.scale = scale
|
|
|
|
def forward(self, outputs, targets):
|
|
outputs = outputs - self.margin * targets
|
|
return self.scale * outputs
|
|
|
|
|
|
class AdditiveAngularMargin(AngularMargin):
|
|
def __init__(self, margin=0.0, scale=1.0, easy_margin=False):
|
|
super(AdditiveAngularMargin, self).__init__(margin, scale)
|
|
self.easy_margin = easy_margin
|
|
|
|
self.cos_m = math.cos(self.margin)
|
|
self.sin_m = math.sin(self.margin)
|
|
self.th = math.cos(math.pi - self.margin)
|
|
self.mm = math.sin(math.pi - self.margin) * self.margin
|
|
|
|
def forward(self, outputs, targets):
|
|
cosine = outputs.astype('float32')
|
|
sine = paddle.sqrt(1.0 - paddle.pow(cosine, 2))
|
|
phi = cosine * self.cos_m - sine * self.sin_m # cos(theta + m)
|
|
if self.easy_margin:
|
|
phi = paddle.where(cosine > 0, phi, cosine)
|
|
else:
|
|
phi = paddle.where(cosine > self.th, phi, cosine - self.mm)
|
|
outputs = (targets * phi) + ((1.0 - targets) * cosine)
|
|
return self.scale * outputs
|
|
|
|
|
|
class LogSoftmaxWrapper(nn.Layer):
|
|
def __init__(self, loss_fn):
|
|
super(LogSoftmaxWrapper, self).__init__()
|
|
self.loss_fn = loss_fn
|
|
self.criterion = paddle.nn.KLDivLoss(reduction="sum")
|
|
|
|
def forward(self, outputs, targets, length=None):
|
|
targets = F.one_hot(targets, outputs.shape[1])
|
|
try:
|
|
predictions = self.loss_fn(outputs, targets)
|
|
except TypeError:
|
|
predictions = self.loss_fn(outputs)
|
|
|
|
predictions = F.log_softmax(predictions, axis=1)
|
|
loss = self.criterion(predictions, targets) / targets.sum()
|
|
return loss
|