// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #include "core/common/common.h" #ifndef SHARED_PROVIDER #include "core/framework/op_kernel.h" #endif #include #include namespace onnxruntime { namespace contrib { class CropBase { protected: template CropBase(const KernelInfoType& info) : border_(info.template GetAttrsOrDefault("border")) { scale_specified_ = info.template GetAttrs("scale", scale_).IsOK(); } Status ValidateInput(const Tensor* X) const { if (border_.size() != 4) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Attribute border needs to be specified with four border elements, got ", border_.size()); } if (std::any_of(border_.begin(), border_.end(), [](int64_t border) { return border < 0; })) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Attribute border values must be non-negative"); } if (scale_specified_) { if (scale_.size() != 2) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Attribute scale needs to be specified with two elements (height, width), got ", scale_.size()); } if (std::any_of(scale_.begin(), scale_.end(), [](int64_t scale) { return scale < 0; })) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Attribute scale values must be non-negative"); } } const auto& dims = X->Shape().GetDims(); if (dims.size() != 4) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input is expected to have four dimensions corresponding to [N,C,H,W], got ", dims.size(), " input dimensions instead"); } const int64_t H = dims[2]; const int64_t W = dims[3]; // find the cropped region, and copy it to the destination matrix int64_t leftBorder = border_[0], topBorder = border_[1], rightBorder = border_[2], bottomBorder = border_[3]; if (scale_.empty()) { if (topBorder > H || bottomBorder > H - topBorder) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input's height (", H, ") needs to be greater than or equal to the topBorder (", topBorder, ") + bottomBorder (", bottomBorder, ")"); } if (leftBorder > W || rightBorder > W - leftBorder) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input's width (", W, ") needs to be greater than or equal to the leftBorder (", leftBorder, ") + rightBorder (", rightBorder, ")"); } return Status::OK(); } // scale = (height, width) if (topBorder > H || scale_[0] > H - topBorder) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input's height (", H, ") needs to be greater than or equal to the topBorder (", topBorder, ") + scale_[0] (", scale_[0], ")"); } if (leftBorder > W || scale_[1] > W - leftBorder) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Input's width (", W, ") needs to be greater than or equal to the leftBorder (", leftBorder, ") + scale_[1] (", scale_[1], ")"); } return Status::OK(); } const std::vector border_; // (leftBorder, topBorder, rightBorder, bottomBorder) std::vector scale_; // (height, width) bool scale_specified_; }; template class Crop final : public CropBase, public OpKernel { public: Crop(const OpKernelInfo& info) : CropBase(info), OpKernel(info) { } common::Status Compute(OpKernelContext* context) const override { const Tensor* X = context->Input(0); ORT_RETURN_IF_ERROR(ValidateInput(X)); const auto& dims = X->Shape().GetDims(); const int64_t N = dims[0]; const int64_t C = dims[1]; const int64_t H = dims[2]; const int64_t W = dims[3]; // find the cropped region, and copy it to the destination matrix int64_t leftBorder = border_[0], topBorder = border_[1], rightBorder = border_[2], bottomBorder = border_[3]; int64_t bottomLimit = H - bottomBorder; int64_t rightLimit = W - rightBorder; // scale = (height, width) if (!scale_.empty()) { bottomLimit = topBorder + scale_[0]; rightLimit = leftBorder + scale_[1]; } Tensor* Y = context->Output(0, TensorShape({N, C, bottomLimit - topBorder, rightLimit - leftBorder})); const T* Xdata = X->Data(); T* Ydata = Y->MutableData(); int64_t dest_idx = 0; int64_t HW = H * W; int64_t CHW = C * HW; int64_t nCHW; int64_t nCHW_p_cHW; int64_t nCHW_p_cHW_p_hW; int64_t source_idx; for (int64_t n = 0; n < N; ++n) { nCHW = n * CHW; for (int64_t c = 0; c < C; ++c) { nCHW_p_cHW = nCHW + c * HW; for (int64_t h = topBorder; h < bottomLimit; ++h) { nCHW_p_cHW_p_hW = nCHW_p_cHW + h * W; for (int64_t w = leftBorder; w < rightLimit; ++w) { source_idx = nCHW_p_cHW_p_hW + w; Ydata[dest_idx++] = Xdata[source_idx]; } } } } return Status::OK(); } }; } // namespace contrib } // namespace onnxruntime