Add CPU tests for TensorsToSegmentationCalculator
PiperOrigin-RevId: 576208735
This commit is contained in:
parent
5b0f1f9ac4
commit
905a18c88c
|
@ -62,12 +62,14 @@ struct FormattingTestCase {
|
|||
Options::Activation activation;
|
||||
int rows;
|
||||
int cols;
|
||||
int rows_new;
|
||||
int cols_new;
|
||||
int channels;
|
||||
double max_abs_diff;
|
||||
};
|
||||
|
||||
using TensorsToSegmentationCalculatorTest = TestWithParam<FormattingTestCase>;
|
||||
|
||||
// Currently only useable for tests with no output resize.
|
||||
TEST_P(TensorsToSegmentationCalculatorTest, ParameterizedTests) {
|
||||
const FormattingTestCase& test_case = GetParam();
|
||||
std::vector<float> inputs = test_case.inputs;
|
||||
|
@ -75,7 +77,10 @@ TEST_P(TensorsToSegmentationCalculatorTest, ParameterizedTests) {
|
|||
Options::Activation activation = test_case.activation;
|
||||
int rows = test_case.rows;
|
||||
int cols = test_case.cols;
|
||||
int rows_new = test_case.rows_new;
|
||||
int cols_new = test_case.cols_new;
|
||||
int channels = test_case.channels;
|
||||
double max_abs_diff = test_case.max_abs_diff;
|
||||
|
||||
std::string string_config = absl::Substitute(
|
||||
R"pb(
|
||||
|
@ -119,28 +124,31 @@ TEST_P(TensorsToSegmentationCalculatorTest, ParameterizedTests) {
|
|||
MP_ASSERT_OK(graph.AddPacketToInputStream(
|
||||
"tensors", mediapipe::Adopt(tensors.release()).At(Timestamp(0))));
|
||||
}
|
||||
// The output size is defined as pair(new_width, new_height).
|
||||
MP_ASSERT_OK(graph.AddPacketToInputStream(
|
||||
"size",
|
||||
mediapipe::Adopt(new std::pair<int, int>(rows, cols)).At(Timestamp(0))));
|
||||
"size", mediapipe::Adopt(new std::pair<int, int>(cols_new, rows_new))
|
||||
.At(Timestamp(0))));
|
||||
MP_ASSERT_OK(graph.WaitUntilIdle());
|
||||
|
||||
ASSERT_THAT(output_packets, SizeIs(1));
|
||||
const Image& image_as_mask = output_packets[0].Get<Image>();
|
||||
std::shared_ptr<cv::Mat> result_mat = formats::MatView(&image_as_mask);
|
||||
EXPECT_EQ(result_mat->rows, rows);
|
||||
EXPECT_EQ(result_mat->cols, cols);
|
||||
EXPECT_EQ(result_mat->channels(), channels);
|
||||
EXPECT_EQ(result_mat->rows, rows_new);
|
||||
EXPECT_EQ(result_mat->cols, cols_new);
|
||||
EXPECT_EQ(result_mat->channels(), 1);
|
||||
|
||||
// Compare the real result with the expected result.
|
||||
cv::Mat expected_result = cv::Mat(
|
||||
rows, cols, CV_32FC1, const_cast<float*>(expected_outputs.data()));
|
||||
cv::Mat expected_result =
|
||||
cv::Mat(rows_new, cols_new, CV_32FC1,
|
||||
const_cast<float*>(expected_outputs.data()));
|
||||
cv::Mat diff;
|
||||
cv::absdiff(*result_mat, expected_result, diff);
|
||||
double max_val;
|
||||
cv::minMaxLoc(diff, nullptr, &max_val);
|
||||
// Expects the maximum absolute pixel-by-pixel difference is less than 1e-5.
|
||||
// This delta is for passthorugh accuracy only.
|
||||
EXPECT_LE(max_val, 1e-5);
|
||||
|
||||
// The max allowable diff between output and expected output varies between
|
||||
// tests.
|
||||
EXPECT_LE(max_val, max_abs_diff);
|
||||
|
||||
MP_ASSERT_OK(graph.CloseInputStream("tensors"));
|
||||
MP_ASSERT_OK(graph.CloseInputStream("size"));
|
||||
|
@ -149,8 +157,8 @@ TEST_P(TensorsToSegmentationCalculatorTest, ParameterizedTests) {
|
|||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
TensorsToSegmentationCalculatorTests, TensorsToSegmentationCalculatorTest,
|
||||
testing::ValuesIn<FormattingTestCase>({
|
||||
{/*test_name=*/"NoActivationAndNoOutputResize",
|
||||
testing::ValuesIn<FormattingTestCase>(
|
||||
{{/*test_name=*/"NoActivationAndNoOutputResize",
|
||||
/*inputs=*/
|
||||
{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
|
||||
14.0, 15.0, 16.0},
|
||||
|
@ -160,8 +168,93 @@ INSTANTIATE_TEST_SUITE_P(
|
|||
/*activation=*/Options::NONE,
|
||||
/*rows=*/4,
|
||||
/*cols=*/4,
|
||||
/*channels=*/1},
|
||||
}),
|
||||
/*rows_new=*/4,
|
||||
/*cols_new=*/4,
|
||||
/*channels=*/1,
|
||||
/*max_abs_diff=*/1e-7},
|
||||
{/*test_name=*/"OutputResizeOnly",
|
||||
/*inputs=*/
|
||||
{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
|
||||
14.0, 15.0, 16.0},
|
||||
/*expected_outputs=*/
|
||||
{1, 1.5, 2.166667, 2.833333, 3.5, 4,
|
||||
3.8, 4.3, 4.966667, 5.633333, 6.3, 6.8,
|
||||
7, 7.5, 8.166667, 8.833333, 9.5, 10,
|
||||
10.2, 10.7, 11.366667, 12.033333, 12.7, 13.2,
|
||||
13, 13.5, 14.166667, 14.833333, 15.5, 16},
|
||||
/*activation=*/Options::NONE,
|
||||
/*rows=*/4,
|
||||
/*cols=*/4,
|
||||
/*rows_new=*/5,
|
||||
/*cols_new=*/6,
|
||||
/*channels=*/1,
|
||||
/*max_abs_diff=*/1e-6},
|
||||
{/*test_name=*/"SigmoidActivationWithNoOutputResize",
|
||||
/*inputs=*/
|
||||
{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
|
||||
14.0, 15.0, 16.0},
|
||||
/*expected_outputs=*/
|
||||
{0.731059, 0.880797, 0.952574, 0.982014, 0.993307, 0.997527, 0.999089,
|
||||
0.999665, 0.999877, 0.999955, 0.999983, 0.999994, 0.999998, 0.999999,
|
||||
1.0, 1.0},
|
||||
/*activation=*/Options::SIGMOID,
|
||||
/*rows=*/4,
|
||||
/*cols=*/4,
|
||||
/*rows_new=*/4,
|
||||
/*cols_new=*/4,
|
||||
/*channels=*/1,
|
||||
/*max_abs_diff=*/1e-6},
|
||||
{/*test_name=*/"SigmoidActivationWithOutputResize",
|
||||
/*inputs=*/
|
||||
{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0,
|
||||
14.0, 15.0, 16.0},
|
||||
/*expected_outputs=*/
|
||||
{0.731059, 0.805928, 0.89276, 0.940611, 0.967294, 0.982014,
|
||||
0.914633, 0.93857, 0.966279, 0.981363, 0.989752, 0.994369,
|
||||
0.996592, 0.997666, 0.998873, 0.999404, 0.999683, 0.999829,
|
||||
0.999913, 0.99994, 0.999971, 0.999985, 0.999992, 0.999996,
|
||||
0.999998, 0.999998, 0.999999, 1.0, 1.0, 1.0},
|
||||
/*activation=*/Options::SIGMOID,
|
||||
/*rows=*/4,
|
||||
/*cols=*/4,
|
||||
/*rows_new=*/5,
|
||||
/*cols_new=*/6,
|
||||
/*channels=*/1,
|
||||
/*max_abs_diff=*/1e-6},
|
||||
{/*test_name=*/"SoftmaxActivationWithNoOutputResize",
|
||||
/*inputs=*/
|
||||
{1.0, 2.0, 4.0, 2.0, 3.0, 5.0, 6.0, 1.5, 7.0, 10.0, 11.0,
|
||||
4.0, 12.0, 15.0, 16.0, 18.5, 19.0, 20.0, 22.0, 23.0, 24.5, 23.4,
|
||||
25.6, 28.3, 29.2, 30.0, 24.6, 29.2, 30.0, 24.9, 31.2, 30.3},
|
||||
/*expected_outputs=*/
|
||||
{0.731059, 0.119203, 0.880797, 0.0109869, 0.952574, 0.000911051,
|
||||
0.952574, 0.924142, 0.731059, 0.731059, 0.24974, 0.937027, 0.689974,
|
||||
0.990048, 0.0060598, 0.28905},
|
||||
/*activation=*/Options::SOFTMAX,
|
||||
/*rows=*/4,
|
||||
/*cols=*/4,
|
||||
/*rows_new=*/4,
|
||||
/*cols_new=*/4,
|
||||
/*channels=*/2,
|
||||
/*max_abs_diff=*/1e-6},
|
||||
{/*test_name=*/"SoftmaxActivationWithOutputResize",
|
||||
/*inputs=*/
|
||||
{1.0, 2.0, 4.0, 2.0, 3.0, 5.0, 6.0, 1.5, 7.0, 10.0, 11.0,
|
||||
4.0, 12.0, 15.0, 16.0, 18.5, 19.0, 20.0, 22.0, 23.0, 24.5, 23.4,
|
||||
25.6, 28.3, 29.2, 30.0, 24.6, 29.2, 30.0, 24.9, 31.2, 30.3},
|
||||
/*expected_outputs=*/
|
||||
{0.731059, 0.425131, 0.246135, 0.753865, 0.445892, 0.0109869,
|
||||
0.886119, 0.461259, 0.185506, 0.781934, 0.790618, 0.650195,
|
||||
0.841816, 0.603901, 0.40518, 0.561962, 0.765871, 0.930584,
|
||||
0.718733, 0.763744, 0.703402, 0.281989, 0.459635, 0.742634,
|
||||
0.689974, 0.840011, 0.82605, 0.170058, 0.147555, 0.28905},
|
||||
/*activation=*/Options::SOFTMAX,
|
||||
/*rows=*/4,
|
||||
/*cols=*/4,
|
||||
/*rows_new=*/5,
|
||||
/*cols_new=*/6,
|
||||
/*channels=*/2,
|
||||
/*max_abs_diff=*/1e-6}}),
|
||||
[](const testing::TestParamInfo<
|
||||
TensorsToSegmentationCalculatorTest::ParamType>& info) {
|
||||
return info.param.test_name;
|
||||
|
|
Loading…
Reference in New Issue
Block a user