mmdetection 训练 Faster R-CNN

训练步骤

  1. 安装 labelme

    conda create --name=labelme python=3.6
    conda activate labelme
    pip install pyqt5
    pip install labelme
    
  2. 安装 scikit-imagescipy

    pip install scikit-image -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install scipy -i https://pypi.tuna.tsinghua.edu.cn/simple
    
  3. 运行 ..\data\coco\train2014\labelme2coco.py,生成 instances_train2014.json,并复制到..\data\coco\annotations\

  4. 运行 ..\data\coco\val2014\labelme2coco.py,生成 instances_val2014.json,并复制到..\data\coco\annotations\

  5. 修改change.py 并运行:

    pretrained_weights  = torch.load('F:\\BaiduNetdiskDownload\\mmdetection\\mmdetection-1.1.0\\checkpoints\\faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth')
    num_class = 类别 + 1
    torch.save(pretrained_weights, "F:\\BaiduNetdiskDownload\\mmdetection\\mmdetection-1.1.0\\faster_rcnn_r50_fpn_1x_%d.pth"%num_class)
    

    可以看到文件夹里多出一个文件: faster_rcnn_r50_fpn_1x_2.pth

  6. mmdetection-1.1.0\mmdet\utils\collect_env.py 注释42-44

    #gcc = subprocess.check_output('gcc --version | head -n1', shell=True)
    #gcc = gcc.decode('utf-8').strip()
    #env_info['GCC'] = gcc
    
  7. 修改mmdetection-1.1.0\configs\My_faster_rcnn_r50_fpn_1x.py:

    训练类别数: num_classes = 类别+1
    训练次数: total_epochs = 10
    学习率: lr = 0.02 
    第2步生成的权重文件: load_from = 'F:\\BaiduNetdiskDownload\\mmdetection\\mmdetection-1.1.0\\faster_rcnn_r50_fpn_1x_2.pth'
    训练多少次保存一次权重: checkpoint_config = dict(interval=5)
    填写你的val2014文件夹图片数目:  
    log_config = dict(
        interval=10,
        hooks=[
            dict(type='TextLoggerHook'),
            # dict(type='TensorboardLoggerHook')
        ])
    
  8. 修改mmdetectio_fasterrcnn\mmdet\datasets\coco.pyCLASSES 为自己要训练的类别名称: CLASSES = ('dog')

  9. 开始训练: python tools/train.py configs/My_faster_rcnn_r50_fpn_1x.py

  10. 这一步可能会报错,不用理会:

    'tail' 不是内部或外部命令,也不是可运行的程序或批处理文件。
    'gcc' 不是内部或外部命令,也不是可运行的程序或批处理文件。
    
  11. 但如果这一步报错是: OSError: symbolic link privilege not held,需要以管理员模式打开进行运行
    训练结果

测试训练结果

  1. 训练模型在: F:\BaiduNetdiskDownload\mmdetection\mmdetection-1.1.0\work_dirs\faster_rcnn_r50_fpn_1x\

  2. 修改demo.py:

    config_file = 'configs/My_faster_rcnn_r50_fpn_1x.py'
    checkpoint_file = 'work_dirs/faster_rcnn_r50_fpn_1x/epoch_10.pth'
    
    img = 'data/coco/val2014/000021.jpg'
    
  3. 运行demo.py: python demo.py,这一步可能会报错:

    Traceback (most recent call last):
    File "demo.py", line 16, in <module>
    	show_result(img, result, model.CLASSES)
    File "F:\BaiduNetdiskDownload\mmdetection\mmdetection-1.1.0\mmdet\apis\inference.py", line 143, in show_result
    	assert isinstance(class_names, (tuple, list))
    AssertionError                                             
    

    因为类别中只有一类,所以需要适当修改,详情参考:解决方案

    上面的错误将通过 mmdet/apis/inference.py 第143行,修改

    assert isinstance(class_names, (tuple, list))
    

    assert isinstance(class_names, (tuple, list, str))
    

结果如图
这个识别出了两部分
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40042498/article/details/112910982