• 3403(3519Dv500)算子精度比对工具标杆数据生成环境搭建指导(Caffe)


    一、简介

            海思平台算子精度比对工具所依赖的标杆数据生成环境搭建指导文档,基于ubuntu操作系统,目前仅支持 Caffe 。

    二、环境准备

            1.caffe安装

            参考:

             2. caffe 推理场景dump脚本使用方法

         (1)功能说明

                    用于获取caffe的运行结果,可以获取每一层的结果,输入需要是bin文件。

                    示例1:dump模型每个节点的npy结果文件

    python3.7 caffe_dump.py -m resnet50.prototxt -w resnet50.caffemodel -i test.bin -n 'data' -o ./output_d

            其中 -n 指定的名称为模型的实际输入名称,-i 指定的输入文件需要与输入名称一一对应,且大小以及数据类型相匹配。

    示例1:npy格式的dump文件会生成到 -o 指定的路径中,如下图:

    caffe_dump.py

    1. # coding=utf-8
    2. import caffe
    3. import sys
    4. import argparse
    5. import os
    6. import caffe.proto.caffe_pb2 as caffe_pb2
    7. import google.protobuf.text_format
    8. import json
    9. import numpy as np
    10. import time
    11. TIME_LENGTH = 1000
    12. FILE_PERMISSION_FLAG = 0o600
    13. class CaffeProcess:
    14. def __init__(self):
    15. parse = argparse.ArgumentParser()
    16. parse.add_argument("-w", dest="weight_file_path",
    17. help=" the caffe weight file path",
    18. required=True)
    19. parse.add_argument("-m", dest="model_file_path",
    20. help=" the caffe model file path",
    21. required=True)
    22. parse.add_argument("-o", dest="output_path", help=" the output path",
    23. required=True)
    24. parse.add_argument("-i", "--input_bins", dest="input_bins", help="input_bins bins. e.g. './a.bin;./c.bin'",
    25. required=True)
    26. parse.add_argument("-n", "--input_names", dest="input_names",
    27. help="input nodes name. e.g. 'input_0;input_1'",
    28. required=True)
    29. args, _ = parse.parse_known_args(sys.argv[1:])
    30. self.weight_file_path = os.path.realpath(args.weight_file_path)
    31. self.model_file_path = os.path.realpath(args.model_file_path)
    32. self.input_bins = args.input_bins.split(";")
    33. self.input_names = args.input_names.split(";")
    34. self.output_path = os.path.realpath(args.output_path)
    35. self.net_param = None
    36. self.cur_layer_idx = -1
    37. @staticmethod
    38. def _check_file_valid(path, is_file):
    39. if not os.path.exists(path):
    40. print('Error: The path "' + path + '" does not exist.')
    41. exit(-1)
    42. if is_file:
    43. if not os.path.isfile(path):
    44. print('Error: The path "' + path + '" is not a file.')
    45. exit(-1)
    46. else:
    47. if not os.path.isdir(path):
    48. print('Error: The path "' + path + '" is not a directory.')
    49. exit(-1)
    50. def _check_arguments_valid(self):
    51. self._check_file_valid(self.model_file_path, True)
    52. self._check_file_valid(self.weight_file_path, True)
    53. self._check_file_valid(self.output_path, False)
    54. for input_file in self.input_bins:
    55. self._check_file_valid(input_file, True)
    56. @staticmethod
    57. def calDataSize(shape):
    58. dataSize = 1
    59. for dim in shape:
    60. dataSize *= dim
    61. return dataSize
    62. def _load_inputs(self, net):
    63. inputs_map = {}
    64. for layer_name, blob in net.blobs.items():
    65. if layer_name in self.input_names:
    66. input_bin = np.fromfile(
    67. self.input_bins[self.input_names.index(layer_name)], np.float32)
    68. input_bin_shape = blob.data.shape
    69. if self.calDataSize(input_bin_shape) == self.calDataSize(input_bin.shape):
    70. input_bin = input_bin.reshape(input_bin_shape)
    71. else:
    72. print("Error: input node data size %d not match with input bin data size %d.", self.calDataSize(
    73. input_bin_shape), self.calDataSize(input_bin.shape))
    74. exit(-1)
    75. inputs_map[layer_name] = input_bin
    76. return inputs_map
    77. def process(self):
    78. """
    79. Function Description:
    80. process the caffe net, save result as dump data
    81. """
    82. # check path valid
    83. self._check_arguments_valid()
    84. # load model and weight file
    85. net = caffe.Net(self.model_file_path, self.weight_file_path,
    86. caffe.TEST)
    87. inputs_map = self._load_inputs(net)
    88. for key, value in inputs_map.items():
    89. net.blobs[key].data[...] = value
    90. # process
    91. net.forward()
    92. # read prototxt file
    93. net_param = caffe_pb2.NetParameter()
    94. with open(self.model_file_path, 'rb') as model_file:
    95. google.protobuf.text_format.Parse(model_file.read(), net_param)
    96. for layer in net_param.layer:
    97. name = layer.name.replace("/", "_").replace(".", "_")
    98. index = 0
    99. for top in layer.top:
    100. data = net.blobs[top].data[...]
    101. file_name = name + "." + str(index) + "." + str(
    102. round(time.time() * 1000000)) + ".npy"
    103. output_dump_path = os.path.join(self.output_path, file_name)
    104. np.save(output_dump_path, data)
    105. os.chmod(output_dump_path, FILE_PERMISSION_FLAG)
    106. print('The dump data of "' + layer.name
    107. + '" has been saved to "' + output_dump_path + '".')
    108. index += 1
    109. if __name__ == "__main__":
    110. caffe_process = CaffeProcess()
    111. caffe_process.process()

  • 相关阅读:
    Java基础常见面试题总结
    Java如何将两个数组合并为一个数组呢?
    数分-工具-Pandas1-预备知识
    云防火墙和传统防火墙区别是什么
    GO语言的由来与发展历程
    leetcode:63. 不同路径II
    HTML期末学生大作业-节日网页作业html+css+javascript
    文件工具类
    Idea 使用 —— Save Actions 插件的安装与配置
    C++ Day4
  • 原文地址:https://blog.csdn.net/u012374012/article/details/139421352