提交 435144b5 编写于 作者: D dongshuilong

fix bugs imgnet and rec train

上级 452f5321
......@@ -50,6 +50,8 @@ class ArcMargin(nn.Layer):
weight = paddle.divide(weight, weight_norm)
cos = paddle.matmul(input, weight)
if not self.training:
return cos
sin = paddle.sqrt(1.0 - paddle.square(cos) + 1e-6)
cos_m = math.cos(self.margin)
sin_m = math.sin(self.margin)
......
......@@ -39,6 +39,9 @@ Loss:
- TripletLossV2:
weight: 1.0
margin: 0.5
Eval:
- CELoss:
weight: 1.0
Optimizer:
name: Momentum
......
......@@ -244,6 +244,9 @@ class Trainer(object):
batch[0] = paddle.to_tensor(batch[0]).astype("float32")
batch[1] = paddle.to_tensor(batch[1]).reshape([-1, 1])
# image input
if self.is_rec:
out = self.model(batch[0], batch[1])
else:
out = self.model(batch[0])
# calc build
if loss_func is not None:
......
......@@ -43,6 +43,6 @@ class CombinedLoss(nn.Layer):
def build_loss(config):
module_class = CombinedLoss(config)
module_class = CombinedLoss(copy.deepcopy(config))
logger.info("build loss {} success.".format(module_class))
return module_class
......@@ -22,6 +22,7 @@ class Loss(object):
"""
Loss
"""
def __init__(self, class_dim=1000, epsilon=None):
assert class_dim > 1, "class_dim=%d is not larger than 1" % (class_dim)
self._class_dim = class_dim
......@@ -35,12 +36,16 @@ class Loss(object):
#do label_smoothing
def _labelsmoothing(self, target):
if target.shape[-1] != self._class_dim:
one_hot_target = F.one_hot(target, self._class_dim) #do ont hot(23,34,46)-> 3 * _class_dim
one_hot_target = F.one_hot(
target,
self._class_dim) #do ont hot(23,34,46)-> 3 * _class_dim
else:
one_hot_target = target
#do label_smooth
soft_target = F.label_smooth(one_hot_target, epsilon=self._epsilon) #(1 - epsilon) * input + eposilon / K.
soft_target = F.label_smooth(
one_hot_target,
epsilon=self._epsilon) #(1 - epsilon) * input + eposilon / K.
soft_target = paddle.reshape(soft_target, shape=[-1, self._class_dim])
return soft_target
......@@ -64,7 +69,8 @@ class Loss(object):
(target + eps) / (input + eps)) * self._class_dim
return cost
def _jsdiv(self, input, target): #so the input and target is the fc output; no softmax
def _jsdiv(self, input,
target): #so the input and target is the fc output; no softmax
input = F.softmax(input)
target = F.softmax(target)
......@@ -87,14 +93,19 @@ class CELoss(Loss):
super(CELoss, self).__init__(class_dim, epsilon)
def __call__(self, input, target, use_pure_fp16=False):
if type(input) is dict:
logits = input["logits"]
else:
logits = input
cost = self._crossentropy(logits, target, use_pure_fp16)
return {"CELoss": cost}
class JSDivLoss(Loss):
"""
JSDiv loss
"""
def __init__(self, class_dim=1000, epsilon=None):
super(JSDivLoss, self).__init__(class_dim, epsilon)
......@@ -112,4 +123,3 @@ class KLDivLoss(paddle.nn.Layer):
p = paddle.nn.functional.softmax(p)
q = paddle.nn.functional.softmax(q)
return -(p * paddle.log(q + 1e-8)).sum(1).mean()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册