ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Driver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
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
17#include "CircleEvalDiff.h"
18
19#include <arser/arser.h>
20#include <vconone/vconone.h>
21
22using namespace circle_eval_diff;
23
24namespace
25{
26
27std::string to_lower_case(std::string s)
28{
29 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
30 return s;
31}
32
33InputFormat to_input_format(const std::string &str)
34{
35 auto small_str = to_lower_case(str);
36 if (small_str.compare("h5") == 0)
37 return InputFormat::H5;
38
39 if (small_str.compare("directory") == 0 || small_str.compare("dir") == 0)
40 return InputFormat::DIR;
41
42 throw std::runtime_error("Unsupported input format.");
43}
44
45void print_version(void)
46{
47 std::cout << "circle-eval-diff version " << vconone::get_string() << std::endl;
48 std::cout << vconone::get_copyright() << std::endl;
49}
50
51} // namespace
52
53int entry(const int argc, char **argv)
54{
55 arser::Arser arser("Compare inference results of two circle models");
56
58
59 arser.add_argument("--first_model").required(true).help("First input model filepath");
60
61 arser.add_argument("--second_model").required(true).help("Second input model filepath");
62
63 arser.add_argument("--first_input_data")
64 .help("Input data filepath for the first model. If not given, circle-eval-diff will run with "
65 "randomly generated data");
66
67 arser.add_argument("--second_input_data")
68 .help("Input data filepath for the second model. If not given, circle-eval-diff will run with "
69 "randomly generated data");
70
71 arser.add_argument("--dump_output_with_prefix")
72 .help("Dump output to files. <prefix> should be given as an argument. "
73 "Outputs are saved in <prefix>.<data_index>.first.output<output_index> and "
74 "<prefix>.<data_index>.second.output<output_index>.");
75
76 arser.add_argument("--print_mae").nargs(0).default_value(false).help("Print Mean Absolute Error");
77
78 arser.add_argument("--print_mape")
79 .nargs(0)
80 .default_value(false)
81 .help("Print Mean Absolute PercentageError");
82
83 arser.add_argument("--print_mpeir")
84 .nargs(0)
85 .default_value(false)
86 .help("Print Mean Peak Error to Interval Ratio");
87
88 arser.add_argument("--print_top1_match")
89 .nargs(0)
90 .default_value(false)
91 .help("Print Mean Top-1 Match Ratio");
92
93 arser.add_argument("--print_top5_match")
94 .nargs(0)
95 .default_value(false)
96 .help("Print Mean Top-5 Match Ratio");
97
98 arser.add_argument("--print_mse").nargs(0).default_value(false).help("Print Mean Squared Error");
99
100 arser.add_argument("--input_data_format")
101 .default_value("h5")
102 .help("Input data format. h5/hdf5 (default) or directory");
103
104 try
105 {
106 arser.parse(argc, argv);
107 }
108 catch (const std::runtime_error &err)
109 {
110 std::cout << err.what() << std::endl;
111 std::cout << arser;
112 return 255;
113 }
114
115 const auto first_model_path = arser.get<std::string>("--first_model");
116 const auto second_model_path = arser.get<std::string>("--second_model");
117
118 // Default values
119 std::string first_input_data_path;
120 std::string second_input_data_path;
121 std::string metric;
122 std::string input_data_format;
123 std::string output_prefix;
124
125 if (arser["--first_input_data"])
126 first_input_data_path = arser.get<std::string>("--first_input_data");
127
128 if (arser["--second_input_data"])
129 second_input_data_path = arser.get<std::string>("--second_input_data");
130
131 if (arser["--first_input_data"] != arser["--second_input_data"])
132 throw std::runtime_error("Input data path should be given for both first_model and "
133 "second_model, or neither must be given.");
134
135 if (arser["--dump_output_with_prefix"])
136 output_prefix = arser.get<std::string>("--dump_output_with_prefix");
137
138 // Set Metrics
139 std::vector<Metric> metrics;
140 if (arser["--print_mae"] and arser.get<bool>("--print_mae"))
141 {
142 metrics.emplace_back(Metric::MAE);
143 }
144 if (arser["--print_mape"] and arser.get<bool>("--print_mape"))
145 {
146 metrics.emplace_back(Metric::MAPE);
147 }
148 if (arser["--print_mpeir"] and arser.get<bool>("--print_mpeir"))
149 {
150 metrics.emplace_back(Metric::MPEIR);
151 }
152 if (arser["--print_top1_match"] and arser.get<bool>("--print_top1_match"))
153 {
154 metrics.emplace_back(Metric::MTOP1);
155 }
156 if (arser["--print_top5_match"] and arser.get<bool>("--print_top5_match"))
157 {
158 metrics.emplace_back(Metric::MTOP5);
159 }
160 if (arser["--print_mse"] and arser.get<bool>("--print_mse"))
161 {
162 metrics.emplace_back(Metric::MSE);
163 }
164
165 input_data_format = arser.get<std::string>("--input_data_format");
166
167 auto ctx = std::make_unique<CircleEvalDiff::Context>();
168 {
169 ctx->first_model_path = first_model_path;
170 ctx->second_model_path = second_model_path;
171 ctx->first_input_data_path = first_input_data_path;
172 ctx->second_input_data_path = second_input_data_path;
173 ctx->metric = metrics;
174 ctx->input_format = to_input_format(input_data_format);
175 ctx->output_prefix = output_prefix;
176 }
177
178 CircleEvalDiff ced(std::move(ctx));
179
180 ced.init();
181
182 ced.evalDiff();
183
184 return EXIT_SUCCESS;
185}
void print_version(void)
static void add_version(Arser &arser, const std::function< void(void)> &func)
Definition arser.h:755
int entry(const int argc, char **argv)
Definition Driver.cpp:53
Definition arser.h:39
std::string to_lower_case(std::string s)
Definition Strings.cpp:41
std::string get_copyright(void)
get_copyright will return copyright string
Definition version.cpp:54
std::string get_string(void)
get_string will return string of major.minor.patch (without build)
Definition version.cpp:44