leondgarse / Keras_insightface

Insightface Keras implementation
MIT License
234 stars 56 forks source link

Balanced Meta-Softmax mixed with CosFace #96

Open abdikaiym01 opened 2 years ago

abdikaiym01 commented 2 years ago

Hello @leondgarse, I've implemeted Balanced Meta-Softmax (https://github.com/jiawei-ren/BalancedMetaSoftmax) with CosFace loss function, but I have problem with the сonvergence. Test metric fall down like AgeDB. Could you implement this on your great framework?

leondgarse commented 2 years ago

There are something I've thought about:

abdikaiym01 commented 2 years ago

Ok, I have some troubles, with the сonvergence. What would you suggest to make it work well since the some datasets are very long-tailed?

leondgarse commented 2 years ago

I don't have many experience in handling those long-tailed classification either. Speaking this Meta-Softmax, I think the original should work better with softmax loss. But with CosFace loss, I think it may be better using * instead of + combing logits with sample_per_class:

  class CosFaceLoss(ArcfaceLossSimple):
      def __init__(self, margin=0.35, scale=64.0, from_logits=True, label_smoothing=0, sample_per_class=None, **kwargs):
          super(CosFaceLoss, self).__init__(margin, scale, from_logits, label_smoothing, **kwargs)
          self.sample_per_class = sample_per_class

      def call(self, y_true, norm_logits):
          if self.batch_labels_back_up is not None:
              self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
          pick_cond = tf.cast(y_true, dtype=tf.bool)
          logits = tf.where(pick_cond, norm_logits - self.margin, norm_logits)
          if self.sample_per_class is not None:
              logits *= self.sample_per_class  # * or +
          logits *= self.scale
          return tf.keras.losses.categorical_crossentropy(y_true, logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)

Anyway, I didn't take any test on this...