Skip to content

Instantly share code, notes, and snippets.

@cyrusbehr
Last active September 16, 2019 17:02
Show Gist options
  • Save cyrusbehr/ebdaef33aaa12689066296ff9a4320e8 to your computer and use it in GitHub Desktop.
Save cyrusbehr/ebdaef33aaa12689066296ff9a4320e8 to your computer and use it in GitHub Desktop.
Performing inference with NCNN
#include <iostream>
#include "ncnn/net.h"
#include "opencv2/opencv.hpp"
void normalize(std::vector<float>& arr) {
double mod = 0.0;
for (float i : arr) {
mod += i * i;
}
double mag = std::sqrt(mod);
if (mag == 0) {
throw std::logic_error("The input vector is a zero vector");
}
for (float & i : arr) {
i /= mag;
}
}
double getSimilarity(const std::vector<float>& v1, const std::vector<float>& v2) {
if (v1.size() != v2.size())
throw std::invalid_argument("Wrong size");
double mul = 0;
for (size_t i = 0; i < v1.size(); ++i) {
mul += v1[i] * v2[i];
}
if (mul < 0) {
return 0;
}
return mul;
}
std::vector<float> getTemplate(const std::string& imagePath, ncnn::Net& net) {
cv::Mat data = cv::imread(imagePath);
ncnn::Mat in = ncnn::Mat::from_pixels(data.data, ncnn::Mat::PIXEL_RGB, data.cols, data.rows);
ncnn::Extractor ex = net.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("fc1", out);
std::vector<float> vec;
for (int i = 0; i < out.w; ++i) {
vec.emplace_back(*(static_cast<float*>(out.data) + i));
}
normalize(vec);
return vec;
}
int main() {
ncnn::Net net;
net.load_param("../models/ncnn.param");
net.load_model("../models/ncnn.bin");
// Two images of the same person
const std::string person1imgAPath = "../images/chip1.jpg";
const std::string person1imgBPath = "../images/chip2.jpg";
// One image of a different person
const std::string person2imgPath = "../images/chip17.jpg";
const auto person1ATempl = getTemplate(person1imgAPath, net);
const auto person1BTempl = getTemplate(person1imgBPath, net);
const auto person2Templ = getTemplate(person2imgPath, net);
// Get score for two images of the same person
const auto scoreSimilar = getSimilarity(person1ATempl, person1BTempl);
std::cout << "Similarity score: " << scoreSimilar << "\n";
// Get score for two images of different people
const auto scoreDissimilar = getSimilarity(person1ATempl, person2Templ);
std::cout << "Similarity score: " << scoreDissimilar << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment