模型准确度测试 - 2.5 简体中文

Vitis AI Library 用户指南 (UG1354)

Document ID
UG1354
Release Date
2022-06-15
Version
2.5 简体中文

要在开发板上测试模型准确度,必须考虑下列因素。

  • 图像数据集
  • 模型
  • 准确度测试程序
  • 图像数据集的真实文件
  • 准确度提取比较脚本

ResNet50 示例

resnet50 为例。

  1. 获取图像数据集和数据集的真实文件。

    您可从 https://github.com/Xilinx/Vitis-AI/tree/master/model_zoo 获取图像数据集信息。resnet50 实用 imagenet 数据集。

  2. 获取模型。
    wget https://www.xilinx.com/bin/public/openDownload?filename=resnet50-zcu102_zcu104_kv260-r2.5.0.tar.gz -O resnet50-zcu102_zcu104_kv260-r2.5.0.tar.gz
  3. 将模型复制到开发板上。
    scp resnet50-zcu102_zcu104_kv260-r2.5.0.tar.gz root@IP_OF_BOARD:~/

    它包含 resnet50resnet50_acc 模型。

  4. 在开发板上将其解压。
    tar -xzvf resnet50-zcu102_zcu104_kv260-r2.5.0.tar.gz -C /usr/share/vitis-ai-library/models
  5. 将图像数据集的路径和图像名称设置到文件中,例如,image.list.txt

  6. 在开发板上运行准确度测试程序。
    cd ~/Vitis-AI/examples/Vitis-AI-Library/samples/classification/
    ./test_accuracy_classification_mt resnet50 image.list.txt resnet50.image.list.result
    注释: 准确度测试会加载 resnet50_acc 模型和 resnet50 模型。这两个模型间的唯一区别是模型 prototxt 文件。

    运行完成准确度测试程序后,将生成结果文件 resnet50.image.list.result

  7. resnet50.image.list.result 复制到主机。
  8. 运行对应的准确度提取比较脚本,以获取最终准确度。
    python evaluation.py image.list.gt resnet50.image.list.result


    evaluation.py 代码如下

    
    #!/usr/bin/env python
    
    
    import sys
    # argv[1] must groundtruth
    readfile=open(sys.argv[1], 'r')
    readfile1=open(sys.argv[2], 'r')
    
    
    dic_val={}
    
    
    m = 0
    for line in readfile:
        temp = line.strip('/').split()
        key = temp[0]
        value = int(temp[1])
        dic_val[key] = value
        m = m + 1
    
    
    n = 0
    for line1 in readfile1:
        temp = line1.strip('/').split()
        if temp[0] in dic_val and int(temp[1]) == dic_val[temp[0]]:
            # print int(temp[1]), dic_val[temp[0]]
            n = n + 1
    
    
    #print m
    #print n
    readfile1.close()
    readfile2=open(sys.argv[2], 'r')
    rate = float(n)/float(m)
    print("accuracy of top-5: ", rate)
    
    
    l = 0
    a = 0
    for line2 in readfile2:
        a = a + 1
        if (a%5 != 1) : continue
        temp = line2.strip('/').split()
        if temp[0] in dic_val and int(temp[1]) == dic_val[temp[0]]:
            l = l + 1
    rate1 = float(l)/float(m)
    
    注释: 如需获取准确度提取比较脚本,请参阅 https://github.com/Xilinx/Vitis-AI/tree/master/model_zoo