[{"data":1,"prerenderedAt":242},["ShallowReactive",2],{"content-query-DpJtZkBuAD":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":236,"_id":237,"_source":238,"_file":239,"_stem":240,"_extension":241},"/technology-blogs/zh/3250","zh",false,"","基于MindSpore案例的香橙派开发板离线推理实践--使用Shufflenet模型对输入图片进行分类","功能：使用Shufflenet模型对输入图片进行分类。 样例输入：原始Cifar20数据集。 样例输出：图片分类的结果。","2024-07-30","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/08/02/ea1108927c0d429f9aaf9015f4e3f184.png","technology-blogs","调试调优",{"type":15,"children":16,"toc":226},"root",[17,25,30,35,40,45,50,60,65,110,115,122,130,136,144,150,158,164,172,178,183,191,197,202],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于mindspore案例的香橙派开发板离线推理实践-使用shufflenet模型对输入图片进行分类",[23],{"type":24,"value":8},"text",{"type":18,"tag":19,"props":26,"children":28},{"id":27},"样例介绍",[29],{"type":24,"value":27},{"type":18,"tag":31,"props":32,"children":33},"p",{},[34],{"type":24,"value":9},{"type":18,"tag":19,"props":36,"children":38},{"id":37},"前期准备",[39],{"type":24,"value":37},{"type":18,"tag":31,"props":41,"children":42},{},[43],{"type":24,"value":44},"基础镜像的样例目录中已包含转换后的om模型以及测试数据集，如果直接运行，可跳过此步骤。如果需要重新转换模型，可参考如下步骤：",{"type":18,"tag":31,"props":46,"children":47},{},[48],{"type":24,"value":49},"利用atc工具将原始模型转换为om模型，转换命令如下：",{"type":18,"tag":51,"props":52,"children":54},"pre",{"code":53},"atc --output_type=FP32 --input_format=NCHW--output=\"./shufflenet\" --soc_version=Ascend310B4 --framework=1 --save_original_model=false --model=\"./shufflenet.air\" --precision_mode=allow_fp32_to_fp16\n",[55],{"type":18,"tag":56,"props":57,"children":58},"code",{"__ignoreMap":7},[59],{"type":24,"value":53},{"type":18,"tag":31,"props":61,"children":62},{},[63],{"type":24,"value":64},"其中各个参数具体含义如下：",{"type":18,"tag":66,"props":67,"children":68},"ul",{},[69,75,80,85,90,95,100,105],{"type":18,"tag":70,"props":71,"children":72},"li",{},[73],{"type":24,"value":74},"--output_type：指定网络输出数据类型。",{"type":18,"tag":70,"props":76,"children":77},{},[78],{"type":24,"value":79},"--input_format：输入Tensor的内存排列方式。",{"type":18,"tag":70,"props":81,"children":82},{},[83],{"type":24,"value":84},"--output：输出的模型文件路径。",{"type":18,"tag":70,"props":86,"children":87},{},[88],{"type":24,"value":89},"--soc_version：昇腾AI处理器型号。",{"type":18,"tag":70,"props":91,"children":92},{},[93],{"type":24,"value":94},"--framework：原始框架类型, 0: Caffe, 1: MindSpore, 3: TensorFlow, 5: ONNX。",{"type":18,"tag":70,"props":96,"children":97},{},[98],{"type":24,"value":99},"--save_original_model：转换后是否保留原始模型文件。",{"type":18,"tag":70,"props":101,"children":102},{},[103],{"type":24,"value":104},"--model：原始模型文件路径。",{"type":18,"tag":70,"props":106,"children":107},{},[108],{"type":24,"value":109},"--precision_mode：选择算子精度模式。",{"type":18,"tag":19,"props":111,"children":113},{"id":112},"模型推理实现",[114],{"type":24,"value":112},{"type":18,"tag":116,"props":117,"children":119},"h2",{"id":118},"_1-导入三方库",[120],{"type":24,"value":121},"1. 导入三方库",{"type":18,"tag":51,"props":123,"children":125},{"code":124},"import numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport acl\n\nimport acllite_utils as utils\nimport constants as const\nfrom acllite_model import AclLiteModel\nfrom acllite_resource import resource_list\n\nimport mindspore as ms\nimport mindspore.dataset as ds\nfrom mindspore.dataset import vision\n",[126],{"type":18,"tag":56,"props":127,"children":128},{"__ignoreMap":7},[129],{"type":24,"value":124},{"type":18,"tag":116,"props":131,"children":133},{"id":132},"_2-定义acllite资源初始化与去初始化类",[134],{"type":24,"value":135},"2. 定义acllite资源初始化与去初始化类",{"type":18,"tag":51,"props":137,"children":139},{"code":138},"class AclLiteResource:\n    \"\"\"\n    AclLiteResource\n    \"\"\"\n    def __init__(self, device_id=0):\n        self.device_id = device_id\n        self.context = None\n        self.stream = None\n        self.run_mode = None\n        \n    def init(self):\n        \"\"\"\n        init resource\n        \"\"\"\n        print(\"init resource stage:\")\n        ret = acl.init()\n\n        ret = acl.rt.set_device(self.device_id)\n        utils.check_ret(\"acl.rt.set_device\", ret)\n\n        self.context, ret = acl.rt.create_context(self.device_id)\n        utils.check_ret(\"acl.rt.create_context\", ret)\n\n        self.stream, ret = acl.rt.create_stream()\n        utils.check_ret(\"acl.rt.create_stream\", ret)\n\n        self.run_mode, ret = acl.rt.get_run_mode()\n        utils.check_ret(\"acl.rt.get_run_mode\", ret)\n\n        print(\"Init resource success\")\n\n    def __del__(self):\n        print(\"acl resource release all resource\")\n        resource_list.destroy()\n        if self.stream:\n            print(\"acl resource release stream\")\n            acl.rt.destroy_stream(self.stream)\n\n        if self.context:\n            print(\"acl resource release context\")\n            acl.rt.destroy_context(self.context)\n\n        print(\"Reset acl device \", self.device_id)\n        acl.rt.reset_device(self.device_id)\n        print(\"Release acl resource success\")\n",[140],{"type":18,"tag":56,"props":141,"children":142},{"__ignoreMap":7},[143],{"type":24,"value":138},{"type":18,"tag":116,"props":145,"children":147},{"id":146},"_3-定义分类类包含前处理推理结果展示等操作",[148],{"type":24,"value":149},"3. 定义分类类，包含前处理、推理、结果展示等操作",{"type":18,"tag":51,"props":151,"children":153},{"code":152},"class Classification(object):\n    \"\"\"\n    class for Classification\n    \"\"\"\n    def __init__(self, model_path):\n        self._model_path = model_path\n        self.device_id = 0\n        self._model = None\n\n    def init(self):\n        \"\"\"\n        Initialize\n        \"\"\"\n        # Load model\n        self._model = AclLiteModel(self._model_path)\n\n        return const.SUCCESS\n\n    def pre_process(self, input_path):\n        # 读取原始Cifar10数据集\n        dataset_predict = ds.Cifar10Dataset(dataset_dir=input_path, shuffle=False, num_samples=16)\n        # 进行一系列的数据前处理操作\n        image_trans = [\n            vision.RandomCrop((32, 32), (4, 4, 4, 4)),\n            vision.RandomHorizontalFlip(prob=0.5),\n            vision.Resize((224, 224)),\n            vision.Rescale(1.0 / 255.0, 0.0),\n            vision.Normalize([0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]),\n            vision.HWC2CHW()\n                ]\n        dataset_predict = dataset_predict.map(image_trans, 'image')\n        # 设置batch_size\n        dataset_predict = dataset_predict.batch(1)\n        data_loader = dataset_predict.create_dict_iterator()\n\n        return data_loader\n    \n\n    def inference(self, data_loader):\n        \"\"\"\n        model inference\n        \"\"\"  \n        results = []\n        for i, resized_image in enumerate(data_loader):\n            # 读物数据集图片\n            result1 = ms.Tensor(resized_image['image'])\n            result = self._model.execute([result1.asnumpy(), ])\n            \n            # 选取概率最高的那个结果\n            pred = np.argmax(result[0], axis=1)\n            results.append(pred[0])\n        \n        return results, data_loader\n\n    def show_process(self, infer_output, data_origin, input_path):\n        \"\"\"\n        show process\n        \"\"\"\n        dataset_predict = ds.Cifar10Dataset(dataset_dir=input_path, shuffle=False, num_samples=16)\n        class_dict = {0:\"airplane\", 1:\"automobile\", 2:\"bird\", 3:\"cat\", 4:\"deer\", 5:\"dog\", 6:\"frog\", 7:\"horse\", 8:\"ship\", 9:\"truck\"}\n\n        # 推理效果展示(上方为预测的结果，下方为推理效果图片)\n        plt.figure(figsize=(16, 5))\n\n        for i, resized_image in enumerate(dataset_predict.create_dict_iterator()):\n            plt.subplot(2, 8, i+1)\n            plt.title('{}'.format(class_dict[infer_output[0][i]]))\n            plt.imshow(resized_image[\"image\"].asnumpy())\n            plt.axis(\"off\")\n        plt.show()\n",[154],{"type":18,"tag":56,"props":155,"children":156},{"__ignoreMap":7},[157],{"type":24,"value":152},{"type":18,"tag":116,"props":159,"children":161},{"id":160},"_4-构造主函数串联整个代码逻辑",[162],{"type":24,"value":163},"4. 构造主函数，串联整个代码逻辑",{"type":18,"tag":51,"props":165,"children":167},{"code":166},"from download import download\n\n# 获取数据集\ndataset_url = \"https://mindspore-courses.obs.cn-north-4.myhuaweicloud.com/orange-pi-mindspore/03-ResNet50/cifar-10-batches-bin.zip\"\ndownload(dataset_url, \"./dataset\", kind=\"zip\", replace=True)\n\n# 获取模型om文件\nmodel_url = \"https://mindspore-courses.obs.cn-north-4.myhuaweicloud.com/orange-pi-mindspore/06-ShuffleNet/shufflenet.zip\"\ndownload(model_url, \"./\", kind=\"zip\", replace=True)\n\n\ndef main():\n    # 模型\n    MODEL_PATH = \"shufflenet.om\"\n    # 数据集\n    input_path = \"./dataset/cifar-10-batches-bin\"\n\n    acl_resource = AclLiteResource()  # 初始化acl资源\n    acl_resource.init()\n    \n    # Instantiation Classification object\n    classification = Classification(MODEL_PATH)  # 构造模型对象\n    \n    # init\n    ret = classification.init()  # 初始化模型类变量\n    utils.check_ret(\"classification.init \", ret)  \n    \n    # preprocess\n    crop_and_paste_image = classification.pre_process(input_path)  # 前处理\n    \n    # inference\n    result = classification.inference(crop_and_paste_image)  # 推理\n\n    # show\n    classification.show_process(result, crop_and_paste_image, input_path)  # 结果展示\n",[168],{"type":18,"tag":56,"props":169,"children":170},{"__ignoreMap":7},[171],{"type":24,"value":166},{"type":18,"tag":116,"props":173,"children":175},{"id":174},"_5-运行",[176],{"type":24,"value":177},"5. 运行",{"type":18,"tag":31,"props":179,"children":180},{},[181],{"type":24,"value":182},"运行完成后，会显示推理后的图片，运行代码如下：",{"type":18,"tag":51,"props":184,"children":186},{"code":185},"main()\n",[187],{"type":18,"tag":56,"props":188,"children":189},{"__ignoreMap":7},[190],{"type":24,"value":185},{"type":18,"tag":116,"props":192,"children":194},{"id":193},"_6-样例总结",[195],{"type":24,"value":196},"6. 样例总结",{"type":18,"tag":31,"props":198,"children":199},{},[200],{"type":24,"value":201},"我们来回顾一下以上代码，可以包括以下几个步骤：",{"type":18,"tag":203,"props":204,"children":205},"ol",{},[206,211,216,221],{"type":18,"tag":70,"props":207,"children":208},{},[209],{"type":24,"value":210},"初始化acl资源：在调用acl相关资源时，必须先初始化AscendCL，否则可能会导致后续系统内部资源初始化出错。",{"type":18,"tag":70,"props":212,"children":213},{},[214],{"type":24,"value":215},"对图片进行前处理：在此样例中，我们首先根据图片路径，使用mindspore.dataset.Cifar10Dataset接口下加载CIFAR-10数据集，再利用mindspore.dataset.vision接口对数据集进行剪切、通道转换等操作，使得模型正常推理。然后基于数据集对象创建数据迭代器。",{"type":18,"tag":70,"props":217,"children":218},{},[219],{"type":24,"value":220},"推理：利用AclLiteModel.execute接口对数据进行推理。",{"type":18,"tag":70,"props":222,"children":223},{},[224],{"type":24,"value":225},"可视化推理结果：利用plt将结果画出。",{"title":7,"searchDepth":227,"depth":227,"links":228},4,[229,231,232,233,234,235],{"id":118,"depth":230,"text":121},2,{"id":132,"depth":230,"text":135},{"id":146,"depth":230,"text":149},{"id":160,"depth":230,"text":163},{"id":174,"depth":230,"text":177},{"id":193,"depth":230,"text":196},"markdown","content:technology-blogs:zh:3250.md","content","technology-blogs/zh/3250.md","technology-blogs/zh/3250","md",1776506127929]