void ScalarLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { if (bottom.size() == 1 && this->blobs_.size() > 0) { LOG(INFO) << "Skipping parameter initialization"; } else if (bottom.size() == 1) { // scalar is a learned parameter; initialize it const ScalarParameter& param = this->layer_param_.scalar_param(); axis_ = bottom[0]->CanonicalAxisIndex(param.axis()); const int num_axes = param.num_axes(); CHECK_GE(num_axes, -1) << "num_axes must be non-negative, " << "or -1 to extend to the end of bottom[0]"; if (num_axes >= 0) { CHECK_GE(bottom[0]->num_axes(), axis_ + num_axes) << "scalar blob's shape extends past bottom[0]'s shape when applied " << "starting with bottom[0] axis = " << axis_; } this->blobs_.resize(1); const vector<int>::const_iterator& shape_start = bottom[0]->shape().begin() + axis_; const vector<int>::const_iterator& shape_end = (num_axes == -1) ? bottom[0]->shape().end() : (shape_start + num_axes); vector<int> scalar_shape(shape_start, shape_end); this->blobs_[0].reset(new Blob<Dtype>(scalar_shape)); FillerParameter filler_param(param.filler()); if (!param.has_filler()) { // Default to unit (1) filler for identity operation. filler_param.set_type("constant"); filler_param.set_value(1); } shared_ptr<Filler<Dtype> > filler(GetFiller<Dtype>(filler_param)); filler->Fill(this->blobs_[0].get()); } this->param_propagate_down_.resize(this->blobs_.size(), true); }
void ScaleLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const ScaleParameter& param = this->layer_param_.scale_param(); if (bottom.size() == 1 && this->blobs_.size() > 0) { LOG(INFO) << "Skipping parameter initialization"; } else if (bottom.size() == 1) { // scale is a learned parameter; initialize it axis_ = bottom[0]->CanonicalAxisIndex(param.axis()); const int num_axes = param.num_axes(); CHECK_GE(num_axes, -1) << "num_axes must be non-negative, " << "or -1 to extend to the end of bottom[0]"; if (num_axes >= 0) { CHECK_GE(bottom[0]->num_axes(), axis_ + num_axes) << "scale blob's shape extends past bottom[0]'s shape when applied " << "starting with bottom[0] axis = " << axis_; } this->blobs_.resize(1); const vector<int>::const_iterator& shape_start = bottom[0]->shape().begin() + axis_; const vector<int>::const_iterator& shape_end = (num_axes == -1) ? bottom[0]->shape().end() : (shape_start + num_axes); vector<int> scale_shape(shape_start, shape_end); this->blobs_[0].reset(new Blob<Dtype>(scale_shape)); FillerParameter filler_param(param.filler()); if (!param.has_filler()) { // Default to unit (1) filler for identity operation. filler_param.set_type("constant"); filler_param.set_value(1); } shared_ptr<Filler<Dtype> > filler(GetFiller<Dtype>(filler_param)); filler->Fill(this->blobs_[0].get()); } if (param.bias_term()) { LayerParameter layer_param(this->layer_param_); layer_param.set_type("Bias"); BiasParameter* bias_param = layer_param.mutable_bias_param(); bias_param->set_axis(param.axis()); if (bottom.size() > 1) { bias_param->set_num_axes(bottom[1]->num_axes()); } else { bias_param->set_num_axes(param.num_axes()); } bias_param->mutable_filler()->CopyFrom(param.bias_filler()); bias_layer_ = LayerRegistry<Dtype>::CreateLayer(layer_param); bias_bottom_vec_.resize(1); bias_bottom_vec_[0] = bottom[0]; bias_layer_->SetUp(bias_bottom_vec_, top); bias_param_id_ = this->blobs_.size(); this->blobs_.resize(bias_param_id_ + 1); this->blobs_[bias_param_id_] = bias_layer_->blobs()[0]; bias_propagate_down_.resize(1, false); } this->param_propagate_down_.resize(this->blobs_.size(), true); }
void MKLBatchNormLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { eps_ = this->layer_param_.batch_norm_param().eps(); use_weight_bias_ = this->layer_param_.batch_norm_param().use_weight_bias(); bias_term_ = this->layer_param_.batch_norm_param().bias_term(); // Workaround. Checking count of parameters in order to handle // topology for reference BatchNorm layer which don't have scaling if (this->layer_param_.param_size() == 3) { this->blobs_.resize(3); use_weight_bias_ = false; } size_t dim = 4, sizes[4], strides[4]; channels_ = bottom[0]->channels(); height_ = bottom[0]->height(); width_ = bottom[0]->width(); num_ = bottom[0]->num(); sizes[0] = width_; sizes[1] = height_; sizes[2] = channels_; sizes[3] = num_; strides[0] = 1; strides[1] = sizes[0]; strides[2] = sizes[0]*sizes[1]; strides[3] = sizes[0]*sizes[1]*sizes[2]; // Names are for debugging only fwd_bottom_data->name = "fwd_bottom_data @ " + this->layer_param_.name(); fwd_top_data->name = "fwd_top_data @ " + this->layer_param_.name(); bwd_bottom_diff->name = "bwd_bottom_diff @ " + this->layer_param_.name(); bwd_top_diff->name = "bwd_top_diff @ " + this->layer_param_.name(); dnnError_t e; e = dnnLayoutCreate<Dtype>(&layout_usr_, dim, sizes, strides); CHECK_EQ(e, E_SUCCESS); fwd_bottom_data->create_user_layout(dim, sizes, strides); fwd_top_data ->create_user_layout(dim, sizes, strides); bwd_bottom_diff->create_user_layout(dim, sizes, strides); bwd_top_diff ->create_user_layout(dim, sizes, strides); workspace_buffer_ = NULL; scaleShift_buffer_ = NULL; // "Lazy" allocation because here we don't know // what layout is used by neighbours. // Primitives will be allocated during the first fwd pass batchNormFwd = NULL; batchNormBwdData = NULL; batchNormBwdScaleShift = NULL; if (use_weight_bias_) { if ( bias_term_ ) { this->blobs_.resize(2); } else { this->blobs_.resize(1); } // Initialize scale and shift vector<int> scaleshift_shape(1); scaleshift_shape[0] = channels_; this->blobs_[0].reset(new Blob<Dtype>(scaleshift_shape)); FillerParameter filler_param( this->layer_param_.batch_norm_param().filler()); if (!this->layer_param_.batch_norm_param().has_filler()) { filler_param.set_type("constant"); filler_param.set_value(1); } shared_ptr<Filler<Dtype> > filler(GetFiller<Dtype>(filler_param)); filler->Fill(this->blobs_[0].get()); if ( bias_term_ ) { this->blobs_[1].reset(new Blob<Dtype>(scaleshift_shape)); FillerParameter bias_filler_param( this->layer_param_.batch_norm_param().bias_filler()); if (!this->layer_param_.batch_norm_param().has_bias_filler()) { bias_filler_param.set_type("constant"); bias_filler_param.set_value(0); } shared_ptr<Filler<Dtype> > bias_filler( GetFiller<Dtype>(bias_filler_param)); bias_filler->Fill(this->blobs_[1].get()); } } }