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

Code before:
        catch (const cv::Exception& e)
        {
            CV_Error_(Error::StsError, ("OneHot: failed to access constant depth: %s", e.what()));
        }
        return false;
    }

    bool tryInferStaticOutputShape(const MatShape& idxShape, MatShape& outShape) const
    {
        outShape.clear();
        int depth = 0;
        if (getConstPositiveDepth(depth))
        {
            outShape = idxShape;
            int insAxis = normalize_axis(axis, (int)outShape.size() + 1);
            outShape.insert(outShape.begin() + insAxis, depth);
            return true;
        }
        return false;
    }

public:
    bool dynamicOutputShapes() const CV_OVERRIDE
    {
        int depth = 0;
        return !getConstPositiveDepth(depth);
    }

    bool getMemoryShapes(const std::vector<MatShape>& inputs,
                         const int requiredOutputs,
                         std::vector<MatShape>& outputs,
                         std::vector<MatShape>& internals) const CV_OVERRIDE
    {
        CV_Assert(inputs.size() == 3);
        const MatShape& idxShape = inputs[0];
        CV_Assert(!idxShape.empty());

        MatShape outShape;
        if (tryInferStaticOutputShape(idxShape, outShape))
        {
            outputs.assign(1, outShape);
            internals.clear();
            return true;
        }
        outputs.assign(1, MatShape());
        internals.clear();
        return true;
    }

    void getTypes(const std::vector<MatType>& inputs,
                  const int requiredOutputs,

Review comment (bug):
this is incorrect. There must be check that the `depth` input is constant argument. Instead, you are trying to use `netimpl_->argTensor()`. The result of this method is undefined if the argument is non-constant and you are calling this before `forward()` method.

Code after:
                }
            }
        }
        catch (const cv::Exception& e)
        {
            CV_Error_(Error::StsError, ("OneHot: failed to access constant depth: %s", e.what()));
        }
        return false;
    }

    bool tryInferStaticOutputShape(const MatShape& idxShape, MatShape& outShape) const
    {
        outShape.clear();
        int depth = 0;
        if (getConstPositiveDepth(depth))
        {
            outShape = idxShape;
            int insAxis = normalize_axis(axis, (int)outShape.size() + 1);
            outShape.insert(outShape.begin() + insAxis, depth);
            return true;
        }
        return false;
    }

public:
    bool dynamicOutputShapes() const CV_OVERRIDE
    {
        Net::Impl* netimpl_ = getNetImpl(this);
        return !(netimpl_ && netimpl_->isConstArg(this->inputs[1]));
    }

    bool getMemoryShapes(const std::vector<MatShape>& inputs,
                         const int requiredOutputs,
                         std::vector<MatShape>& outputs,
                         std::vector<MatShape>& internals) const CV_OVERRIDE
    {
        CV_Assert(inputs.size() == 3);
        const MatShape& idxShape = inputs[0];
        CV_Assert(!idxShape.empty());

        MatShape outShape;
        if (tryInferStaticOutputShape(idxShape, outShape))
        {
            outputs.assign(1, outShape);
            internals.clear();
            return true;
        }
        outputs.assign(1, MatShape());
        internals.clear();
        return true;
    }