1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <cstddef>
17 #include <string>
18 #include <vector>
19 #include <memory>
20 #include "include/train/accuracy_metrics.h"
21 #include "include/api/metrics/accuracy.h"
22 #include "src/litert/cxx_api/metrics/metrics_impl.h"
23 #include "src/common/log_adapter.h"
24
25 namespace mindspore {
AccuracyMetrics(int accuracy_metrics,const std::vector<int> & input_indexes,const std::vector<int> & output_indexes)26 AccuracyMetrics::AccuracyMetrics(int accuracy_metrics, const std::vector<int> &input_indexes,
27 const std::vector<int> &output_indexes) {
28 metrics_impl_ = new (std::nothrow)
29 MetricsImpl(new (std::nothrow) lite::AccuracyMetrics(accuracy_metrics, input_indexes, output_indexes));
30 if (metrics_impl_ == nullptr) {
31 MS_LOG(ERROR) << "Metrics implement new failed";
32 }
33 }
34
~AccuracyMetrics()35 AccuracyMetrics::~AccuracyMetrics() {
36 if (metrics_impl_ == nullptr) {
37 MS_LOG(ERROR) << "Metrics implement is null.";
38 return;
39 }
40 auto internal_metrics = metrics_impl_->GetInternalMetrics();
41 if (internal_metrics != nullptr) {
42 delete internal_metrics;
43 }
44 delete metrics_impl_;
45 metrics_impl_ = nullptr;
46 }
47
Clear()48 void AccuracyMetrics::Clear() {
49 if (metrics_impl_ == nullptr) {
50 MS_LOG(ERROR) << "Metrics implement is null.";
51 return;
52 }
53 auto internal_metrics = metrics_impl_->GetInternalMetrics();
54 (reinterpret_cast<lite::AccuracyMetrics *>(internal_metrics))->Clear();
55 }
56
Eval()57 float AccuracyMetrics::Eval() {
58 if (metrics_impl_ == nullptr) {
59 MS_LOG(ERROR) << "Metrics implement is null.";
60 return 0.0f;
61 }
62 auto internal_metrics = metrics_impl_->GetInternalMetrics();
63 return (reinterpret_cast<lite::AccuracyMetrics *>(internal_metrics))->Eval();
64 }
65 } // namespace mindspore
66