|
| 1 | +#include <algorithm> |
| 2 | +#include <map> |
| 3 | +#include <set> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "caffe/layer.hpp" |
| 7 | +#include "caffe/net.hpp" |
| 8 | +#include "caffe/vision_layers.hpp" |
| 9 | + |
| 10 | +namespace caffe { |
| 11 | + |
| 12 | +template <typename Dtype> |
| 13 | +void CropLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 14 | + const vector<Blob<Dtype>*>& top) { |
| 15 | + // Construct a map from top blobs to layer inds, skipping over in-place |
| 16 | + // connections. |
| 17 | + map<Blob<Dtype>*, int> down_map; |
| 18 | + for (int layer_ind = 0; layer_ind < this->net_->top_vecs().size(); |
| 19 | + ++layer_ind) { |
| 20 | + vector<Blob<Dtype>*> tops = this->net_->top_vecs()[layer_ind]; |
| 21 | + for (int top_ind = 0; top_ind < tops.size(); ++top_ind) { |
| 22 | + if (down_map.find(tops[top_ind]) == down_map.end()) { |
| 23 | + down_map[tops[top_ind]] = layer_ind; |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + // Walk back from the first bottom, keeping track of all the blobs we pass. |
| 28 | + set<Blob<Dtype>*> path_blobs; |
| 29 | + Blob<Dtype>* blob = bottom[0]; |
| 30 | + int layer_ind; |
| 31 | + // TODO this logic can be simplified if all blobs are tops |
| 32 | + path_blobs.insert(blob); |
| 33 | + while (down_map.find(blob) != down_map.end()) { |
| 34 | + layer_ind = down_map[blob]; |
| 35 | + if (this->net_->bottom_vecs()[layer_ind].size() == 0) { |
| 36 | + break; |
| 37 | + } |
| 38 | + blob = this->net_->bottom_vecs()[layer_ind][0]; |
| 39 | + path_blobs.insert(blob); |
| 40 | + } |
| 41 | + // Now walk back from the second bottom, until we find a blob of intersection. |
| 42 | + Blob<Dtype>* inter_blob = bottom[1]; |
| 43 | + while (path_blobs.find(inter_blob) == path_blobs.end()) { |
| 44 | + CHECK(down_map.find(inter_blob) != down_map.end()) |
| 45 | + << "Cannot align apparently disconnected blobs."; |
| 46 | + layer_ind = down_map[inter_blob]; |
| 47 | + CHECK_GT(this->net_->bottom_vecs()[layer_ind].size(), 0) |
| 48 | + << "Cannot align apparently disconnected blobs."; |
| 49 | + inter_blob = this->net_->bottom_vecs()[layer_ind][0]; |
| 50 | + } |
| 51 | + // Compute the coord map from the blob of intersection to each bottom. |
| 52 | + vector<DiagonalAffineMap<Dtype> > coord_maps(2, |
| 53 | + DiagonalAffineMap<Dtype>::identity(2)); |
| 54 | + for (int i = 0; i < 2; ++i) { |
| 55 | + for (Blob<Dtype>* blob = bottom[i]; blob != inter_blob; |
| 56 | + blob = this->net_->bottom_vecs()[down_map[blob]][0]) { |
| 57 | + shared_ptr<Layer<Dtype> > layer = this->net_->layers()[down_map[blob]]; |
| 58 | + coord_maps[i] = coord_maps[i].compose(layer->coord_map()); |
| 59 | + } |
| 60 | + } |
| 61 | + // Compute the mapping from first bottom coordinates to second. |
| 62 | + DiagonalAffineMap<Dtype> crop_map = |
| 63 | + coord_maps[1].compose(coord_maps[0].inv()); |
| 64 | + for (int i = 0; i < 2; ++i) { |
| 65 | + // Check for scale mismatch (unfortunately, CHECK_DOUBLE_EQ does not |
| 66 | + // support a message like the other CHECKs). |
| 67 | + CHECK_DOUBLE_EQ(crop_map.coefs()[i].first, 1); |
| 68 | + CHECK_LE(crop_map.coefs()[i].second, 0) << "Negative crop width."; |
| 69 | + // Check that the crop width is an integer. |
| 70 | + CHECK_DOUBLE_EQ(crop_map.coefs()[i].second, |
| 71 | + round(crop_map.coefs()[i].second)); |
| 72 | + } |
| 73 | + crop_h_ = - round(crop_map.coefs()[0].second); |
| 74 | + crop_w_ = - round(crop_map.coefs()[1].second); |
| 75 | +} |
| 76 | + |
| 77 | +template <typename Dtype> |
| 78 | +void CropLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 79 | + const vector<Blob<Dtype>*>& top) { |
| 80 | + top[0]->Reshape(bottom[0]->num(), bottom[0]->channels(), bottom[1]->height(), |
| 81 | + bottom[1]->width()); |
| 82 | +} |
| 83 | + |
| 84 | +template <typename Dtype> |
| 85 | +void CropLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |
| 86 | + const vector<Blob<Dtype>*>& top) { |
| 87 | + const Dtype* bottom_data = bottom[0]->cpu_data(); |
| 88 | + Dtype* top_data = top[0]->mutable_cpu_data(); |
| 89 | + for (int n = 0; n < top[0]->num(); ++n) { |
| 90 | + for (int c = 0; c < top[0]->channels(); ++c) { |
| 91 | + for (int h = 0; h < top[0]->height(); ++h) { |
| 92 | + caffe_copy(top[0]->width(), |
| 93 | + bottom_data + bottom[0]->offset(n, c, crop_h_ + h, crop_w_), |
| 94 | + top_data + top[0]->offset(n, c, h)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +template <typename Dtype> |
| 101 | +void CropLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, |
| 102 | + const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { |
| 103 | + const Dtype* top_diff = top[0]->cpu_diff(); |
| 104 | + Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); |
| 105 | + if (propagate_down[0]) { |
| 106 | + caffe_set(bottom[0]->count(), static_cast<Dtype>(0), bottom_diff); |
| 107 | + for (int n = 0; n < top[0]->num(); ++n) { |
| 108 | + for (int c = 0; c < top[0]->channels(); ++c) { |
| 109 | + for (int h = 0; h < top[0]->height(); ++h) { |
| 110 | + caffe_copy(top[0]->width(), |
| 111 | + top_diff + top[0]->offset(n, c, h), |
| 112 | + bottom_diff + bottom[0]->offset(n, c, crop_h_ + h, crop_w_)); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +#ifdef CPU_ONLY |
| 120 | +STUB_GPU(CropLayer); |
| 121 | +#endif |
| 122 | + |
| 123 | +INSTANTIATE_CLASS(CropLayer); |
| 124 | +REGISTER_LAYER_CLASS(CROP, CropLayer); |
| 125 | + |
| 126 | +} // namespace caffe |
0 commit comments