Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev roialign #6879

Merged
merged 9 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions oneflow/core/autograd/gradient_funcs/roi_align.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "oneflow/core/framework/op_expr_grad_function.h"
#include "oneflow/core/framework/op_expr.h"
#include "oneflow/core/functional/functional.h"

namespace oneflow {
namespace one {

struct RoiAlignCaptureState : public AutoGradCaptureState {
float spatial_scale = 1.0;
int32_t pooled_h = 0;
int32_t pooled_w = 0;
int32_t sampling_ratio = -1;
bool aligned = false;
bool requires_grad = false;
};

class RoiAlign : public OpExprGradFunction<RoiAlignCaptureState> {
public:
Maybe<void> Init(const OpExpr& op) override {
const auto* fw_op_expr = dynamic_cast<const UserOpExpr*>(&op);
CHECK_NOTNULL_OR_RETURN(fw_op_expr);
base_attrs_ = MakeAttrMapFromUserOpConf(fw_op_expr->proto());
return Maybe<void>::Ok();
}

Maybe<void> Capture(RoiAlignCaptureState* ctx, const TensorTuple& inputs,
const TensorTuple& outputs, const AttrMap& attrs) const override {
ctx->requires_grad = inputs.at(0)->requires_grad();
if (!ctx->requires_grad) { return Maybe<void>::Ok(); }

ctx->SaveTensorForBackward(inputs.at(0));
ctx->SaveTensorForBackward(inputs.at(1));

ComposedAttrMap composed_attrs(attrs, base_attrs_);
ctx->spatial_scale = JUST(composed_attrs.GetAttr<float>("spatial_scale"));
ctx->pooled_h = JUST(composed_attrs.GetAttr<int32_t>("pooled_h"));
ctx->pooled_w = JUST(composed_attrs.GetAttr<int32_t>("pooled_w"));
ctx->sampling_ratio = JUST(composed_attrs.GetAttr<int32_t>("sampling_ratio"));
ctx->aligned = JUST(composed_attrs.GetAttr<bool>("aligned"));
return Maybe<void>::Ok();
}

Maybe<void> Apply(const RoiAlignCaptureState* ctx, const TensorTuple& out_grads,
TensorTuple* in_grads) const override {
if (!ctx->requires_grad) { return Maybe<void>::Ok(); }
const auto& x_like = ctx->SavedTensors().at(0);
const auto& rois = ctx->SavedTensors().at(1);
in_grads->at(0) = JUST(
functional::RoiAlignGrad(out_grads.at(0), x_like, rois, ctx->spatial_scale, ctx->pooled_h,
ctx->pooled_w, ctx->sampling_ratio, ctx->aligned));
return Maybe<void>::Ok();
}

private:
AttrMap base_attrs_;
};

REGISTER_OP_EXPR_GRAD_FUNCTION("roi_align", RoiAlign);

} // namespace one
} // namespace oneflow
10 changes: 10 additions & 0 deletions oneflow/core/functional/functional_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1773,6 +1773,16 @@
"Tensor (Tensor x, Float iou_threshold, Int32 keep_n=-1) => Nms"
bind_python: True

- name: "roi_align"
signature:
"Tensor (Tensor x, Tensor rois, Float spatial_scale, Int32 pooled_h, Int32 pooled_w, Int32 sampling_ratio, Bool aligned) => RoiAlign"
bind_python: True

- name: "roi_align_grad"
signature:
"Tensor (Tensor dy, Tensor x_like, Tensor rois, Float spatial_scale, Int32 pooled_h, Int32 pooled_w, Int32 sampling_ratio, Bool aligned) => RoiAlignGrad"
bind_python: False

- name: "meshgrid"
signature: "TensorTuple (TensorTuple tensors) => Meshgrid"
bind_python: True
54 changes: 54 additions & 0 deletions oneflow/core/functional/impl/nn_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,58 @@ class NmsFunctor {
std::shared_ptr<OpExpr> op_;
};

class RoiAlignFunctor {
public:
RoiAlignFunctor() {
op_ = CHECK_JUST(one::OpBuilder("roi_align").Input("x").Input("rois").Output("y").Build());
}

Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& x,
const std::shared_ptr<one::Tensor>& rois, const float& spatial_scale,
const int32_t& pooled_h, const int32_t& pooled_w,
const int32_t& sampling_ratio, const bool& aligned) const {
MutableAttrMap attrs;
JUST(attrs.SetAttr<float>("spatial_scale", spatial_scale));
JUST(attrs.SetAttr<int32_t>("pooled_h", pooled_h));
JUST(attrs.SetAttr<int32_t>("pooled_w", pooled_w));
JUST(attrs.SetAttr<int32_t>("sampling_ratio", sampling_ratio));
JUST(attrs.SetAttr<bool>("aligned", aligned));
return OpInterpUtil::Dispatch<Tensor>(*op_, {x, rois}, attrs);
}

private:
std::shared_ptr<OpExpr> op_;
};

class RoiAlignGradFunctor {
public:
RoiAlignGradFunctor() {
op_ = CHECK_JUST(one::OpBuilder("roi_align_grad")
.Input("dy")
.Input("x_like")
.Input("rois")
.Output("dx")
.Build());
}

Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& dy,
const std::shared_ptr<one::Tensor>& x_like,
const std::shared_ptr<one::Tensor>& rois, const float& spatial_scale,
const int32_t& pooled_h, const int32_t& pooled_w,
const int32_t& sampling_ratio, const bool& aligned) const {
MutableAttrMap attrs;
JUST(attrs.SetAttr<float>("spatial_scale", spatial_scale));
JUST(attrs.SetAttr<int32_t>("pooled_h", pooled_h));
JUST(attrs.SetAttr<int32_t>("pooled_w", pooled_w));
JUST(attrs.SetAttr<int32_t>("sampling_ratio", sampling_ratio));
JUST(attrs.SetAttr<bool>("aligned", aligned));
return OpInterpUtil::Dispatch<Tensor>(*op_, {dy, x_like, rois}, attrs);
}

private:
std::shared_ptr<OpExpr> op_;
};

} // namespace impl

ONEFLOW_FUNCTION_LIBRARY(m) {
Expand Down Expand Up @@ -2092,6 +2144,8 @@ ONEFLOW_FUNCTION_LIBRARY(m) {
m.add_functor<impl::PartialFCSampleFunctor>("DistributedPariticalFCSample");
m.add_functor<impl::PariticalFCSampleDisableBoxing>("DistributedPariticalFCSampleDisableBoxing");
m.add_functor<impl::NmsFunctor>("Nms");
m.add_functor<impl::RoiAlignFunctor>("RoiAlign");
m.add_functor<impl::RoiAlignGradFunctor>("RoiAlignGrad");
};

} // namespace functional
Expand Down
Loading