【YOLO3代码详解系列08】平均池化层

1 avgpool_layer.h

#ifndef AVGPOOL_LAYER_H
#define AVGPOOL_LAYER_H
#include "image.h"
#include "cuda.h"
#include "layer.h"
#include "network.h"

typedef layer avgpool_layer;

image get_avgpool_image(avgpool_layer l);

// 构造平均池化层函数
avgpool_layer make_avgpool_layer(int batch, int w, int h, int c);

void resize_avgpool_layer(avgpool_layer *l, int w, int h);

// 平均池化层前向传播函数
void forward_avgpool_layer(const avgpool_layer l, network net);

// 平均池化层后向传播函数
void backward_avgpool_layer(const avgpool_layer l, network net);

#ifdef GPU
void forward_avgpool_layer_gpu(avgpool_layer l, network net);
void backward_avgpool_layer_gpu(avgpool_layer l, network net);
#endif

#endif



2 avgpool_layer.c

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

// 构造平均池化层
avgpool_layer make_avgpool_layer(int batch, int w, int h, int c)
{
fprintf(stderr, "avg                     %4d x%4d x%4d   ->  %4d\n",  w, h, c, c);
avgpool_layer l = {0};
l.type = AVGPOOL;
l.batch = batch;
l.h = h;
l.w = w;
l.c = c;
l.out_w = 1; //平均池化后宽度为1
l.out_h = 1; //平均池化后高度为1
l.out_c = c;
l.outputs = l.out_c;
l.inputs = h*w*c;
int output_size = l.outputs * batch;
l.output =  calloc(output_size, sizeof(float));
l.delta =   calloc(output_size, sizeof(float));
l.forward = forward_avgpool_layer;
l.backward = backward_avgpool_layer;

#ifdef GPU
l.forward_gpu = forward_avgpool_layer_gpu;
l.backward_gpu = backward_avgpool_layer_gpu;
l.output_gpu  = cuda_make_array(l.output, output_size);
l.delta_gpu   = cuda_make_array(l.delta, output_size);
#endif

return l;
}

void resize_avgpool_layer(avgpool_layer *l, int w, int h)
{
l->w = w;
l->h = h;
l->inputs = h*w*l->c;
}

// 平均池化层前向传播
void forward_avgpool_layer(const avgpool_layer l, network net)
{
int b,i,k;

for(b = 0; b < l.batch; ++b){
for(k = 0; k < l.c; ++k){
int out_index = k + b*l.c;
l.output[out_index] = 0;
for(i = 0; i < l.h*l.w; ++i){
int in_index = i + l.h*l.w*(k + b*l.c);
l.output[out_index] += net.input[in_index];
}
l.output[out_index] /= l.h*l.w;
}
}
}

// 平均池化层后向传播
void backward_avgpool_layer(const avgpool_layer l, network net)
{
int b,i,k;

for(b = 0; b < l.batch; ++b){
for(k = 0; k < l.c; ++k){
int out_index = k + b*l.c;
for(i = 0; i < l.h*l.w; ++i){
int in_index = i + l.h*l.w*(k + b*l.c);
net.delta[in_index] += l.delta[out_index] / (l.h*l.w);
}
}
}
}

还能输入1000个字符

09-12 23万+

05-23 82

06-06 4540

02-22 186

11-10 4032

03-22 1599

01-29 6369

05-11 8117

04-02 1954

07-25 705

05-23 1万+

04-05 2953

11-22 7567

05-18 8307

02-25 510

06-13 640

03-13 6827

08-07 2492

02-29 203

03-19 83万+

大学四年自学走来，这些私藏的实用工具/学习网站我贡献出来了

没有更多推荐了，返回首页

©️2019 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读