本文共 3828 字,大约阅读时间需要 12 分钟。
在darknet版的yoloV4中。data文件中train.txt中只提供了图像的路径,那标签文件是怎么对应读取的呢?
在源码中,找到下面这个函数可以看到:
void replace_image_to_label(const char* input_path, char* output_path){ find_replace(input_path, "/images/train2014/", "/labels/train2014/", output_path); // COCO find_replace(output_path, "/images/val2014/", "/labels/val2014/", output_path); // COCO find_replace(output_path, "/JPEGImages/", "/labels/", output_path); // PascalVOC find_replace(output_path, "\\images\\train2014\\", "\\labels\\train2014\\", output_path); // COCO find_replace(output_path, "\\images\\val2014\\", "\\labels\\val2014\\", output_path); // COCO find_replace(output_path, "\\JPEGImages\\", "\\labels\\", output_path); // PascalVOC //find_replace(output_path, "/images/", "/labels/", output_path); // COCO //find_replace(output_path, "/VOC2007/JPEGImages/", "/VOC2007/labels/", output_path); // PascalVOC //find_replace(output_path, "/VOC2012/JPEGImages/", "/VOC2012/labels/", output_path); // PascalVOC //find_replace(output_path, "/raw/", "/labels/", output_path); trim(output_path); // replace only ext of files find_replace_extension(output_path, ".jpg", ".txt", output_path); find_replace_extension(output_path, ".JPG", ".txt", output_path); // error find_replace_extension(output_path, ".jpeg", ".txt", output_path); find_replace_extension(output_path, ".JPEG", ".txt", output_path); find_replace_extension(output_path, ".png", ".txt", output_path); find_replace_extension(output_path, ".PNG", ".txt", output_path); find_replace_extension(output_path, ".bmp", ".txt", output_path); find_replace_extension(output_path, ".BMP", ".txt", output_path); find_replace_extension(output_path, ".ppm", ".txt", output_path); find_replace_extension(output_path, ".PPM", ".txt", output_path); find_replace_extension(output_path, ".tiff", ".txt", output_path); find_replace_extension(output_path, ".TIFF", ".txt", output_path); // Check file ends with txt: if(strlen(output_path) > 4) { char *output_path_ext = output_path + strlen(output_path) - 4; if( strcmp(".txt", output_path_ext) != 0){ fprintf(stderr, "Failed to infer label file name (check image extension is supported): %s \n", output_path); } }else{ fprintf(stderr, "Label file name is too short: %s \n", output_path); }}
以PascalVOC数据集为例,程序会自动把图像路径中的/JPEGImages/ 替换为 /labels/
然后把文件扩展名替换为txt 就得到对应的标签文件路径了所以我们在任一路径下可以新建两个文件夹
-JPEGImages #放入所有的训练图片文件-labels #放入所有的txt文件,会自动生成此文件夹后来想想,只要得到图像的全路径就可以,文件夹都不用创建,标签文件也不用分开,直接放在一个文件夹,执行下面的程序,会自动给你划分出训练集和验证集,更简单!
执行下面的Python程序
import os# iter floderdef iter_files(rootDir, extend_name): file_list = [] fileName = [] #Iterate over the roots for root,dirs,files in os.walk(rootDir): for file_name in files: root = os.path.normpath(root) #格式化路径 斜杠问题 / -> \ file_path = os.path.join(root, file_name) # 连接路径 file_path = file_path.replace("\\", "/") flag = file_path.endswith(extend_name) if flag: file_list.append(file_path) fileName.append(os.path.splitext(file_name)[0]) return file_list, fileName# split train and valdef split_train_val(fileList): trainFile = open('train.txt', 'a') validFile = open('val.txt', 'a') num = 0 for files in fileList: num = num + 1 if num % 4 == 0: validFile.write(files + '\n') else: trainFile.write(files + '\n') trainFile.close() validFile.close()if __name__ == '__main__': root = 'D:/Python/images/JEPGImages' #训练图像的路径 [fileList, fileName] = iter_files(root, 'png') split_train_val(fileList)
这样我们就直接得到了训练集和验证集的txt文件,前提是你直接有txt格式的标注文件,不是xml的。不然还是需要之前的那篇教程。
后面就简简单单改几个地方,就能训练起来了!
转载地址:http://rxwni.baihongyu.com/