本仓库基于[owlv2-base-patch16-finetuned](https://huggingface.co/google/owlv2-base-patch16-finetuned)仓库,提供基于AXERA NPU平台的OWLViT2模型的使用示例。以下是如何使用该模型进行量化推理的步骤: # 模型导出 通过[export_split_owlv2_onnx.py](export_split_owlv2_onnx.py)脚本可以将OWLViT2模型导出为ONNX模型,方便后续进行量化编译。 本示例以OWLViT2模型的4分类版本为例,导出时会将模型分为三个部分:图像编码器、文本编码器和后处理模块。每个部分都会被导出为一个独立的ONNX模型。 运行以下命令: ```bash python export_split_owlv2_onnx.py --model-dir ../owlv2-base-patch16-finetuned \ --output-dir ./split_4cls \ --image-size 640 ``` 就可以得到:owlv2_image_4cls.onnx、owlv2_text_4cls.onnx和owlv2_post_4cls.onnx三个ONNX模型文件。 # Pulsar2量化和编译 使用AXERA的Pulsar2工具可以对导出的ONNX模型进行量化和编译,生成适用于AXERA NPU的模型文件(.axmodel)。 相关工具下载和使用说明可以参考: - [Pulsar2工具下载](https://huggingface.co/AXERA-TECH/Pulsar2) - [AXERA官方文档](https://pulsar2-docs.readthedocs.io/zh-cn/latest/index.html) 当前使用PTQ(Post-Training Quantization)方法进行量化,需要相关量化数据集进行校准。可以使用OWLViT2的验证集或应用场景相关数据集进行量化校准。 以下是示例命令: ```bash # 将config目录下的image.json、text.json和post.json配置文件中的路径替换为实际的ONNX模型路径和量化数据集路径后,运行以下命令进行量化和编译: cd config Pulsar2 build --config ./image.json Pulsar2 build --config ./text.json Pulsar2 build --config ./post.json ``` # 推理示例和精度验证 通过[run_split_owlv2_full_demo_ax.py](run_split_owlv2_full_demo_ax.py)脚本可以进行基于AXERA NPU的OWLViT2模型的推理示例和精度验证。同时,也提供了基于ONNX Runtime的推理示例[run_split_owlv2_full_demo.py](run_split_owlv2_full_demo.py),方便实际测试编译量化后的精度损失。 将相关测试数据和模型拷贝的AXERA NPU设备上后,运行以下命令: ```bash python run_split_owlv2_full_demo_ax.py --model-dir ../owlv2-base-patch16-finetuned \ --split-dir ./axmodel \ --labels ./4cls_labels.json \ --image ./test_img/ssd_horse.jpg \ --image-size 640 \ --score-threshold 0.1 \ --iou-threshold 0.45 \ --topk-per-class 20 \ --output-image ./result.jpg \ --output-report ./result.json #output: [INFO] Available providers: ['AxEngineExecutionProvider', 'AXCLRTExecutionProvider'] [INFO] Using provider: AxEngineExecutionProvider [INFO] Chip type: ChipType.MC50 [INFO] VNPU type: VNPUType.DISABLED [INFO] Engine version: 2.12.0s [INFO] Model type: 2 (triple core) [INFO] Compiler version: 5.0-patch1 fd447d0d [INFO] Using provider: AxEngineExecutionProvider [INFO] Model type: 2 (triple core) [INFO] Compiler version: 5.0-patch1 fd447d0d [INFO] Using provider: AxEngineExecutionProvider [INFO] Model type: 2 (triple core) [INFO] Compiler version: 5.0-patch1 fd447d0d Saved result image: /root/wangli/owlvit2_ax/ax_result.jpg Saved result report: /root/wangli/owlvit2_ax/ax_result.json Detections: 24 a photo of a dog: 0.661 [135.375, 244.473, 204.636, 356.683] a photo of a car: 0.349 [0.0, 94.699, 151.441, 210.296] a photo of a person: 0.252 [263.865, 0.0, 363.061, 254.001] a photo of a person: 0.229 [420.104, 133.739, 427.748, 145.166] a photo of a person: 0.224 [405.144, 133.3, 411.354, 140.433] a photo of a person: 0.222 [415.782, 141.18, 427.126, 152.6] a photo of a person: 0.213 [480.405, 133.277, 488.438, 140.105] a photo of a person: 0.193 [444.329, 120.899, 451.309, 131.946] a photo of a person: 0.185 [405.423, 130.287, 438.112, 152.387] a photo of a person: 0.184 [428.561, 141.619, 435.495, 152.574] ```