最新从 https://github.com/Tencent/ncnn下载下来的安装包。
protobuf 版本最好为3.6.1,用系统默认的2.6.1编译报错。
编译过之后,让我们来跑一下benchmark打印每一层的时间.
先看代码:
Net::forward_layer
#if NCNN_BENCHMARK
double start = get_current_time();
int ret = layer->forward_inplace(bottom_top_blob, opt);
double end = get_current_time();
benchmark(layer, bottom_top_blob, bottom_top_blob, start, end);
#else
int ret = layer->forward_inplace(bottom_top_blob, opt);
#endif // NCNN_BENCHMARK
已经定义好了,每一层的时间,并且已经在benchmark中进行输出,那我们来看一下benchmark.
void benchmark(const Layer* layer, double start, double end)
{
fprintf(stderr, "%-24s %-24s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start);
fprintf(stderr, " |");
fprintf(stderr, "\n");
}
void benchmark(const Layer* layer, const Mat& bottom_blob, Mat& top_blob, double start, double end)
{
fprintf(stderr, "%-24s %-24s %8.2lfms", layer->type.c_str(), layer->name.c_str(), end - start);
fprintf(stderr, " | feature_map: %4d x %-4d inch: %4d outch: %4d", bottom_blob.w, bottom_blob.h, bottom_blob.c, top_blob.c);
if (layer->type == "Convolution")
{
fprintf(stderr, " kernel: %1d x %1d stride: %1d x %1d",
((Convolution*)layer)->kernel_w,
((Convolution*)layer)->kernel_h,
((Convolution*)layer)->stride_w,
((Convolution*)layer)->stride_h
);
}
fprintf(stderr, "\n");
}
重载了两个函数,一个只有时间,另一个带有bottom_blob与top_blob层.也就是说,只要把编译的benchmark打开,就能打印的.
2.修改编译选项
option(NCNN_BENCHMARK "print benchmark information for every layer" ON) #将OFF改为ON
...
add_subdirectory(benchmark) #放开
在ncnn/build目录下,cmake … && make -j4就能看到benchncnn了,然后./benchncnn 运行就好,若是提示加载失败,那就把param文件拷贝到同目录中.
结果如下:
有详细的,各层的时间已经当前层的情况...