diff options
Diffstat (limited to 'source/use_case/ad')
-rw-r--r-- | source/use_case/ad/include/AdMelSpectrogram.hpp | 97 | ||||
-rw-r--r-- | source/use_case/ad/include/AdModel.hpp | 53 | ||||
-rw-r--r-- | source/use_case/ad/include/AdPostProcessing.hpp | 50 | ||||
-rw-r--r-- | source/use_case/ad/include/MelSpectrogram.hpp | 233 | ||||
-rw-r--r-- | source/use_case/ad/include/UseCaseHandler.hpp | 33 | ||||
-rw-r--r-- | source/use_case/ad/src/AdMelSpectrogram.cc | 90 | ||||
-rw-r--r-- | source/use_case/ad/src/AdModel.cc | 55 | ||||
-rw-r--r-- | source/use_case/ad/src/AdPostProcessing.cc | 116 | ||||
-rw-r--r-- | source/use_case/ad/src/MainLoop.cc | 114 | ||||
-rw-r--r-- | source/use_case/ad/src/MelSpectrogram.cc | 311 | ||||
-rw-r--r-- | source/use_case/ad/src/UseCaseHandler.cc | 422 | ||||
-rw-r--r-- | source/use_case/ad/usecase.cmake | 111 |
12 files changed, 1685 insertions, 0 deletions
diff --git a/source/use_case/ad/include/AdMelSpectrogram.hpp b/source/use_case/ad/include/AdMelSpectrogram.hpp new file mode 100644 index 0000000..cf8a1d4 --- /dev/null +++ b/source/use_case/ad/include/AdMelSpectrogram.hpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 ADMELSPECTROGRAM_HPP +#define ADMELSPECTROGRAM_HPP + +#include "MelSpectrogram.hpp" + +namespace arm { +namespace app { +namespace audio { + + /* Class to provide anomaly detection specific Mel Spectrogram calculation requirements */ + class AdMelSpectrogram : public MelSpectrogram { + + public: + static constexpr uint32_t ms_defaultSamplingFreq = 16000; + static constexpr uint32_t ms_defaultNumFbankBins = 64; + static constexpr uint32_t ms_defaultMelLoFreq = 0; + static constexpr uint32_t ms_defaultMelHiFreq = 8000; + static constexpr bool ms_defaultUseHtkMethod = false; + + explicit AdMelSpectrogram(const size_t frameLen) + : MelSpectrogram(MelSpecParams( + ms_defaultSamplingFreq, ms_defaultNumFbankBins, + ms_defaultMelLoFreq, ms_defaultMelHiFreq, + frameLen, ms_defaultUseHtkMethod)) + {} + + AdMelSpectrogram() = delete; + ~AdMelSpectrogram() = default; + + protected: + + /** + * @brief Overrides base class implementation of this function. + * @param[in] fftVec Vector populated with FFT magnitudes + * @param[in] melFilterBank 2D Vector with filter bank weights + * @param[in] filterBankFilterFirst Vector containing the first indices of filter bank + * to be used for each bin. + * @param[in] filterBankFilterLast Vector containing the last indices of filter bank + * to be used for each bin. + * @param[out] melEnergies Pre-allocated vector of MEL energies to be + * populated. + * @return true if successful, false otherwise + */ + virtual bool ApplyMelFilterBank( + std::vector<float>& fftVec, + std::vector<std::vector<float>>& melFilterBank, + std::vector<int32_t>& filterBankFilterFirst, + std::vector<int32_t>& filterBankFilterLast, + std::vector<float>& melEnergies) override; + + /** + * @brief Override for the base class implementation convert mel + * energies to logarithmic scale. The difference from + * default behaviour is that the power is converted to dB + * and subsequently clamped. + * @param[in/out] melEnergies - 1D vector of Mel energies + **/ + virtual void ConvertToLogarithmicScale(std::vector<float>& melEnergies) override; + + /** + * @brief Given the low and high Mel values, get the normaliser + * for weights to be applied when populating the filter + * bank. Override for the base class implementation. + * @param[in] leftMel - low Mel frequency value + * @param[in] rightMel - high Mel frequency value + * @param[in] useHTKMethod - bool to signal if HTK method is to be + * used for calculation + * @return Return float value to be applied + * when populating the filter bank. + */ + virtual float GetMelFilterBankNormaliser( + const float& leftMel, + const float& rightMel, + const bool useHTKMethod) override; + }; + +} /* namespace audio */ +} /* namespace app */ +} /* namespace arm */ + +#endif /* ADMELSPECTROGRAM_HPP */ diff --git a/source/use_case/ad/include/AdModel.hpp b/source/use_case/ad/include/AdModel.hpp new file mode 100644 index 0000000..2d83455 --- /dev/null +++ b/source/use_case/ad/include/AdModel.hpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 AD_MODEL_HPP +#define AD_MODEL_HPP + +#include "Model.hpp" + +extern const int g_FrameLength; +extern const int g_FrameStride; +extern const float g_ScoreThreshold; +extern const float g_TrainingMean; + +namespace arm { +namespace app { + + class AdModel : public Model { + protected: + /** @brief Gets the reference to op resolver interface class */ + const tflite::MicroOpResolver& GetOpResolver() override; + + /** @brief Adds operations to the op resolver instance */ + bool EnlistOperations() override; + + const uint8_t* ModelPointer() override; + + size_t ModelSize() override; + + private: + /* Maximum number of individual operations that can be enlisted */ + static constexpr int _ms_maxOpCnt = 6; + + /* A mutable op resolver instance */ + tflite::MicroMutableOpResolver<_ms_maxOpCnt> _m_opResolver; + }; + +} /* namespace app */ +} /* namespace arm */ + +#endif /* AD_MODEL_HPP */ diff --git a/source/use_case/ad/include/AdPostProcessing.hpp b/source/use_case/ad/include/AdPostProcessing.hpp new file mode 100644 index 0000000..f3b35a1 --- /dev/null +++ b/source/use_case/ad/include/AdPostProcessing.hpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 ADPOSTPROCESSING_HPP +#define ADPOSTPROCESSING_HPP + +#include "TensorFlowLiteMicro.hpp" + +#include <vector> + +namespace arm { +namespace app { + + /** @brief Dequantize TensorFlow Lite Micro tensor. + * @param[in] tensor Pointer to the TensorFlow Lite Micro tensor to be dequantized. + * @return Vector with the dequantized tensor values. + **/ + template<typename T> + std::vector<float> Dequantize(TfLiteTensor* tensor); + + /** + * @brief Calculates the softmax of vector in place. **/ + void Softmax(std::vector<float>& inputVector); + + + /** @brief Given a wav file name return AD model output index. + * @param[in] wavFileName Audio WAV filename. + * File name should be in format <anything>_<goes>_XX_<here>.wav + * where XX is the machine ID e.g. 00, 02, 04 or 06 + * @return AD model output index as 8 bit integer. + **/ + int8_t OutputIndexFromFileName(std::string wavFileName); + +} /* namespace app */ +} /* namespace arm */ + +#endif /* ADPOSTPROCESSING_HPP */ diff --git a/source/use_case/ad/include/MelSpectrogram.hpp b/source/use_case/ad/include/MelSpectrogram.hpp new file mode 100644 index 0000000..c1dd61e --- /dev/null +++ b/source/use_case/ad/include/MelSpectrogram.hpp @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 MELSPECTROGRAM_HPP +#define MELSPECTROGRAM_HPP + +#include "PlatformMath.hpp" + +#include <vector> +#include <cstdint> +#include <cmath> +#include <limits> +#include <string> + +namespace arm { +namespace app { +namespace audio { + + /* Mel Spectrogram consolidated parameters */ + class MelSpecParams { + public: + float m_samplingFreq; + uint32_t m_numFbankBins; + float m_melLoFreq; + float m_melHiFreq; + uint32_t m_frameLen; + uint32_t m_frameLenPadded; + bool m_useHtkMethod; + + /** @brief Constructor */ + MelSpecParams(const float samplingFreq, const uint32_t numFbankBins, + const float melLoFreq, const float melHiFreq, + const uint32_t frameLen, const bool useHtkMethod); + + MelSpecParams() = delete; + ~MelSpecParams() = default; + + /** @brief String representation of parameters */ + std::string Str(); + }; + + /** + * @brief Class for Mel Spectrogram feature extraction. + * Based on https://github.com/ARM-software/ML-KWS-for-MCU/blob/master/Deployment/Source/MFCC/mfcc.cpp + * This class is designed to be generic and self-sufficient but + * certain calculation routines can be overridden to accommodate + * use-case specific requirements. + */ + class MelSpectrogram { + + public: + /** + * @brief Extract Mel Spectrogram for one single small frame of + * audio data e.g. 640 samples. + * @param[in] audioData - Vector of audio samples to calculate + * features for. + * @param[in] trainingMean - Value to subtract from the the computed mel spectrogram, default 0. + * @return Vector of extracted Mel Spectrogram features. + **/ + std::vector<float> ComputeMelSpec(const std::vector<int16_t>& audioData, float trainingMean = 0); + + /** + * @brief Constructor + * @param[in] params - Mel Spectrogram parameters + */ + MelSpectrogram(const MelSpecParams& params); + + MelSpectrogram() = delete; + ~MelSpectrogram() = default; + + /** @brief Initialise */ + void Init(); + + /** + * @brief Extract Mel Spectrogram features and quantise for one single small + * frame of audio data e.g. 640 samples. + * @param[in] audioData - Vector of audio samples to calculate + * features for. + * @param[in] quantScale - quantisation scale. + * @param[in] quantOffset - quantisation offset + * @return Vector of extracted quantised Mel Spectrogram features. + **/ + template<typename T> + std::vector<T> MelSpecComputeQuant(const std::vector<int16_t>& audioData, + const float quantScale, + const int quantOffset, + float trainingMean = 0) + { + this->ComputeMelSpec(audioData, trainingMean); + float minVal = std::numeric_limits<T>::min(); + float maxVal = std::numeric_limits<T>::max(); + + std::vector<T> melSpecOut(this->_m_params.m_numFbankBins); + const size_t numFbankBins = this->_m_params.m_numFbankBins; + + /* Quantize to T. */ + for (size_t k = 0; k < numFbankBins; ++k) { + auto quantizedEnergy = std::round(((this->_m_melEnergies[k]) / quantScale) + quantOffset); + melSpecOut[k] = static_cast<T>(std::min<float>(std::max<float>(quantizedEnergy, minVal), maxVal)); + } + + return melSpecOut; + } + + /* Constants */ + static constexpr float ms_logStep = /*logf(6.4)*/ 1.8562979903656 / 27.0; + static constexpr float ms_freqStep = 200.0 / 3; + static constexpr float ms_minLogHz = 1000.0; + static constexpr float ms_minLogMel = ms_minLogHz / ms_freqStep; + + protected: + /** + * @brief Project input frequency to Mel Scale. + * @param[in] freq - input frequency in floating point + * @param[in] useHTKmethod - bool to signal if HTK method is to be + * used for calculation + * @return Mel transformed frequency in floating point + **/ + static float MelScale(const float freq, + const bool useHTKMethod = true); + + /** + * @brief Inverse Mel transform - convert MEL warped frequency + * back to normal frequency + * @param[in] freq - Mel frequency in floating point + * @param[in] useHTKmethod - bool to signal if HTK method is to be + * used for calculation + * @return Real world frequency in floating point + **/ + static float InverseMelScale(const float melFreq, + const bool useHTKMethod = true); + + /** + * @brief Populates MEL energies after applying the MEL filter + * bank weights and adding them up to be placed into + * bins, according to the filter bank's first and last + * indices (pre-computed for each filter bank element + * by _CreateMelFilterBank function). + * @param[in] fftVec Vector populated with FFT magnitudes + * @param[in] melFilterBank 2D Vector with filter bank weights + * @param[in] filterBankFilterFirst Vector containing the first indices of filter bank + * to be used for each bin. + * @param[in] filterBankFilterLast Vector containing the last indices of filter bank + * to be used for each bin. + * @param[out] melEnergies Pre-allocated vector of MEL energies to be + * populated. + * @return true if successful, false otherwise + */ + virtual bool ApplyMelFilterBank( + std::vector<float>& fftVec, + std::vector<std::vector<float>>& melFilterBank, + std::vector<int32_t>& filterBankFilterFirst, + std::vector<int32_t>& filterBankFilterLast, + std::vector<float>& melEnergies); + + /** + * @brief Converts the Mel energies for logarithmic scale + * @param[in/out] melEnergies - 1D vector of Mel energies + **/ + virtual void ConvertToLogarithmicScale(std::vector<float>& melEnergies); + + /** + * @brief Given the low and high Mel values, get the normaliser + * for weights to be applied when populating the filter + * bank. + * @param[in] leftMel - low Mel frequency value + * @param[in] rightMel - high Mel frequency value + * @param[in] useHTKMethod - bool to signal if HTK method is to be + * used for calculation + * @return Return float value to be applied + * when populating the filter bank. + */ + virtual float GetMelFilterBankNormaliser( + const float& leftMel, + const float& rightMel, + const bool useHTKMethod); + + private: + MelSpecParams _m_params; + std::vector<float> _m_frame; + std::vector<float> _m_buffer; + std::vector<float> _m_melEnergies; + std::vector<float> _m_windowFunc; + std::vector<std::vector<float>> _m_melFilterBank; + std::vector<int32_t> _m_filterBankFilterFirst; + std::vector<int32_t> _m_filterBankFilterLast; + bool _m_filterBankInitialised; + arm::app::math::FftInstance _m_fftInstance; + + /** + * @brief Initialises the filter banks. + **/ + void _InitMelFilterBank(); + + /** + * @brief Signals whether the instance of MelSpectrogram has had its + * required buffers initialised + * @return True if initialised, false otherwise + **/ + bool _IsMelFilterBankInited(); + + /** + * @brief Create mel filter banks for Mel Spectrogram calculation. + * @return 2D vector of floats + **/ + std::vector<std::vector<float>> _CreateMelFilterBank(); + + /** + * @brief Computes the magnitude from an interleaved complex array + **/ + void _ConvertToPowerSpectrum(); + + }; + +} /* namespace audio */ +} /* namespace app */ +} /* namespace arm */ + + +#endif /* MELSPECTROGRAM_HPP */ diff --git a/source/use_case/ad/include/UseCaseHandler.hpp b/source/use_case/ad/include/UseCaseHandler.hpp new file mode 100644 index 0000000..b62b36d --- /dev/null +++ b/source/use_case/ad/include/UseCaseHandler.hpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 AD_EVT_HANDLER_H +#define AD_EVT_HANDLER_H +#include "AppContext.hpp" + +namespace arm { +namespace app { + /** + * @brief Handles the inference event + * @param[in] ctx pointer to the application context + * @param[in] dataIndex index to the input data to classify + * @param[in] runAll flag to request classification of all the available audio clips + * @return True or false based on execution success + **/ + bool ClassifyVibrationHandler(ApplicationContext& ctx, uint32_t dataIndex, bool runAll); +} /* namespace app */ +} /* namespace arm */ +#endif /* AD_EVT_HANDLER_H */
\ No newline at end of file diff --git a/source/use_case/ad/src/AdMelSpectrogram.cc b/source/use_case/ad/src/AdMelSpectrogram.cc new file mode 100644 index 0000000..183c05c --- /dev/null +++ b/source/use_case/ad/src/AdMelSpectrogram.cc @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "AdMelSpectrogram.hpp" + +#include "PlatformMath.hpp" + +namespace arm { +namespace app { +namespace audio { + + bool AdMelSpectrogram::ApplyMelFilterBank( + std::vector<float>& fftVec, + std::vector<std::vector<float>>& melFilterBank, + std::vector<int32_t>& filterBankFilterFirst, + std::vector<int32_t>& filterBankFilterLast, + std::vector<float>& melEnergies) + { + const size_t numBanks = melEnergies.size(); + + if (numBanks != filterBankFilterFirst.size() || + numBanks != filterBankFilterLast.size()) { + printf_err("unexpected filter bank lengths\n"); + return false; + } + + for (size_t bin = 0; bin < numBanks; ++bin) { + auto filterBankIter = melFilterBank[bin].begin(); + float melEnergy = 1e-10; /* Avoid log of zero at later stages. */ + const int32_t firstIndex = filterBankFilterFirst[bin]; + const int32_t lastIndex = filterBankFilterLast[bin]; + + for (int32_t i = firstIndex; i <= lastIndex; ++i) { + melEnergy += (*filterBankIter++ * fftVec[i]); + } + + melEnergies[bin] = melEnergy; + } + + return true; + } + + void AdMelSpectrogram::ConvertToLogarithmicScale( + std::vector<float>& melEnergies) + { + /* Container for natural logarithms of mel energies */ + std::vector <float> vecLogEnergies(melEnergies.size(), 0.f); + + /* Because we are taking natural logs, we need to multiply by log10(e). + * Also, for wav2letter model, we scale our log10 values by 10 */ + constexpr float multiplier = 10.0 * /* default scalar */ + 0.4342944819032518; /* log10f(std::exp(1.0))*/ + + /* Take log of the whole vector */ + math::MathUtils::VecLogarithmF32(melEnergies, vecLogEnergies); + + /* Scale the log values. */ + for (auto iterM = melEnergies.begin(), iterL = vecLogEnergies.begin(); + iterM != melEnergies.end(); ++iterM, ++iterL) { + + *iterM = *iterL * multiplier; + } + } + + float AdMelSpectrogram::GetMelFilterBankNormaliser( + const float& leftMel, + const float& rightMel, + const bool useHTKMethod) + { + /* Slaney normalization for mel weights. */ + return (2.0f / (AdMelSpectrogram::InverseMelScale(rightMel, useHTKMethod) - + AdMelSpectrogram::InverseMelScale(leftMel, useHTKMethod))); + } + +} /* namespace audio */ +} /* namespace app */ +} /* namespace arm */ diff --git a/source/use_case/ad/src/AdModel.cc b/source/use_case/ad/src/AdModel.cc new file mode 100644 index 0000000..148bc98 --- /dev/null +++ b/source/use_case/ad/src/AdModel.cc @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "AdModel.hpp" + +#include "hal.h" + +const tflite::MicroOpResolver& arm::app::AdModel::GetOpResolver() +{ + return this->_m_opResolver; +} + +bool arm::app::AdModel::EnlistOperations() +{ + this->_m_opResolver.AddAveragePool2D(); + this->_m_opResolver.AddConv2D(); + this->_m_opResolver.AddDepthwiseConv2D(); + this->_m_opResolver.AddRelu6(); + this->_m_opResolver.AddReshape(); + +#if defined(ARM_NPU) + if (kTfLiteOk == this->_m_opResolver.AddEthosU()) { + info("Added %s support to op resolver\n", + tflite::GetString_ETHOSU()); + } else { + printf_err("Failed to add Arm NPU support to op resolver."); + return false; + } +#endif /* ARM_NPU */ + return true; +} + +extern uint8_t* GetModelPointer(); +const uint8_t* arm::app::AdModel::ModelPointer() +{ + return GetModelPointer(); +} +extern size_t GetModelLen(); +size_t arm::app::AdModel::ModelSize() +{ + return GetModelLen(); +} diff --git a/source/use_case/ad/src/AdPostProcessing.cc b/source/use_case/ad/src/AdPostProcessing.cc new file mode 100644 index 0000000..157784b --- /dev/null +++ b/source/use_case/ad/src/AdPostProcessing.cc @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "AdPostProcessing.hpp" + +#include "hal.h" + +#include <numeric> +#include <cmath> +#include <string> + +namespace arm { +namespace app { + + template<typename T> + std::vector<float> Dequantize(TfLiteTensor* tensor) { + + if (tensor == nullptr) { + printf_err("Tensor is null pointer can not dequantize.\n"); + return std::vector<float>(); + } + T* tensorData = tflite::GetTensorData<T>(tensor); + + uint32_t totalOutputSize = 1; + for (int inputDim = 0; inputDim < tensor->dims->size; inputDim++){ + totalOutputSize *= tensor->dims->data[inputDim]; + } + + /* For getting the floating point values, we need quantization parameters */ + QuantParams quantParams = GetTensorQuantParams(tensor); + + std::vector<float> dequantizedOutput(totalOutputSize); + + for (size_t i = 0; i < totalOutputSize; ++i) { + dequantizedOutput[i] = quantParams.scale * (tensorData[i] - quantParams.offset); + } + + return dequantizedOutput; + } + + void Softmax(std::vector<float>& inputVector) { + auto start = inputVector.begin(); + auto end = inputVector.end(); + + /* Fix for numerical stability and apply exp. */ + float maxValue = *std::max_element(start, end); + for (auto it = start; it!=end; ++it) { + *it = std::exp((*it) - maxValue); + } + + float sumExp = std::accumulate(start, end, 0.0f); + + for (auto it = start; it!=end; ++it) { + *it = (*it)/sumExp; + } + } + + int8_t OutputIndexFromFileName(std::string wavFileName) { + /* Filename is assumed in the form machine_id_00.wav */ + std::string delimiter = "_"; /* First character used to split the file name up. */ + size_t delimiterStart; + std::string subString; + size_t machineIdxInString = 3; /* Which part of the file name the machine id should be at. */ + + for (size_t i = 0; i < machineIdxInString; ++i) { + delimiterStart = wavFileName.find(delimiter); + subString = wavFileName.substr(0, delimiterStart); + wavFileName.erase(0, delimiterStart + delimiter.length()); + } + + /* At this point substring should be 00.wav */ + delimiter = "."; /* Second character used to split the file name up. */ + delimiterStart = subString.find(delimiter); + subString = (delimiterStart != std::string::npos) ? subString.substr(0, delimiterStart) : subString; + + auto is_number = [](const std::string& str) -> bool + { + std::string::const_iterator it = str.begin(); + while (it != str.end() && std::isdigit(*it)) ++it; + return !str.empty() && it == str.end(); + }; + + const int8_t machineIdx = is_number(subString) ? std::stoi(subString) : -1; + + /* Return corresponding index in the output vector. */ + if (machineIdx == 0) { + return 0; + } else if (machineIdx == 2) { + return 1; + } else if (machineIdx == 4) { + return 2; + } else if (machineIdx == 6) { + return 3; + } else { + printf_err("%d is an invalid machine index \n", machineIdx); + return -1; + } + } + + template std::vector<float> Dequantize<uint8_t>(TfLiteTensor* tensor); + template std::vector<float> Dequantize<int8_t>(TfLiteTensor* tensor); +} /* namespace app */ +} /* namespace arm */
\ No newline at end of file diff --git a/source/use_case/ad/src/MainLoop.cc b/source/use_case/ad/src/MainLoop.cc new file mode 100644 index 0000000..5455b43 --- /dev/null +++ b/source/use_case/ad/src/MainLoop.cc @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "hal.h" /* Brings in platform definitions */ +#include "InputFiles.hpp" /* For input data */ +#include "AdModel.hpp" /* Model class for running inference */ +#include "UseCaseCommonUtils.hpp" /* Utils functions */ +#include "UseCaseHandler.hpp" /* Handlers for different user options */ + +enum opcodes +{ + MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector */ + MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index */ + MENU_OPT_RUN_INF_ALL, /* Run inference on all */ + MENU_OPT_SHOW_MODEL_INFO, /* Show model info */ + MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio signals */ +}; + +static void DisplayMenu() +{ + printf("\n\nUser input required\n"); + printf("Enter option number from:\n\n"); + printf(" %u. Classify next audio signal\n", MENU_OPT_RUN_INF_NEXT); + printf(" %u. Classify audio signal at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); + printf(" %u. Run classification on all audio signals\n", MENU_OPT_RUN_INF_ALL); + printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); + printf(" %u. List audio signals\n\n", MENU_OPT_LIST_AUDIO_CLIPS); + printf(" Choice: "); +} + + +void main_loop(hal_platform& platform) +{ + arm::app::AdModel model; /* Model wrapper object. */ + + /* Load the model. */ + if (!model.Init()) + { + printf_err("failed to initialise model\n"); + return; + } + + /* Instantiate application context. */ + arm::app::ApplicationContext caseContext; + + caseContext.Set<hal_platform&>("platform", platform); + caseContext.Set<arm::app::Model&>("model", model); + caseContext.Set<uint32_t>("clipIndex", 0); + caseContext.Set<int>("frameLength", g_FrameLength); + caseContext.Set<int>("frameStride", g_FrameStride); + caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); + caseContext.Set<float>("trainingMean", g_TrainingMean); + + /* Main program loop. */ + bool executionSuccessful = true; + constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; + + /* Loop. */ + do { + int menuOption = MENU_OPT_RUN_INF_NEXT; + if (bUseMenu) { + DisplayMenu(); + menuOption = arm::app::ReadUserInputAsInt(platform); + printf("\n"); + } + switch (menuOption) { + case MENU_OPT_RUN_INF_NEXT: + executionSuccessful = ClassifyVibrationHandler( + caseContext, + caseContext.Get<uint32_t>("clipIndex"), + false); + break; + case MENU_OPT_RUN_INF_CHOSEN: { + printf(" Enter the data index [0, %d]: ", + NUMBER_OF_FILES-1); + auto audioIndex = static_cast<uint32_t>( + arm::app::ReadUserInputAsInt(platform)); + executionSuccessful = ClassifyVibrationHandler(caseContext, + audioIndex, + false); + break; + } + case MENU_OPT_RUN_INF_ALL: + executionSuccessful = ClassifyVibrationHandler( + caseContext, + caseContext.Get<uint32_t>("clipIndex"), + true); + break; + case MENU_OPT_SHOW_MODEL_INFO: + executionSuccessful = model.ShowModelInfoHandler(); + break; + case MENU_OPT_LIST_AUDIO_CLIPS: + executionSuccessful = ListFilesHandler(caseContext); + break; + default: + printf("Incorrect choice, try again."); + break; + } + } while (executionSuccessful && bUseMenu); + info("Main loop terminated.\n"); +} diff --git a/source/use_case/ad/src/MelSpectrogram.cc b/source/use_case/ad/src/MelSpectrogram.cc new file mode 100644 index 0000000..86d57e6 --- /dev/null +++ b/source/use_case/ad/src/MelSpectrogram.cc @@ -0,0 +1,311 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "MelSpectrogram.hpp" + +#include "PlatformMath.hpp" + +#include <cfloat> + +namespace arm { +namespace app { +namespace audio { + + MelSpecParams::MelSpecParams( + const float samplingFreq, + const uint32_t numFbankBins, + const float melLoFreq, + const float melHiFreq, + const uint32_t frameLen, + const bool useHtkMethod): + m_samplingFreq(samplingFreq), + m_numFbankBins(numFbankBins), + m_melLoFreq(melLoFreq), + m_melHiFreq(melHiFreq), + m_frameLen(frameLen), + + /* Smallest power of 2 >= frame length. */ + m_frameLenPadded(pow(2, ceil((log(frameLen)/log(2))))), + m_useHtkMethod(useHtkMethod) + {} + + std::string MelSpecParams::Str() + { + char strC[1024]; + snprintf(strC, sizeof(strC) - 1, "\n \ + \n\t Sampling frequency: %f\ + \n\t Number of filter banks: %u\ + \n\t Mel frequency limit (low): %f\ + \n\t Mel frequency limit (high): %f\ + \n\t Frame length: %u\ + \n\t Padded frame length: %u\ + \n\t Using HTK for Mel scale: %s\n", + this->m_samplingFreq, this->m_numFbankBins, this->m_melLoFreq, + this->m_melHiFreq, this->m_frameLen, + this->m_frameLenPadded, this->m_useHtkMethod ? "yes" : "no"); + return std::string{strC}; + } + + MelSpectrogram::MelSpectrogram(const MelSpecParams& params): + _m_params(params), + _m_filterBankInitialised(false) + { + this->_m_buffer = std::vector<float>( + this->_m_params.m_frameLenPadded, 0.0); + this->_m_frame = std::vector<float>( + this->_m_params.m_frameLenPadded, 0.0); + this->_m_melEnergies = std::vector<float>( + this->_m_params.m_numFbankBins, 0.0); + + this->_m_windowFunc = std::vector<float>(this->_m_params.m_frameLen); + const float multiplier = 2 * M_PI / this->_m_params.m_frameLen; + + /* Create window function. */ + for (size_t i = 0; i < this->_m_params.m_frameLen; ++i) { + this->_m_windowFunc[i] = (0.5 - (0.5 * + math::MathUtils::CosineF32(static_cast<float>(i) * multiplier))); + } + + math::MathUtils::FftInitF32(this->_m_params.m_frameLenPadded, this->_m_fftInstance); + debug("Instantiated Mel Spectrogram object: %s\n", this->_m_params.Str().c_str()); + } + + void MelSpectrogram::Init() + { + this->_InitMelFilterBank(); + } + + float MelSpectrogram::MelScale(const float freq, const bool useHTKMethod) + { + if (useHTKMethod) { + return 1127.0f * logf (1.0f + freq / 700.0f); + } else { + /* Slaney formula for mel scale. */ + float mel = freq / ms_freqStep; + + if (freq >= ms_minLogHz) { + mel = ms_minLogMel + logf(freq / ms_minLogHz) / ms_logStep; + } + return mel; + } + } + + float MelSpectrogram::InverseMelScale(const float melFreq, const bool useHTKMethod) + { + if (useHTKMethod) { + return 700.0f * (expf (melFreq / 1127.0f) - 1.0f); + } else { + /* Slaney formula for inverse mel scale. */ + float freq = ms_freqStep * melFreq; + + if (melFreq >= ms_minLogMel) { + freq = ms_minLogHz * expf(ms_logStep * (melFreq - ms_minLogMel)); + } + return freq; + } + } + + bool MelSpectrogram::ApplyMelFilterBank( + std::vector<float>& fftVec, + std::vector<std::vector<float>>& melFilterBank, + std::vector<int32_t>& filterBankFilterFirst, + std::vector<int32_t>& filterBankFilterLast, + std::vector<float>& melEnergies) + { + const size_t numBanks = melEnergies.size(); + + if (numBanks != filterBankFilterFirst.size() || + numBanks != filterBankFilterLast.size()) { + printf_err("unexpected filter bank lengths\n"); + return false; + } + + for (size_t bin = 0; bin < numBanks; ++bin) { + auto filterBankIter = melFilterBank[bin].begin(); + float melEnergy = FLT_MIN; /* Avoid log of zero at later stages */ + int32_t firstIndex = filterBankFilterFirst[bin]; + int32_t lastIndex = filterBankFilterLast[bin]; + + for (int i = firstIndex; i <= lastIndex; ++i) { + float energyRep = math::MathUtils::SqrtF32(fftVec[i]); + melEnergy += (*filterBankIter++ * energyRep); + } + + melEnergies[bin] = melEnergy; + } + + return true; + } + + void MelSpectrogram::ConvertToLogarithmicScale(std::vector<float>& melEnergies) + { + for (size_t bin = 0; bin < melEnergies.size(); ++bin) { + melEnergies[bin] = logf(melEnergies[bin]); + } + } + + void MelSpectrogram::_ConvertToPowerSpectrum() + { + const uint32_t halfDim = this->_m_params.m_frameLenPadded / 2; + + /* Handle this special case. */ + float firstEnergy = this->_m_buffer[0] * this->_m_buffer[0]; + float lastEnergy = this->_m_buffer[1] * this->_m_buffer[1]; + + math::MathUtils::ComplexMagnitudeSquaredF32( + this->_m_buffer.data(), + this->_m_buffer.size(), + this->_m_buffer.data(), + this->_m_buffer.size()/2); + + this->_m_buffer[0] = firstEnergy; + this->_m_buffer[halfDim] = lastEnergy; + } + + float MelSpectrogram::GetMelFilterBankNormaliser( + const float& leftMel, + const float& rightMel, + const bool useHTKMethod) + { + UNUSED(leftMel); + UNUSED(rightMel); + UNUSED(useHTKMethod); + + /* By default, no normalisation => return 1 */ + return 1.f; + } + + void MelSpectrogram::_InitMelFilterBank() + { + if (!this->_IsMelFilterBankInited()) { + this->_m_melFilterBank = this->_CreateMelFilterBank(); + this->_m_filterBankInitialised = true; + } + } + + bool MelSpectrogram::_IsMelFilterBankInited() + { + return this->_m_filterBankInitialised; + } + + std::vector<float> MelSpectrogram::ComputeMelSpec(const std::vector<int16_t>& audioData, float trainingMean) + { + this->_InitMelFilterBank(); + + /* TensorFlow way of normalizing .wav data to (-1, 1). */ + constexpr float normaliser = 1.0/(1<<15); + for (size_t i = 0; i < this->_m_params.m_frameLen; ++i) { + this->_m_frame[i] = static_cast<float>(audioData[i]) * normaliser; + } + + /* Apply window function to input frame. */ + for(size_t i = 0; i < this->_m_params.m_frameLen; ++i) { + this->_m_frame[i] *= this->_m_windowFunc[i]; + } + + /* Set remaining frame values to 0. */ + std::fill(this->_m_frame.begin() + this->_m_params.m_frameLen,this->_m_frame.end(), 0); + + /* Compute FFT. */ + math::MathUtils::FftF32(this->_m_frame, this->_m_buffer, this->_m_fftInstance); + + /* Convert to power spectrum. */ + this->_ConvertToPowerSpectrum(); + + /* Apply mel filterbanks. */ + if (!this->ApplyMelFilterBank(this->_m_buffer, + this->_m_melFilterBank, + this->_m_filterBankFilterFirst, + this->_m_filterBankFilterLast, + this->_m_melEnergies)) { + printf_err("Failed to apply MEL filter banks\n"); + } + + /* Convert to logarithmic scale */ + this->ConvertToLogarithmicScale(this->_m_melEnergies); + + /* Perform mean subtraction. */ + for (auto& energy:this->_m_melEnergies) { + energy -= trainingMean; + } + + return this->_m_melEnergies; + } + + std::vector<std::vector<float>> MelSpectrogram::_CreateMelFilterBank() + { + size_t numFftBins = this->_m_params.m_frameLenPadded / 2; + float fftBinWidth = static_cast<float>(this->_m_params.m_samplingFreq) / this->_m_params.m_frameLenPadded; + + float melLowFreq = MelSpectrogram::MelScale(this->_m_params.m_melLoFreq, + this->_m_params.m_useHtkMethod); + float melHighFreq = MelSpectrogram::MelScale(this->_m_params.m_melHiFreq, + this->_m_params.m_useHtkMethod); + float melFreqDelta = (melHighFreq - melLowFreq) / (this->_m_params.m_numFbankBins + 1); + + std::vector<float> thisBin = std::vector<float>(numFftBins); + std::vector<std::vector<float>> melFilterBank( + this->_m_params.m_numFbankBins); + this->_m_filterBankFilterFirst = + std::vector<int32_t>(this->_m_params.m_numFbankBins); + this->_m_filterBankFilterLast = + std::vector<int32_t>(this->_m_params.m_numFbankBins); + + for (size_t bin = 0; bin < this->_m_params.m_numFbankBins; bin++) { + float leftMel = melLowFreq + bin * melFreqDelta; + float centerMel = melLowFreq + (bin + 1) * melFreqDelta; + float rightMel = melLowFreq + (bin + 2) * melFreqDelta; + + int32_t firstIndex = -1; + int32_t lastIndex = -1; + const float normaliser = this->GetMelFilterBankNormaliser(leftMel, rightMel, this->_m_params.m_useHtkMethod); + + for (size_t i = 0; i < numFftBins; ++i) { + float freq = (fftBinWidth * i); /* Center freq of this fft bin. */ + float mel = MelSpectrogram::MelScale(freq, this->_m_params.m_useHtkMethod); + thisBin[i] = 0.0; + + if (mel > leftMel && mel < rightMel) { + float weight; + if (mel <= centerMel) { + weight = (mel - leftMel) / (centerMel - leftMel); + } else { + weight = (rightMel - mel) / (rightMel - centerMel); + } + + thisBin[i] = weight * normaliser; + if (firstIndex == -1) { + firstIndex = i; + } + lastIndex = i; + } + } + + this->_m_filterBankFilterFirst[bin] = firstIndex; + this->_m_filterBankFilterLast[bin] = lastIndex; + + /* Copy the part we care about. */ + for (int32_t i = firstIndex; i <= lastIndex; ++i) { + melFilterBank[bin].push_back(thisBin[i]); + } + } + + return melFilterBank; + } + +} /* namespace audio */ +} /* namespace app */ +} /* namespace arm */ diff --git a/source/use_case/ad/src/UseCaseHandler.cc b/source/use_case/ad/src/UseCaseHandler.cc new file mode 100644 index 0000000..c18a0a4 --- /dev/null +++ b/source/use_case/ad/src/UseCaseHandler.cc @@ -0,0 +1,422 @@ +/* + * Copyright (c) 2021 Arm Limited. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + * + * 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 "UseCaseHandler.hpp" + +#include "AdModel.hpp" +#include "InputFiles.hpp" +#include "Classifier.hpp" +#include "hal.h" +#include "AdMelSpectrogram.hpp" +#include "AudioUtils.hpp" +#include "UseCaseCommonUtils.hpp" +#include "AdPostProcessing.hpp" + +namespace arm { +namespace app { + + /** + * @brief Helper function to increment current audio clip index + * @param[in/out] ctx pointer to the application context object + **/ + static void _IncrementAppCtxClipIdx(ApplicationContext& ctx); + + /** + * @brief Helper function to set the audio clip index + * @param[in/out] ctx pointer to the application context object + * @param[in] idx value to be set + * @return true if index is set, false otherwise + **/ + static bool _SetAppCtxClipIdx(ApplicationContext& ctx, uint32_t idx); + + /** + * @brief Presents inference results using the data presentation + * object. + * @param[in] platform reference to the hal platform object + * @param[in] result average sum of classification results + * @param[in] threhsold if larger than this value we have an anomaly + * @return true if successful, false otherwise + **/ + static bool _PresentInferenceResult(hal_platform& platform, float result, float threshold); + + /** + * @brief Returns a function to perform feature calculation and populates input tensor data with + * MelSpe data. + * + * Input tensor data type check is performed to choose correct MFCC feature data type. + * If tensor has an integer data type then original features are quantised. + * + * Warning: mfcc calculator provided as input must have the same life scope as returned function. + * + * @param[in] mfcc MFCC feature calculator. + * @param[in/out] inputTensor Input tensor pointer to store calculated features. + * @param[i] cacheSize Size of the feture vectors cache (number of feature vectors). + * @return function function to be called providing audio sample and sliding window index. + */ + static std::function<void (std::vector<int16_t>&, int, bool, size_t, size_t)> + GetFeatureCalculator(audio::AdMelSpectrogram& melSpec, + TfLiteTensor* inputTensor, + size_t cacheSize, + float trainingMean); + + /* Vibration classification handler */ + bool ClassifyVibrationHandler(ApplicationContext& ctx, uint32_t clipIndex, bool runAll) + { + auto& platform = ctx.Get<hal_platform&>("platform"); + + constexpr uint32_t dataPsnTxtInfStartX = 20; + constexpr uint32_t dataPsnTxtInfStartY = 40; + + platform.data_psn->clear(COLOR_BLACK); + + auto& model = ctx.Get<Model&>("model"); + + /* If the request has a valid size, set the audio index */ + if (clipIndex < NUMBER_OF_FILES) { + if (!_SetAppCtxClipIdx(ctx, clipIndex)) { + return false; + } + } + if (!model.IsInited()) { + printf_err("Model is not initialised! Terminating processing.\n"); + return false; + } + + const auto frameLength = ctx.Get<int>("frameLength"); + const auto frameStride = ctx.Get<int>("frameStride"); + const auto scoreThreshold = ctx.Get<float>("scoreThreshold"); + const float trainingMean = ctx.Get<float>("trainingMean"); + auto startClipIdx = ctx.Get<uint32_t>("clipIndex"); + + TfLiteTensor* outputTensor = model.GetOutputTensor(0); + TfLiteTensor* inputTensor = model.GetInputTensor(0); + + if (!inputTensor->dims) { + printf_err("Invalid input tensor dims\n"); + return false; + } + + TfLiteIntArray* inputShape = model.GetInputShape(0); + const uint32_t kNumRows = inputShape->data[1]; + const uint32_t kNumCols = inputShape->data[2]; + + audio::AdMelSpectrogram melSpec = audio::AdMelSpectrogram(frameLength); + melSpec.Init(); + + /* Deduce the data length required for 1 inference from the network parameters. */ + const uint8_t inputResizeScale = 2; + const uint32_t audioDataWindowSize = (((inputResizeScale * kNumCols) - 1) * frameStride) + frameLength; + + /* We are choosing to move by 20 frames across the audio for each inference. */ + const uint8_t nMelSpecVectorsInAudioStride = 20; + + auto audioDataStride = nMelSpecVectorsInAudioStride * frameStride; + + do { + auto currentIndex = ctx.Get<uint32_t>("clipIndex"); + + /* Get the output index to look at based on id in the filename. */ + int8_t machineOutputIndex = OutputIndexFromFileName(get_filename(currentIndex)); + if (machineOutputIndex == -1) { + return false; + } + + /* Creating a Mel Spectrogram sliding window for the data required for 1 inference. + * "resizing" done here by multiplying stride by resize scale. */ + auto audioMelSpecWindowSlider = audio::SlidingWindow<const int16_t>( + get_audio_array(currentIndex), + audioDataWindowSize, frameLength, + frameStride * inputResizeScale); + + /* Creating a sliding window through the whole audio clip. */ + auto audioDataSlider = audio::SlidingWindow<const int16_t>( + get_audio_array(currentIndex), + get_audio_array_size(currentIndex), + audioDataWindowSize, audioDataStride); + + /* Calculate number of the feature vectors in the window overlap region taking into account resizing. + * These feature vectors will be reused.*/ + auto numberOfReusedFeatureVectors = kNumRows - (nMelSpecVectorsInAudioStride / inputResizeScale); + + /* Construct feature calculation function. */ + auto melSpecFeatureCalc = GetFeatureCalculator(melSpec, inputTensor, + numberOfReusedFeatureVectors, trainingMean); + if (!melSpecFeatureCalc){ + return false; + } + + /* Result is an averaged sum over inferences. */ + float result = 0; + + /* Display message on the LCD - inference running. */ + std::string str_inf{"Running inference... "}; + platform.data_psn->present_data_text( + str_inf.c_str(), str_inf.size(), + dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); + info("Running inference on audio clip %u => %s\n", currentIndex, get_filename(currentIndex)); + + /* Start sliding through audio clip. */ + while (audioDataSlider.HasNext()) { + const int16_t *inferenceWindow = audioDataSlider.Next(); + + /* We moved to the next window - set the features sliding to the new address. */ + audioMelSpecWindowSlider.Reset(inferenceWindow); + + /* The first window does not have cache ready. */ + bool useCache = audioDataSlider.Index() > 0 && numberOfReusedFeatureVectors > 0; + + /* Start calculating features inside one audio sliding window. */ + while (audioMelSpecWindowSlider.HasNext()) { + const int16_t *melSpecWindow = audioMelSpecWindowSlider.Next(); + std::vector<int16_t> melSpecAudioData = std::vector<int16_t>(melSpecWindow, + melSpecWindow + frameLength); + + /* Compute features for this window and write them to input tensor. */ + melSpecFeatureCalc(melSpecAudioData, audioMelSpecWindowSlider.Index(), + useCache, nMelSpecVectorsInAudioStride, inputResizeScale); + } + + info("Inference %zu/%zu\n", audioDataSlider.Index() + 1, + audioDataSlider.TotalStrides() + 1); + + /* Run inference over this audio clip sliding window */ + arm::app::RunInference(platform, model); + + /* Use the negative softmax score of the corresponding index as the outlier score */ + std::vector<float> dequantOutput = Dequantize<int8_t>(outputTensor); + Softmax(dequantOutput); + result += -dequantOutput[machineOutputIndex]; + +#if VERIFY_TEST_OUTPUT + arm::app::DumpTensor(outputTensor); +#endif /* VERIFY_TEST_OUTPUT */ + } /* while (audioDataSlider.HasNext()) */ + + /* Use average over whole clip as final score. */ + result /= (audioDataSlider.TotalStrides() + 1); + + /* Erase. */ + str_inf = std::string(str_inf.size(), ' '); + platform.data_psn->present_data_text( + str_inf.c_str(), str_inf.size(), + dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); + + ctx.Set<float>("result", result); + if (!_PresentInferenceResult(platform, result, scoreThreshold)) { + return false; + } + + _IncrementAppCtxClipIdx(ctx); + + } while (runAll && ctx.Get<uint32_t>("clipIndex") != startClipIdx); + + return true; + } + + static void _IncrementAppCtxClipIdx(ApplicationContext& ctx) + { + auto curAudioIdx = ctx.Get<uint32_t>("clipIndex"); + + if (curAudioIdx + 1 >= NUMBER_OF_FILES) { + ctx.Set<uint32_t>("clipIndex", 0); + return; + } + ++curAudioIdx; + ctx.Set<uint32_t>("clipIndex", curAudioIdx); + } + + static bool _SetAppCtxClipIdx(ApplicationContext& ctx, const uint32_t idx) + { + if (idx >= NUMBER_OF_FILES) { + printf_err("Invalid idx %u (expected less than %u)\n", + idx, NUMBER_OF_FILES); + return false; + } + ctx.Set<uint32_t>("clipIndex", idx); + return true; + } + + static bool _PresentInferenceResult(hal_platform& platform, float result, float threshold) + { + constexpr uint32_t dataPsnTxtStartX1 = 20; + constexpr uint32_t dataPsnTxtStartY1 = 30; + constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment */ + + platform.data_psn->set_text_color(COLOR_GREEN); + + /* Display each result */ + uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr; + + std::string resultStr = std::string{"Average anomaly score is: "} + std::to_string(result) + + std::string("\n") + std::string("Anomaly threshold is: ") + std::to_string(threshold) + + std::string("\n"); + + if (result > threshold) { + resultStr += std::string("Anomaly detected!"); + } else { + resultStr += std::string("Everything fine, no anomaly detected!"); + } + + platform.data_psn->present_data_text( + resultStr.c_str(), resultStr.size(), + dataPsnTxtStartX1, rowIdx1, 0); + + info("%s\n", resultStr.c_str()); + + return true; + } + + /** + * @brief Generic feature calculator factory. + * + * Returns lambda function to compute features using features cache. + * Real features math is done by a lambda function provided as a parameter. + * Features are written to input tensor memory. + * + * @tparam T feature vector type. + * @param inputTensor model input tensor pointer. + * @param cacheSize number of feature vectors to cache. Defined by the sliding window overlap. + * @param compute features calculator function. + * @return lambda function to compute features. + */ + template<class T> + std::function<void (std::vector<int16_t>&, size_t, bool, size_t, size_t)> + _FeatureCalc(TfLiteTensor* inputTensor, size_t cacheSize, + std::function<std::vector<T> (std::vector<int16_t>& )> compute) + { + /* Feature cache to be captured by lambda function*/ + static std::vector<std::vector<T>> featureCache = std::vector<std::vector<T>>(cacheSize); + + return [=](std::vector<int16_t>& audioDataWindow, + size_t index, + bool useCache, + size_t featuresOverlapIndex, + size_t resizeScale) + { + T *tensorData = tflite::GetTensorData<T>(inputTensor); + std::vector<T> features; + + /* Reuse features from cache if cache is ready and sliding windows overlap. + * Overlap is in the beginning of sliding window with a size of a feature cache. */ + if (useCache && index < featureCache.size()) { + features = std::move(featureCache[index]); + } else { + features = std::move(compute(audioDataWindow)); + } + auto size = features.size() / resizeScale; + auto sizeBytes = sizeof(T); + + /* Input should be transposed and "resized" by skipping elements. */ + for (size_t outIndex = 0; outIndex < size; outIndex++) { + std::memcpy(tensorData + (outIndex*size) + index, &features[outIndex*resizeScale], sizeBytes); + } + + /* Start renewing cache as soon iteration goes out of the windows overlap. */ + if (index >= featuresOverlapIndex / resizeScale) { + featureCache[index - featuresOverlapIndex / resizeScale] = std::move(features); + } + }; + } + + template std::function<void (std::vector<int16_t>&, size_t , bool, size_t, size_t)> + _FeatureCalc<int8_t>(TfLiteTensor* inputTensor, + size_t cacheSize, + std::function<std::vector<int8_t> (std::vector<int16_t>&)> compute); + + template std::function<void (std::vector<int16_t>&, size_t , bool, size_t, size_t)> + _FeatureCalc<uint8_t>(TfLiteTensor* inputTensor, + size_t cacheSize, + std::function<std::vector<uint8_t> (std::vector<int16_t>&)> compute); + + template std::function<void (std::vector<int16_t>&, size_t , bool, size_t, size_t)> + _FeatureCalc<int16_t>(TfLiteTensor* inputTensor, + size_t cacheSize, + std::function<std::vector<int16_t> (std::vector<int16_t>&)> compute); + + template std::function<void(std::vector<int16_t>&, size_t, bool, size_t, size_t)> + _FeatureCalc<float>(TfLiteTensor *inputTensor, + size_t cacheSize, + std::function<std::vector<float>(std::vector<int16_t>&)> compute); + + + static std::function<void (std::vector<int16_t>&, int, bool, size_t, size_t)> + GetFeatureCalculator(audio::AdMelSpectrogram& melSpec, TfLiteTensor* inputTensor, size_t cacheSize, float trainingMean) + { + std::function<void (std::vector<int16_t>&, size_t, bool, size_t, size_t)> melSpecFeatureCalc; + + TfLiteQuantization quant = inputTensor->quantization; + + if (kTfLiteAffineQuantization == quant.type) { + + auto *quantParams = (TfLiteAffineQuantization *) quant.params; + const float quantScale = quantParams->scale->data[0]; + const int quantOffset = quantParams->zero_point->data[0]; + + switch (inputTensor->type) { + case kTfLiteInt8: { + melSpecFeatureCalc = _FeatureCalc<int8_t>(inputTensor, + cacheSize, + [=, &melSpec](std::vector<int16_t>& audioDataWindow) { + return melSpec.MelSpecComputeQuant<int8_t>(audioDataWindow, + quantScale, + quantOffset, + trainingMean); + } + ); + break; + } + case kTfLiteUInt8: { + melSpecFeatureCalc = _FeatureCalc<uint8_t>(inputTensor, + cacheSize, + [=, &melSpec](std::vector<int16_t>& audioDataWindow) { + return melSpec.MelSpecComputeQuant<uint8_t>(audioDataWindow, + quantScale, + quantOffset, + trainingMean); + } + ); + break; + } + case kTfLiteInt16: { + melSpecFeatureCalc = _FeatureCalc<int16_t>(inputTensor, + cacheSize, + [=, &melSpec](std::vector<int16_t>& audioDataWindow) { + return melSpec.MelSpecComputeQuant<int16_t>(audioDataWindow, + quantScale, + quantOffset, + trainingMean); + } + ); + break; + } + default: + printf_err("Tensor type %s not supported\n", TfLiteTypeGetName(inputTensor->type)); + } + + + } else { + melSpecFeatureCalc = melSpecFeatureCalc = _FeatureCalc<float>(inputTensor, + cacheSize, + [=, &melSpec](std::vector<int16_t>& audioDataWindow) { + return melSpec.ComputeMelSpec(audioDataWindow, + trainingMean); + }); + } + return melSpecFeatureCalc; + } + +} /* namespace app */ +} /* namespace arm */ diff --git a/source/use_case/ad/usecase.cmake b/source/use_case/ad/usecase.cmake new file mode 100644 index 0000000..46e4101 --- /dev/null +++ b/source/use_case/ad/usecase.cmake @@ -0,0 +1,111 @@ +#---------------------------------------------------------------------------- +# Copyright (c) 2021 Arm Limited. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# 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. +#---------------------------------------------------------------------------- + +# If the path to a directory or source file has been defined, +# get the type here (FILEPATH or PATH): +if (DEFINED ${use_case}_FILE_PATH) + get_path_type(${${use_case}_FILE_PATH} PATH_TYPE) + + # Set the default type if path is not a dir or file path (or undefined) + if (NOT ${PATH_TYPE} STREQUAL PATH AND NOT ${PATH_TYPE} STREQUAL FILEPATH) + message(FATAL_ERROR "Invalid ${use_case}_FILE_PATH. It should be a dir or file path.") + endif() +else() + # Default is a directory path + set(PATH_TYPE PATH) +endif() + +message(STATUS "${use_case}_FILE_PATH is of type: ${PATH_TYPE}") + +USER_OPTION(${use_case}_FILE_PATH "Directory with custom WAV input files, or path to a single input WAV file, to use in the evaluation application." + ${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/samples/ + ${PATH_TYPE}) + +USER_OPTION(${use_case}_AUDIO_RATE "Specify the target sampling rate. Default is 16000." + 16000 + STRING) + +USER_OPTION(${use_case}_AUDIO_MONO "Specify if the audio needs to be converted to mono. Default is ON." + ON + BOOL) + +USER_OPTION(${use_case}_AUDIO_OFFSET "Specify the offset to start reading after this time (in seconds). Default is 0." + 0 + STRING) + +USER_OPTION(${use_case}_AUDIO_DURATION "Specify the audio duration to load (in seconds). If set to 0 the entire audio will be processed." + 0 + STRING) + +USER_OPTION(${use_case}_AUDIO_RES_TYPE "Specify re-sampling algorithm to use. By default is 'kaiser_best'." + kaiser_best + STRING) + +USER_OPTION(${use_case}_AUDIO_MIN_SAMPLES "Specify the minimum number of samples to use. By default is amount needed to do one inference, + if the audio is shorter then it will be automatically padded." + 33280 + STRING) + +USER_OPTION(${use_case}_MODEL_SCORE_THRESHOLD "Specify the score threshold for a result to be deemed anomalous." + -0.8 + STRING) + +generate_audio_code(${${use_case}_FILE_PATH} ${SRC_GEN_DIR} ${INC_GEN_DIR} + ${${use_case}_AUDIO_RATE} + ${${use_case}_AUDIO_MONO} + ${${use_case}_AUDIO_OFFSET} + ${${use_case}_AUDIO_DURATION} + ${${use_case}_AUDIO_RES_TYPE} + ${${use_case}_AUDIO_MIN_SAMPLES}) + +USER_OPTION(${use_case}_ACTIVATION_BUF_SZ "Activation buffer size for the chosen model" + 0x00200000 + STRING) + +# If there is no tflite file pointed to +if (NOT DEFINED ${use_case}_MODEL_TFLITE_PATH) + + set(MODEL_RESOURCES_DIR ${DOWNLOAD_DEP_DIR}/${use_case}) + file(MAKE_DIRECTORY ${MODEL_RESOURCES_DIR}) + set(MODEL_FILENAME ad_med_nov11_int8.tflite) + set(DEFAULT_MODEL_PATH ${MODEL_RESOURCES_DIR}/${MODEL_FILENAME}) + + # TODO: Download the model here for this use case when available on Model Zoo. + # For now we write a place holder file. + file(WRITE ${DEFAULT_MODEL_PATH} "Placeholder") +else() + set(DEFAULT_MODEL_PATH "N/A") +endif() + +set(EXTRA_MODEL_CODE + "/* Model parameters for ${use_case} */" + "extern const int g_FrameLength = 1024" + "extern const int g_FrameStride = 512" + "extern const float g_ScoreThreshold = ${${use_case}_MODEL_SCORE_THRESHOLD}" + "extern const float g_TrainingMean = -30" + ) + +USER_OPTION(${use_case}_MODEL_TFLITE_PATH "NN models file to be used in the evaluation application. Model files must be in tflite format." + ${DEFAULT_MODEL_PATH} + FILEPATH) + +# Generate model file +generate_tflite_code( + MODEL_PATH ${${use_case}_MODEL_TFLITE_PATH} + DESTINATION ${SRC_GEN_DIR} + EXPRESSIONS ${EXTRA_MODEL_CODE} +) |