Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么mobilefacenet提取的任意图片的特征向量都是一样的,下面是我的主程序 #5

Open
songyy137222 opened this issue Jun 19, 2018 · 6 comments

Comments

@songyy137222
Copy link

#include <stdio.h>
#include
#include
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include
//using namespace std;
using namespace cv;
#include "net.h"

static int detect_squeezenet(const cv::Mat& bgr, std::vector& cls_scores)
{
ncnn::Net squeezenet;
squeezenet.load_param("/home1/syy/DownLoad/ncnn-mtcnn/ncnn-master/mobilefacenet/mobilefacenet.param");
squeezenet.load_model("/home1/syy/DownLoad/ncnn-mtcnn/ncnn-master/mobilefacenet/mobilefacenet.bin");

ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 112, 112);  

const float mean_vals[3] = {127.5, 127.5, 127.5};
const float norm_vals[3] = {0.0078125, 0.0078125, 0.0078125};
in.substract_mean_normalize(mean_vals, norm_vals);  

ncnn::Extractor ex = squeezenet.create_extractor();  
ex.set_light_mode(true);  

ex.input("data", in);  

ncnn::Mat out;  
ex.extract("fc1", out);  

cls_scores.resize(out.c);  
for (int j=0; j<out.c; j++)  
{  
    const float* prob = out.data + out.cstep * j;  
    cls_scores[j] = prob[0]; 
    std::cout<< prob[0]<<std::endl;
}  

return 0;  

}
double calculSimilar(std::vector& v1, std::vector& v2)
{
assert(v1.size() == v2.size());
double ret = 0.0, mod1 = 0.0, mod2 = 0.0;
for (std::vector::size_type i = 0; i != v1.size(); ++i)
{
ret += v1[i] * v2[i];
mod1 += v1[i] * v1[i];
mod2 += v2[i] * v2[i];
}
return (ret / sqrt(mod1) / sqrt(mod2) + 1) / 2.0;
//std::cout<<ret<<" "<<mod1<<" "<<mod2<<std::endl;
//return ret / sqrt(mod1) / sqrt(mod2);
}
int main(int argc, char** argv)
{
cv::Mat img1 = cv::imread("/home1/syy/DownLoad/ncnn-mtcnn/ncnn-master/mobilefacenet/1.jpg", CV_LOAD_IMAGE_COLOR);

cv::Mat img2 = cv::imread("/home1/syy/DownLoad/ncnn-mtcnn/ncnn-master/mobilefacenet/2.jpg", CV_LOAD_IMAGE_COLOR);
std::vector<float> feature1;
std::vector<float> feature2;
    detect_squeezenet(img1, feature1) ;
    std::cout<<"$$$$$$$$$$$$$$$$$$$$$"<<std::endl;
    detect_squeezenet(img1, feature2) ;
    double similar = calculSimilar(feature1, feature2);
   // printf(similar);
    std::cout << "similarity is : " << similar <<std::endl;

    return 0;  

}

@MaeThird
Copy link

你读读ncnn的源码net.cpp就知道extractor是不能重用的,它直接返回上次的结果。

@af258963
Copy link

mobilefacenet.param里面已经做了substract_mean_normalize,应该不用再归一化一次吧

@zhangxiaopang88
Copy link

您好,很感谢你能提供这么方便的工具,代码中我只改了路径,转换后测试结果全为0.9999,请问您知道这是什么原因吗

@honghuCode
Copy link
Owner

@af258963 不用归一化了,thank

@honghuCode
Copy link
Owner

mobilefacenet.param里面已经做了substract_mean_normalize,应该不用再归一化一次吧

您好,很感谢你能提供这么方便的工具,代码中我只改了路径,转换后测试结果全为0.9999,请问您知道这是什么原因吗

改了哪一部分的路径,代码可以贴出来 review一下

@LanWong1
Copy link

image
image
为啥我这里fc1 的输出是错的?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants