mmdetection2使用教程

mmdetection2.0_tutorials.rar
mmdetection2.0_All.rar(里面没有mmcv安装文件)
这两个是windows的,是2.0,需要torch1.4.0,cuda10.0


ubuntu_mmd2.3.tar.gz
mmdetection-all_ubuntu.tar.gz(里面没有mmcv安装文件)
这两个是ubuntu的,是2.3,可以使用torch1.5.0,cuda10.1


我用的是windows的两个文件

准备环境就用mydetection_python3.7.3

安装mmcv

#在在mmcv-0.5.8文件夹打开终端

source ~/anaconda3/bin/activate mydetection_python3.7.3

MMCV_WITH_OPS=1 python3 ./setup.py develop	#我用的这个
或者MMCV_WITH_OPS=1 pip install -e .

#==========================================================================
#会在/home/liuhao/anaconda3/envs/mydetection_python3.7.3/lib/python3.7/site-packages/生成:
easy-install.pth:
/home/liuhao/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/ubuntu_mmd2.3/mmcv-master

mmcv.egg-link


#会在/home/liuhao/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmcv-0.5.8/生成:
build文件夹
mmcv.egg-info文件夹

pip show mmcv

liuhao@liuhao-Lenovo:~/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmcv-0.5.8$ source ~/anaconda3/bin/activate mydetection_python3.7.3(mydetection_python3.7.3) liuhao@liuhao-Lenovo:~/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmcv-0.5.8$ pip show mmcv
Name: mmcv
Version: 0.5.8
Summary: Open MMLab Computer Vision Foundation
Home-page: https://github.com/open-mmlab/mmcv
Author: Kai Chen
Author-email: [email protected]
License: UNKNOWN
Location: /home/liuhao/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmcv-0.5.8
Requires: addict, numpy, pyyaml, yapf, opencv-python
Required-by: mmdet
(mydetection_python3.7.3) liuhao@liuhao-Lenovo:~/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmcv-0.5.8$

安装mmdet

#在在mmdetection2.0文件夹打开终端

source ~/anaconda3/bin/activate mydetection_python3.7.3

python3 ./setup.py develop
或者pip install -e .

#==========================================================================
#会在/home/liuhao/anaconda3/envs/mydetection_python3.7.3/lib/python3.7/site-packages/生成:
easy-install.pth:
/home/liuhao/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/ubuntu_mmd2.3/mmdetection-master

mmdet.egg-link


#会在/home/liuhao/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmdetection2.0/生成:
build文件夹
mmdet.egg-info文件夹

pip show mmdet

(mydetection_python3.7.3) liuhao@liuhao-Lenovo:~/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmdetection2.0$ pip show mmdet
Name: mmdet
Version: 2.0.0+unknown
Summary: Open MMLab Detection Toolbox and Benchmark
Home-page: https://github.com/open-mmlab/mmdetection
Author: OpenMMLab
Author-email: [email protected]
License: Apache License 2.0
Location: /home/liuhao/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmdetection2.0
Requires: matplotlib, mmcv, numpy, Pillow, six, terminaltables, torch, torchvision
Required-by: 
(mydetection_python3.7.3) liuhao@liuhao-Lenovo:~/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_tutorials/mmdetection2.0$

测试


#在/mmdetection2_download/mmdetection2.0_tutorials/mmdetection2.0/打开终端

source ~/anaconda3/bin/activate mydetection_python3.7.3


python3 ./demo/image_demo.py ./demo/demo.jpg ./configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py ./checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth --score-thr=0.8


python3 ./demo/webcam_demo.py ./configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py ./checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth --score-thr=0.8 --camera-id=0

训练自定义数据(liuhao_coco_kouzhao)

1.准备coco数据集

修改./mmdet/datasets/coco.py

    # CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    #            'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
    #            'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog',
    #            'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
    #            'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
    #            'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat',
    #            'baseball glove', 'skateboard', 'surfboard', 'tennis racket',
    #            'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
    #            'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot',
    #            'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
    #            'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
    #            'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
    #            'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock',
    #            'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush')

    CLASSES = (
    "no_mask", "have_mask"
    )

修改./mmdet/core/evaluation/class_names.py

def coco_classes():
    #return [
    #    'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
    #    'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign',
    #    'parking_meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
    #    'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
    #    'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
    #    'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard',
    #    'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'fork',
    #    'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
    #    'broccoli', 'carrot', 'hot_dog', 'pizza', 'donut', 'cake', 'chair',
    #    'couch', 'potted_plant', 'bed', 'dining_table', 'toilet', 'tv',
    #    'laptop', 'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave',
    #    'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
    #    'scissors', 'teddy_bear', 'hair_drier', 'toothbrush'
    #]

    return [
    "no_mask", "have_mask"
    ]

修改./configs/base/datasets/coco_detection_liuhao_coco_kouzhao.py


data_root = '/home/liuhao/PycharmProjects/DeepLearning_liuhao/datasets/kouzhao/liuhao_coco_kouzhao/'#####################

data = dict(
    samples_per_gpu=2,
    workers_per_gpu=2,
    train=dict(
        type=dataset_type,
        ann_file=data_root + 'train/annotations/instances_train2017.json',############
        img_prefix=data_root + 'train/train2017/',   #################################
        pipeline=train_pipeline),
    val=dict(
        type=dataset_type,
        ann_file=data_root + 'val/annotations/instances_val2017.json',################
        img_prefix=data_root + 'val/val2017/',   #####################################
        pipeline=test_pipeline),
    test=dict(
        type=dataset_type,
        ann_file=data_root + 'val/annotations/instances_val2017.json',################
        img_prefix=data_root + 'val/val2017/',   #####################################
        pipeline=test_pipeline))
evaluation = dict(interval=1, metric='bbox')

修改./configs/base/datasets/coco_instance_liuhao_coco_kouzhao.py

_base_ = 'coco_detection_liuhao_coco_kouzhao.py'

2.修改配置文件

修改./configs/base/schedules/schedule_1x_liuhao_coco_kouzhao.py

# optimizer
optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=None)
# learning policy
lr_config = dict(
    policy='step',
    warmup='linear', ###################constant linear exp
    warmup_iters=500,
    warmup_ratio=0.001,
    step=[8, 11])
total_epochs = 5 ###################

修改./configs/base/models/mask_rcnn_r50_fpn_liuhao_coco_kouzhao.py


pretrained='torchvision://resnet50', ##################### None
num_classes=2,##########################################xiugai
num_classes=2,##########################################xiugai


如果pretrained='torchvision://resnet50', # None

ln -s /home/liuhao/PycharmProjects/DeepLearning_liuhao/model_liuhao/resnet50-19c8e357.pth ./

修改./configs/base/default_runtime_liuhao_coco_kouzhao.py

#训练Mask-RCNN部分

import torch
pretrained_weights  = torch.load('checkpoints/mask_rcnn_r50_fpn_1x_coco_20200205-d4b0c5d6.pth')

num_class = 2
pretrained_weights['state_dict']['roi_head.bbox_head.fc_cls.weight'].resize_(num_class+1, 1024)
pretrained_weights['state_dict']['roi_head.bbox_head.fc_cls.bias'].resize_(num_class+1)
pretrained_weights['state_dict']['roi_head.bbox_head.fc_reg.weight'].resize_(num_class*4, 1024)
pretrained_weights['state_dict']['roi_head.bbox_head.fc_reg.bias'].resize_(num_class*4)

torch.save(pretrained_weights, "mask_rcnn_r50_fpn_1x_%d_liuhao_coco_kouzhao.pth"%num_class)




source ~/anaconda3/bin/activate mydetection_python3.7.3
python3 ./changemaskrcnn.py		# 生成 mask_rcnn_r50_fpn_1x_2_liuhao_coco_kouzhao.pth

checkpoint_config = dict(interval=5)
# yapf:disable
log_config = dict(
    interval=11,
    hooks=[
        dict(type='TextLoggerHook'),
        dict(type='TensorboardLoggerHook')                ##############tf_logs
    ])
# yapf:enable
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = './mask_rcnn_r50_fpn_1x_2_liuhao_coco_kouzhao.pth'##################
resume_from = None
workflow = [('train', 1)]

4.训练

./configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao.py

_base_ = [
    '../_base_/models/mask_rcnn_r50_fpn_liuhao_coco_kouzhao.py',
    '../_base_/datasets/coco_instance_liuhao_coco_kouzhao.py',
    '../_base_/schedules/schedule_1x_liuhao_coco_kouzhao.py', 
    '../_base_/default_runtime_liuhao_coco_kouzhao.py'
]
source ~/anaconda3/bin/activate mydetection_python3.7.3

python3 ./tools/train.py ./configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao.py
#生成work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/文件夹



#可视化
/home/liuhao/anaconda3/envs/mydetection_python3.7.3/bin/tensorboard --logdir ./work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/tf_logs --port 6666

输出日志

(mydetection_python3.7.3) liuhao@liuhao-Lenovo:~/PycharmProjects/DeepLearning_liuhao/mmdetection2_download/mmdetection2.0_All$ python3 ./tools/train.py ./configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao.py
2021-04-28 21:05:39,975 - mmdet - INFO - Environment info:
------------------------------------------------------------
sys.platform: linux
Python: 3.7.3 | packaged by conda-forge | (default, Dec  6 2019, 08:54:18) [GCC 7.3.0]
CUDA available: True
CUDA_HOME: /usr/local/cuda/
NVCC: Cuda compilation tools, release 10.0, V10.0.130
GPU 0: GeForce GTX 1060
PyTorch: 1.4.0
PyTorch compiling details: PyTorch built with:
  - GCC 7.3
  - Intel(R) Math Kernel Library Version 2019.0.4 Product Build 20190411 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CUDA Runtime 10.1
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  - CuDNN 7.6.3
  - Magma 2.5.1
  - Build settings: BLAS=MKL, BUILD_NAMEDTENSOR=OFF, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Wno-stringop-overflow, DISABLE_NUMA=1, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF, 

TorchVision: 0.5.0
OpenCV: 3.4.6
MMCV: 0.5.8
MMDetection: 2.0.0+unknown
MMDetection Compiler: GCC 7.4
MMDetection CUDA Compiler: 10.0

5.测试

#测试训练的结果1
python3 ./demo/image_demo.py ../../datasets/kouzhao/liuhao_coco_kouzhao/val/val2017/402.jpg ./configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao.py ./work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/epoch_5.pth --score-thr=0.8
#可以预测,效果不行


#测试训练的结果2
python3 ./tools/test.py ./configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao.py ./work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/epoch_5.pth --out ./result_liuhao/result_liuhao_coco_kouzhao_5.pkl --eval bbox --show

#可视化训练的loss结果
python3 ./tools/analyze_logs.py plot_curve ./work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/20210428_212133.log.json --keys loss_rpn_cls loss_rpn_bbox loss_cls loss_bbox loss acc --out ./plot_result_liuhao/liuhao_coco_kouzhao/


#计算训练的时间
python3 ./tools/analyze_logs.py cal_train_time ./work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/20210428_212133.log.json
#这个没有报错

-----Analyze train time of ./work_dirs/mask_rcnn_r50_fpn_1x_coco_liuhao_coco_kouzhao/20210428_212133.log.json-----
slowest epoch 1, average time is 0.8491
fastest epoch 4, average time is 0.8249
time std over epochs is 0.0093
average iter time: 0.8306 s/iter

你可能感兴趣的:(mmdetection2使用教程)