[{"data":1,"prerenderedAt":242},["ShallowReactive",2],{"content-query-xe3TYwQAzJ":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/3249","zh",false,"","基于MindSpore案例的香橙派开发板离线推理实践--使用Pix2Pix实现图像转换","功能：使用Pix2Pix实现图像转换。 样例输入：mindrecord格式的文件。 样例输出：风格转换后的图象。","2024-07-30","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/08/02/361ca858452b42c88ff6df2b6181da7e.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案例的香橙派开发板离线推理实践-使用pix2pix实现图像转换",[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=\"./pix2pix\" --soc_version=Ascend310B4 --framework=1 --save_original_model=false --model=\"./pix2pix.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\n\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 Conversion(object):\n    \"\"\"\n    class for Conversion\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        \n        dataset = ds.MindDataset(input_path, columns_list=[\"input_images\", \"target_images\"], shuffle=True)\n        data_iter = next(dataset.create_dict_iterator(output_numpy=True))\n        result1 = ms.Tensor(data_iter[\"input_images\"])\n\n        return result1, data_iter\n    \n    def inference(self, result1):\n        \"\"\"\n        model inference\n        \"\"\"\n        predict_show_list  = self._model.execute([result1.asnumpy(), ])\n        predict_show = np.array(predict_show_list)\n        \n        return predict_show      \n\n    def show_process(self, predict_show, data_iter):\n        \"\"\"\n        show process\n        \"\"\"\n        plt.figure(figsize=(10, 3), dpi=140)\n        for i in range(10):\n            plt.subplot(2, 10, i + 1)\n            plt.imshow(((data_iter[\"input_images\"][i]).transpose(1, 2, 0) + 1) / 2)\n            plt.axis(\"off\")\n            plt.subplots_adjust(wspace=0.05, hspace=0.02)\n            plt.subplot(2, 10, i + 11)\n            plt.imshow((predict_show[0][i].transpose(1, 2, 0) + 1) / 2)\n            plt.axis(\"off\")\n            plt.subplots_adjust(wspace=0.05, hspace=0.02)\n        plt.show()\n    \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# 获取数据集\ndataset_url = \"https://mindspore-courses.obs.cn-north-4.myhuaweicloud.com/orange-pi-mindspore/08-Pix2pix/dataset.zip\"\ndownload(dataset_url, \"./\", kind=\"zip\", replace=True)\n# 获取模型om文件\nmodel_url = \"https://mindspore-courses.obs.cn-north-4.myhuaweicloud.com/orange-pi-mindspore/08-Pix2pix/pix2pix.zip\"\ndownload(model_url, \"./\", kind=\"zip\", replace=True)\n\n\ndef main():\n    MODEL_PATH = \"pix2pix.om\"\n    input_path = \"./dataset/pix2pix_data.mindrecord\"\n\n    acl_resource = AclLiteResource()  # 初始化acl资源\n    acl_resource.init()\n    \n    # instantiation Conversion object\n    conversion = Conversion(MODEL_PATH)  # 构造模型对象\n    \n    # init\n    ret = conversion .init()  # 初始化模型类变量\n    utils.check_ret(\"conversion.init \", ret)  \n\n    # read image\n    image_pre, data_iter = conversion.pre_process(input_path)  # 数据读取\n    \n    # inference\n    predict_show = conversion.inference(image_pre)  # 推理\n\n    # show\n    conversion.show_process(predict_show , data_iter)  # 结果展示\n    \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.MindDataset接口下加载测试用数据集。然后基于数据集对象使用mindspore中的create_dict_iterator接口创建数据迭代器。",{"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:3249.md","content","technology-blogs/zh/3249.md","technology-blogs/zh/3249","md",1776506127885]