sql注入网站建设百度云,wordpress官网视频教程,seo推广营销网站,东莞旅游必去十大景点mmdetection 使用笔记 01: 安装与简单的推理demo
mmdetection是来自商汤和港中文联合实验室openmmlab推出的目标检测工具包#xff0c;与其同系列的还有基础视觉包mmcv#xff0c;图像分类mmclassification#xff0c;还有mmaction#xff0c;mmaction2等等。
今天第一次…mmdetection 使用笔记 01: 安装与简单的推理demo
mmdetection是来自商汤和港中文联合实验室openmmlab推出的目标检测工具包与其同系列的还有基础视觉包mmcv图像分类mmclassification还有mmactionmmaction2等等。
今天第一次尝试使用mmdetection先进行安装和简单的demo使用。
安装
根据其github的get_started.md介绍进行安装即可
这里介绍一下我的安装过程亲测没什么问题 Ubuntu 18.04 PyTorch 1.7.0 Cuda 11.1 RTX 3060 Ti 为其新建一个conda环境并激活并安装pytorch这里就不详细介绍了网上教程很多应该不难 安装mmdetection pip install openmim
mim install mmdet以上方式即可自动安装成功mmdetectionget_started.md还提供了一种手动安装的方法比较麻烦可以自己去试一下。
demo尝试
这里我们根据商汤给出的教程给出的介绍尝试了一下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.jpegdevice 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.pthmodel init_detector(config, checkpoint, devicedevice)
result inference_detector(model, image)
show_result_pyplot(model, image, result, score_thr0.9)config文件是模型的一些配置如果是直接拉的mmdet github仓库的话config是可以按照相对路径拿到的但我们是mim安装的所以我就找了一下这个config文件的绝对路径拿过来了。 checkpoint可以从mmdet的Faster RCNN model zoo下载。 运行ipynb文件即可以看到结果啦。 可以看到检测结果还是不错的。 然后我们看一下这个过程中的中间变量都是什么东西。 model model # print(model) for .pyFasterRCNN((backbone): ResNet((conv1): Conv2d(3, 64, kernel_size(7, 7), stride(2, 2), padding(3, 3), biasFalse)(bn1): BatchNorm2d(64, eps1e-05, momentum0.1, affineTrue, track_running_statsTrue)(relu): ReLU(inplaceTrue)(maxpool): MaxPool2d(kernel_size3, stride2, padding1, dilation1, ceil_modeFalse)(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_scale0.25, sampling_ratio0, pool_modeavg, alignedTrue, use_torchvisionFalse)(1): RoIAlign(output_size(7, 7), spatial_scale0.125, sampling_ratio0, pool_modeavg, alignedTrue, use_torchvisionFalse)(2): RoIAlign(output_size(7, 7), spatial_scale0.0625, sampling_ratio0, pool_modeavg, alignedTrue, use_torchvisionFalse)(3): RoIAlign(output_size(7, 7), spatial_scale0.03125, sampling_ratio0, pool_modeavg, alignedTrue, use_torchvisionFalse)))(bbox_head): Shared2FCBBoxHead((loss_cls): CrossEntropyLoss()(loss_bbox): L1Loss()(fc_cls): Linear(in_features1024, out_features81, biasTrue)(fc_reg): Linear(in_features1024, out_features320, biasTrue)(shared_convs): ModuleList()(shared_fcs): ModuleList((0): Linear(in_features12544, out_features1024, biasTrue)(1): Linear(in_features1024, out_features1024, biasTrue))(cls_convs): ModuleList()(cls_fcs): ModuleList()(reg_convs): ModuleList()(reg_fcs): ModuleList()(relu): ReLU(inplaceTrue))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中已经被封装的很好了。 result type(result) # print(type(result)) for .py
len(result)
result[0]list80array([[3.7534842e02, 1.1917159e02, 3.8195099e02, 1.3446083e02,1.3552596e-01],[5.3236182e02, 1.0955501e02, 5.4052661e02, 1.2522261e02,8.8892356e-02],[3.6112421e02, 1.0904940e02, 3.6862576e02, 1.2248302e02,7.2088778e-02]], dtypefloat32)这里的result就是我们检测的结果了。其最外层是一个列表长度为80这是因为我们使用COCO数据集进行训练的共有80个类其返回的结果就是每个类可能存在的边界框。 最外层列表的每个元素即是其中一类可能存在的 nnn 个边界框。每个边界框由四个值来表示最后一个是置信度而前四个则是xyhw格式中心点坐标高和宽的边界框坐标。 show_result_pyplot函数中给出的参数 score_thr 即是置信度的阈值即各个边界框的置信度大于该值时才会被显示。 我们可以将阈值 score_thr更改看一下 result inference_detector(model, image)
show_result_pyplot(model, image, result, score_thr0.2) # 将阈值由 0.9 改为 0.2将阈值由 0.9 改为 0.2可以明显看到显示出来的边界框个数增多了。
教程中还给出了单独使用RPN网络生成提议框的展示来帮助大家理解RPN网络。我们这里只是展示mmdetecition的用法展示RPN的流程和我们展示Faster RCNN都是一样的只是将config和checkpoint对应替换即可有兴趣可以自己动手是一些这里就不再详细介绍了。
本次mmdet的安装和初步inference demo尝试就记录到这里后续会继续记录一下笔者使用mmdet的过程。