参考:https://blog.csdn.net/laovife/article/details/106802725
代码地址:https://github.com/ultralytics/YOLOv5
关于多分类:好像有个标签单独标注是否是物体,再有one-hot分类是哪类物体
有一个文件.shapes,里面放的每个文件的宽高
数据集准备
yolov5使用的是yolo格式的标注文件,内容长这样,第一个数是标签的序号,后面四个是坐标。
坐标格式:xc yc w h,都是百分比
load_image:
最大边放大到640,同比例缩放
坐标框处理:
if x.size > 0:
# Normalized xywh to pixel xyxy format
labels = x.copy()
labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width
labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height
labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0]
labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1]
在下面两个函数中都有处理,送入算法转xc yc w h形式。
getitem
load_mosaic
标注软件依然是labelimg,在使用前将VOC格式转换为YOLO即可
如果有之前标注好的xml文件,可以通过脚本直接转成yolo所需的txt格式: link.
不过在转换完成后记得添加labels文件,标注文件根据序号从labels里面对应标签。
到此数据集准备完毕,在data/coco128.yaml文件里,修改为自己的参数,到这一步就可以尝试train。
coco128.yaml:
train: ./data/coco128/images/train2017/
val: ./data/coco128/images/train2017/
# number of classes
nc: 80
# class names
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush']
三.参数调整
yolov5提供了几种权重供选择,其中5l的性价比最高,适合CV爱好者日常研究;5x效果最好,如果硬件配置低,还可以选用只有27M的5s
在train.py修改你选用的权重,并前往权重文件中将nc改为和你样本库匹配的值。
根据要求修改epoch和batchsize,就可以开始初步的训练了。