基于insightface训练mobilefacenet的相关步骤及ncnn转换流程
- -经多位网友的共同实验,原方案部分情况下迭代次数稍微不足,导致最终识别率略有小差异,为了相对容易获得论文的最佳结果,对训练方案进行简单更新,实际训练也可根据数据acc训练是否已稳定来判断lr下降的迭代次数:. 适当增大softmax迭代次数,4万-->12万;. 增大arcface第一级lr0.1的迭代次数,8万-->12万;.
1、模型转换
从insightface项目中下载mxnet模型: https://github.com/deepinsight/insightface/tree/master/gender-age/model
2、使用ncnn的模型转换工具mxnet2ncnn进行模型转换
./mxnet2ncnn model-symbol.json model-0000.params ag.param ag.bin
3、使用下面代码测试:
#include "ncnn/net.h"
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <vector>
int main(int argc, char* argv[]) {
cv::Mat img_src = cv::imread("test.png");
if (img_src.empty()) {
std::cout << "input image is empty." << std::endl;
return -1;
}
ncnn::Net net;
if (net.load_param("ag.param") == -1 ||
net.load_model("ag.bin") == -1) {
std::cout << "load ga model failed." << std::endl;
return -1;
}
ncnn::Extractor ex = net.create_extractor();
ncnn::Mat img_ncnn = ncnn::Mat::from_pixels_resize(img_src.data,
ncnn::Mat::PIXEL_BGR, img_src.cols, img_src.rows, 112, 112);
ex.input("data", img_ncnn);
ncnn::Mat img_out;
ex.extract("fc1", img_out);
std::vector<float> out;
for (int i = 0; i < img_out.w; ++i) {
out.push_back(img_out[i]);
}
if (out[0] > out[1]) {
std::cout << "female" << std::endl;
} else {
std::cout << "male" << std::endl;
}
int counts = 0;
for (int i = 2; i < 102; ++i) {
if (out[2 * i] < out[2 * i + 1]) {
++counts;
}
}
std::cout << "age: " << counts << std::endl;
return 0;
}
参考资料: