6.2 KiB
6.2 KiB
Caffe 模型转换示例
本文档以 lenet_caffe 为例,介绍如何使用 Netrans 对 Caffe 模型进行转换。
Netrans 支持所有的 Caffe 模型。
安装 Netrans
创建虚拟环境。
# 下载 mamba 安装脚本
wget "https://mirrors.tuna.tsinghua.edu.cn/github-release/conda-forge/miniforge/LatestRelease//Miniforge3-$(uname)-$(uname -m).sh"
# 创建 mamba 的安装目录
mkdir -p ~/app
# 安装 mamba 到 ~/app/
bash Miniforge3-Linux-x86_64.sh -b -p ${HOME}/app/miniforge3
# 添加 mamba 的初始化脚本到环境配置文件
echo "source " ${HOME}/app/miniforge3/etc/profile.d/mamba.sh"" >> ${HOME}/.bashrc
# 重新加载 ~/.bashrc 文件,使 mamba 初始化生效
source ${HOME}/.bashrc
# 创建一个名为 netrans 的虚拟环境,并安装 Python 3.8
mamba create -n netrans python=3.10 -y
# 激活 netrans 虚拟环境
mamba activate netrans
下载 Netrans
cd ~/app
git clone https://gitlink.org.cn/nudt_dsp/netrans.git
Netrans_cli 是基于 Netrans_api 封装的命令行工具,执行 setup.sh 可安装 Netrans_cli。
cd ~/app/netrans
# 执行 setup.sh
bash setup.sh
# setup.sh 会修改系统环境变量,需要 source 重新生效
source ~/.bashrc
# 重新激活 netrans 环境
mamba activate netrans
数据准备
转换 Caffe 模型时,模型工程目录应包含以下文件:
- 以
.prototxt结尾的模型结构定义文件 - 以
.caffemodel结尾的模型权重文件 dataset.txt包含数据路径的文本文件(支持图像和 NPY 格式)
我们的示例已经完成数据准备,可以使用以下命令进入目录执行。
cd netrans/
cd examples/caffe
# 激活 netrans 环境
mamba activate netrans
此时目录如下:
lenet_caffe/
├── 0.jpg # 校准数据
├── dataset.txt # 指定数据地址的文件
├── lenet_caffe.caffemodel # caffe 模型权重
└── lenet_caffe.prototxt # caffe 模型结构
使用 Netrans_cli 命令行工具
模型导入
load lenet_caffe
该命令会在工程目录下生成包含模型信息的 .json 和 .data 数据文件。
此时 lenet_caffe 的目录结构如下:
lenet_caffe/
├── 0.jpg
├── dataset.txt
├── lenet_caffe.caffemodel
├── lenet_caffe.data
├── lenet_caffe_inputmeta.yml
├── lenet_caffe.json
├── lenet_caffe_postprocess_file.yml
└── lenet_caffe.prototxt
模型量化
为了优化模型的推理效率,加快模型的推理速度,我们使用以下命令对模型进行量化处理。量化模型需要两个参数:目录(模型)名字和量化类型。支持的量化类型包括: symi8: 对称量化算法,使用 int8 类型 asymu8: 非对称量化算法,使用 uint8 类型 symi16: 对称量化算法,使用 int16 类型
quantize lenet_caffe asymu8
此时 lenet_caffe 的目录结构如下:
lenet_caffe/
├── 0.jpg
├── dataset.txt
├── lenet_caffe_asymu8.quantize
├── lenet_caffe.caffemodel
├── lenet_caffe.data
├── lenet_caffe_inputmeta.yml
├── lenet_caffe.json
├── lenet_caffe_postprocess_file.yml
└── lenet_caffe.prototxt
前后处理加入推理计算图
将前后处理加入推理计算图,提升整体工程性能效率。
add_pre_post lenet_caffe asymu8 --preprocess --postprocess
模型导出
使用 export 将模型导出为 nbg 格式并生成应用程序工程。
export lenet_caffe asymu8
此时 lenet_caffe 的目录结构如下:
lenet_caffe/
├── 0.jpg
├── dataset.txt
├── lenet_caffe_asymu8.quantize
├── lenet_caffe.caffemodel
├── lenet_caffe.data
├── lenet_caffe_inputmeta.yml
├── lenet_caffe.json
├── lenet_caffe_postprocess_file.yml
├── lenet_caffe.prototxt
└── wksp
├── lenet_caffe_asymu8
│ ├── analysis.json
│ ├── BUILD
│ ├── dump_core_graph.json
│ ├── graph.json
│ ├── lenetcaffeasymu8.2012.vcxproj
│ ├── lenet_caffe_asymu8.export.data
│ ├── lenetcaffeasymu8.vcxproj
│ ├── main.c
│ ├── makefile.linux
│ ├── vnn_global.h
│ ├── vnn_lenetcaffeasymu8.c
│ ├── vnn_lenetcaffeasymu8.h
│ ├── vnn_lenetcaffeasymu8_tensor.c
│ ├── vnn_post_process.c
│ ├── vnn_post_process.h
│ ├── vnn_pre_process.c
│ └── vnn_pre_process.h
└── lenet_caffe_asymu8_nbg_unify
├── BUILD
├── cmd.sh
├── lenetcaffeasymu8.2012.vcxproj
├── lenetcaffeasymu8.vcxproj
├── main.c
├── makefile.linux
├── nbg_meta.json
├── network_binary.nb
├── vnn_global.h
├── vnn_lenetcaffeasymu8.c
├── vnn_lenetcaffeasymu8.h
├── vnn_lenetcaffeasymu8_tensor.c
├── vnn_post_process.c
├── vnn_post_process.h
├── vnn_pre_process.c
└── vnn_pre_process.h
使用 Netrans_py Python API
示例代码
# example.py
from netrans import Netrans
def main(model_path: str, quantize_type: str):
# 初始化 Netrans
net = Netrans()
# 导入模型并配置预处理参数
net.load(model_path, mean=[128, 128, 128], scale=[1, 1, 1])
# 模型量化
net.quantize(quantize_type)
# 配置前后处理加入推理计算图
net.add_pre_post(quantize_type, pre=True, post=True)
# 模型导出
net.export(quantize_type)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Netrans Model Conversion")
parser.add_argument("model_path", type=str, help="Path to the model directory")
parser.add_argument("-q", "--quantize", type=str, default="asymu8", help="Quantization type (default: asymu8)")
args = parser.parse_args()
main(args.model_path, args.quantize)
运行示例
python example.py lenet_caffe -q asymu8
作者:{{xujiao}}