1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/segmentation_platform/public/result.h"
#include <sstream>
#include <string_view>
namespace segmentation_platform {
namespace {
std::string StatusToString(PredictionStatus status) {
switch (status) {
case PredictionStatus::kNotReady:
return "Not ready";
case PredictionStatus::kFailed:
return "Failed";
case PredictionStatus::kSucceeded:
return "Succeeded";
}
}
} // namespace
ClassificationResult::ClassificationResult(PredictionStatus status)
: status(status) {}
ClassificationResult::~ClassificationResult() = default;
ClassificationResult::ClassificationResult(const ClassificationResult&) =
default;
ClassificationResult& ClassificationResult::operator=(
const ClassificationResult&) = default;
std::string ClassificationResult::ToDebugString() const {
std::stringstream debug_string;
debug_string << "Status: " << StatusToString(status);
for (unsigned i = 0; i < ordered_labels.size(); ++i) {
debug_string << " output " << i << ": " << ordered_labels.at(i);
}
return debug_string.str();
}
AnnotatedNumericResult::AnnotatedNumericResult(PredictionStatus status)
: status(status) {}
AnnotatedNumericResult::~AnnotatedNumericResult() = default;
AnnotatedNumericResult::AnnotatedNumericResult(const AnnotatedNumericResult&) =
default;
AnnotatedNumericResult& AnnotatedNumericResult::operator=(
const AnnotatedNumericResult&) = default;
std::optional<float> AnnotatedNumericResult::GetResultForLabel(
std::string_view label) const {
if (status != PredictionStatus::kSucceeded) {
return std::nullopt;
}
if (result.output_config().predictor().has_generic_predictor()) {
const auto& labels =
result.output_config().predictor().generic_predictor().output_labels();
DCHECK_EQ(result.result_size(), labels.size());
for (int index = 0; index < labels.size(); ++index) {
if (labels.at(index) == label) {
return result.result().at(index);
}
}
} else if (result.output_config().predictor().has_multi_class_classifier()) {
const auto& labels = result.output_config()
.predictor()
.multi_class_classifier()
.class_labels();
DCHECK_EQ(result.result_size(), labels.size());
for (int index = 0; index < labels.size(); ++index) {
if (labels.at(index) == label) {
return result.result().at(index);
}
}
}
return std::nullopt;
}
base::flat_map<std::string, float> AnnotatedNumericResult::GetAllResults()
const {
base::flat_map<std::string, float> all_results;
if (result.output_config().predictor().has_generic_predictor()) {
const auto& labels =
result.output_config().predictor().generic_predictor().output_labels();
for (int index = 0; index < labels.size(); ++index) {
all_results[labels.at(index)] = result.result().at(index);
}
} else if (result.output_config().predictor().has_multi_class_classifier()) {
const auto& labels = result.output_config()
.predictor()
.multi_class_classifier()
.class_labels();
for (int index = 0; index < labels.size(); ++index) {
all_results[labels.at(index)] = result.result().at(index);
}
}
return all_results;
}
std::string AnnotatedNumericResult::ToDebugString() const {
std::stringstream debug_string;
debug_string << "Status: " << StatusToString(status);
for (int i = 0; i < result.result_size(); ++i) {
const std::string* label = nullptr;
if (result.output_config().predictor().has_multi_class_classifier()) {
label = &result.output_config()
.predictor()
.multi_class_classifier()
.class_labels(i);
} else if (result.output_config().predictor().has_generic_predictor()) {
label =
&result.output_config().predictor().generic_predictor().output_labels(
i);
}
debug_string << " output " << (label ? *label : "") << ": "
<< result.result(i);
}
return debug_string.str();
}
} // namespace segmentation_platform
|