Repository: opencv/opencv
File: modules/dnn/src/layers/affinegrid_layer.cpp (C++)

Code before:
        {
            sz = *explicitSz;
        }
        else
        {
            Net::Impl* netimpl_ = getNetImpl(this);
            if (!netimpl_)
                return;
            try
            {
                sz = netimpl_->argTensor(this->inputs[1]);
            }
            catch (const cv::Exception& e)
            {
                CV_Error(cv::Error::StsError,
                        cv::format("DNN/AffineGrid: failed to resolve output shape from 'size' tensor: %s", e.what()));
            }
        }

        if (sz.empty())
            return;

        CV_CheckTrue(sz.total() == (size_t)(is3d ? 5 : 4),
                    "size input must have 4 (2D) or 5 (3D) elements");

        auto readAt = [&](int idx) -> int {
            if (sz.depth() == CV_64S) return (int)sz.at<int64_t>(idx);
            if (sz.depth() == CV_32S) return (int)sz.at<int32_t>(idx);
            CV_CheckType(sz.depth(), sz.depth() == CV_64S || sz.depth() == CV_32S,
                        "size tensor must be int32 or int64");
            return -1;
        };

        if (is3d) {
            D = readAt(2);
            H = readAt(3);
            W = readAt(4);
            if (D > 0 && H > 0 && W > 0)
                outShape = MatShape{N, D, H, W, 3};
        } else {
            H = readAt(2);
            W = readAt(3);
            if (H > 0 && W > 0)
                outShape = MatShape{N, H, W, 2};
        }
    }

    bool getMemoryShapes(const std::vector<MatShape> &inputs,
                         const int requiredOutputs,
                         std::vector<MatShape> &outputs,
                         std::vector<MatShape> &internals) const CV_OVERRIDE

Review comment (suggestion):
let's just use `tensorToIntVec()` from layers_common.hpp instead of this code with lambda.

Code after:
    }

private:
    void resolveSizeAndShape(int N, bool is3d, const Mat* explicitSz,
                         MatShape& outShape, int& D, int& H, int& W) const
    {
        outShape = is3d ? MatShape{N, -1, -1, -1, 3} : MatShape{N, -1, -1, 2};
        D = 1; H = -1; W = -1;

        Mat sz;
        if (explicitSz)
        {
            sz = *explicitSz;
        }
        else
        {
            Net::Impl* netimpl_ = getNetImpl(this);
            if (!netimpl_)
                return;
            try
            {
                sz = netimpl_->argTensor(this->inputs[1]);
            }
            catch (const cv::Exception& e)
            {
                CV_Error(cv::Error::StsError,
                        cv::format("DNN/AffineGrid: failed to resolve output shape from 'size' tensor: %s", e.what()));
            }
        }

        if (sz.empty())
            return;

        std::vector<int> sizeVec;
        tensorToIntVec(sz, sizeVec);
        CV_CheckTrue((int)sizeVec.size() == (is3d ? 5 : 4), "size input must have 4 (2D) or 5 (3D) elements");

        if (is3d) {
            D = sizeVec[2];
            H = sizeVec[3];
            W = sizeVec[4];
            if (D > 0 && H > 0 && W > 0)
                outShape = MatShape{N, D, H, W, 3};
        } else {
            H = sizeVec[2];
            W = sizeVec[3];
            if (H > 0 && W > 0)
                outShape = MatShape{N, H, W, 2};
        }
    }