zhen8838 / Circle-Loss

Tensorflow2 implementation of CircleLoss. Support class-level, sparse class-level, pair-wise labels
MIT License
108 stars 40 forks source link

Examples of PairCircleLoss #6

Open jinhong-ni opened 3 years ago

jinhong-ni commented 3 years ago

Hi there,

Really appreciate your implementation on Circle loss. I'm trying to utilize circle loss for an eye recognition task, which would use a CNN for encoding eyes and construct positive and negative similarity pairs. I think PairCircleLoss is the most suitable for this task. As the example task provided is still classical classification task, could you please provide some examples of how to use pairwise circle loss? Thank you in advance. :)

zhen8838 commented 3 years ago

Just like the classical face recognition task, make the batch data input, the y_true is reference image, compute the each data cos similarity with y_true , if current data have the same label with y_true, put it into sp , else put into sn. Finally, use pairwise circle loss.

jinhong-ni commented 3 years ago

Thanks for the fast response! I'm a bit new to Tensorflow and keras, so just to wish I'm understanding this correctly.

First we have a model to encode images into vectors: encoder=tf.keras.Sequential([ ...# model architecture here ])

Then we encode the references, true label images and false label images: true_label=encoder(X_true) false_label=encoder(X_false) reference=encoder(reference)

And compute cosine similarity scores: sp=cosine_similarity(true_label,reference) sn=cosine_similarity(false_label,reference)

Then compile the model with PairCircleLoss (This is where I get confused, since similarity score is a unique value, does that mean the shape of the input is ()?): `model = Model(Input(shape=()), Input(shape=()))

model.compile( loss=PairCircleLoss(), optimizer=k.optimizers.Adam(), metrics=[k.metrics.CategoricalAccuracy('acc')] )`

And finally we could fit the model with 'sp' and 'sn': model.fit(sp, sn)

Does the implementation above seem correct? And could you please clarify what should I do to handle the input of the model as similarity scores? Much appreciate for your patience and response!