yolo.cc 3.22 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

Siju committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*!
 * \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.";
55
  std::vector<IndexExpr> oshape(data->shape.begin(), data->shape.end());
Siju committed
56
  oshape[1] = oshape[1] * param->stride * param->stride;
57 58
  oshape[2] = indexdiv(oshape[2], param->stride);
  oshape[3] = indexdiv(oshape[3], param->stride);
59
  reporter->Assign(types[1], TensorType(oshape, data->dtype));
Siju committed
60 61 62 63 64
  return true;
}

Expr MakeYoloReorg(Expr data,
                   Integer stride) {
65
  auto attrs = make_object<YoloReorgAttrs>();
Siju committed
66 67
  attrs->stride = stride;
  static const Op& op = Op::Get("vision.yolo_reorg");
68
  return Call(op, {data}, Attrs(attrs), {});
Siju committed
69 70 71
}


72
TVM_REGISTER_GLOBAL("relay.op.vision._make.yolo_reorg")
73
.set_body_typed(MakeYoloReorg);
Siju committed
74 75 76 77 78 79 80 81


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)
82
.set_attrs_type<YoloReorgAttrs>()
Siju committed
83 84
.add_type_rel("YoloReorg", YoloReorgRel)
.set_attr<FTVMCompute>("FTVMCompute", [](const Attrs& attrs,
85
                                         const Array<te::Tensor>& inputs,
86
                                         const Type& out_type) {
Siju committed
87 88
  const auto* params = attrs.as<YoloReorgAttrs>();
  CHECK(params != nullptr);
89
  return Array<te::Tensor>{ topi::vision::reorg(inputs[0], params->stride) };
Siju committed
90 91 92 93
});

}  // namespace relay
}  // namespace tvm