构建 Darknet 分类器 (Tiny Darknet) 训练数据集 (color recognition 颜色识别/color classification 颜色分类)

构建 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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值