【darknet学习笔记】load_data_augment源代码解析

修改训练图像分类加载数据类型

目录

训练load_data_old和预测load_data_augment()比较

load_data_augment代码解析

get_random_paths

load_image_augment_paths

load_labels_paths

load_labels_paths调用参数说明

load_labels_paths源代码解析


训练load_data_old和预测load_data_augment()比较

data load_data_old(char **paths, int n, int m, char **labels, int k, int w, int h)
{
    if(m) paths = get_random_paths(paths, n, m);
    data d = {0};
    d.shallow = 0;
    d.X = load_image_paths(paths, n, w, h);
    d.y = load_labels_paths(paths, n, labels, k, 0);
    if(m) free(paths);
    return d;
}

可以看出load_data_old与load_data_augment()只有获取data.X不同,一个是load_image_paths,另一个是load_image_augment_paths()。

matrix load_image_paths(char **paths, int n, int w, int h)
{
    int i;
    matrix X;
    X.rows = n;
    X.vals = (float**)calloc(X.rows, sizeof(float*));
    X.cols = 0;

    for(i = 0; i < n; ++i){
        image im = load_image_color(paths[i], w, h);
        X.vals[i] = im.data;
        X.cols = im.h*im.w*im.c;
    }
    return X;
}

通过path获取image数据都通过函数load_iamge_color()。

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;
}

get_random_paths

get_random_paths()从m张图片中随机选取n张图片,

其中m为metal.data文件中train变量对应list文件中所有图片张数

n=batch*subdivision*ngpus,batch和subdivision都是*.cfg文件中变量,ngpus默认为1也可在训练指令中通过“-gpus”指定gpu个数。

load_image_augment_paths

 data d = {0};
 d.shallow = 0;
 d.X = load_image_augment_paths(paths, n, use_flip, min, max, size, angle, aspect, hue, saturation, exposure);

调用中涉及到的参数:

参数名

含义

相关文件

文件中变量

默认值

a.paths

tain变量对应list中包含所有图片路径

Metal.data

train

a.n

a.n=batch * subdivisions * ngpus

*.cfg

batch和subdivisions

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

图像增强参数,方位

 

1

a.hue

图像增强参数,色调

hue

0

a.saturation

图像增强参数,饱和度

saturation

1

a.exposure

图像增强参数,曝光时间

exposure

1

load_image_augment_paths()函数的源代码:

matrix load_image_augment_paths(char **paths, int n, int use_flip, int min, int max, int size, float angle, float aspect, float hue, float saturation, float exposure)
{
    int i;
    matrix X;
    X.rows = n;
    X.vals = (float**)calloc(X.rows, sizeof(float*));
    X.cols = 0;

    for(i = 0; i < n; ++i){
        image im = load_image_color(paths[i], 0, 0);
        image crop = random_augment_image(im, angle, aspect, min, max, size);
        int flip = use_flip ? random_gen() % 2 : 0;
        if (flip)
            flip_image(crop);
        random_distort_image(crop, hue, saturation, exposure);
        free_image(im);
        X.vals[i] = crop.data;
        X.cols = crop.h*crop.w*crop.c;
    }
    return X;
}

load_labels_paths

load_labels_paths源代码:

matrix load_labels_paths(char **paths, int n, char **labels, int k, tree *hierarchy)
{
    matrix y = make_matrix(n, k);
    int i;
    for(i = 0; i < n && labels; ++i){
        fill_truth(paths[i], labels, k, y.vals[i]);
        if(hierarchy){
            fill_hierarchy(y.vals[i], k, hierarchy);
        }
    }
    return y;
}

load_labels_paths调用参数说明

参数名

含义

相关文件

文件中变量

默认值

a.paths

tain变量对应list中包含所有图片路径

Metal.data

train

a.n

a.n=batch * subdivisions * ngpus

*.cfg

batch和subdivisions

a.labels

分类任务涉及类别

 

label

a.classes

分类任务的类别个数

classes

a.hierarchy

如果此前网络层为softmax层,hirerarchy指向一个softmax_tree。

*.cfg

softmax

k就是a.classes,本案例中k=2.

本案例中 n=64*4*1

labels是类别名数组,里面保存的类别名分别为:normal、abnor。

load_labels_paths源代码解析

1.make_matrix()

matrix make_matrix(int rows, int cols)
{
    int i;
    matrix m;
    m.rows = rows;
    m.cols = cols;
    m.vals = (float**)calloc(m.rows, sizeof(float*));
    for(i = 0; i < m.rows; ++i){
        m.vals[i] = (float*)calloc(m.cols, sizeof(float));
    }
    return m;
}

由源代码可知,make_matrix生产row行cols列二维数组。

所以 matrix y = make_matrix(n, k);y是一个二维数组,其行数为n=64*4*1;列数为k=2。

即该二维数组,每一行表示一张图片,每一列代表一张图的一个类别normal或abnor。

2.fill_truth()

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;
        }
    }
    if(count != 1) printf("Too many or too few labels: %d, %s\n", count, path);
}

fill_truth的作用就是设置每一张图片的类别,其中strstr比较重要:

strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回 str1字符串从 str2第一次出现的位置开始到 str1结尾的字符串;否则,返回NULL。

3.fill_hierarchy()

暂时不研究。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

haimianjie2012

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值