You're reading the documentation for a development version.
For the latest stable release version, please have a look at master.

Netrans 简介

Netrans 是 PNNA NPU 配套的AI编译器,提供命令行工具 Netrans_cli 和 python API, 其功能是将模型权重转换成在 PNNA NPU 上运行的 NBG(network binary graph)格式文件(.nb 后缀)。 NBG 文件用于后续模型部署和推理工程的交叉编译。

工程结构

Netrans 目录结构如下:

1netrans/ 
2├── bin  # binary file
3├── docs  # 文档,包括用户指南和命令行工具的详细说明
4├── examples  # 示例代码,展示不同框架如何使用netrans进行模型转换
5├── README.md  # 说明文档,通常包含项目概述、安装指南等
6├── script  # 命令行工具
7├── setup.sh  # 用于设置环境或安装依赖的Shell脚本
8└── test  # 测试代码

安装指南

系统依赖

  • CPU : Intel® Core™ i5-6500 CPU @ 3.2 GHz x4 支持 the Intel® Advanced Vector Extensions.

  • RAM : 至少8GB

  • 硬盘 : 160GB

  • 操作系统 : Ubuntu 20.04 LTS 64-bit with Python 3.10,不推荐使用其他版本

安装步骤

  • 安装依赖

 1sudo apt update
 2sudo apt install build-essential
 3
 4# 安装 mamba ,本项目使用 mamba 创建虚拟环境,演示安装过程
 5
 6# 下载 mamba 安装脚本
 7 wget "https://mirrors.tuna.tsinghua.edu.cn/github-release/conda-forge/miniforge/LatestRelease//Miniforge3-$(uname)-$(uname -m).sh"
 8 # 创建 mamba 的安装目录
 9 mkdir -p ~/app 
10 # 安装 mamba 到 ~/app/ 
11 bash Miniforge3-Linux-x86_64.sh -b -p ${HOME}/app/miniforge3
12 # 添加 mamba 的初始化脚本到环境配置文件
13 echo "source " ${HOME}/app/miniforge3/etc/profile.d/mamba.sh"" >> ${HOME}/.bashrc
14 # 重新加载 ~/.bashrc 文件,使 mamba 初始化生效
15 source ${HOME}/.bashrc
16 # 创建一个名为 netrans 的虚拟环境,并安装 Python 3.10
17 mamba create -n netrans python=3.10 -y
18 # 激活 netrans 虚拟环境
19 mamba activate netrans
  • 下载 Netrans

1# 下载 Netrans 到 ~/app
2cd ~/app
3git clone --depth 1 --branch v251204 https://gitlink.org.cn/nudt_dsp/netrans.git netrans
  • 运行配置脚本

1cd ~/app/netrans
2bash setup.sh

Netrans 使用说明

Netrans 提供 Tensorflow、Caffe、Darknet、ONNX 和 Pytorch 的模型转换示例,请参考目录 ~/app/netrans/examples

命令行工具

Netrans 提供了简单的命令行接口,用于编译和优化模型。

 1# 以 转成 ONNX 格式的 YOLOv8s 模型为例,演示使用 Netrans 命令行工具完成转换的全过程。
 2# 1. 定义模型路径,模型路径默认为工作路径。
 3work_path='~/app/netrans/examples/infer_with_pre_post_process/yolov8s'
 4cd ${work_path}
 5# 2. 激活环境
 6mamba activate netrans
 7# 3. 模型导入
 8netrans load ./ --mean 0 0 0 --scale 255 255 255
 9# 4. 模型量化
10netrans quantize ./ asymu8 
11# 5. 将前后处理加入推理网络
12netrans add_pre_post ./ asymu8
13# 6. 导出 nbg 文件
14netrans export ./ asymu8 

详细说明请参考netrans 命令行使用说明

Python接口

通过Netrans Python接口,可以方便地在Python脚本中调用编译器。 示例代码:

 1from netrans import Netrans
 2
 3# 定义模型路径,模型路径默认为工作路径。
 4model_path='~/app/netrans/examples/infer_with_pre_post_process/yolov8s'
 5import sys
 6model_path=sys.argv[1]
 7
 8# 初始化netrans
 9netrans = Netrans()
10# 模型载入,同时配置 mean 和 scale  
11netrans.load(model_path,  mean=[0,0,0] ,scale=[255,255,255] )
12# 模型量化
13netrans.quantize("asymu8", pre = False, post= False)
14# 前后处理添加进推理
15netrans.add_pre_post("asymu8")
16# 模型导出
17netrans.export("asymu8")

详细说明请参考netrans api 使用说明

模型支持

Netrans 支持主流框架见下表。

输入支持

描述

caffe

支持所有的Caffe 模型

Tensorflow

支持版本1.4.x, 2.0.x, 2.3.x, 2.6.x, 2.8.x, 2.10.x, 2.12.x 以tf.io.write_graph()保存的模型

ONNX

支持 ONNX 至 1.14.0, opset支持至19

Pytorch

支持 Pytorch 至 1.5.1

Darknet

支持官网列出 darknet 模型

注意: Pytorch 动态图的特性,建议将 Pytorch 模型导出成 ONNX ,再使用 Netrans 进行转换。

算子支持

支持的Caffe算子

absval

innerproduct

reorg

axpy

lrn

roipooling

batchnorm/bn

l2normalizescale

relu

convolution

leakyrelu

reshape

concat

lstm

reverse

convolutiondepthwise

normalize

swish

dropout

poolwithargmax

slice

depthwiseconvolution

premute

scale

deconvolution

prelu

shufflechannel

elu

pooling

softmax

eltwise

priorbox

sigmoid

flatten

proposal

tanh

支持的TensorFlow算子

tf.abs

tf.nn.rnn_cell_GRUCell

tf.negative

tf.add

tf.nn.dynamic_rnn

tf.pad

tf.nn.bias_add

tf.nn.rnn_cell_GRUCell

tf.transpose

tf.add_n

tf.greater

tf.nn.avg_pool

tf.argmin

tf.greater_equal

tf.nn.max_pool

tf.argmax

tf.image.resize_bilinear

tf.reduce_mean

tf.batch_to_space_nd

tf.image.resize_nearest_neighbor

tf.nn.max_pool_with_argmax

tf.nn.batch_normalization

tf.contrib.layers.instance_norm

tf.pow

tf.nn.fused_batchnorm

tf.nn.fused_batch_norm

tf.reduce_mean

tf.cast

tf.stack

tf.reduce_sum

tf.clip_by_value

tf.nn.sigmoid

tf.reverse

tf.concat

tf.signal.frame

tf.reverse_sequence

tf.nn.conv1d

tf.slice

tf.nn.relu

tf.nn.conv2d

tf.nn.softmax

tf.nn.relu6

tf.nn.depthwise_conv2d

tf.space_to_batch_nd

tf.rsqrt

tf.nn.conv1d

tf.space_to_depth

tf.realdiv

tf.nn.conv3d

tf.nn.local_response_normalization

tf.reshape

tf.image.crop_and_resize

tf.nn.l2_normalize

tf.expand_dims

tf.nn.conv2d_transposed

tf.nn.rnn_cell_LSTMCelltf.nn_dynamic_rnn

tf.squeeze

tf.depth_to_space

tf.rnn_cell.LSTMCell

tf.strided_slice

tf.equal

tf.less

tf.sqrt

tf.exp

tf.less_equal

tf.square

tf.nn.elu

tf.logical_or

tf.subtract

tf.nn.embedding_lookup

tf.logical_add

tf.scatter_nd

tf.maximum

tf.nn.leaky_relu

tf.split

tf.floor

tf.multiply

tf.nn.swish

tf.matmul

tf.nn.moments

tf.tile

tf.floordiv

tf.minimum

tf.nn.tanh

tf.gather_nd

tf.matmul

tf.unstack

tf.gather

tf.batch_matmul

tf.where

tf.nn.embedding_lookup

tf.not_equal

tf.select

支持的ONNX算子

ArgMin

LeakyRelu

ReverseSequence

ArgMax

Less

ReduceMax

Add

LSTM

ReduceMin

Abs

MatMul

ReduceL1

And

Max

ReduceL2

BatchNormalization

Min

ReduceLogSum

Clip

MaxPool

ReduceLogSumExp

Cast

AveragePool

ReduceSumSquare

Concat

Globa

Reciprocal

ConvTranspose

lAveragePool

Resize

Conv

GlobalMaxPool

Sum

Div

MaxPool

SpaceToDepth

Dropout

AveragePool

Sqrt

DepthToSpace

Mul

Split

DequantizeLinear

Neg

Slice

Equal

Or

Squeeze

Exp

Prelu

Softmax

Elu

Pad

Sub

Expand

POW

Sigmoid

Floor

QuantizeLinear

Softsign

InstanceNormalization

QLinearMatMul

Softplus

Gemm

QLinearConv

Sin

Gather

Relu

Tile

Greater

Reshape

Transpose

GatherND

Squeeze

Tanh

GRU

Unsqueeze

Upsample

Logsoftmax

Flatten

Where

LRN

ReduceSum

Xor

Log

ReduceMean

支持的Darknet算子

avgpool

maxpool

softmax

batch_normalize

mish

shortcut

connected

region

scale_channels

convolutional

reorg

swish

depthwise_convolutional

relu

upsample

leaky

route

yolo

logistic


作者 @xj xujiao@hngwg.com