Ask Your Question
0

dnn example question.

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

holger gravatar image

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

Hello,

I want to port that application here https://docs.opencv.org/master/d5/d86... 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 = vec.dot(personVec);

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);
    net.setInput(blob);
    Mat result = net.forward();
    return result;
}


int main( int argc, char** argv)
{   
    Net net = readNetFromTorch("openface.nn4.small2.v1.t7");
    net.setPreferableTarget(DNN_TARGET_OPENCL);
    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 = face1Vec.dot(face1Vec);
        auto s2 = face1Vec.dot(face2Vec);

        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
0

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

berak gravatar image

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

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

Comments

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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 605 times

Last updated: Jul 03 '18