deepinsight / insightface

State-of-the-art 2D and 3D Face Analysis Project
https://insightface.ai
23.37k stars 5.41k forks source link

arcloss implementation #362

Closed yang2640 closed 6 years ago

yang2640 commented 6 years ago
elif args.loss_type == 4: # arc face
    s = args.margin_s
    m = args.margin_m
    assert s > 0.0
    assert m >= 0.0
    assert m < (math.pi / 2)

    # start to compute the cos(theta)
    _weight = mx.symbol.L2Normalization(_weight, mode='instance')
    nembedding = mx.symbol.L2Normalization(embedding, mode='instance', name='fc1n') * s
    fc7 = mx.sym.FullyConnected(data=nembedding, weight=_weight, no_bias=True, num_hidden=args.num_classes,
                                name='fc7')
    zy = mx.sym.pick(fc7, gt_label, axis=1)
    cos_t = zy / s
    # end to compute the cos(theta)

    cos_m = math.cos(m)
    sin_m = math.sin(m)
    **mm = math.sin(math.pi - m) * m**
    # threshold = 0.0
    threshold = math.cos(math.pi - m)
    if args.easy_margin:
        cond = mx.symbol.Activation(data=cos_t, act_type='relu')
    else:
        cond_v = cos_t - threshold
        cond = mx.symbol.Activation(data=cond_v, act_type='relu')
    # compute cos(theta + m)
    body = cos_t * cos_t
    body = 1.0 - body
    sin_t = mx.sym.sqrt(body)
    new_zy = cos_t * cos_m
    b = sin_t * sin_m
    new_zy = new_zy - b
    # new_zy is cos(theta + m) = cos(theta)*cos(m) - sin(theta)*sin(m)
    new_zy = new_zy * s
    if args.easy_margin:
        zy_keep = zy
    else:
        **zy_keep = zy - s * mm**

1) In the above arc loss implementation, what's the motivation of doing "zy_keep = zy - s mm" ? 2) How does "mm = math.sin(math.pi - m) m" derived ?

chenkingwen commented 6 years ago

The same confusion with you, theta*sin(theta) means what?@nttstar

JingyuanHu commented 6 years ago

108 Maybe you can find the answer in this issue

chenkingwen commented 6 years ago

thank you! @JingyuanHu