From 2ce4b83e4ba276d94037fe394a7816d45bf99b00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ivan=20Vagin/AI=20Tools=20Lab=20/SRR/Engineer/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 21 Dec 2018 16:07:31 +0300 Subject: [PATCH] [nnc] Caffe2 importer unittest (#2735) Implemented caffe2 importer unittest for unsupported op Signed-off-by: Ivan Vagin --- contrib/nnc/unittests/CMakeLists.txt | 1 + .../nnc/unittests/caffe2_frontend/CMakeLists.txt | 17 +++++++++++++ .../caffe2_frontend/test_data/gen_model.py | 27 +++++++++++++++++++++ .../caffe2_frontend/unsupported_caffe2_model.cpp | 28 ++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 contrib/nnc/unittests/caffe2_frontend/CMakeLists.txt create mode 100755 contrib/nnc/unittests/caffe2_frontend/test_data/gen_model.py create mode 100644 contrib/nnc/unittests/caffe2_frontend/unsupported_caffe2_model.cpp diff --git a/contrib/nnc/unittests/CMakeLists.txt b/contrib/nnc/unittests/CMakeLists.txt index 1294a9f..26c9415 100644 --- a/contrib/nnc/unittests/CMakeLists.txt +++ b/contrib/nnc/unittests/CMakeLists.txt @@ -18,4 +18,5 @@ add_subdirectory(soft_backend) add_subdirectory(acl_backend) add_subdirectory(support) add_subdirectory(caffe_frontend) +add_subdirectory(caffe2_frontend) add_subdirectory(tflite_frontend) diff --git a/contrib/nnc/unittests/caffe2_frontend/CMakeLists.txt b/contrib/nnc/unittests/caffe2_frontend/CMakeLists.txt new file mode 100644 index 0000000..488a9f7 --- /dev/null +++ b/contrib/nnc/unittests/caffe2_frontend/CMakeLists.txt @@ -0,0 +1,17 @@ +file(GLOB_RECURSE TESTS "*.cpp") + +execute_process( + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/test_data/gen_model.py ${CMAKE_CURRENT_BINARY_DIR} + OUTPUT_VARIABLE outp + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + RESULT_VARIABLE test_create_failed +) + +if (NNC_FRONTEND_CAFFE2_ENABLED AND NOT ${test_create_failed}) + add_definitions(-DTEST_DIR="${CMAKE_CURRENT_BINARY_DIR}") + add_nnc_unit_test(nnc_caffe2_frontend_test ${TESTS} ${OPTIONS_SRC}) + if (TARGET nnc_caffe2_frontend_test) + nncc_target_link_libraries(nnc_caffe2_frontend_test caffe2_importer nnc_support nnc_core) + target_include_directories(nnc_caffe2_frontend_test PRIVATE ${NNC_CAFFE2_FRONTEND_DIR}) + endif() +endif() diff --git a/contrib/nnc/unittests/caffe2_frontend/test_data/gen_model.py b/contrib/nnc/unittests/caffe2_frontend/test_data/gen_model.py new file mode 100755 index 0000000..697a1a7 --- /dev/null +++ b/contrib/nnc/unittests/caffe2_frontend/test_data/gen_model.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 + +import os +import sys + +try: + from caffe2.python import workspace, model_helper + from caffe2.python.predictor import mobile_exporter +except ImportError: + print("!! Caffe2 not installed, caffe2 frontend test not generated", file=sys.stderr) + exit(1) + + +def save_net(init_net_pb, predict_net_pb, model): + init_net, predict_net = mobile_exporter.Export(workspace, model.net, m.params) + with open(predict_net_pb, 'wb') as f: + f.write(model.net._net.SerializeToString()) + with open(init_net_pb, 'wb') as f: + f.write(init_net.SerializeToString()) + + +resDir = sys.argv[1] + +m = model_helper.ModelHelper(name='unsupported_net') +m.net.GivenTensorFill([], 'input_data', values=(1.,), shape=(1,)) +m.net.Sin(['input_data'], 'result') +save_net(os.path.join(resDir, 'init_net.pb'), os.path.join(resDir, 'predict_net.pb'), m) diff --git a/contrib/nnc/unittests/caffe2_frontend/unsupported_caffe2_model.cpp b/contrib/nnc/unittests/caffe2_frontend/unsupported_caffe2_model.cpp new file mode 100644 index 0000000..6985f60 --- /dev/null +++ b/contrib/nnc/unittests/caffe2_frontend/unsupported_caffe2_model.cpp @@ -0,0 +1,28 @@ +#include "caffe2_importer.h" +#include "gtest/gtest.h" +#include "pass/PassException.h" +#include +#include + +const char *ErrorMsg = "Detected problems:\n" + "Sin: unknown layer\n"; + +// When adding support for new layers, change the model, not the test +TEST(CAFFE_IMPORT_UNSUPPORTED, ImportAModelWithUnsupportedLayers) { + + std::string predict_net = std::string(TEST_DIR) + "/predict_net.pb"; + std::string init_net = std::string(TEST_DIR) + "/init_net.pb"; + + nnc::Caffe2Importer importer{predict_net, init_net, {{1}}}; + try { + importer.import(); + importer.createIR(); + } + catch (nnc::PassException &e) { + ASSERT_EQ(std::string(ErrorMsg), e.what()); + importer.cleanup(); + return; + } + + FAIL(); +} -- 2.7.4