|
1 | 1 | #include "mediapipe/framework/api2/builder.h"
|
2 | 2 |
|
| 3 | +#include <utility> |
3 | 4 | #include <vector>
|
4 | 5 |
|
5 | 6 | #include "absl/strings/string_view.h"
|
@@ -256,6 +257,150 @@ TEST(BuilderTest, BuildGraphSettingSourceLayer) {
|
256 | 257 | EXPECT_THAT(graph.GetConfig(), EqualsProto(expected));
|
257 | 258 | }
|
258 | 259 |
|
| 260 | +TEST(BuilderTest, CanUseBackEdges) { |
| 261 | + Graph graph; |
| 262 | + // Graph inputs. |
| 263 | + Stream<AnyType> image = graph.In("IMAGE").SetName("image"); |
| 264 | + |
| 265 | + auto [prev_detections, set_prev_detections_fn] = [&]() { |
| 266 | + auto* loopback_node = &graph.AddNode("PreviousLoopbackCalculator"); |
| 267 | + image >> loopback_node->In("MAIN"); |
| 268 | + auto set_loop_fn = [loopback_node](Stream<AnyType> loop) { |
| 269 | + loop >> loopback_node->In("LOOP").AsBackEdge(); |
| 270 | + }; |
| 271 | + Stream<AnyType> prev_loop = loopback_node->Out("PREV_LOOP"); |
| 272 | + return std::pair(prev_loop, set_loop_fn); |
| 273 | + }(); |
| 274 | + |
| 275 | + Stream<AnyType> detections = [&]() { |
| 276 | + auto& detection_node = graph.AddNode("ObjectDetectionCalculator"); |
| 277 | + image >> detection_node.In("IMAGE"); |
| 278 | + prev_detections >> detection_node.In("PREV_DETECTIONS"); |
| 279 | + return detection_node.Out("DETECTIONS"); |
| 280 | + }(); |
| 281 | + |
| 282 | + set_prev_detections_fn(detections); |
| 283 | + |
| 284 | + // Graph outputs. |
| 285 | + detections.SetName("detections") >> graph.Out("OUT"); |
| 286 | + |
| 287 | + CalculatorGraphConfig expected = |
| 288 | + mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( |
| 289 | + node { |
| 290 | + calculator: "PreviousLoopbackCalculator" |
| 291 | + input_stream: "LOOP:detections" |
| 292 | + input_stream: "MAIN:image" |
| 293 | + output_stream: "PREV_LOOP:__stream_0" |
| 294 | + input_stream_info { tag_index: "LOOP" back_edge: true } |
| 295 | + } |
| 296 | + node { |
| 297 | + calculator: "ObjectDetectionCalculator" |
| 298 | + input_stream: "IMAGE:image" |
| 299 | + input_stream: "PREV_DETECTIONS:__stream_0" |
| 300 | + output_stream: "DETECTIONS:detections" |
| 301 | + } |
| 302 | + input_stream: "IMAGE:image" |
| 303 | + output_stream: "OUT:detections" |
| 304 | + )pb"); |
| 305 | + EXPECT_THAT(graph.GetConfig(), EqualsProto(expected)); |
| 306 | +} |
| 307 | + |
| 308 | +TEST(BuilderTest, CanUseBackEdgesWithIndex) { |
| 309 | + Graph graph; |
| 310 | + // Graph inputs. |
| 311 | + Stream<AnyType> image = graph.In("IN").SetName("in_data"); |
| 312 | + |
| 313 | + auto [processed_data, set_back_edge_fn] = [&]() { |
| 314 | + auto* back_edge_node = &graph.AddNode("SomeBackEdgeCalculator"); |
| 315 | + image >> back_edge_node->In("DATA")[0]; |
| 316 | + auto set_back_edge_fn = [back_edge_node](Stream<AnyType> loop) { |
| 317 | + loop >> back_edge_node->In("DATA")[1].AsBackEdge(); |
| 318 | + }; |
| 319 | + Stream<AnyType> processed_data = back_edge_node->Out("PROCESSED_DATA"); |
| 320 | + return std::pair(processed_data, set_back_edge_fn); |
| 321 | + }(); |
| 322 | + |
| 323 | + Stream<AnyType> output_data = [&]() { |
| 324 | + auto& detection_node = graph.AddNode("SomeOutputDataCalculator"); |
| 325 | + image >> detection_node.In("IMAGE"); |
| 326 | + processed_data >> detection_node.In("PROCESSED_DATA"); |
| 327 | + return detection_node.Out("OUTPUT_DATA"); |
| 328 | + }(); |
| 329 | + |
| 330 | + set_back_edge_fn(output_data); |
| 331 | + |
| 332 | + // Graph outputs. |
| 333 | + output_data.SetName("out_data") >> graph.Out("OUT"); |
| 334 | + |
| 335 | + CalculatorGraphConfig expected = |
| 336 | + mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( |
| 337 | + node { |
| 338 | + calculator: "SomeBackEdgeCalculator" |
| 339 | + input_stream: "DATA:0:in_data" |
| 340 | + input_stream: "DATA:1:out_data" |
| 341 | + output_stream: "PROCESSED_DATA:__stream_0" |
| 342 | + input_stream_info { tag_index: "DATA:1" back_edge: true } |
| 343 | + } |
| 344 | + node { |
| 345 | + calculator: "SomeOutputDataCalculator" |
| 346 | + input_stream: "IMAGE:in_data" |
| 347 | + input_stream: "PROCESSED_DATA:__stream_0" |
| 348 | + output_stream: "OUTPUT_DATA:out_data" |
| 349 | + } |
| 350 | + input_stream: "IN:in_data" |
| 351 | + output_stream: "OUT:out_data" |
| 352 | + )pb"); |
| 353 | + EXPECT_THAT(graph.GetConfig(), EqualsProto(expected)); |
| 354 | +} |
| 355 | + |
| 356 | +TEST(BuilderTest, CanUseBackEdgesWithIndexAndNoTag) { |
| 357 | + Graph graph; |
| 358 | + // Graph inputs. |
| 359 | + Stream<AnyType> image = graph.In("IN").SetName("in_data"); |
| 360 | + |
| 361 | + auto [processed_data, set_back_edge_fn] = [&]() { |
| 362 | + auto* back_edge_node = &graph.AddNode("SomeBackEdgeCalculator"); |
| 363 | + image >> back_edge_node->In(0); |
| 364 | + auto set_back_edge_fn = [back_edge_node](Stream<AnyType> loop) { |
| 365 | + loop >> back_edge_node->In(1).AsBackEdge(); |
| 366 | + }; |
| 367 | + Stream<AnyType> processed_data = back_edge_node->Out("PROCESSED_DATA"); |
| 368 | + return std::pair(processed_data, set_back_edge_fn); |
| 369 | + }(); |
| 370 | + |
| 371 | + Stream<AnyType> output_data = [&]() { |
| 372 | + auto& detection_node = graph.AddNode("SomeOutputDataCalculator"); |
| 373 | + image >> detection_node.In("IMAGE"); |
| 374 | + processed_data >> detection_node.In("PROCESSED_DATA"); |
| 375 | + return detection_node.Out("OUTPUT_DATA"); |
| 376 | + }(); |
| 377 | + |
| 378 | + set_back_edge_fn(output_data); |
| 379 | + |
| 380 | + // Graph outputs. |
| 381 | + output_data.SetName("out_data") >> graph.Out("OUT"); |
| 382 | + |
| 383 | + CalculatorGraphConfig expected = |
| 384 | + mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb( |
| 385 | + node { |
| 386 | + calculator: "SomeBackEdgeCalculator" |
| 387 | + input_stream: "in_data" |
| 388 | + input_stream: "out_data" |
| 389 | + output_stream: "PROCESSED_DATA:__stream_0" |
| 390 | + input_stream_info { tag_index: ":1" back_edge: true } |
| 391 | + } |
| 392 | + node { |
| 393 | + calculator: "SomeOutputDataCalculator" |
| 394 | + input_stream: "IMAGE:in_data" |
| 395 | + input_stream: "PROCESSED_DATA:__stream_0" |
| 396 | + output_stream: "OUTPUT_DATA:out_data" |
| 397 | + } |
| 398 | + input_stream: "IN:in_data" |
| 399 | + output_stream: "OUT:out_data" |
| 400 | + )pb"); |
| 401 | + EXPECT_THAT(graph.GetConfig(), EqualsProto(expected)); |
| 402 | +} |
| 403 | + |
259 | 404 | TEST(BuilderTest, CopyableStream) {
|
260 | 405 | Graph graph;
|
261 | 406 | Stream<int> a = graph.In("A").SetName("a").Cast<int>();
|
|
0 commit comments