mmdetection 安装与使用(win10)

我会带着你远行 2023-07-01 04:23 139阅读 0赞

一、安装

(1)创建虚拟环境:

conda create -n mmdec python=3.6

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70

完成

(2)激活虚拟环境,安装torch(现在mmdetection需要的torch最低版本为1.1.0)

activate mmdec

发现官网的这个pip也是挺快的(https://pytorch.org/)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 1

pip install http://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp36m-win\_amd64.whl (本人使用)

pip (—default-timeout=600等待时间) install ****** 若果超时可以用下面的多等待一会儿

其他版本路径:

  1. https://download.pytorch.org/whl/cpu/torch-1.1.0-cp36-cp36m-win_amd64.whl(WIN,CPU)
  2. https://download.pytorch.org/whl/cu100/torch-1.1.0-cp36-cp36m-win_amd64.whl (WIN,CUDA 10.0)
  3. https://download.pytorch.org/whl/cu90/torch-1.1.0-cp36-cp36m-win_amd64.whl (WIN,CUDA 9.0)
  4. pip install https://download.pytorch.org/whl/cu100/torch-1.0.1-cp35-cp35m-linux_x86_64.whl(linux,CUDA 10.0)

pip install PyYAML -i https://pypi.tuna.tsinghua.edu.cn/simple​

pip install cython

pip install scikit-image

pip install torchvision

Pycocotools 这个东西windows安装需要https://github.com/philferriere/cocoapi下载后进入pythonAPI文件夹内

使用python setup.py install

继续安装上面缺少的库:

pip install 库名称 -i https://pypi.tuna.tsinghua.edu.cn/simple

V2EX:http://pypi.v2ex.com/simple
豆瓣:http://pypi.douban.com/simple
中国科学技术大学:http://pypi.mirrors.ustc.edu.cn/simple
清华:https://pypi.tuna.tsinghua.edu.cn/simple

(3)安装mmcv

直接pip安装就可以:pip install mmcv

以前 我是按照以下安装的

下载:https://github.com/open-mmlab/mmcv

pip install .

补充,电脑重装遇到的问题:anaconda3\envs\torch\lib\site-packages\pip\compat\__init__.py”, line 75, in console_to_str
return s.decode(‘utf_8’)
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xa1 in position 60: invalid start byte

发现是pip版本过低,更新pip 解决问题| python -m pip install —upgrade pip

(4)安装

mmdetection 1.x版本:如果你安装的是VS2015,自定义安装,updata3需要选上,否则这一步失败。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 2

下载:https://github.com/open-mmlab/mmdetection

使用如下命令安装

python setup.py develop -i https://pypi.tuna.tsinghua.edu.cn/simple

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 3

mmdetection 2.x版本,使用原本的VS2015报错,然后我安装了vs2019结果cuda又不支持了,我太难了

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 4

纠结了一下是升级cuda还是卸载vs2019,决定重新安装一个VS2017 试试。

待续。。。2.1版本的报错,先安装了个2.0 的

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 5

ubuntu下和1.x一样的安装方式正常安装

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 6

(5)测试

  1. from mmdet.apis import init_detector, inference_detector, show_result
  2. config_file = './configs/faster_rcnn_r50_fpn_1x.py'
  3. checkpoint_file = './checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'
  4. model = init_detector(config_file, checkpoint_file)
  5. img = 'demo.jpg'
  6. result = inference_detector(model, img)
  7. show_result(img, result, model.CLASSES)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 7

遇到问题:

0、ImportError: cannot import name ‘get_dist_info’

mmcv版本和mmdetection版本不匹配

1、在测试的时候提示:AttributeError: module ‘torch.distributed’ has no attribute ‘is_initialized’定位到错误处修改

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 8

-———————————————————————————分割线————————————————————————-

使用自己的数据集进行训练

1数据集准备:

因为一直用的都是VOC数据集,所以直接训练Voc的数据集

2.在mmdet/datasets文件夹内,修改自己的数据

  1. 1、在\_\_init\_\_ 中新增自己的

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 9

  1. 2、复制voc重命名成自己的MyData.py修改地方已标红

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1RZdGFuZ3lhbg_size_16_color_FFFFFF_t_70 10

3.修改configs中的配置文件

  1. # model settings
  2. model = dict(
  3. type='FasterRCNN',
  4. pretrained='torchvision://resnet50',
  5. backbone=dict(
  6. type='ResNet',
  7. depth=50,
  8. num_stages=4,
  9. out_indices=(0, 1, 2, 3),
  10. frozen_stages=1,
  11. style='pytorch'),
  12. neck=dict(
  13. type='FPN',
  14. in_channels=[256, 512, 1024, 2048],
  15. out_channels=256,
  16. num_outs=5),
  17. rpn_head=dict(
  18. type='RPNHead',
  19. in_channels=256,
  20. feat_channels=256,
  21. anchor_scales=[8],
  22. anchor_ratios=[0.5, 1.0, 2.0],
  23. anchor_strides=[4, 8, 16, 32, 64],
  24. target_means=[.0, .0, .0, .0],
  25. target_stds=[1.0, 1.0, 1.0, 1.0],
  26. loss_cls=dict(
  27. type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
  28. loss_bbox=dict(type='SmoothL1Loss', beta=1.0 / 9.0, loss_weight=1.0)),
  29. bbox_roi_extractor=dict(
  30. type='SingleRoIExtractor',
  31. roi_layer=dict(type='RoIAlign', out_size=7, sample_num=2),
  32. out_channels=256,
  33. featmap_strides=[4, 8, 16, 32]),
  34. bbox_head=dict(
  35. type='SharedFCBBoxHead',
  36. num_fcs=2,
  37. in_channels=256,
  38. fc_out_channels=1024,
  39. roi_feat_size=7,
  40. num_classes=3, # 类别+1
  41. target_means=[0., 0., 0., 0.],
  42. target_stds=[0.1, 0.1, 0.2, 0.2],
  43. reg_class_agnostic=False,
  44. loss_cls=dict(
  45. type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
  46. loss_bbox=dict(type='SmoothL1Loss', beta=1.0, loss_weight=1.0)))
  47. # model training and testing settings
  48. train_cfg = dict(
  49. rpn=dict(
  50. assigner=dict(
  51. type='MaxIoUAssigner',
  52. pos_iou_thr=0.7,
  53. neg_iou_thr=0.3,
  54. min_pos_iou=0.3,
  55. ignore_iof_thr=-1),
  56. sampler=dict(
  57. type='RandomSampler',
  58. num=256,
  59. pos_fraction=0.5,
  60. neg_pos_ub=-1,
  61. add_gt_as_proposals=False),
  62. allowed_border=0,
  63. pos_weight=-1,
  64. debug=False),
  65. rpn_proposal=dict(
  66. nms_across_levels=False,
  67. nms_pre=2000,
  68. nms_post=2000,
  69. max_num=2000,
  70. nms_thr=0.7,
  71. min_bbox_size=0),
  72. rcnn=dict(
  73. assigner=dict(
  74. type='MaxIoUAssigner',
  75. pos_iou_thr=0.5,
  76. neg_iou_thr=0.5,
  77. min_pos_iou=0.5,
  78. ignore_iof_thr=-1),
  79. sampler=dict(
  80. type='RandomSampler',
  81. num=512,
  82. pos_fraction=0.25,
  83. neg_pos_ub=-1,
  84. add_gt_as_proposals=True),
  85. pos_weight=-1,
  86. debug=False))
  87. test_cfg = dict(
  88. rpn=dict(
  89. nms_across_levels=False,
  90. nms_pre=1000,
  91. nms_post=1000,
  92. max_num=1000,
  93. nms_thr=0.7,
  94. min_bbox_size=0),
  95. rcnn=dict(
  96. score_thr=0.05, nms=dict(type='nms', iou_thr=0.5), max_per_img=100)
  97. # soft-nms is also supported for rcnn testing
  98. # e.g., nms=dict(type='soft_nms', iou_thr=0.5, min_score=0.05)
  99. )
  100. # dataset settings
  101. dataset_type = 'MyData'
  102. data_root = 'E:/mm/VOCdevkit/'
  103. img_norm_cfg = dict(mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
  104. data = dict(
  105. imgs_per_gpu=2,
  106. workers_per_gpu=2,
  107. train=dict(
  108. type='RepeatDataset', # to avoid reloading datasets frequently
  109. times=3,
  110. dataset=dict(
  111. type=dataset_type,
  112. ann_file=[data_root + 'VOC2007/ImageSets/Main/train.txt'],
  113. img_prefix=[data_root + 'VOC2007/'],
  114. img_scale=(400, 400),
  115. img_norm_cfg=img_norm_cfg,
  116. size_divisor=32,
  117. flip_ratio=0.5,
  118. with_mask=False,
  119. with_crowd=False,
  120. with_label=True)),
  121. val=dict(
  122. type=dataset_type,
  123. ann_file=data_root + 'VOC2007/ImageSets/Main/val.txt',
  124. img_prefix=data_root + 'VOC2007/',
  125. img_scale=(400, 400),
  126. img_norm_cfg=img_norm_cfg,
  127. size_divisor=32,
  128. flip_ratio=0,
  129. with_mask=False,
  130. with_crowd=False,
  131. with_label=True),
  132. test=dict(
  133. type=dataset_type,
  134. ann_file=data_root + 'VOC2007/ImageSets/Main/test.txt',
  135. img_prefix=data_root + 'VOC2007/',
  136. img_scale=(400, 400),
  137. img_norm_cfg=img_norm_cfg,
  138. size_divisor=32,
  139. flip_ratio=0,
  140. with_mask=False,
  141. with_crowd=False,
  142. with_label=False,
  143. test_mode=True))
  144. # optimizer
  145. optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001)
  146. optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
  147. # learning policy
  148. lr_config = dict(
  149. policy='step',
  150. warmup='linear',
  151. warmup_iters=500,
  152. warmup_ratio=1.0 / 3,
  153. step=[8, 11])
  154. checkpoint_config = dict(interval=1)
  155. # yapf:disable
  156. log_config = dict(
  157. interval=100,
  158. hooks=[
  159. dict(type='TextLoggerHook'),
  160. # dict(type='TensorboardLoggerHook')
  161. ])
  162. # yapf:enable
  163. # runtime settings
  164. total_epochs = 12
  165. dist_params = dict(backend='nccl')
  166. log_level = 'INFO'
  167. work_dir = 'model'
  168. load_from = None
  169. resume_from = None
  170. workflow = [('train', 1)]

4.使用tools中的train训练

config 变为—config =“配置文件”

5.测试

下载权重

常见问题(本人遇见的):

1、在训练的时候提示的:KeyError: ‘None is not in the dataset registry’

修改mmdet/datasets内修改不完整

2、在训练完一个epoch保存权重后提示的:OSError: symbolic link privilege not held

以管理员身份运行pycharm

20200119090124622.png

发表评论

表情:
评论列表 (有 0 条评论,139人围观)

还没有评论,来说两句吧...

相关阅读