caffe之upsample实现

upsample

1. src/caffe/proto/caffe.proto

添加upsamper参数描述

message UpsampleParameter{
  optional int32 scale = 1 [default = 1];
}
message LayerParameter{
...
 optional UpsampleParameter upsample_param = 149;  //序号不冲突即可
}

2. upsample 实现参考

https://github.com/SeanQ88/caffe_upsample

实现分析

假设输入为2x2, scale=2.0;则输出则为4x4

1. 原矩阵2x2

12
34

2. 经过upsample, scale=2.0,

1122
1122
3344
3344

3. 核心算法如下

用目的坐标映射到原坐标

  1. input NCHW=(1,1,2,2)
  2. ouput NCHW=(1,1,4,4)
int N = 1, C =1, H = 4, H = 4;
for (int n = 0; n <N; n++) {
	for (int c = 0; c < C; c++) {
		for (int h = 0; h < H; h++){
			for (int w = 0; w< W; w++) {
				//计算目的坐标到原坐标的对应关系;
				int nw = w / scale_; 
				int nh = h / scale_;
				//n * C * H * W + c * H * W + h * W + w
				//output_dix 坐标计算可自行化简,可得如下
				int output_idx = (((n * C + c) * H) + h) * W + w;
				//input_dix坐标同理,可得如下, 就是把HxW scale到原HxW
				int input_idx = (((n * C + c) * (H/scale)) + nh) * (W/sacle) + nw;
			}
		}
	}
}

4. 反向传播

由于bottom_diff是NCHW=(1,1,2,2), top_diff(1,1,4,4)
并且scale是常数,不参数梯度,即可认为是f = x,导数为f’=1
从整个表达来说f = scale * x,top_diff * scale 应该也是可以的,然后再做如下运算,传递梯度到下一层。如有理解误的,望指正。

  for (int n = 0; n < N; n++) {
    for (int c = 0; c < C; c++) {
      for (int h = 0; h < H; h++) {
        for (int w = 0; w < W; w++) {
          for (int i = 0; i < scale_; i++) {
            for (int j = 0; j < scale_; j++) {
              int nw = w * scale_ + i;
              int nh = h * scale_ + j;
              int out_idx = (((n * C + c) * H) + h) * W + w;
              int in_idx = (((n * C + c) * (H * scale_))
                  + nh) * (W * scale_) + nw;
              bottom_diff[out_idx] += top_diff[in_idx];
            }
          }
        }
      }
    }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值