当前位置:   article > 正文

基于YOLOV5的道路损伤(GRDDC‘2020)检测_道路缺陷d00

道路缺陷d00

1. GRDDC'2020 数据集介绍        

GRDDC'2020 数据集是从印度、日本和捷克收集的道路图像。包括三个部分:Train, Test1, Test2。训练集包括带有 PASCAL VOC 格式 XML 文件标注的道路图像。

缺陷类型:D00、D01、D11、D10、D20、D40、D43、D44、D50、D0w0 

1.2数据集重新划分

通过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.8
  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.3 通过voc_label.py得到生成适合yolo的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 = ["D00","D01","D11","D10","D20","D40","D43","D44","D50","D0w0"]
  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 == 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()

2.基于yolov5的道路损伤检测

2.1 yolov5网络结构展示

 2.2本文选择yolov5作为检测模型

2.2.1 修改road_crack.yaml

  1. # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
  2. train: ./road_crack_voc/train.txt # 16551 images
  3. val: ./road_crack_voc/val.txt # 4952 images
  4. # number of classes
  5. nc: 10
  6. # class names
  7. names: ['D00','D01','D11','D10','D20','D40','D43','D44','D50','D0w0']

2.2.2 修改train.py

  1. parser = argparse.ArgumentParser()
  2. parser.add_argument('--weights', type=str, default=ROOT / 'weights/yolov5s.pt', help='initial weights path')
  3. parser.add_argument('--cfg', type=str, default='models/yolov5s_road_crack.yaml', help='model.yaml path')
  4. parser.add_argument('--data', type=str, default=ROOT / 'data/road_crack.yaml', help='dataset.yaml path')
  5. parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path')
  6. parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
  7. parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
  8. parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
  9. parser.add_argument('--rect', action='store_true', help='rectangular training')
  10. parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
  11. parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
  12. parser.add_argument('--noval', action='store_true', help='only validate final epoch')
  13. parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
  14. parser.add_argument('--noplots', action='store_true', help='save no plot files')
  15. parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
  16. parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
  17. parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
  18. parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
  19. parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
  20. parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
  21. parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
  22. parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW'], default='SGD', help='optimizer')
  23. parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
  24. parser.add_argument('--workers', type=int, default=0, help='max dataloader workers (per RANK in DDP mode)')
  25. parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name')
  26. parser.add_argument('--name', default='exp', help='save to project/name')
  27. parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
  28. parser.add_argument('--quad', action='store_true', help='quad dataloader')
  29. parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
  30. parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
  31. parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
  32. parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
  33. parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
  34. parser.add_argument('--seed', type=int, default=0, help='Global training seed')
  35. parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')

2.2.3 yolov5s_road_crack.yaml

仅仅修改了nc:10(共有十类)

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