-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TensorsToSegmentationCalculator test utilities.
PiperOrigin-RevId: 586817713
- Loading branch information
1 parent
7013b23
commit a0eda45
Showing
5 changed files
with
249 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
mediapipe/calculators/tensor/tensors_to_segmentation_calculator_test_utils.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright 2023 The MediaPipe Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "mediapipe/calculators/tensor/tensors_to_segmentation_calculator_test_utils.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "absl/log/absl_log.h" | ||
#include "absl/strings/substitute.h" | ||
#include "mediapipe/calculators/tensor/tensors_to_segmentation_calculator.pb.h" | ||
#include "mediapipe/framework/calculator.pb.h" | ||
#include "mediapipe/framework/port/parse_text_proto.h" | ||
|
||
namespace mediapipe { | ||
namespace tensors_to_segmentation_utils { | ||
|
||
std::string ActivationTypeToString( | ||
const TensorsToSegmentationCalculatorOptions::Activation& activation) { | ||
switch (activation) { | ||
case TensorsToSegmentationCalculatorOptions::NONE: | ||
return "NONE"; | ||
case TensorsToSegmentationCalculatorOptions::SIGMOID: | ||
return "SIGMOID"; | ||
case TensorsToSegmentationCalculatorOptions::SOFTMAX: | ||
return "SOFTMAX"; | ||
} | ||
ABSL_LOG(FATAL) << "Unknown activation type: " << activation; | ||
return "UNKNOWN"; | ||
} | ||
|
||
std::vector<unsigned char> ArrayFloatToUnsignedChar( | ||
const std::vector<float>& array) { | ||
std::vector<unsigned char> result; | ||
result.reserve(array.size()); | ||
for (int i = 0; i < array.size(); ++i) { | ||
result.push_back(static_cast<unsigned char>(array[i])); | ||
} | ||
return result; | ||
} | ||
|
||
std::vector<float> MakeRedAlphaMatrix(const std::vector<float>& values) { | ||
std::vector<float> result; | ||
result.reserve(values.size() * 4); | ||
for (const float& value : values) { | ||
result.push_back(value); | ||
result.push_back(0); | ||
result.push_back(0); | ||
result.push_back(value); | ||
} | ||
return result; | ||
} | ||
|
||
// For GPU tests, the input tensor needs to be moved to GPU, using | ||
// TensorViewRequestor. After calculation, the output needs to be moved back | ||
// to CPU, using ToImageCalculator. The output is an ImageFrame. | ||
mediapipe::CalculatorGraphConfig CreateGraphConfigForTest( | ||
bool test_gpu, | ||
const TensorsToSegmentationCalculatorOptions::Activation& activation) { | ||
std::string pre_process = R"pb( | ||
node { | ||
calculator: "mediapipe.aimatter.TensorViewRequestor" | ||
input_stream: "TENSORS:tensors" | ||
output_stream: "TENSORS:tensors_gpu" | ||
options { | ||
[mediapipe.aimatter.TensorViewRequestorOptions.ext] { gpu {} } | ||
} | ||
} | ||
)pb"; | ||
std::string post_process = R"pb( | ||
node { | ||
calculator: "FromImageCalculator" | ||
input_stream: "IMAGE:image_as_mask_gpu" | ||
output_stream: "IMAGE_CPU:image_as_mask" | ||
} | ||
)pb"; | ||
return mediapipe::ParseTextProtoOrDie<mediapipe::CalculatorGraphConfig>( | ||
absl::Substitute( | ||
R"pb( | ||
input_stream: "tensors" | ||
input_stream: "size" $0 | ||
node { | ||
calculator: "TensorsToSegmentationCalculator" | ||
input_stream: "TENSORS:tensors$1" | ||
input_stream: "OUTPUT_SIZE:size" | ||
output_stream: "MASK:image_as_mask$2" | ||
options: { | ||
[mediapipe.TensorsToSegmentationCalculatorOptions.ext] { | ||
activation: $3 | ||
gpu_origin: TOP_LEFT | ||
} | ||
} | ||
} $4 | ||
)pb", | ||
test_gpu ? pre_process : "", test_gpu ? "_gpu" : "", | ||
test_gpu ? "_gpu" : "", ActivationTypeToString(activation), | ||
test_gpu ? post_process : "")); | ||
} | ||
} // namespace tensors_to_segmentation_utils | ||
} // namespace mediapipe |
57 changes: 57 additions & 0 deletions
57
mediapipe/calculators/tensor/tensors_to_segmentation_calculator_test_utils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2023 The MediaPipe Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef MEDIAPIPE_CALCULATORS_TENSOR_TENSORS_TO_SEGMENTATION_CALCULATOR_TEST_UTILS_H_ | ||
#define MEDIAPIPE_CALCULATORS_TENSOR_TENSORS_TO_SEGMENTATION_CALCULATOR_TEST_UTILS_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "mediapipe/calculators/tensor/tensors_to_segmentation_calculator.pb.h" | ||
#include "mediapipe/framework/calculator.pb.h" | ||
|
||
namespace mediapipe { | ||
namespace tensors_to_segmentation_utils { | ||
std::string ActivationTypeToString( | ||
const mediapipe::TensorsToSegmentationCalculatorOptions::Activation& | ||
activation); | ||
|
||
std::vector<unsigned char> ArrayFloatToUnsignedChar( | ||
const std::vector<float>& array); | ||
|
||
std::vector<float> MakeRedAlphaMatrix(const std::vector<float>& values); | ||
|
||
mediapipe::CalculatorGraphConfig CreateGraphConfigForTest( | ||
bool test_gpu, | ||
const mediapipe::TensorsToSegmentationCalculatorOptions::Activation& | ||
activation); | ||
|
||
struct FormattingTestCase { | ||
std::string test_name; | ||
std::vector<float> inputs; | ||
std::vector<float> expected_outputs; | ||
mediapipe::TensorsToSegmentationCalculatorOptions::Activation activation; | ||
int rows = 1; | ||
int cols = 1; | ||
int rows_new = 1; | ||
int cols_new = 1; | ||
int channels = 1; | ||
double max_abs_diff = 1e-7; | ||
}; | ||
} // namespace tensors_to_segmentation_utils | ||
} // namespace mediapipe | ||
|
||
#endif // MEDIAPIPE_CALCULATORS_TENSOR_TENSORS_TO_SEGMENTATION_CALCULATOR_TEST_UTILS_H_ |
50 changes: 50 additions & 0 deletions
50
mediapipe/calculators/tensor/tensors_to_segmentation_calculator_test_utils_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 The MediaPipe Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "mediapipe/calculators/tensor/tensors_to_segmentation_calculator_test_utils.h" | ||
|
||
#include <vector> | ||
|
||
#include "mediapipe/calculators/tensor/tensors_to_segmentation_calculator.pb.h" | ||
#include "mediapipe/framework/port/gtest.h" | ||
|
||
namespace mediapipe::tensors_to_segmentation_utils { | ||
namespace { | ||
|
||
using Options = ::mediapipe::TensorsToSegmentationCalculatorOptions; | ||
|
||
TEST(TensorsToSegmentationCalculatorTestUtilsTest, | ||
ActivationTypeToStringWorksCorrectly) { | ||
EXPECT_EQ(ActivationTypeToString(Options::NONE), "NONE"); | ||
EXPECT_EQ(ActivationTypeToString(Options::SIGMOID), "SIGMOID"); | ||
EXPECT_EQ(ActivationTypeToString(Options::SOFTMAX), "SOFTMAX"); | ||
} | ||
|
||
TEST(TensorsToSegmentationCalculatorTestUtilsTest, | ||
ArrayFloatToUnsignedCharWorksCorrectly) { | ||
std::vector<float> input = {1.0, 2.0, 3.0}; | ||
std::vector<unsigned char> expected = {1, 2, 3}; | ||
EXPECT_EQ(ArrayFloatToUnsignedChar(input), expected); | ||
} | ||
|
||
TEST(TensorsToSegmentationCalculatorTestUtilsTest, | ||
MakeRedAlphaMatrixWorksCorrectly) { | ||
std::vector<float> input = {1.0, 2.0, 3.0}; | ||
std::vector<float> expected = {1.0, 0.0, 0.0, 1.0, 2.0, 0.0, | ||
0.0, 2.0, 3.0, 0.0, 0.0, 3.0}; | ||
EXPECT_EQ(MakeRedAlphaMatrix(input), expected); | ||
} | ||
|
||
} // namespace | ||
} // namespace mediapipe::tensors_to_segmentation_utils |