Adelaidet使用BoxInst文章理解和训练

BoxInst: High-Performance Instance Segmentation with Box Annotations

paper:https://arxiv.org/abs/2012.02310

GitHub:https://github.com/aim-uofa/AdelaiDet

 

文章实现了仅通过矩形框,即可完成实例分割任务,并且有非常不错的效果。极大节省了分割任务的成本。

感谢沈春华老师团队

上图第一行:同一目标在分割任务中的mask和检测任务中的box在水平和垂直方向上的投影一致,因此设计了投影损失。上图第二行:计算像素与其8个相邻像素(步长为2)之间的颜色相似度。若同为背景或者前景,相似度高;若不同属于前景或者背景,相似度低。因此设计了成对项损失。基于这两个损失完成通过box训练可预测mask的模型。

1、投影损失:

Proj_{x}(b) = l_{x}, Proj_{y}(b) = l_{y}

其中b为真实框,Proj_{x}为b在x轴上的投影,Proj_{y}为b在y轴上的投影,l_{x}l_{y}分别为在x和y轴上的一维mask。

L_{proj}=L(Proj_{x}(m),Proj_{x}(b))+L(Proj_{y}(m),Proj_{y}(b))

其中,m为预测mask,b为真实框,Proj_{x}(m)为m在x轴上的投影,Proj_{y}(m)为m在y轴上的投影。计算预测mask和真实框在x、y轴上的投影损失。

2、颜色损失:

蓝线:颜色相似度高于阈值的边缘中正边缘的比例;红线:监督的正边缘占所用正边的比例。横轴:颜色相似度阈值(计算基于COCO数据集)。

任意两像素的相似度计算公式:

S_{e} = exp(-\frac{||c_{i,j}-c_{l,k}||}{\theta })

相似度损失公式:

L_{pairwise}=-\frac{1}{N}\sum \Re _{S_{e}\geq \tau }logP(y_{e}=1)

\tau为颜色相似度阈值,当S_{e}大于等于\tau时,\Re _{S_{e}\geq \tau }=1;当S_{e}小于\tau时,\Re _{S_{e}\geq \tau }=0。P(y_{e}=1)代表两个像素同为前景或者同为背景。

训练自己的数据集:

1.在detectron2/detectron2/data/datasets/builtin.py中注册数据集。

_PREDEFINED_SPLITS_COCO["coco"] = {
    "coco_2014_train": ("coco/train2014", "coco/annotations/instances_train2014.json"),
    "coco_2014_val": ("coco/val2014", "coco/annotations/instances_val2014.json"),
    "coco_2014_minival": ("coco/val2014", "coco/annotations/instances_minival2014.json"),
    "coco_2014_minival_100": ("coco/val2014", "coco/annotations/instances_minival2014_100.json"),
    "coco_2014_valminusminival": (
        "coco/val2014",
        "coco/annotations/instances_valminusminival2014.json",
    ),
    "coco_2017_train": ("coco/train2017", "coco/annotations/instances_train2017.json"),
    "coco_2017_val": ("coco/val2017", "coco/annotations/instances_val2017.json"),
    "coco_2017_test": ("coco/test2017", "coco/annotations/image_info_test2017.json"),
    "coco_2017_test-dev": ("coco/test2017", "coco/annotations/image_info_test-dev2017.json"),
    "coco_2017_val_100": ("coco/val2017", "coco/annotations/instances_val2017_100.json"),

    #增加自己的数据集
    "coco_our_train": ("coco_our/train2017", "coco_our/annotations/our_train.json"),
    "coco_our_val": ("coco_our/val2017", "coco_our/annotations/our_val.json"),

}

2.将数据coco_our放到Adelaidet/datasets/下,结构如下:

./datasets

----coco_our

--------annotations

------------our_train.json

------------our_val.json

--------train2017

--------val2017

3.在detectron2/detectron2/data/datasets/builtin_meta.py中注册数据集类别。

COCO_CATEGORIES_our = [
    {"color": [220, 20, 60], "isthing": 1, "id": 0, "name": "car"},
    {"color": [119, 11, 32], "isthing": 1, "id": 1, "name": "truck"},
    {"color": [0, 0, 142], "isthing": 1, "id": 2, "name": "road"},
    {"color": [0, 0, 230], "isthing": 1, "id": 3, "name": "person"},
    {"color": [106, 0, 228], "isthing": 1, "id": 4, "name": "bus"},
]

4.将detectron2/detectron2/data/datasets/builtin_meta.py中COCO_CATEGORIES修改为COCO_CATEGORIES_our。

def _get_coco_instances_meta():
    thing_ids = [k["id"] for k in COCO_CATEGORIES_our if k["isthing"] == 1]
    thing_colors = [k["color"] for k in COCO_CATEGORIES_our if k["isthing"] == 1]
    assert len(thing_ids) == 18, len(thing_ids)
    # Mapping from the incontiguous COCO category id to an id in [0, 79]
    thing_dataset_id_to_contiguous_id = {k: i for i, k in enumerate(thing_ids)}
    thing_classes = [k["name"] for k in COCO_CATEGORIES_our if k["isthing"] == 1]
    ret = {
        "thing_dataset_id_to_contiguous_id": thing_dataset_id_to_contiguous_id,
        "thing_classes": thing_classes,
        "thing_colors": thing_colors,
    }
    return ret

5.修改Adelaidet/adet/config/default.py中的NUM_CLASSES为你的类别数。

_C.MODEL.BASIS_MODULE.NUM_CLASSES = 5

6.修改Adelaidet/configs/BoxInst/Base-BoxInst.yaml中的训练路径,batch_size等

DATASETS:
  TRAIN: ("coco_our_train",)
  TEST: ("coco_our_val",)
SOLVER:
  IMS_PER_BATCH: 2
  BASE_LR: 0.01
  STEPS: (60000, 80000)
  MAX_ITER: 90000

7.开始训练

python tools/train_net.py --config-file configs/BoxInst/MS_R_50_1x.yaml --num-gpus 2 OUTPUT_DIR training_dir/BoxInst_MS_R_50_1x

8.测试

python demo/demo.py config-file configs/BoxInst/MS_R_50_3x.yaml input input1.jpg input2.jpg opts MODEL.WEIGHTS BoxInst_MS_R_50_3x.pth

如有错误欢迎指出,谢谢观看!

 

 

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
AdelaiDet是Detectron2的一个扩展包,用于目标检测任务。它包含了一些新的anchor-free模型,如FCOS,并且支持COCO格式的数据集。要训练自己的数据集,你需要按照以下步骤进行操作: 1. 准备好自己的数据集,包括标注文件和图像。标注文件可以是COCO格式的JSON文件或者VOC格式的XML文件。确保标注文件中的类别id从1开始,并且与图像路径对应。 2. 安装好Detectron2或AdelaiDet,并熟悉其安装和使用方法。你可以参考官方的安装文档和入门指南。 3. 配置数据集,可以参考AdelaiDet的GitHub上的datasets/readme.md文件,了解如何使用内置数据集。你需要注册你自己的数据集,指定标注文件和图像路径等信息。 4. 使用以下命令克隆Detectron2和AdelaiDet的GitHub仓库,并安装AdelaiDet: ``` git clone https://github.com/facebookresearch/detectron2.git cd detectron2 git checkout -f 9eb4831 cd .. python -m pip install -e detectron2 git clone https://github.com/aim-uofa/AdelaiDet.git cd AdelaiDet python setup.py build develop ``` 5. 根据你的需求修改配置文件,例如训练参数、模型架构等。你可以参考AdelaiDet的GitHub仓库中的示例配置文件。 6. 运行训练脚本,指定配置文件和数据集名称。例如: ``` python tools/train_net.py --config-file configs/FCOS-Detection/R_50_1x.yaml --num-gpus 8 DATASETS.TRAIN "('your_dataset_name',)" OUTPUT_DIR "outputs/your_experiment_name" ``` 其中,`--config-file`指定配置文件路径,`--num-gpus`指定使用的GPU数量,`DATASETS.TRAIN`指定训练数据集名称,`OUTPUT_DIR`指定输出目录。 希望以上步骤对你训练自己的数据集有所帮助。如果有任何问题,请在评论中留言,我会尽力帮助你。 #### 引用[.reference_title] - *1* *2* *3* [[Detectron2]使用Detectron2/AdelaiDet训练自己的数据集](https://blog.csdn.net/weixin_43823854/article/details/108980188)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值