Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove copies in image and video implementation #104

Merged
merged 1 commit into from
May 17, 2019

Conversation

luisremis
Copy link
Contributor

Optimized video and image code, avoiding copies of cvmats. This increase performance of video read and several image operations by up to 25%.

@vishakha041
Copy link
Contributor

I don't see new tests for checking the copy parameters. Are our current tests sufficient to make sure we are not losing image content anywhere? Our typical checks involve metadata verification. I guess that concern applies to the old code too but just thought of it now.

@vishakha041
Copy link
Contributor

What are the pros/cons of merging the ImageData/Image classes? That code was reviewed well back when and this change kind of breaks consistency across how video and descriptors were structured. Is that not true?

@luisremis luisremis force-pushed the optimize_image_video_impl branch from ea80b48 to 48bd510 Compare May 10, 2019 19:00
@luisremis
Copy link
Contributor Author

What are the pros/cons of merging the ImageData/Image classes? That code was reviewed well back when and this change kind of breaks consistency across how video and descriptors were structured. Is that not true?

These changes have been moved to another branch and will be part of a future pull request

@luisremis
Copy link
Contributor Author

luisremis commented May 10, 2019

I don't see new tests for checking the copy parameters. Are our current tests sufficient to make sure we are not losing image content anywhere? Our typical checks involve metadata verification. I guess that concern applies to the old code too but just thought of it now.

We have tests for both, the metadata an pixels. We have many tests that check every single pixel of the image and video and compare it with direct OpenCV results. The copies in the images are tests through the video implementation that is using those copy and move constructors.

All tests are passing.

vishakha041
vishakha041 previously approved these changes May 16, 2019
Copy link
Contributor

@vishakha041 vishakha041 left a comment

Choose a reason for hiding this comment

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

Just make sure it is thread safe and remove that all caps TODO comment. Rest looks fine.

void Image::operator=(const Image &img)
{
ImageData *temp = _image;
_image = new ImageData(*img._image);
delete temp;
}


Image::~Image()
{
delete _image;
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed. Is this never null? I think we talked about this but I am not remembering why there is no null check here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is never null.


if ( img._tdb != NULL )
_tdb = new TDBImage(*img._tdb);
else
_tdb = NULL;

int start;
// TODO IS THIS REALLY NEEDED????
Copy link
Contributor

Choose a reason for hiding this comment

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

Its better to not have comments like that. Either you can resolve it or file an issue and remove the comment


_cv_type = cv_img.type();

_cv_img = cv_img.clone();
_cv_img = cv_img; // shallow copy
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably a silly question but is this still thread safe? That would be hard to catch with our tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

none of this is thread safe. these changes do not affect that.

@@ -137,7 +137,7 @@ void VideoData::Write::operator()(VideoData *video)
std::vector<VCL::Image>& frames = video->get_frames();

for (auto& frame : frames) {
outputVideo << frame.get_cvmat();
outputVideo << frame.get_cvmat(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see why the shallow copy would be important here!

@@ -173,14 +185,16 @@ namespace VCL {
* y coordinate, height, width)
* @return A new Image object that is only the requested area
*/
Image get_area(const Rectangle &roi) const;
Image get_area(const Rectangle &roi);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

move this change to the remove layer PR

void Image::operator=(const Image &img)
{
ImageData *temp = _image;
_image = new ImageData(*img._image);
delete temp;
}


Image::~Image()
{
delete _image;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is never null.

* @return An OpenCV Mat
*/
cv::Mat get_cvmat() const;
cv::Mat get_cvmat(bool copy=false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this should be true.

@@ -64,7 +64,8 @@ void ImageData::Read::operator()(ImageData *img)
img->_channels = img->_tdb->get_image_channels();
}
else {
img->copy_cv(cv::imread(_fullpath, cv::IMREAD_ANYCOLOR));
auto img_read = cv::imread(_fullpath, cv::IMREAD_ANYCOLOR);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

cv::Mat

@@ -86,6 +86,8 @@ namespace VCL {
*/
Image(const cv::Mat &cv_img);

Image(cv::Mat &cv_img);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

see if we can use a parameter instead

@@ -381,7 +401,8 @@ void ImageData::operator=(const ImageData &img)
std::shared_ptr<Operation> front = img._operations.front();
if (front->get_type() == OperationType::READ) {
start = 1;
copy_cv(cv::imread(img._image_id, cv::IMREAD_ANYCOLOR));
cv::Mat img_read = cv::imread(img._image_id, cv::IMREAD_ANYCOLOR);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

check if this hurts or not.


_cv_type = cv_img.type();

_cv_img = cv_img.clone();
_cv_img = cv_img; // shallow copy
Copy link
Contributor Author

Choose a reason for hiding this comment

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

none of this is thread safe. these changes do not affect that.

Copy link
Contributor Author

@luisremis luisremis left a comment

Choose a reason for hiding this comment

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

Comments during code review with @philiplantz

@luisremis luisremis force-pushed the optimize_image_video_impl branch 2 times, most recently from d3efb8a to 5184192 Compare May 17, 2019 23:33
@luisremis
Copy link
Contributor Author

All comments applied, all tests passing, noticeable improvement on video performance by avoing copies. Same should be good images that is now avoiding multiple copies. looks good to go!

@luisremis luisremis merged commit daacc52 into develop May 17, 2019
@luisremis luisremis deleted the optimize_image_video_impl branch July 2, 2019 20:35
cwlacewe added a commit to cwlacewe/vdms that referenced this pull request Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants