欢迎访问宙启技术站
智能推送

目标检测中的ObjectDetection.Protos.Post_Processing_PB2解析

发布时间:2023-12-26 02:58:19

目标检测中的ObjectDetection.Protos.Post_Processing_PB2是一个Protocol Buffers定义的消息类型,用于表示目标检测的后处理操作。

首先,要使用Post_Processing_PB2,我们需要先定义一个Protocol Buffers文件,其中包含了消息类型的定义。下面是一个示例的Protocol Buffers文件定义:

syntax = "proto3";

package object_detection.protos;

import "object_detection/protos/box_coder.proto";
import "object_detection/protos/post_processing.proto";
import import "tensorflow/core/framework/float_list.proto";
import import "tensorflow/core/framework/int64_list.proto";
import import "tensorflow/core/framework/step_stats.proto";
import import "tensorflow/core/framework/tensor.proto";
import import "tensorflow/core/framework/versions.proto";

option optimize_for = SPEED;

message PostProcessingOptions {
  optional bool use_matmul_gather = 1 [default = false];
}

message BatchNonMaxSuppression {
  optional bool use_matmul_gather = 1 [default = false];
  optional int32 max_total_detections = 2 [default = 100];
  optional float score_threshold = 3 [default = 0.05];
  optional float iou_threshold = 4 [default = 0.6];
  optional bool per_class_suppression = 5 [default = true];
  optional bool legacy_per_class_suppression = 6 [default = false];
  optional float pad_to_max_output_size = 7 [default = 0];
}

message PostProcessing {
  optional bool use_matmul_gather = 1 [default = false];
  optional float score_threshold = 2 [default = 0.05];
  optional float iou_threshold = 3 [default = 0.6];
  repeated float scale_values = 4;
  optional int32 max_detections_per_class = 5 [default = 100];
  optional int32 max_total_detections = 6 [deprecated = true];
  optional int32 use_static_shapes = 7 [default = false];
  optional int32 max_classes_per_detection = 8 [default = 1];
  optional bool use_regular_nms = 9 [default = false];
  repeated string nms_regularization_method = 10 [default = "SOFT-NMS"];
  optional int32 nms_scale_value = 11 [default = -1];
  optional bool nms_score_converter = 12 [default = false];
  optional float pad_to_max_output_size = 13 [default = 0];
  optional bool merge_multiple_boxes = 14 [default = false];
  repeated PostProcessingOptions per_class_non_max_suppression_options = 15;
  optional float new_suppression = 16 [default = 0];
  optional bool softmax_multiclass_scores = 17 [default = false];
  optional int32 max_detections_per_image = 18 [default = -1];
  optional int32 scenario = 19 [default = 0];
  optional int32 force_nms_cpu_only = 20 [default = -1];
  optional int32 max_classes_per_image = 21 [default = -1];
  optional int32 use_approximate_boxes = 22 [default = -1];
  optional bool use_cls_confidences_for_matching = 23 [default = false];
  optional float use_max_score_for_matching = 24 [default = -1];
  optional bool use_box_confidences_for_matching = 25 [default = false];
  optional bool use_diagonal = 26 [default = false];
  optional bool retain_relative_bounding_boxes = 27 [default = false];
  optional bool allow_suppressing_groundtruth = 28 [default = false];
}

message LightPostProcessing {
  optional BatchNonMaxSuppression non_max_suppression = 1;
  repeated PostProcessingOptions per_class_non_max_suppression_options = 2;
  optional int32 force_cpu_nms = 3;
}

在使用Post_Processing_PB2之前,我们首先要将这个Protocol Buffers文件编译为相应的Python文件。我们可以使用Protocol Buffers库提供的工具来实现这一点。假设上述Protocol Buffers文件名为post_processing.proto,我们可以使用如下命令来编译:

protoc object_detection/protos/post_processing.proto --python_out=.

上述命令将生成一个名为post_processing_pb2.py的Python文件,其中定义了Post_Processing_PB2消息类型。

现在我们可以使用Post_Processing_PB2进行目标检测的后处理操作了。下面是一个简单的示例代码:

import post_processing_pb2

def parse_post_processing_options():
    options = post_processing_pb2.PostProcessingOptions()
    options.use_matmul_gather = True
    return options

def parse_batch_non_max_suppression():
    batch_nms = post_processing_pb2.BatchNonMaxSuppression()
    batch_nms.max_total_detections = 100
    batch_nms.score_threshold = 0.05
    batch_nms.iou_threshold = 0.6
    batch_nms.per_class_suppression = True
    return batch_nms

def parse_post_processing():
    post_processing = post_processing_pb2.PostProcessing()
    post_processing.use_matmul_gather = True
    post_processing.score_threshold = 0.05
    post_processing.iou_threshold = 0.6
    post_processing.max_detections_per_class = 100
    post_processing.merge_multiple_boxes = True
    post_processing.per_class_non_max_suppression_options.extend([parse_post_processing_options()])
    return post_processing

def parse_light_post_processing():
    light_post_processing = post_processing_pb2.LightPostProcessing()
    light_post_processing.non_max_suppression.CopyFrom(parse_batch_non_max_suppression())
    light_post_processing.per_class_non_max_suppression_options.extend([parse_post_processing_options()])
    light_post_processing.force_cpu_nms = 1
    return light_post_processing

if __name__ == '__main__':
    post_processing = parse_post_processing()
    print(post_processing)

上述代码定义了几个辅助函数来构建Post_Processing_PB2消息类型的实例。parse_post_processing_options函数构建一个PostProcessingOptions消息类型的实例;parse_batch_non_max_suppression函数构建一个BatchNonMaxSuppression消息类型的实例;parse_post_processing函数构建一个PostProcessing消息类型的实例;parse_light_post_processing函数构建一个LightPostProcessing消息类型的实例。

在主函数中,我们调用parse_post_processing函数来构建一个PostProcessing消息类型的实例,并打印出来。这样我们就成功使用Post_Processing_PB2进行了目标检测的后处理操作。

总结:

Post_Processing_PB2是目标检测中的一个Protocol Buffers消息类型,用于表示目标检测的后处理操作。要使用Post_Processing_PB2,我们需要先定义一个Protocol Buffers文件,包含了消息类型的定义,并编译为相应的Python文件。然后,我们可以使用Post_Processing_PB2来构建消息类型的实例,并进行后续的操作。以上是一个简单的例子,展示了如何使用Post_Processing_PB2进行目标检测的后处理操作。