|
| 1 | +/*! |
| 2 | + * Copyright (c) 2018 by Contributors |
| 3 | + * \file yolo.cc |
| 4 | + * \brief Yolo related operators |
| 5 | + */ |
| 6 | +#include <tvm/relay/op.h> |
| 7 | +#include <tvm/relay/attrs/vision.h> |
| 8 | +#include <vector> |
| 9 | +#include "../op_common.h" |
| 10 | +#include "../type_relations.h" |
| 11 | + |
| 12 | +namespace tvm { |
| 13 | +namespace relay { |
| 14 | + |
| 15 | +TVM_REGISTER_NODE_TYPE(YoloReorgAttrs); |
| 16 | + |
| 17 | +/*! |
| 18 | +* \brief YoloReorgRel Output type and shape relation evaluation function. |
| 19 | +* \param num_inputs Number of input types in the args. |
| 20 | +* \param attrs The additional attributes of the operator. |
| 21 | +* \param reporter The reporter to report solution to. |
| 22 | +* \return false if This relation cannot be resolved. true if this relation has been resolved. |
| 23 | +*/ |
| 24 | +bool YoloReorgRel(const Array<Type>& types, |
| 25 | + int num_inputs, |
| 26 | + const Attrs& attrs, |
| 27 | + const TypeReporter& reporter) { |
| 28 | + CHECK_EQ(types.size(), 2); |
| 29 | + const auto* data = types[0].as<TensorTypeNode>(); |
| 30 | + if (data == nullptr) return false; |
| 31 | + |
| 32 | + const YoloReorgAttrs* param = attrs.as<YoloReorgAttrs>(); |
| 33 | + CHECK(param != nullptr); |
| 34 | + |
| 35 | + CHECK(data->shape.size() == 4) << "Yolo reorg supports only 4 dimension."; |
| 36 | + std::vector<IndexExpr>&& oshape = AsVector(data->shape); |
| 37 | + oshape[1] = oshape[1] * param->stride * param->stride; |
| 38 | + oshape[2] = oshape[2] / param->stride; |
| 39 | + oshape[3] = oshape[3] / param->stride; |
| 40 | + reporter->Assign(types[1], TensorTypeNode::make(oshape, data->dtype)); |
| 41 | + return true; |
| 42 | +} |
| 43 | + |
| 44 | +Expr MakeYoloReorg(Expr data, |
| 45 | + IndexExpr stride) { |
| 46 | + auto attrs = make_node<YoloReorgAttrs>(); |
| 47 | + attrs->stride = stride; |
| 48 | + static const Op& op = Op::Get("vision.yolo_reorg"); |
| 49 | + return CallNode::make(op, {data}, Attrs(attrs), {}); |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +TVM_REGISTER_API("relay.op.vision._make.yolo_reorg") |
| 54 | +.set_body([](const TVMArgs& args, TVMRetValue* rv) { |
| 55 | + runtime::detail::unpack_call<Expr, 2>(MakeYoloReorg, args, rv); |
| 56 | +}); |
| 57 | + |
| 58 | + |
| 59 | +RELAY_REGISTER_OP("vision.yolo_reorg") |
| 60 | +.describe(R"doc("Yolo reorg operation. This layer reorganize the output. |
| 61 | +Its function is mostly shape transform.")doc" TVM_ADD_FILELINE) |
| 62 | +.add_argument("data", "Tensor", "The input tensor.") |
| 63 | +.set_num_inputs(1) |
| 64 | +.set_support_level(5) |
| 65 | +.set_attrs_type_key("relay.attrs.YoloReorgAttrs") |
| 66 | +.add_type_rel("YoloReorg", YoloReorgRel); |
| 67 | + |
| 68 | + |
| 69 | +Expr MakeYoloRegion(Expr data) { |
| 70 | + static const Op& op = Op::Get("vision.yolo_region"); |
| 71 | + return CallNode::make(op, {data}, Attrs(), {}); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +TVM_REGISTER_API("relay.op.vision._make.yolo_region") |
| 76 | +.set_body([](const TVMArgs& args, TVMRetValue* rv) { |
| 77 | + runtime::detail::unpack_call<Expr, 1>(MakeYoloRegion, args, rv); |
| 78 | +}); |
| 79 | + |
| 80 | + |
| 81 | +RELAY_REGISTER_OP("vision.yolo_region") |
| 82 | +.describe(R"doc("Yolo region operation used for detection." |
| 83 | +)doc" TVM_ADD_FILELINE) |
| 84 | +.add_argument("data", "Tensor", "The input tensor.") |
| 85 | +.set_num_inputs(1) |
| 86 | +.set_support_level(5) |
| 87 | +.add_type_rel("Identity", IdentityRel); |
| 88 | + |
| 89 | + |
| 90 | +Expr MakeYolov3Yolo(Expr data) { |
| 91 | + static const Op& op = Op::Get("vision.yolov3_yolo"); |
| 92 | + return CallNode::make(op, {data}, Attrs(), {}); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +TVM_REGISTER_API("relay.op.vision._make.yolov3_yolo") |
| 97 | +.set_body([](const TVMArgs& args, TVMRetValue* rv) { |
| 98 | + runtime::detail::unpack_call<Expr, 1>(MakeYolov3Yolo, args, rv); |
| 99 | +}); |
| 100 | + |
| 101 | + |
| 102 | +RELAY_REGISTER_OP("vision.yolov3_yolo") |
| 103 | +.describe(R"doc("Yolov3 operation used for detection." |
| 104 | +)doc" TVM_ADD_FILELINE) |
| 105 | +.add_argument("data", "Tensor", "The input tensor.") |
| 106 | +.set_num_inputs(1) |
| 107 | +.set_support_level(5) |
| 108 | +.add_type_rel("Identity", IdentityRel); |
| 109 | + |
| 110 | +} // namespace relay |
| 111 | +} // namespace tvm |
0 commit comments