ncnn 官方文档很少,自己摸索写了例子
#include
#include
//模型加载
ncnn::Net net;
net.load_param("D:/github/ncnn-win10/ncnn-20190611/examples/squeezenet_v1.1.param");
net.load_model("D:/github/ncnn-win10/ncnn-20190611/examples/squeezenet_v1.1.bin");
//数据处理,from_pixels_resize:将cv::Mat数据转换到ncnn::Mat同时进行resize操作
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
const float mean_vals[3] = { 104.f, 117.f, 123.f };
in.substract_mean_normalize(mean_vals, 0); //减均值除方差
//模型推理
ncnn::Extractor ex = net.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("prob", out);
cls_scores.resize(out.w);
for (int j = 0; j
cls_scores[j] = out[j];
}
return 0;
}
static int print_topk(const std::vector
{
// partial sort topk with index
int size = cls_scores.size();
std::vector< std::pair
vec.resize(size);
for (int i = 0; i
vec[i] = std::make_pair(cls_scores[i], i);
}
std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
std::greater< std::pair
// print topk and score
for (int i = 0; i
float score = vec[i].first;
int index = vec[i].second;
fprintf(stderr, "%d = %f\n", index, score);
}
return 0;
}
void pretty_print(const ncnn::Mat& m)
{
//cv::Mat imageDate(m.h, m.w, CV_32FC1);
//for (int c = 0; c < 3; c++) {
// for (int i = 0; i < m.h; i++)
// {
// for (int j = 0; j < m.w; j++)
// {
// float t = ((float*)m.data)[j + i * m.w + c * m.h * m.w];
// cout << t;
// //imageDate.data[(2 - c) + j * 3 + i * m.w * 3] = t;
// }
// cout << endl;
// }
// cout << endl << endl;
//}
for (int q = 0; q
const float* ptr = m.channel(q);
for (int y = 0; y
for (int x = 0; x
printf("%f ", ptr[x]);
}
ptr += m.w;
printf("\n");
}
printf("------------------------\n");
}
}
int main()
{
fstream file1, file2;//创建文件流对象
file1.open("D:/ncnn/ncnn_test/80x1x1.txt");
int w = 1;
int h = 80;
#define CHANNAL 1
Mat input = Mat::zeros(h, w, CV_32FC(CHANNAL));//创建Mat类矩阵,定义初始化值全部是0,矩阵大小和txt一致
//将txt文件数据写入到Data矩阵中
typedef cv::Vec
char ch;
for (int i = 0; i < CHANNAL; i++)
{
for (int j = 0; j < h; j++)
{
for (int k = 0; k < w; k++)
{
//file1 >> Ky1_Data.at
file1 >> input.at
//cout << Ky1_Data.at
}
//file1 >> ch;
}
}
//模型加载
ncnn::Net net;
//net.load_param("D:/ncnn/ncnn_test/bias/1D_bias_001_80x1x1.param");
//net.load_param("D:/ncnn/ncnn_test/MUL_EItwise/1D_MUL_EItwise_001_80x1x1.param");
net.load_param("D:/ncnn/ncnn_test/Scale/scale.param");
net.load_model("D:/ncnn/ncnn_test/Scale/scale.bin");
//net.load_param("D:/ncnn/ncnn_test/bias/1D_bias_001_80x1x1.param");
//net.load_model("D:/github/ncnn-win10/ncnn-20190611/examples/squeezenet_v1.1.bin");
ncnn::Mat in(w, h, CHANNAL);
memcpy(in.data, input.data, input.cols*input.rows*input.channels()*sizeof(float));
//ncnn::Mat in = ncnn::Mat::from_pixels(input.data, ncnn::Mat::PIXEL_GRAY, input.cols, input.rows);
//ncnn::Mat in = ncnn::Mat::from_float16((unsigned short *)input.data, input.cols*input.rows);
pretty_print(in);
//const float mean_vals[3] = { 104.f, 117.f, 123.f };
//in.substract_mean_normalize(mean_vals, 0); //减均值除方差
//模型推理
ncnn::Extractor ex = net.create_extractor();
ex.input("data", in);
ncnn::Mat out0;
ex.extract("out", out0);
pretty_print(out0);
std::cout << "finshed!";
//std::string imagepath = "./test.jpg";
//cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR);
//if (m.empty())
//{
// std::cout << "cv::imread " << imagepath << " failed\n" << std::endl;
// return -1;
//}
//std::vector
//detect_squeezenet(m, cls_scores);
//print_topk(cls_scores, 3);
return 0;
}