轉(zhuǎn)Pytorch框架下ResNet到caffe的時(shí)候遇到的問(wèn)題:
Pytorch中池化層默認(rèn)的ceil mode是false,而caffe只實(shí)現(xiàn)了ceil mode= true的氏身。
github上搜到大神自己增加了Pooling層ceil mode的配置枢冤,需要修改源碼烹看,記錄在此。
include/caffe/layers/pooling_layers.hpp中:
int height_, width_;
int pooled_height_, pooled_width_;
bool global_pooling_;
bool ceil_mode_;//+
Blob<Dtype> rand_idx_;
Blob<int> max_idx_;
};
src/caffe/layers/pooling_layer.cpp中:
//modify for ceil mode
//pooled_height_ = static_cast<int>(ceil(static_cast<float>(
// height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
//pooled_width_ = static_cast<int>(ceil(static_cast<float>(
// width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
// Specify the structure by ceil or floor mode
if (ceil_mode_) {
pooled_height_ = static_cast<int>(ceil(static_cast<float>(
height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
pooled_width_ = static_cast<int>(ceil(static_cast<float>(
width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
} else {
pooled_height_ = static_cast<int>(floor(static_cast<float>(
height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
pooled_width_ = static_cast<int>(floor(static_cast<float>(
width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
}
src/caffe/proto/caffe.proto中:
// If global_pooling then it will pool over the size of the bottom by doing
// kernel_h = bottom->height and kernel_w = bottom->width
optional bool global_pooling = 12 [default = false];
// Specify floor/ceil mode
optional bool ceil_mode = 13 [default = true];//+
}