summaryrefslogtreecommitdiff
path: root/src/caffe/layers/split_layer.cpp
blob: a8a240f74a63ceca00ea5f7d6a3c521184bf6f2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2014 Jeff Donahue

#include <vector>

#include "caffe/layer.hpp"
#include "caffe/vision_layers.hpp"
#include "caffe/util/math_functions.hpp"

namespace caffe {

template <typename Dtype>
void SplitLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* 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) {
    // Allow the 0th top blob to be 'in-place', but no others.
    if (i == 0 && (*top)[i] == bottom[0]) {
      continue;
    } else {
      CHECK_NE((*top)[i], bottom[0]) << "Only 0th top blob may be in place.";
    }
    (*top)[i]->Reshape(bottom[0]->num(), bottom[0]->channels(),
                       bottom[0]->height(), bottom[0]->width());
    CHECK_EQ(count_, (*top)[i]->count());
  }
}

template <typename Dtype>
Dtype SplitLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      vector<Blob<Dtype>*>* top) {
  const Dtype* bottom_data = bottom[0]->cpu_data();
  for (int i = 0; i < top->size(); ++i) {
    if (i == 0 && (*top)[i] == bottom[0]) {
      continue;
    }
    Dtype* top_data = (*top)[i]->mutable_cpu_data();
    caffe_copy(count_, bottom_data, top_data);
  }
  return Dtype(0.);
}

template <typename Dtype>
void SplitLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
  if (propagate_down) {
    const Dtype* top_diff = top[0]->cpu_diff();
    Dtype* bottom_diff = (*bottom)[0]->mutable_cpu_diff();
    // Initialize by copying first top blob diff to our diff, unless we're
    // doing in-place computation for the first blob, in which case the diff is
    // already initialized.
    if (top[0] != (*bottom)[0]) {
      caffe_copy(count_, top_diff, bottom_diff);
    }
    // Add remaining top blob diffs.
    for (int i = 1; i < top.size(); ++i) {
      top_diff = top[i]->cpu_diff();
      caffe_axpy(count_, Dtype(1.), top_diff, bottom_diff);
    }
  }
}


INSTANTIATE_CLASS(SplitLayer);

}  // namespace caffe