深度学习和目标检测系列教程 17-尊龙游戏旗舰厅官网
@author:runsen
yolo 是目前最火爆的的计算机视觉算法之一,今天使用的数据集来源:https://www.kaggle.com/andrewmvd/face-mask-detection
这是数据集可以创建一个模型来检测戴口罩、不戴口罩或不正确戴口罩的人。
该数据集包含属于3 个类别的853张图像,以及它们的 pascal voc 格式的边界框。分别是
- 带口罩;
- 不戴口罩;
- 口罩佩戴不正确。
首先将voc形式变成yolo形式,voc是xml文件,需要通过elementtree进行解析,得到bnd_box ,具体解析代码如下
import os from xml.etree.elementtree import elementtreedef xml_to_yolo(path):root = elementtree().parse(path)img_path = root.find('filename').text.replace('png','txt')img_size = root.find('size')width = int(img_size.find('width').text)height = int(img_size.find('height').text)with open('../dataset/label/' img_path, 'w') as f:lines = []for node in root.findall('object'):object_ = dict(class_=none, x=none, y=none, width=none, height=none)# classclass_name = node.find('name').textif(class_name == 'without_mask'):object_['class_'] = '0'elif(class_name == 'with_mask'):object_['class_'] = '1'else:object_['class_'] = '2'# bounding boxbnd_box = node.find("bndbox")x_min = float(bnd_box[0].text)y_min = float(bnd_box[1].text)x_max = float(bnd_box[2].text)y_max = float(bnd_box[3].text)dw = float(1/width)dh = float(1/height)w = float(x_max - x_min)h = float(y_max - y_min)x = float((x_min x_max)/2 -1)y = float((y_min y_max)/2 -1)w = float(w * dw)h = float(h * dh)x = float(x * dw)y = float(y * dh)object_['x'] = str(x)object_['y'] = str(y)object_['width'] = str(w)object_['height'] = str(h)line = object_['class_'] ' ' object_['x'] ' ' object_['y'] ' ' object_['width'] ' ' object_['height']lines.append(line)lines.append('\n')for line in lines[:-1]:f.write(line) f.closedef process_data():img_paths = []for dirname, _, filenames in os.walk('../dataset/images'):for filename in filenames:img_paths.append(os.path.join('../dataset/images', filename)) # local machine# xml for dirname, _, filenames in os.walk('../dataset/xml'):for filename in filenames:annotation_path = (os.path.join(dirname, filename))xml_to_yolo(annotation_path)process_data()克隆github的yolov5的代码:https://github.com/ultralytics/yolov5。
为了跟数据集的目录保持一致
在dataset新建images和labels文件夹存放对于的文件。
data文件夹下的data.yaml文件
train: ./dataset/images/train val: ./dataset/images/val# number of classes nc: 3# class names names: ['without_mask','with_mask','mask_weared_incorrect']model文件夹下的yolov5s.yaml文件
# parameters nc: 3 # number of classes depth_multiple: 0.33 # model depth multiple width_multiple: 0.50 # layer channel multiple anchors:- [10,13, 16,30, 33,23] # p3/8- [30,61, 62,45, 59,119] # p4/16- [116,90, 156,198, 373,326] # p5/32# yolov5 backbone backbone:# [from, number, module, args][[-1, 1, focus, [64, 3]], # 0-p1/2[-1, 1, conv, [128, 3, 2]], # 1-p2/4[-1, 3, c3, [128]],[-1, 1, conv, [256, 3, 2]], # 3-p3/8[-1, 9, c3, [256]],[-1, 1, conv, [512, 3, 2]], # 5-p4/16[-1, 9, c3, [512]],[-1, 1, conv, [1024, 3, 2]], # 7-p5/32[-1, 1, spp, [1024, [5, 9, 13]]],[-1, 3, c3, [1024, false]], # 9]# yolov5 head head:[[-1, 1, conv, [512, 1, 1]],[-1, 1, nn.upsample, [none, 2, 'nearest']],[[-1, 6], 1, concat, [1]], # cat backbone p4[-1, 3, c3, [512, false]], # 13[-1, 1, conv, [256, 1, 1]],[-1, 1, nn.upsample, [none, 2, 'nearest']],[[-1, 4], 1, concat, [1]], # cat backbone p3[-1, 3, c3, [256, false]], # 17 (p3/8-small)[-1, 1, conv, [256, 3, 2]],[[-1, 14], 1, concat, [1]], # cat head p4[-1, 3, c3, [512, false]], # 20 (p4/16-medium)[-1, 1, conv, [512, 3, 2]],[[-1, 10], 1, concat, [1]], # cat head p5[-1, 3, c3, [1024, false]], # 23 (p5/32-large)[[17, 20, 23], 1, detect, [nc, anchors]], # detect(p3, p4, p5)]下载yolov5s.pt对应的权重,大约14m
训练代码:python train.py --batch 16 --epochs 50 --data /data/data.yaml --cfg /models/yolov5s.yaml
训练结果
关于p r map50 和map95的指标
map50,map60,map70……等等指的是取detector的iou阈值大于0.5,大于0.6,大于0.7…的比例。数值越高,越好,如果阈值越大,精度会变低。
因为iou大于0.95的小于iou大于0.5
yolov5训练完,会生成runs的文件夹
训练结束会生成train文件夹,第一次训练完成一般默认是exp,如果再训练一次,就是exp2。exp19是我在不断的改batch 和epochs。
50次训练的指标结果
epoch, train/box_loss, train/obj_loss, train/cls_loss, metrics/precision, metrics/recall, metrics/map_0.5,metrics/map_0.5:0.95, val/box_loss, val/obj_loss, val/cls_loss, x/lr0, x/lr1, x/lr20, 0.11183, 0.061692, 0.036166, 0.0040797, 0.023207, 0.00094919, 0.00015348, 0.088802, 0.044058, 0.029341, 0.00053, 0.00053, 0.095231, 0.090971, 0.06624, 0.024651, 0.44734, 0.13502, 0.11474, 0.0279, 0.067798, 0.037852, 0.022663, 0.0010692, 0.0010692, 0.0903692, 0.070342, 0.052357, 0.02153, 0.85191, 0.20848, 0.22242, 0.077028, 0.055487, 0.027934, 0.019595, 0.0016049, 0.0016049, 0.0855053, 0.0699, 0.043899, 0.020244, 0.87295, 0.16442, 0.20657, 0.078501, 0.055767, 0.024396, 0.018845, 0.0021348, 0.0021348, 0.0806354, 0.064317, 0.04001, 0.018747, 0.83973, 0.19818, 0.20992, 0.068715, 0.055507, 0.022381, 0.018095, 0.0026562, 0.0026562, 0.0757565, 0.060517, 0.040462, 0.01871, 0.92677, 0.26977, 0.31691, 0.13554, 0.046723, 0.020782, 0.017638, 0.0031668, 0.0031668, 0.0708676, 0.055565, 0.037719, 0.017239, 0.56328, 0.42257, 0.34731, 0.14537, 0.042455, 0.020308, 0.015934, 0.0036641, 0.0036641, 0.0659647, 0.051137, 0.036675, 0.01443, 0.70063, 0.59002, 0.56658, 0.28446, 0.037626, 0.018761, 0.012734, 0.0041459, 0.0041459, 0.0610468, 0.051564, 0.036086, 0.010754, 0.75331, 0.59469, 0.57015, 0.21859, 0.044253, 0.018153, 0.010504, 0.00461, 0.00461, 0.056119, 0.054336, 0.036489, 0.01014, 0.81292, 0.53958, 0.57799, 0.28552, 0.037579, 0.018368, 0.009468, 0.0050544, 0.0050544, 0.05115410, 0.052323, 0.034572, 0.0095445, 0.85048, 0.62312, 0.6267, 0.338, 0.031516, 0.017655, 0.010057, 0.005477, 0.005477, 0.04617711, 0.050382, 0.035035, 0.0086806, 0.86829, 0.59491, 0.61092, 0.3379, 0.032883, 0.017608, 0.0090742, 0.0058761, 0.0058761, 0.04117612, 0.046103, 0.035051, 0.0082707, 0.8746, 0.60331, 0.72019, 0.42562, 0.030186, 0.016849, 0.0087845, 0.00625, 0.00625, 0.0361513, 0.045144, 0.032905, 0.0071727, 0.50942, 0.80319, 0.76972, 0.38975, 0.033539, 0.016185, 0.0083475, 0.0065973, 0.0065973, 0.03109714, 0.043005, 0.031431, 0.0076075, 0.94694, 0.59886, 0.75893, 0.45433, 0.028811, 0.016204, 0.0087185, 0.0069167, 0.0069167, 0.02601715, 0.042583, 0.032493, 0.0075345, 0.54628, 0.78986, 0.70483, 0.39052, 0.034077, 0.016426, 0.010784, 0.007207, 0.007207, 0.02090716, 0.044892, 0.032174, 0.0079312, 0.93707, 0.62972, 0.75324, 0.417, 0.031565, 0.015583, 0.0074945, 0.0074674, 0.0074674, 0.01576717, 0.04351, 0.033322, 0.0071257, 0.9655, 0.60346, 0.75329, 0.43919, 0.029984, 0.015935, 0.0071412, 0.0076971, 0.0076971, 0.01059718, 0.041603, 0.032276, 0.0070445, 0.56572, 0.84931, 0.80283, 0.50439, 0.027445, 0.015567, 0.0067956, 0.0077031, 0.0077031, 0.007703119, 0.03878, 0.031363, 0.0064713, 0.57055, 0.85292, 0.81709, 0.54818, 0.02845, 0.014806, 0.006212, 0.0077031, 0.0077031, 0.007703120, 0.037742, 0.030032, 0.0062044, 0.57265, 0.90265, 0.83532, 0.52484, 0.024745, 0.01497, 0.0060971, 0.0074725, 0.0074725, 0.007472521, 0.035583, 0.029891, 0.0062017, 0.6255, 0.95098, 0.87989, 0.55897, 0.026561, 0.014704, 0.0072068, 0.0072361, 0.0072361, 0.007236122, 0.034756, 0.030238, 0.0059138, 0.76515, 0.85503, 0.93007, 0.62612, 0.02538, 0.014571, 0.0066402, 0.0069948, 0.0069948, 0.006994823, 0.033698, 0.030118, 0.0058317, 0.71699, 0.99118, 0.96047, 0.68458, 0.024377, 0.01393, 0.0055267, 0.0067495, 0.0067495, 0.006749524, 0.031946, 0.029104, 0.0052597, 0.80874, 0.98185, 0.97812, 0.6923, 0.024714, 0.014598, 0.0056557, 0.0065013, 0.0065013, 0.006501325, 0.031391, 0.029652, 0.0048984, 0.74641, 0.99789, 0.97365, 0.64525, 0.024137, 0.013945, 0.0055031, 0.0062512, 0.0062512, 0.006251226, 0.029442, 0.028553, 0.005284, 0.77383, 0.94401, 0.96556, 0.72068, 0.022413, 0.013277, 0.0049306, 0.006, 0.006, 0.00627, 0.029758, 0.027931, 0.0052183, 0.85002, 0.95022, 0.98664, 0.72732, 0.022897, 0.013339, 0.005281, 0.0057488, 0.0057488, 0.005748828, 0.027972, 0.028458, 0.0046879, 0.89455, 0.99578, 0.99146, 0.70404, 0.022004, 0.013255, 0.0037246, 0.0054987, 0.0054987, 0.005498729, 0.027527, 0.026557, 0.0043153, 0.95, 0.97609, 0.98743, 0.67707, 0.022925, 0.013022, 0.0033831, 0.0052505, 0.0052505, 0.005250530, 0.028249, 0.028263, 0.0041167, 0.97478, 0.98066, 0.99444, 0.72056, 0.020498, 0.012977, 0.0044037, 0.0050052, 0.0050052, 0.005005231, 0.027591, 0.026807, 0.0039948, 0.99205, 0.97724, 0.99428, 0.68932, 0.021719, 0.01319, 0.0033467, 0.0047639, 0.0047639, 0.004763932, 0.027873, 0.027414, 0.0041063, 0.93928, 0.97948, 0.99237, 0.71491, 0.021192, 0.012699, 0.0026829, 0.0045275, 0.0045275, 0.004527533, 0.026139, 0.025726, 0.0036621, 0.98341, 0.94802, 0.99364, 0.7167, 0.020188, 0.012794, 0.0028303, 0.0042969, 0.0042969, 0.004296934, 0.025655, 0.026828, 0.0033113, 0.97988, 0.9678, 0.99428, 0.7098, 0.019681, 0.012586, 0.0024685, 0.004073, 0.004073, 0.00407335, 0.025622, 0.026283, 0.0033939, 0.98079, 0.96134, 0.9945, 0.73714, 0.020264, 0.012446, 0.0022277, 0.0038567, 0.0038567, 0.003856736, 0.02521, 0.025613, 0.003236, 0.98049, 0.98546, 0.99512, 0.71957, 0.019993, 0.012117, 0.0020501, 0.0036489, 0.0036489, 0.003648937, 0.024812, 0.026664, 0.0031498, 0.98609, 0.98551, 0.99503, 0.73326, 0.018914, 0.012313, 0.0019162, 0.0034503, 0.0034503, 0.003450338, 0.024114, 0.025925, 0.0029253, 0.987, 0.98471, 0.99513, 0.74502, 0.018339, 0.012051, 0.0018452, 0.0032618, 0.0032618, 0.003261839, 0.02411, 0.023745, 0.0030621, 0.98562, 0.981, 0.99503, 0.75325, 0.018894, 0.012225, 0.0019048, 0.0030841, 0.0030841, 0.003084140, 0.023838, 0.02533, 0.002918, 0.98616, 0.98312, 0.99524, 0.7713, 0.018355, 0.011694, 0.0017296, 0.0029179, 0.0029179, 0.002917941, 0.024228, 0.025735, 0.0031651, 0.97865, 0.98523, 0.99515, 0.77026, 0.018076, 0.011688, 0.0021994, 0.0027639, 0.0027639, 0.002763942, 0.023581, 0.025971, 0.0027009, 0.98297, 0.9877, 0.99519, 0.7426, 0.018612, 0.011757, 0.0014865, 0.0026227, 0.0026227, 0.002622743, 0.02345, 0.025302, 0.0028664, 0.97805, 0.98938, 0.99525, 0.76359, 0.018273, 0.011761, 0.0013719, 0.0024948, 0.0024948, 0.002494844, 0.023614, 0.024621, 0.0028453, 0.99051, 0.98734, 0.99537, 0.75192, 0.017944, 0.01167, 0.0012174, 0.0023807, 0.0023807, 0.002380745, 0.023214, 0.024148, 0.0023234, 0.98904, 0.98296, 0.99532, 0.75336, 0.018311, 0.011531, 0.0011202, 0.0022809, 0.0022809, 0.002280946, 0.022992, 0.025107, 0.0026533, 0.98561, 0.98809, 0.99534, 0.76908, 0.017906, 0.011331, 0.0011833, 0.0021958, 0.0021958, 0.002195847, 0.023114, 0.023664, 0.0026155, 0.9892, 0.98336, 0.99531, 0.76103, 0.018336, 0.011521, 0.0011389, 0.0021257, 0.0021257, 0.002125748, 0.02274, 0.02358, 0.002211, 0.98464, 0.98066, 0.99535, 0.76574, 0.017788, 0.011413, 0.0011274, 0.0020709, 0.0020709, 0.002070949, 0.023034, 0.024154, 0.0021493, 0.98092, 0.98945, 0.99529, 0.75593, 0.017796, 0.011467, 0.00096875, 0.0020315, 0.0020315, 0.0020315测试代码:python detect.py --weights runs/train/exp19/weights/best.pt --img 512 --conf 0.4 --source dataset/images/val --save-txt
预测结束,会在runs文件夹生成detect文件夹
如果加了–save-txt,就有对应的训练样本的yolo格式
下面的几个测试结果
附上数据集和训练结果:
链接:https://pan.baidu.com/s/1xqtlbo5kpzzych2yqm0s1g
提取码:toxf
总结
以上是尊龙游戏旗舰厅官网为你收集整理的深度学习和目标检测系列教程 17-300: 3 个类别面罩检测类别数据集训练yolov5s模型的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: