问题描述:
vscode连接服务器跑yolov5 train.py 报错找不到Dataset
看一下下面的missing path的路径有些不对,一般找不到啥啥啥,但这个东西你确实有的话,就是路径没放对。
可以看出生成的test.txt里面的路径有问题 前面先是项目的地址,后面又是数据集图片的地址
我们现在要做的是把前面项目的地址去掉,就是这三个生成的txt文件里只存放图像路径
应该的路径格式为
/home/zhangm/Projects/MyVOC/images/potholes
这三个生成的txt文件是xml转txt的程序里生成的,应该路径问题就出在xml转txt的程序里。
放上我的xml2yolo.py
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
from os import getcwd
sets = ['train', 'val', 'test']
classes = ["pothole","car","bus","person"] # 改成自己的类别
abs_path = os.getcwd()
#print(abs_path)
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return x, y, w, h
def convert_annotation(image_id):
in_file = open('/home/zhangm/Projects/MyVOC/Annotations/%s.xml' % (image_id), encoding='UTF-8')
out_file = open('/home/zhangm/Projects/MyVOC/labels/%s.txt' % (image_id), 'w')
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
# difficult = obj.find('difficult').text
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
b1, b2, b3, b4 = b
# 标注越界修正
if b2 > w:
b2 = w
if b4 > h:
b4 = h
b = (b1, b2, b3, b4)
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
#wd = getcwd()
for image_set in sets:
if not os.path.exists('/home/zhangm/Projects/MyVOC/labels/'):
os.makedirs('/home/zhangm/Projects/MyVOC/labels/')
image_ids = open('/home/zhangm/Projects/MyVOC/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
list_file = open('/home/zhangm/Projects/MyVOC/%s.txt' % (image_set), 'w')# 这里是mydata下生成3个txt文件
for image_id in image_ids:
list_file.write('/home/zhangm/Projects/MyVOC/Images/%s.jpg\n' % (image_id))
#list_file.write(abs_path + '/home/zhangm/Projects/MyVOC/images/%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
我们接着来看一下xml2yolo.txt
标红的这段代码是txt文件里的路径生成,前面的abs_path是项目文件的地址,去掉这个abs_path。
再运行xml2yolo.py 查看一下txt文件
前面的项目地址就没有了
其实问题很简单,但还是找不到问题所在,归咎原因还是菜鸡还不懂代码。