mirror of
https://github.com/opencv/opencv.git
synced 2026-09-25 04:09:57 +03: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>
150 lines
5.9 KiB
C++
150 lines
5.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 <opencv2/dnn/shape_utils.hpp>
|
|
|
|
namespace cv { namespace dnn {
|
|
|
|
class ExpandLayerImpl CV_FINAL : public ExpandLayer
|
|
{
|
|
public:
|
|
ExpandLayerImpl(const LayerParams ¶ms) {
|
|
setParamsFrom(params);
|
|
|
|
// shape as param
|
|
CV_CheckTrue(params.has("shape"), "DNN/Expand: shape is required in Expand layer initialization");
|
|
DictValue param_shape = params.get("shape");
|
|
int ndims_shape = param_shape.size();
|
|
CV_CheckGT(ndims_shape, 0, "DNN/Expand: ndims of shape must be > 0");
|
|
target_shape.resize(ndims_shape);
|
|
for (int i = 0; i < ndims_shape; i++) {
|
|
target_shape[i] = param_shape.get<int>(i);
|
|
}
|
|
|
|
// FIXME: remove when 0d/1d mat is available
|
|
const_input_1d = params.get("const_input_1d", false);
|
|
}
|
|
|
|
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_CheckGE(inputs.size(), static_cast<size_t>(1), "DNN/Expand: one input at least");
|
|
CV_CheckLE(inputs.size(), static_cast<size_t>(2), "DNN/Expand: two input at most");
|
|
CV_CheckFalse(target_shape.empty(), "DNN/Expand: shape must known before memory is set");
|
|
|
|
MatShape input_shape = inputs[0]; // 1d tensor is represented as 2d mat, e.g. [3] -> [3, 1]
|
|
if (const_input_1d) {
|
|
input_shape = {inputs[0][0]};
|
|
}
|
|
|
|
auto& moreDimension = input_shape.size() > target_shape.size() ? input_shape : target_shape;
|
|
auto& lessDimension = input_shape.size() <= target_shape.size() ? input_shape : target_shape;
|
|
|
|
/* Example:
|
|
i = 3
|
|
|
|
|
moreDimension: 1 2 3 4 5, assign non-aligned dimensions to output shape
|
|
lessDimension: 1 1 5, when dimension is aligned, check valid dimension (either equal or one of them is 1) and assign bigger one
|
|
|
|
|
j = 0 = i - (moreDimension.size() - lessDimension.size());
|
|
*/
|
|
MatShape outputShape(moreDimension.size(), 1);
|
|
for (int i = 0; i < moreDimension.size(); i++) {
|
|
int d = moreDimension[i];
|
|
int j = i - (moreDimension.size() - lessDimension.size());
|
|
if (j >= 0) {
|
|
if (d == 1 || lessDimension[j] == 1 || // broadcast
|
|
d == lessDimension[j]) { // plain copy
|
|
outputShape[i] = std::max(d, lessDimension[j]);
|
|
} else {
|
|
CV_Error(Error::StsBadSize, cv::format("DNN/Expand: invalid dimension, d (%d) != d (%d)", moreDimension[i], lessDimension[j]));
|
|
}
|
|
} else {
|
|
outputShape[i] = d;
|
|
}
|
|
}
|
|
outputs.assign(1, outputShape);
|
|
return false;
|
|
}
|
|
|
|
virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE {
|
|
std::vector<Mat> inputs;
|
|
inputs_arr.getMatVector(inputs);
|
|
|
|
const auto &input = inputs[0];
|
|
auto input_shape = shape(input);
|
|
if (const_input_1d) {
|
|
input_shape = {input_shape[0]};
|
|
}
|
|
|
|
auto& moreDimension = input_shape.size() > target_shape.size() ? input_shape : target_shape;
|
|
auto& lessDimension = input_shape.size() <= target_shape.size() ? input_shape : target_shape;
|
|
|
|
MatShape final_target_shape(moreDimension.size(), 1);
|
|
for (int i = 0; i < moreDimension.size(); i++) {
|
|
int d = moreDimension[i];
|
|
int j = i - (moreDimension.size() - lessDimension.size());
|
|
if (j >= 0) {
|
|
final_target_shape[i] = std::max(lessDimension[j], d);
|
|
} else {
|
|
final_target_shape[i] = d;
|
|
}
|
|
}
|
|
target_shape.clear();
|
|
target_shape = std::move(final_target_shape);
|
|
}
|
|
|
|
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);
|
|
|
|
int target_shape_total = std::accumulate(target_shape.begin(), target_shape.end(), 1, std::multiplies<int>());
|
|
if (target_shape_total == inputs[0].total()) {
|
|
const char *data = inputs[0].ptr<const char>();
|
|
char *output = outputs[0].ptr<char>();
|
|
int step = target_shape_total * outputs[0].elemSize();
|
|
std::memcpy(output, data, step);
|
|
return;
|
|
}
|
|
|
|
if (const_input_1d) {
|
|
const char *data = inputs[0].ptr<const char>();
|
|
char *output = outputs[0].ptr<char>();
|
|
int step = target_shape.back() * outputs[0].elemSize();
|
|
int total = std::accumulate(target_shape.begin(), target_shape.end() - 1, 1, std::multiplies<int>());
|
|
for (int i = 0; i < total; i++) {
|
|
std::memcpy(output + i * step, data, step);
|
|
}
|
|
} else {
|
|
cv::broadcast(inputs[0], target_shape, outputs[0]);
|
|
}
|
|
}
|
|
|
|
private:
|
|
MatShape target_shape;
|
|
bool const_input_1d;
|
|
};
|
|
|
|
Ptr<ExpandLayer> ExpandLayer::create(const LayerParams ¶ms) {
|
|
return makePtr<ExpandLayerImpl>(params);
|
|
}
|
|
|
|
}} // cv::dnn
|