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];
            }
          }
        }
      }
    }
  }
安装onnx2caffe可以按照以下步骤进行操作。首先,确保你已经安装了cuda和cudnn,并按照Readme上的方法安装了yolov5_caffe。然后,你可以使用开源提供的方法来安装caffe环境以及onnx转caffe的脚本\[1\]。你可以通过以下步骤来完成安装: 1. 编译caffe:使用以下命令编译caffe: ``` make -j8 make pycaffe -j8 ``` 2. 下载onnx2caffe的轮子:你可以从https://github.com/Wulingtian/yolov5_onnx2caffe 下载onnx2caffe的轮子。这个版本是针对yolov5改过的,支持upsample和transpose的转换\[2\]。 3. 安装onnxsimplifier:使用以下命令安装onnxsimplifier: ``` pip install onnx-simplifier ``` 4. 运行onnx2caffe转换脚本:使用以下命令运行onnx2caffe转换脚本: ``` python -m onnx2caffe srcXXX.onnx dstXXX.onnx ``` 在转换过程,如果遇到不支持的类型或报错,你可以根据具体情况进行修改和添加。通常报错是由于类型不支持或者pytorch的函数参数与caffe不一致导致的,你可以根据报错信息进行相应的修改\[2\]。 希望这些步骤对你有帮助! #### 引用[.reference_title] - *1* [onnx转caffe环境搭建及转换](https://blog.csdn.net/u010497704/article/details/125327929)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [yolov5:onnx2caffe](https://blog.csdn.net/qq_37516798/article/details/122611039)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [onnx2caffe的一些经验](https://blog.csdn.net/Ray_Tang_/article/details/111880353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值