From c66b7e83f03b660657090582abb7cf3af3d7a725 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Tue, 14 Feb 2023 10:41:01 -0800 Subject: [PATCH] ssd anchors calculator add fixed anchors. PiperOrigin-RevId: 509574814 --- mediapipe/calculators/tflite/BUILD | 1 + .../tflite/ssd_anchors_calculator.cc | 15 +++++++++++++ .../tflite/ssd_anchors_calculator.proto | 21 ++++++++++++------- .../framework/formats/object_detection/BUILD | 10 ++------- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/mediapipe/calculators/tflite/BUILD b/mediapipe/calculators/tflite/BUILD index db2a27630..94d4f40fe 100644 --- a/mediapipe/calculators/tflite/BUILD +++ b/mediapipe/calculators/tflite/BUILD @@ -26,6 +26,7 @@ mediapipe_proto_library( deps = [ "//mediapipe/framework:calculator_options_proto", "//mediapipe/framework:calculator_proto", + "//mediapipe/framework/formats/object_detection:anchor_proto", ], ) diff --git a/mediapipe/calculators/tflite/ssd_anchors_calculator.cc b/mediapipe/calculators/tflite/ssd_anchors_calculator.cc index 1d8f6e3ea..b4829c449 100644 --- a/mediapipe/calculators/tflite/ssd_anchors_calculator.cc +++ b/mediapipe/calculators/tflite/ssd_anchors_calculator.cc @@ -162,6 +162,21 @@ class SsdAnchorsCalculator : public CalculatorBase { cc->Options(); auto anchors = absl::make_unique>(); + if (!options.fixed_anchors().empty()) { + // Check fields for generating anchors are not set. + if (options.has_input_size_height() || options.has_input_size_width() || + options.has_min_scale() || options.has_max_scale() || + options.has_num_layers() || options.multiscale_anchor_generation()) { + return absl::InvalidArgumentError( + "Fixed anchors are provided, but fields are set for generating " + "anchors. When fixed anchors are set, fields for generating " + "anchors must not be set."); + } + anchors->assign(options.fixed_anchors().begin(), + options.fixed_anchors().end()); + cc->OutputSidePackets().Index(0).Set(Adopt(anchors.release())); + return absl::OkStatus(); + } MP_RETURN_IF_ERROR(GenerateAnchors(anchors.get(), options)); cc->OutputSidePackets().Index(0).Set(Adopt(anchors.release())); return absl::OkStatus(); diff --git a/mediapipe/calculators/tflite/ssd_anchors_calculator.proto b/mediapipe/calculators/tflite/ssd_anchors_calculator.proto index 3b1e36700..0776d7886 100644 --- a/mediapipe/calculators/tflite/ssd_anchors_calculator.proto +++ b/mediapipe/calculators/tflite/ssd_anchors_calculator.proto @@ -17,6 +17,7 @@ syntax = "proto2"; package mediapipe; import "mediapipe/framework/calculator.proto"; +import "mediapipe/framework/formats/object_detection/anchor.proto"; // Options to generate anchors for SSD object detection models. message SsdAnchorsCalculatorOptions { @@ -24,20 +25,22 @@ message SsdAnchorsCalculatorOptions { optional SsdAnchorsCalculatorOptions ext = 247258239; } // Size of input images. - optional int32 input_size_width = 1; // required - optional int32 input_size_height = 2; // required + optional int32 input_size_width = 1; // required for generating anchors. + optional int32 input_size_height = 2; // required for generating anchros. // Min and max scales for generating anchor boxes on feature maps. - optional float min_scale = 3; // required - optional float max_scale = 4; // required + optional float min_scale = 3; // required for generating anchors. + optional float max_scale = 4; // required for generating anchors. // The offset for the center of anchors. The value is in the scale of stride. // E.g. 0.5 meaning 0.5 * |current_stride| in pixels. - optional float anchor_offset_x = 5 [default = 0.5]; // required - optional float anchor_offset_y = 6 [default = 0.5]; // required + optional float anchor_offset_x = 5 + [default = 0.5]; // required for generating anchors. + optional float anchor_offset_y = 6 + [default = 0.5]; // required for generating anchors. // Number of output feature maps to generate the anchors on. - optional int32 num_layers = 7; // required + optional int32 num_layers = 7; // required for generating anchors. // Sizes of output feature maps to create anchors. Either feature_map size or // stride should be provided. repeated int32 feature_map_width = 8; @@ -86,4 +89,8 @@ message SsdAnchorsCalculatorOptions { // Whether to produce anchors in normalized coordinates. // for multiscale_anchor_generation only! optional bool normalize_coordinates = 20 [default = true]; + + // Fixed list of anchors. If set, all the other options to generate anchors + // are ignored. + repeated Anchor fixed_anchors = 21; } diff --git a/mediapipe/framework/formats/object_detection/BUILD b/mediapipe/framework/formats/object_detection/BUILD index 35292e1cc..703d5d774 100644 --- a/mediapipe/framework/formats/object_detection/BUILD +++ b/mediapipe/framework/formats/object_detection/BUILD @@ -15,19 +15,13 @@ # Description: # Working with dense optical flow in mediapipe. -load("//mediapipe/framework/port:build_config.bzl", "mediapipe_cc_proto_library") +load("//mediapipe/framework/port:build_config.bzl", "mediapipe_proto_library") licenses(["notice"]) package(default_visibility = ["//visibility:public"]) -proto_library( +mediapipe_proto_library( name = "anchor_proto", srcs = ["anchor.proto"], ) - -mediapipe_cc_proto_library( - name = "anchor_cc_proto", - srcs = ["anchor.proto"], - deps = [":anchor_proto"], -)