2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <arser/arser.h>
24 #include "CircleModel.h"
27 #include <vconone/vconone.h>
29 void print_version(void)
31 std::cout << "tflite2circle version " << vconone::get_string() << std::endl;
32 std::cout << vconone::get_copyright() << std::endl;
35 int entry(int argc, char **argv)
37 arser::Arser arser{"tflite2circle is a Tensorflow lite to circle model converter"};
39 arser.add_argument("--version")
43 .help("Show version information and exit")
44 .exit_with(print_version);
46 arser.add_argument("tflite")
48 .type(arser::DataType::STR)
49 .help("Source tflite file path to convert");
50 arser.add_argument("circle").nargs(1).type(arser::DataType::STR).help("Target circle file path");
54 arser.parse(argc, argv);
56 catch (const std::runtime_error &err)
58 std::cout << err.what() << std::endl;
63 std::string tfl_path = arser.get<std::string>("tflite");
64 std::string circle_path = arser.get<std::string>("circle");
66 tflite2circle::TFLModel tfl_model(tfl_path);
67 if (!tfl_model.is_valid())
69 std::cerr << "ERROR: Failed to load tflite '" << tfl_path << "'" << std::endl;
73 // create flatbuffer builder
74 auto flatbuffer_builder = std::make_unique<flatbuffers::FlatBufferBuilder>(1024);
76 // convert tflite to circle
77 tflite2circle::CircleModel circle_model{flatbuffer_builder, tfl_model};
79 std::ofstream outfile{circle_path, std::ios::binary};
81 outfile.write(circle_model.base(), circle_model.size());
83 // TODO find a better way of error handling
86 std::cerr << "ERROR: Failed to write circle '" << circle_path << "'" << std::endl;