日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mmdetection 使用笔记 01: 安装与简单的推理demo

發(fā)布時間:2025/3/8 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mmdetection 使用笔记 01: 安装与简单的推理demo 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

mmdetection 使用筆記 01: 安裝與簡單的推理demo

mmdetection是來自商湯和港中文聯(lián)合實驗室openmmlab推出的目標檢測工具包,與其同系列的還有基礎視覺包mmcv,圖像分類mmclassification,還有mmaction,mmaction2等等。

今天第一次嘗試使用mmdetection,先進行安裝和簡單的demo使用。

安裝

根據(jù)其github的get_started.md介紹進行安裝即可

這里介紹一下我的安裝過程,親測沒什么問題

Ubuntu 18.04

PyTorch 1.7.0

Cuda 11.1

RTX 3060 Ti

  • 為其新建一個conda環(huán)境并激活并安裝pytorch(這里就不詳細介紹了,網(wǎng)上教程很多,應該不難)

  • 安裝mmdetection

    pip install openmim mim install mmdet
  • 以上方式即可自動安裝成功mmdetection,get_started.md還提供了一種手動安裝的方法,比較麻煩,可以自己去試一下。

    demo嘗試

    這里我們根據(jù)商湯給出的教程給出的介紹嘗試了一下inference的demo。

    使用Faster RCNN進行demo圖像的目標檢測

    安裝成功后,新建一個工程即可

    mkdir mmdet_proj cd mmdet_proj

    新建一個ipynb文件

    (因為這個demo默認要用matplotlib來展示檢測結果框,而我是用vscode連接遠程服務器進行工作的,所以直接用py文件無法進行結果展示,要savefig的話應該要改一點源碼,如果是在本機進行demo嘗試的話,py文件應該也可)

    import torch from PIL import Image from mmdet.apis import inference_detector, init_detector, show_result_pyplot from mmdet.configs.faster_rcnn import faster_rcnn_r50_fpn_1x_cocoimport warnings # 我這里會報一些警告,不影響運行,但影響心情,就先把警告關掉了 warnings.filterwarnings("ignore")image = 'demo.jpeg'device = torch.device('cuda:0') config = '~/anaconda3/envs/[your_conda_env_name]/lib/python3.8/site-packages/mmdet/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py' checkpoint = 'weights/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'model = init_detector(config, checkpoint, device=device) result = inference_detector(model, image) show_result_pyplot(model, image, result, score_thr=0.9)
  • config文件是模型的一些配置,如果是直接拉的mmdet github倉庫的話,config是可以按照相對路徑拿到的,但我們是mim安裝的,所以我就找了一下這個config文件的絕對路徑拿過來了。

  • checkpoint可以從mmdet的Faster RCNN model zoo下載。

  • 運行ipynb文件即可以看到結果啦。

    可以看到檢測結果還是不錯的。

  • 然后我們看一下這個過程中的中間變量都是什么東西。

    model:

    model # print(model) for .py FasterRCNN((backbone): ResNet((conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)(relu): ReLU(inplace=True)(maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)(layer1): ResLayer(# ...# ...)init_cfg={'type': 'Pretrained', 'checkpoint': 'torchvision://resnet50'}(neck): FPN((lateral_convs): ModuleList((0): ConvModule((conv): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1)))(1): ConvModule((conv): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1)))(2): ConvModule((conv): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1)))(3): ConvModule((conv): Conv2d(2048, 256, kernel_size=(1, 1), stride=(1, 1))))(fpn_convs): ModuleList((0): ConvModule((conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))(1): ConvModule((conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))(2): ConvModule((conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))(3): ConvModule((conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)))))init_cfg={'type': 'Xavier', 'layer': 'Conv2d', 'distribution': 'uniform'}(rpn_head): RPNHead((loss_cls): CrossEntropyLoss()(loss_bbox): L1Loss()(rpn_conv): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))(rpn_cls): Conv2d(256, 3, kernel_size=(1, 1), stride=(1, 1))(rpn_reg): Conv2d(256, 12, kernel_size=(1, 1), stride=(1, 1)))init_cfg={'type': 'Normal', 'layer': 'Conv2d', 'std': 0.01}(roi_head): StandardRoIHead((bbox_roi_extractor): SingleRoIExtractor((roi_layers): ModuleList((0): RoIAlign(output_size=(7, 7), spatial_scale=0.25, sampling_ratio=0, pool_mode=avg, aligned=True, use_torchvision=False)(1): RoIAlign(output_size=(7, 7), spatial_scale=0.125, sampling_ratio=0, pool_mode=avg, aligned=True, use_torchvision=False)(2): RoIAlign(output_size=(7, 7), spatial_scale=0.0625, sampling_ratio=0, pool_mode=avg, aligned=True, use_torchvision=False)(3): RoIAlign(output_size=(7, 7), spatial_scale=0.03125, sampling_ratio=0, pool_mode=avg, aligned=True, use_torchvision=False)))(bbox_head): Shared2FCBBoxHead((loss_cls): CrossEntropyLoss()(loss_bbox): L1Loss()(fc_cls): Linear(in_features=1024, out_features=81, bias=True)(fc_reg): Linear(in_features=1024, out_features=320, bias=True)(shared_convs): ModuleList()(shared_fcs): ModuleList((0): Linear(in_features=12544, out_features=1024, bias=True)(1): Linear(in_features=1024, out_features=1024, bias=True))(cls_convs): ModuleList()(cls_fcs): ModuleList()(reg_convs): ModuleList()(reg_fcs): ModuleList()(relu): ReLU(inplace=True))init_cfg=[{'type': 'Normal', 'std': 0.01, 'override': {'name': 'fc_cls'}}, {'type': 'Normal', 'std': 0.001, 'override': {'name': 'fc_reg'}}, {'type': 'Xavier', 'layer': 'Linear', 'override': [{'name': 'shared_fcs'}, {'name': 'cls_fcs'}, {'name': 'reg_fcs'}]}]) )

    由于mmdetection也是基于pytorch的,所以可以看到我們這里的Faster RCNN的檢測模型也是一個nn.Module的子類,與我們平時自己使用pytorch定義的模型類似。只不過在mmdetection中已經(jīng)被封裝的很好了。

    result:

    type(result) # print(type(result)) for .py len(result) result[0] list80array([[3.7534842e+02, 1.1917159e+02, 3.8195099e+02, 1.3446083e+02,1.3552596e-01],[5.3236182e+02, 1.0955501e+02, 5.4052661e+02, 1.2522261e+02,8.8892356e-02],[3.6112421e+02, 1.0904940e+02, 3.6862576e+02, 1.2248302e+02,7.2088778e-02]], dtype=float32)

    這里的result就是我們檢測的結果了。其最外層是一個列表,長度為80,這是因為我們使用COCO數(shù)據(jù)集進行訓練的,共有80個類,其返回的結果就是每個類可能存在的邊界框。

    最外層列表的每個元素即是其中一類可能存在的 nnn 個邊界框。每個邊界框由四個值來表示,最后一個是置信度,而前四個則是xyhw格式(中心點坐標+高和寬)的邊界框坐標。

    show_result_pyplot函數(shù)中給出的參數(shù) score_thr 即是置信度的閾值,即各個邊界框的置信度大于該值時才會被顯示。

    我們可以將閾值 score_thr更改看一下

    result = inference_detector(model, image) show_result_pyplot(model, image, result, score_thr=0.2) # 將閾值由 0.9 改為 0.2
  • 將閾值由 0.9 改為 0.2,可以明顯看到顯示出來的邊界框個數(shù)增多了。

  • 教程中還給出了單獨使用RPN網(wǎng)絡生成提議框的展示,來幫助大家理解RPN網(wǎng)絡。我們這里只是展示mmdetecition的用法,展示RPN的流程和我們展示Faster RCNN都是一樣的,只是將config和checkpoint對應替換即可,有興趣可以自己動手是一些,這里就不再詳細介紹了。
  • 本次mmdet的安裝和初步inference demo嘗試就記錄到這里,后續(xù)會繼續(xù)記錄一下筆者使用mmdet的過程。

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結

    以上是生活随笔為你收集整理的mmdetection 使用笔记 01: 安装与简单的推理demo的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。