mirror of
https://github.com/opencv/opencv.git
synced 2026-11-28 20:37:06 -06:00
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>
218 lines
7.9 KiB
C++
218 lines
7.9 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"
|
|
|
|
#include <algorithm> // for std::max & std::min
|
|
|
|
namespace cv { namespace dnn {
|
|
|
|
class ScatterNDLayerImpl CV_FINAL : public ScatterNDLayer
|
|
{
|
|
public:
|
|
enum class REDUCTION
|
|
{
|
|
NONE = 1,
|
|
ADD,
|
|
MUL,
|
|
MAX,
|
|
MIN
|
|
} reduction;
|
|
|
|
ScatterNDLayerImpl(const LayerParams& params)
|
|
{
|
|
setParamsFrom(params);
|
|
|
|
String reduction_name = toLowerCase(params.get<String>("reduction", "none"));
|
|
if (reduction_name == "none")
|
|
reduction = REDUCTION::NONE;
|
|
else if (reduction_name == "add")
|
|
reduction = REDUCTION::ADD;
|
|
else if (reduction_name == "mul")
|
|
reduction = REDUCTION::MUL;
|
|
else if (reduction_name == "max")
|
|
reduction = REDUCTION::MAX;
|
|
else if (reduction_name == "min")
|
|
reduction = REDUCTION::MIN;
|
|
else
|
|
CV_Error(cv::Error::StsBadArg, "Unkown reduction \"" + reduction_name + "\"");
|
|
}
|
|
|
|
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(), 3ull, "ScatterND: require three inputs.");
|
|
|
|
size_t r = inputs[0].size(), q = inputs[1].size(), p = inputs[2].size(), k = inputs[1].back();
|
|
CV_CheckEQ(r + q - inputs[1].back() - 1, p, "ScatterND: updates should have rank of data.dims + indices.dims - indices.size[-1] - 1");
|
|
CV_CheckLE(k, r, "ScatterND: indices.shape[-1] must be less than (or equal to) the rank of input data.");
|
|
|
|
for (int i = 0; i < q - 1; i++) // np.ndindex(indices.shape[-1])
|
|
{
|
|
CV_CheckEQ(inputs[2][i], inputs[1][i], "ScatterND: updates.shape[0 : rank(indices)-1] must equal to indices.shape[0 : rank(indices)-1].");
|
|
}
|
|
for (int i = q - 1, j = k, m = 0; i + m < p; m++)
|
|
{
|
|
CV_CheckEQ(inputs[2][i + m], inputs[0][j + m], "ScatterND: updates.shape[rank(indices)-1 : ] must equal to data[indices.shape[-1] : rank(data)-1].");
|
|
}
|
|
|
|
outputs.assign(1, inputs[0]);
|
|
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());
|
|
|
|
if (inputs_arr.depth() == CV_16F) {
|
|
forward_fallback(inputs_arr, outputs_arr, internals_arr);
|
|
return;
|
|
}
|
|
|
|
std::vector<Mat> inputs, outputs;
|
|
inputs_arr.getMatVector(inputs);
|
|
outputs_arr.getMatVector(outputs);
|
|
|
|
const Mat& data = inputs[0];
|
|
const Mat& indices = inputs[1];
|
|
const Mat& updates = inputs[2];
|
|
Mat& out = outputs[0];
|
|
|
|
typeDispatch(outputs[0].type(), data, indices, updates, out);
|
|
}
|
|
|
|
// NOTE: This impl does not check whether indices have duplicate entries.
|
|
// The last duplicate entry will overwrite the previous.
|
|
template<typename T, typename Functor>
|
|
void forward_impl(const Functor &reduce_operation, const Mat &input_mat, const Mat &indices_mat, const Mat &updates_mat, Mat& output_mat) {
|
|
input_mat.copyTo(output_mat);
|
|
|
|
const auto &input_mat_shape = shape(input_mat);
|
|
std::vector<size_t> input_mat_step(input_mat_shape.size());
|
|
for (int i = 0; i < input_mat.dims; i++) {
|
|
input_mat_step[i] = static_cast<size_t>(input_mat.step.p[i] / sizeof(T));
|
|
}
|
|
|
|
const int indices_mat_ndims = indices_mat.dims;
|
|
const auto &indices_mat_shape = shape(indices_mat);
|
|
|
|
const int updates_mat_ndims = updates_mat.dims;
|
|
const auto &updates_mat_shape = shape(updates_mat);
|
|
|
|
int indices_last_dim = indices_mat_shape[indices_mat_ndims - 1]; // last dim of indices
|
|
|
|
size_t updates_size = 1;
|
|
for (int i = indices_mat_ndims - 1; i < updates_mat_ndims; i++)
|
|
updates_size *= updates_mat_shape[i];
|
|
|
|
auto fn = [&](const Range &r) {
|
|
size_t input_offset = 0,
|
|
indices_offset = r.start * indices_last_dim,
|
|
updates_offset = r.start * updates_size;
|
|
for (int i = r.start; i < r.end; i++) {
|
|
const T* indices = indices_mat.ptr<const T>();
|
|
const T* updates = updates_mat.ptr<const T>();
|
|
T* output = output_mat.ptr<T>();
|
|
|
|
input_offset = 0;
|
|
indices += indices_offset;
|
|
for (int j = 0; j < indices_last_dim; j++) {
|
|
int index = static_cast<int>(*(indices + j));
|
|
index = (index + input_mat_shape[j]) % input_mat_shape[j];
|
|
CV_Assert(index < input_mat_shape[j] && index >= 0);
|
|
input_offset += index * input_mat_step[j];
|
|
}
|
|
|
|
updates += updates_offset;
|
|
output += input_offset;
|
|
for (int j = 0; j < updates_size; j++) {
|
|
output[j] = reduce_operation(output[j], updates[j]);
|
|
}
|
|
|
|
indices_offset += indices_last_dim;
|
|
updates_offset += updates_size;
|
|
}
|
|
};
|
|
|
|
size_t total = (size_t)(indices_mat.total() / indices_last_dim);
|
|
double nstripes = (size_t)total * (indices_last_dim + updates_size) * (1 / 1024.0);
|
|
parallel_for_(Range(0, total), fn, nstripes);
|
|
}
|
|
|
|
template<typename... Args>
|
|
inline void typeDispatch(const int type, Args&&... args)
|
|
{
|
|
switch (type)
|
|
{
|
|
case CV_8U:
|
|
reductionDispatch<uint8_t>(std::forward<Args>(args)...);
|
|
break;
|
|
case CV_32S:
|
|
reductionDispatch<int32_t>(std::forward<Args>(args)...);
|
|
break;
|
|
case CV_32F:
|
|
reductionDispatch<float>(std::forward<Args>(args)...);
|
|
break;
|
|
default:
|
|
CV_Error(cv::Error::BadDepth, "Unsupported type.");
|
|
};
|
|
}
|
|
|
|
template<typename T, typename... Args>
|
|
inline void reductionDispatch(Args&&... args)
|
|
{
|
|
switch (reduction)
|
|
{
|
|
case REDUCTION::NONE:
|
|
{
|
|
auto rd = [](const T& a, const T& b) { return b; }; // a from input data, b from updates
|
|
forward_impl<T>(rd, std::forward<Args>(args)...);
|
|
break;
|
|
}
|
|
case REDUCTION::ADD:
|
|
{
|
|
auto rd = [](const T& a, const T& b) { return a + b; };
|
|
forward_impl<T>(rd, std::forward<Args>(args)...);
|
|
break;
|
|
}
|
|
case REDUCTION::MUL:
|
|
{
|
|
auto rd = [](const T& a, const T& b) { return a * b; };
|
|
forward_impl<T>(rd, std::forward<Args>(args)...);
|
|
break;
|
|
}
|
|
case REDUCTION::MAX:
|
|
{
|
|
auto rd = [](const T& a, const T& b) { return std::max(a, b); };
|
|
forward_impl<T>(rd, std::forward<Args>(args)...);
|
|
break;
|
|
}
|
|
case REDUCTION::MIN:
|
|
{
|
|
auto rd = [](const T& a, const T& b) { return std::min(a, b); };
|
|
forward_impl<T>(rd, std::forward<Args>(args)...);
|
|
break;
|
|
}
|
|
default:
|
|
CV_Error(Error::StsBadArg, "Unsupported reduction.");
|
|
};
|
|
}
|
|
};
|
|
|
|
Ptr<ScatterNDLayer> ScatterNDLayer::create(const LayerParams& params)
|
|
{
|
|
return makePtr<ScatterNDLayerImpl>(params);
|
|
}
|
|
|
|
}} // namespace cv::dnn
|