• Margin Based Loss


    目录

    17-ICCV-Sampling Matters in Deep Embedding Learning

    Preliminaries

    contrastive loss

    triplet loss

    hard negative mining

    semi-hard negative mining

    Distance weighted sampling

    Margin based loss

    Relationship to isotonic regression


    17-ICCV-Sampling Matters in Deep Embedding Learning

    Preliminaries

    contrastive loss

    正样本尽可能近,负样本被固定距离α隔开

    visually diverse classes are embedded in the same small space as visually similar ones. The embedding space does not allow for distortions.

    triplet loss

    loss+sampling strategy

    1. embedding space to be arbitrarily distorted
    2. does not impose a constant margin α

    hard negative mining

    ——>模型坍塌

    semi-hard negative mining

    online selection:one triplet is sampled for every (a, p) pair

    offline selection:a batch has 1=3 of images as anchors, positives, and negatives respectively

    如果有正确的采样策略,简单的pairwse loss也是高效的。

    Distance weighted sampling
     

    n维单位球面成对距离分布:球面上取一固定点a,随机在球面上另选一点,这个点和a之间距离(余弦距离?球面上的距离?)为d的概率

    在高维空间中q(d)接近正态分布

     

    如果负样本均匀分散,随机抽样最可能得到的样本;阈值<,没有loss,训练不会有进展。

     负样本梯度

    决定梯度方向,如果很小(困难样本),有噪音z,梯度方向会被噪音主导。

    在anchor为a时负样本n被选中的概率

    1. 根据距离均匀采样,权重(采样概率与出现的概率成反比,随机采样到的概率越大乘的权重越小才能保证均匀)【 避免采样都聚集在
    2. 用λ切断权重采样【过近或过远的样本随机采样到的概率较小,对应的权重 会比较大。为了避免噪音样本,设定λ限制两端的样本权重不会过大】

    距离加权采样提供较大的距离范围,在控制方差的同时,稳定地生成信息丰富的示例。

    1. def inverse_sphere_distances(self, batch, anchor_to_all_dists, labels, anchor_label):
    2. dists = anchor_to_all_dists
    3. bs,dim = len(dists),batch.shape[-1]
    4. #negated log-distribution of distances of unit sphere in dimension <dim>
    5. log_q_d_inv = ((2.0 - float(dim)) * torch.log(dists) - (float(dim-3) / 2) * torch.log(1.0 - 0.25 * (dists.pow(2))))
    6. log_q_d_inv[np.where(labels==anchor_label)[0]] = 0
    7. q_d_inv = torch.exp(log_q_d_inv - torch.max(log_q_d_inv)) # - max(log) for stability
    8. q_d_inv[np.where(labels==anchor_label)[0]] = 0
    9. ### NOTE: Cutting of values with high distances made the results slightly worse. It can also lead to
    10. # errors where there are no available negatives (for high samples_per_class cases).
    11. # q_d_inv[np.where(dists.detach().cpu().numpy()>self.upper_cutoff)[0]] = 0
    12. q_d_inv = q_d_inv/q_d_inv.sum()
    13. return q_d_inv.detach().cpu().numpy()

    实际实现:权重log分布,没有λ切断可能更好

    Margin based loss

    • triplet loss没有预设阈值α分离相似和不相似的图片,灵活的扭曲空间容忍异常值,适应不同水平的不同类的类内方差。
    • triplet loss只需要正样本距离anchor比负样本近,contrastive loss使所有正样本尽量近是没必要的(细粒度识别允许类内差异,图像检索只需要相对关系)。
    • hard negative mining易导致模型坍塌:困难正样本对大吸引梯度,困难负样本对小排斥梯度,所有点聚在同一点(4b)。
    • 真实距离代替平方距离,对所有嵌入的梯度长度都为1(4c)。

    basic idea:ordinal regression关注相对顺序,不是绝对距离。Isotonic regression(保序回归)单独估计阈值,根据阈值惩罚分数。这里应用到成对距离而不是分数函数上:

    1. 在contrastive loss基础上,正样本距离在范围内即可而不是尽可能的近
    2. 为了获得和triplet loss的灵活性,增加了灵活边界参数 偏置β

    共同学习

     >0,=1;否则=0

     β越大越能更好的使用嵌入空间,需要正则化β

    Relationship to isotonic regression

    优化margin based loss=解决距离的排序问题

     

     

     

  • 相关阅读:
    容斥问题的一个例题[银行行测]
    Unity之NetCode多人网络游戏联机对战教程(5)--ConnectionData与MemoryPack
    ChatGPT改写:论文写作新时代
    Python【控制台输出案例】
    SQLite常用语句
    【云原生】Docker Compose初窥门径
    阿里 P8 架构师力荐 java 程序员人手一套 116 页 JVM 吊打面试官专属秘籍
    【K8S系列】第十讲:Knative 简介
    pycharm
    BOM批量查询
  • 原文地址:https://blog.csdn.net/weixin_44742887/article/details/125297945