Skip to content

Commit

Permalink
[CAPI] support multi_channels for preprocess scale and mean
Browse files Browse the repository at this point in the history
  • Loading branch information
riverlijunjie committed Mar 27, 2024
1 parent fc5da9d commit eb14201
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/bindings/c/include/openvino/c/ov_prepostprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ ov_preprocess_preprocess_steps_resize(ov_preprocess_preprocess_steps_t* preproce
OPENVINO_C_API(ov_status_e)
ov_preprocess_preprocess_steps_scale(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps, float value);

/**
* @brief Add scale preprocess operation. Divide each channel element of input by different specified value.
* @ingroup ov_prepostprocess_c_api
* @param preprocess_input_process_steps A pointer to ov_preprocess_preprocess_steps_t.
* @param values Scaling values array for each channels
* @param value_size Scaling value size
* @return Status code of the operation: OK(0) for success.
*/
OPENVINO_C_API(ov_status_e)
ov_preprocess_preprocess_steps_scale_multi_channels(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps,
const float* values,
const int32_t value_size);

/**
* @brief Add mean preprocess operation. Subtract specified value from each element of input.
* @ingroup ov_prepostprocess_c_api
Expand All @@ -225,6 +238,19 @@ ov_preprocess_preprocess_steps_scale(ov_preprocess_preprocess_steps_t* preproces
OPENVINO_C_API(ov_status_e)
ov_preprocess_preprocess_steps_mean(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps, float value);

/**
* @brief Add mean preprocess operation. Subtract each channel element of input by different specified value.
* @ingroup ov_prepostprocess_c_api
* @param preprocess_input_process_steps A pointer to ov_preprocess_preprocess_steps_t.
* @param values Value array to subtract from each element.
* @param value_size Mean value size
* @return Status code of the operation: OK(0) for success.
*/
OPENVINO_C_API(ov_status_e)
ov_preprocess_preprocess_steps_mean_multi_channels(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps,
const float* values,
const int32_t value_size);

/**
* @brief Crop input tensor between begin and end coordinates.
* @ingroup ov_prepostprocess_c_api
Expand Down
32 changes: 32 additions & 0 deletions src/bindings/c/src/ov_prepostprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ ov_status_e ov_preprocess_preprocess_steps_scale(ov_preprocess_preprocess_steps_
return ov_status_e::OK;
}

OPENVINO_C_API(ov_status_e)
ov_preprocess_preprocess_steps_scale_multi_channels(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps,
const float* values,
const int32_t value_size) {
if (!preprocess_input_process_steps || !values || value_size <= 0) {
return ov_status_e::INVALID_C_PARAM;
}
try {
std::vector<float> scale_vec(values, values + value_size);
preprocess_input_process_steps->object->scale(scale_vec);
}
CATCH_OV_EXCEPTIONS

return ov_status_e::OK;
}

ov_status_e ov_preprocess_preprocess_steps_mean(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps,
float value) {
if (!preprocess_input_process_steps) {
Expand All @@ -180,6 +196,22 @@ ov_status_e ov_preprocess_preprocess_steps_mean(ov_preprocess_preprocess_steps_t
return ov_status_e::OK;
}

OPENVINO_C_API(ov_status_e)
ov_preprocess_preprocess_steps_mean_multi_channels(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps,
const float* values,
const int32_t value_size) {
if (!preprocess_input_process_steps || !values || value_size <= 0) {
return ov_status_e::INVALID_C_PARAM;
}
try {
std::vector<float> mean_vec(values, values + value_size);
preprocess_input_process_steps->object->mean(mean_vec);
}
CATCH_OV_EXCEPTIONS

return ov_status_e::OK;
}

ov_status_e ov_preprocess_preprocess_steps_crop(ov_preprocess_preprocess_steps_t* preprocess_input_process_steps,
int32_t* begin,
int32_t begin_size,
Expand Down
28 changes: 28 additions & 0 deletions src/bindings/c/tests/ov_preprocess_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ TEST_F(ov_preprocess_test, ov_preprocess_preprocess_steps_scale) {
OV_EXPECT_OK(ov_preprocess_preprocess_steps_scale(input_process, 2.0f));
}

TEST_F(ov_preprocess_test, ov_preprocess_preprocess_steps_scale_multi_channels) {
OV_EXPECT_OK(ov_preprocess_prepostprocessor_create(model, &preprocess));
EXPECT_NE(nullptr, preprocess);

OV_EXPECT_OK(ov_preprocess_prepostprocessor_get_input_info_by_index(preprocess, 0, &input_info));
EXPECT_NE(nullptr, input_info);

OV_EXPECT_OK(ov_preprocess_input_info_get_preprocess_steps(input_info, &input_process));
EXPECT_NE(nullptr, input_process);

float values[4] = {2.0f, 2.0f, 2.0f, 2.0f};
OV_EXPECT_OK(ov_preprocess_preprocess_steps_scale_multi_channels(input_process, values, 4));
}

TEST_F(ov_preprocess_test, ov_preprocess_preprocess_steps_mean) {
OV_EXPECT_OK(ov_preprocess_prepostprocessor_create(model, &preprocess));
EXPECT_NE(nullptr, preprocess);
Expand All @@ -145,6 +159,20 @@ TEST_F(ov_preprocess_test, ov_preprocess_preprocess_steps_mean) {
OV_EXPECT_OK(ov_preprocess_preprocess_steps_mean(input_process, 2.0f));
}

TEST_F(ov_preprocess_test, ov_preprocess_preprocess_steps_mean_multi_channels) {
OV_EXPECT_OK(ov_preprocess_prepostprocessor_create(model, &preprocess));
EXPECT_NE(nullptr, preprocess);

OV_EXPECT_OK(ov_preprocess_prepostprocessor_get_input_info_by_index(preprocess, 0, &input_info));
EXPECT_NE(nullptr, input_info);

OV_EXPECT_OK(ov_preprocess_input_info_get_preprocess_steps(input_info, &input_process));
EXPECT_NE(nullptr, input_process);

float values[4] = {2.0f, 2.0f, 2.0f, 2.0f};
OV_EXPECT_OK(ov_preprocess_preprocess_steps_mean_multi_channels(input_process, values, 4));
}

TEST_F(ov_preprocess_test, ov_preprocess_preprocess_steps_crop) {
OV_EXPECT_OK(ov_preprocess_prepostprocessor_create(model, &preprocess));
EXPECT_NE(nullptr, preprocess);
Expand Down

0 comments on commit eb14201

Please sign in to comment.