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

Code before:
                        int insAxis = normalize_axis(axis, (int)outShape.size() + 1);
                        outShape.insert(outShape.begin() + insAxis, (int)depth64);
                        outputs.assign(1, outShape);
                        internals.clear();
                        return true;
                    }
                }
            }
        }
        catch (const cv::Exception& e)
        {
            CV_LOG_DEBUG(NULL, "OneHot: getMemoryShapes dynamic fallback (cv::Exception): " << e.what());
        }

        outputs.assign(1, MatShape());
        internals.clear();
        return true;
    }

    void getTypes(const std::vector<MatType>& inputs,
                  const int requiredOutputs,
                  const int requiredInternals,
                  std::vector<MatType>& outputs,
                  std::vector<MatType>& internals) const CV_OVERRIDE
    {
        int outType = CV_32F;
        if (inputs.size() >= 3 && inputs[2] >= 0)
            outType = inputs[2];
        outputs.assign(requiredOutputs, outType);
        internals.assign(requiredInternals, MatType(-1));
    }

private:
    template<typename Tout>
    void runKernel(const Mat& indices, const Mat& depthMat, const Mat& values, Mat& Y)
    {
        int64_t depth64 = 0;
        tensorToScalar(depthMat, CV_64S, &depth64);
        CV_CheckGT(depth64, 0, "OneHot: depth must be > 0");
        int depth = (int)depth64;

        MatShape outShape = shape(indices);
        int ndims = (int)outShape.size();
        int insAxis = normalize_axis(axis, ndims + 1);
        outShape.insert(outShape.begin() + insAxis, depth);

        if (Y.empty() || Y.type() != DataType<Tout>::type || Y.shape() != outShape)
            Y.create(outShape, DataType<Tout>::type);

        Tout offVal = (Tout)0;
        Tout onVal  = (Tout)1;

Review comment (suggestion):
please, implement this method for all types; it should not be difficult. Just implement it for 1-byte, 2-byte, 4-byte and 8-byte elements.

```
CV_Assert(inputs.size() == 3);
int outType = inputs[2];
...
```


Code after:
        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;
    }

    void getTypes(const std::vector<MatType>& inputs,
                  const int requiredOutputs,
                  const int requiredInternals,
                  std::vector<MatType>& outputs,
                  std::vector<MatType>& internals) const CV_OVERRIDE
    {
        CV_Assert(inputs.size() == 3);
        int outType = (inputs[2] >= 0) ? inputs[2] : CV_32F;
        outputs.assign(requiredOutputs, outType);
        internals.assign(requiredInternals, MatType(-1));
    }

private:
    template<typename Tout>
    void runKernel(const Mat& indices, int depth, const Mat& values, Mat& Y)
    {
        CV_CheckGT(depth, 0, "OneHot: depth must be > 0");

        MatShape outShape = shape(indices);
        int ndims = (int)outShape.size();