ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Driver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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 <ONNXImporterImpl.h>
18#include <mir2loco.h>
19#include <exo/TFLExporter.h>
20
21#include <iostream>
22
23namespace
24{
25
26// String decorators?
27std::string quote(const std::string &s) { return "'" + s + "'"; }
28
29std::unique_ptr<mir::Graph> import(const std::string &onnx_path)
30{
31 return mir_onnx::loadModel(onnx_path);
32}
33
34std::unique_ptr<loco::Graph> transform(const std::unique_ptr<mir::Graph> &mir_graph)
35{
36 mir2loco::Transformer transformer;
37 return transformer.transform(mir_graph.get());
38}
39
40void printHelp()
41{
42 std::cout << "Usage: onnx2tflite <mode> <path/to/onnx> <path/to/output>\n"
43 "Modes: -t (text file); -b (binary file)"
44 << std::endl;
45}
46
47} // namespace
48
49// ONNX-to-MIR (mir-onnx-importer)
50// MIR-to-LOCO (mir2loco)
51// LOCO-to-TFLITE (exo-tflite)
52int main(int argc, char **argv)
53{
54 // onnx2tflite <mode> <path/to/onnx> <path/to/output>
55 // modes: -t (text file); -b (binary file)
56 if (argc != 4)
57 {
58 printHelp();
59 exit(1);
60 }
61 std::string mode{argv[1]};
62 std::string onnx_path{argv[2]};
63 std::string tflite_path{argv[3]};
64
65 std::cout << "Import " << quote(onnx_path) << std::endl;
66 std::unique_ptr<mir::Graph> mir_graph;
67 if (mode == "-t")
68 mir_graph = mir_onnx::importModelFromTextFile(onnx_path);
69 else if (mode == "-b")
70 mir_graph = mir_onnx::importModelFromBinaryFile(onnx_path);
71 else
72 {
73 printHelp();
74 exit(1);
75 }
76 std::cout << "Import " << quote(onnx_path) << " - Done" << std::endl;
77
78 auto loco_graph = transform(mir_graph);
79
80 exo::TFLExporter(loco_graph.get()).dumpToFile(tflite_path.c_str());
81
82 return 0;
83}
int main(void)
void dumpToFile(const char *path) const
write to a file
std::unique_ptr< loco::Graph > transform(mir::Graph *mir_graph)
Definition mir2loco.cpp:710
std::unique_ptr< mir::Graph > loadModel(const std::string &filename)
std::unique_ptr< mir::Graph > importModelFromTextFile(const std::string &filename)
std::unique_ptr< mir::Graph > importModelFromBinaryFile(const std::string &filename)