• 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;  //序号不冲突即可
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2. upsample 实现参考

    https://github.com/SeanQ88/caffe_upsample
    
    • 1

    实现分析

    假设输入为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 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    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];
                }
              }
            }
          }
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    threejs examples 学习
    php-fpm 讲解
    车牌比对程序源代码(c++)
    java-php-python-ssm视频网站的设计与实现计算机毕业设计
    LeetCode-2485-找出中枢整数
    机器学习基础之《回归与聚类算法(5)—分类的评估方法》
    Kafka集群架构设计原理详解
    人工智能十大流行算法
    新研一暑假计划
    【SQL刷题】Day6----SQL综合专项练习
  • 原文地址:https://blog.csdn.net/u012385733/article/details/126583735