diff options
author | alexander <alexander.efremov@arm.com> | 2021-04-29 20:36:09 +0100 |
---|---|---|
committer | Alexander Efremov <alexander.efremov@arm.com> | 2021-05-04 19:57:44 +0000 |
commit | c350cdced0a8a2ca17376f58813e6d48d796ac7c (patch) | |
tree | f732cde664837a7cb9429b17e1366bb31a635b15 /source/application/main | |
parent | 6448932cc1c612d78e62c778ebb228b3cbe96a58 (diff) | |
download | ml-embedded-evaluation-kit-c350cdced0a8a2ca17376f58813e6d48d796ac7c.tar.gz |
MLECO-1868: Code static analyzer warnings fixes
Signed-off-by: alexander <alexander.efremov@arm.com>
Change-Id: Ie423e9cad3fabec6ab077ded7236813fe4933dea
Diffstat (limited to 'source/application/main')
-rw-r--r-- | source/application/main/Classifier.cc | 117 | ||||
-rw-r--r-- | source/application/main/Mfcc.cc | 61 | ||||
-rw-r--r-- | source/application/main/PlatformMath.cc | 4 | ||||
-rw-r--r-- | source/application/main/Profiler.cc | 6 | ||||
-rw-r--r-- | source/application/main/include/Classifier.hpp | 2 | ||||
-rw-r--r-- | source/application/main/include/DataStructures.hpp | 4 | ||||
-rw-r--r-- | source/application/main/include/Mfcc.hpp | 24 | ||||
-rw-r--r-- | source/application/main/include/Profiler.hpp | 4 |
8 files changed, 99 insertions, 123 deletions
diff --git a/source/application/main/Classifier.cc b/source/application/main/Classifier.cc index bc2c378..9a47f3d 100644 --- a/source/application/main/Classifier.cc +++ b/source/application/main/Classifier.cc @@ -28,69 +28,52 @@ namespace arm { namespace app { template<typename T> - bool Classifier::_GetTopNResults(TfLiteTensor* tensor, - std::vector<ClassificationResult>& vecResults, - uint32_t topNCount, - const std::vector <std::string>& labels) - { - std::set<std::pair<T, uint32_t>> sortedSet; - - /* NOTE: inputVec's size verification against labels should be - * checked by the calling/public function. */ - T* tensorData = tflite::GetTensorData<T>(tensor); - - /* Set initial elements. */ - for (uint32_t i = 0; i < topNCount; ++i) { - sortedSet.insert({tensorData[i], i}); - } - - /* Initialise iterator. */ - auto setFwdIter = sortedSet.begin(); - - /* Scan through the rest of elements with compare operations. */ - for (uint32_t i = topNCount; i < labels.size(); ++i) { - if (setFwdIter->first < tensorData[i]) { - sortedSet.erase(*setFwdIter); - sortedSet.insert({tensorData[i], i}); - setFwdIter = sortedSet.begin(); - } - } - - /* Final results' container. */ - vecResults = std::vector<ClassificationResult>(topNCount); + void SetVectorResults(std::set<std::pair<T, uint32_t>>& topNSet, + std::vector<ClassificationResult>& vecResults, + TfLiteTensor* tensor, + const std::vector <std::string>& labels) { /* For getting the floating point values, we need quantization parameters. */ QuantParams quantParams = GetTensorQuantParams(tensor); /* Reset the iterator to the largest element - use reverse iterator. */ - auto setRevIter = sortedSet.rbegin(); - - /* Populate results - * Note: we could combine this loop with the loop above, but that - * would, involve more multiplications and other operations. - **/ - for (size_t i = 0; i < vecResults.size(); ++i, ++setRevIter) { - double score = static_cast<int> (setRevIter->first); - vecResults[i].m_normalisedVal = quantParams.scale * - (score - quantParams.offset); - vecResults[i].m_label = labels[setRevIter->second]; - vecResults[i].m_labelIdx = setRevIter->second; + auto topNIter = topNSet.rbegin(); + for (size_t i = 0; i < vecResults.size() && topNIter != topNSet.rend(); ++i, ++topNIter) { + T score = topNIter->first; + vecResults[i].m_normalisedVal = quantParams.scale * (score - quantParams.offset); + vecResults[i].m_label = labels[topNIter->second]; + vecResults[i].m_labelIdx = topNIter->second; } - return true; } template<> - bool Classifier::_GetTopNResults<float>(TfLiteTensor* tensor, - std::vector<ClassificationResult>& vecResults, - uint32_t topNCount, - const std::vector <std::string>& labels) + void SetVectorResults<float>(std::set<std::pair<float, uint32_t>>& topNSet, + std::vector<ClassificationResult>& vecResults, + TfLiteTensor* tensor, + const std::vector <std::string>& labels) { + UNUSED(tensor); + /* Reset the iterator to the largest element - use reverse iterator. */ + auto topNIter = topNSet.rbegin(); + for (size_t i = 0; i < vecResults.size() && topNIter != topNSet.rend(); ++i, ++topNIter) { + vecResults[i].m_normalisedVal = topNIter->first; + vecResults[i].m_label = labels[topNIter->second]; + vecResults[i].m_labelIdx = topNIter->second; + } + + } + + template<typename T> + bool Classifier::GetTopNResults(TfLiteTensor* tensor, + std::vector<ClassificationResult>& vecResults, + uint32_t topNCount, + const std::vector <std::string>& labels) { - std::set<std::pair<float, uint32_t>> sortedSet; + std::set<std::pair<T, uint32_t>> sortedSet; /* NOTE: inputVec's size verification against labels should be * checked by the calling/public function. */ - float* tensorData = tflite::GetTensorData<float>(tensor); + T* tensorData = tflite::GetTensorData<T>(tensor); /* Set initial elements. */ for (uint32_t i = 0; i < topNCount; ++i) { @@ -112,29 +95,18 @@ namespace app { /* Final results' container. */ vecResults = std::vector<ClassificationResult>(topNCount); - /* Reset the iterator to the largest element - use reverse iterator. */ - auto setRevIter = sortedSet.rbegin(); - - /* Populate results - * Note: we could combine this loop with the loop above, but that - * would, involve more multiplications and other operations. - **/ - for (size_t i = 0; i < vecResults.size(); ++i, ++setRevIter) { - vecResults[i].m_normalisedVal = setRevIter->first; - vecResults[i].m_label = labels[setRevIter->second]; - vecResults[i].m_labelIdx = setRevIter->second; - } + SetVectorResults<T>(sortedSet, vecResults, tensor, labels); return true; } - template bool Classifier::_GetTopNResults<uint8_t>(TfLiteTensor* tensor, - std::vector<ClassificationResult>& vecResults, - uint32_t topNCount, const std::vector <std::string>& labels); + template bool Classifier::GetTopNResults<uint8_t>(TfLiteTensor* tensor, + std::vector<ClassificationResult>& vecResults, + uint32_t topNCount, const std::vector <std::string>& labels); - template bool Classifier::_GetTopNResults<int8_t>(TfLiteTensor* tensor, - std::vector<ClassificationResult>& vecResults, - uint32_t topNCount, const std::vector <std::string>& labels); + template bool Classifier::GetTopNResults<int8_t>(TfLiteTensor* tensor, + std::vector<ClassificationResult>& vecResults, + uint32_t topNCount, const std::vector <std::string>& labels); bool Classifier::GetClassificationResults( TfLiteTensor* outputTensor, @@ -158,6 +130,9 @@ namespace app { } else if (totalOutputSize != labels.size()) { printf_err("Output size doesn't match the labels' size\n"); return false; + } else if (topNCount == 0) { + printf_err("Top N results cannot be zero\n"); + return false; } bool resultState; @@ -166,13 +141,13 @@ namespace app { /* Get the top N results. */ switch (outputTensor->type) { case kTfLiteUInt8: - resultState = _GetTopNResults<uint8_t>(outputTensor, vecResults, topNCount, labels); + resultState = GetTopNResults<uint8_t>(outputTensor, vecResults, topNCount, labels); break; case kTfLiteInt8: - resultState = _GetTopNResults<int8_t>(outputTensor, vecResults, topNCount, labels); + resultState = GetTopNResults<int8_t>(outputTensor, vecResults, topNCount, labels); break; case kTfLiteFloat32: - resultState = _GetTopNResults<float>(outputTensor, vecResults, topNCount, labels); + resultState = GetTopNResults<float>(outputTensor, vecResults, topNCount, labels); break; default: printf_err("Tensor type %s not supported by classifier\n", TfLiteTypeGetName(outputTensor->type)); @@ -180,7 +155,7 @@ namespace app { } if (!resultState) { - printf_err("Failed to get sorted set\n"); + printf_err("Failed to get top N results set\n"); return false; } diff --git a/source/application/main/Mfcc.cc b/source/application/main/Mfcc.cc index bf16159..9ddcb5d 100644 --- a/source/application/main/Mfcc.cc +++ b/source/application/main/Mfcc.cc @@ -44,7 +44,7 @@ namespace audio { m_useHtkMethod(useHtkMethod) {} - std::string MfccParams::Str() + std::string MfccParams::Str() const { char strC[1024]; snprintf(strC, sizeof(strC) - 1, "\n \ @@ -74,7 +74,7 @@ namespace audio { 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; + const auto multiplier = static_cast<float>(2 * M_PI / this->_m_params.m_frameLen); /* Create window function. */ for (size_t i = 0; i < this->_m_params.m_frameLen; i++) { @@ -88,7 +88,7 @@ namespace audio { void MFCC::Init() { - this->_InitMelFilterBank(); + this->InitMelFilterBank(); } float MFCC::MelScale(const float freq, const bool useHTKMethod) @@ -126,8 +126,8 @@ namespace audio { bool MFCC::ApplyMelFilterBank( std::vector<float>& fftVec, std::vector<std::vector<float>>& melFilterBank, - std::vector<int32_t>& filterBankFilterFirst, - std::vector<int32_t>& filterBankFilterLast, + std::vector<uint32_t>& filterBankFilterFirst, + std::vector<uint32_t>& filterBankFilterLast, std::vector<float>& melEnergies) { const size_t numBanks = melEnergies.size(); @@ -140,11 +140,12 @@ namespace audio { for (size_t bin = 0; bin < numBanks; ++bin) { auto filterBankIter = melFilterBank[bin].begin(); + auto end = melFilterBank[bin].end(); float melEnergy = FLT_MIN; /* Avoid log of zero at later stages */ - int32_t firstIndex = filterBankFilterFirst[bin]; - int32_t lastIndex = filterBankFilterLast[bin]; + const uint32_t firstIndex = filterBankFilterFirst[bin]; + const uint32_t lastIndex = std::min<uint32_t>(filterBankFilterLast[bin], fftVec.size() - 1); - for (int i = firstIndex; i <= lastIndex; i++) { + for (uint32_t i = firstIndex; i <= lastIndex && filterBankIter != end; i++) { float energyRep = math::MathUtils::SqrtF32(fftVec[i]); melEnergy += (*filterBankIter++ * energyRep); } @@ -157,14 +158,14 @@ namespace audio { void MFCC::ConvertToLogarithmicScale(std::vector<float>& melEnergies) { - for (size_t bin = 0; bin < melEnergies.size(); ++bin) { - melEnergies[bin] = logf(melEnergies[bin]); + for (float& melEnergy : melEnergies) { + melEnergy = logf(melEnergy); } } - void MFCC::_ConvertToPowerSpectrum() + void MFCC::ConvertToPowerSpectrum() { - const uint32_t halfDim = this->_m_params.m_frameLenPadded / 2; + const uint32_t halfDim = this->_m_buffer.size() / 2; /* Handle this special case. */ float firstEnergy = this->_m_buffer[0] * this->_m_buffer[0]; @@ -193,7 +194,7 @@ namespace audio { for (int32_t k = 0, m = 0; k < coefficientCount; k++, m += inputLength) { for (int32_t n = 0; n < inputLength; n++) { dctMatix[m+n] = normalizer * - math::MathUtils::CosineF32((n + 0.5) * angle); + math::MathUtils::CosineF32((n + 0.5f) * angle); } angle += angleIncr; } @@ -214,10 +215,10 @@ namespace audio { return 1.f; } - void MFCC::_InitMelFilterBank() + void MFCC::InitMelFilterBank() { - if (!this->_IsMelFilterBankInited()) { - this->_m_melFilterBank = this->_CreateMelFilterBank(); + if (!this->IsMelFilterBankInited()) { + this->_m_melFilterBank = this->CreateMelFilterBank(); this->_m_dctMatrix = this->CreateDCTMatrix( this->_m_params.m_numFbankBins, this->_m_params.m_numMfccFeatures); @@ -225,17 +226,17 @@ namespace audio { } } - bool MFCC::_IsMelFilterBankInited() + bool MFCC::IsMelFilterBankInited() const { return this->_m_filterBankInitialised; } - void MFCC::_MfccComputePreFeature(const std::vector<int16_t>& audioData) + void MFCC::MfccComputePreFeature(const std::vector<int16_t>& audioData) { - this->_InitMelFilterBank(); + this->InitMelFilterBank(); /* TensorFlow way of normalizing .wav data to (-1, 1). */ - constexpr float normaliser = 1.0/(1<<15); + constexpr float normaliser = 1.0/(1u<<15u); for (size_t i = 0; i < this->_m_params.m_frameLen; i++) { this->_m_frame[i] = static_cast<float>(audioData[i]) * normaliser; } @@ -252,7 +253,7 @@ namespace audio { math::MathUtils::FftF32(this->_m_frame, this->_m_buffer, this->_m_fftInstance); /* Convert to power spectrum. */ - this->_ConvertToPowerSpectrum(); + this->ConvertToPowerSpectrum(); /* Apply mel filterbanks. */ if (!this->ApplyMelFilterBank(this->_m_buffer, @@ -269,7 +270,7 @@ namespace audio { std::vector<float> MFCC::MfccCompute(const std::vector<int16_t>& audioData) { - this->_MfccComputePreFeature(audioData); + this->MfccComputePreFeature(audioData); std::vector<float> mfccOut(this->_m_params.m_numMfccFeatures); @@ -288,7 +289,7 @@ namespace audio { return mfccOut; } - std::vector<std::vector<float>> MFCC::_CreateMelFilterBank() + std::vector<std::vector<float>> MFCC::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; @@ -303,17 +304,18 @@ namespace audio { std::vector<std::vector<float>> melFilterBank( this->_m_params.m_numFbankBins); this->_m_filterBankFilterFirst = - std::vector<int32_t>(this->_m_params.m_numFbankBins); + std::vector<uint32_t>(this->_m_params.m_numFbankBins); this->_m_filterBankFilterLast = - std::vector<int32_t>(this->_m_params.m_numFbankBins); + std::vector<uint32_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; + uint32_t firstIndex = 0; + uint32_t lastIndex = 0; + bool firstIndexFound = false; const float normaliser = this->GetMelFilterBankNormaliser(leftMel, rightMel, this->_m_params.m_useHtkMethod); for (size_t i = 0; i < numFftBins; i++) { @@ -330,8 +332,9 @@ namespace audio { } thisBin[i] = weight * normaliser; - if (firstIndex == -1) { + if (!firstIndexFound) { firstIndex = i; + firstIndexFound = true; } lastIndex = i; } @@ -341,7 +344,7 @@ namespace audio { this->_m_filterBankFilterLast[bin] = lastIndex; /* Copy the part we care about. */ - for (int32_t i = firstIndex; i <= lastIndex; i++) { + for (uint32_t i = firstIndex; i <= lastIndex; i++) { melFilterBank[bin].push_back(thisBin[i]); } } diff --git a/source/application/main/PlatformMath.cc b/source/application/main/PlatformMath.cc index a9f5049..9d18151 100644 --- a/source/application/main/PlatformMath.cc +++ b/source/application/main/PlatformMath.cc @@ -121,7 +121,7 @@ namespace math { float sumReal = 0, sumImag = 0; for (int t = 0; t < inputLength; t++) { - float angle = 2 * M_PI * t * k / inputLength; + auto angle = static_cast<float>(2 * M_PI * t * k / inputLength); sumReal += input[t] * cosf(angle); sumImag += -input[t] * sinf(angle); } @@ -147,7 +147,7 @@ namespace math { output.size()); #else /* ARM_DSP_AVAILABLE */ for (auto in = input.begin(), out = output.begin(); - in != input.end(); ++in, ++out) { + in != input.end() && out != output.end(); ++in, ++out) { *out = logf(*in); } #endif /* ARM_DSP_AVAILABLE */ diff --git a/source/application/main/Profiler.cc b/source/application/main/Profiler.cc index 0456ba4..10a828a 100644 --- a/source/application/main/Profiler.cc +++ b/source/application/main/Profiler.cc @@ -54,7 +54,7 @@ namespace app { this->_m_tstampEnd = this->_m_pPlatform->timer->stop_profiling(); this->_m_started = false; - this->_AddProfilingUnit(this->_m_tstampSt, this->_m_tstampEnd, this->_m_name); + this->AddProfilingUnit(this->_m_tstampSt, this->_m_tstampEnd, this->_m_name); return true; } @@ -238,8 +238,8 @@ namespace app { this->_m_name = std::string(str); } - void Profiler::_AddProfilingUnit(time_counter start, time_counter end, - const std::string& name) + void Profiler::AddProfilingUnit(time_counter start, time_counter end, + const std::string& name) { platform_timer * timer = this->_m_pPlatform->timer; diff --git a/source/application/main/include/Classifier.hpp b/source/application/main/include/Classifier.hpp index 510e6f9..3ee3148 100644 --- a/source/application/main/include/Classifier.hpp +++ b/source/application/main/include/Classifier.hpp @@ -62,7 +62,7 @@ namespace app { * @return true if successful, false otherwise. **/ template<typename T> - bool _GetTopNResults(TfLiteTensor* tensor, + bool GetTopNResults(TfLiteTensor* tensor, std::vector<ClassificationResult>& vecResults, uint32_t topNCount, const std::vector <std::string>& labels); diff --git a/source/application/main/include/DataStructures.hpp b/source/application/main/include/DataStructures.hpp index 5cc8b5e..2f267c0 100644 --- a/source/application/main/include/DataStructures.hpp +++ b/source/application/main/include/DataStructures.hpp @@ -47,15 +47,13 @@ namespace app { * @param[in] rows Number of rows. * @param[in] cols Number of columns. */ - Array2d(unsigned rows, unsigned cols) + Array2d(unsigned rows, unsigned cols): _m_rows(rows), _m_cols(cols) { if (rows == 0 || cols == 0) { printf_err("Array2d constructor has 0 size.\n"); _m_data = nullptr; return; } - _m_rows = rows; - _m_cols = cols; _m_data = new T[rows * cols]; } diff --git a/source/application/main/include/Mfcc.hpp b/source/application/main/include/Mfcc.hpp index 6379fab..dcafe62 100644 --- a/source/application/main/include/Mfcc.hpp +++ b/source/application/main/include/Mfcc.hpp @@ -52,7 +52,7 @@ namespace audio { ~MfccParams() = default; /** @brief String representation of parameters */ - std::string Str(); + std::string Str() const; }; /** @@ -100,7 +100,7 @@ namespace audio { const float quantScale, const int quantOffset) { - this->_MfccComputePreFeature(audioData); + this->MfccComputePreFeature(audioData); float minVal = std::numeric_limits<T>::min(); float maxVal = std::numeric_limits<T>::max(); @@ -154,7 +154,7 @@ namespace audio { * 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). + * 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 @@ -168,8 +168,8 @@ namespace audio { 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<uint32_t>& filterBankFilterFirst, + std::vector<uint32_t>& filterBankFilterLast, std::vector<float>& melEnergies); /** @@ -214,37 +214,37 @@ namespace audio { std::vector<float> _m_windowFunc; std::vector<std::vector<float>> _m_melFilterBank; std::vector<float> _m_dctMatrix; - std::vector<int32_t> _m_filterBankFilterFirst; - std::vector<int32_t> _m_filterBankFilterLast; + std::vector<uint32_t> _m_filterBankFilterFirst; + std::vector<uint32_t> _m_filterBankFilterLast; bool _m_filterBankInitialised; arm::app::math::FftInstance _m_fftInstance; /** * @brief Initialises the filter banks and the DCT matrix. **/ - void _InitMelFilterBank(); + void InitMelFilterBank(); /** * @brief Signals whether the instance of MFCC has had its * required buffers initialised. * @return true if initialised, false otherwise. **/ - bool _IsMelFilterBankInited(); + bool IsMelFilterBankInited() const; /** * @brief Create mel filter banks for MFCC calculation. * @return 2D vector of floats. **/ - std::vector<std::vector<float>> _CreateMelFilterBank(); + std::vector<std::vector<float>> CreateMelFilterBank(); /** * @brief Computes and populates internal memeber buffers used * in MFCC feature calculation * @param[in] audioData 1D vector of 16-bit audio data. */ - void _MfccComputePreFeature(const std::vector<int16_t>& audioData); + void MfccComputePreFeature(const std::vector<int16_t>& audioData); /** @brief Computes the magnitude from an interleaved complex array. */ - void _ConvertToPowerSpectrum(); + void ConvertToPowerSpectrum(); }; diff --git a/source/application/main/include/Profiler.hpp b/source/application/main/include/Profiler.hpp index d93b257..c5f77e7 100644 --- a/source/application/main/include/Profiler.hpp +++ b/source/application/main/include/Profiler.hpp @@ -125,8 +125,8 @@ namespace app { * @param[in] name Name for the profiling unit series to be * appended to. **/ - void _AddProfilingUnit(time_counter start, time_counter end, - const std::string& name); + void AddProfilingUnit(time_counter start, time_counter end, + const std::string& name); }; } /* namespace app */ |