diff options
Diffstat (limited to 'source/application/main')
-rw-r--r-- | source/application/main/Classifier.cc | 7 | ||||
-rw-r--r-- | source/application/main/Main.cc | 1 | ||||
-rw-r--r-- | source/application/main/Mfcc.cc | 4 | ||||
-rw-r--r-- | source/application/main/PlatformMath.cc | 315 | ||||
-rw-r--r-- | source/application/main/UseCaseCommonUtils.cc | 12 | ||||
-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/PlatformMath.hpp | 176 | ||||
-rw-r--r-- | source/application/main/include/UseCaseCommonUtils.hpp | 10 |
9 files changed, 21 insertions, 510 deletions
diff --git a/source/application/main/Classifier.cc b/source/application/main/Classifier.cc index a6ff532..6fabebe 100644 --- a/source/application/main/Classifier.cc +++ b/source/application/main/Classifier.cc @@ -16,15 +16,16 @@ */ #include "Classifier.hpp" -#include "hal.h" #include "TensorFlowLiteMicro.hpp" +#include "PlatformMath.hpp" +#include "log_macros.h" #include <vector> #include <string> #include <set> #include <cstdint> -#include <inttypes.h> -#include "PlatformMath.hpp" +#include <cinttypes> + namespace arm { namespace app { diff --git a/source/application/main/Main.cc b/source/application/main/Main.cc index 9622566..3a1c110 100644 --- a/source/application/main/Main.cc +++ b/source/application/main/Main.cc @@ -20,6 +20,7 @@ \****************************************************************************/ #include "hal.h" /* our hardware abstraction api */ +#include "log_macros.h" #include "TensorFlowLiteMicro.hpp" /* our inference logic api */ #include <cstdio> diff --git a/source/application/main/Mfcc.cc b/source/application/main/Mfcc.cc index 2d697ee..3bf5eb3 100644 --- a/source/application/main/Mfcc.cc +++ b/source/application/main/Mfcc.cc @@ -15,11 +15,11 @@ * limitations under the License. */ #include "Mfcc.hpp" - #include "PlatformMath.hpp" +#include "log_macros.h" #include <cfloat> -#include <inttypes.h> +#include <cinttypes> namespace arm { namespace app { diff --git a/source/application/main/PlatformMath.cc b/source/application/main/PlatformMath.cc deleted file mode 100644 index 26b4b72..0000000 --- a/source/application/main/PlatformMath.cc +++ /dev/null @@ -1,315 +0,0 @@ -/* - * 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 "PlatformMath.hpp" -#include <algorithm> -#include <numeric> - -#if 0 == ARM_DSP_AVAILABLE - #include <cmath> - #include <numeric> -#endif /* 0 == ARM_DSP_AVAILABLE */ - -namespace arm { -namespace app { -namespace math { - - float MathUtils::CosineF32(float radians) - { -#if ARM_DSP_AVAILABLE - return arm_cos_f32(radians); -#else /* ARM_DSP_AVAILABLE */ - return cos(radians); -#endif /* ARM_DSP_AVAILABLE */ - } - - float MathUtils::SineF32(float radians) - { -#if ARM_DSP_AVAILABLE - return arm_sin_f32(radians); -#else /* ARM_DSP_AVAILABLE */ - return sin(radians); -#endif /* ARM_DSP_AVAILABLE */ - } - - float MathUtils::SqrtF32(float input) - { -#if ARM_DSP_AVAILABLE - float output = 0.f; - arm_sqrt_f32(input, &output); - return output; -#else /* ARM_DSP_AVAILABLE */ - return sqrtf(input); -#endif /* ARM_DSP_AVAILABLE */ - } - - float MathUtils::MeanF32(float* ptrSrc, const uint32_t srcLen) - { - if (!srcLen) { - return 0.f; - } - -#if ARM_DSP_AVAILABLE - float result = 0.f; - arm_mean_f32(ptrSrc, srcLen, &result); - return result; -#else /* ARM_DSP_AVAILABLE */ - float acc = std::accumulate(ptrSrc, ptrSrc + srcLen, 0.0); - return acc/srcLen; -#endif /* ARM_DSP_AVAILABLE */ - } - - float MathUtils::StdDevF32(float* ptrSrc, const uint32_t srcLen, - const float mean) - { - if (!srcLen) { - return 0.f; - } -#if ARM_DSP_AVAILABLE - /** - * Note Standard deviation calculation can be off - * by > 0.01 but less than < 0.1, according to - * preliminary findings. - **/ - UNUSED(mean); - float stdDev = 0; - arm_std_f32(ptrSrc, srcLen, &stdDev); - return stdDev; -#else /* ARM_DSP_AVAILABLE */ - auto VarianceFunction = [=](float acc, const float value) { - return acc + (((value - mean) * (value - mean))/ srcLen); - }; - - float acc = std::accumulate(ptrSrc, ptrSrc + srcLen, 0.0, - VarianceFunction); - - return sqrtf(acc); -#endif /* ARM_DSP_AVAILABLE */ - } - - void MathUtils::FftInitF32(const uint16_t fftLen, - FftInstance& fftInstance, - const FftType type) - { - fftInstance.m_fftLen = fftLen; - fftInstance.m_initialised = false; - fftInstance.m_optimisedOptionAvailable = false; - fftInstance.m_type = type; - -#if ARM_DSP_AVAILABLE - arm_status status = ARM_MATH_ARGUMENT_ERROR; - switch (fftInstance.m_type) { - case FftType::real: - status = arm_rfft_fast_init_f32(&fftInstance.m_instanceReal, fftLen); - break; - - case FftType::complex: - status = arm_cfft_init_f32(&fftInstance.m_instanceComplex, fftLen); - break; - - default: - printf_err("Invalid FFT type\n"); - return; - } - - if (ARM_MATH_SUCCESS != status) { - printf_err("Failed to initialise FFT for len %d\n", fftLen); - } else { - fftInstance.m_optimisedOptionAvailable = true; - } -#endif /* ARM_DSP_AVAILABLE */ - - if (!fftInstance.m_optimisedOptionAvailable) { - debug("Non optimised FFT will be used\n."); - } - - fftInstance.m_initialised = true; - } - - static void FftRealF32(std::vector<float>& input, - std::vector<float>& fftOutput) - { - const size_t inputLength = input.size(); - const size_t halfLength = input.size() / 2; - - fftOutput[0] = 0; - fftOutput[1] = 0; - for (size_t t = 0; t < inputLength; t++) { - fftOutput[0] += input[t]; - fftOutput[1] += input[t] * - MathUtils::CosineF32(2 * M_PI * halfLength * t / inputLength); - } - - for (size_t k = 1, j = 2; k < halfLength; ++k, j += 2) { - float sumReal = 0; - float sumImag = 0; - - const float theta = static_cast<float>(2 * M_PI * k / inputLength); - - for (size_t t = 0; t < inputLength; t++) { - const auto angle = static_cast<float>(t * theta); - sumReal += input[t] * MathUtils::CosineF32(angle); - sumImag += -input[t]* MathUtils::SineF32(angle); - } - - /* Arrange output to [real0, realN/2, real1, im1, real2, im2, ...] */ - fftOutput[j] = sumReal; - fftOutput[j + 1] = sumImag; - } - } - - static void FftComplexF32(std::vector<float>& input, - std::vector<float>& fftOutput) - { - const size_t fftLen = input.size() / 2; - for (size_t k = 0; k < fftLen; k++) { - float sumReal = 0; - float sumImag = 0; - const auto theta = static_cast<float>(2 * M_PI * k / fftLen); - for (size_t t = 0; t < fftLen; t++) { - const auto angle = theta * t; - const auto cosine = MathUtils::CosineF32(angle); - const auto sine = MathUtils::SineF32(angle); - sumReal += input[t*2] * cosine + input[t*2 + 1] * sine; - sumImag += -input[t*2] * sine + input[t*2 + 1] * cosine; - } - fftOutput[k*2] = sumReal; - fftOutput[k*2 + 1] = sumImag; - } - } - - void MathUtils::FftF32(std::vector<float>& input, - std::vector<float>& fftOutput, - arm::app::math::FftInstance& fftInstance) - { - if (!fftInstance.m_initialised) { - printf_err("FFT uninitialised\n"); - return; - } else if (input.size() < fftInstance.m_fftLen) { - printf_err("FFT len: %" PRIu16 "; input len: %zu\n", - fftInstance.m_fftLen, input.size()); - return; - } else if (fftOutput.size() < input.size()) { - printf_err("Output vector len insufficient to hold FFTs\n"); - return; - } - - switch (fftInstance.m_type) { - case FftType::real: - -#if ARM_DSP_AVAILABLE - if (fftInstance.m_optimisedOptionAvailable) { - arm_rfft_fast_f32(&fftInstance.m_instanceReal, input.data(), fftOutput.data(), 0); - return; - } -#endif /* ARM_DSP_AVAILABLE */ - FftRealF32(input, fftOutput); - return; - - case FftType::complex: - if (input.size() < fftInstance.m_fftLen * 2) { - printf_err("Complex FFT instance should have input size >= (FFT len x 2)"); - return; - } -#if ARM_DSP_AVAILABLE - if (fftInstance.m_optimisedOptionAvailable) { - fftOutput = input; /* Complex function works in-place */ - arm_cfft_f32(&fftInstance.m_instanceComplex, fftOutput.data(), 0, 1); - return; - } -#endif /* ARM_DSP_AVAILABLE */ - FftComplexF32(input, fftOutput); - return; - - default: - printf_err("Invalid FFT type\n"); - return; - } - } - - void MathUtils::VecLogarithmF32(std::vector <float>& input, - std::vector <float>& output) - { -#if ARM_DSP_AVAILABLE - arm_vlog_f32(input.data(), output.data(), - output.size()); -#else /* ARM_DSP_AVAILABLE */ - for (auto in = input.begin(), out = output.begin(); - in != input.end() && out != output.end(); ++in, ++out) { - *out = logf(*in); - } -#endif /* ARM_DSP_AVAILABLE */ - } - - float MathUtils::DotProductF32(float* srcPtrA, float* srcPtrB, - const uint32_t srcLen) - { - float output = 0.f; - -#if ARM_DSP_AVAILABLE - arm_dot_prod_f32(srcPtrA, srcPtrB, srcLen, &output); -#else /* ARM_DSP_AVAILABLE */ - for (uint32_t i = 0; i < srcLen; ++i) { - output += *srcPtrA++ * *srcPtrB++; - } -#endif /* ARM_DSP_AVAILABLE */ - - return output; - } - - bool MathUtils::ComplexMagnitudeSquaredF32(float* ptrSrc, - const uint32_t srcLen, - float* ptrDst, - const uint32_t dstLen) - { - if (dstLen < srcLen/2) { - printf_err("dstLen must be greater than srcLen/2"); - return false; - } - -#if ARM_DSP_AVAILABLE - arm_cmplx_mag_squared_f32(ptrSrc, ptrDst, srcLen/2); -#else /* ARM_DSP_AVAILABLE */ - for (uint32_t j = 0; j < srcLen/2; ++j) { - const float real = *ptrSrc++; - const float im = *ptrSrc++; - *ptrDst++ = real*real + im*im; - } -#endif /* ARM_DSP_AVAILABLE */ - return true; - } - - void MathUtils::SoftmaxF32(std::vector<float>& vec) - { - /* Fix for numerical stability and apply exp. */ - auto start = vec.begin(); - auto end = vec.end(); - - 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; - } - } - -} /* namespace math */ -} /* namespace app */ -} /* namespace arm */ diff --git a/source/application/main/UseCaseCommonUtils.cc b/source/application/main/UseCaseCommonUtils.cc index 67e784b..dd9a32d 100644 --- a/source/application/main/UseCaseCommonUtils.cc +++ b/source/application/main/UseCaseCommonUtils.cc @@ -16,7 +16,9 @@ */ #include "UseCaseCommonUtils.hpp" #include "InputFiles.hpp" -#include <inttypes.h> +#include "log_macros.h" + +#include <cinttypes> void DisplayCommonMenu() @@ -74,7 +76,7 @@ bool image::PresentInferenceResult( platform.data_psn->present_data_text( resultStr.c_str(), resultStr.size(), - dataPsnTxtStartX1, rowIdx1, 0); + dataPsnTxtStartX1, rowIdx1, false); rowIdx1 += dataPsnTxtYIncr; resultStr = std::to_string(i + 1) + ") " + results[i].m_label; @@ -105,7 +107,7 @@ void image::RgbToGrayscale(const uint8_t *srcPtr, uint8_t *dstPtr, const size_t } } -void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase) +void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase) { #if NUMBER_OF_FILES > 0 auto curImIdx = ctx.Get<uint32_t>(useCase); @@ -122,7 +124,7 @@ void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCas #endif /* NUMBER_OF_FILES > 0 */ } -bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName) +bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName) { #if NUMBER_OF_FILES > 0 if (idx >= NUMBER_OF_FILES) { @@ -192,7 +194,7 @@ void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements } const uint32_t tensorSz = tensor->bytes; - const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor); + const auto* tensorData = tflite::GetTensorData<uint8_t>(tensor); DumpTensorData(tensorData, tensorSz, lineBreakForNumElements); } diff --git a/source/application/main/include/Classifier.hpp b/source/application/main/include/Classifier.hpp index d899e8e..d641c22 100644 --- a/source/application/main/include/Classifier.hpp +++ b/source/application/main/include/Classifier.hpp @@ -50,7 +50,7 @@ namespace app { TfLiteTensor* outputTensor, std::vector<ClassificationResult>& vecResults, const std::vector <std::string>& labels, uint32_t topNCount, - bool use_softmax = false); + bool use_softmax); /** * @brief Populate the elements of the Classification Result object. diff --git a/source/application/main/include/DataStructures.hpp b/source/application/main/include/DataStructures.hpp index d369cb6..0616839 100644 --- a/source/application/main/include/DataStructures.hpp +++ b/source/application/main/include/DataStructures.hpp @@ -17,8 +17,6 @@ #ifndef DATA_STRUCTURES_HPP #define DATA_STRUCTURES_HPP -#include "hal.h" - #include <iterator> namespace arm { @@ -50,7 +48,7 @@ namespace app { Array2d(unsigned rows, unsigned cols): m_rows(rows), m_cols(cols) { if (rows == 0 || cols == 0) { - printf_err("Array2d constructor has 0 size.\n"); + printf("Array2d constructor has 0 size.\n"); m_data = nullptr; return; } diff --git a/source/application/main/include/PlatformMath.hpp b/source/application/main/include/PlatformMath.hpp deleted file mode 100644 index fdb51b2..0000000 --- a/source/application/main/include/PlatformMath.hpp +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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 PLATFORM_MATH_HPP -#define PLATFORM_MATH_HPP - -#include "hal.h" - -/* See if ARM DSP functions can be used. */ -#if PLATFORM_HAL == PLATFORM_CORTEX_M_BAREMETAL - #if defined(__DSP_PRESENT) && (__DSP_PRESENT == 1U) - - #define ARM_DSP_AVAILABLE (1U) - #include "arm_math.h" - #define M_PI (PI) - - #endif /* defined(__DSP_PRESENT) && (__DSP_PRESENT == 1U) */ -#endif /* PLATFORM_HAL == PLATFORM_CORTEX_M_BAREMETAL */ - -#include <vector> - -namespace arm { -namespace app { -namespace math { - - enum class FftType { - real = 0, - complex = 1 - }; - - struct FftInstance { -#if ARM_DSP_AVAILABLE - arm_rfft_fast_instance_f32 m_instanceReal; - arm_cfft_instance_f32 m_instanceComplex; -#endif - uint16_t m_fftLen{0}; - FftType m_type; - bool m_optimisedOptionAvailable{false}; - bool m_initialised{false}; - }; - - /* Class to provide Math functions like FFT, mean, stddev etc. - * This will allow other classes, functions to be independent of - * #if definition checks and provide a cleaner API. Also, it will - * consolidate all arm math functions used in one place and make - * them easier to test. */ - class MathUtils { - - public: - /** - * @brief Get the cosine value of the argument in floating point. - * @param[in] radians Angle in radians. - * @return Cosine value (floating point). - */ - static float CosineF32(float radians); - - /** - * @brief Get the sine value of the argument in floating point. - * @param[in] radians Angle in radians. - * @return Sine value (floating point). - */ - static float SineF32(float radians); - - /** - * @brief Get the square root of the argument in floating point. - * @param[in] input Value to compute square root of. - * @return Square root (floating point) value. - */ - static float SqrtF32(float input); - - /** - * @brief Gets the mean of a floating point array of elements. - * @param[in] ptrSrc Pointer to the first element. - * @param[in] srcLen Number of elements in the array/vector. - * @return Average value. - */ - static float MeanF32(float* ptrSrc, uint32_t srcLen); - - /** - * @brief Gets the standard deviation of a floating point array - * of elements. - * @param[in] ptrSrc Pointer to the first element. - * @param[in] srcLen Number of elements in the array/vector. - * @param[in] mean Pre-computed mean value. - * @return Standard deviation value. - */ - static float StdDevF32(float* ptrSrc, uint32_t srcLen, - float mean); - - /** - * @brief Initialises the internal FFT structures (if available - * for the platform). This function should be called - * prior to Fft32 function call if built with ARM DSP functions. - * @param[in] fftLen Requested length of the FFT. - * @param[in] fftInstance FFT instance struct to use. - * @param[in] type FFT type (real or complex) - */ - static void FftInitF32(const uint16_t fftLen, - FftInstance& fftInstance, - const FftType type = FftType::real); - - /** - * @brief Computes the FFT for the input vector. - * @param[in] input Floating point vector of input elements - * @param[out] fftOutput Output buffer to be populated by computed FFTs. - * @param[in] fftInstance FFT instance struct to use. - */ - static void FftF32(std::vector<float>& input, - std::vector<float>& fftOutput, - FftInstance& fftInstance); - - /** - * @brief Computes the natural logarithms of input floating point - * vector - * @param[in] input Floating point input vector - * @param[out] output Pre-allocated buffer to be populated with - * natural log values of each input element. - */ - static void VecLogarithmF32(std::vector <float>& input, - std::vector <float>& output); - - /** - * @brief Computes the dot product of two 1D floating point - * vectors. - * result = sum(srcA[0]*srcB[0] + srcA[1]*srcB[1] + ..) - * @param[in] srcPtrA Pointer to the first element of first - * array. - * @param[in] srcPtrB Pointer to the first element of second - * array. - * @param[in] srcLen Number of elements in the array/vector. - * @return Dot product. - */ - static float DotProductF32(float* srcPtrA, float* srcPtrB, - const uint32_t srcLen); - - /** - * @brief Computes the squared magnitude of floating point - * complex number array. - * @param[in] ptrSrc Pointer to the first element of input - * array. - * @param[in] srcLen Number of elements in the array/vector. - * @param[out] ptrDst Output buffer to be populated. - * @param[in] dstLen Output buffer len (for sanity check only). - * @return true if successful, false otherwise. - */ - static bool ComplexMagnitudeSquaredF32(float* ptrSrc, - const uint32_t srcLen, - float* ptrDst, - const uint32_t dstLen); - - /** - * @brief Scales output scores for an arbitrary number of classes so - * that they sum to 1, allowing output to be expressed as a probability. - * @param[in] vector Vector of floats modified in-place - */ - static void SoftmaxF32(std::vector<float>& vec); - }; - -} /* namespace math */ -} /* namespace app */ -} /* namespace arm */ - -#endif /* PLATFORM_MATH_HPP */
\ No newline at end of file diff --git a/source/application/main/include/UseCaseCommonUtils.hpp b/source/application/main/include/UseCaseCommonUtils.hpp index 84b5de3..cd0cb69 100644 --- a/source/application/main/include/UseCaseCommonUtils.hpp +++ b/source/application/main/include/UseCaseCommonUtils.hpp @@ -24,7 +24,7 @@ #include "UseCaseHandler.hpp" /* Handlers for different user options. */ #include "Classifier.hpp" /* Classifier. */ #include "InputFiles.hpp" -#include <inttypes.h> +#include <cinttypes> /* Helper macro to convert RGB888 to RGB565 format. */ @@ -33,8 +33,8 @@ ((B8>>3) & 0x1F)) constexpr uint16_t COLOR_BLACK = 0; -constexpr uint16_t COLOR_GREEN = RGB888_TO_RGB565( 0, 255, 0); // 2016; -constexpr uint16_t COLOR_YELLOW = RGB888_TO_RGB565(255, 255, 0); // 65504; +constexpr uint16_t COLOR_GREEN = RGB888_TO_RGB565( 0u, 255u, 0u); // 2016; +constexpr uint16_t COLOR_YELLOW = RGB888_TO_RGB565(255u, 255u, 0u); // 65504; void DisplayCommonMenu(); @@ -72,7 +72,7 @@ namespace image{ * @param[in,out] ctx Pointer to the application context object. * @param[in] useCase Use case name **/ -void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase); +void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase); /** * @brief Helper function to set the input feature map index. @@ -81,7 +81,7 @@ void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCas * @param[in] ctxIfmName Input Feature Map name * @return true if index is set, false otherwise. **/ -bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName); +bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName); namespace common { |