【darknet源码解析-15】maxpool_layer.h 和 maxpool_layer.c 解析

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/caicaiatnbu/article/details/102667051

 本系列为darknet源码解析,本次解析src/maxpool_layer.h 与 src/maxpool_layer.c 两个。avgpool_pool主要完成了最大池化操作。

maxpool_layer.h 的解析如下:

#ifndef MAXPOOL_LAYER_H
#define MAXPOOL_LAYER_H

#include "image.h"
#include "cuda.h"
#include "layer.h"
#include "network.h"

typedef layer maxpool_layer;

image get_maxpool_image(maxpool_layer l);
// 构建最大池化层
maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding);
void resize_maxpool_layer(maxpool_layer *l, int w, int h);
// 最大池化层的前向传播函数
void forward_maxpool_layer(const maxpool_layer l, network net);
// 最大池化层的反向传播函数
void backward_maxpool_layer(const maxpool_layer l, network net);

#ifdef GPU
void forward_maxpool_layer_gpu(maxpool_layer l, network net);
void backward_maxpool_layer_gpu(maxpool_layer l, network net);
#endif

#endif

maxpool_layer.c 的解析如下:

#include "maxpool_layer.h"
#include "cuda.h"
#include <stdio.h>

image get_maxpool_image(maxpool_layer l)
{
    int h = l.out_h;
    int w = l.out_w;
    int c = l.c;
    return float_to_image(w,h,c,l.output);
}

image get_maxpool_delta(maxpool_layer l)
{
    int h = l.out_h;
    int w = l.out_w;
    int c = l.c;
    return float_to_image(w,h,c,l.delta);
}

/**
 * 构建最大池化层
 * @param batch 该层输入一个batch包含的图片的数量
 * @param h 输入图片的高度
 * @param w 输入图片的宽度
 * @param c 输入图片的通道数
 * @param size 池化窗口大小
 * @param stride 步幅
 * @param padding 补0的长度
 * @return
 */
maxpool_layer make_maxpool_layer(int batch, int h, int w, int c, int size, int stride, int padding)
{
    maxpool_layer l = {0};
    l.type = MAXPOOL; // 层类别
    l.batch = batch; // batch中包含的图片的数量
    l.h = h; // 输入图片的高度
    l.w = w; // 输入图片的宽度
    l.c = c; // 输入图片的通道数
    l.pad = padding; // 补0的个数
    l.out_w = (w + 2*padding)/stride; //输出图片的宽度
    l.out_h = (h + 2*padding)/stride; //输入图片的高度
    l.out_c = c; // 输出图片的通道数
    l.outputs = l.out_h * l.out_w * l.out_c; // 最大化层对应一张输入图片的输出元素个数
    l.inputs = h*w*c; // 最大池化层一张输入图片中所有元素的个数
    l.size = size; // 最大池化层池化窗口大小
    l.stride = stride; // 最大池化层步幅
    int output_size = l.out_h * l.out_w * l.out_c * batch; // 最大池化层所有输出的元素个数(包含整个batch的)
    l.indexes = calloc(output_size, sizeof(int)); // 用于保存每个最大池化窗口内的最大值对应的索引,方便之后的反向传播
    l.output =  calloc(output_size, sizeof(float)); // 最大池化层的所有输出(包含整个batch)
    l.delta =   calloc(output_size, sizeof(float)); // 最大池化层的误差项(包含整个batch的)

    // 最大池化层的前向,反向传播函数
    l.forward = forward_maxpool_layer;
    l.backward = backward_maxpool_layer;
    #ifdef GPU
    l.forward_gpu = forward_maxpool_layer_gpu;
    l.backward_gpu = backward_maxpool_layer_gpu;
    l.indexes_gpu = cuda_make_int_array(0, output_size);
    l.output_gpu  = cuda_make_array(l.output, output_size);
    l.delta_gpu   = cuda_make_array(l.delta, output_size);
    #endif
    fprintf(stderr, "max          %d x %d / %d  %4d x%4d x%4d   ->  %4d x%4d x%4d\n", size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c);
    return l;
}

void resize_maxpool_layer(maxpool_layer *l, int w, int h)
{
    l->h = h;
    l->w = w;
    l->inputs = h*w*l->c;

    l->out_w = (w + 2*l->pad)/l->stride;
    l->out_h = (h + 2*l->pad)/l->stride;
    l->outputs = l->out_w * l->out_h * l->c;
    int output_size = l->outputs * l->batch;

    l->indexes = realloc(l->indexes, output_size * sizeof(int));
    l->output = realloc(l->output, output_size * sizeof(float));
    l->delta = realloc(l->delta, output_size * sizeof(float));

    #ifdef GPU
    cuda_free((float *)l->indexes_gpu);
    cuda_free(l->output_gpu);
    cuda_free(l->delta_gpu);
    l->indexes_gpu = cuda_make_int_array(0, output_size);
    l->output_gpu  = cuda_make_array(l->output, output_size);
    l->delta_gpu   = cuda_make_array(l->delta,  output_size);
    #endif
}

/**
 * 最大池化层的前向传播函数
 * @param l 当前最大池化层
 * @param net 整个网络
 */
void forward_maxpool_layer(const maxpool_layer l, network net)
{
    int b,i,j,k,m,n;
    int w_offset = -l.pad;
    int h_offset = -l.pad;

    int h = l.out_h;
    int w = l.out_w;
    int c = l.c;

    for(b = 0; b < l.batch; ++b){ // 遍历整个batch中的每张图片
        for(k = 0; k < c; ++k){ // 遍历每张图片的所有通道

            for(i = 0; i < h; ++i){
                for(j = 0; j < w; ++j){
                    int out_index = j + w*(i + h*(k + c*b));
                    float max = -FLT_MAX;
                    int max_i = -1;
                    for(n = 0; n < l.size; ++n){ // 遍历最大池化层窗口,找到最大值,以及对应index
                        for(m = 0; m < l.size; ++m){
                            int cur_h = h_offset + i*l.stride + n;
                            int cur_w = w_offset + j*l.stride + m;
                            int index = cur_w + l.w*(cur_h + l.h*(k + b*l.c));
                            int valid = (cur_h >= 0 && cur_h < l.h &&
                                         cur_w >= 0 && cur_w < l.w);
                            float val = (valid != 0) ? net.input[index] : -FLT_MAX;
                            max_i = (val > max) ? index : max_i;
                            max   = (val > max) ? val   : max;
                        }
                    }
                    l.output[out_index] = max; // 最大池化层的输出值
                    l.indexes[out_index] = max_i; // 最大池化层的输出值的索引,方便反向传播
                }
            }
        }
    }
}

/**
 * 最大池化层反向传播函数
 * @param l 当前最大池化层
 * @param net 整个网络
 */
void backward_maxpool_layer(const maxpool_layer l, network net)
{
    int i;
    int h = l.out_h;
    int w = l.out_w;
    int c = l.c;
    for(i = 0; i < h*w*c*l.batch; ++i){ // 遍历所有的
        int index = l.indexes[i];
        // 下一层的误差项的值直接传递到上一层对应区块内的最大值所对应的神经元,而次区块内的其他神经元的误差项为0
        net.delta[index] += l.delta[i];
    }
}

完,

猜你喜欢

转载自blog.csdn.net/caicaiatnbu/article/details/102667051
今日推荐