构建 Darknet 分类器 (Tiny Darknet) 训练数据集 (color recognition 颜色识别/color classification 颜色分类)
1. CompCars_Color9_Dataset
2. parser
#
# Created by foreverstrong on 12/31/2018
#
import os
import cv2
color_classes = ["white", "gray", "black", "red", "green", "blue", "yellow", "purple", "brown"]
color_dataset_dir = "./CompCars_Color9_Dataset"
color_dataset_path = color_dataset_dir
training_data_folder = "./CompCars_Color9_Training"
def recursive_directory_creation_function(training_data_folder, color_classes):
for color_name in color_classes:
color_name_directory = training_data_folder + '/' + color_name
if not os.path.exists(color_name_directory):
os.makedirs(color_name_directory)
def main(color_dataset_path, training_data_folder, color_classes):
recursive_directory_creation_function(training_data_folder, color_classes)
for folderName, subfolders, filenames in os.walk(color_dataset_path):
print("The current folder is " + folderName)
for subfolder in subfolders:
print("SUBFOLDER OF " + folderName + ': ' + subfolder)
num = 0
for filename in filenames:
# print("FILE INSIDE " + folderName + ': ' + filename)
target_filename = filename
class_name = (folderName.split('/'))[-1]
if ".jpg" not in filename:
print(filename)
continue
oldfile = folderName + '/' + filename
try:
img = cv2.imread(oldfile)
[imgh, imgw, imgc] = img.shape
except:
print(oldfile)
continue
if class_name in color_classes:
padnum = str(num + 1)
newfile = training_data_folder + '/' + class_name + '/' + 'b' + padnum + '_' + class_name + ".jpg"
os.rename(oldfile, newfile)
# print(oldfile, ">>>>>>======>>>>>>", newfile)
num += 1
if __name__ == "__main__":
main(color_dataset_path, training_data_folder, color_classes)
3. CompCars_Color9_Training
/usr/bin/python3.5 /home/strong/training_validation_test_sets_color/rename_color_data_classification-CompCars_Color9_Dataset.py
The current folder is ./CompCars_Color9_Dataset
SUBFOLDER OF ./CompCars_Color9_Dataset: black
SUBFOLDER OF ./CompCars_Color9_Dataset: blue
SUBFOLDER OF ./CompCars_Color9_Dataset: red
SUBFOLDER OF ./CompCars_Color9_Dataset: brown
SUBFOLDER OF ./CompCars_Color9_Dataset: yellow
SUBFOLDER OF ./CompCars_Color9_Dataset: gray
SUBFOLDER OF ./CompCars_Color9_Dataset: white
SUBFOLDER OF ./CompCars_Color9_Dataset: green
SUBFOLDER OF ./CompCars_Color9_Dataset: purple
labels.txt
The current folder is ./CompCars_Color9_Dataset/black
The current folder is ./CompCars_Color9_Dataset/blue
The current folder is ./CompCars_Color9_Dataset/red
The current folder is ./CompCars_Color9_Dataset/brown
The current folder is ./CompCars_Color9_Dataset/yellow
The current folder is ./CompCars_Color9_Dataset/gray
The current folder is ./CompCars_Color9_Dataset/white
The current folder is ./CompCars_Color9_Dataset/green
The current folder is ./CompCars_Color9_Dataset/purple
Process finished with exit code 0
注意: Darknet 分类器训练数据集使用的数据集,需要将文件名修改为:
bl_ack
bl_ue
br_own
gr_ay
gr_een
pu_rple
re_d
wh_ite
ye_llow
训练图片路径中不能包含分类器的类别标签,否则将打印报错语句:
printf(“Too many or too few labels: %d, %s\n”, count, path);
…/master/src/data.c
void fill_truth(char *path, char **labels, int k, float *truth)
{
int i;
memset(truth, 0, k*sizeof(float));
int count = 0;
for(i = 0; i < k; ++i){
if(strstr(path, labels[i])){
truth[i] = 1;
++count;
//printf("%s %s %d\n", path, labels[i], i);
}
}
if(count != 1 && (k != 1 || count != 0)) printf("Too many or too few labels: %d, %s\n", count, path);
}