netrans/examples/measure
gwg_xujiao da7906e2f3 docs: streamline public usage guidance 2026-07-23 11:17:21 +08:00
..
README.md docs: streamline public usage guidance 2026-07-23 11:17:21 +08:00
measure_example.py refactor: rename preprocessing scale parameter to std 2026-07-20 18:19:32 +08:00

README.md

Measure 网络计算量统计示例

统计网络的 FLOPs、MACs 等计算量指标,用于性能评估和优化分析。

前置条件

请先在源码根目录执行 make install 安装 Netrans。

本示例使用 ../caffe/lenet_caffe/ 目录下的模型文件。请先执行以下命令生成所需文件:

cd ../caffe/lenet_caffe

# 1. 加载模型(生成 .json / .data / _inputmeta.yml
netrans load . --mean 128 --std 1

# 2. 量化模型(生成 .quantize 文件)
netrans quantize . asymu8
netrans quantize . fp16

# 3. 如需统计 Hybrid 模型,先生成 Hybrid 产物
printf 'conv1\n' > cust_qnt_layers.txt
netrans quantize_hybrid . asymu8 --cust-qnt-layers cust_qnt_layers.txt

cd ../../measure

执行完毕后,../caffe/lenet_caffe/ 下应包含 .json.data_inputmeta.yml.quantize 文件。

使用方式

CLI

# float32未量化模型计算量统计
netrans measure ../caffe/lenet_caffe

# 已量化模型计算量统计
netrans measure ../caffe/lenet_caffe asymu8

# Hybrid 量化模式(需先完成上面的 quantize_hybrid
netrans measure ../caffe/lenet_caffe asymu8 --use-hybrid

# 详细输出
netrans measure ../caffe/lenet_caffe asymu8 -v

Python API

python measure_example.py

或手动调用:

from netrans import Netrans

model = Netrans()

# 加载并量化模型
model.load('../caffe/lenet_caffe', mean=128, std=1)
model.quantize('asymu8')

# 统计计算量
output_dir = model.measure('asymu8')
print(f"结果保存在: {output_dir}")

直接调用 measure 模块

python -m netrans.measure ../caffe/lenet_caffe asymu8

输出

普通模式的计算结果保存在模型目录下的 wksp/<model>_<qtype>/Hybrid 模式保存在 wksp/<model>_<qtype>_hy/。输出中包含逐层及汇总的计算量信息;文件名和字段可能随工具链版本变化,请以实际生成结果为准。

参数说明

参数 必需 默认值 说明
model_path - 模型目录路径,需包含已生成的文件
quant_type float32 量化类型,可选值见 QuantizerType
--use-hybrid False 是否使用 hybrid 量化文件
--verbose/-v False 显示详细输出

常见问题

  • 提示缺少 <model>.json.data_inputmeta.yml:先执行 netrans load
  • 提示缺少 <model>_<qtype>.quantize:先执行相同量化类型的 netrans quantize
  • Hybrid 文件不存在:确认已完成 netrans quantize_hybrid,并在 measure 时指定 --use-hybrid