Skip to content
Snippets Groups Projects
Commit a5f42002 authored by Thibault Allenet's avatar Thibault Allenet
Browse files

Change TensorImpl to store a cv::Mat & update tests accordingly

parent a6a1363d
No related branches found
No related tags found
2 merge requests!10Update backend_opencv with modifications from aidge_core,!4Change tensorimpl opencv `future_std::span<cv::Mat>` to `cv::Mat`
Pipeline #37666 failed
......@@ -28,7 +28,7 @@ namespace Aidge {
class TensorImpl_opencv_ {
public:
virtual const cv::Mat& getCvMat() const = 0;
virtual const cv::Mat& data() const = 0;
virtual void setCvMat(const cv::Mat& mat ) = 0;
};
......@@ -37,8 +37,7 @@ private:
const Tensor &mTensor; // Impl needs to access Tensor information, but is not
// supposed to change it!
cv::Mat* mData;
std::unique_ptr<cv::Mat> mDataOwner;
cv::Mat mData;
public:
static constexpr const char *Backend = "opencv";
......@@ -46,21 +45,18 @@ public:
TensorImpl_opencv() = delete;
TensorImpl_opencv(const Tensor &tensor)
: TensorImpl(Backend), mTensor(tensor)
{
mDataOwner = std::unique_ptr<cv::Mat>(new cv::Mat(0,0,OpenCvType<T>::type));
mData = mDataOwner.get();
}
{}
bool operator==(const TensorImpl &otherImpl) const override final {
// Create iterators for both matrices
cv::MatConstIterator_<T> it1 = mDataOwner->begin<T>();
cv::MatConstIterator_<T> it1 = mData.begin<T>();
const cv::Mat otherData = reinterpret_cast<const TensorImpl_opencv<T> &>(otherImpl).data();
const cv::Mat & otherData = reinterpret_cast<const TensorImpl_opencv<T> &>(otherImpl).data();
cv::MatConstIterator_<T> it2 = otherData.begin<T>();
// Iterate over the elements and compare them
for (; it1 != mDataOwner->end<T>(); ++it1, ++it2) {
for (; it1 != mData.end<T>(); ++it1, ++it2) {
if (*it1 != *it2) {
return false;
}
......@@ -73,11 +69,11 @@ public:
}
// native interface
const cv::Mat data() const { return *mData; }
const cv::Mat & data() const override { return mData; }
std::size_t scalarSize() const override { return sizeof(T); }
std::size_t size() const override { return mData->total() * mData->channels();}
std::size_t size() const override { return mData.total() * mData.channels();}
void setDevice(DeviceIdx_t device) override {
AIDGE_ASSERT(device == 0, "device cannot be != 0 for Opencv backend");
......@@ -164,29 +160,26 @@ public:
void *rawPtr(NbElts_t offset = 0) override {
lazyInit();
return (mData->ptr<T>() + offset);
return (mData.ptr<T>() + offset);
};
const void *rawPtr(NbElts_t offset = 0) const override {
AIDGE_ASSERT(size() >= mTensor.size(), "accessing uninitialized const rawPtr");
return (mData->ptr<T>() + offset);
return (mData.ptr<T>() + offset);
};
void *hostPtr(NbElts_t offset = 0) override {
lazyInit();
return (mData->ptr<T>() + offset);
return (mData.ptr<T>() + offset);
};
const void *hostPtr(NbElts_t offset = 0) const override {
AIDGE_ASSERT(size() >= mTensor.size(), "accessing uninitialized const hostPtr");
AIDGE_ASSERT(mData->isContinuous(), "CV Matrix not continuous");
return (mData->ptr<T>() + offset);
AIDGE_ASSERT(mData.isContinuous(), "CV Matrix not continuous");
return (mData.ptr<T>() + offset);
};
const cv::Mat& getCvMat() const override { return *mDataOwner.get(); }
void setCvMat(const cv::Mat& mat) override {
mDataOwner.reset(new cv::Mat(std::move(mat)));
mData = mDataOwner.get();}
void setCvMat(const cv::Mat& mat) override {mData=mat;}
virtual ~TensorImpl_opencv() = default;
......@@ -196,11 +189,10 @@ private:
void lazyInit() {
if (size() < mTensor.size()) {
// Need more data, a re-allocation will occur
AIDGE_ASSERT(mData->empty() || mDataOwner != nullptr, "trying to enlarge non-owned data");
AIDGE_ASSERT(mData.empty() , "trying to enlarge non-owned data");
cv::Mat myNewMatrix;
if (mTensor.nbDims() < 3) {
myNewMatrix = cv::Mat(((mTensor.nbDims() > 1) ? static_cast<int>(mTensor.dims()[1])
mData = cv::Mat(((mTensor.nbDims() > 1) ? static_cast<int>(mTensor.dims()[1])
: (mTensor.nbDims() > 0) ? 1
: 0),
(mTensor.nbDims() > 0) ? static_cast<int>(mTensor.dims()[0]) : 0,
......@@ -214,11 +206,8 @@ private:
OpenCvType<T>::type));
}
cv::merge(channels, myNewMatrix);
cv::merge(channels, mData);
}
mDataOwner.reset(new cv::Mat(std::forward<cv::Mat>(myNewMatrix)));
mData = mDataOwner.get();
}
}
......
......@@ -22,7 +22,7 @@ std::shared_ptr<Aidge::Tensor> Aidge::convertCpu(std::shared_ptr<Aidge::Tensor>
// Get the cv::Mat from the tensor backend Opencv
Aidge::TensorImpl_opencv_* tImplOpencv = dynamic_cast<Aidge::TensorImpl_opencv_*>(tensorOpencv->getImpl().get());
cv::Mat dataOpencv = tImplOpencv->getCvMat();
cv::Mat dataOpencv = tImplOpencv->data();
// Convert the cv::Mat into a vector of cv::Mat (vector of channels)
std::vector<cv::Mat> channels;
......
......@@ -28,8 +28,8 @@ TEST_CASE("Stimuli creation", "[Stimuli][OpenCV]") {
// Access the cv::Mat with the tensor
TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor_load->getImpl().get());
REQUIRE(tImpl_opencv->getCvMat().size() == true_mat.size());
REQUIRE(cv::countNonZero(tImpl_opencv->getCvMat() != true_mat) == 0);
REQUIRE(tImpl_opencv->data().size() == true_mat.size());
REQUIRE(cv::countNonZero(tImpl_opencv->data() != true_mat) == 0);
// This time the tensor is already loaded in memory
......@@ -39,8 +39,8 @@ TEST_CASE("Stimuli creation", "[Stimuli][OpenCV]") {
// Access the cv::Mat with the tensor
TensorImpl_opencv_* tImpl_opencv_2 = dynamic_cast<TensorImpl_opencv_*>(tensor_load_2->getImpl().get());
REQUIRE(tImpl_opencv_2->getCvMat().size() == true_mat.size());
REQUIRE(cv::countNonZero(tImpl_opencv_2->getCvMat() != true_mat) == 0);
REQUIRE(tImpl_opencv_2->data().size() == true_mat.size());
REQUIRE(cv::countNonZero(tImpl_opencv_2->data() != true_mat) == 0);
}
}
......@@ -27,8 +27,8 @@ TEST_CASE("StimuliImpl_opencv_imread creation", "[StimuliImpl_opencv_imread][Ope
// Access the cv::Mat with the tensor
TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensor_load->getImpl().get());
REQUIRE(tImpl_opencv->getCvMat().size() == true_mat.size());
REQUIRE(cv::countNonZero(tImpl_opencv->getCvMat() != true_mat) == 0);
REQUIRE(tImpl_opencv->data().size() == true_mat.size());
REQUIRE(cv::countNonZero(tImpl_opencv->data() != true_mat) == 0);
}
}
......@@ -62,11 +62,11 @@ TEST_CASE("Tensor creation opencv", "[Tensor][OpenCV]") {
}
SECTION("OpenCV tensor features") {
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().rows == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().cols == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().dims == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().total() == 4);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->getCvMat().channels() == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().rows == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().cols == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().dims == 2);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().total() == 4);
REQUIRE(static_cast<TensorImpl_opencv<int>*>(x.getImpl().get())->data().channels() == 2);
}
SECTION("Access to array") {
......
......@@ -64,7 +64,7 @@ TEMPLATE_TEST_CASE("Opencv Utils", "[Utils][OpenCV]", signed char, unsigned char
// Check the matrix inside the tensor coorresponds to the matrix
TensorImpl_opencv_* tImpl_opencv = dynamic_cast<TensorImpl_opencv_*>(tensorOcv->getImpl().get());
auto mat_tensor = tImpl_opencv->getCvMat();
auto mat_tensor = tImpl_opencv->data();
REQUIRE(mat_tensor.size() == mat.size());
REQUIRE(cv::countNonZero(mat_tensor != mat) == 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment