From e386936ee911bde8891d5dd7810ae988e851e380 Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sat, 15 Feb 2014 12:07:02 -0800 Subject: [PATCH] make split_layer backward obey propagate_down --- src/caffe/layers/split_layer.cpp | 77 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/caffe/layers/split_layer.cpp diff --git a/src/caffe/layers/split_layer.cpp b/src/caffe/layers/split_layer.cpp new file mode 100644 index 0000000..cdfd539 --- /dev/null +++ b/src/caffe/layers/split_layer.cpp @@ -0,0 +1,77 @@ +// Copyright 2014 Jeff Donahue + +#include + +#include "caffe/layer.hpp" +#include "caffe/vision_layers.hpp" +#include "caffe/util/math_functions.hpp" + +namespace caffe { + +template +void SplitLayer::SetUp(const vector*>& bottom, + vector*>* top) { + CHECK_EQ(bottom.size(), 1) << "Split Layer takes a single blob as input."; + CHECK_GE(top->size(), 1) << "Split Layer takes at least one blob as output."; + count_ = bottom[0]->count(); + for (int i = 0; i < top->size(); ++i) { + (*top)[i]->Reshape(bottom[0]->num(), bottom[0]->channels(), + bottom[0]->height(), bottom[0]->width()); + CHECK_EQ(count_, (*top)[i]->count()); + } +}; + +template +void SplitLayer::Forward_cpu(const vector*>& bottom, + vector*>* top) { + const Dtype* bottom_data = bottom[0]->cpu_data(); + for (int i = 0; i < top->size(); ++i) { + Dtype* top_data = (*top)[i]->mutable_cpu_data(); + caffe_copy(count_, bottom_data, top_data); + } +} + +template +void SplitLayer::Forward_gpu(const vector*>& bottom, + vector*>* top) { + const Dtype* bottom_data = bottom[0]->gpu_data(); + for (int i = 0; i < top->size(); ++i) { + Dtype* top_data = (*top)[i]->mutable_gpu_data(); + caffe_gpu_copy(count_, bottom_data, top_data); + } +} + +template +Dtype SplitLayer::Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { + if (propagate_down) { + const Dtype* top_diff = top[0]->cpu_diff(); + Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff(); + caffe_copy(count_, top_diff, bottom_diff); + for (int i = 1; i < top.size(); ++i) { + top_diff = top[i]->cpu_diff(); + caffe_axpy(count_, Dtype(1.), top_diff, bottom_diff); + } + } + return Dtype(0.); +} + + +template +Dtype SplitLayer::Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { + if (propagate_down) { + const Dtype* top_diff = top[0]->gpu_diff(); + Dtype* bottom_diff = (*bottom)[0]->mutable_gpu_diff(); + caffe_gpu_copy(count_, top_diff, bottom_diff); + for (int i = 1; i < top.size(); ++i) { + top_diff = top[i]->gpu_diff(); + caffe_gpu_axpy(count_, Dtype(1.), top_diff, bottom_diff); + } + } + return Dtype(0.); +} + +INSTANTIATE_CLASS(SplitLayer); + +} // namespace caffe -- 2.7.4