当前位置:   article > 正文

基于Yolov8的野外烟雾检测(1)_yolo火灾检测

yolo火灾检测

目录

 1.Yolov8介绍

2.野外火灾烟雾数据集介绍

2.1数据集划分

1.2 通过voc_label.py得到适合yolov8需要的txt

2.3生成内容如下

3.训练结果分析

4.系列篇


 1.Yolov8介绍

         Ultralytics YOLOv8是Ultralytics公司开发的YOLO目标检测和图像分割模型的最新版本。YOLOv8是一种尖端的、最先进的(SOTA)模型,它建立在先前YOLO成功基础上,并引入了新功能和改进,以进一步提升性能和灵活性。它可以在大型数据集上进行训练,并且能够在各种硬件平台上运行,从CPU到GPU。

具体改进如下:

  1. Backbone:使用的依旧是CSP的思想,不过YOLOv5中的C3模块被替换成了C2f模块,实现了进一步的轻量化,同时YOLOv8依旧使用了YOLOv5等架构中使用的SPPF模块;

  2. PAN-FPN:毫无疑问YOLOv8依旧使用了PAN的思想,不过通过对比YOLOv5与YOLOv8的结构图可以看到,YOLOv8将YOLOv5中PAN-FPN上采样阶段中的卷积结构删除了,同时也将C3模块替换为了C2f模块;

  3. Decoupled-Head:是不是嗅到了不一样的味道?是的,YOLOv8走向了Decoupled-Head;

  4. Anchor-Free:YOLOv8抛弃了以往的Anchor-Base,使用了Anchor-Free的思想;

  5. 损失函数:YOLOv8使用VFL Loss作为分类损失,使用DFL Loss+CIOU Loss作为分类损失;

  6. 样本匹配:YOLOv8抛弃了以往的IOU匹配或者单边比例的分配方式,而是使用了Task-Aligned Assigner匹配方式

框架图提供见链接:Brief summary of YOLOv8 model structure · Issue #189 · ultralytics/ultralytics · GitHub

2.野外火灾烟雾数据集介绍

数据集大小737张,train:val:test 随机分配为7:2:1,类别:smoke

2.1数据集划分

通过split_train_val.py得到trainval.txt、val.txt、test.txt  

  1. # coding:utf-8
  2. import os
  3. import random
  4. import argparse
  5. parser = argparse.ArgumentParser()
  6. #xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
  7. parser.add_argument('--xml_path', default='Annotations', type=str, help='input xml label path')
  8. #数据集的划分,地址选择自己数据下的ImageSets/Main
  9. parser.add_argument('--txt_path', default='ImageSets/Main', type=str, help='output txt label path')
  10. opt = parser.parse_args()
  11. trainval_percent = 0.9
  12. train_percent = 0.7
  13. xmlfilepath = opt.xml_path
  14. txtsavepath = opt.txt_path
  15. total_xml = os.listdir(xmlfilepath)
  16. if not os.path.exists(txtsavepath):
  17. os.makedirs(txtsavepath)
  18. num = len(total_xml)
  19. list_index = range(num)
  20. tv = int(num * trainval_percent)
  21. tr = int(tv * train_percent)
  22. trainval = random.sample(list_index, tv)
  23. train = random.sample(trainval, tr)
  24. file_trainval = open(txtsavepath + '/trainval.txt', 'w')
  25. file_test = open(txtsavepath + '/test.txt', 'w')
  26. file_train = open(txtsavepath + '/train.txt', 'w')
  27. file_val = open(txtsavepath + '/val.txt', 'w')
  28. for i in list_index:
  29. name = total_xml[i][:-4] + '\n'
  30. if i in trainval:
  31. file_trainval.write(name)
  32. if i in train:
  33. file_train.write(name)
  34. else:
  35. file_val.write(name)
  36. else:
  37. file_test.write(name)
  38. file_trainval.close()
  39. file_train.close()
  40. file_val.close()
  41. file_test.close()

1.2 通过voc_label.py得到适合yolov8需要的txt

  1. # -*- coding: utf-8 -*-
  2. import xml.etree.ElementTree as ET
  3. import os
  4. from os import getcwd
  5. sets = ['train', 'val']
  6. classes = ["smoke"] # 改成自己的类别
  7. abs_path = os.getcwd()
  8. print(abs_path)
  9. def convert(size, box):
  10. dw = 1. / (size[0])
  11. dh = 1. / (size[1])
  12. x = (box[0] + box[1]) / 2.0 - 1
  13. y = (box[2] + box[3]) / 2.0 - 1
  14. w = box[1] - box[0]
  15. h = box[3] - box[2]
  16. x = x * dw
  17. w = w * dw
  18. y = y * dh
  19. h = h * dh
  20. return x, y, w, h
  21. def convert_annotation(image_id):
  22. in_file = open('Annotations/%s.xml' % (image_id), encoding='UTF-8')
  23. out_file = open('labels/%s.txt' % (image_id), 'w')
  24. tree = ET.parse(in_file)
  25. root = tree.getroot()
  26. size = root.find('size')
  27. w = int(size.find('width').text)
  28. h = int(size.find('height').text)
  29. for obj in root.iter('object'):
  30. difficult = obj.find('difficult').text
  31. #difficult = obj.find('Difficult').text
  32. cls = obj.find('name').text
  33. if cls not in classes or int(difficult) == 1:
  34. continue
  35. cls_id = classes.index(cls)
  36. xmlbox = obj.find('bndbox')
  37. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
  38. float(xmlbox.find('ymax').text))
  39. b1, b2, b3, b4 = b
  40. # 标注越界修正
  41. if b2 > w:
  42. b2 = w
  43. if b4 > h:
  44. b4 = h
  45. b = (b1, b2, b3, b4)
  46. bb = convert((w, h), b)
  47. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  48. wd = getcwd()
  49. for image_set in sets:
  50. if not os.path.exists('labels/'):
  51. os.makedirs('labels/')
  52. image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
  53. list_file = open('%s.txt' % (image_set), 'w')
  54. for image_id in image_ids:
  55. list_file.write(abs_path + '/images/%s.jpg\n' % (image_id))
  56. convert_annotation(image_id)
  57. list_file.close()# -*- coding: utf-8 -*-
  58. import xml.etree.ElementTree as ET
  59. import os
  60. from os import getcwd
  61. sets = ['train', 'val']
  62. classes = ["smoke"] # 改成自己的类别
  63. abs_path = os.getcwd()
  64. print(abs_path)
  65. def convert(size, box):
  66. dw = 1. / (size[0])
  67. dh = 1. / (size[1])
  68. x = (box[0] + box[1]) / 2.0 - 1
  69. y = (box[2] + box[3]) / 2.0 - 1
  70. w = box[1] - box[0]
  71. h = box[3] - box[2]
  72. x = x * dw
  73. w = w * dw
  74. y = y * dh
  75. h = h * dh
  76. return x, y, w, h
  77. def convert_annotation(image_id):
  78. in_file = open('Annotations/%s.xml' % (image_id), encoding='UTF-8')
  79. out_file = open('labels/%s.txt' % (image_id), 'w')
  80. tree = ET.parse(in_file)
  81. root = tree.getroot()
  82. size = root.find('size')
  83. w = int(size.find('width').text)
  84. h = int(size.find('height').text)
  85. for obj in root.iter('object'):
  86. difficult = obj.find('difficult').text
  87. #difficult = obj.find('Difficult').text
  88. cls = obj.find('name').text
  89. if cls not in classes or int(difficult) == 1:
  90. continue
  91. cls_id = classes.index(cls)
  92. xmlbox = obj.find('bndbox')
  93. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
  94. float(xmlbox.find('ymax').text))
  95. b1, b2, b3, b4 = b
  96. # 标注越界修正
  97. if b2 > w:
  98. b2 = w
  99. if b4 > h:
  100. b4 = h
  101. b = (b1, b2, b3, b4)
  102. bb = convert((w, h), b)
  103. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  104. wd = getcwd()
  105. for image_set in sets:
  106. if not os.path.exists('labels/'):
  107. os.makedirs('labels/')
  108. image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
  109. list_file = open('%s.txt' % (image_set), 'w')
  110. for image_id in image_ids:
  111. list_file.write(abs_path + '/images/%s.jpg\n' % (image_id))
  112. convert_annotation(image_id)
  113. list_file.close()

2.3生成内容如下

3.训练结果分析

训练结果如下:

mAP@0.5 0.839

  1. YOLOv8n summary (fused): 168 layers, 3005843 parameters, 0 gradients, 8.1 GFLOPs
  2. Class Images Instances Box(P R mAP50 mAP50-95): 100%|██████████| 4/4 [00:06<00:00, 1.55s/it]
  3. all 199 177 0.749 0.859 0.839 0.469

4.系列篇

1)基于Yolov8的野外烟雾检测

2)基于Yolov8的野外烟雾检测(2):多维协作注意模块MCA| 2023.9最新发布

3)基于Yolov8的野外烟雾检测(3):动态蛇形卷积,实现暴力涨点 | ICCV2023

4)基于Yolov8的野外烟雾检测(4):通道优先卷积注意力(CPCA) | 中科院2023最新发表 

5)  基于Yolov8的野外烟雾检测(5):Gold-YOLO,遥遥领先,超越所有YOLO

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

闽ICP备14008679号