当前位置:   article > 正文

Yolov8_OBB斜框训练自己的数据集手把手教学_yolov8 obb

yolov8 obb

一、Yolov8环境搭建

1.下载最新的Yolov8-obb代码:

 https://github.com/ultralytics/ultralytics

2.安装配置环境

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

二、数据集制作

1.标注软件:roLabelImg

roLabelImg是基于labelImg改进的,是用来标注为VOC格式的数据,但是在labelImg的基础上增加了能够使标注的框进行旋转的功能。

2.数据格式转换

在ultralytics-main下新建一个文件夹myData设置如下结构:

其中,train_original和val_original为dota格式的标注文件,labels/train和labels/val为空,在执行完dota2obb步骤后,labels/train和labels/val则保存转换后可训练的obb格式的标注文件。

2.1 roxml2dota

  1. # 文件名称 :roxml_to_dota.py
  2. # 功能描述 :把rolabelimg标注的xml文件转换成dota能识别的xml文件,
  3. # 再转换成dota格式的txt文件
  4. # 把旋转框 cx,cy,w,h,angle,或者矩形框cx,cy,w,h,转换成四点坐标x1,y1,x2,y2,x3,y3,x4,y4
  5. import os
  6. import xml.etree.ElementTree as ET
  7. import math
  8. cls_list = ['1', 'gj', 'ladder'] #修改为自己的标签
  9. def edit_xml(xml_file, dotaxml_file):
  10. """
  11. 修改xml文件
  12. :param xml_file:xml文件的路径
  13. :return:
  14. """
  15. # dxml_file = open(xml_file,encoding='gbk')
  16. # tree = ET.parse(dxml_file).getroot()
  17. tree = ET.parse(xml_file)
  18. objs = tree.findall('object')
  19. for ix, obj in enumerate(objs):
  20. x0 = ET.Element("x0") # 创建节点
  21. y0 = ET.Element("y0")
  22. x1 = ET.Element("x1")
  23. y1 = ET.Element("y1")
  24. x2 = ET.Element("x2")
  25. y2 = ET.Element("y2")
  26. x3 = ET.Element("x3")
  27. y3 = ET.Element("y3")
  28. # obj_type = obj.find('bndbox')
  29. # type = obj_type.text
  30. # print(xml_file)
  31. if (obj.find('robndbox') == None):
  32. obj_bnd = obj.find('bndbox')
  33. obj_xmin = obj_bnd.find('xmin')
  34. obj_ymin = obj_bnd.find('ymin')
  35. obj_xmax = obj_bnd.find('xmax')
  36. obj_ymax = obj_bnd.find('ymax')
  37. # 以防有负值坐标
  38. xmin = max(float(obj_xmin.text), 0)
  39. ymin = max(float(obj_ymin.text), 0)
  40. xmax = max(float(obj_xmax.text), 0)
  41. ymax = max(float(obj_ymax.text), 0)
  42. obj_bnd.remove(obj_xmin) # 删除节点
  43. obj_bnd.remove(obj_ymin)
  44. obj_bnd.remove(obj_xmax)
  45. obj_bnd.remove(obj_ymax)
  46. x0.text = str(xmin)
  47. y0.text = str(ymax)
  48. x1.text = str(xmax)
  49. y1.text = str(ymax)
  50. x2.text = str(xmax)
  51. y2.text = str(ymin)
  52. x3.text = str(xmin)
  53. y3.text = str(ymin)
  54. else:
  55. obj_bnd = obj.find('robndbox')
  56. obj_bnd.tag = 'bndbox' # 修改节点名
  57. obj_cx = obj_bnd.find('cx')
  58. obj_cy = obj_bnd.find('cy')
  59. obj_w = obj_bnd.find('w')
  60. obj_h = obj_bnd.find('h')
  61. obj_angle = obj_bnd.find('angle')
  62. cx = float(obj_cx.text)
  63. cy = float(obj_cy.text)
  64. w = float(obj_w.text)
  65. h = float(obj_h.text)
  66. angle = float(obj_angle.text)
  67. obj_bnd.remove(obj_cx) # 删除节点
  68. obj_bnd.remove(obj_cy)
  69. obj_bnd.remove(obj_w)
  70. obj_bnd.remove(obj_h)
  71. obj_bnd.remove(obj_angle)
  72. x0.text, y0.text = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle)
  73. x1.text, y1.text = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle)
  74. x2.text, y2.text = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle)
  75. x3.text, y3.text = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle)
  76. # obj.remove(obj_type) # 删除节点
  77. obj_bnd.append(x0) # 新增节点
  78. obj_bnd.append(y0)
  79. obj_bnd.append(x1)
  80. obj_bnd.append(y1)
  81. obj_bnd.append(x2)
  82. obj_bnd.append(y2)
  83. obj_bnd.append(x3)
  84. obj_bnd.append(y3)
  85. tree.write(dotaxml_file, method='xml', encoding='utf-8') # 更新xml文件
  86. # 转换成四点坐标
  87. def rotatePoint(xc, yc, xp, yp, theta):
  88. xoff = xp - xc;
  89. yoff = yp - yc;
  90. cosTheta = math.cos(theta)
  91. sinTheta = math.sin(theta)
  92. pResx = cosTheta * xoff + sinTheta * yoff
  93. pResy = - sinTheta * xoff + cosTheta * yoff
  94. return str(int(xc + pResx)), str(int(yc + pResy))
  95. def totxt(xml_path, out_path):
  96. # 想要生成的txt文件保存的路径,这里可以自己修改
  97. files = os.listdir(xml_path)
  98. i = 0
  99. for file in files:
  100. tree = ET.parse(xml_path + os.sep + file)
  101. root = tree.getroot()
  102. name = file.split('.')[0]
  103. output = out_path + '\\' + name + '.txt'
  104. file = open(output, 'w')
  105. i = i + 1
  106. objs = tree.findall('object')
  107. for obj in objs:
  108. cls = obj.find('name').text
  109. box = obj.find('bndbox')
  110. x0 = int(float(box.find('x0').text))
  111. y0 = int(float(box.find('y0').text))
  112. x1 = int(float(box.find('x1').text))
  113. y1 = int(float(box.find('y1').text))
  114. x2 = int(float(box.find('x2').text))
  115. y2 = int(float(box.find('y2').text))
  116. x3 = int(float(box.find('x3').text))
  117. y3 = int(float(box.find('y3').text))
  118. if x0 < 0:
  119. x0 = 0
  120. if x1 < 0:
  121. x1 = 0
  122. if x2 < 0:
  123. x2 = 0
  124. if x3 < 0:
  125. x3 = 0
  126. if y0 < 0:
  127. y0 = 0
  128. if y1 < 0:
  129. y1 = 0
  130. if y2 < 0:
  131. y2 = 0
  132. if y3 < 0:
  133. y3 = 0
  134. for cls_index, cls_name in enumerate(cls_list):
  135. if cls == cls_name:
  136. file.write("{} {} {} {} {} {} {} {} {} {}\n".format(x0, y0, x1, y1, x2, y2, x3, y3, cls, cls_index))
  137. file.close()
  138. # print(output)
  139. print(i)
  140. if __name__ == '__main__':
  141. # -----**** 第一步:把xml文件统一转换成旋转框的xml文件 ****-----
  142. roxml_path = r'E:\CodeProject\ultralytics-main-OBB\data_transfor\org_xml'
  143. dotaxml_path = r'E:\CodeProject\ultralytics-main-OBB\data_transfor\dota_xml'
  144. out_path = r'E:\CodeProject\ultralytics-main-OBB\data_transfor\dota_txt'
  145. filelist = os.listdir(roxml_path)
  146. for file in filelist:
  147. edit_xml(os.path.join(roxml_path, file), os.path.join(dotaxml_path, file))
  148. # -----**** 第二步:把旋转框xml文件转换成txt格式 ****-----
  149. totxt(dotaxml_path, out_path)

2.2 dota2obb

  1. import sys
  2. sys.path.append('/home/code/wll/code/ultralytics-main-OBB/ultralytics')
  3. from ultralytics.data.converter import convert_dota_to_yolo_obb
  4. convert_dota_to_yolo_obb('/home/code/wll/code/ultralytics-main-OBB/myData')
  5. #关于dataobb文件下的目录下面会详细说明

三、配置文件设置

3.1 新建my-dota8-obb.yaml

3.2 修改yolov8-obb.yaml

只需将nc修改为自己的类别数量。

四、训练

4.1 下载预训练权重

4.2 训练

yolo obb train data=/home/code/wll/code/ultralytics-main-OBB/ultralytics/cfg/datasets/my-dota8-obb.yaml model=yolov8s-obb.pt epochs=200 imgsz=640 device=0,1,2,3

五、验证

yolo obb val model=best-obb.pt data=/home/code/wll/code/ultralytics-main-OBB/ultralytics/cfg/datasets/my-dota8-obb.yaml 

六、推理

yolo obb predict model=yolov8n-obb.pt source='https://ultralytics.com/images/bus.jpg'

七、导出为onnx

yolo export model=yolov8n-obb.pt format=onnx

参考博客:

全网首发!Yolov8_obb旋转框训练、测试、推理手把手教学(DOTA1.0数据集map50已达80%)_yolov8 obb-CSDN博客

记录yolov8_obb训练自己的数据集_yolov8 obb训练自己的数据集-CSDN博客

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

闽ICP备14008679号