cubiq / ComfyUI_FaceAnalysis

Extension for ComfyUI to evaluate the similarity between two faces
Apache License 2.0
227 stars 25 forks source link

Other distance measurements to compare faces #24

Open abcnorio opened 3 weeks ago

abcnorio commented 3 weeks ago

Hello Matteo,

have you ever thought about adding Mahalanobis distance additional to Euclidean/ Cosine as it gives attention to correlations of the data set? Calculation with python doesn't look difficult (just know it from R). Problem may be that it requires an inverse of the cov matrix (such a calculation can fail...), but on the other hand it is quite an interesting value that under special circumstances equals the Euclidean distance. And the math people have better ways to calculate inversees like QR decomposition.

Then - the distance measures of face analyses - just a note that you surely know: the Euclidean distance is a special case of the Minkowski metric and the general formula of the Minkowski metric covers Manhattan/ Mannheim, Euclidean, and Chebyshev distance if one parameter of the formula is changed. Would be interesting whether different dist measures bring out the same results to your existent research on face comparisons (see also Bregman divergence and squared Euclidean distance if you shift from metric to mere statistical distance).

Example https://cran.r-project.org/web/packages/cmahalanobis/vignettes/my-vignette3.html (sorry, R not python, but easy to understand and applied also on the well-known iris dataset.

Note - what most of those dist measures show is that the arithmetic average as being part of the formula itself is sensitive to outliers which aturally limits the robustness of the statistic. Pls see a python example here on robust covar estimation: https://scikit-learn.org/stable/auto_examples/covariance/plot_mahalanobis_distances.html

If ome thinks this further one could also use the Kullback–Leibler divergence as a measurement of relative entropy (shifting the whole thing to information theory) which compares two prob dists but probably could be applied here as well to compare a reference vs. empirical example.

How did you decide which distance measure you use? I am just curious because there are so many present.

best

cubiq commented 3 weeks ago

How did you decide which distance measure you use? I am just curious because there are so many present.

normalized L2 and cosine are pretty much the standard when it comes to face embeds. It's what they all use so I stick to that. There are a lot of options of course but I tend to use what is supposed to be the standard.

abcnorio commented 3 weeks ago

Thanks for the explanation, yes very understandable to stick to standards. When there is some time and more understanding of the subject will have a look at this part of your code to learn from it and export it to R for some comparisons.