1.环境搭建:利用anaconda创建虚拟环境,在pycharm里面,利用终端输入以下代码:
pip install -r requirements.txt
安装好对应的依赖包。
2.数据集的准备:用的是自己课题组的traffic_sign数据集。(JPEGImages+Annnotation)打上标签的为.xml格式,此处.txt格式容易报错,推荐保存为.xml格式。利用如下代码进行数据集的划分以及将.xml转为.txt格式。
"""
@author:Guo Tianfeng
@contact:Guotf1999@163.com
@software:PyCharm
@file:yoloToVOC.py.py
@time:2023/3/11 10:05
"""
from xml.dom.minidom import Document
import os
import cv2
def makexml(picPath, txtPath, xmlPath):# txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
"""此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件
在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
"""
dic = {'0': "1", # 创建字典用来对类型进行转换
'1': "2", # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
'2': "4",
'3': "5",
'4': "P",
'5': "W",
}
files = os.listdir(txtPath)
for i, name in enumerate(files):
xmlBuilder = Document()
annotation = xmlBuilder.createElement("annotation") # 创建annotation标签
xmlBuilder.appendChild(annotation)
txtFile = open(txtPath + name)
txtList = txtFile.readlines()
img = cv2.imread(picPath + name[0:-4] + ".jpg")
Pheight, Pwidth, Pdepth = img.shape
folder = xmlBuilder.createElement("folder") # folder标签
foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
folder.appendChild(foldercontent)
annotation.appendChild(folder) # folder标签结束
filename = xmlBuilder.createElement("filename") # filename标签
filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
filename.appendChild(filenamecontent)
annotation.appendChild(filename) # filename标签结束
size = xmlBuilder.createElement("size") # size标签
width = xmlBuilder.createElement("width") # size子标签width
widthcontent = xmlBuilder.createTextNode(str(Pwidth))
width.appendChild(widthcontent)
size.appendChild(width) # size子标签width结束
height = xmlBuilder.createElement("height") # size子标签height
heightcontent = xmlBuilder.createTextNode(str(Pheight))
height.appendChild(heightcontent)
size.appendChild(height) # size子标签height结束
depth = xmlBuilder.createElement("depth") # size子标签depth
depthcontent = xmlBuilder.createTextNode(str(Pdepth))
depth.appendChild(depthcontent)
size.appendChild(depth) # size子标签depth结束
annotation.appendChild(size) # size标签结束
for j in txtList:
oneline = j.strip().split(" ")
object = xmlBuilder.createElement("object") # object 标签
picname = xmlBuilder.createElement("name") # name标签
namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
picname.appendChild(namecontent)
object.appendChild(picname) # name标签结束
pose = xmlBuilder.createElement("pose") # pose标签
posecontent = xmlBuilder.createTextNode("Unspecified")
pose.appendChild(posecontent)
object.appendChild(pose) # pose标签结束
truncated = xmlBuilder.createElement("truncated") # truncated标签
truncatedContent = xmlBuilder.createTextNode("0")
truncated.appendChild(truncatedContent)
object.appendChild(truncated) # truncated标签结束
difficult = xmlBuilder.createElement("difficult") # difficult标签
difficultcontent = xmlBuilder.createTextNode("0")
difficult.appendChild(difficultcontent)
object.appendChild(difficult) # difficult标签结束
bndbox = xmlBuilder.createElement("bndbox") # bndbox标签
xmin = xmlBuilder.createElement("xmin") # xmin标签
mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)
xminContent = xmlBuilder.createTextNode(str(mathData))
xmin.appendChild(xminContent)
bndbox.appendChild(xmin) # xmin标签结束
ymin = xmlBuilder.createElement("ymin") # ymin标签
mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)
yminContent = xmlBuilder.createTextNode(str(mathData))
ymin.appendChild(yminContent)
bndbox.appendChild(ymin) # ymin标签结束
xmax = xmlBuilder.createElement("xmax") # xmax标签
mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)
xmaxContent = xmlBuilder.createTextNode(str(mathData))
xmax.appendChild(xmaxContent)
bndbox.appendChild(xmax) # xmax标签结束
ymax = xmlBuilder.createElement("ymax") # ymax标签
mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)
ymaxContent = xmlBuilder.createTextNode(str(mathData))
ymax.appendChild(ymaxContent)
bndbox.appendChild(ymax) # ymax标签结束
object.appendChild(bndbox) # bndbox标签结束
annotation.appendChild(object) # object标签结束
f = open(xmlPath + name[0:-4] + ".xml", 'w')
xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
f.close()
if __name__ == "__main__":
picPath = "E:/desktop/yolov5-master/VOCdevkit/VOC2007/JPEGImages/" # 图片所在文件夹路径,后面的/一定要带上
txtPath = "E:/desktop/yolov5-master/VOCdevkit/VOC2007/Annatations/" # txt所在文件夹路径,后面的/一定要带上
xmlPath = "E:/desktop/yolov5-master/VOCdevkit/VOC2007/xml_ana/" # xml文件保存路径,后面的/一定要带上
makexml(picPath, txtPath, xmlPath)
注:上述代码需要修改一些地方:创建字典用来对类型进行转换,此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致。代码最后修改好自己的路径(推荐使用绝对路径)
3.笔者选取的是YOLOv5-5.0附上链接:ultralytics/yolov5 at v5.0 (github.com)
3.1随后在运行train.py程序时遭遇报错:TypeError: No loop matching the specified signature and casting was found for ufunc greater,如下图所示。
解决方法如下:更换numpy,很大一部分原因是因为python包的版本不兼容引起的。笔者通过以下代码查询当前包的版本:
pip list
查询到当前的numpy版本为1.24.2
pip uninstall numpy
pip install numpy==1.23.5
安装上了1.23.5的numpy成功解决报错。
3.2报错二:RuntimeError: result type Float can't be cast to the desired output type __int64
解决方法借鉴这位老哥:http://t.csdn.cn/zsJ7s
至此,成功开始train自己的dataset
文末附上训练图片。