“f52cdaa0cee682ddc3588286af42d960141596f0”上不存在“paddle/fluid/operators/optimizers/proximal_gd_op.h”
未验证 提交 3b4a45f8 编写于 作者: B Bin Lu 提交者: GitHub

Update deephashloss.py

上级 f7ee2b98
......@@ -23,7 +23,7 @@ class DSHSDLoss(nn.Layer):
# [DSHSD] epoch:250, bit:48, dataset:nuswide_21, MAP:0.809, Best MAP: 0.815
# [DSHSD] epoch:135, bit:48, dataset:imagenet, MAP:0.647, Best MAP: 0.647
"""
def __init__(self, n_class, bit, alpha, multi_label=False):
def __init__(self, alpha, multi_label=False):
super(DSHSDLoss, self).__init__()
self.alpha = alpha
self.multi_label = multi_label
......@@ -32,8 +32,8 @@ class DSHSDLoss(nn.Layer):
feature = input["features"]
logits = input["logits"]
dist = paddle.sum(
paddle.square((paddle.unsqueeze(feature, 1) - paddle.unsqueeze(feature, 0))),
dist = paddle.sum(paddle.square(
(paddle.unsqueeze(feature, 1) - paddle.unsqueeze(feature, 0))),
axis=2)
# label to ont-hot
......@@ -41,20 +41,24 @@ class DSHSDLoss(nn.Layer):
n_class = logits.shape[1]
label = paddle.nn.functional.one_hot(label, n_class).astype("float32")
s = (paddle.matmul(label, label, transpose_y=True) == 0).astype("float32")
s = (paddle.matmul(
label, label, transpose_y=True) == 0).astype("float32")
margin = 2 * feature.shape[1]
Ld = (1 - s) / 2 * dist + s / 2 * (margin - dist).clip(min=0)
Ld = Ld.mean()
if self.multi_label:
# multiple labels classification loss
Lc = (logits - label * logits + ((1 + (-logits).exp()).log())).sum(axis=1).mean()
Lc = (logits - label * logits + (
(1 + (-logits).exp()).log())).sum(axis=1).mean()
else:
# single labels classification loss
Lc = (-paddle.nn.functional.softmax(logits).log() * label).sum(axis=1).mean()
Lc = (-paddle.nn.functional.softmax(logits).log() * label).sum(
axis=1).mean()
return {"dshsdloss": Lc + Ld * self.alpha}
class LCDSHLoss(nn.Layer):
"""
# paper [Locality-Constrained Deep Supervised Hashing for Image Retrieval](https://www.ijcai.org/Proceedings/2017/0499.pdf)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册