[{"data":1,"prerenderedAt":473},["ShallowReactive",2],{"content-query-n34worYmWv":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":467,"_id":468,"_source":469,"_file":470,"_stem":471,"_extension":472},"/technology-blogs/zh/2637","zh",false,"","MindSpore应用系列：树莓派上运行YOLOv5进行实时目标检测（二）","上一篇主要介绍了YOLOv5的一些基本知识，包括网络，数据集等，本篇主要是实际运行。","2023-07-16","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2023/07/19/636b6d697b954b83bfca81b2800a0b97.png","technology-blogs","实践",{"type":15,"children":16,"toc":464},"root",[17,25,30,35,43,48,53,64,69,74,84,89,99,104,109,114,122,130,135,140,148,153,161,166,174,179,184,189,197,202,210,215,220,228,233,265,273,278,288,301,333,343,368,373,383,391,396,404,409,414,422,427,436,441,446,454,459],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore应用系列树莓派上运行yolov5进行实时目标检测二",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":9},{"type":18,"tag":26,"props":31,"children":32},{},[33],{"type":24,"value":34},"最近更新树莓派到4B 4G, 后续均在该设备下进行测试。",{"type":18,"tag":26,"props":36,"children":37},{},[38],{"type":18,"tag":39,"props":40,"children":42},"img",{"alt":7,"src":41},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716152827.89033285251432598932909383185097:50540718032508:2400:A83663AD270E89EA8BB9D513E08216048487123EBF4A370B056604C353E37BCE.jpeg",[],{"type":18,"tag":26,"props":44,"children":45},{},[46],{"type":24,"value":47},"首先是下载模型。",{"type":18,"tag":26,"props":49,"children":50},{},[51],{"type":24,"value":52},"模型地址：",{"type":18,"tag":26,"props":54,"children":55},{},[56],{"type":18,"tag":57,"props":58,"children":62},"a",{"href":59,"rel":60},"https://gitee.com/mindspore/models/tree/master/official/cv/YOLOv5",[61],"nofollow",[63],{"type":24,"value":59},{"type":18,"tag":26,"props":65,"children":66},{},[67],{"type":24,"value":68},"这里就不进行实际训练了，太耗费时间和机器了。就直接用现成的预训练权重。",{"type":18,"tag":26,"props":70,"children":71},{},[72],{"type":24,"value":73},"预训练权重地址：",{"type":18,"tag":26,"props":75,"children":76},{},[77],{"type":18,"tag":57,"props":78,"children":81},{"href":79,"rel":80},"https://download.mindspore.cn/models/r1.10/yolov5shape640%5C_ascend%5C_v1100%5C_coco2017%5C_official%5C_cv%5C_acc36.6.ckpt",[61],[82],{"type":24,"value":83},"https://download.mindspore.cn/models/r1.10/yolov5shape640\\_ascend\\_v1100\\_coco2017\\_official\\_cv\\_acc36.6.ckpt",{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":24,"value":88},"下载完上面的文件后进行mindir导出。",{"type":18,"tag":26,"props":90,"children":91},{},[92],{"type":18,"tag":57,"props":93,"children":96},{"href":94,"rel":95},"https://gitee.com/mindspore/models/tree/master/official/cv/YOLOv5#%E7%9B%AE%E5%BD%95",[61],[97],{"type":24,"value":98},"导出MindIR",{"type":18,"tag":26,"props":100,"children":101},{},[102],{"type":24,"value":103},"python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT]",{"type":18,"tag":26,"props":105,"children":106},{},[107],{"type":24,"value":108},"export.py --ckpt_file yolov5shape640_ascend_v1100_coco2017_official_cv_acc36.6.ckpt --file_name yolov5.mindir --file_format MINDIR",{"type":18,"tag":26,"props":110,"children":111},{},[112],{"type":24,"value":113},"上面的file_name设置的不对，应该是不带后缀的名称，如果设置如上面的一样的话，最终生成的文件是yolov5.mindir_yolov5s.mindir",{"type":18,"tag":26,"props":115,"children":116},{},[117],{"type":18,"tag":39,"props":118,"children":121},{"alt":119,"src":120},"cke_3058.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716132000.00121618821758665717308352047423:50540718032508:2400:75C7E9CE50A57B5A1E69736A20B6BC9AD9DAD4F47B340EA875091E105618FADA.png",[],{"type":18,"tag":26,"props":123,"children":124},{},[125],{"type":18,"tag":39,"props":126,"children":129},{"alt":127,"src":128},"cke_5153.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716132130.39491756322849201073505699609818:50540718032508:2400:0003665F302575F3CE6AC041777BB4811D99F0066D14118A8A14DA8ACF8ADCA4.png",[],{"type":18,"tag":26,"props":131,"children":132},{},[133],{"type":24,"value":134},"缺少依赖，不是所有的模块名安装也是一样的名称。",{"type":18,"tag":26,"props":136,"children":137},{},[138],{"type":24,"value":139},"pip install pyyaml",{"type":18,"tag":26,"props":141,"children":142},{},[143],{"type":18,"tag":39,"props":144,"children":147},{"alt":145,"src":146},"cke_6658.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716132331.05884781849994383344374854950757:50540718032508:2400:E35981972DEDC3F54E68DA1D2C5B6B9FAB3F0DE834B64553199790A2B57E44CF.png",[],{"type":18,"tag":26,"props":149,"children":150},{},[151],{"type":24,"value":152},"安装后运行还是报错，报错是device target不对。去看下目录下的default_config.yaml",{"type":18,"tag":26,"props":154,"children":155},{},[156],{"type":18,"tag":39,"props":157,"children":160},{"alt":158,"src":159},"cke_8344.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716132735.50879654581960807782422619287201:50540718032508:2400:DEE71B1AD0F59E7A7AD4E3AE3A2940B2F9FD0F7D0D84345F86FBC1A93C4AB403.png",[],{"type":18,"tag":26,"props":162,"children":163},{},[164],{"type":24,"value":165},"里面默认配置的是Ascend，修改为CPU继续。",{"type":18,"tag":26,"props":167,"children":168},{},[169],{"type":18,"tag":39,"props":170,"children":173},{"alt":171,"src":172},"cke_9665.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716133133.15975493349083150988445843111620:50540718032508:2400:0832A9D8BF05C1F492FE20D82D5DA340EBA4ED1C466B539377F6B3ECA3696135.png",[],{"type":18,"tag":26,"props":175,"children":176},{},[177],{"type":24,"value":178},"导出成功。",{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":24,"value":183},"接下来就是推理了。文件夹下面有个cpp_infer, 先跑跑看吧。",{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":188},"首先是编译。",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":18,"tag":39,"props":193,"children":196},{"alt":194,"src":195},"cke_11867.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716134148.73464681079893208017908067811254:50540718032508:2400:A824A74F43940574B9A5A9DE932E56285AA3D3D705A360BD2D6067C8ABBF7592.png",[],{"type":18,"tag":26,"props":198,"children":199},{},[200],{"type":24,"value":201},"报错提示有点奇怪，先看下build.sh怎么写的吧。",{"type":18,"tag":26,"props":203,"children":204},{},[205],{"type":18,"tag":39,"props":206,"children":209},{"alt":207,"src":208},"cke_14174.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716134225.19634266615220870250107230270660:50540718032508:2400:D0BD81E1D1FAF7A123E90201ACC47A0A2C3D9A61D0DB73E026E09626F1E00E28.png",[],{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":24,"value":214},"上面的最终修改后的代码。代码的本意的如果不是ascend环境就获取cpu环境。但是最新的mindspore已经三合一了，而且realpath 在部分系统上不是返回空，而是返回异常。",{"type":18,"tag":26,"props":216,"children":217},{},[218],{"type":24,"value":219},"所以直接注释掉相关代码就可以了。",{"type":18,"tag":26,"props":221,"children":222},{},[223],{"type":18,"tag":39,"props":224,"children":227},{"alt":225,"src":226},"cke_16272.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716134258.15342300834864932961858458670290:50540718032508:2400:30F6393E29B25D6A9428955C77CFA8E577F209E0BBD7551FAF3A5A3EEB8265DF.png",[],{"type":18,"tag":26,"props":229,"children":230},{},[231],{"type":24,"value":232},"cmake没安装，运行如下命令进行安装。",{"type":18,"tag":26,"props":234,"children":235},{},[236,243,245,251,252,258,259],{"type":18,"tag":237,"props":238,"children":240},"code",{"className":239},[],[241],{"type":24,"value":242},"sudo",{"type":24,"value":244}," ",{"type":18,"tag":237,"props":246,"children":248},{"className":247},[],[249],{"type":24,"value":250},"apt",{"type":24,"value":244},{"type":18,"tag":237,"props":253,"children":255},{"className":254},[],[256],{"type":24,"value":257},"install",{"type":24,"value":244},{"type":18,"tag":237,"props":260,"children":262},{"className":261},[],[263],{"type":24,"value":264},"-y cmake",{"type":18,"tag":26,"props":266,"children":267},{},[268],{"type":18,"tag":39,"props":269,"children":272},{"alt":270,"src":271},"cke_19748.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716134444.27314083808962098676884487533837:50540718032508:2400:E974B93D7504C9BC1374BDFBD51FD79DC4EC78EFB5B6C8F72A494D845E1ECF8C.png",[],{"type":18,"tag":26,"props":274,"children":275},{},[276],{"type":24,"value":277},"缺少相关头文件。查看相关文档。",{"type":18,"tag":26,"props":279,"children":280},{},[281],{"type":18,"tag":57,"props":282,"children":285},{"href":283,"rel":284},"https://gitee.com/mindspore/models/blob/master/utils/cpp%5C_infer/README%5C_CN.md",[61],[286],{"type":24,"value":287},"https://gitee.com/mindspore/models/blob/master/utils/cpp\\_infer/README\\_CN.md",{"type":18,"tag":26,"props":289,"children":290},{},[291,293,299],{"type":24,"value":292},"在开发一个新的模型时，需要把这几个文件拷到执行目录下，并编写对应模型的",{"type":18,"tag":237,"props":294,"children":296},{"className":295},[],[297],{"type":24,"value":298},"main.cc",{"type":24,"value":300},"执行。",{"type":18,"tag":26,"props":302,"children":303},{},[304],{"type":18,"tag":305,"props":306,"children":307},"strong",{},[308,310,316,318,324,326,331],{"type":24,"value":309},"有的模型",{"type":18,"tag":237,"props":311,"children":313},{"className":312},[],[314],{"type":24,"value":315},"cpp_infer",{"type":24,"value":317},"下面没有",{"type":18,"tag":237,"props":319,"children":321},{"className":320},[],[322],{"type":24,"value":323},"common_inc",{"type":24,"value":325},"文件夹，在执行的时候需要将这个文件夹拷到",{"type":18,"tag":237,"props":327,"children":329},{"className":328},[],[330],{"type":24,"value":298},{"type":24,"value":332},"同目录下",{"type":18,"tag":26,"props":334,"children":335},{},[336,341],{"type":18,"tag":237,"props":337,"children":339},{"className":338},[],[340],{"type":24,"value":298},{"type":24,"value":342},"里面是做模型推理的脚本文件，一般包括：",{"type":18,"tag":344,"props":345,"children":346},"ul",{},[347,353,358,363],{"type":18,"tag":348,"props":349,"children":350},"li",{},[351],{"type":24,"value":352},"模型加载及构建",{"type":18,"tag":348,"props":354,"children":355},{},[356],{"type":24,"value":357},"数据集构建/bin文件加载",{"type":18,"tag":348,"props":359,"children":360},{},[361],{"type":24,"value":362},"模型推理",{"type":18,"tag":348,"props":364,"children":365},{},[366],{"type":24,"value":367},"推理结果保存",{"type":18,"tag":26,"props":369,"children":370},{},[371],{"type":24,"value":372},"然后在如下地址拷贝common_inc到build.sh的同目录下。上面的文档写的main.cc同目录，但是不同项目makefile写的不一样。",{"type":18,"tag":26,"props":374,"children":375},{},[376],{"type":18,"tag":57,"props":377,"children":380},{"href":378,"rel":379},"https://gitee.com/mindspore/models/tree/master/utils/cpp%5C_infer/example",[61],[381],{"type":24,"value":382},"https://gitee.com/mindspore/models/tree/master/utils/cpp\\_infer/example",{"type":18,"tag":26,"props":384,"children":385},{},[386],{"type":18,"tag":39,"props":387,"children":390},{"alt":388,"src":389},"cke_27508.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716140449.70960582565132539213141494865807:50540718032508:2400:73E8CC665E2B148E8E21CE63D89DD0BABAF089003BF7B775AF9DC09108C7D760.png",[],{"type":18,"tag":26,"props":392,"children":393},{},[394],{"type":24,"value":395},"编译成功，然后就可以运行了。",{"type":18,"tag":26,"props":397,"children":398},{},[399],{"type":18,"tag":39,"props":400,"children":403},{"alt":401,"src":402},"cke_30418.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716142039.26472287217298588162442610807739:50540718032508:2400:E3C66781FF8A1D187135D840BD70CF1CB6E040D45569A689A6A85BC70A4DD5D4.png",[],{"type":18,"tag":26,"props":405,"children":406},{},[407],{"type":24,"value":408},"运行报错。提升不支持CPU.",{"type":18,"tag":26,"props":410,"children":411},{},[412],{"type":24,"value":413},"于是去看源码，源码里面创建的设备类型是Ascend310， 所以会报错。这个推理代码只能运行在310上。但是文档没说明。",{"type":18,"tag":26,"props":415,"children":416},{},[417],{"type":18,"tag":39,"props":418,"children":421},{"alt":419,"src":420},"cke_33736.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716142100.19508000371981056036324164247639:50540718032508:2400:71863A43E00B1CF412693CD8774AC2E4E26FD7B2F879E5B46329B10C14848113.png",[],{"type":18,"tag":26,"props":423,"children":424},{},[425],{"type":24,"value":426},"那只能自己写推理了",{"type":18,"tag":428,"props":429,"children":431},"pre",{"code":430},"import mindspore_lite as mslite\nimport numpy as np\nimport cv2 as cv\nmodel = mslite.Model()\n#default context's target is cpu\nmodel.build_from_file(\"yolov5s.mindir\", mslite.ModelType.MINDIR)\ninputs = model.get_inputs()\n\nin_data = cv.imread(\"/home/ai/YOLOv5/cpp_infer/out/data/000000000139.jpg\")\ninputs[0].set_data_from_numpy(in_data)\noutputs = model.predict(inputs)\nprint(outputs)\n",[432],{"type":18,"tag":237,"props":433,"children":434},{"__ignoreMap":7},[435],{"type":24,"value":430},{"type":18,"tag":26,"props":437,"children":438},{},[439],{"type":24,"value":440},"先这么写着调试下看看。",{"type":18,"tag":26,"props":442,"children":443},{},[444],{"type":24,"value":445},"通过mindspore lite的云端推理。然后去下载mindspore lite。",{"type":18,"tag":26,"props":447,"children":448},{},[449],{"type":18,"tag":39,"props":450,"children":453},{"alt":451,"src":452},"cke_1738745.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230716152220.28628337726539271216236300777264:50540718032508:2400:209D0D933735D1B2AD38182F704C3728CEDC102CCDFF03A3F3A218986C0CDCA0.png",[],{"type":18,"tag":26,"props":455,"children":456},{},[457],{"type":24,"value":458},"尽然只有python3.7 可选。",{"type":18,"tag":26,"props":460,"children":461},{},[462],{"type":24,"value":463},"未完待续~",{"title":7,"searchDepth":465,"depth":465,"links":466},4,[],"markdown","content:technology-blogs:zh:2637.md","content","technology-blogs/zh/2637.md","technology-blogs/zh/2637","md",1776506122419]