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

一、简介

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

二、环境准备

1.caffe安装

参考:

  1. 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

复制代码
# coding=utf-8

import caffe
import sys
import argparse
import os
import caffe.proto.caffe_pb2 as caffe_pb2
import google.protobuf.text_format
import json
import numpy as np
import time

TIME_LENGTH = 1000
FILE_PERMISSION_FLAG = 0o600
class CaffeProcess:
    def __init__(self):
        parse = argparse.ArgumentParser()
        parse.add_argument("-w", dest="weight_file_path",
                           help="<Required> the caffe weight file path",
                           required=True)
        parse.add_argument("-m", dest="model_file_path",
                           help="<Required> the caffe model file path",
                           required=True)
        parse.add_argument("-o", dest="output_path", help="<Required> the output path",
                           required=True)
        parse.add_argument("-i", "--input_bins", dest="input_bins", help="input_bins bins. e.g. './a.bin;./c.bin'",
                           required=True)
        parse.add_argument("-n", "--input_names", dest="input_names",
                           help="input nodes name. e.g. 'input_0;input_1'",
                           required=True)
        args, _ = parse.parse_known_args(sys.argv[1:])
        self.weight_file_path = os.path.realpath(args.weight_file_path)
        self.model_file_path = os.path.realpath(args.model_file_path)
        self.input_bins = args.input_bins.split(";")
        self.input_names = args.input_names.split(";")
        self.output_path = os.path.realpath(args.output_path)
        self.net_param = None
        self.cur_layer_idx = -1

    @staticmethod
    def _check_file_valid(path, is_file):
        if not os.path.exists(path):
            print('Error: The path "' + path + '" does not exist.')
            exit(-1)
        if is_file:
            if not os.path.isfile(path):
                print('Error: The path "' + path + '" is not a file.')
                exit(-1)
        else:
            if not os.path.isdir(path):
                print('Error: The path "' + path + '" is not a directory.')
                exit(-1)

    def _check_arguments_valid(self):
        self._check_file_valid(self.model_file_path, True)
        self._check_file_valid(self.weight_file_path, True)
        self._check_file_valid(self.output_path, False)
        for input_file in self.input_bins:
            self._check_file_valid(input_file, True)

    @staticmethod
    def calDataSize(shape):
        dataSize = 1
        for dim in shape:
            dataSize *= dim
        return dataSize

    def _load_inputs(self, net):
        inputs_map = {}
        for layer_name, blob in net.blobs.items():
            if layer_name in self.input_names:
                input_bin = np.fromfile(
                    self.input_bins[self.input_names.index(layer_name)], np.float32)
                input_bin_shape = blob.data.shape
                if self.calDataSize(input_bin_shape) == self.calDataSize(input_bin.shape):
                    input_bin = input_bin.reshape(input_bin_shape)
                else:
                    print("Error: input node data size %d not match with input bin data size %d.", self.calDataSize(
                        input_bin_shape), self.calDataSize(input_bin.shape))
                    exit(-1)
                inputs_map[layer_name] = input_bin
        return inputs_map

    def process(self):
        """
        Function Description:
            process the caffe net, save result as dump data
        """
        # check path valid
        self._check_arguments_valid()

        # load model and weight file
        net = caffe.Net(self.model_file_path, self.weight_file_path,
                        caffe.TEST)
        inputs_map = self._load_inputs(net)
        for key, value in inputs_map.items():
            net.blobs[key].data[...] = value
        # process
        net.forward()

        # read prototxt file
        net_param = caffe_pb2.NetParameter()
        with open(self.model_file_path, 'rb') as model_file:
            google.protobuf.text_format.Parse(model_file.read(), net_param)
        for layer in net_param.layer:
            name = layer.name.replace("/", "_").replace(".", "_")
            index = 0
            for top in layer.top:
                data = net.blobs[top].data[...]
                file_name = name + "." + str(index) + "." + str(
                    round(time.time() * 1000000)) + ".npy"
                output_dump_path = os.path.join(self.output_path, file_name)
                np.save(output_dump_path, data)
                os.chmod(output_dump_path, FILE_PERMISSION_FLAG)
                print('The dump data of "' + layer.name
                      + '" has been saved to "' + output_dump_path + '".')
                index += 1
if __name__ == "__main__":
    caffe_process = CaffeProcess()
    caffe_process.process()
相关推荐
北京地铁1号线13 分钟前
机器学习笔试选择题:题组2
人工智能·算法·机器学习
算家云36 分钟前
OpenAI秘密测试ChatGPT安全路由,情感对话触发GPT-5严格审查
人工智能·chatgpt·算家云·openai秘密测试安全路由·算家计算·租算力,就到算家云
新加坡内哥谈技术36 分钟前
OpenAI近日推出了一项名为 ChatGPT Pulse 的全新功能
人工智能·chatgpt
hunteritself38 分钟前
DeepSeek V3.1-Terminus、阿里 Qwen3-Max、ChatGPT Pulse 同周登场!| AI Weekly 9.22-9.28
人工智能·ios·chatgpt·语音识别·iphone
ai_xiaogui41 分钟前
ChatGPT开源模型发布!部署20B/120B AI模型实战指南
人工智能·chatgpt·20b 120b模型部署教程·本地部署chatgpt模型实战·高性能加速开源ai模型开发·开源模型windows配置方法
春末的南方城市41 分钟前
阿里开源视频修复方法Vivid-VR:以独特策略与架构革新,引领生成视频修复高质量可控新时代。
人工智能·深度学习·机器学习·计算机视觉·aigc
Juchecar1 小时前
人工智能重塑人类生活范式
人工智能
FIT2CLOUD飞致云1 小时前
飞致云开源社区月度动态报告(2025年9月)
人工智能·开源
量子位1 小时前
宇树机器人被曝漏洞,机器人之间可相互感染,官方火速回应
人工智能·ai编程
chaofan9801 小时前
如何用 Claude Code 搭建安全、可测、可自动化的 GitHub CI 流程?
运维·人工智能·ci/cd·ai·自动化·github·claude