From 4b273e35cc2b3c29f291d4fa96139a0260ffcde7 Mon Sep 17 00:00:00 2001 From: linmin Date: Tue, 25 Feb 2014 09:59:30 +0800 Subject: [PATCH] remove the pad=0 case in conv_layer and im2col_layer --- src/caffe/layers/conv_layer.cpp | 54 +++++++++------------------------------ src/caffe/layers/im2col_layer.cpp | 36 ++++++-------------------- 2 files changed, 20 insertions(+), 70 deletions(-) diff --git a/src/caffe/layers/conv_layer.cpp b/src/caffe/layers/conv_layer.cpp index 8f06524..3eb5506 100644 --- a/src/caffe/layers/conv_layer.cpp +++ b/src/caffe/layers/conv_layer.cpp @@ -88,13 +88,8 @@ void ConvolutionLayer::Forward_cpu(const vector*>& bottom, int top_offset = M_ * N_; for (int n = 0; n < NUM_; ++n) { // First, im2col - if (PAD_ == 0) { - im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, col_data); - } else { - padded_im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); - } + padded_im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); // Second, innerproduct with groups for (int g = 0; g < GROUP_; ++g) { caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, K_, @@ -123,13 +118,8 @@ void ConvolutionLayer::Forward_gpu(const vector*>& bottom, int top_offset = M_ * N_; for (int n = 0; n < NUM_; ++n) { // First, im2col - if (PAD_ == 0) { - im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, col_data); - } else { - padded_im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); - } + padded_im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); // Second, innerproduct with groups for (int g = 0; g < GROUP_; ++g) { caffe_gpu_gemm(CblasNoTrans, CblasNoTrans, M_, N_, K_, @@ -177,13 +167,8 @@ Dtype ConvolutionLayer::Backward_cpu(const vector*>& top, for (int n = 0; n < NUM_; ++n) { // since we saved memory in the forward pass by not storing all col data, // we will need to recompute them. - if (PAD_ == 0) { - im2col_cpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, col_data); - } else { - padded_im2col_cpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); - } + padded_im2col_cpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); // gradient w.r.t. weight. Note that we will accumulate diffs. for (int g = 0; g < GROUP_; ++g) { caffe_cpu_gemm(CblasNoTrans, CblasTrans, M_, K_, N_, @@ -200,13 +185,8 @@ Dtype ConvolutionLayer::Backward_cpu(const vector*>& top, (Dtype)0., col_diff + col_offset * g); } // col2im back to the data - if (PAD_ == 0) { - col2im_cpu(col_diff, CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } else { - padded_col2im_cpu(col_diff, CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } + padded_col2im_cpu(col_diff, CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); } } return Dtype(0.); @@ -245,13 +225,8 @@ Dtype ConvolutionLayer::Backward_gpu(const vector*>& top, for (int n = 0; n < NUM_; ++n) { // since we saved memory in the forward pass by not storing all col data, // we will need to recompute them. - if (PAD_ == 0) { - im2col_gpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, col_data); - } else { - padded_im2col_gpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); - } + padded_im2col_gpu(bottom_data + (*bottom)[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, col_data); // gradient w.r.t. weight. Note that we will accumulate diffs. for (int g = 0; g < GROUP_; ++g) { caffe_gpu_gemm(CblasNoTrans, CblasTrans, M_, K_, N_, @@ -268,13 +243,8 @@ Dtype ConvolutionLayer::Backward_gpu(const vector*>& top, (Dtype)0., col_diff + col_offset * g); } // col2im back to the data - if (PAD_ == 0) { - col2im_gpu(col_diff, CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } else { - padded_col2im_gpu(col_diff, CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } + padded_col2im_gpu(col_diff, CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); } } return Dtype(0.); diff --git a/src/caffe/layers/im2col_layer.cpp b/src/caffe/layers/im2col_layer.cpp index 7aced1d..bb4cb40 100644 --- a/src/caffe/layers/im2col_layer.cpp +++ b/src/caffe/layers/im2col_layer.cpp @@ -30,13 +30,8 @@ void Im2colLayer::Forward_cpu(const vector*>& bottom, const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = (*top)[0]->mutable_cpu_data(); for (int n = 0; n < bottom[0]->num(); ++n) { - if (PAD_ == 0) { - im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, top_data + (*top)[0]->offset(n)); - } else { - padded_im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n)); - } + padded_im2col_cpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n)); } } @@ -46,13 +41,8 @@ void Im2colLayer::Forward_gpu(const vector*>& bottom, const Dtype* bottom_data = bottom[0]->gpu_data(); Dtype* top_data = (*top)[0]->mutable_gpu_data(); for (int n = 0; n < bottom[0]->num(); ++n) { - if (PAD_ == 0) { - im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, top_data + (*top)[0]->offset(n)); - } else { - padded_im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n)); - } + padded_im2col_gpu(bottom_data + bottom[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, top_data + (*top)[0]->offset(n)); } } @@ -62,13 +52,8 @@ Dtype Im2colLayer::Backward_cpu(const vector*>& top, const Dtype* top_diff = top[0]->cpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff(); for (int n = 0; n < top[0]->num(); ++n) { - if (PAD_ == 0) { - col2im_cpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } else { - padded_col2im_cpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } + padded_col2im_cpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); } return Dtype(0.); } @@ -80,13 +65,8 @@ Dtype Im2colLayer::Backward_gpu(const vector*>& top, const Dtype* top_diff = top[0]->gpu_diff(); Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); for (int n = 0; n < top[0]->num(); ++n) { - if (PAD_ == 0) { - col2im_gpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } else { - padded_col2im_gpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, - WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); - } + padded_col2im_gpu(top_diff + top[0]->offset(n), CHANNELS_, HEIGHT_, + WIDTH_, KSIZE_, PAD_, STRIDE_, bottom_diff + (*bottom)[0]->offset(n)); } return Dtype(0.); } -- 2.7.4