当前位置:   article > 正文

旋转目标检测-环境配置-数据集制作_旋转目标检测数据集

旋转目标检测数据集

一、环境配置

  • Ubuntu 22.04
  • Torch 1.10
  • CUDA 11.3
  • python 3.9

      环境配置参考下面链接(建议Linux系统)yolov5_obb/install.md at master · hukaixuan19970627/yolov5_obb (github.com)https://github.com/hukaixuan19970627/yolov5_obb/blob/master/docs/install.md

  •  创建虚拟环境
  1. conda create -n Py39_Torch1.10_cu11.3 python=3.9 -y
  2. source activate Py39_Torch1.10_cu11.3
  •  查看CUDA版本(确保CUDA runtime api version ≤ CUDA driver version)
  1. nvcc -V
  2. nvidia-smi

  •  安装PyTorch和torchvision
  1. pip3 install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
  2. nvcc -V
  3. python
  4. >>> import torch
  5. >>> torch.version.cuda
  6. >>> exit()

  •  克隆yolov7-obb项目(也可直接在GitHub上下载)
git clone https://github.com/Egrt/yolov7-obb.git

  •  配置yolov7-obb所需环境
  1. cd yolov7-obb
  2. pip install -r requirements.txt
  3. cd utils/nms_rotated
  4. python setup.py develop #安装非极大值抑制库

运行 python setup.py develop时报错

可以看出时gcc版本问题,检测已有版本发现gcc 12 > gcc 11

故安装低版本的gcc,并创建软链接(此处要对应改为自己的位置)

  1. sudo apt-get install gcc-10
  2. sudo apt-get install g++-10
  3. sudo ln -s /usr/bin/gcc-10 /usr/local/cuda-11.6/bin/gcc #创建软链接

再次运行 python setup.py develop

至此,环境配置完成!!!

二、数据集制作

  • 下载官方源码
https://github.com/cgvict/roLabelImg
  •  进入下载好的roLabellmg-master文件夹内 ,在终端打开
  1. pyrcc5 -oresources.py resources.qrc
  2. python roLabelImg.py

  •  快捷键
w创建矩形框
e创建旋转矩形框

d

下一张

a上一张
zxcv旋转矩形框
Ctrl + s保存

  •  标注的XML格式
  1. <annotation verified="yes">
  2. <folder>hsrc</folder>
  3. <filename>100000001</filename>
  4. <path>/Users/haoyou/Library/Mobile Documents/com~apple~CloudDocs/OneDrive/hsrc/100000001.bmp</path>
  5. <source>
  6. <database>Unknown</database>
  7. </source>
  8. <size>
  9. <width>1166</width>
  10. <height>753</height>
  11. <depth>3</depth>
  12. </size>
  13. <segmented>0</segmented>
  14. <object>
  15. <type>bndbox</type>
  16. <name>ship</name>
  17. <pose>Unspecified</pose>
  18. <truncated>0</truncated>
  19. <difficult>0</difficult>
  20. <bndbox>
  21. <xmin>178</xmin>
  22. <ymin>246</ymin>
  23. <xmax>974</xmax>
  24. <ymax>504</ymax>
  25. </bndbox>
  26. </object>
  27. </annotation>
  •  数据集格式转换(把旋转框 cx,cy,w,h,angle,转换成四点坐标x1,y1,x2,y2,x3,y3,x4,y4)
  1. import os
  2. import xml.etree.ElementTree as ET
  3. import math
  4. def edit_xml(xml_file, dotaxml_file):
  5. """
  6. 修改xml文件
  7. :param xml_file:xml文件的路径
  8. :return:
  9. """
  10. tree = ET.parse(xml_file)
  11. objs = tree.findall('object')
  12. for ix, obj in enumerate(objs):
  13. x0 = ET.Element("x0") # 创建节点
  14. y0 = ET.Element("y0")
  15. x1 = ET.Element("x1")
  16. y1 = ET.Element("y1")
  17. x2 = ET.Element("x2")
  18. y2 = ET.Element("y2")
  19. x3 = ET.Element("x3")
  20. y3 = ET.Element("y3")
  21. # obj_type = obj.find('bndbox')
  22. # type = obj_type.text
  23. # print(xml_file)
  24. if (obj.find('robndbox') == None):
  25. obj_bnd = obj.find('bndbox')
  26. obj_xmin = obj_bnd.find('xmin')
  27. obj_ymin = obj_bnd.find('ymin')
  28. obj_xmax = obj_bnd.find('xmax')
  29. obj_ymax = obj_bnd.find('ymax')
  30. xmin = float(obj_xmin.text)
  31. ymin = float(obj_ymin.text)
  32. xmax = float(obj_xmax.text)
  33. ymax = float(obj_ymax.text)
  34. obj_bnd.remove(obj_xmin) # 删除节点
  35. obj_bnd.remove(obj_ymin)
  36. obj_bnd.remove(obj_xmax)
  37. obj_bnd.remove(obj_ymax)
  38. x0.text = str(xmin)
  39. y0.text = str(ymax)
  40. x1.text = str(xmax)
  41. y1.text = str(ymax)
  42. x2.text = str(xmax)
  43. y2.text = str(ymin)
  44. x3.text = str(xmin)
  45. y3.text = str(ymin)
  46. else:
  47. obj_bnd = obj.find('robndbox')
  48. obj_bnd.tag = 'bndbox' # 修改节点名
  49. obj_cx = obj_bnd.find('cx')
  50. obj_cy = obj_bnd.find('cy')
  51. obj_w = obj_bnd.find('w')
  52. obj_h = obj_bnd.find('h')
  53. obj_angle = obj_bnd.find('angle')
  54. cx = float(obj_cx.text)
  55. cy = float(obj_cy.text)
  56. w = float(obj_w.text)
  57. h = float(obj_h.text)
  58. angle = float(obj_angle.text)
  59. obj_bnd.remove(obj_cx) # 删除节点
  60. obj_bnd.remove(obj_cy)
  61. obj_bnd.remove(obj_w)
  62. obj_bnd.remove(obj_h)
  63. obj_bnd.remove(obj_angle)
  64. x0.text, y0.text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
  65. x1.text, y1.text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
  66. x2.text, y2.text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
  67. x3.text, y3.text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
  68. # obj.remove(obj_type) # 删除节点
  69. obj_bnd.append(x0) # 新增节点
  70. obj_bnd.append(y0)
  71. obj_bnd.append(x1)
  72. obj_bnd.append(y1)
  73. obj_bnd.append(x2)
  74. obj_bnd.append(y2)
  75. obj_bnd.append(x3)
  76. obj_bnd.append(y3)
  77. tree.write(dotaxml_file, method='xml', encoding='utf-8') # 更新xml文件
  78. # 转换成四点坐标
  79. def rotatePoint(xc, yc, xp, yp, theta):
  80. xoff = xp - xc;
  81. yoff = yp - yc;
  82. cosTheta = math.cos(theta)
  83. sinTheta = math.sin(theta)
  84. pResx = cosTheta * xoff + sinTheta * yoff
  85. pResy = - sinTheta * xoff + cosTheta * yoff
  86. return str(int(xc + pResx)), str(int(yc + pResy))
  87. def totxt(xml_path, out_path):
  88. # 想要生成的txt文件保存的路径,这里可以自己修改
  89. files = os.listdir(xml_path)
  90. for file in files:
  91. tree = ET.parse(xml_path + os.sep + file)
  92. root = tree.getroot()
  93. name = file.strip('.xml')
  94. output = out_path + name + '.txt'
  95. file = open(output, 'w')
  96. objs = tree.findall('object')
  97. for obj in objs:
  98. cls = obj.find('name').text
  99. box = obj.find('bndbox')
  100. x0 = int(float(box.find('x0').text))
  101. y0 = int(float(box.find('y0').text))
  102. x1 = int(float(box.find('x1').text))
  103. y1 = int(float(box.find('y1').text))
  104. x2 = int(float(box.find('x2').text))
  105. y2 = int(float(box.find('y2').text))
  106. x3 = int(float(box.find('x3').text))
  107. y3 = int(float(box.find('y3').text))
  108. file.write("{} {} {} {} {} {} {} {} {} 0\n".format(x0, y0, x1, y1, x2, y2, x3, y3, cls))
  109. file.close()
  110. print(output)
  111. if __name__ == '__main__':
  112. # -----**** 第一步:把xml文件统一转换成旋转框的xml文件 ****-----
  113. roxml_path = "./datasets/Annotations" # 目录下保存的是需要转换的xml文件
  114. dotaxml_path = './datasets/dotaxml'
  115. filelist = os.listdir(roxml_path)
  116. for file in filelist:
  117. edit_xml(os.path.join(roxml_path, file), os.path.join(dotaxml_path, file))

三、训练自己的数据集

      本文参考yolov7-obb项目如下Egrt/yolov7-obb: 在YOLOv7的基础上使用KLD损失修改为旋转目标检测yolov7-obb (github.com)https://github.com/Egrt/yolov7-obb

  • 数据集格式 (其中Annotations为上面格式转换过的xml文件)
  1. VOCdevkit/VOC2007
  2. ├── Annotations
  3. ├── 0001.xml
  4. ├── 0002.xml
  5. .
  6. .
  7. ├── ImageSets
  8. ├── Main
  9. ├── JPEGImages
  10. ├── 0001.xml
  11. ├── 0002.xml
  12. .
  13. .
  •  运行voc_annotation.py文件,生成2007_train.txt和2007_val.txt文件

  •  开始训练

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/509474
推荐阅读
相关标签
  

闽ICP备14008679号