3rd_Anti-UAV_CVPR23 是 CVPR23 第三届 Anti-UAV 研讨会的基线模型,基于 SiamFC(孪生全卷积网络)架构,用于无人机跟踪任务。该模型是 CVPR2023 研讨会 Track1(单目标跟踪)的参赛方案。
3rd_Anti-UAV_CVPR23-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志 (完整)
├── README.md # 本文档
├── test_sample.pt # 测试样本
├── test_sample_info.json # 测试样本信息
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23/iic/3rd_Anti-UAV_CVPR23/ 目录下:
pip install torch torch_npu -i https://pypi.huaweicloud.com/repository/pypi/simple/Run the inference script for NPU inference testing:
cd /data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23-ascend/
# 普通推理模式
python3 inference.py
# 首次运行会有模型加载和编译开销运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23-ascend/
# 精度测试模式
python3 inference.py precision_test| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 相对误差 | 0.0578% | < 1.00% | PASS |
| 最大绝对误差 | 1.222447e-03 | - | - |
| CPU 推理时间 | 0.3300s | - | - |
| NPU 推理时间 | 0.0017s | - | - |
| 加速比 | 191.39x | - | - |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 (单次) | 0.3300s |
| NPU 推理时间 (单次) | 0.0017s |
| 首次 NPU 推理 (含编译) | 5.7031s |
| 加速比 | 191.39x |
| 输入尺寸 | 输出尺寸 | NPU 推理时间 |
|---|---|---|
| [1, 3, 255, 255] | [1, 256, 22, 22] | 0.0017s |
结果: CPU 和 NPU 输出的相对误差仅为 0.0578%,远低于 1% 阈值,完全通过精度验证。
============================================================
3rd_Anti-UAV_CVPR23 NPU Test Suite
Model: SiamFC-based UAV Tracking
Output: /data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23-ascend
============================================================
Mode: PRECISION TEST (CPU vs NPU)
============================================================
3rd_Anti-UAV_CVPR23 NPU Inference Test
============================================================
Device: npu:0
Model: /data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23/iic/3rd_Anti-UAV_CVPR23/pytorch_model.pt
Loading model state dict...
Loaded 30 state dict entries
Building SiamFC model...
Model built successfully
Input shape: torch.Size([1, 3, 255, 255])
Running inference...
Output shape: torch.Size([1, 256, 22, 22])
Inference time: 5.7031s
============================================================
Creating Test Samples
============================================================
Saved test sample: /data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23-ascend/test_sample.pt
Shape: torch.Size([1, 3, 255, 255])
Seed: 42 (for reproducibility)
============================================================
3rd_Anti-UAV_CVPR23 Precision Test (CPU vs NPU)
============================================================
Device: npu:0
Loading model state dict...
Building CPU model...
Building NPU model...
Input shape: torch.Size([1, 3, 255, 255])
Input seed: 42 (for reproducibility)
Running inference on CPU...
CPU inference time: 0.3300s
CPU output shape: torch.Size([1, 256, 22, 22])
Running inference on NPU...
NPU inference time: 0.0017s
NPU output shape: torch.Size([1, 256, 22, 22])
============================================================
Precision Results
============================================================
CPU inference time: 0.3300s
NPU inference time: 0.0017s
Speedup: 191.39x
Max absolute error: 1.222447e-03
Max relative error: 5.782230e-04 (0.0578%)
Threshold: 1.0%
Status: PASS
============================================================
Test Complete!
============================================================SiamFC 模型架构(类 AlexNet 骨干网络):
| 层 | 输入通道 | 输出通道 | 卷积核 | 步长 | 分组 |
|---|---|---|---|---|---|
| conv1 | 3 | 96 | 11x11 | 2 | 1 |
| pool1 | - | - | 3x3 | 2 | - |
| conv2 | 96 | 256 | 5x5 | 1 | 2 |
| pool2 | - | - | 3x3 | 2 | - |
| conv3 | 256 | 384 | 3x3 | 1 | 1 |
| conv4 | 384 | 384 | 3x3 | 1 | 2 |
| conv5 | 384 | 256 | 3x3 | 1 | 2 |
import torch
import torch.nn as nn
import torch.nn.functional as F
MODEL_PATH = "/data/ysws/agentsp/5-19-1/3rd_Anti-UAV_CVPR23/iic/3rd_Anti-UAV_CVPR23/pytorch_model.pt"
class SiamFCModel(nn.Module):
def __init__(self, state_dict):
super().__init__()
self.conv1 = nn.Conv2d(3, 96, 11, stride=2)
self.bn1 = nn.BatchNorm2d(96)
self.conv2 = nn.Conv2d(96, 256, 5, groups=2)
self.bn2 = nn.BatchNorm2d(256)
self.conv3 = nn.Conv2d(256, 384, 3)
self.bn3 = nn.BatchNorm2d(384)
self.conv4 = nn.Conv2d(384, 384, 3, groups=2)
self.bn4 = nn.BatchNorm2d(384)
self.conv5 = nn.Conv2d(384, 256, 3, groups=2)
# Load weights from state_dict...
def forward(self, x):
x = torch.relu(self.bn1(self.conv1(x)))
x = F.max_pool2d(x, 3, stride=2)
x = torch.relu(self.bn2(self.conv2(x)))
x = F.max_pool2d(x, 3, stride=2)
x = torch.relu(self.bn3(self.conv3(x)))
x = torch.relu(self.bn4(self.conv4(x)))
x = self.conv5(x)
return x
# Load and run
state_dict = torch.load(MODEL_PATH, map_location="cpu")
model = SiamFCModel(state_dict).to("npu:0")
model.eval()
test_input = torch.randn(1, 3, 255, 255).to("npu:0")
with torch.no_grad():
output = model(test_input)
print(f"Output shape: {output.shape}") # torch.Size([1, 256, 22, 22])# 提取 exemplar 特征
exemplar = torch.randn(1, 3, 127, 127).to("npu:0") # 127x127 exemplar
exemplar_feat = model(exemplar) # [1, 256, 6, 6]
# 提取 search 区域特征
search = torch.randn(1, 3, 255, 255).to("npu:0") # 255x255 search
search_feat = model(search) # [1, 256, 22, 22]
# 交叉相关计算响应图
response = F.conv2d(search_feat, exemplar_feat)
print(f"Response shape: {response.shape}") # [1, 1, H, W]A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。0.05-0.1% 的数值误差是正常的,因为 NPU 和 CPU 使用不同的计算精度。
A: 首次推理需要 JIT 编译和算子加载,约 5-6 秒。后续推理会显著加速 (0.0017 秒)。
A: 分组卷积将输入通道分成多组,每组独立卷积。SiamFC 使用 groups=2,减少 50% 的计算量和参数量。
A: SiamFC 跟踪器通过交叉相关 (cross-correlation) 计算搜索区域与 exemplar 的相似度,响应图峰值位置即为目标位置。
本项目遵循 MIT 许可证