# 环境安装:
1.anaconda创建环境
conda create -n detectron2 python=3.7
conda activate detectron2
2. 安装pytorch 1.6 cuda10.1
```
# https://pytorch.org/get-started/previous-versions/
conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.1 -c pytorch
```
```
#测试一下
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())
```
3.安装其他依赖项
```
pip install cython pyyaml==5.1
pip install opencv-python
pip install cython
pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
```
4.linux 安装detectron2
```
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
或
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.6/index.html
```
5.测试
```
python3 demo/demo.py \
--config-file configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml \
--input input.jpg --output outputs/ \
--opts MODEL.WEIGHTS detectron2://COCO-Detection/faster_rcnn_R_50_FPN_1x/137257794/model_final_b275ba.pkl
```
# faster-RCNN训练:
参考 https://blog.csdn.net/qq_29750461/article/details/106761382 \
https://www.cnblogs.com/rioka/p/13781852.html \
1.准备coco数据集
```
# COCO格式的数据集,category_id是从0开始的
--MyDataset
--images
--01_00001.jpg # 图片名无所谓
...
--annotations
--train.json
--valid.json
```
2.将数据集注册到Detectron2中
```
#将detectron2/tools/train_net.py 这个文件复制为 my_train.py
#将数据集注册代码包装成类
from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.data.datasets.coco import load_coco_json
class Register:
"""用于注册自己的数据集"""
CLASS_NAMES = ['table']
ROOT = "/media/ubuntu/datadist/hao/med_coco"
def __init__(self):
self.CLASS_NAMES = Register.CLASS_NAMES or ['table']
self.DATASET_CATEGORIES = [
{"name": "table", "id": 0, "isthing": 1, "color": [220, 20, 60]}
]
# 数据集路径
self.DATASET_ROOT = Register.ROOT or '/media/ubuntu/datadist/hao/med_coco'
self.ANN_ROOT = os.path.join(self.DATASET_ROOT, 'annotations')
self.TRAIN_PATH = os.path.join(self.DATASET_ROOT, 'images')
self.VAL_PATH = os.path.join(self.DATASET_ROOT, 'images')
self.TRAIN_JSON = os.path.join(self.ANN_ROOT, 'train.json')
self.VAL_JSON = os.path.join(self.ANN_ROOT, 'valid.json')
# self.VAL_JSON = os.path.join(self.ANN_ROOT, 'test.json')
# 声明数据集的子集
self.PREDEFINED_SPLITS_DATASET = {
"coco_my_train": (self.TRAIN_PATH, self.TRAIN_JSON),
"coco_my_val": (self.VAL_PATH, self.VAL_JSON),
}
def register_dataset(self):
"""
purpose: register all splits of datasets with PREDEFINED_SPLITS_DATASET
注册数据集(这一步就是将自定义数据集注册进Detectron2)
"""
for key, (image_root, json_file) in self.PREDEFINED_SPLITS_DATASET.items():
self.register_dataset_instances(name=key,
metadate=self.get_dataset_instances_meta(),
json_file=json_file,
image_root=image_root)
@staticmethod
def register_dataset_instances(name, metadate,json_file, image_root):
"""
purpose: register datasets to DatasetCatalog,
register metadata to MetadataCatalog and set attribute
注册数据集实例,加载数据集中的对象实例
"""
DatasetCatalog.register(name, lambda: load_coco_json(json_file, image_root, name))
MetadataCatalog.get(name).set(json_file=json_file,
image_root=image_root,
evaluator_type="coco",
**metadate)
def get_dataset_instances_meta(self):
"""
purpose: get metadata of dataset from DATASET_CATEGORIES
return: dict[metadata]
"""
thing_ids = [k["id"] for k in self.DATASET_CATEGORIES if k["isthing"] == 1]
thing_colors = [k["color"] for k in self.DATASET_CATEGORIES if k["isthing"] == 1]
# assert len(thing_ids) == 2, len(thing_ids)
thing_dataset_id_to_contiguous_id = {k: i for i, k in enumerate(thing_ids)}
thing_classes = [k["name"] for k in self.DATASET_CATEGORIES 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
def plain_register_dataset(self):
"""注册数据集和元数据"""
# 训练集
DatasetCatalog.register("coco_my_train", lambda: load_coco_json(self.TRAIN_JSON, self.TRAIN_PATH))
MetadataCatalog.get("coco_my_train").set(thing_classes=self.CLASS_NAMES, # 可以选择开启,但是不能显示中文,这里需要注意,中文的话最好关闭
evaluator_type='coco', # 指定评估方式
json_file=self.TRAIN_JSON,
image_root=self.TRAIN_PATH)
# DatasetCatalog.register("coco_my_val", lambda: load_coco_json(VAL_JSON, VAL_PATH, "coco_2017_val"))
# 验证/测试集
DatasetCatalog.register("coco_my_val", lambda: load_coco_json(self.VAL_JSON, self.VAL_PATH))
MetadataCatalog.get("coco_my_val").set(thing_classes=self.CLASS_NAMES, # 可以选择开启,但是不能显示中文,这里需要注意,中文的话最好关闭
evaluator_type='coco', # 指定评估方式
json_file=self.VAL_JSON,
image_root=self.VAL_PATH)
def checkout_dataset_annotation(self, name="coco_my_val"):
"""
查看数据集标注,可视化检查数据集标注是否正确,
这个也可以自己写脚本判断,其实就是判断标注框是否超越图像边界
可选择使用此方法
"""
# dataset_dicts = load_coco_json(TRAIN_JSON, TRAIN_PATH, name)
dataset_dicts = load_coco_json(self.TRAIN_JSON, self.TRAIN_PATH)
print(len(dataset_dicts))
for i, d in enumerate(dataset_dicts, 0):
# print(d)
img = cv2.imread(d["file_name"])
visualizer = Visualizer(img[:, :, ::-1], metadata=MetadataCatalog.get(name), scale=1.5)
vis = visualizer.draw_dataset_dict(d)
# cv2.imshow('show', vis.get_image()[:, :, ::-1])
cv2.imwrite('out/' + str(i) + '.jpg', vis.get_image()[:, :, ::-1])
# cv2.waitKey(0)
if i == 200:
break
```
```
#并在 main() 函数中第2行添加调用
def main(args):
cfg = setup(args)
Register().register_dataset() # register my dataset
```
3.编辑配置文件
```
#从官方下载的 Faster_RCNN 配置文件及其依赖配置最好不要修改,而是基于其配置另写一个文件 configs/faster_rcnn_med.yam
_BASE_: "COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"
DATASETS:
TRAIN: ("coco_my_train",)
TEST: ("coco_my_val",)
MODEL:
RETINANET:
NUM_CLASSES: 1
# WEIGHTS: "../tools/output/model_final.pth"
SOLVER:
IMS_PER_BATCH: 8
# 初始学习率
BASE_LR: 0.001
# 迭代到指定次数,学习率进行衰减
STEPS: (10000, 15000)
MAX_ITER: 20000
CHECKPOINT_PERIOD: 2000
TEST:
# 迭代到指定次数,进行一次评估
EVAL_PERIOD: 2000
OUTPUT_DIR: "output/med/"
```
4.Train & Eval
```
################ train.sh ################
# lr = 0.00025 * num_gpus
python3 tools/my_train.py \
--config-file configs/faster_rcnn_med.yaml \
--num-gpus 1 \
OUTPUT_DIR tools/output/med1/ \
SOLVER.IMS_PER_BATCH 8 \
SOLVER.BASE_LR 0.001 \
SOLVER.MAX_ITER 20000 \
SOLVER.STEPS '(10000, 15000)' \
TEST.EVAL_PERIOD 2000 \
>./train_log/0808.log 2>&1 &
############# train_resume.sh #############
# 断点续 train
# --num-gpus 亲测不能省略
python3 tools/my_train.py \
--config-file configs/faster_rcnn_med.yaml \
--num-gpus 1 \
--resume \
OUTPUT_DIR tools/output/med/ \
SOLVER.IMS_PER_BATCH 8
################# eval.sh #################
# 将coco_my_val,指代的 valid.json,变为test.json,即可,然后运行
python3 tools/my_train.py \
--config-file tools/output/med/config.yaml \
--eval-only \
OUTPUT_DIR tools/output/med/ \
MODEL.WEIGHTS tools/output/med/model_final.pth
```
5.测试(也要注册数据集)
```
# 自己数据集测试(一组图像)
python3 demo/inference.py \
--config-file tools/output/med/config.yaml \
--input demo/test_image/*.jpg \
--output demo/inference_results \
--opts MODEL.WEIGHTS tools/output/med/model_final.pth
```
```
# 自己数据集测试(单张图像)
python3 demo/inference.py \
--config-file tools/output/med/config.yaml \
--input demo/test_image/001.jpg \
--output demo/inference_results \
--opts MODEL.WEIGHTS tools/output/med/model_final.pth
```
```
# paddleocr调用测试(单张)
python3 demo/detect.py
```