Skip to content

Added new data types to cv::Mat & UMat#23865

Merged
vpisarev merged 15 commits intoopencv:5.xfrom
vpisarev:add_new_data_type_v2
Aug 4, 2023
Merged

Added new data types to cv::Mat & UMat#23865
vpisarev merged 15 commits intoopencv:5.xfrom
vpisarev:add_new_data_type_v2

Conversation

@vpisarev
Copy link
Copy Markdown
Contributor

@vpisarev vpisarev commented Jun 24, 2023

This is updated version of #23591.

The related opencv_contrib PR: opencv/opencv_contrib#3523

  • CV_Bool (boolean), CV_32U, CV_64S, CV_64U and CV_16BF (bfloat16) have been added. Similarly to float16, bfloat16 is always available, not matter if the target architecture supports hardware bf16 ops or not, because conversion to/from FP32 is trivial and super-fast (faster than FP16<=>FP32).
  • In order to preserve compatibility with other cv::Mat flags, we still use 12 bits for type. The 'depth' bit field now takes 5 bits (allowing for 32 different types), therefore 'channels' field has been shrinked from 9 to 7 bits, allowing 128 channels at maximum (down from 512). Don't worry, in deep learning we use 4D tensors, where channels is a dedicated dimensionality, so this limitation should not affect OpenCV DNN module. For image processing and even multi-spectral image processing 128 channels should probably be enough.
  • basic operations: copyTo(), setTo(), convertTo(), RNG::fill(), XML, YAML, Json I/O etc. are supported. Arithmetic operations are not extended yet, but at least they don't crash, they throw OpenCV error.

Let's see how many tests fail.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

#endif
};

class bfloat16_t
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can conflict with C++23 floating-point types: https://en.cppreference.com/w/cpp/types/floating-point , though the same issue can be seen with the float16_t.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the link, I did not know about those new type definitions. Yes, it would be nice to resolve possible name conflicts.

Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to mention related discussions #12584


std::vector<cv::Mat> img_pts_cams(num_cameras);
std::vector<bool> visible(num_cameras, false);
std::vector<uchar> visible(num_cameras, (uchar)0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?
where is it used with OpenCV API?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the change makes the test pass

#define CV_64F 6
#define CV_16F 7
#define CV_16BF 8
#define CV_Bool 9
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all uppercase.

P.S. Put #undef in GAPI header.

Finally we should drop defines and start using of C++ enums.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postpone it till the next patch

CV_16U - 2 bytes
...
*/
#define CV_ELEM_SIZE1(type) ((int)(0x4881228442211ULL >> CV_MAT_DEPTH(type) * 4) & 15)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CV_MAT_DEPTH(type) * 4 => (CV_MAT_DEPTH(type) * 4)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so far there is no warning

#endif

#if (!defined CV_SIMD_64F) || (!CV_SIMD_64F)
typedef struct v_float64 { int dummy; } v_float64;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to drop this hack,

If we don't have type support, then we don't declare type itself.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disagree; v_float64 and the relevant ops should always be available. CV_SIMD_64F and code fragmentation was a bad idea

inline v_int64x4 operator > (const v_int64x4& a, const v_int64x4& b)
{ return v_int64x4(_mm256_cmpgt_epi64(a.val, b.val)); }
inline v_int64x4 operator < (const v_int64x4& a, const v_int64x4& b)
{ return v_int64x4(_mm256_cmpgt_epi64(b.val, a.val)); }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding of new intrinsic functions requires modification of C++ reference code / documentation and adding new tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, sometimes

data += sizeof(int);
break;
case CV_64U:
ptr = fs::itoa(*(uint64_t*) data, buf, 10, false);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What implementation supports uint64_t?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is overload

const int radix = 10;
char* ptr=buffer + 23 /* enough even for 64-bit integers */;
int sign = _signed && _val < 0 ? -1 : 1;
uint64_t val = _signed ? *(uint64_t*)&_val : abs(_val);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it really work? condition is wrong

It is better to have different overloads instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, that was a bug; fixed


Mat m16bfc1, m16bfc3;
m16fc1.convertTo(m16bfc1, CV_16BF);
m16fc3.convertTo(m16bfc3, CV_16BF);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_20279

It is better to write own test function instead of hijacking of existed one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

if (depth > 7) {
// [TODO] temporary hack; remove after rebuilding plugins or add a new
// version of plugins. Convert type from the old one to the new one (5 bits)
type = CV_MAKETYPE((type & 7), (type >> 3) + 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is time to drop hack on the line 148 of this file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, it's a subject or a separate PR; feel free to submit it later

img_pts2.copyTo(image_points_all[1][i]);
}
std::vector<Size> image_sizes (2, imageSize);
Mat visibility_mat = Mat_<bool>::ones(2, numImgs);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asmorkalov Need to cleanup this buggy code from 4.x too.

And look on traits why they doesn't disable that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's a subject or separate PR

@vpisarev vpisarev force-pushed the add_new_data_type_v2 branch from 65fe53e to 9805a62 Compare July 11, 2023 06:44
@vpisarev vpisarev force-pushed the add_new_data_type_v2 branch from 9805a62 to 127fe75 Compare July 11, 2023 06:49
2. restored ==, !=, > and < univ. intrinsics on ARM32/ARM64.
@vpisarev vpisarev merged commit 518486e into opencv:5.x Aug 4, 2023
@fengyuentau
Copy link
Copy Markdown
Member

Will this patch be ported back to 4.x?

@zihaomu
Copy link
Copy Markdown
Member

zihaomu commented Aug 4, 2023

Hi @vpisarev, which is our main development branch now?

@opencv-alalek opencv-alalek mentioned this pull request Sep 11, 2023
@vpisarev vpisarev deleted the add_new_data_type_v2 branch February 10, 2024 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants