forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yolo reorg op for relay (apache#1941)
- Loading branch information
1 parent
1b70315
commit d85e780
Showing
7 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pylint: disable=invalid-name, unused-argument | ||
"""Backend compiler related feature registration""" | ||
from __future__ import absolute_import | ||
from ..op import register_schedule, register_pattern | ||
from ..op import schedule_injective, OpPattern | ||
|
||
# reorg | ||
register_pattern("vision.yolo_reorg", OpPattern.INJECTIVE) | ||
register_schedule("vision.yolo_reorg", schedule_injective) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Yolo operations.""" | ||
from . import _make | ||
|
||
def yolo_reorg(data, stride): | ||
"""Yolo reorg operation used in darknet models. | ||
This layer shuffles the input tensor values based on the stride value. | ||
Along with the shuffling, it does the shape transform. | ||
If '(n, c, h, w)' is the data shape and 's' is stride, output shape is '(n, c*s*s, h/s, w/s)' | ||
Example: data(1, 4, 2, 2) = [[[[ 0 1] [ 2 3]] | ||
[[ 4 5] [ 6 7]] | ||
[[ 8 9] [10 11]] | ||
[[12 13] [14 15]]]] | ||
stride = 2 | ||
ret(1, 16, 1, 1) = [[[[ 0]] [[ 2]] [[ 8]] [[10]] | ||
[[ 1]] [[ 3]] [[ 9]] [[11]] | ||
[[ 4]] [[ 6]] [[12]] [[14]] | ||
[[ 5]] [[ 7]] [[13]] [[15]]]] | ||
Note: stride=1 has no significance for reorg operation. | ||
Parameters | ||
---------- | ||
data : relay.Expr | ||
The input data tensor. | ||
stride : int | ||
The stride value for reorganisation. | ||
Returns | ||
------- | ||
ret : relay.Expr | ||
The computed result. | ||
""" | ||
return _make.yolo_reorg(data, stride) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/*! | ||
* Copyright (c) 2018 by Contributors | ||
* \file yolo.cc | ||
* \brief Yolo related operators | ||
*/ | ||
#include <tvm/relay/op.h> | ||
#include <tvm/relay/attrs/vision.h> | ||
#include <topi/vision/reorg.h> | ||
#include <vector> | ||
#include "../op_common.h" | ||
#include "../type_relations.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
TVM_REGISTER_NODE_TYPE(YoloReorgAttrs); | ||
|
||
/*! | ||
* \brief YoloReorgRel Output type and shape relation evaluation function. | ||
* \param num_inputs Number of input types in the args. | ||
* \param attrs The additional attributes of the operator. | ||
* \param reporter The reporter to report solution to. | ||
* \return false if This relation cannot be resolved. true if this relation has been resolved. | ||
*/ | ||
bool YoloReorgRel(const Array<Type>& types, | ||
int num_inputs, | ||
const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
CHECK_EQ(types.size(), 2); | ||
const auto* data = types[0].as<TensorTypeNode>(); | ||
if (data == nullptr) return false; | ||
|
||
const YoloReorgAttrs* param = attrs.as<YoloReorgAttrs>(); | ||
CHECK(param != nullptr); | ||
|
||
CHECK(data->shape.size() == 4) << "Yolo reorg supports only 4 dimension."; | ||
std::vector<IndexExpr>&& oshape = AsVector(data->shape); | ||
oshape[1] = oshape[1] * param->stride * param->stride; | ||
oshape[2] = oshape[2] / param->stride; | ||
oshape[3] = oshape[3] / param->stride; | ||
reporter->Assign(types[1], TensorTypeNode::make(oshape, data->dtype)); | ||
return true; | ||
} | ||
|
||
Expr MakeYoloReorg(Expr data, | ||
Integer stride) { | ||
auto attrs = make_node<YoloReorgAttrs>(); | ||
attrs->stride = stride; | ||
static const Op& op = Op::Get("vision.yolo_reorg"); | ||
return CallNode::make(op, {data}, Attrs(attrs), {}); | ||
} | ||
|
||
|
||
TVM_REGISTER_API("relay.op.vision._make.yolo_reorg") | ||
.set_body([](const TVMArgs& args, TVMRetValue* rv) { | ||
runtime::detail::unpack_call<Expr, 2>(MakeYoloReorg, args, rv); | ||
}); | ||
|
||
|
||
RELAY_REGISTER_OP("vision.yolo_reorg") | ||
.describe(R"doc("Yolo reorg operation. This layer reorganize the output. | ||
Its function is mostly shape transform.")doc" TVM_ADD_FILELINE) | ||
.add_argument("data", "Tensor", "The input tensor.") | ||
.set_num_inputs(1) | ||
.set_support_level(5) | ||
.set_attrs_type_key("relay.attrs.YoloReorgAttrs") | ||
.add_type_rel("YoloReorg", YoloReorgRel) | ||
.set_attr<FTVMCompute>("FTVMCompute", [](const Attrs& attrs, | ||
const Array<Tensor>& inputs, | ||
const Type& out_type, | ||
const Target& target) { | ||
const auto* params = attrs.as<YoloReorgAttrs>(); | ||
CHECK(params != nullptr); | ||
return Array<Tensor>{ topi::vision::reorg(inputs[0], params->stride) }; | ||
}); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters