Files
opencv-MIRROR/modules/dnn/src/layers/gather_layer.cpp
Alexander Alekhin efc9837df1 Merge pull request #24892 from opencv-pushbot:gitee/alalek/dnn_avoid_16s_usage
DNN: avoid CV_16S usage for FP16 #24892

**Merge after**: #24918

TODO:
- [x] measure performance changes
- [x] optimize convertTo for OpenCL: #24918

12700K iGPU:

|Name of Test|0|1|1 vs 0 (x-factor)|
|---|:-:|:-:|:-:|
|AlexNet::DNNTestNetwork::OCV/OCL_FP16|7.441|7.480|0.99|
|CRNN::DNNTestNetwork::OCV/OCL_FP16|10.776|10.736|1.00|
|DenseNet_121::DNNTestNetwork::OCV/OCL_FP16|52.762|52.833|1.00|
|EAST_text_detection::DNNTestNetwork::OCV/OCL_FP16|60.694|60.721|1.00|
|EfficientNet::DNNTestNetwork::OCV/OCL_FP16|33.373|33.173|1.01|
|FastNeuralStyle_eccv16::DNNTestNetwork::OCV/OCL_FP16|81.840|81.724|1.00|
|GoogLeNet::DNNTestNetwork::OCV/OCL_FP16|20.965|20.927|1.00|
|Inception_5h::DNNTestNetwork::OCV/OCL_FP16|22.204|22.173|1.00|
|Inception_v2_SSD_TensorFlow::DNNTestNetwork::OCV/OCL_FP16|47.115|47.460|0.99|
|MPHand::DNNTestNetwork::OCV/OCL_FP16|6.760|6.670|1.01|
|MPPalm::DNNTestNetwork::OCV/OCL_FP16|10.188|10.171|1.00|
|MPPose::DNNTestNetwork::OCV/OCL_FP16|12.510|12.561|1.00|
|MobileNet_SSD_Caffe::DNNTestNetwork::OCV/OCL_FP16|17.290|17.072|1.01|
|MobileNet_SSD_v1_TensorFlow::DNNTestNetwork::OCV/OCL_FP16|19.473|19.306|1.01|
|MobileNet_SSD_v2_TensorFlow::DNNTestNetwork::OCV/OCL_FP16|22.874|23.404|0.98|
|OpenFace::DNNTestNetwork::OCV/OCL_FP16|9.568|9.517|1.01|
|OpenPose_pose_mpi_faster_4_stages::DNNTestNetwork::OCV/OCL_FP16|539.899|539.845|1.00|
|PPHumanSeg::DNNTestNetwork::OCV/OCL_FP16|18.015|18.769|0.96|
|PPOCRv3::DNNTestNetwork::OCV/OCL_FP16|63.122|63.540|0.99|
|ResNet_50::DNNTestNetwork::OCV/OCL_FP16|34.947|34.925|1.00|
|SFace::DNNTestNetwork::OCV/OCL_FP16|10.249|10.206|1.00|
|SSD::DNNTestNetwork::OCV/OCL_FP16|213.068|213.108|1.00|
|SqueezeNet_v1_1::DNNTestNetwork::OCV/OCL_FP16|4.867|4.878|1.00|
|VIT_B_32::DNNTestNetwork::OCV/OCL_FP16|200.563|190.788|1.05|
|VitTrack::DNNTestNetwork::OCV/OCL_FP16|7.528|7.173|1.05|
|YOLOX::DNNTestNetwork::OCV/OCL_FP16|132.858|132.701|1.00|
|YOLOv3::DNNTestNetwork::OCV/OCL_FP16|209.559|208.809|1.00|
|YOLOv4::DNNTestNetwork::OCV/OCL_FP16|221.357|220.924|1.00|
|YOLOv4_tiny::DNNTestNetwork::OCV/OCL_FP16|24.446|24.382|1.00|
|YOLOv5::DNNTestNetwork::OCV/OCL_FP16|43.922|44.080|1.00|
|YOLOv8::DNNTestNetwork::OCV/OCL_FP16|64.159|63.842|1.00|
|YuNet::DNNTestNetwork::OCV/OCL_FP16|10.177|10.231|0.99|
|opencv_face_detector::DNNTestNetwork::OCV/OCL_FP16|15.121|15.445|0.98|

Co-authored-by: Alexander Alekhin <alexander.a.alekhin@gmail.com>
2024-01-26 16:34:17 +03:00

128 lines
4.3 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "../precomp.hpp"
#include "layers_common.hpp"
namespace cv { namespace dnn {
class GatherLayerImpl CV_FINAL : public GatherLayer
{
public:
GatherLayerImpl(const LayerParams& params)
{
setParamsFrom(params);
m_axis = params.get<int>("axis", 0);
m_real_ndims = params.get<int>("real_ndims", -1);
}
virtual bool supportBackend(int backendId) CV_OVERRIDE
{
return backendId == DNN_BACKEND_OPENCV;
}
virtual bool getMemoryShapes(const std::vector<MatShape> &inputs,
const int requiredOutputs,
std::vector<MatShape> &outputs,
std::vector<MatShape> &internals) const CV_OVERRIDE
{
CV_CheckEQ(inputs.size(), 2ull, "");
MatShape inpShape = inputs[0];
const int axis = normalize_axis(m_axis, inpShape);
inpShape.erase(inpShape.begin() + axis);
auto end = m_real_ndims == -1 ? inputs[1].end() : inputs[1].begin() + m_real_ndims;
inpShape.insert(inpShape.begin() + axis, inputs[1].begin(), end);
outputs.assign(1, inpShape);
return false;
}
void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE
{
CV_TRACE_FUNCTION();
CV_TRACE_ARG_VALUE(name, "name", name.c_str());
// FP16 fallback is not needed as we handle FP16 below
std::vector<Mat> inputs, outputs;
inputs_arr.getMatVector(inputs);
outputs_arr.getMatVector(outputs);
CV_CheckEQ(inputs.size(), (size_t)2, "");
CV_CheckEQ(outputs.size(), (size_t)1, "");
const Mat& inp = inputs[0];
int indicesType = inputs[1].type();
CV_CheckType(indicesType, indicesType == CV_32FC1 || indicesType == CV_16FC1, "");
Mat indices32S;
if (indicesType == CV_16F/*FP16*/)
{
Mat indicesF32;
inputs[1].convertTo(indicesF32, CV_32F);
indicesF32.convertTo(indices32S, CV_32S);
}
else
{
inputs[1].convertTo(indices32S, CV_32S);
}
const size_t indices_total = indices32S.total();
indices32S = indices32S.reshape(1, indices_total);
Mat& out = outputs[0];
CV_CheckTypeEQ(inp.type(), out.type(), "");
CV_CheckTypeEQ(indices32S.type(), CV_32SC1, "");
const int axis = normalize_axis(m_axis, shape(inp));
// FIXIT: why should we work with non-normalized input? it should be handled in importer or layers's output generator
const int axis_size = (int)inp.size[axis];
for (size_t j = 0 ; j < indices_total; ++j)
{
int& idx = indices32S.at<int>(j);
idx = normalize_axis(idx, axis_size); // validate and normalize indices
}
const size_t outer_size = axis == 0 ? inp.total() : inp.step1(axis - 1);
const size_t outer_dims = inp.total() / outer_size;
const size_t inner_size = inp.step1(axis);
const int* idx = indices32S.ptr<int>();
const char* src = inp.ptr<const char>();
char* dst = out.ptr<char>();
CV_CheckEQ(out.total(), outer_dims * indices_total * inner_size, "");
const size_t es = inp.elemSize1();
// TODO: optimize through switch (inner_size * es)
const size_t inner_bytes = inner_size * es;
for (size_t i = 0; i < outer_dims; ++i)
{
const size_t src_offset = i * outer_size;
for (size_t j = 0 ; j < indices_total; ++j)
{
const int index = idx[j];
CV_DbgCheck(index, index >= 0 && index < axis_size, "");
const size_t new_offset = src_offset + index * inner_size;
std::memcpy(dst, src + new_offset * es, inner_bytes);
dst += inner_bytes;
}
}
}
private:
// The axis to gather along
int m_axis;
int m_real_ndims;
};
Ptr<GatherLayer> GatherLayer::create(const LayerParams& params)
{
return makePtr<GatherLayerImpl>(params);
}
}} // namespace cv::dnn