diff options
Diffstat (limited to 'compiler/tfl-verify')
-rw-r--r-- | compiler/tfl-verify/CMakeLists.txt | 13 | ||||
-rw-r--r-- | compiler/tfl-verify/README.md | 23 | ||||
-rw-r--r-- | compiler/tfl-verify/requires.cmake | 5 | ||||
-rw-r--r-- | compiler/tfl-verify/src/Driver.cpp | 59 | ||||
-rw-r--r-- | compiler/tfl-verify/src/VerifyFlatBuffers.cpp | 36 | ||||
-rw-r--r-- | compiler/tfl-verify/src/VerifyFlatBuffers.h | 32 |
6 files changed, 168 insertions, 0 deletions
diff --git a/compiler/tfl-verify/CMakeLists.txt b/compiler/tfl-verify/CMakeLists.txt new file mode 100644 index 000000000..4421a4660 --- /dev/null +++ b/compiler/tfl-verify/CMakeLists.txt @@ -0,0 +1,13 @@ +if(NOT TARGET mio_tflite) + return() +endif(NOT TARGET mio_tflite) + +file(GLOB_RECURSE SOURCES "src/*.cpp") + +add_executable(tfl-verify ${SOURCES}) +target_include_directories(tfl-verify PRIVATE src) +target_link_libraries(tfl-verify arser) +target_link_libraries(tfl-verify foder) +target_link_libraries(tfl-verify mio_tflite) +target_link_libraries(tfl-verify safemain) +target_link_libraries(tfl-verify cwrap) diff --git a/compiler/tfl-verify/README.md b/compiler/tfl-verify/README.md new file mode 100644 index 000000000..c50016873 --- /dev/null +++ b/compiler/tfl-verify/README.md @@ -0,0 +1,23 @@ +# tfl-verify + +_tfl-verify_ allows users to verify TF Lite models. + +## Usage + +Provide _tflite_ file as a parameter to verify validity. + +``` +$ tfl-verify tflitefile.tflite +``` + +Result for valid file +``` +[ RUN ] Check tflitefile.tflite +[ PASS ] Check tflitefile.tflite +``` + +Result for invalid file +``` +[ RUN ] Check tflitefile.tflite +[ FAIL ] Check tflitefile.tflite +``` diff --git a/compiler/tfl-verify/requires.cmake b/compiler/tfl-verify/requires.cmake new file mode 100644 index 000000000..79503f325 --- /dev/null +++ b/compiler/tfl-verify/requires.cmake @@ -0,0 +1,5 @@ +require("arser") +require("foder") +require("mio-tflite") +require("safemain") +require("cwrap") diff --git a/compiler/tfl-verify/src/Driver.cpp b/compiler/tfl-verify/src/Driver.cpp new file mode 100644 index 000000000..6d1897607 --- /dev/null +++ b/compiler/tfl-verify/src/Driver.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "VerifyFlatBuffers.h" + +#include <arser/arser.h> + +#include <iostream> +#include <memory> +#include <string> + +int entry(int argc, char **argv) +{ + arser::Arser arser; + arser.add_argument("tflite").type(arser::DataType::STR).help("TFLite file path to verify"); + + try + { + arser.parse(argc, argv); + } + catch (const std::runtime_error &err) + { + std::cout << err.what() << std::endl; + std::cout << arser; + return 255; + } + + auto verifier = std::make_unique<VerifyFlatbuffers>(); + + std::string model_file = arser.get<std::string>("tflite"); + + std::cout << "[ RUN ] Check " << model_file << std::endl; + + auto result = verifier->run(model_file); + + if (result == 0) + { + std::cout << "[ PASS ] Check " << model_file << std::endl; + } + else + { + std::cout << "[ FAIL ] Check " << model_file << std::endl; + } + + return result; +} diff --git a/compiler/tfl-verify/src/VerifyFlatBuffers.cpp b/compiler/tfl-verify/src/VerifyFlatBuffers.cpp new file mode 100644 index 000000000..7fb48a71e --- /dev/null +++ b/compiler/tfl-verify/src/VerifyFlatBuffers.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "VerifyFlatBuffers.h" + +#include <foder/FileLoader.h> +#include <mio/tflite/schema_generated.h> + +int VerifyFlatbuffers::run(const std::string &model_file) +{ + foder::FileLoader fileLoader{model_file}; + std::vector<char> modeldata = fileLoader.load(); + + const uint8_t *data = reinterpret_cast<const uint8_t *>(modeldata.data()); + flatbuffers::Verifier verifier{data, modeldata.size()}; + + if (!tflite::VerifyModelBuffer(verifier)) + { + return -1; + } + + return 0; +} diff --git a/compiler/tfl-verify/src/VerifyFlatBuffers.h b/compiler/tfl-verify/src/VerifyFlatBuffers.h new file mode 100644 index 000000000..c301b5b10 --- /dev/null +++ b/compiler/tfl-verify/src/VerifyFlatBuffers.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __VERIFY_FLATBUFFERS_H__ +#define __VERIFY_FLATBUFFERS_H__ + +#include <ostream> +#include <string> + +class VerifyFlatbuffers +{ +public: + VerifyFlatbuffers() = default; + +public: + int run(const std::string &model_file); +}; + +#endif // __VERIFY_FLATBUFFERS_H__ |