给ncnn的网络添加layer分为两种情况:
1.模型转换之后,某些layer没有转换成功:
如,shufflenetV2中,param文件只转换到了fc这一个layer:
这时需要添加一层softmax,可以先对param文件中转换成功的layer做推理,然后手动添加一个softmax层:
ncnn::Extractor ex = shufflenetv2.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("fc", out);
// manually call softmax on the fc output
// convert result into probability
// skip if your model already has softmax operation
{
ncnn::Layer* softmax = ncnn::create_layer("Softmax");
ncnn::ParamDict pd;
softmax->load_param(pd);
softmax->forward_inplace(out, shufflenetv2.opt);
delete softmax;
}
out = out.reshape(out.w * out.h * out.c);
2.某些layer,ncnn不支持,需要进行自定义实现:
具体可以去看一下ncnn的wiki:
https://github.com/Tencent/ncnn/wiki/how-to-implement-custom-layer-step-by-step
参考资料:
[1] https://github.com/Tencent/ncnn
[2] https://github.com/Tencent/ncnn/wiki/how-to-implement-custom-layer-step-by-step