Feature-Distance
1 概述¶
1.1 背景介绍¶
Feature_distance通过计算图像之间的相似度,完成图像特征比对的任务,通过IPU加速了特征比对的计算。其关键思想是:相似图像对之间的特征距离较近, 而不相似的图像之间的特征距离较远。
1.2 使用说明¶
Linux SDK-alkaid中默认带了板端示例, 相关文件路径如下:
-
板端示例程序路径
Linux_SDK/sdk/verify/opendla/source/feature_distance -
板端测试图像路径
Linux_SDK/sdk/verify/opendla/source/resource/feature_library/feat_gallery.bin Linux_SDK/sdk/verify/opendla/source/resource/feature_library/name_gallery.list Linux_SDK/sdk/verify/opendla/source/resource/feature_library/feat_query.bin Linux_SDK/sdk/verify/opendla/source/resource/feature_library/name_query.list
如果用户不需要转换模型可直接跳转至第3章节。
2 模型转换¶
2.1 生成onnx模型¶
-
使用IPU对图像相似度计算进行加速时使用的模型不需要加载权重,因此不需要模型权重文件。运行以下代码可以直接转换出所需的onnx模型
cos.onnx:注:根据底库的大小,修改gallery_size(e.g. 底库中有256张图片,则gallery_size = 256)
import torch import numpy as np import torch.nn as nn import torch.nn.functional as F gallery_size = 256 class Net(nn.Module): def __init__(self): super(Net, self).__init__() def forward(self, x, y): dot = torch.matmul(x, y.permute(1, 0)) norm = torch.sqrt(torch.matmul(x, x.permute(1, 0))).repeat(1, gallery_size) * torch.norm(y, dim=1).view(1, -1) return dot, norm torch_model = Net() x = torch.randn(1, 512, requires_grad=False) y = torch.randn(gallery_size, 512, requires_grad=False) torch.onnx.export( torch_model, (x, y), "cos.onnx", opset_version=12, input_names=['input0', 'input1'], output_names=['output0', 'output1'] )
2.2 离线模型转换¶
注意:1)OpenDLAModel对应的是压缩包image-dev_model_convert.tar解压之后的smodel文件。2)转换命令需要在docker环境下运行, 请先根据Docker开发环境教程, 加载SGS Docker环境。
-
拷贝onnx模型到转换代码目录
$cp ./cos.onnx OpenDLAModel/feature_distance/onnx -
根据底库图片数量(gallery_size),生成量化数据
cos.txt:该模型是特征相似度计算模型,量化数据是从特征提取模型获得的输出特征; shape=(1,gallery_size,512) -
转换命令
$cd IPU_SDK_Release/docker $bash run_docker.sh #进入到docker环境下的OpenDLAModel目录 $cd /work/SGS_XXX/OpenDLAModel $bash convert.sh -a feature_distance -c config/feature_distance.cfg -p SGS_IPU_Toolchain(绝对路径) -s false -
最终生成的模型地址
output/${chip}_${时间}/cos.img
2.2.3 关键脚本参数解析¶
- config.ini
[INPUT_CONFIG]
inputs = input0,input1; #onnx 输入节点名称, 如果有多个需以“,”隔开;
input_formats = RAWDATA_S16_NHWC;RAWDATA_S16_NHWC #板端输入格式, 可以根据情况选择BGRA或者YUV_NV12;
quantizations = TRUE,TRUE; #打开输入量化, 不需要修改;
[OUTPUT_CONFIG]
outputs = output0,output1; #onnx 输出节点名称, 如果有多个需以“,”隔开;
dequantizations = TRUE,TRUE; #是否开启反量化, 根据实际需求填写, 建议为TRUE。设为False, 输出为int16; 设为True, 输出为float32
- feature_distance.cfg
[FEATURE]
CHIP_LIST=pcupid #平台名称, 必须和板端平台一致, 否则模型无法运行
Model_LIST=cos #输入onnx模型名称
INPUT_SIZE_LIST=256 #模型输入长度(底库大小)
INPUT_INI_LIST=config.ini #配置文件
CLASS_NUM_LIST=Unknown #填Unknown即可
SAVE_NAME_LIST=cos.img #输出模型名称
QUANT_DATA_PATH=./cos.txt #量化数据路径
3 板端部署¶
3.1 程序编译¶
示例程序编译之前需要先根据板子(nand/nor/emmc, ddr型号等)选择deconfig进行sdk整包编译, 具体可以参考alkaid sdk sigdoc《开发环境搭建》文档。
-
编译板端clip示例。
$cd sdk/verify/opendla $make clean && make source/feature_distance -j8 -
最终生成的可执行文件地址
sdk/verify/opendla/out/${AARCH}/app/prog_feature_distance
3.2 运行文件¶
-
运行程序时, 需要先将以下文件拷贝到板端
- 运行脚本
- prog_feature_distance
- offline模型
- cos.img
- 运行脚本
-
准备好图像文件
- 图像底库
- feat_gallery.bin
- name_gallery.list
- 查询图像
- feat_query.bin
- name_query.list
- 图像底库
3.3 运行说明¶
-
Usage:
./prog_feature_distance modelPath galleryBin galleryNames queryBin queryNames topk(执行文件使用命令) -
Required Input:
- modelPath: offline模型路径
- galleryBin: 图像底库(bin 文件)
- galleryNames: 图像查询库中所有图像的名称(list 文件)
- queryBin: 查询图像(bin 文件)
- queryNames:查询图像的名称(list 文件)
- tok:输出前k个预测值
-
Typical output:
/prog_feature_distance models/cos.img resource/feature_library/feat_gallery.bin resource/feature_library/name_gallery.list resource/feature_library/feat_query.bin resource/feature_library/name_query.list 3 client [812] connected, module:ipu query_0001, the image path is 044, the score is 0.570715 query_0001, the image path is 194, the score is 0.299766 query_0001, the image path is 052, the score is 0.297114 query_0002, the image path is 224, the score is 0.510769 query_0002, the image path is 294, the score is 0.301047 query_0002, the image path is 266, the score is 0.295059 query_0003, the image path is 164, the score is 0.549499 query_0003, the image path is 010, the score is 0.317435 query_0003, the image path is 052, the score is 0.303460 query_0004, the image path is 298, the score is 0.634573 query_0004, the image path is 044, the score is 0.230089 query_0004, the image path is 258, the score is 0.225121 collect channel0's resource ------shutdown IPU0------ client [812] disconnected, module:ipu