<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          實(shí)踐教程|無需nms,onnxruntime20行代碼玩轉(zhuǎn)RT-DETR

          共 6378字,需瀏覽 13分鐘

           ·

          2024-07-13 22:00

          ↑ 點(diǎn)擊藍(lán)字 關(guān)注極市平臺(tái)
          作者丨陳er
          來源丨GiantPandaCV
          編輯丨極市平臺(tái)

          極市導(dǎo)讀

           

          本文采用RT-DETR兩種不同風(fēng)格的onnx格式,使用onnxruntime20行代碼,無需nms操作即可實(shí)現(xiàn)簡易部署推理。>>加入極市CV技術(shù)交流群,走在計(jì)算機(jī)視覺的最前沿

          【前言】

          RT-DETR是由百度近期推出的DETR-liked目標(biāo)檢測器,該檢測器由HGNetv2、混合編碼器和帶有輔助預(yù)測頭的Transformer編碼器組成,整體結(jié)構(gòu)如下所示。

          本文將采用RT-DETR兩種不同風(fēng)格的onnx格式,使用onnxruntime20行代碼,無需nms操作即可實(shí)現(xiàn)簡易部署推理.

          一、原生onnx+ort推理方式

          使用以下命令抽取出模型配置文件和模型參數(shù)文件:

          python tools/export_model.py -c configs/rtdetr/rtdetr_hgnetv2_l_6x_coco.yml -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_hgnetv2_l_6x_coco.pdparams trt=True --output_dir=output_inference  

          轉(zhuǎn)化模型為onnx形式:

          paddle2onnx --model_dir=./output_inference/rtdetr_hgnetv2_l_6x_coco/ --model_filename model.pdmodel  --params_filename model.pdiparams --opset_version 16 --save_file rtdetr_hgnetv2_l_6x_coco.onnx  

          抽取后的onnx可視化如下:

          可以看到,除了圖像的輸入,還有另外兩個(gè)輸入頭,其中,im_shape指原輸入圖像的尺寸,scale_factor指靜態(tài)圖尺度/原輸入圖像尺度,其實(shí)就是縮放的系數(shù)。我們將batch_size固定為1,裁減掉不需要使用到的算子:

          python -m paddle2onnx.optimize --input_model rtdetr_hgnetv2_l_6x_coco.onnx --output_model rtdetr_hgnetv2_l_6x_coco_sim.onnx --input_shape_dict "{'image':[1,3,640,640]}  

          使用簡化后的onnx模型進(jìn)行推理:

          import onnxruntime as rt  
          import cv2  
          import numpy as np  
            
          sess = rt.InferenceSession("/home/aistudio/PaddleDetection/rtdetr_hgnetv2_l_6x_coco_sim.onnx")  
          img = cv2.imread("../000283.jpg")  
          org_img = img  
          im_shape = np.array([[float(img.shape[0]), float(img.shape[1])]]).astype('float32')  
          img = cv2.resize(img, (640,640))  
          scale_factor = np.array([[float(640/img.shape[0]), float(640/img.shape[1])]]).astype('float32')  
          img = img.astype(np.float32) / 255.0  
          input_img = np.transpose(img, [2, 0, 1])  
          image = input_img[np.newaxis, :, :, :]  
          result = sess.run(["reshape2_83.tmp_0","tile_3.tmp_0"], {'im_shape': im_shape, 'image': image, 'scale_factor': scale_factor})  
          for value in result[0]:  
              if value[1] > 0.5:  
                  cv2.rectangle(org_img, (int(value[2]), int(value[3])), (int(value[4]), int(value[5])), (255,0,0), 2)  
                  cv2.putText(org_img, str(int(value[0]))+": "+str(value[1]), (int(value[2]), int(value[3])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1)  
          cv2.imwrite("../result.png", org_img)  

          推理結(jié)果:

          二、野生onnx+ort推理方式

          其實(shí)通過官方onnx模型的格式可以看出,官方已經(jīng)將所有后處理步驟寫入到模型中,此時(shí)不需要額外添加后處理代碼,是一種比較省心的方式。但對(duì)于有強(qiáng)迫癥的筆者而言,對(duì)于三個(gè)輸入頭的模型實(shí)在是看著別扭,因此我更偏向于下面的這種推理方式。同樣是抽取官方模型,但此時(shí)我們將后處理的所有操作全部摘除,只保留原模型參數(shù):將模型的exclude_post_process設(shè)置為True,然后使用同樣的代碼進(jìn)行轉(zhuǎn)化:

          python tools/export_model.py -c configs/rtdetr/rtdetr_hgnetv2_l_6x_coco.yml -o weights=https://bj.bcebos.com/v1/paddledet/models/rtdetr_hgnetv2_l_6x_coco.pdparams trt=True --output_dir=output_inference_sim  

          將轉(zhuǎn)化后的pdmodel進(jìn)行可視化:

          左邊為未摘除后處理的pdmodel,右邊為摘除后的pdmodel,以分類支路為例,我們可以看到,分類支路從Sigmoid開始,已經(jīng)Sigmoid和后面的Children Node摘除干凈,那么可以轉(zhuǎn)化為onnx文件,步驟與上面一致。

          使用轉(zhuǎn)化后的onnx文件進(jìn)行推理:

          import onnxruntime as rt  
          import cv2  
          import numpy as np  
            
          sess = rt.InferenceSession("rtdetr_hgnetv2_l_6x_coco_sim2.onnx")  
          img = cv2.imread("../000283.jpg")  
          img = cv2.resize(img, (640,640))  
          image = img.astype(np.float32) / 255.0  
          input_img = np.transpose(image, [2, 0, 1])  
          image = input_img[np.newaxis, :, :, :]  
          results = sess.run(['scores',  'boxes'], {'image': image})  
          scores, boxes = [o[0] for o in results]  
          index = scores.max(-1)  
          boxes, scores = boxes[index>0.5] * 640, scores[index>0.5]  
          labels = scores.argmax(-1)  
          scores = scores.max(-1)  
          for box, score, label in zip(boxes, scores, labels):  
              cx, cy, w, h = int(box[0]), int(box[1]), int(box[2]), int(box[3])  
              cv2.rectangle(img, (cx-int(w/2), cy-int(h/2)), (cx+int(w/2), cy+int(h/2)), (0, 255, 255), 2)  
              cv2.putText(img, f'{label} : {score:.2f}', (cx-int(w/2), cy-int(h/2)-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1)  
          cv2.imwrite('../result.jpg', img)  

          推理結(jié)果:

          【結(jié)尾】

          本文介紹了RT-DETR兩種風(fēng)格的onnx格式和推理方式,不管哪種風(fēng)格,精度無任何差別,至于是使用哪款,純憑個(gè)人愛好,下一期會(huì)出一篇CNN-liked代表YOLOv8和DETR-liked代表RT-DETR在C++部署上的性能差異,在本文結(jié)尾先附上本文使用的兩個(gè)onnx模型。

          鏈接:https://pan.baidu.com/s/1AkG3uvILNQhQXeE7z8rYQw ,提取碼:pogg

          鏈接:https://pan.baidu.com/s/193Yt99CspP8vZ6ynWOl-ag ,提取碼:pogg

          參考鏈接:

          https://github.com/PaddlePaddle/PaddleDetection/tree/develop/configs/rtdetr https://aistudio.baidu.com/aistudio/projectdetail/6000200?channelType=0&channel=0 https://zhuanlan.zhihu.com/p/622940435


          公眾號(hào)后臺(tái)回復(fù)“數(shù)據(jù)集”獲取100+深度學(xué)習(xí)各方向資源整理

          極市干貨

          技術(shù)專欄:多模態(tài)大模型超詳細(xì)解讀專欄搞懂Tranformer系列ICCV2023論文解讀極市直播
          極視角動(dòng)態(tài)歡迎高校師生申報(bào)極視角2023年教育部產(chǎn)學(xué)合作協(xié)同育人項(xiàng)目新視野+智慧腦,「無人機(jī)+AI」成為道路智能巡檢好幫手!
          技術(shù)綜述:四萬字詳解Neural ODE:用神經(jīng)網(wǎng)絡(luò)去刻畫非離散的狀態(tài)變化transformer的細(xì)節(jié)到底是怎么樣的?Transformer 連環(huán)18問!

          點(diǎn)擊閱讀原文進(jìn)入CV社區(qū)

          收獲更多技術(shù)干貨

          瀏覽 81
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲性受 | 国产精品成人va在线观看在线 | 黄片免费看视频 | 国产精品久久久久久久毛片明星 | 人人搞天天摸 |