【darknet學習筆記】修改訓練圖像分類加載數據類型

修改darknet源代碼,使其能夠直接訓練二進制圖像數據

目錄

load_data_augment()調用參數說明

a.paths,

a.n,

a.m,

a.labels,

a.classes,

a.hierarchy,

a.flip,

a.min,

a.max,

a.size,

a.angle,

a.aspect,

a.hue,

a.saturation,

a.exposure

load_data_augment源代碼解析


classifier.c文件中數據類型爲CLASSIFICATION_DATA

 args.type = CLASSIFICATION_DATA;

data.c文件中的load_thread()函數中調用load_data_augment()函數:

void *load_thread(void *ptr)
{
    //srand(time(0));
    //printf("Loading data: %d\n", random_gen());
    load_args a = *(struct load_args*)ptr;
    if(a.exposure == 0) a.exposure = 1;
    if(a.saturation == 0) a.saturation = 1;
    if(a.aspect == 0) a.aspect = 1;

    if (a.type == OLD_CLASSIFICATION_DATA){
        *a.d = load_data_old(a.paths, a.n, a.m, a.labels, a.classes, a.w, a.h);
    } else if (a.type == CLASSIFICATION_DATA){
        *a.d = load_data_augment(a.paths, a.n, a.m, a.labels, a.classes, a.hierarchy, a.flip, a.min, a.max, a.size, a.angle, a.aspect, a.hue, a.saturation, a.exposure);
    } else if (a.type == SUPER_DATA){

load_data_augment()調用參數說明

 args.paths = paths;
 args.n = imgs;
 args.m = train_images_num;
 args.labels = labels;

 args.classes = classes;
 args.hierarchy = net.hierarchy;
 
 args.flip = net.flip;
 args.min = net.min_crop;
 args.max = net.max_crop;

 args.size = net.w > net.h ? net.w : net.h;

 args.angle = net.angle;
 args.aspect = net.aspect;
 args.hue = net.hue; 
 args.saturation = net.saturation;
 args.exposure = net.exposure;

a.paths,

list *options = read_data_cfg(datacfg);
char *train_list = option_find_str(options, "train", "data/train.list");
list *plist = get_paths(train_list);
char **paths = (char **)list_to_array(plist);
args.paths = paths;

train_classifier()函數調用參數說明已經知道,datacfg,數據說明文件路徑,圖像分類數據說明文件一般命名爲meta.data。

paths字符串數組,是meta.data文件夾下train後面對應list文件所包含所有圖片路徑,train默認值爲“data/train.list”。

a.n,

int subdivs = option_find_int(options, "subdivisions",1);
net->subdivisions = subdivs;
net->batch = option_find_int(options, "batch",1);
int imgs = net.batch * net.subdivisions * ngpus;
args.n = imgs;

ngpus由train指令的-ngpus命令指定,如果沒有指定該命令,ngpus默認爲1。

batch,subdivisions爲cfg文件指定batch和subdivisions。cfg文件中batch和subdivisions代表的含義,參看以前的博文:

a.m,

list *options = read_data_cfg(datacfg);
char *train_list = option_find_str(options, "train", "data/train.list");
list *plist = get_paths(train_list);
int train_images_num = plist->size; 
args.m = train_images_num;

用於分類的圖像張數,該值等於datacfg路徑所指文件meta.data文件中train字段對應list中的圖片張數。

a.labels,

 list *options = read_data_cfg(datacfg);
 char *label_list = option_find_str(options, "labels", "data/labels.list");
 char **labels = get_labels(label_list);
 args.labels = labels;

a.classes,

  list *options = read_data_cfg(datacfg);
  int classes = option_find_int(options, "classes", 2);
  args.classes = classes;

分類任務的類別個數,例如二分類任務,此處classes=2。

a.hierarchy,

 }else if(lt == SOFTMAX){
            l = parse_softmax(options, params);
            net.hierarchy = l.softmax_tree;
。。。。。。
args.hierarchy = net.hierarchy;

cfg文件中每一層結構前有一個[]標記起來的層名,例如sofmax網絡層參數最前面會有一行[softmax]

部分cfg文件參數如下:

[convolutional]
batch_normalize=1
filters=1024
size=3
stride=1
pad=1
activation=leaky

[convolutional]
filters=2
size=1
stride=1
pad=1
activation=linear

[avgpool]

[softmax]
groups=1

每一段代表一個網絡層,每一段第一行類似[convolutional]標識該網絡層的類別,緊接着是該網絡層的參數。[convolutional]標識下面這一段爲一個卷積層參數。

迴歸到net.hierarchy,hirerarchy中文意思爲層次關係,那麼net.hierarchy表示如果此前網絡層爲softmax層,hirerarchy指向一個softmax_tree。

a.flip,

  net->flip = option_find_int_quiet(options, "flip", 1);
  args.flip = net.flip;

flip就是cfg網絡結構文件中flip參數,flip中文意思翻轉開關

a.min,

net->min_crop = option_find_int_quiet(options, "min_crop",net->w);
args.min = net.min_crop;

a.max,

 net->max_crop = option_find_int_quiet(options, "max_crop",net->w*2);
 args.max = net.max_crop;

a.size,

 net->h = option_find_int_quiet(options, "height",0);
 net->w = option_find_int_quiet(options, "width",0);
 args.size = net.w > net.h ? net.w : net.h;

a.angle,

net->angle = option_find_float_quiet(options, "angle", 0);
args.angle = net.angle;

角度

a.aspect,

 net->aspect = option_find_float_quiet(options, "aspect", 1);
 args.aspect = net.aspect;

方位

a.hue,

net->hue = option_find_float_quiet(options, "hue", 0);
args.hue = net.hue;

色調

a.saturation,

 net->saturation = option_find_float_quiet(options, "saturation", 1);
 args.saturation = net.saturation;

飽和度

a.exposure

 net->exposure = option_find_float_quiet(options, "exposure", 1);
 args.exposure = net.exposure;

 曝光時間

總結

參數名

含義

相關文件

文件中變量

默認值

a.paths

tain變量對應list中包含所有圖片路徑

Metal.data

train

a.n

a.n=batch * subdivisions * ngpus

*.cfg

batch和subdivisions

a.m

tain變量對應list中包含所有圖片總數

metal.data

train

a.labels

分類任務涉及類別

label

a.classes

分類任務的類別個數

classes

a.hierarchy

如果此前網絡層爲softmax層,hirerarchy指向一個softmax_tree。

*.cfg

softmax

a.flip

 

flip

1

a.min

還看不出來

min_crop,width

Net.w即width

a.max

還看不出來

max_crop,width

Net.w*2

a.size

a.size = net.w > net.h ? net.w : net.h

Width,height

0

a.angle

還看不出來

angle

0

a.aspect

還看不出來

aspect

1

a.hue

還看不出來

hue

0

a.saturation

還看不出來

saturation

1

a.exposure

還看不出來

exposure

1

load_data_augment源代碼解析

data.c中的load_data_augment()代碼:

data load_data_augment(char **paths, int n, int m, char **labels, int k, tree *hierarchy, int use_flip, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
{
    if(m) paths = get_random_paths(paths, n, m);
    data d = {0};
    d.shallow = 0;
    d.X = load_image_augment_paths(paths, n, use_flip, min, max, size, angle, aspect, hue, saturation, exposure);
    d.y = load_labels_paths(paths, n, labels, k, hierarchy);
    if(m) free(paths);
    return d;
}

 

發佈了373 篇原創文章 · 獲贊 151 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章