caffe跑densenet的錯誤:Message type "caffe.PoolingParameter" has no field named "ceil_mode".【轉自CSDN】


最近看了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即可。

1、修改pooling_layer.hpp文件PoolingLayer類

在pooling_layer.hpp中往PoolingLayer類中添加ceil_mode_這個參數,修改如下:

    int height_, width_; int pooled_height_, pooled_width_; bool global_pooling_; bool ceil_mode_; //添加的類成員變量 Blob<Dtype> rand_idx_; Blob<int> max_idx_;

2、修改pooling_layer.cpp文件中相關參數

主要涉及到LayerSetUp函數和Reshape函數。LayerSetUp函數修改如下:

 || (!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函數修改如下:

 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.

3、修改caffe.proto文件中PoolingParameter的定義

因為添加了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,同時設置一個默認值。 }
4、重新編譯caffe

返回到caffe的根目錄,使用make指令,即可。

 make -j32 // 這里j后面的數字與電腦配置有關系,可以加速編譯 

注意:這個流程也是一般修改caffe的層的一般流程:1、主要修改相應層的hpp文件和cpp文件;2、如果有參數的添加或者減少,還需要修改caffe.proto文件對應層的參數即可(注意設置正確的ID以及默認值default);3、重新編譯caffe


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM