Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test vgg-face with 4096 dimensional outputs on LFW dataset #944

Closed
serengil opened this issue Jan 7, 2024 · 11 comments
Closed

test vgg-face with 4096 dimensional outputs on LFW dataset #944

serengil opened this issue Jan 7, 2024 · 11 comments
Assignees
Labels
enhancement New feature or request

Comments

@serengil
Copy link
Owner

serengil commented Jan 7, 2024

Currently, our vgg-face model has 2622 outputs. But this is the output for classification model of vgg-face model. In original paper, they are using its early layer with 4096 dimensions for descriptor. Test this and compare with exisiting model.

@serengil serengil added the enhancement New feature or request label Jan 7, 2024
@serengil
Copy link
Owner Author

serengil commented Jan 7, 2024

If we change, VGGFace.py as

    # from
    # vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output)
    # to
    base_model_output = Sequential()
    base_model_output = Convolution2D(4096, (1, 1), name="predictions")(model.layers[-4].output)
    base_model_output = Flatten()(base_model_output)
    vgg_face_descriptor = Model(inputs=model.input, outputs=base_model_output)

and update distance.py as

"VGG-Face": {"cosine": 0.66, "euclidean": 0.61, "euclidean_l2": 1.12}

thresholds are found from this tutorial.

cosine and euclidean_l2 metrics are giving good results but euclidean is giving bad result as shown in attachment.

early-results

@serengil serengil self-assigned this Jan 7, 2024
@serengil
Copy link
Owner Author

serengil commented Jan 7, 2024

on the other hand, model with 2622 dimensional vectors has the following distributions. euclidean is more stable, but cosine and euclidean l2 was more stable in 4096d one.

results-2622d

@serengil
Copy link
Owner Author

serengil commented Jan 7, 2024

Finally, if we replace the vgg descriptor as

    base_model_output = Sequential()
    base_model_output = Convolution2D(4096, (1, 1), name="predictions")(model.layers[-4].output)
    base_model_output = Flatten()(base_model_output)
    base_model_output = Activation("softmax")(base_model_output)
    vgg_face_descriptor = Model(inputs=model.input, outputs=base_model_output)

(Just added softmax layer in addition to first experiment)

We have these results:

4096-with-softmax

So, dropping softmax layer makes it more stable.

@serengil
Copy link
Owner Author

serengil commented Jan 7, 2024

  • try 4096 dimensional model without softmax layer on LFW dataset.
  • if cosine and euclidean_l2 overperforms with this form, then overwrite euclidean with euclidean_l2 for vgg-face model.

@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

When I do perform a test for VGG-Face and mtcnn detector

Model Distance Metric LFW Score
2622d euclidean_l2 90.3
2622d cosine 90.3
4096d euclidean_l2 96.0
4096d cosine 96.0

So, using its early layer obviously contributed the score

btw, the best thresholds are 0.6975578183956332 for cosine and 1.186702879413616 for euclidean_l2.

@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

To avoid the failure in euclidean distance metric, what if we add normalization layer to the descriptor? I do not want to overwrite euclidean to euclidean_l2 when model is set to VGG.

base_model_output = Sequential()
    base_model_output = Convolution2D(4096, (1, 1), name="predictions")(model.layers[-4].output)
    base_model_output = Flatten()(base_model_output)
    base_model_output = Lambda(lambda x: K.l2_normalize(x, axis=1), name="norm_layer")(
        base_model_output
    )
    vgg_face_descriptor = Model(inputs=model.input, outputs=base_model_output)

distribution graph on deepface unit test items according to this tutorial becomes stable for euclidean (because it would be equivalent to euclidean_l2), and negative impact on cosine and euclidean_l2.
norm_layer

now, testing all metrics on LFW.

@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

To sum up, (model_name ="VGG-Face", detector_backend = "mtcnn", align = True, enforce_detection = False) configuration is tested on LFW dataset's test samples (1000 instances distributed 50% 50% for same person and different persons);

  • In the 4096d one, there is NO softmax layer and there is a l2 normalization layer.
distance metric 2622d 4096d
cosine 90.3 96.0
euclidean 82.2 96.0
euclidean_l2 90.3 96.0

and these are the thresholds:

cosine 0.7077406336745886
euclidean 1.1897399682146557
euclidean_l2 1.1897399998945895

So, doing this action increases the model performance 6% o 14%!

@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

now, it is creating different embeddings in every run. we should get rid of the Convolution2D in the descriptor because it generates random weights for that layer!

base_model_output = Sequential()
    base_model_output = Flatten()(model.layers[-5].output)
    base_model_output = Lambda(lambda x: K.l2_normalize(x, axis=1), name="norm_layer")(
        base_model_output
    )
    vgg_face_descriptor = Model(inputs=model.input, outputs=base_model_output)

@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

With the new structure, same accuracy values gotten

distance metric 2622d 4096d
cosine 90.3 96.0
euclidean 82.2 96.0
euclidean_l2 90.3 96.0

for these thresholds

metric threshold
cosine 0.6848793279466694
euclidean 1.1735069839807788
euclidean_l2 1.1735069853005953

@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

Closed with PR - #948

@serengil serengil closed this as completed Jan 8, 2024
@serengil
Copy link
Owner Author

serengil commented Jan 8, 2024

vgg-face is the default facial recognition model. so, this improvement will affect every user of deepface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant