
# 精度校验
转换模型后执行推理前，可以使用benchmark工具对MindSpore Lite云侧推理模型进行基准测试。它不仅可以对MindSpore Lite云侧推理模型前向推理执行耗时进行定量分析（性能），还可以通过指定模型输出进行可对比的误差分析（精度）。
#### 精度测试
benchmark工具用于精度验证，主要工作原理是：固定模型的输入，通过benchmark工具进行推理，并将推理得到的输出与标杆数据进行相似度度量（余弦相似度和平均相对误差），得到模型转换后的精度偏差信息。使用benchmark进行精度比对的基本流程如下：
1. 将模型输入保存二进制文件。
   ```
   # 数据读取，预处理
   image = img_preprocess(image_path)
   image = np.array(image, dtype=np.float32)
   image = np.frombuffer(image.tobytes(), np.float32)
   # 保存网络输入为二进制文件
   image.tofile("input_data.bin")
   ```
   
2. 将基准模型的输出保存到文本文件。 本例中输出节点名称为output_node_name，输出节点的shape为"(1, 1000)"，因此一共有两维，对应的输出文件为"output_node_name 2 1 1000"，再加上输出的值即可。
   ```
   # 基于原始pth模型前向推理。
   output = model_inference(input_data)
   # 保存网络输出节点名称、维度、shape及输出到本地文件。
   with open("output_data.txt", "w") as f:
   f.write("output_node_name 2 1 1000\n")
   f.write(" ".join([str(i) for i in output]))
   ```
   
3. 使用benchmark工具进行精度对比。
   ```
   # shell
   benchmark --modelFile=model.mindir --inputShapes=1,3,224,224 --inDataFile=input_data.bin --device=Ascend --benchmarkDataFile=output_data.txt --accuracyThreshold=5 --cosineDistanceThreshold=0.99
   ```
   其中，--accuracyThreshold=5表示平均绝对误差的容忍度最大为5%，--cosineDistanceThreshold =0.99表示余弦相似度至少为99%，--inputShapes可将模型放入到[netron官网](https://netron.app/)中查看。
   图1benchmark对接结果输出示例图   
   ![](https://support.huaweicloud.com/bestpractice-modelarts/figure/zh-cn_image_0000001792836209.png "点击放大")
   为了简化用户使用，ModelArts提供了Tailor工具便于用户进行Benchmark精度测试，具体使用方式参考[Tailor指导文档](https://support.huaweicloud.com/bestpractice-modelarts/modelarts_10_0166.html)。
   
 
