alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | #---------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | #---------------------------------------------------------------------------- |
| 17 | |
| 18 | # If the path to a directory or source file has been defined, |
| 19 | # get the type here (FILEPATH or PATH): |
| 20 | if (DEFINED ${use_case}_FILE_PATH) |
| 21 | get_path_type(${${use_case}_FILE_PATH} PATH_TYPE) |
| 22 | |
| 23 | # Set the default type if path is not a dir or file path (or undefined) |
| 24 | if (NOT ${PATH_TYPE} STREQUAL PATH AND NOT ${PATH_TYPE} STREQUAL FILEPATH) |
| 25 | message(FATAL_ERROR "Invalid ${use_case}_FILE_PATH. It should be a dir or file path.") |
| 26 | endif() |
| 27 | else() |
| 28 | # Default is a directory path |
| 29 | set(PATH_TYPE PATH) |
| 30 | endif() |
| 31 | |
| 32 | message(STATUS "${use_case}_FILE_PATH is of type: ${PATH_TYPE}") |
| 33 | |
| 34 | USER_OPTION(${use_case}_FILE_PATH "Directory with custom WAV input files, or path to a single WAV file, to use in the evaluation application." |
| 35 | ${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/samples/ |
| 36 | ${PATH_TYPE}) |
| 37 | |
| 38 | USER_OPTION(${use_case}_LABELS_TXT_FILE "Labels' txt file for the chosen model." |
| 39 | ${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/labels/labels_wav2letter.txt |
| 40 | FILEPATH) |
| 41 | |
| 42 | USER_OPTION(${use_case}_AUDIO_RATE "Specify the target sampling rate. Default is 16000." |
| 43 | 16000 |
| 44 | STRING) |
| 45 | |
| 46 | USER_OPTION(${use_case}_AUDIO_MONO "Specify if the audio needs to be converted to mono. Default is ON." |
| 47 | ON |
| 48 | BOOL) |
| 49 | |
| 50 | USER_OPTION(${use_case}_AUDIO_OFFSET "Specify the offset to start reading after this time (in seconds). Default is 0." |
| 51 | 0 |
| 52 | STRING) |
| 53 | |
| 54 | USER_OPTION(${use_case}_AUDIO_DURATION "Specify the audio duration to load (in seconds). If set to 0 the entire audio will be processed." |
| 55 | 0 |
| 56 | STRING) |
| 57 | |
| 58 | USER_OPTION(${use_case}_AUDIO_RES_TYPE "Specify re-sampling algorithm to use. By default is 'kaiser_best'." |
| 59 | kaiser_best |
| 60 | STRING) |
| 61 | |
| 62 | USER_OPTION(${use_case}_AUDIO_MIN_SAMPLES "Specify the minimum number of samples to use. By default is 16000, if the audio is shorter will be automatically padded." |
| 63 | 16000 |
| 64 | STRING) |
| 65 | |
| 66 | USER_OPTION(${use_case}_MODEL_SCORE_THRESHOLD "Specify the score threshold [0.0, 1.0) that must be applied to the inference results for a label to be deemed valid." |
| 67 | 0.5 |
| 68 | STRING) |
| 69 | |
| 70 | # Generate input files |
| 71 | generate_audio_code(${${use_case}_FILE_PATH} ${SRC_GEN_DIR} ${INC_GEN_DIR} |
| 72 | ${${use_case}_AUDIO_RATE} |
| 73 | ${${use_case}_AUDIO_MONO} |
| 74 | ${${use_case}_AUDIO_OFFSET} |
| 75 | ${${use_case}_AUDIO_DURATION} |
| 76 | ${${use_case}_AUDIO_RES_TYPE} |
| 77 | ${${use_case}_AUDIO_MIN_SAMPLES}) |
| 78 | |
| 79 | # Generate labels file |
| 80 | set(${use_case}_LABELS_CPP_FILE Labels) |
| 81 | generate_labels_code( |
| 82 | INPUT "${${use_case}_LABELS_TXT_FILE}" |
| 83 | DESTINATION_SRC ${SRC_GEN_DIR} |
| 84 | DESTINATION_HDR ${INC_GEN_DIR} |
| 85 | OUTPUT_FILENAME "${${use_case}_LABELS_CPP_FILE}" |
| 86 | ) |
| 87 | |
| 88 | |
| 89 | USER_OPTION(${use_case}_ACTIVATION_BUF_SZ "Activation buffer size for the chosen model" |
| 90 | 0x00200000 |
| 91 | STRING) |
| 92 | |
| 93 | |
| 94 | # If there is no tflite file pointed to |
| 95 | if (NOT DEFINED ${use_case}_MODEL_TFLITE_PATH) |
| 96 | |
| 97 | set(MODEL_FILENAME wav2letter_int8.tflite) |
| 98 | set(MODEL_RESOURCES_DIR ${DOWNLOAD_DEP_DIR}/${use_case}) |
| 99 | file(MAKE_DIRECTORY ${MODEL_RESOURCES_DIR}) |
| 100 | set(DEFAULT_MODEL_PATH ${MODEL_RESOURCES_DIR}/${MODEL_FILENAME}) |
| 101 | |
| 102 | # Download the default model |
| 103 | set(ZOO_COMMON_SUBPATH "models/speech_recognition/wav2letter/tflite_int8") |
| 104 | set(ZOO_MODEL_SUBPATH "${ZOO_COMMON_SUBPATH}/${MODEL_FILENAME}") |
| 105 | |
| 106 | download_file_from_modelzoo(${ZOO_MODEL_SUBPATH} ${DEFAULT_MODEL_PATH}) |
| 107 | |
| 108 | if (ETHOS_U55_ENABLED) |
| 109 | message(STATUS |
| 110 | "Ethos-U55 is enabled, but the model downloaded is not optimized by vela. " |
| 111 | "To use Ethos-U55 acceleration, optimise the downloaded model and pass it " |
| 112 | "as ${use_case}_MODEL_TFLITE_PATH to the CMake configuration.") |
| 113 | endif() |
| 114 | |
| 115 | # If the target platform is native |
| 116 | if (${TARGET_PLATFORM} STREQUAL native) |
| 117 | |
| 118 | # Download test vectors |
| 119 | set(ZOO_TEST_IFM_SUBPATH "${ZOO_COMMON_SUBPATH}/testing_input/input_2_int8/0.npy") |
| 120 | set(ZOO_TEST_OFM_SUBPATH "${ZOO_COMMON_SUBPATH}/testing_output/Identity_int8/0.npy") |
| 121 | |
| 122 | set(${use_case}_TEST_IFM ${MODEL_RESOURCES_DIR}/ifm0.npy CACHE FILEPATH |
| 123 | "Input test vector for ${use_case}") |
| 124 | set(${use_case}_TEST_OFM ${MODEL_RESOURCES_DIR}/ofm0.npy CACHE FILEPATH |
| 125 | "Input test vector for ${use_case}") |
| 126 | |
| 127 | download_file_from_modelzoo(${ZOO_TEST_IFM_SUBPATH} ${${use_case}_TEST_IFM}) |
| 128 | download_file_from_modelzoo(${ZOO_TEST_OFM_SUBPATH} ${${use_case}_TEST_OFM}) |
| 129 | |
| 130 | set(TEST_SRC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/tests/src) |
| 131 | set(TEST_INC_GEN_DIR ${CMAKE_BINARY_DIR}/generated/${use_case}/tests/include) |
| 132 | file(MAKE_DIRECTORY ${TEST_SRC_GEN_DIR} ${TEST_INC_GEN_DIR}) |
| 133 | |
| 134 | # Generate test data files to be included in x86 tests |
| 135 | generate_test_data_code( |
| 136 | INPUT_DIR "${DOWNLOAD_DEP_DIR}/${use_case}" |
| 137 | DESTINATION_SRC ${TEST_SRC_GEN_DIR} |
| 138 | DESTINATION_HDR ${TEST_INC_GEN_DIR} |
| 139 | USECASE "${use_case}") |
| 140 | endif() |
| 141 | |
| 142 | else() |
| 143 | set(DEFAULT_MODEL_PATH "N/A") |
| 144 | endif() |
| 145 | |
| 146 | set(EXTRA_MODEL_CODE |
| 147 | "/* Model parameters for ${use_case} */" |
| 148 | "extern const int g_FrameLength = 512" |
| 149 | "extern const int g_FrameStride = 160" |
| 150 | "extern const int g_ctxLen = 98" |
| 151 | "extern const float g_ScoreThreshold = ${${use_case}_MODEL_SCORE_THRESHOLD}" |
| 152 | ) |
| 153 | |
| 154 | USER_OPTION(${use_case}_MODEL_TFLITE_PATH "NN models file to be used in the evaluation application. Model files must be in tflite format." |
| 155 | ${DEFAULT_MODEL_PATH} |
| 156 | FILEPATH |
| 157 | ) |
| 158 | |
| 159 | # Generate model file |
| 160 | generate_tflite_code( |
| 161 | MODEL_PATH ${${use_case}_MODEL_TFLITE_PATH} |
| 162 | DESTINATION ${SRC_GEN_DIR} |
| 163 | EXPRESSIONS ${EXTRA_MODEL_CODE} |
| 164 | ) |