【Detectron2】使用 Detectron2 训练基于 coco 数据集的目标检测网络

2023-11-11

一、安装 Detectron2

初次接触 Detectron2 的话,可能安装会遇到些坑,大家可以按下面的方式逐步安装,基本不会有什么问题。如果用到别的数据库的话,安装对应的api就可以。

# 使用 conda 创建虚拟环境
conda create -n detectron2 python=3.7
# 激活虚拟环境
conda activate detectron2
# 安装pytorch,注意对应的 cuda
conda install pytorch=1.6 torchvision cudatoolkit=10.2 -c pytorch
conda install opencv-python
pip install cython
# 安装cocopai
https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI/
make
python setup.py install --user
cd ../../
# 安装 DCNv2
git clone https://github.com/CharlesShang/DCNv2.git
cd DCNv2
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
./make.sh
# 克隆detectron2包,并安装
git clone https://github.com/facebookresearch/detectron2.git
python setup.py build develop
pip install -e .

二、软连接 coco 数据集

克隆下来的 detectron2 是下面的结构,为了不混淆,我自己命名成了 detectron2_test:

在这里插入图片描述
如果不想在 coco.py 中修改文件路径的话,可以把现有的 coco 数据集软连接到 detectron2_test/detectron2/data/datasets/ 中即可:

$ cd detectron2_test/detectron2/data/datasets/
$ ln -s xxx/coco .

三、训练

$ pwd
detectron2_test
$ ./tools/train_net.py --config-file configs/COCO-Detection/faster_rcnn_R_50_C4_1x.yaml

四、数据集相关参数

数据集的相关参数是模型训练的很重要的一部分,如果需要训练自己的数据集的话,需要在下面的三个文件中,修改成自己数据集中对应的类别个数、类别名称和 json 路径等。

# 1
detectron2_test/detectron2/data/datasets/coco.py

在这里插入图片描述

# 2
detectron2_test/detectron2/data/datasets/builtin.py

在这里插入图片描述

# 3
detectron2_test/detectron2/data/datasets/builtin_meta.py

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
coco 数据集介绍:

coco 数据集每个类别数量:

在这里插入图片描述

五、输出结果路径

detectron2_test/configs/defaults.py

六、COCO 数据集简介

COCO 数据集下载:

wget http://images.cocodataset.org/zips/train2017.zip
wget http://images.cocodataset.org/zips/val2017.zip
wget http://images.cocodataset.org/zips/test2017.zip
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip

coco 数据集的结构如下,共由5个关键字段构成,每个字段的内容如下面代码打印所示:

  • info
  • licenses
  • images
  • annotations
  • categories
file = io.open('instances_val2017.json', 'r', encoding='utf-8')
content = json.load(file)
print(content.keys())
>>>
dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
print(content['info'])
>>>
{'description': 'COCO 2017 Dataset', 'url': 'http://cocodataset.org', 'version': '1.0', 'year': 2017, 'contributor': 'COCO Consortium', 'date_created': '2017/09/01'}
print(content['licenses'])
>>>[
{'url': 'http://creativecommons.org/licenses/by-nc-sa/2.0/', 'id': 1, 'name': 'Attribution-NonCommercial-ShareAlike License'}, 
{'url': 'http://creativecommons.org/licenses/by-nc/2.0/', 'id': 2, 'name': 'Attribution-NonCommercial License'}, 
{'url': 'http://creativecommons.org/licenses/by-nc-nd/2.0/', 'id': 3, 'name': 'Attribution-NonCommercial-NoDerivs License'}, 
{'url': 'http://creativecommons.org/licenses/by/2.0/', 'id': 4, 'name': 'Attribution License'}, 
{'url': 'http://creativecommons.org/licenses/by-sa/2.0/', 'id': 5, 'name': 'Attribution-ShareAlike License'}, 
{'url': 'http://creativecommons.org/licenses/by-nd/2.0/', 'id': 6, 'name': 'Attribution-NoDerivs License'}, 
{'url': 'http://flickr.com/commons/usage/', 'id': 7, 'name': 'No known copyright restrictions'}, 
{'url': 'http://www.usa.gov/copyright.shtml', 'id': 8, 'name': 'United States Government Work'}]
print(content['images'])
>>>[
{'license': 1, 'file_name': '000000548246.jpg', 'coco_url': 'http://images.cocodataset.org/val2017/000000548246.jpg', 'height': 428, 'width': 640, 'date_captured': '2013-11-23 03:52:52', 'flickr_url': 'http://farm4.staticflickr.com/3115/2905881071_5b16058d7b_z.jpg', 'id': 548246}, ...]
print(content['annotations'][0]) # type = list, len(content['annotations'])=36781
>>>{
'segmentation': [[510.66, 423.01, 511.72, 420.03, 510.45, 416.0, 510.34, 413.02, 510.77, 410.26, 510.77, 407.5, 510.34, 405.16, 511.51, 402.83, 511.41, 400.49, 510.24, 398.16, 509.39, 397.31, 504.61, 399.22, 502.17, 399.64, 500.89, 401.66, 500.47, 402.08, 499.09, 401.87, 495.79, 401.98, 490.59, 401.77, 488.79, 401.77, 485.39, 398.58, 483.9, 397.31, 481.56, 396.35, 478.48, 395.93, 476.68, 396.03, 475.4, 396.77, 473.92, 398.79, 473.28, 399.96, 473.49, 401.87, 474.56, 403.47, 473.07, 405.59, 473.39, 407.71, 476.68, 409.41, 479.23, 409.73, 481.56, 410.69, 480.4, 411.85, 481.35, 414.93, 479.86, 418.65, 477.32, 420.03, 476.04, 422.58, 479.02, 422.58, 480.29, 423.01, 483.79, 419.93, 486.66, 416.21, 490.06, 415.57, 492.18, 416.85, 491.65, 420.24, 492.82, 422.9, 493.56, 424.39, 496.43, 424.6, 498.02, 423.01, 498.13, 421.31, 497.07, 420.03, 497.07, 415.15, 496.33, 414.51, 501.1, 411.96, 502.06, 411.32, 503.02, 415.04, 503.33, 418.12, 501.1, 420.24, 498.98, 421.63, 500.47, 424.39, 505.03, 423.32, 506.2, 421.31, 507.69, 419.5, 506.31, 423.32, 510.03, 423.01, 510.45, 423.01]], 
'area': 702.1057499999998, 
'iscrowd': 0, 
'image_id': 289343, 
'bbox': [473.07, 395.93, 38.65, 28.67], 
'category_id': 18, 
'id': 1768}
print(content['categories']) # len(content['categories']=80)
>>>[
{'supercategory': 'person', 'id': 1, 'name': 'person'}, 
{'supercategory': 'vehicle', 'id': 2, 'name': 'bicycle'}, 
{'supercategory': 'vehicle', 'id': 3, 'name': 'car'}, 
{'supercategory': 'vehicle', 'id': 4, 'name': 'motorcycle'}, 
{'supercategory': 'vehicle', 'id': 5, 'name': 'airplane'}, 
{'supercategory': 'vehicle', 'id': 6, 'name': 'bus'}, 
{'supercategory': 'vehicle', 'id': 7, 'name': 'train'}, 
{'supercategory': 'vehicle', 'id': 8, 'name': 'truck'}, 
{'supercategory': 'vehicle', 'id': 9, 'name': 'boat'}, 
{'supercategory': 'outdoor', 'id': 10, 'name': 'traffic light'}, 
{'supercategory': 'outdoor', 'id': 11, 'name': 'fire hydrant'}, 
{'supercategory': 'outdoor', 'id': 13, 'name': 'stop sign'}, 
{'supercategory': 'outdoor', 'id': 14, 'name': 'parking meter'}, 
{'supercategory': 'outdoor', 'id': 15, 'name': 'bench'}, 
{'supercategory': 'animal', 'id': 16, 'name': 'bird'}, 
{'supercategory': 'animal', 'id': 17, 'name': 'cat'}, 
{'supercategory': 'animal', 'id': 18, 'name': 'dog'}, 
{'supercategory': 'animal', 'id': 19, 'name': 'horse'}, 
{'supercategory': 'animal', 'id': 20, 'name': 'sheep'}, 
{'supercategory': 'animal', 'id': 21, 'name': 'cow'}, 
{'supercategory': 'animal', 'id': 22, 'name': 'elephant'}, 
{'supercategory': 'animal', 'id': 23, 'name': 'bear'}, 
{'supercategory': 'animal', 'id': 24, 'name': 'zebra'}, 
{'supercategory': 'animal', 'id': 25, 'name': 'giraffe'}, 
{'supercategory': 'accessory', 'id': 27, 'name': 'backpack'}, 
{'supercategory': 'accessory', 'id': 28, 'name': 'umbrella'}, 
{'supercategory': 'accessory', 'id': 31, 'name': 'handbag'}, 
{'supercategory': 'accessory', 'id': 32, 'name': 'tie'}, 
{'supercategory': 'accessory', 'id': 33, 'name': 'suitcase'}, 
{'supercategory': 'sports', 'id': 34, 'name': 'frisbee'}, 
{'supercategory': 'sports', 'id': 35, 'name': 'skis'}, 
{'supercategory': 'sports', 'id': 36, 'name': 'snowboard'}, 
{'supercategory': 'sports', 'id': 37, 'name': 'sports ball'}, 
{'supercategory': 'sports', 'id': 38, 'name': 'kite'}, 
{'supercategory': 'sports', 'id': 39, 'name': 'baseball bat'}, 
{'supercategory': 'sports', 'id': 40, 'name': 'baseball glove'}, 
{'supercategory': 'sports', 'id': 41, 'name': 'skateboard'}, 
{'supercategory': 'sports', 'id': 42, 'name': 'surfboard'}, 
{'supercategory': 'sports', 'id': 43, 'name': 'tennis racket'}, 
{'supercategory': 'kitchen', 'id': 44, 'name': 'bottle'}, 
{'supercategory': 'kitchen', 'id': 46, 'name': 'wine glass'}, 
{'supercategory': 'kitchen', 'id': 47, 'name': 'cup'},
{'supercategory': 'kitchen', 'id': 48, 'name': 'fork'}, 
{'supercategory': 'kitchen', 'id': 49, 'name': 'knife'}, 
{'supercategory': 'kitchen', 'id': 50, 'name': 'spoon'}, 
{'supercategory': 'kitchen', 'id': 51, 'name': 'bowl'}, 
{'supercategory': 'food', 'id': 52, 'name': 'banana'}, 
{'supercategory': 'food', 'id': 53, 'name': 'apple'}, 
{'supercategory': 'food', 'id': 54, 'name': 'sandwich'},
{'supercategory': 'food', 'id': 55, 'name': 'orange'}, 
{'supercategory': 'food', 'id': 56, 'name': 'broccoli'}, 
{'supercategory': 'food', 'id': 57, 'name': 'carrot'}, 
{'supercategory': 'food', 'id': 58, 'name': 'hot dog'}, 
{'supercategory': 'food', 'id': 59, 'name': 'pizza'}, 
{'supercategory': 'food', 'id': 60, 'name': 'donut'}, 
{'supercategory': 'food', 'id': 61, 'name': 'cake'}, 
{'supercategory': 'furniture', 'id': 62, 'name': 'chair'}, 
{'supercategory': 'furniture', 'id': 63, 'name': 'couch'},
{'supercategory': 'furniture', 'id': 64, 'name': 'potted plant'}, 
{'supercategory': 'furniture', 'id': 65, 'name': 'bed'}, 
{'supercategory': 'furniture', 'id': 67, 'name': 'dining table'}, 
{'supercategory': 'furniture', 'id': 70, 'name': 'toilet'}, 
{'supercategory': 'electronic', 'id': 72, 'name': 'tv'}, 
{'supercategory': 'electronic', 'id': 73, 'name': 'laptop'}, 
{'supercategory': 'electronic', 'id': 74, 'name': 'mouse'},   
{'supercategory': 'electronic', 'id': 75, 'name': 'remote'}, 
{'supercategory': 'electronic', 'id': 76, 'name': 'keyboard'},
{'supercategory': 'electronic', 'id': 77, 'name': 'cell phone'},
{'supercategory': 'appliance', 'id': 78, 'name': 'microwave'}, 
{'supercategory': 'appliance', 'id': 79, 'name': 'oven'}, 
{'supercategory': 'appliance', 'id': 80, 'name': 'toaster'}, 
{'supercategory': 'appliance', 'id': 81, 'name': 'sink'}, 
{'supercategory': 'appliance', 'id': 82, 'name': 'refrigerator'}, 
{'supercategory': 'indoor', 'id': 84, 'name': 'book'}, 
{'supercategory': 'indoor', 'id': 85, 'name': 'clock'}, 
{'supercategory': 'indoor', 'id': 86, 'name': 'vase'}, 
{'supercategory': 'indoor', 'id': 87, 'name': 'scissors'}, 
{'supercategory': 'indoor', 'id': 88, 'name': 'teddy bear'}, 
{'supercategory': 'indoor', 'id': 89, 'name': 'hair drier'},
{'supercategory': 'indoor', 'id': 90, 'name': 'toothbrush'}]

七、模型相关参数

MODEL:
  META_ARCHITECTURE: "GeneralizedRCNN"
  BACKBONE:
    NAME: "build_resnet_fpn_backbone"
  RESNETS:
    OUT_FEATURES: ["res2", "res3", "res4", "res5"]
  FPN:
    IN_FEATURES: ["res2", "res3", "res4", "res5"]
  ANCHOR_GENERATOR:
    SIZES: [[32], [64], [128], [256], [512]]  # One size for each in feature map
    ASPECT_RATIOS: [[0.5, 1.0, 2.0]]  # Three aspect ratios (same for all in feature maps)
  RPN:
    IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"]
    PRE_NMS_TOPK_TRAIN: 2000  # Per FPN level
    PRE_NMS_TOPK_TEST: 1000  # Per FPN level
    # Detectron1 uses 2000 proposals per-batch,
    # (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue)
    # which is approximately 1000 proposals per-image since the default batch size for FPN is 2.
    POST_NMS_TOPK_TRAIN: 1000
    POST_NMS_TOPK_TEST: 1000
  ROI_HEADS:
    NAME: "StandardROIHeads"
    IN_FEATURES: ["p2", "p3", "p4", "p5"]
  ROI_BOX_HEAD:
    NAME: "FastRCNNConvFCHead"
    NUM_FC: 2
    POOLER_RESOLUTION: 7
  ROI_MASK_HEAD:
    NAME: "MaskRCNNConvUpsampleHead"
    NUM_CONV: 4
    POOLER_RESOLUTION: 14
DATASETS:
  TRAIN: ("coco_2017_train",)
  TEST: ("coco_2017_val",)
SOLVER:
  IMS_PER_BATCH: 16
  BASE_LR: 0.02
  STEPS: (60000, 80000)
  MAX_ITER: 90000
INPUT:
  MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800)
VERSION: 2

八、可视化结果

demo/demo.py
python demo/demo.py --config-file configs/COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml --input /mnt/nfs-storage/train_data/coco/val2017/*.jpg   --output coco_val_test --opt MODEL.WEIGHTS output/model_0049999.pth

如果类别不是coco的80类的话,需要修改下面两个地方为自己的类别数量:

detectron2_test/detectron2/configs/default.py

在这里插入图片描述
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Detectron2】使用 Detectron2 训练基于 coco 数据集的目标检测网络 的相关文章

随机推荐

  • 基于Python+Flask实现一个简易网页验证码登录系统案例

    在当今的互联网世界中 为了防止恶意访问 许多网站在登录和注册表单中都采用了验证码技术 验证码可以防止机器人自动提交表单 确保提交行为背后有一个真实的人类用户 本文将向您展示如何使用Python的Flask框架来创建一个简单的验证码登录系统
  • sparksql报错

    执行时报错 org apache spark sql AnalysisException Unable to generate an encoder for inner class cn itcast spark sql Intro Per
  • linux常用文本编辑命令

    cat 命令 cat 命令用于查看纯文本文件 内容较少的 格式为 cat 选项 文件 cat命令常用于查看内容较少的纯文本文件 more 命令 more 命令用于查看纯文本文件 内容较多的 格式为 more 选项 文件 more 命令会在最
  • Ubuntu16.04 LTS自带的Python3.5升级到Python3.7详细记录

    起因 有些第三方库运行只支持Python3 5以上 以及需要使用pip3安装 因此不得不升级Python版本 主要步骤为python官方源码安装 然后修改Python3和pip3的软连接即可 具体升级步骤 安装依赖 sudo apt get
  • Python数据分析与机器学习项目实战

    时值蚂蚁上市之际 马云在上海滩发表演讲 马云的核心逻辑其实只有一个 在全球数字经济时代 有且只有一种金融优势 那就是基于消费者大数据的纯信用 我们不妨称之为数据信用 它比抵押更靠谱 它比担保更保险 它比监管更高明 它是一种面向未来的财产权
  • 迁移学习入门,新手该如何下手?

    推荐迁移学习技术的实用入门图书 自然语言处理迁移学习实战 加纳 保罗 阿祖雷 Paul Azunre 著 李想 朱仲书 张世武 译 一本书带你读懂ChatGPT背后的技术 自然语言处理迁移学习 解锁机器学习新境界 从浅层到深度 掌握NLP迁
  • 2016 OWASP Mobile TOP 10 中文版

    M1 平台使用不当 这个类别包括平台功能的滥用 或未能使用平台的安全控制 它可能包括 Android 的意图 intent 平台权限 TouchID 的误用 密钥链 KeyChain 或是移动操作系统中的其他一些安全控制 有几种方式使移动应
  • javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExcepti

    问题现象 Java Spring应用发送数据报如下问题 AxisFault faultCode http schemas xmlsoap org soap envelope Server userException faultSubcode
  • 网络编程先导知识

    目录 1 什么是网络协议 2 什么是Socket Socket主要类型 3 C S和B S架构 4 网络字节序和主机字节序 5 局域网和广域网 6 IP地址和端口的概念 1 什么是网络协议 为了在计算机网络中做到有条不紊地交换数据 就必须遵
  • android.content包-----ClipboardManager

    ClipboardManager类介绍 Clipboardmanager类通过getSystemService String 方法进行实例化操作 ClipboardManger类的相关方法很简单 包含set和get剪切板的数据 剪切板的数据
  • Tesseract-OCR 中文识别(附上源码)

    简介 光学字符识别 OCR Optical Character Recognition 是指对文本资料进行扫描 然后对图像文件进行分析处理 获取文字及版面信息的过程 OCR技术非常专业 一般多是印刷 打印行业的从业人员使用 可以快速的将纸质
  • Arcmap卫星影像去黑边(彻底去除黑边)

    在处理栅格数据时 我们常常会遇到一个问题 下载下来的卫星影像数据在Arcmap等软件上会出现黑边问题 如图 出现黑边的原因是因为我们下载影像图层是按外接矩形下载的 所以下载时矩形内没图的地方会填充透明色 透明后下下来后就会用黑色代替 那么我
  • 计算机组成原理-复习题2

    二 简答题 43 请写出8位定点原码整数中能表示的最大正数 最小正数 最大负数和最小负数的机器数形式 并用十进制表示其数值范围 答 最大正数 01111111 最小正数 00000001 最大负数 10000001 最小负数 1111111
  • 【动手学】36 图片增广_代码

    matplotlib inline import torch import torchvision from torch import nn from d2l import torch as d2l d2l set figsize img
  • qt学习笔记7:控件、自定义控件封装

    系统提供的控件们 按钮组 QPushButton 常用按钮 QToolButton 工具按钮 用于显示图片 如果想显示文字 可以修改风格 RadioButton 单选框 如果想设置默认 ui gt 空间名 gt setChecked Che
  • js最简单的数组去重方法set数组去重

    js最简单的数组去重方法set数组去重 数组去重 function newArr arr return Array from new Set arr 对象数组去重 arr是数组参数 type是要根据去重的字段参数 function newA
  • C--游游的水果大礼包--牛客周赛 Round 3

    示例1 输入 3 4 1 2 输出 4 说明 组成两个二号水果大礼包 使用了2个苹果和4个桃子 总价值为4 示例2 输入 1 1 5 6 输出 0 说明 显然无法组合成任意一个大礼包 解析 数据量1e6 应该O n 遍历苹果数量 计算最大值
  • 【python基础】猜数字游戏

    前言 相信很多人都玩过猜数字游戏 游戏规则也十分简单 还记得小时候我经常和朋友一起玩 我们在桌上摆放一些1 100以内的数字卡片 一个人随机抽取一张卡片 这里不许偷看卡片内容 首先这个人随机猜一个数字 然后其他人会告诉你猜大了还是猜小了 先
  • Oracle数据库同时执行多条DML语句

    在sqlplus中 SQL gt begin 语句1 语句2 语句N end
  • 【Detectron2】使用 Detectron2 训练基于 coco 数据集的目标检测网络

    文章目录 一 安装 Detectron2 二 软连接 coco 数据集 三 训练 四 数据集相关参数 五 输出结果路径 六 COCO 数据集简介 七 模型相关参数 八 可视化结果 一 安装 Detectron2 初次接触 Detectron