[{"data":1,"prerenderedAt":289},["ShallowReactive",2],{"content-query-cDdyNCJElw":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":283,"_id":284,"_source":285,"_file":286,"_stem":287,"_extension":288},"/technology-blogs/zh/2678","zh",false,"","MindSpore应用系列：树莓派上运行YOLOv5进行实时目标检测（三）","接上回，https://www.hiascend.com/developer/blog/details/0239124904620783059","2023-07-23","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2023/08/02/07747f5a8ae64e1c94e25887b7cf754a.png","technology-blogs","实践",{"type":15,"children":16,"toc":280},"root",[17,25,39,44,49,59,64,69,74,79,84,93,98,111,116,124,132,137,142,147,152,157,165,170,178,183,188,197,205,210,215,223,230,235,240,245,254,259,270,275],{"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,31],{"type":24,"value":30},"接上回，",{"type":18,"tag":32,"props":33,"children":37},"a",{"href":34,"rel":35},"https://www.hiascend.com/developer/blog/details/0239124904620783059",[36],"nofollow",[38],{"type":24,"value":34},{"type":18,"tag":26,"props":40,"children":41},{},[42],{"type":24,"value":43},"结尾处即使换成python3.7推理也会报错，报的是图编译的错。",{"type":18,"tag":26,"props":45,"children":46},{},[47],{"type":24,"value":48},"还是用mindspore的接口来进行mindir的推理",{"type":18,"tag":50,"props":51,"children":53},"pre",{"code":52},"import numpy as np\nimport cv2 as cv\nimport time\nimport numpy as np\nimport mindspore as ms\nimport mindspore.nn as nn\nfrom mindspore import Tensor\nfrom mindspore import context\ncontext.set_context(mode=context.GRAPH_MODE)\ngraph = ms.load(\"yolov5s.mindir\")\nnet = nn.GraphCell(graph)\nin_data = cv.imread(\"C:\\\\ai\\\\infer\\\\data\\\\images\\\\dog.jpg\")\nimg = cv.resize(in_data, (640,640), interpolation=cv.INTER_LINEAR)\nimage_np_expanded = img.astype('float32') / 255.0\ninput_tensor = Tensor(image_np_expanded)\nprint(input_tensor.shape)\nimg_dim=in_data.shape[:2]\noutput = net(input_tensor)\nprint(output[0].shape)\nprint(output[1].shape)\nprint(output[2].shape)\n",[54],{"type":18,"tag":55,"props":56,"children":57},"code",{"__ignoreMap":7},[58],{"type":24,"value":52},{"type":18,"tag":26,"props":60,"children":61},{},[62],{"type":24,"value":63},"推理输出的shape分别为",{"type":18,"tag":26,"props":65,"children":66},{},[67],{"type":24,"value":68},"(1, 20, 20, 3, 85)",{"type":18,"tag":26,"props":70,"children":71},{},[72],{"type":24,"value":73},"(1, 40, 40, 3, 85)",{"type":18,"tag":26,"props":75,"children":76},{},[77],{"type":24,"value":78},"(1, 80, 80, 3, 85)",{"type":18,"tag":26,"props":80,"children":81},{},[82],{"type":24,"value":83},"这个就是对应下图的输出，略有差异是因为输入图像的shape不一致导致的。",{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":18,"tag":88,"props":89,"children":92},"img",{"alt":90,"src":91},"image.png","https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/f94/7f6/9e8/95b919d24bf947f69e892b965a1b4277.20230723102450.11721273015160638151745038932722:50540801030653:2400:F863BDA5D017918FEF895F4145D433F443CDFFDB071261F201BFB2BE5EB33928.png",[],{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":24,"value":97},"对应mindspore",{"type":18,"tag":26,"props":99,"children":100},{},[101,103,109],{"type":24,"value":102},"三个特征图中，最大的80, 80 负责检测小目标，而对应到640*640上，每格特征图的感受野是",{"type":18,"tag":104,"props":105,"children":106},"strong",{},[107],{"type":24,"value":108},"640",{"type":24,"value":110},"/80=8*8****大小。",{"type":18,"tag":26,"props":112,"children":113},{},[114],{"type":24,"value":115},"40, 40 负责检测中等目标，20, 20 负责检测大目标。",{"type":18,"tag":26,"props":117,"children":118},{},[119],{"type":18,"tag":104,"props":120,"children":121},{},[122],{"type":24,"value":123},"输出物体边框、置信度、物体分类",{"type":18,"tag":26,"props":125,"children":126},{},[127],{"type":18,"tag":104,"props":128,"children":129},{},[130],{"type":24,"value":131},"1.输出物体边框",{"type":18,"tag":26,"props":133,"children":134},{},[135],{"type":24,"value":136},"每层特征图最终都会经过1乘1卷积，变成（5+分类数）乘3个通道",{"type":18,"tag":26,"props":138,"children":139},{},[140],{"type":24,"value":141},"每一层都有3个anchor，每一anchor对应的（5+分类数）个通道，假设分类数为2，那一共就是7个通道了，这7个通道分别是xywh（4个通道)，置信度（1个通道），分类（此处2分类，就是2个通道）",{"type":18,"tag":26,"props":143,"children":144},{},[145],{"type":24,"value":146},"对应上面的输出，分类数是80，所以shape是85。",{"type":18,"tag":26,"props":148,"children":149},{},[150],{"type":24,"value":151},"物体边框的4个值，x,y,w,h，不过这个x，y并不直接是物体框中心点的坐标，而是它相对于自身所处的格子左上角的偏移，然后再把这个中心点的相对值作用到原图的尺度得到最终的坐标。",{"type":18,"tag":26,"props":153,"children":154},{},[155],{"type":24,"value":156},"然后就是宽高，宽高也不是直接预测出物体边框的宽高，而是基于anchor的，预测出来的值会乘上anchor的宽高得出最终的宽高。",{"type":18,"tag":26,"props":158,"children":159},{},[160],{"type":18,"tag":104,"props":161,"children":162},{},[163],{"type":24,"value":164},"2. 置信度",{"type":18,"tag":26,"props":166,"children":167},{},[168],{"type":24,"value":169},"代表预测出的物体边框的可信度。在推理脚本中，最终得到的置信度会再乘上最大的分类得分。",{"type":18,"tag":26,"props":171,"children":172},{},[173],{"type":18,"tag":104,"props":174,"children":175},{},[176],{"type":24,"value":177},"3.分类",{"type":18,"tag":26,"props":179,"children":180},{},[181],{"type":24,"value":182},"有几个分类，就会再加几个通道，分别代表对应分类的概率，在计算损失的时候，标签对应分类所在通道的值为1，其它都为0，然后分别计算BCE损失。",{"type":18,"tag":26,"props":184,"children":185},{},[186],{"type":24,"value":187},"接下来就是处理输出的数据了。有现成的api可用。",{"type":18,"tag":26,"props":189,"children":190},{},[191],{"type":18,"tag":32,"props":192,"children":195},{"href":193,"rel":194},"https://gitee.com/mindspore/models/blob/master/official/cv/YOLOv5/infer/sdk/api-server/postprocess.py",[36],[196],{"type":24,"value":193},{"type":18,"tag":50,"props":198,"children":200},{"code":199},"img_dim = in_data.shape[:2]\nfrom model_utils.config import config\nfrom src.logger import get_logger\nfrom postprocess import DetectionEngine\nconfig.logger = get_logger(config.output_dir, 0)\n# init detection engine\ndetection = DetectionEngine(config)\ndetection.detect(output,1,img_dim,139)\nboxes=detection.do_nms_for_results()\nprint(boxes)\n\n\nclass DetectionEngine:\n    \"\"\"Detection engine.\"\"\"\n\n    def __init__(self, args_detection):\n        self.ignore_threshold = args_detection.ignore_threshold\n        self.args = args_detection\n        self.labels = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',\n                       'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat',\n                       'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack',\n                       'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',\n                       'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket',\n                       'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',\n                       'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',\n                       'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',\n                       'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book',\n                       'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']\n        self.num_classes = len(self.labels)\n        self.results = {}\n        self.file_path = ''\n        self.ann_file = args_detection.val_ann_file\n        self._coco = COCO(self.ann_file)\n        self._img_ids = list(sorted(self._coco.imgs.keys()))\n        self.det_boxes = []\n        self.nms_thresh = args_detection.eval_nms_thresh\n        self.multi_label = args_detection.multi_label\n        self.multi_label_thresh = args_detection.multi_label_thresh\n        self.coco_catIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27,\n                            28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53,\n                            54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80,\n                            81, 82, 84, 85, 86, 87, 88, 89, 90]\n\n\nDetectionEngine需要输入一些配置参数，所以直接就用model_utils.config里面的了。\n",[201],{"type":18,"tag":55,"props":202,"children":203},{"__ignoreMap":7},[204],{"type":24,"value":199},{"type":18,"tag":26,"props":206,"children":207},{},[208],{"type":24,"value":209},"DetectionEngine里面有几个参数有改动。",{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":24,"value":214},"然后就是detect方法了，它是batch数据处理，我就只有一张图，就修改下相关代码。",{"type":18,"tag":50,"props":216,"children":218},{"code":217}," #ori_w, ori_h = img_shape[batch_id]\n                ori_w, ori_h = img_shape\n                #img_id = int(image_id[batch_id])\n                img_id = int(image_id)\n",[219],{"type":18,"tag":55,"props":220,"children":221},{"__ignoreMap":7},[222],{"type":24,"value":217},{"type":18,"tag":26,"props":224,"children":225},{},[226],{"type":18,"tag":88,"props":227,"children":229},{"alt":7,"src":228},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2023/08/02/f5b49aa4dfef45b1a7d7b972b1ebbcf8.png",[],{"type":18,"tag":26,"props":231,"children":232},{},[233],{"type":24,"value":234},"但是以上代码经调试，detect获取的值为空，这个就不知道是模型权重问题还是什么了，毕竟权重文件不一定是匹配的。",{"type":18,"tag":26,"props":236,"children":237},{},[238],{"type":24,"value":239},"看来又是一个弯路。",{"type":18,"tag":26,"props":241,"children":242},{},[243],{"type":24,"value":244},"在昇思MindSpore 2.0发布公告里面看到了MindSpore Yolo套件。",{"type":18,"tag":26,"props":246,"children":247},{},[248],{"type":18,"tag":32,"props":249,"children":252},{"href":250,"rel":251},"https://www.mindspore.cn/news/newschildren?id=2568",[36],[253],{"type":24,"value":250},{"type":18,"tag":26,"props":255,"children":256},{},[257],{"type":24,"value":258},"MindSpore YOLO套件是基于昇思MindSpore的YOLO系列算法套件，统一了各类 YOLO 算法模块的实现，通过提供YOLO系列算法常用的模块接口(数据处理/模型构建/优化器等)，简化模型构建和训练流程。当前提供YOLOv3/v4/v5/v7/v8/x 等6+基础模型，可快速复现和迁移；",{"type":18,"tag":26,"props":260,"children":261},{},[262,264],{"type":24,"value":263},"套件代码地址：",{"type":18,"tag":32,"props":265,"children":268},{"href":266,"rel":267},"https://github.com/mindspore-lab/mindyolo",[36],[269],{"type":24,"value":266},{"type":18,"tag":26,"props":271,"children":272},{},[273],{"type":24,"value":274},"然后权重文件也是官方提供了，看来只能再去看看MindYOLO。",{"type":18,"tag":26,"props":276,"children":277},{},[278],{"type":24,"value":279},"未完待续~",{"title":7,"searchDepth":281,"depth":281,"links":282},4,[],"markdown","content:technology-blogs:zh:2678.md","content","technology-blogs/zh/2678.md","technology-blogs/zh/2678","md",1776506122716]