373 lines
12 KiB
Python
373 lines
12 KiB
Python
"""
|
|
ONNX模型验证脚本
|
|
|
|
功能:
|
|
1. 验证ONNX模型的正确性
|
|
2. 检查模型结构和算子
|
|
3. 对比PyTorch和ONNX的输出
|
|
4. 测试ONNX模型的推理性能
|
|
"""
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import numpy as np
|
|
import onnx
|
|
import onnxruntime as ort
|
|
from PIL import Image
|
|
|
|
|
|
def configure_stdio():
|
|
"""Avoid console encoding failures when run from Windows terminals."""
|
|
for stream in (sys.stdout, sys.stderr):
|
|
if hasattr(stream, "reconfigure"):
|
|
stream.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
|
|
configure_stdio()
|
|
|
|
|
|
DEFAULT_TEST_IMAGE = os.path.join('data', 'processed', 'val', 'class_0', '10.jpg')
|
|
|
|
|
|
def load_training_modules():
|
|
"""按需导入PyTorch训练代码;缺依赖时允许跳过PyTorch对比。"""
|
|
try:
|
|
import torch
|
|
training_dir = os.path.join(os.path.dirname(__file__), '..', 'pc_training')
|
|
if training_dir not in sys.path:
|
|
sys.path.append(training_dir)
|
|
from model import create_model
|
|
from dataset import get_transforms
|
|
return torch, create_model, get_transforms
|
|
except Exception as e:
|
|
print(f"\n⚠ 无法加载PyTorch训练依赖,跳过PyTorch/ONNX输出对比: {e}")
|
|
return None, None, None
|
|
|
|
|
|
def load_pytorch_model(torch, create_model, model_path, num_classes=10):
|
|
"""
|
|
加载PyTorch模型
|
|
|
|
参数:
|
|
model_path: 模型权重路径
|
|
num_classes: 类别数
|
|
|
|
返回:
|
|
model: PyTorch模型
|
|
"""
|
|
print("加载PyTorch模型...")
|
|
model = create_model(num_classes=num_classes, pretrained=False)
|
|
checkpoint = torch.load(model_path, map_location='cpu')
|
|
model.load_state_dict(checkpoint['model_state_dict'])
|
|
model.eval()
|
|
print(f"✓ PyTorch模型加载成功")
|
|
return model
|
|
|
|
|
|
def verify_onnx_model(onnx_path):
|
|
"""
|
|
验证ONNX模型的有效性
|
|
|
|
参数:
|
|
onnx_path: ONNX模型路径
|
|
|
|
返回:
|
|
bool: 是否有效
|
|
"""
|
|
print("\n验证ONNX模型...")
|
|
|
|
try:
|
|
# 加载ONNX模型
|
|
onnx_model = onnx.load(onnx_path)
|
|
|
|
# 检查模型格式
|
|
onnx.checker.check_model(onnx_model)
|
|
print("✓ ONNX模型格式正确")
|
|
|
|
# 打印模型信息
|
|
print(f"\n模型信息:")
|
|
print(f" IR版本: {onnx_model.ir_version}")
|
|
print(f" 生产者: {onnx_model.producer_name}")
|
|
print(f" Opset版本: {onnx_model.opset_import[0].version}")
|
|
|
|
# 打印输入输出信息
|
|
graph = onnx_model.graph
|
|
print(f"\n输入:")
|
|
for input_tensor in graph.input:
|
|
shape = [dim.dim_value for dim in input_tensor.type.tensor_type.shape.dim]
|
|
print(f" 名称: {input_tensor.name}")
|
|
print(f" 形状: {shape}")
|
|
print(f" 类型: {input_tensor.type.tensor_type.elem_type}")
|
|
|
|
print(f"\n输出:")
|
|
for output_tensor in graph.output:
|
|
shape = [dim.dim_value for dim in output_tensor.type.tensor_type.shape.dim]
|
|
print(f" 名称: {output_tensor.name}")
|
|
print(f" 形状: {shape}")
|
|
print(f" 类型: {output_tensor.type.tensor_type.elem_type}")
|
|
|
|
# 统计算子
|
|
op_types = {}
|
|
for node in graph.node:
|
|
op_type = node.op_type
|
|
op_types[op_type] = op_types.get(op_type, 0) + 1
|
|
|
|
print(f"\n算子统计:")
|
|
for op_type, count in sorted(op_types.items()):
|
|
print(f" {op_type}: {count}")
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"✗ ONNX模型验证失败: {e}")
|
|
return False
|
|
|
|
|
|
def compare_outputs(torch, get_transforms, pytorch_model, onnx_path, test_image_path, num_classes=10):
|
|
"""
|
|
对比PyTorch和ONNX模型的输出
|
|
|
|
参数:
|
|
pytorch_model: PyTorch模型
|
|
onnx_path: ONNX模型路径
|
|
test_image_path: 测试图像路径
|
|
num_classes: 类别数
|
|
|
|
返回:
|
|
bool: 输出是否一致
|
|
"""
|
|
print("\n对比PyTorch和ONNX输出...")
|
|
|
|
try:
|
|
# 加载ONNX Runtime会话
|
|
ort_session = ort.InferenceSession(onnx_path)
|
|
|
|
# 准备测试输入
|
|
if test_image_path and os.path.exists(test_image_path):
|
|
# 使用真实图像
|
|
transform = get_transforms(input_size=224, augment=False)
|
|
image = Image.open(test_image_path).convert('RGB')
|
|
input_tensor = transform(image).unsqueeze(0)
|
|
print(f"使用测试图像: {test_image_path}")
|
|
else:
|
|
# 使用随机输入
|
|
input_tensor = torch.randn(1, 3, 224, 224)
|
|
print("使用随机输入")
|
|
|
|
# PyTorch推理
|
|
with torch.no_grad():
|
|
pytorch_output = pytorch_model(input_tensor)
|
|
pytorch_output = pytorch_output.numpy()
|
|
|
|
# ONNX推理
|
|
onnx_input = {ort_session.get_inputs()[0].name: input_tensor.numpy()}
|
|
onnx_output = ort_session.run(None, onnx_input)[0]
|
|
|
|
# 对比输出
|
|
max_diff = np.max(np.abs(pytorch_output - onnx_output))
|
|
mean_diff = np.mean(np.abs(pytorch_output - onnx_output))
|
|
|
|
print(f"\n输出对比:")
|
|
print(f" PyTorch输出形状: {pytorch_output.shape}")
|
|
print(f" ONNX输出形状: {onnx_output.shape}")
|
|
print(f" 最大差异: {max_diff:.6f}")
|
|
print(f" 平均差异: {mean_diff:.6f}")
|
|
|
|
# 对比预测类别
|
|
pytorch_pred = np.argmax(pytorch_output)
|
|
onnx_pred = np.argmax(onnx_output)
|
|
|
|
print(f"\n预测结果:")
|
|
print(f" PyTorch预测: 类别 {pytorch_pred}")
|
|
print(f" ONNX预测: 类别 {onnx_pred}")
|
|
|
|
# 判断是否一致
|
|
if max_diff < 1e-5:
|
|
print("✓ 输出完全一致")
|
|
return True
|
|
elif max_diff < 1e-3:
|
|
print("✓ 输出基本一致(存在微小数值差异)")
|
|
return True
|
|
else:
|
|
print("✗ 输出差异较大,可能存在问题")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"✗ 输出对比失败: {e}")
|
|
return False
|
|
|
|
|
|
def test_onnx_performance(onnx_path, num_runs=100):
|
|
"""
|
|
测试ONNX模型的推理性能
|
|
|
|
参数:
|
|
onnx_path: ONNX模型路径
|
|
num_runs: 测试次数
|
|
"""
|
|
print(f"\n测试ONNX推理性能({num_runs}次)...")
|
|
|
|
try:
|
|
import time
|
|
|
|
# 加载ONNX Runtime会话
|
|
ort_session = ort.InferenceSession(onnx_path)
|
|
|
|
# 准备输入
|
|
input_name = ort_session.get_inputs()[0].name
|
|
dummy_input = np.random.randn(1, 3, 224, 224).astype(np.float32)
|
|
|
|
# 预热
|
|
for _ in range(10):
|
|
ort_session.run(None, {input_name: dummy_input})
|
|
|
|
# 测试
|
|
start_time = time.time()
|
|
for _ in range(num_runs):
|
|
ort_session.run(None, {input_name: dummy_input})
|
|
end_time = time.time()
|
|
|
|
avg_time = (end_time - start_time) / num_runs
|
|
fps = 1.0 / avg_time
|
|
|
|
print(f"\n性能指标:")
|
|
print(f" 平均推理时间: {avg_time*1000:.2f} ms")
|
|
print(f" 推理速度: {fps:.2f} FPS")
|
|
|
|
except Exception as e:
|
|
print(f"✗ 性能测试失败: {e}")
|
|
|
|
|
|
def check_finn_compatibility(onnx_path):
|
|
"""
|
|
检查ONNX模型与FINN的兼容性
|
|
|
|
参数:
|
|
onnx_path: ONNX模型路径
|
|
"""
|
|
print("\n检查FINN兼容性...")
|
|
|
|
try:
|
|
onnx_model = onnx.load(onnx_path)
|
|
graph = onnx_model.graph
|
|
|
|
# 初步检查常见算子。最终是否可生成FPGA由FINN build_dataflow决定。
|
|
common_finn_ops = {
|
|
'Conv', 'Gemm', 'MatMul', 'Add', 'Mul', 'Relu', 'MaxPool',
|
|
'AveragePool', 'GlobalAveragePool', 'BatchNormalization',
|
|
'Flatten', 'Reshape', 'Transpose', 'Concat', 'Dropout',
|
|
'Clip', 'Div', 'Sub', 'Identity'
|
|
}
|
|
|
|
# 检查不支持的算子
|
|
unsupported_ops = set()
|
|
for node in graph.node:
|
|
if node.op_type not in common_finn_ops:
|
|
unsupported_ops.add(node.op_type)
|
|
|
|
if unsupported_ops:
|
|
print(f"⚠ 发现FINN可能不支持的算子:")
|
|
for op in unsupported_ops:
|
|
print(f" - {op}")
|
|
print("\n建议:")
|
|
print(" 1. 先用FINN/QONNX转换工具做模型清理和折叠")
|
|
print(" 2. 如仍失败,改用Brevitas/QONNX重新导出量化模型")
|
|
print(" 3. 必要时简化模型结构或降低输入尺寸")
|
|
else:
|
|
print("✓ 未发现明显不兼容算子")
|
|
|
|
quantization_markers = {'Quant', 'BipolarQuant', 'MultiThreshold', 'Trunc'}
|
|
found_quant_markers = {
|
|
node.op_type for node in graph.node
|
|
if node.op_type in quantization_markers or 'Quant' in node.op_type
|
|
}
|
|
|
|
if found_quant_markers:
|
|
print(f"✓ 发现量化相关节点: {', '.join(sorted(found_quant_markers))}")
|
|
else:
|
|
print("⚠ 未发现QONNX/Brevitas量化节点")
|
|
print(" 当前ONNX很可能是FP32导出。FINN可能可以做部分转换,但FPGA闭环更建议使用Brevitas/QONNX量化模型。")
|
|
|
|
except Exception as e:
|
|
print(f"✗ 兼容性检查失败: {e}")
|
|
|
|
|
|
def find_default_test_image(project_root):
|
|
"""返回真实存在的默认测试图。"""
|
|
preferred = os.path.join(project_root, DEFAULT_TEST_IMAGE)
|
|
if os.path.exists(preferred):
|
|
return preferred
|
|
|
|
val_dir = os.path.join(project_root, 'data', 'processed', 'val')
|
|
for class_idx in range(10):
|
|
class_dir = os.path.join(val_dir, f'class_{class_idx}')
|
|
if not os.path.isdir(class_dir):
|
|
continue
|
|
for img_name in sorted(os.listdir(class_dir)):
|
|
if img_name.lower().endswith(('.jpg', '.jpeg', '.png')):
|
|
return os.path.join(class_dir, img_name)
|
|
return None
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
parser = argparse.ArgumentParser(description='ONNX模型验证工具')
|
|
parser.add_argument('--model', default=None,
|
|
help='ONNX模型路径,默认data/models/model.onnx')
|
|
parser.add_argument('--pytorch_model', default=None,
|
|
help='PyTorch权重路径,默认data/models/best_model.pth')
|
|
parser.add_argument('--test_image', default=None,
|
|
help='用于PyTorch/ONNX对比的测试图,默认data/processed/val/class_0/10.jpg')
|
|
parser.add_argument('--runs', type=int, default=100,
|
|
help='ONNX Runtime性能测试次数')
|
|
args = parser.parse_args()
|
|
|
|
print("=" * 60)
|
|
print("ONNX模型验证工具")
|
|
print("=" * 60)
|
|
|
|
# 配置路径
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
pytorch_model_path = args.pytorch_model or os.path.join(project_root, 'data', 'models', 'best_model.pth')
|
|
onnx_model_path = args.model or os.path.join(project_root, 'data', 'models', 'model.onnx')
|
|
test_image_path = args.test_image or find_default_test_image(project_root)
|
|
|
|
# 检查文件是否存在
|
|
if not os.path.exists(onnx_model_path):
|
|
print(f"\n✗ ONNX模型不存在: {onnx_model_path}")
|
|
print("\n请先运行量化脚本生成ONNX模型:")
|
|
print(" cd pc_training")
|
|
print(" python quantize.py --model_path ../data/models/best_model.pth --export_onnx")
|
|
return
|
|
|
|
# 1. 验证ONNX模型
|
|
if not verify_onnx_model(onnx_model_path):
|
|
return
|
|
|
|
# 2. 对比输出(如果PyTorch模型存在)
|
|
if os.path.exists(pytorch_model_path):
|
|
torch, create_model, get_transforms = load_training_modules()
|
|
if torch is not None:
|
|
pytorch_model = load_pytorch_model(torch, create_model, pytorch_model_path)
|
|
compare_outputs(torch, get_transforms, pytorch_model, onnx_model_path, test_image_path)
|
|
else:
|
|
print(f"\n⚠ PyTorch模型不存在,跳过输出对比")
|
|
|
|
# 3. 测试性能
|
|
test_onnx_performance(onnx_model_path, num_runs=args.runs)
|
|
|
|
# 4. 检查FINN兼容性
|
|
check_finn_compatibility(onnx_model_path)
|
|
|
|
print("\n" + "=" * 60)
|
|
print("验证完成!")
|
|
print("=" * 60)
|
|
print("\n下一步:")
|
|
print(" 1. 如果验证通过,可以开始FINN转换")
|
|
print(" 2. 运行: python finn_convert.py")
|
|
print("=" * 60)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|