Apollo 11.0
自动驾驶开放平台
roipooling_layer.cc
浏览该文件的文档.
1/******************************************************************************
2COPYRIGHT
3
4All contributions by the University of California:
5Copyright (c) 2014-2017 The Regents of the University of California (Regents)
6All rights reserved.
7
8All other contributions:
9Copyright (c) 2014-2017, the respective contributors
10All rights reserved.
11
12Caffe uses a shared copyright model: each contributor holds copyright over
13their contributions to Caffe. The project versioning records all such
14contribution and copyright details. If a contributor wants to further mark
15their specific copyright on a particular contribution, they should indicate
16their copyright solely in the commit message of the change when it is
17committed.
18
19LICENSE
20
21Redistribution and use in source and binary forms, with or without
22modification, are permitted provided that the following conditions are met:
23
241. Redistributions of source code must retain the above copyright notice, this
25 list of conditions and the following disclaimer.
262. Redistributions in binary form must reproduce the above copyright notice,
27 this list of conditions and the following disclaimer in the documentation
28 and/or other materials provided with the distribution.
29
30THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
34ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41CONTRIBUTION AGREEMENT
42
43By contributing to the BVLC/caffe repository through pull-request, comment,
44or otherwise, the contributor releases their content to the
45license and copyright terms herein.
46 *****************************************************************************/
47
48/******************************************************************************
49 * Copyright 2018 The Apollo Authors. All Rights Reserved.
50 *
51 * Licensed under the Apache License, Version 2.0 (the "License");
52 * you may not use this file except in compliance with the License.
53 * You may obtain a copy of the License at
54 *
55 * http://www.apache.org/licenses/LICENSE-2.0
56 *
57 * Unless required by applicable law or agreed to in writing, software
58 * distributed under the License is distributed on an "AS IS" BASIS,
59 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
60 * See the License for the specific language governing permissions and
61 * limitations under the License.
62 *****************************************************************************/
63
65
66#include <algorithm>
67
69
70namespace apollo {
71namespace perception {
72namespace inference {
73
74template <typename Dtype>
76 const std::vector<std::shared_ptr<base::Blob<Dtype>>> &bottom,
77 const std::vector<std::shared_ptr<base::Blob<Dtype>>> &top) {
78 channels_ = bottom[0]->channels();
79 height_ = bottom[0]->height();
80 width_ = bottom[0]->width();
81 top[0]->Reshape(bottom[1]->num(), channels_, pooled_height_, pooled_width_);
82 auto feat_bottom = bottom[0];
83 auto roi_bottom = bottom[1];
84 const Dtype *bottom_data = feat_bottom->cpu_data();
85 const Dtype *bottom_rois = roi_bottom->cpu_data();
86 // Number of ROIs
87 int num_rois = roi_bottom->num();
88 int batch_size = feat_bottom->num();
89 int top_count = top[0]->count();
90 Dtype *top_data = top[0]->mutable_cpu_data();
91 memset(top_data, -1, static_cast<size_t>(float_max_ * sizeof(Dtype)));
92 int *argmax_data = max_idx_.mutable_cpu_data();
93 memset(argmax_data, -1, top_count * sizeof(int));
94 // For each ROI R = [batch_index x1 y1 x2 y2]: max pool over R
95 for (int n = 0; n < num_rois; ++n) {
96 int roi_batch_ind = static_cast<int>(bottom_rois[0]);
97 int roi_start_w = 0;
98 int roi_start_h = 0;
99 int roi_end_w = 0;
100 int roi_end_h = 0;
101 if (use_floor_) {
102 roi_start_w = static_cast<int>(floor(bottom_rois[1] * spatial_scale_));
103 roi_start_h = static_cast<int>(floor(bottom_rois[2] * spatial_scale_));
104 roi_end_w = static_cast<int>(floor(bottom_rois[3] * spatial_scale_));
105 roi_end_h = static_cast<int>(floor(bottom_rois[4] * spatial_scale_));
106 } else {
107 roi_start_w = static_cast<int>(round(bottom_rois[1] * spatial_scale_));
108 roi_start_h = static_cast<int>(round(bottom_rois[2] * spatial_scale_));
109 roi_end_w = static_cast<int>(round(bottom_rois[3] * spatial_scale_));
110 roi_end_h = static_cast<int>(round(bottom_rois[4] * spatial_scale_));
111 }
112 CHECK_GE(roi_batch_ind, 0);
113 CHECK_LT(roi_batch_ind, batch_size);
114
115 int roi_height = std::max(roi_end_h - roi_start_h + 1, 1);
116 int roi_width = std::max(roi_end_w - roi_start_w + 1, 1);
117 const Dtype bin_size_h =
118 static_cast<Dtype>(roi_height) / static_cast<Dtype>(pooled_height_);
119 const Dtype bin_size_w =
120 static_cast<Dtype>(roi_width) / static_cast<Dtype>(pooled_width_);
121
122 const Dtype *batch_data = bottom_data + bottom[0]->offset(roi_batch_ind);
123
124 for (int c = 0; c < channels_; ++c) {
125 for (int ph = 0; ph < pooled_height_; ++ph) {
126 for (int pw = 0; pw < pooled_width_; ++pw) {
127 // Compute pooling region for this output unit:
128 int hstart =
129 static_cast<int>(floor(static_cast<Dtype>(ph) * bin_size_h));
130 int wstart =
131 static_cast<int>(floor(static_cast<Dtype>(pw) * bin_size_w));
132 int hend =
133 static_cast<int>(ceil(static_cast<Dtype>(ph + 1) * bin_size_h));
134 int wend =
135 static_cast<int>(ceil(static_cast<Dtype>(pw + 1) * bin_size_w));
136
137 hstart = std::min(std::max(hstart + roi_start_h, 0), height_);
138 hend = std::min(std::max(hend + roi_start_h, 0), height_);
139 wstart = std::min(std::max(wstart + roi_start_w, 0), width_);
140 wend = std::min(std::max(wend + roi_start_w, 0), width_);
141
142 bool is_empty = (hend <= hstart) || (wend <= wstart);
143 const int pool_index = ph * pooled_width_ + pw;
144 if (is_empty) {
145 top_data[pool_index] = 0;
146 argmax_data[pool_index] = -1;
147 }
148
149 for (int h = hstart; h < hend; ++h) {
150 for (int w = wstart; w < wend; ++w) {
151 const int index = h * width_ + w;
152 if (batch_data[index] > top_data[pool_index]) {
153 top_data[pool_index] = batch_data[index];
154 argmax_data[pool_index] = index;
155 }
156 }
157 }
158 }
159 }
160 // Increment all data pointers by one channel
161 batch_data += feat_bottom->offset(0, 1);
162 top_data += top[0]->offset(0, 1);
163 argmax_data += max_idx_.offset(0, 1);
164 }
165 // Increment ROI data pointer
166 bottom_rois += roi_bottom->offset(1);
167 }
168}
169template class ROIPoolingLayer<float>;
170template class ROIPoolingLayer<double>;
171
172} // namespace inference
173} // namespace perception
174} // namespace apollo
A wrapper around SyncedMemory holders serving as the basic computational unit for images,...
Definition blob.h:88
void ForwardCPU(const std::vector< std::shared_ptr< base::Blob< Dtype > > > &bottom, const std::vector< std::shared_ptr< base::Blob< Dtype > > > &top)
class register implement
Definition arena_queue.h:37