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

 本系列为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];
    }
}

完,

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux创始人LinusTorvalds有一句名言:Talk is cheap, Show me the code.(冗谈不够,放码过来!)。 代码阅读是从入门到提高的必由之路。尤其对深度学习,许多框架隐藏了神经网络底层的实现,只能在上层调包使用,对其内部原理很难认识清晰,不利于进一步优化和创新。  YOLOv3是一种基于深度学习的端到端实时目标检测方法,以速度快见长。YOLOv3的实现Darknet是使用C语言开发的轻型开源深度学习框架,依赖少,可移植性好,可以作为很好的代码阅读案例,让我们深入探究其实现原理。  本课程将解析YOLOv3的实现原理和源码,具体内容包括: YOLO目标检测原理  神经网络及Darknet的C语言实现,尤其是反向传播的梯度求解和误差计算 代码阅读工具及方法 深度学习计算的利器:BLAS和GEMM GPU的CUDA编程方法及在Darknet的应用 YOLOv3的程序流程及各层的源码解析本课程将提供注释后的Darknet源码程序文件。  除本课程《YOLOv3目标检测:原理与源码解析》外,本人推出了有关YOLOv3目标检测的系列课程,包括:   《YOLOv3目标检测实战:训练自己的数据集》  《YOLOv3目标检测实战:交通标志识别》  《YOLOv3目标检测:原理与源码解析》  《YOLOv3目标检测:网络模型改进方法》 建议先学习课程《YOLOv3目标检测实战:训练自己的数据集》或课程《YOLOv3目标检测实战:交通标志识别》,对YOLOv3的使用方法了解以后再学习本课程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值