|
6 | 6 | namespace caffe { |
7 | 7 |
|
8 | 8 | template <typename Dtype> |
9 | | - void NotEqualLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
10 | | - const vector<Blob<Dtype>*>& top) { |
11 | | - |
12 | | - Blob<Dtype>* comparand = (bottom.size() > 1) ? bottom[1] : this->blobs_[0].get(); |
13 | | - vector<int> bottom_shape = bottom[0]->shape(); |
14 | | - |
15 | | - // case 1: bottom[1] is a scalar(bottom[0] may be a scalar) |
16 | | - if (comparand->num_axes() == 0) {} |
17 | | - // case 2: bottom[0] and bottom[1] are tensor and have the same dimension |
18 | | - else if (comparand->num_axes() == bottom[0]->num_axes()) { |
19 | | - for (int i = 0; i < bottom[0]->num_axes(); ++i) { |
20 | | - CHECK_EQ(bottom[0]->shape(i), comparand->shape(i)) << "Broadcasting is not supported now!!! Please confirm that 2 inputs have the same shape!!"; |
21 | | - } |
| 9 | + void NotEqualLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype> *> &bottom, |
| 10 | + const vector<Blob<Dtype> *> &top) { |
| 11 | + if ((bottom.size() == 1) && (this->blobs_.size() == 0)) { |
| 12 | + const NotEqualParameter ¬_equal_param = this->layer_param_.not_equal_param(); |
| 13 | + comparand_ = not_equal_param.comparand(); |
| 14 | + const_flag_ = 1; |
22 | 15 | } |
23 | | - // case 3: bottom[0] and bottom[1] are tensor and have different dimension/shape |
24 | 16 | else { |
25 | | - CHECK_EQ(bottom[0]->num_axes(), comparand->num_axes()) << "Broadcasting is not supported now!!! Please confirm that 2 inputs have the same shape!!"; |
| 17 | + const_flag_ = 0; |
| 18 | + Blob<Dtype>* comparand = (bottom.size() > 1) ? bottom[1] : this->blobs_[0].get(); |
| 19 | + // case 1: bottom[0] and bottom[1] are tensor and have the same dimension |
| 20 | + if (comparand->num_axes() == bottom[0]->num_axes()) { |
| 21 | + for (int i = 0; i < bottom[0]->num_axes(); ++i) { |
| 22 | + CHECK_EQ(bottom[0]->shape(i), comparand->shape(i)) << "Broadcasting is not supported now!!! Please confirm that 2 inputs have the same shape!!"; |
| 23 | + } |
| 24 | + } |
| 25 | + // case 2: bottom[0] and bottom[1] are tensor and have different dimension/shape |
| 26 | + else { |
| 27 | + CHECK_EQ(bottom[0]->num_axes(), comparand->num_axes()) << "Broadcasting is not supported now!!! Please confirm that 2 inputs have the same shape!!"; |
| 28 | + } |
26 | 29 | } |
27 | | - vector<int> top_shape = bottom_shape; |
28 | | - top[0]->Reshape(top_shape); |
| 30 | + } |
| 31 | + |
| 32 | + template <typename Dtype> |
| 33 | + void NotEqualLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 34 | + const vector<Blob<Dtype>*>& top) { |
| 35 | + vector<int> bottom_shape = bottom[0]->shape(); |
| 36 | + top[0]->Reshape(bottom_shape); |
29 | 37 | } |
30 | 38 |
|
31 | 39 | template <typename Dtype> |
32 | 40 | void NotEqualLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |
33 | 41 | const vector<Blob<Dtype>*>& top) { |
34 | 42 | const Dtype* bottom_data = bottom[0]->cpu_data(); |
35 | | - Blob<Dtype>* comparand = (bottom.size() > 1) ? bottom[1] : this->blobs_[0].get(); |
36 | | - const Dtype* comparand_data = comparand->cpu_data(); |
37 | 43 | Dtype* top_data = top[0]->mutable_cpu_data(); |
38 | | - if (comparand->num_axes() == 0) { |
| 44 | + if (const_flag_ == 1) { |
39 | 45 | for (int i = 0; i < top[0]->count(); ++i) { |
40 | | - top_data[i] = bool(bottom_data[i] != comparand_data[0]); |
| 46 | + top_data[i] = bool(bottom_data[i] != comparand_); |
41 | 47 | } |
42 | 48 | } |
43 | 49 | else { |
| 50 | + Blob<Dtype>* comparand = (bottom.size() > 1) ? bottom[1] : this->blobs_[0].get(); |
| 51 | + const Dtype* comparand_data = comparand->cpu_data(); |
44 | 52 | for (int i = 0; i < top[0]->count(); ++i) { |
45 | 53 | top_data[i] = bool(bottom_data[i] != comparand_data[i]); |
46 | 54 | } |
|
0 commit comments