Ask Your Question

dnn example question.

asked 2018-07-03 05:39:26 -0500

holger gravatar image

updated 2018-07-03 05:44:44 -0500


I want to port that application here to a server app. I managed to read the model and get the embeddings for the pictures, so far so good.

What i dont get it is the following line:

var personVec = persons[name];
var score =;

For me the dot product is always 1 - even when the embeddings have different values. I guess my math is not good enough here to understand.

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/dnn.hpp"
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
using namespace dnn;

Mat eval( Net net, Mat face) {
    Mat blob = blobFromImage(face, 1.0 / 255, Size(96, 96), Scalar(0,0,0,0), true, false);
    Mat result = net.forward();
    return result;

int main( int argc, char** argv)
    Net net = readNetFromTorch("openface.nn4.small2.v1.t7");
    cout << "read model" << endl;

    Mat face1 = imread("face1.jpeg");   
    Mat face1Vec = eval(net, face1);
    cout << "Face1Vec" << face1Vec << endl;

    Mat face2 = imread("face2.jpeg");   
    Mat face2Vec = eval(net, face2);
    cout << "Face2Vec" << face2Vec << endl;

        auto s1 =;
        auto s2 =;

        cout << "s1: " << s1 << endl;    
        cout << "s2: " << s2 << endl; 
    return 0;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-07-03 06:09:25 -0500

berak gravatar image

updated 2018-07-03 06:10:09 -0500

there is a (c++ specific) trap here:

    Mat result = net.forward();

result is a shallow copy of the last dnn output layer, so if you call your eval() function twice, both results will point to the very same data ! (the last evaluation)

the remedy is to clone() the result, like:

    Mat result = net.forward().clone(); // "deep" copy !

(and yea, i've beeen bitten by that before, too. painful learning curve, there ..)

edit flag offensive delete link more


Ahh ok - this "problem" is related to pointer stuff? Anyway - your solution is working!!! I was also seeing the clone() call in javascript but i was tended to ignore it, Thank you very very much(again)!

holger gravatar imageholger ( 2018-07-03 06:51:06 -0500 )edit

@berak Do you develop the open cv code base or are you an official committer? Just curious.

holger gravatar imageholger ( 2018-07-03 06:54:27 -0500 )edit

no, i'm only an unofficial nitpicker (and occasionally go & fix bugs)

(the main dev team there in nidzny nowgogrod is fairly small, maybe a handful of ppl)

berak gravatar imageberak ( 2018-07-03 07:18:19 -0500 )edit

Ahh - so open cv is developed in Russia. I come from the chess area - the eastern European / Russian guys were always tough nuts XD. Anyway - for me you are like a core developer for open cv :-)

holger gravatar imageholger ( 2018-07-03 07:22:02 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-07-03 05:39:26 -0500

Seen: 172 times

Last updated: Jul 03 '18