最近看了densenet這篇論文,論文做者給了基於caffe的源碼,本身在電腦上跑了下,可是出現了Message type 「caffe.PoolingParameter」 has no field named 「ceil_mode」.的錯誤,現將解決辦法記載以下。主要是參考
(https://github.com/BVLC/caffe/pull/3057/files)。錯誤緣由:因爲caffe的版本的緣由,現用的caffe的源碼中的pooling層沒有ceil_mode
這個函數,所以解決辦法也是在如今的源碼中網pooling層中添加這個參數以及相關的代碼,並從新編譯caffe便可。git
在pooling_layer.hpp中往PoolingLayer類中添加ceil_mode_這個參數,修改以下:github
int height_, width_; int pooled_height_, pooled_width_; bool global_pooling_; bool ceil_mode_; //添加的類成員變量 Blob<Dtype> rand_idx_; Blob<int> max_idx_;
主要涉及到LayerSetUp函數和Reshape函數。LayerSetUp函數修改以下:markdown
|| (!pool_param.has_stride_h() && !pool_param.has_stride_w())) << "Stride is stride OR stride_h and stride_w are required."; global_pooling_ = pool_param.global_pooling(); ceil_mode_ = pool_param.ceil_mode(); //添加的代碼,主要做用是從參數文件中獲取ceil_mode_的參數數值。 if (global_pooling_) { kernel_h_ = bottom[0]->height(); kernel_w_ = bottom[0]->width(); if (pad_h_ != 0 || pad_w_ != 0) { CHECK(this->layer_param_.pooling_param().pool() == PoolingParameter_PoolMethod_AVE || this->layer_param_.pooling_param().pool() == PoolingParameter_PoolMethod_MAX) << "Padding implemented only for average and max pooling."; CHECK_LT(pad_h_, kernel_h_); CHECK_LT(pad_w_, kernel_w_); .......
Reshape函數修改以下:ide
void PoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { CHECK_EQ(4, bottom[0]->num_axes()) << "Input must have 4 axes, " << "corresponding to (num, channels, height, width)"; channels_ = bottom[0]->channels(); height_ = bottom[0]->height(); width_ = bottom[0]->width(); if (global_pooling_) { kernel_h_ = bottom[0]->height(); kernel_w_ = bottom[0]->width(); } - 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; + } + // ------------------------------------------------------ + if (pad_h_ || pad_w_) { // If we have padding, ensure that the last pooling starts strictly // inside the image (instead of at the padding); otherwise clip the last.
由於添加了pooling層的參數,所以須要修改caffe.proto文件中PoolingParameter中的定義,須要增長參數的聲明。函數
// 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];// 爲pooling層添加參數,這樣能夠在net.prototxt文件中爲pooling層設置該參數,注意後面須要給其設置一個ID,同時設置一個默認值。 }
四、從新編譯caffe
返回到caffe的根目錄,使用make指令,便可。ui
make -j32 // 這裏j後面的數字與電腦配置有關係,能夠加速編譯