Skip to content

ImageManipV2 optimization and refactor #1290

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

Open
wants to merge 52 commits into
base: v3_develop
Choose a base branch
from

Conversation

asahtik
Copy link
Contributor

@asahtik asahtik commented Apr 2, 2025

Purpose

Currently ImageManipV2 on RVC4 uses warp affine even for simpler transformations like cropping and scaling. This PR improves the performance by optimizing these use cases which required refactoring in the ImageManipV2 code.

Specification

None / not applicable

Dependencies & Potential Impact

None / not applicable

Deployment Plan

None / not applicable

Testing & Validation

None / not applicable

asahtik added 24 commits March 13, 2025 15:43
… incorrect striding in Camera gray output, refactor ImageManipV2
@asahtik asahtik requested a review from Copilot April 10, 2025 06:56
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 8 out of 11 changed files in this pull request and generated 1 comment.

Files not reviewed (3)
  • cmake/Depthai/DepthaiDeviceRVC4Config.cmake: Language not supported
  • cmake/Depthai/DepthaiDeviceSideConfig.cmake: Language not supported
  • tests/CMakeLists.txt: Language not supported
Comments suppressed due to low confidence (1)

tests/src/ondevice_tests/pipeline/node/image_manip_v2_optimization_test.cpp:169

  • [nitpick] Consider using more descriptive log messages instead of abbreviations like 'FSD' to improve test readability and maintainability.
std::cout << "FSD" << std::endl;

[&](const ImageManipConfigV2& config, const ImgFrame& srcFrame, ImgFrame& dstFrame) {
auto outType = config.outputFrameType == ImgFrame::Type::NONE ? srcFrame.getType() : config.outputFrameType;
[&](std::shared_ptr<Memory>& src, std::shared_ptr<impl::_ImageManipMemory> dst) {
auto srcMem = std::make_shared<impl::_ImageManipMemory>(src->getData());
Copy link
Preview

Copilot AI Apr 10, 2025

Choose a reason for hiding this comment

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

[nitpick] Reusing or caching the wrapper for the source memory instead of creating a new shared pointer on every call might help further optimize performance if this code path is frequently executed.

Copilot uses AI. Check for mistakes.

@SzabolcsGergely SzabolcsGergely requested a review from Copilot April 20, 2025 07:00
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes and refactors ImageManipV2 to improve performance for simpler transformations by reducing reliance on warp affine. Key changes include:

  • Removal of setRunOnHost(true) from one test to align with updated node behavior.
  • Addition of new backend configuration methods (setBackendCPU and setBackendHW) along with property serialization updates.
  • Refactored core image manipulation operations with revised lambda signatures and an added warp backend template parameter.

Reviewed Changes

Copilot reviewed 8 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/src/ondevice_tests/pipeline/node/image_manip_v2_test.cpp Removed redundant runOnHost setting to reflect optimized behavior.
tests/src/ondevice_tests/pipeline/node/image_manip_v2_optimization_test.cpp Added comprehensive tests comparing host and device implementations across various transformation modes.
src/pipeline/node/ImageManipV2.cpp Refactored manipulation logic to include a new warp backend and updated lambda signatures.
include/depthai/properties/ImageManipPropertiesV2.hpp Introduced Backend enum and updated serialization to include the new backend property.
include/depthai/pipeline/node/ImageManipV2.hpp Exposed new backend setter functions in the public API.
bindings/python/src/pipeline/node/ImageManipV2Bindings.cpp Added Python bindings for the newly introduced backend setter methods.
Files not reviewed (3)
  • cmake/Depthai/DepthaiDeviceRVC4Config.cmake: Language not supported
  • cmake/Depthai/DepthaiDeviceSideConfig.cmake: Language not supported
  • tests/CMakeLists.txt: Language not supported
Comments suppressed due to low confidence (2)

src/pipeline/node/ImageManipV2.cpp:13

  • Ensure that the addition of the 'impl::WarpH' template parameter does not introduce unintended behavior in transformations that do not require warp operations. Please verify that each transformation mode is correctly handled with this new backend.
impl::ImageManipOperations<impl::_ImageManipBuffer, impl::_ImageManipMemory, impl::WarpH> manip(properties, logger);

src/pipeline/node/ImageManipV2.cpp:25

  • Review the performance implications of wrapping the source memory in a new shared pointer for every call. Consider reusing buffers or optimizing this memory handling for high-frequency processing scenarios.
auto srcMem = std::make_shared<impl::_ImageManipMemory>(src->getData());

Copy link
Collaborator

@moratom moratom left a comment

Choose a reason for hiding this comment

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

Thanks! Left a few comments/questions.

Comment on lines +37 to +38
.def("setBackendCPU", &ImageManipV2::setBackendCPU, DOC(dai, node, ImageManipV2, setBackendCPU))
.def("setBackendHW", &ImageManipV2::setBackendHW, DOC(dai, node, ImageManipV2, setBackendHW))
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd go with just "setBackend()" and have an enum here.

Comment on lines +76 to +85
/**
* Set CPU as backend preference
*/
ImageManipV2& setBackendCPU();

/**
* Set HW as backend preference
*/
ImageManipV2& setBackendHW();

Copy link
Collaborator

Choose a reason for hiding this comment

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

setBackend(...) for consistency

Comment on lines 13 to 15
enum class Backend : uint8_t { CPU, HW };
enum class PerformanceMode : uint8_t { AUTO, PERFORMANCE, BALANCED, LOW_POWER };

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's document how each of the mode behaves (what's used for different operations, etc).
Yot down the platform differences as well, so it's clear that this doesn't take any effect on RVC2.

Comment on lines +21 to +28
#ifndef DEPTHAI_STRIDE_ALIGNMENT
#define DEPTHAI_STRIDE_ALIGNMENT 128
#endif
#ifndef DEPTHAI_HEIGHT_ALIGNMENT
#define DEPTHAI_HEIGHT_ALIGNMENT 32
#endif
#ifndef DEPTHAI_PLANE_ALIGNMENT
#define DEPTHAI_PLANE_ALIGNMENT 128 * 32
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this mean that even RGB888p/i are strided again?

@@ -29,7 +38,10 @@
#define DEPTHAI_IMAGEMANIPV2_OPENCV 1
#include <opencv2/opencv.hpp>
#endif

#ifdef DEPTHAI_HAVE_FASTCV_SUPPORT
// #define DEPTHAI_IMAGEMANIPV2_FASTCV 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not needed any more?

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