onnx2ncnn并在pc端调用ncnn模型

本文档介绍了如何将onnx模型转换为ncnn模型,并在PC端进行调用验证。首先,编译安装ncnn,接着转换onnx模型,详细解析了ncnn模型的结构,特别是LeNet的例子。在模型调用部分,提供了CMakeLists.txt的编写示例和main.cpp代码,强调了归一化处理和数据排列的不同。
摘要由CSDN通过智能技术生成

onnx2ncnn并在pc端调用ncnn模型

1、编译安装ncnn

首先应该在电脑上编译安装ncnn,配置安装过程可以参考笔者的这篇博文Linux下编译安装NCNN

2、确保已经转换好onnx模型

pytorch转onnx模型可以参考笔者的这篇 pytorch2onnx

3、onnx2ncnn

在经过步骤1的编译后,在目录ncnn/build/tools/onnx下,有onnx2ncnn这个可执行文件,这个文件是将模型onnx转化为ncnn模型。在ncnn/build/tools/caffe文件夹下有caffe2ncnn这个可执行文件,是将caffe模型转为ncnn,在ncnn/build/tools下有一个ncnn2mem 这个文件用来将ncnn模型进行加密的。onnx转化为ncnn模型时,我们需要使用的是onnx2ncnn这个可执行文件。

例如在笔者的这个模型中,使用

./onnx2ncnn lw50.onnx lw50.param lw50.bin

4、ncnn模型说明

ncnn模型有两个文件一个.param文件,一个是.bin文件,前者是神经网络的结构,后者是神经网络的参数。下面以一个简单的神经网络结构来说明.param文件的意义。

4.1 LeNet为例

caffe模型文件内容为,关于caffe模型文件的解析可以参考笔者的这篇博文 tensorflow2caffe

name: "LeNet"
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 20
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值