训练COCO数据(liuhao_coco_kouzhao)
1.准备数据集
#1.创建软链接
ln -s ../../../datasets/kouzhao/liuhao_coco_kouzhao/ ./
1
2
3
/detectron2-master/detectron2/data/datasets/builtin.py
"liuhao_coco_kouzhao_train2017": ("liuhao_coco_kouzhao/train/train2017", "liuhao_coco_kouzhao/train/annotations/instances_train2017.json"), #自定义数据集
"liuhao_coco_kouzhao_val2017": ("liuhao_coco_kouzhao/val/val2017", "liuhao_coco_kouzhao/val/annotations/instances_val2017.json"), #自定义数据集
1
2
3
4
/detectron2-master/detectron2/data/datasets/builtin_meta.py第7行
#注意COCO_CATEGORIES要与coco的json的categories相同
"categories": [
{
"supercategory": "none",
"id": 0,
"name": "no_mask"
},
{
"supercategory": "none",
"id": 1,
"name": "have_mask"
}
]
COCO_CATEGORIES = [
{"color": [220, 20, 60], "isthing": 1, "id": 0, "name": "no_mask"},
{"color": [119, 11, 32], "isthing": 1, "id": 1, "name": "have_mask"}
]
COCO_CATEGORIES = [
{"color": [220, 20, 60], "isthing": 1, "id": 0, "name": "0"},
{"color": [119, 11, 32], "isthing": 1, "id": 1, "name": "1"},
{"color": [0, 0, 142], "isthing": 1, "id": 2, "name": "2"},
{"color": [0, 0, 230], "isthing": 1, "id": 3, "name": "3"},
{"color": [106, 0, 228], "isthing": 1, "id": 4, "name": "4"},
{"color": [0, 60, 100], "isthing": 1, "id": 5, "name": "5"},
{"color": [0, 80, 100], "isthing": 1, "id": 6, "name": "6"},
{"color": [0, 0, 70], "isthing": 1, "id": 7, "name": "7"},
{"color": [0, 0, 192], "isthing": 1, "id": 8, "name": "8"},
{"color": [250, 170, 30], "isthing": 1, "id": 9, "name": "9"},
{"color": [100, 170, 30], "isthing": 1, "id": 10, "name": "A"},
{"color": [220, 220, 0], "isthing": 1, "id": 11, "name": "B"},
{"color": [175, 116, 175], "isthing": 1, "id": 12, "name": "C"},
{"color": [250, 0, 30], "isthing": 1, "id": 13, "name": "D"},
{"color": [165, 42, 42], "isthing": 1, "id": 14, "name": "E"},
{"color": [255, 77, 255], "isthing": 1, "id": 15, "name": "F"},
{"color": [0, 226, 252], "isthing": 1, "id": 16, "name": "U"},
{"color": [182, 182, 255], "isthing": 1, "id": 17, "name": "V"},
{"color": [0, 82, 0], "isthing": 1, "id": 18, "name": "W"},
{"color": [120, 166, 157], "isthing": 1, "id": 19, "name": "X"},
{"color": [110, 76, 0], "isthing": 1, "id": 20, "name": "Y"},
{"color": [174, 57, 255], "isthing": 1, "id": 21, "name": "Z"},
]
/detectron2-master/detectron2/data/datasets/builtin_meta.py第194行
#assert len(thing_ids) == 80, len(thing_ids)
assert len(thing_ids) == 2, len(thing_ids) #########################################################
/detectron2-master/detectron2/data/datasets/builtin_meta.py第211行
#assert len(stuff_ids) == 53, len(stuff_ids)
assert len(stuff_ids) == 0, len(stuff_ids) #########################################################
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
这个还不确定要不要修改,不修改也能训练
./detectron2/config/defaults.py:
_C.MODEL.ROI_HEADS.NUM_CLASSES = 80
1
2
2.修改配置文件./configs/COCO-Detection/faster_rcnn_R_50_FPN_1x_liuhao_coco_kouzhao
_BASE_: "../Base-RCNN-FPN_liuhao_coco_kouzhao.yaml"
MODEL:
WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl"
MASK_ON: False
RESNETS:
DEPTH: 50
1
2
3
4
5
6
Base-RCNN-FPN_liuhao_coco_kouzhao.yaml
DATASETS:
TRAIN: ("liuhao_coco_kouzhao_train2017",) ########### coco_2017_train
TEST: ("liuhao_coco_kouzhao_val2017",) ############## coco_2017_val
SOLVER:
IMS_PER_BATCH: 2 #######################
BASE_LR: 0.02
STEPS: (600, 700) #######################
MAX_ITER: 800 ############# 自定义最大迭代次数=(训练集400张图片/batch_size2) * 4epochs
INPUT:
MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800)
VERSION: 2
1
2
3
4
5
6
7
8
9
10
11
3.训练
#在/detectron2-master文件夹下面打开终端
source ~/anaconda3/bin/activate mydetection_python3.7.3
python3 ./tools/train_net.py -h
python3 ./tools/train_net.py --config-file=./configs/COCO-Detection/faster_rcnn_R_50_FPN_1x_liuhao_coco_kouzhao.yaml --num-gpus=1 SOLVER.IMS_PER_BATCH 2 SOLVER.BASE_LR 0.01
#查看/detectron2-master/output/
1
2
3
4
5
6
7
8
9
10
11
12
报错,估计是生成的coco数据的categories没有去重
AssertionError: Attribute 'thing_classes' in the metadata of 'liuhao_coco_kouzhao_train2017' cannot be set to a different value!
1
2
4训练成功,部分日志
[04/28 22:11:05 d2.data.datasets.coco]: Loaded 400 images in COCO format from datasets/liuhao_coco_kouzhao/train/annotations/instances_train2017.json
[04/28 22:11:05 d2.data.build]: Removed 68 images with no usable annotations. 332 images left.
[04/28 22:11:05 d2.data.build]: Distribution of instances among all 2 categories:
| category | #instances | category | #instances |
|:----------:|:-------------|:----------:|:-------------|
| no_mask | 390 | have_mask | 521 |
| | | | |
| total | 911 | | |
[04/28 22:20:40 fvcore.common.checkpoint]: Saving checkpoint to ./output/model_final.pth
[04/28 22:20:41 d2.data.datasets.coco]: Loaded 177 images in COCO format from datasets/liuhao_coco_kouzhao/val/annotations/instances_val2017.json
[04/28 22:20:41 d2.data.build]: Distribution of instances among all 2 categories:
| category | #instances | category | #instances |
|:----------:|:-------------|:----------:|:-------------|
| no_mask | 139 | have_mask | 209 |
| | | | |
| total | 348 | | |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
修改./output/为./output_liuhao_coco_kouzhao/
1
5可视化
/home/liuhao/anaconda3/envs/mybase_python3.7.3/bin/tensorboard --logdir ./output_liuhao_coco_kouzhao/
1
6预测
python3 ./demo/demo.py --config-file=./configs/COCO-Detection/faster_rcnn_R_50_FPN_1x_liuhao_coco_kouzhao.yaml --input=./datasets/liuhao_coco_kouzhao/val/val2017/402.jpg --opts MODEL.WEIGHTS ./output_liuhao_coco_kouzhao/model_final.pth
python3 ./demo/demo.py --config-file=./configs/COCO-Detection/faster_rcnn_R_50_FPN_1x_liuhao_coco_kouzhao.yaml --input=./datasets/liuhao_coco_kouzhao/val/val2017/569.jpg --opts MODEL.WEIGHTS ./output_liuhao_coco_kouzhao/model_final.pth
#可以预测,效果不错
————————————————
版权声明:本文为CSDN博主「魔方研究生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liuhaobjtu/article/details/116949282