Introduce tf2circle model test (#8551)
author박천교/On-Device Lab(SR)/Engineer/삼성전자 <ch.bahk@samsung.com>
Wed, 30 Oct 2019 00:13:53 +0000 (09:13 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 30 Oct 2019 00:13:53 +0000 (09:13 +0900)
This commit introduces tf2circle model test.

Signed-off-by: Cheongyo Bahk <ch.bahk@samsung.com>
compiler/tf2circle-model-test/.gitignore [new file with mode: 0644]
compiler/tf2circle-model-test/CMakeLists.txt [new file with mode: 0644]
compiler/tf2circle-model-test/README.md [new file with mode: 0644]
compiler/tf2circle-model-test/contrib/.gitignore [new file with mode: 0644]
compiler/tf2circle-model-test/requires.cmake [new file with mode: 0644]
compiler/tf2circle-model-test/runner.sh [new file with mode: 0755]

diff --git a/compiler/tf2circle-model-test/.gitignore b/compiler/tf2circle-model-test/.gitignore
new file mode 100644 (file)
index 0000000..23c7c1b
--- /dev/null
@@ -0,0 +1 @@
+/contrib.lst
diff --git a/compiler/tf2circle-model-test/CMakeLists.txt b/compiler/tf2circle-model-test/CMakeLists.txt
new file mode 100644 (file)
index 0000000..2fb8223
--- /dev/null
@@ -0,0 +1,110 @@
+nnas_include(TargetRequire)
+
+unset(REQUIRED_TARGETS)
+list(APPEND REQUIRED_TARGETS tf2circle)
+list(APPEND REQUIRED_TARGETS tfkit)
+TargetRequire_Return(${REQUIRED_TARGETS})
+
+unset(KEYS)
+unset(DEPS)
+
+###
+### Add "Contrib" tests
+###
+macro(Add PREFIX)
+  # Let's use CONTRIB prefix to avoid name conflicts with official models
+  set(TEST_KEY "CONTRIB.${PREFIX}")
+
+  set(PACKAGE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/contrib/${PREFIX}")
+
+  set(MODEL_DOWNLOAD_SCRIPT "${PACKAGE_DIR}/model.download")
+  set(MODEL_PB_FILE "${PACKAGE_DIR}/model.pb")
+  set(MODEL_INFO_FILE "${PACKAGE_DIR}/model.info")
+  set(MODEL_MD5SUM_FILE "${PACKAGE_DIR}/model.md5sum")
+
+  # Try to download a model if it is missing
+  if(NOT EXISTS "${MODEL_PB_FILE}")
+    # TODO Extract this routine as a helper function
+    if(NOT EXISTS "${MODEL_DOWNLOAD_SCRIPT}")
+      message(FATAL_ERROR "${TEST_KEY} - Download script is missing")
+    endif(NOT EXISTS "${MODEL_DOWNLOAD_SCRIPT}")
+
+    execute_process(
+      COMMAND ${CMAKE_COMMAND} -D OUTPUT_PATH=${MODEL_PB_FILE} -P "${MODEL_DOWNLOAD_SCRIPT}"
+      RESULT_VARIABLE EXITCODE
+    )
+
+    if(NOT EXITCODE EQUAL 0)
+      message(FATAL_ERROR "${TEST_KEY} - Download fails")
+    endif(NOT EXITCODE EQUAL 0)
+  endif()
+
+  if(EXISTS "${MODEL_MD5SUM_FILE}")
+    # TODO Extract this routine as a helper function
+    file(STRINGS "${MODEL_MD5SUM_FILE}" EXPECTED_MD5SUM)
+    file(MD5 "${MODEL_PB_FILE}" OBTAINED_MD5SUM)
+
+    if(NOT "${EXPECTED_MD5SUM}" STREQUAL "${OBTAINED_MD5SUM}")
+      message(FATAL_ERROR "${TEST_KEY} - Checksum mismatches")
+    endif()
+  endif()
+
+  # Generate .test file which declares MODEL_PB_PATH and MODEL_INFO_PATH
+  set(TEST_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TEST_KEY}.test")
+
+  add_custom_command(
+    OUTPUT ${TEST_CONFIG_FILE}
+    COMMAND ${CMAKE_COMMAND} -E remove -f ${TEST_CONFIG_FILE}
+    COMMAND ${CMAKE_COMMAND} -E echo 'MODEL_PB_PATH="${MODEL_PB_FILE}"' >> ${TEST_CONFIG_FILE}
+    COMMAND ${CMAKE_COMMAND} -E echo 'MODEL_INFO_PATH="${MODEL_INFO_FILE}"' >> ${TEST_CONFIG_FILE}
+    COMMENT "Generate ${TEST_KEY} configuration"
+  )
+
+  list(APPEND KEYS "${TEST_KEY}")
+  list(APPEND DEPS "${TEST_CONFIG_FILE}")
+endmacro(Add)
+
+include(contrib.lst OPTIONAL)
+
+###
+### Generate toolchain.config
+###
+set(TOOLCHAIN_CONFIG "${CMAKE_CURRENT_BINARY_DIR}/toolchain.config")
+
+add_custom_command(
+  OUTPUT ${TOOLCHAIN_CONFIG}
+  COMMAND ${CMAKE_COMMAND} -E remove -f ${TOOLCHAIN_CONFIG}
+  COMMAND ${CMAKE_COMMAND} -E echo 'TF2CIRCLE_PATH=\"$<TARGET_FILE:tf2circle>\"' >> ${TOOLCHAIN_CONFIG}
+  DEPENDS
+    tf2circle
+  COMMENT "Generate toolchin configuration"
+)
+
+list(APPEND DEPS "${TOOLCHAIN_CONFIG}")
+
+##
+## Generate test runner
+##
+set(TEST_RUNNER_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/runner.sh")
+set(TEST_RUNNER_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/run")
+
+add_custom_command(
+  OUTPUT ${TEST_RUNNER_SCRIPT}
+  COMMAND ${CMAKE_COMMAND} -E copy "${TEST_RUNNER_SOURCE}" "${TEST_RUNNER_SCRIPT}"
+  DEPENDS ${TEST_RUNNER_SOURCE}
+  COMMENT "Generate test runner"
+)
+
+list(APPEND DEPS "${TEST_RUNNER_SCRIPT}")
+
+### Generate dependencies
+add_custom_target(tf2circle_model_test_deps ALL DEPENDS ${DEPS})
+
+# NOTE This target is not built by default
+add_test(
+  NAME tf2circle_model_test
+  COMMAND
+    "${TEST_RUNNER_SCRIPT}"
+    "${TOOLCHAIN_CONFIG}"
+    ${KEYS}
+)
diff --git a/compiler/tf2circle-model-test/README.md b/compiler/tf2circle-model-test/README.md
new file mode 100644 (file)
index 0000000..fb53437
--- /dev/null
@@ -0,0 +1 @@
+# tf2circle-model-test
diff --git a/compiler/tf2circle-model-test/contrib/.gitignore b/compiler/tf2circle-model-test/contrib/.gitignore
new file mode 100644 (file)
index 0000000..968c345
--- /dev/null
@@ -0,0 +1,3 @@
+/*
+# Exclude all except below
+!.gitignore
diff --git a/compiler/tf2circle-model-test/requires.cmake b/compiler/tf2circle-model-test/requires.cmake
new file mode 100644 (file)
index 0000000..b1f8726
--- /dev/null
@@ -0,0 +1,2 @@
+require("tf2circle")
+require("tfkit")
diff --git a/compiler/tf2circle-model-test/runner.sh b/compiler/tf2circle-model-test/runner.sh
new file mode 100755 (executable)
index 0000000..9e3b75c
--- /dev/null
@@ -0,0 +1,83 @@
+#!/bin/bash
+
+WORKDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
+
+# Need at least toolchain.config
+if [[ $# -lt 1 ]]; then
+  echo "USAGE: $0 ..."
+  echo
+  echo "ARGUMENTS:"
+  echo "  [toolchain.config path]"
+  echo "  [Prefix1]"
+  echo "  [Prefix2]"
+  echo "  ..."
+  exit 255
+fi
+
+CONFIG_PATH="$1"; shift
+
+source "${CONFIG_PATH}"
+
+echo "-- Found TF2CIRCLE: ${TF2CIRCLE_PATH}"
+echo "-- Found workdir: ${WORKDIR}"
+
+TESTED=()
+PASSED=()
+FAILED=()
+
+pushd "${WORKDIR}"
+while [[ $# -ne 0 ]]; do
+  PREFIX="$1"; shift
+
+  echo "[ RUN      ] ${PREFIX}"
+
+  TESTED+=("${PREFIX}")
+
+  PASSED_TAG="${PREFIX}.passed"
+
+  rm -f "${PASSED_TAG}"
+
+  cat > "${PREFIX}.log" <(
+    exec 2>&1
+
+    source "${PREFIX}.test"
+
+    echo "-- Use '${MODEL_PB_PATH}' and '${MODEL_INFO_PATH}'"
+
+    # Exit immediately if any command fails
+    set -e
+    # Show commands
+    set -x
+
+    # Generate circle
+    "${TF2CIRCLE_PATH}" \
+      "${MODEL_INFO_PATH}" \
+      "${MODEL_PB_PATH}" \
+      "${WORKDIR}/${PREFIX}.circle"
+
+    if [[ $? -eq 0 ]]; then
+      touch "${PASSED_TAG}"
+    fi
+  )
+
+  if [[ -f "${PASSED_TAG}" ]]; then
+    echo "[       OK ] ${PREFIX}"
+    PASSED+=("$PREFIX")
+  else
+    echo "[      FAIL] ${PREFIX}"
+    FAILED+=("$PREFIX")
+  fi
+done
+popd
+
+if [[ ${#TESTED[@]} -ne ${#PASSED[@]} ]]; then
+  echo "FAILED"
+  for TEST in "${FAILED[@]}"
+  do
+    echo "- ${TEST}"
+  done
+  exit 255
+fi
+
+echo "PASSED"
+exit 0