roi_align.cpp 6.15 KB
Newer Older
1
#include "roi_align.h"
2
3
4

#include <torch/autograd.h>
#include <torch/types.h>
5

6
7
namespace vision {
namespace ops {
8
9

at::Tensor roi_align(
10
11
    const at::Tensor& input, // Input feature map.
    const at::Tensor& rois, // List of ROIs to pool over.
12
    double spatial_scale, // The scale of the image features. ROIs will be
13
    // scaled to this.
14
15
16
17
    int64_t pooled_height, // The height of the pooled feature map.
    int64_t pooled_width, // The width of the pooled feature
    int64_t sampling_ratio, // The number of points to sample in each bin
    bool aligned) // The flag for pixel shift
18
19
// along each axis.
{
20
21
22
23
  static auto op = c10::Dispatcher::singleton()
                       .findSchemaOrThrow("torchvision::roi_align", "")
                       .typed<decltype(roi_align)>();
  return op.call(
Francisco Massa's avatar
Francisco Massa committed
24
25
26
27
28
29
30
      input,
      rois,
      spatial_scale,
      pooled_height,
      pooled_width,
      sampling_ratio,
      aligned);
31
32
}

33
at::Tensor _roi_align_backward(
34
35
    const at::Tensor& grad,
    const at::Tensor& rois,
36
37
38
39
40
41
42
43
44
    double spatial_scale,
    int64_t pooled_height,
    int64_t pooled_width,
    int64_t batch_size,
    int64_t channels,
    int64_t height,
    int64_t width,
    int64_t sampling_ratio,
    bool aligned) {
45
46
47
48
49
  static auto op =
      c10::Dispatcher::singleton()
          .findSchemaOrThrow("torchvision::_roi_align_backward", "")
          .typed<decltype(_roi_align_backward)>();
  return op.call(
50
51
52
53
54
55
56
57
58
      grad,
      rois,
      spatial_scale,
      pooled_height,
      pooled_width,
      batch_size,
      channels,
      height,
      width,
AhnDW's avatar
AhnDW committed
59
60
      sampling_ratio,
      aligned);
61
}
62

63
64
65
66
67
68
69
TORCH_LIBRARY_FRAGMENT(torchvision, m) {
  m.def(
      "roi_align(Tensor input, Tensor rois, float spatial_scale, int pooled_height, int pooled_width, int sampling_ratio, bool aligned) -> Tensor");
  m.def(
      "_roi_align_backward(Tensor grad, Tensor rois, float spatial_scale, int pooled_height, int pooled_width, int batch_size, int channels, int height, int width, int sampling_ratio, bool aligned) -> Tensor");
}

70
71
namespace {

72
73
class ROIAlignFunction : public torch::autograd::Function<ROIAlignFunction> {
 public:
74
75
  static torch::autograd::variable_list forward(
      torch::autograd::AutogradContext* ctx,
76
77
78
79
80
81
82
      const torch::autograd::Variable& input,
      const torch::autograd::Variable& rois,
      double spatial_scale,
      int64_t pooled_height,
      int64_t pooled_width,
      int64_t sampling_ratio,
      bool aligned) {
83
84
85
86
    ctx->saved_data["spatial_scale"] = spatial_scale;
    ctx->saved_data["pooled_height"] = pooled_height;
    ctx->saved_data["pooled_width"] = pooled_width;
    ctx->saved_data["sampling_ratio"] = sampling_ratio;
AhnDW's avatar
AhnDW committed
87
    ctx->saved_data["aligned"] = aligned;
88
89
    ctx->saved_data["input_shape"] = input.sizes();
    ctx->save_for_backward({rois});
90
91
    at::AutoNonVariableTypeMode g;
    auto result = roi_align(
92
93
94
95
96
        input,
        rois,
        spatial_scale,
        pooled_height,
        pooled_width,
AhnDW's avatar
AhnDW committed
97
98
        sampling_ratio,
        aligned);
99
100
101
    return {result};
  }

102
103
  static torch::autograd::variable_list backward(
      torch::autograd::AutogradContext* ctx,
104
      const torch::autograd::variable_list& grad_output) {
105
106
107
108
    // Use data saved in forward
    auto saved = ctx->get_saved_variables();
    auto rois = saved[0];
    auto input_shape = ctx->saved_data["input_shape"].toIntList();
109
    auto grad_in = _roi_align_backward(
110
111
112
113
114
115
116
117
118
        grad_output[0],
        rois,
        ctx->saved_data["spatial_scale"].toDouble(),
        ctx->saved_data["pooled_height"].toInt(),
        ctx->saved_data["pooled_width"].toInt(),
        input_shape[0],
        input_shape[1],
        input_shape[2],
        input_shape[3],
AhnDW's avatar
AhnDW committed
119
120
        ctx->saved_data["sampling_ratio"].toInt(),
        ctx->saved_data["aligned"].toBool());
Francisco Massa's avatar
Francisco Massa committed
121
    return {grad_in,
122
123
124
125
126
127
            torch::autograd::Variable(),
            torch::autograd::Variable(),
            torch::autograd::Variable(),
            torch::autograd::Variable(),
            torch::autograd::Variable(),
            torch::autograd::Variable()};
128
129
130
  }
};

131
132
133
134
135
136
// TODO: There should be an easier way to do this
class ROIAlignBackwardFunction
    : public torch::autograd::Function<ROIAlignBackwardFunction> {
 public:
  static torch::autograd::variable_list forward(
      torch::autograd::AutogradContext* ctx,
137
138
139
140
141
142
143
144
145
146
147
      const torch::autograd::Variable& grad,
      const torch::autograd::Variable& rois,
      double spatial_scale,
      int64_t pooled_height,
      int64_t pooled_width,
      int64_t batch_size,
      int64_t channels,
      int64_t height,
      int64_t width,
      int64_t sampling_ratio,
      bool aligned) {
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    at::AutoNonVariableTypeMode g;
    auto result = _roi_align_backward(
        grad,
        rois,
        spatial_scale,
        pooled_height,
        pooled_width,
        batch_size,
        channels,
        height,
        width,
        sampling_ratio,
        aligned);
    return {result};
  }

  static torch::autograd::variable_list backward(
      torch::autograd::AutogradContext* ctx,
166
      const torch::autograd::variable_list& grad_output) {
167
168
169
170
    TORCH_CHECK(0, "double backwards on roi_align not supported");
  }
};

171
at::Tensor roi_align_autograd(
172
173
    const at::Tensor& input,
    const at::Tensor& rois,
174
175
176
177
178
    double spatial_scale,
    int64_t pooled_height,
    int64_t pooled_width,
    int64_t sampling_ratio,
    bool aligned) {
179
180
181
182
183
184
  return ROIAlignFunction::apply(
      input,
      rois,
      spatial_scale,
      pooled_height,
      pooled_width,
AhnDW's avatar
AhnDW committed
185
186
      sampling_ratio,
      aligned)[0];
187
}
188

189
at::Tensor roi_align_backward_autograd(
190
191
    const at::Tensor& grad,
    const at::Tensor& rois,
192
193
194
195
196
197
198
199
200
    double spatial_scale,
    int64_t pooled_height,
    int64_t pooled_width,
    int64_t batch_size,
    int64_t channels,
    int64_t height,
    int64_t width,
    int64_t sampling_ratio,
    bool aligned) {
201
202
203
204
205
206
207
208
209
210
211
212
213
  return ROIAlignBackwardFunction::apply(
      grad,
      rois,
      spatial_scale,
      pooled_height,
      pooled_width,
      batch_size,
      channels,
      height,
      width,
      sampling_ratio,
      aligned)[0];
}
214

215
216
} // namespace

217
218
219
220
221
TORCH_LIBRARY_IMPL(torchvision, Autograd, m) {
  m.impl("roi_align", roi_align_autograd);
  m.impl("_roi_align_backward", roi_align_backward_autograd);
}

222
223
} // namespace ops
} // namespace vision