Beispiel #1
0
        public double CompareSimilarity(FaceEmbeddings emb1, FaceEmbeddings emb2)
        {
            var mat1 = emb1.CreateMatFromEmbeddings(emb1.Embeddings);
            var mat2 = emb2.CreateMatFromEmbeddings(emb2.Embeddings);

            // l2 normalize the vectors
            for (int i = 0; i < 128; i++)
            {
                mat1.put(0, i, mat1.get(0, i)[0] / Core.norm(mat1, 4));
            }

            for (int i = 0; i < 128; i++)
            {
                mat2.put(0, i, mat2.get(0, i)[0] / Core.norm(mat2, 4));
            }

            if (mat1 == null || mat2 == null)
            {
                return(1000.0);
            }

            var ab      = mat1.dot(mat2);
            var aMagni  = Core.norm(mat1);
            var bMagni  = Core.norm(mat2);
            var cos_sim = ab / (aMagni * bMagni);

            mat1.Dispose(); mat2.Dispose();

            return(1 - cos_sim);
        }
Beispiel #2
0
        /// <summary>
        /// Calculate the euclidean distance between 2 FaceEmbeddings objects.
        /// </summary>
        /// <param name="emb1"></param>
        /// <param name="emb2"></param>
        /// <returns> double </returns>
        public double CompareEmbeddings(FaceEmbeddings emb1, FaceEmbeddings emb2)
        {
            var mat1 = emb1.CreateMatFromEmbeddings(emb1.Embeddings);
            var mat2 = emb2.CreateMatFromEmbeddings(emb2.Embeddings);

            if (mat1 == null || mat2 == null)
            {
                return(1000.0);
            }

            var diff   = mat1 - mat2;
            var result = diff.dot(diff);

            result = Mathf.Sqrt((float)result);
            return(result);
        }