liveBook · Manning

Cosine similarity

As explained in Fight Fraud with Machine Learning

Cosine similarity is a metric that quantifies the directional similarity between two vectors as the cosine of the angle between them.

Cosine similarity compares two vectors by the angle between them. Vectors pointing in nearly the same direction receive a high score; scores near 0 indicate unrelated directions, while scores near -1 indicate opposite directions. In face matching, the vectors are facial embeddings rather than raw pixels, so the score estimates whether two detected faces represent the same person. section 7.2.1

A practical face-matching flow is: detect and crop a face from each image, generate an embedding for each crop, then calculate cosine similarity between the embeddings. High scores support a match and low scores support a mismatch, but a threshold turns the continuous score into an operational duplicate or identity decision. section 7.2.1 section 7.2.3

Cosine similarity can also participate directly in training. A Siamese network runs two images through replicas of the same CNN, compares the resulting embeddings, passes the similarity through a sigmoid, and measures error against a same-person or different-person label using binary cross-entropy. Backpropagation then encourages same-person embeddings to become similar and different-person embeddings to become separable. section 7.2.2

Duplicate and ID checks

Two photos of Albert Einstein produced 76.52% similarity, while Einstein versus John McCarthy produced 9.92%; a 50% threshold was suggested for that duplicate-check example. Comparing an Einstein selfie with an ID photo produced 61.54%, whereas a different person's ID produced 2.52%. A 100% score may be suspicious when the identical photo was uploaded twice or pasted onto a fraudulent ID. section 7.2.3 section 7.2.4

Figure 7.17 Demonstration of duplicate check implemented using face-matching which is broken down into three stages – face detection, face recognition, and cosine similarity. For the 4 example pairs, the first two pairs are of the same person (Einstein), therefore a high similarity score. The third pair contains different people (Einstein and McCarthy) leading to a low score. Last pair has the exact same photos, producing an abnormally high score of 100%.

Inspect the end-to-end duplicate-check pipeline and compare high, low, and abnormally perfect similarity scores across face pairs. section 7.2.3

How Fight Fraud with Machine Learning covers it

How deep it goes
Defines directional vector similarity and works through inference, thresholding, and Siamese-network training with sigmoid and binary cross-entropy.
Examples
Demonstrates Einstein selfie pairs, an Einstein–McCarthy mismatch, ID comparisons, and duplicated or pasted-photo fraud cases.
What it assumes
Introduces cosine similarity through face embeddings while using pre-trained face-detection and face-recognition models.
What you can do after
Apply it via PyTorch to compare MTCNN-cropped, InceptionResnetV1-generated face embeddings for duplicate and selfie-ID checks.
How it connects
Connects face detection, face recognition, embeddings, Siamese networks, sigmoid functions, binary cross-entropy, and backpropagation.

Also explained in

Compare how 25 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fight Fraud with Machine Learning. The section links point to the parts of the book it draws on.