Files

430 lines
12 KiB
Python

"""
PYNQ部署和测试脚本
功能:
1. 在PYNQ Z2上加载FINN生成的overlay
2. 运行推理测试
3. 性能评估
4. 准确率测试
使用方法:
1. 将此脚本和overlay文件传输到PYNQ
2. 在PYNQ上运行: python3 pynq_test.py
"""
import os
import sys
import time
import argparse
import numpy as np
from PIL import Image
from pathlib import Path
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_INPUT_SIZE = 128
try:
from pynq import Overlay
try:
import pynq
PYNQ_VERSION = getattr(pynq, "__version__", "unknown")
except Exception:
PYNQ_VERSION = "unknown"
PYNQ_AVAILABLE = True
except ImportError:
print("警告: PYNQ库未安装,部分功能不可用")
PYNQ_VERSION = "unavailable"
PYNQ_AVAILABLE = False
class FINNInference:
"""
FINN推理封装类
"""
def __init__(self, bitfile_path, hwh_path=None, driver_path="driver.py", input_size=DEFAULT_INPUT_SIZE):
"""
初始化FINN推理器
参数:
bitfile_path: bitstream文件路径
hwh_path: 硬件描述文件路径(可选)
"""
if not PYNQ_AVAILABLE:
raise RuntimeError("PYNQ库未安装,无法加载overlay")
print(f"PYNQ版本: {PYNQ_VERSION}")
print(f"加载FINN overlay: {bitfile_path}")
self.input_size = input_size
self.overlay = Overlay(bitfile_path)
self.driver = self._load_finn_driver(driver_path)
if self.driver is not None:
self.accelerator = self.driver
print("✓ FINN driver.py已加载")
else:
# 获取加速器接口
# 注意:实际的接口名称取决于FINN生成的设计
# 可能需要根据实际情况调整
try:
self.accelerator = self.overlay.finn_design_wrapper_0
print("✓ 加速器接口已连接")
except AttributeError:
print("⚠ 未找到标准加速器接口,尝试列出可用接口...")
print("可用接口:", dir(self.overlay))
self.accelerator = None
# 图像预处理参数(ImageNet标准)
self.mean = None
self.std = None
def _load_finn_driver(self, driver_path):
"""优先使用FINN生成的driver.py接口。"""
if not driver_path or not os.path.exists(driver_path):
return None
driver_dir = os.path.abspath(os.path.dirname(driver_path))
if driver_dir and driver_dir not in sys.path:
sys.path.insert(0, driver_dir)
try:
import driver
except Exception as e:
print(f"⚠ driver.py导入失败,回退到overlay接口: {e}")
return None
for class_name in ("FINNDriver", "FINNExampleOverlay"):
driver_cls = getattr(driver, class_name, None)
if driver_cls is None:
continue
try:
return driver_cls(self.overlay)
except TypeError:
try:
return driver_cls()
except Exception as e:
print(f"⚠ {class_name}初始化失败: {e}")
except Exception as e:
print(f"⚠ {class_name}初始化失败: {e}")
print("⚠ driver.py中未找到FINNDriver/FINNExampleOverlay,回退到overlay接口")
return None
def preprocess(self, image_path):
"""
图像预处理
参数:
image_path: 图像路径
返回:
preprocessed: 预处理后的图像数组
"""
# 加载图像
img = Image.open(image_path).convert('RGB')
# 调整大小
img = img.resize((self.input_size, self.input_size))
# 转换为numpy数组
img_array = np.array(img, dtype=np.float32) / 255.0
# 归一化
# 转换为CHW格式
img_array = np.transpose(img_array, (2, 0, 1))
# 添加batch维度
img_array = np.expand_dims(img_array, axis=0)
return img_array
def infer(self, input_data):
"""
执行推理
参数:
input_data: 输入数据
返回:
output: 推理结果
"""
if self.accelerator is None:
raise RuntimeError("加速器接口未初始化")
# 执行推理
# 注意:实际的推理接口取决于FINN生成的设计
# 这里提供一个通用的框架
for method_name in ("execute", "infer", "run"):
method = getattr(self.accelerator, method_name, None)
if method is not None:
output = method(input_data)
return np.asarray(output)
raise RuntimeError("加速器接口未提供execute/infer/run方法")
def predict(self, image_path):
"""
预测图像类别
参数:
image_path: 图像路径
返回:
class_id: 预测类别
confidence: 置信度
"""
# 预处理
input_data = self.preprocess(image_path)
# 推理
output = self.infer(input_data)
# 后处理
output = np.asarray(output).reshape(-1)
class_id = int(np.argmax(output))
confidence = float(output[class_id])
return class_id, confidence
def test_single_image(finn_model, image_path):
"""
测试单张图像
参数:
finn_model: FINN推理模型
image_path: 图像路径
"""
print(f"\n测试图像: {image_path}")
try:
start_time = time.time()
class_id, confidence = finn_model.predict(image_path)
inference_time = time.time() - start_time
print(f" 预测类别: {class_id}")
print(f" 置信度: {confidence:.4f}")
print(f" 推理时间: {inference_time*1000:.2f} ms")
return class_id, confidence, inference_time
except Exception as e:
print(f" ✗ 推理失败: {e}")
return None, None, None
def test_accuracy(finn_model, test_dir):
"""
测试准确率
参数:
finn_model: FINN推理模型
test_dir: 测试数据目录
返回:
accuracy: 准确率
"""
print("\n" + "=" * 60)
print("准确率测试")
print("=" * 60)
correct = 0
total = 0
class_correct = [0] * 10
class_total = [0] * 10
# 遍历所有类别
for class_idx in range(10):
class_dir = os.path.join(test_dir, f'class_{class_idx}')
if not os.path.exists(class_dir):
print(f"⚠ 类别目录不存在: {class_dir}")
continue
print(f"\n测试类别 {class_idx}...")
# 遍历该类别的所有图像
for img_name in os.listdir(class_dir):
if not img_name.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
img_path = os.path.join(class_dir, img_name)
try:
pred_class, _ = finn_model.predict(img_path)
if pred_class == class_idx:
correct += 1
class_correct[class_idx] += 1
total += 1
class_total[class_idx] += 1
except Exception as e:
print(f" ✗ 处理失败 {img_name}: {e}")
# 打印该类别的准确率
if class_total[class_idx] > 0:
class_acc = 100.0 * class_correct[class_idx] / class_total[class_idx]
print(f" 类别 {class_idx} 准确率: {class_acc:.2f}% ({class_correct[class_idx]}/{class_total[class_idx]})")
# 总体准确率
if total > 0:
accuracy = 100.0 * correct / total
print(f"\n总体准确率: {accuracy:.2f}% ({correct}/{total})")
else:
accuracy = 0.0
print("\n⚠ 没有测试数据")
return accuracy
def find_first_test_image(test_dir):
"""查找验证集中第一张真实存在的测试图。"""
preferred = os.path.join(test_dir, 'class_0', '10.jpg')
if os.path.exists(preferred):
return preferred
for class_idx in range(10):
class_dir = os.path.join(test_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(('.png', '.jpg', '.jpeg')):
return os.path.join(class_dir, img_name)
return None
def test_performance(finn_model, num_runs=100):
"""
测试推理性能
参数:
finn_model: FINN推理模型
num_runs: 测试次数
返回:
avg_time: 平均推理时间
fps: 帧率
"""
print("\n" + "=" * 60)
print(f"性能测试({num_runs}次推理)")
print("=" * 60)
# 创建随机输入
dummy_input = np.random.rand(
1, 3, finn_model.input_size, finn_model.input_size
).astype(np.float32)
# 预热
print("预热中...")
for _ in range(10):
finn_model.infer(dummy_input)
# 测试
print("测试中...")
times = []
for i in range(num_runs):
start_time = time.time()
finn_model.infer(dummy_input)
end_time = time.time()
times.append(end_time - start_time)
if (i + 1) % 20 == 0:
print(f" 进度: {i+1}/{num_runs}")
# 统计
avg_time = np.mean(times)
std_time = np.std(times)
min_time = np.min(times)
max_time = np.max(times)
fps = 1.0 / avg_time
print(f"\n性能指标:")
print(f" 平均推理时间: {avg_time*1000:.2f} ms")
print(f" 标准差: {std_time*1000:.2f} ms")
print(f" 最小时间: {min_time*1000:.2f} ms")
print(f" 最大时间: {max_time*1000:.2f} ms")
print(f" 推理速度: {fps:.2f} FPS")
return avg_time, fps
def main():
"""主函数"""
parser = argparse.ArgumentParser(description='FINN PYNQ-Z2部署测试')
parser.add_argument('--bitfile', default='finn_design.bit',
help='FINN生成的bitstream路径')
parser.add_argument('--hwh', default='finn_design.hwh',
help='FINN生成的hwh路径')
parser.add_argument('--driver', default='driver.py',
help='FINN生成的driver.py路径')
parser.add_argument('--test_dir', default='test_images',
help='验证集目录,包含class_0到class_9')
parser.add_argument('--test_image', default=None,
help='单图测试路径,默认自动使用test_images/class_0/10.jpg')
parser.add_argument('--runs', type=int, default=100,
help='性能测试次数')
parser.add_argument('--input_size', type=int, default=DEFAULT_INPUT_SIZE,
help='FINN QCNN input image size')
args = parser.parse_args()
print("=" * 60)
print("FINN PYNQ部署测试")
print("=" * 60)
# 配置路径
bitfile_path = args.bitfile
hwh_path = args.hwh
test_dir = args.test_dir
# 检查文件
if not os.path.exists(bitfile_path):
print(f"\n✗ Bitstream文件不存在: {bitfile_path}")
print("\n请确保已将以下文件传输到PYNQ:")
print(" - finn_design.bit")
print(" - finn_design.hwh")
print(" - driver.py (如果有)")
print(" - test_images/ (测试图像)")
return
# 初始化FINN推理器
try:
print("\n初始化FINN推理器...")
finn_model = FINNInference(bitfile_path, hwh_path, args.driver, args.input_size)
print("✓ 初始化成功")
except Exception as e:
print(f"✗ 初始化失败: {e}")
return
# 测试单张图像
test_image = args.test_image or find_first_test_image(test_dir)
if test_image and os.path.exists(test_image):
test_single_image(finn_model, test_image)
else:
print(f"\n⚠ 未找到测试图像,预期路径示例: {os.path.join(test_dir, 'class_0', '10.jpg')}")
# 性能测试
test_performance(finn_model, num_runs=args.runs)
# 准确率测试
if os.path.exists(test_dir):
test_accuracy(finn_model, test_dir)
else:
print(f"\n⚠ 测试目录不存在: {test_dir}")
print("\n" + "=" * 60)
print("测试完成!")
print("=" * 60)
if __name__ == '__main__':
main()