summaryrefslogtreecommitdiff
path: root/source/application/main/Classifier.cc
diff options
context:
space:
mode:
authorKshitij Sisodia <kshitij.sisodia@arm.com>2022-05-06 09:13:03 +0100
committerKshitij Sisodia <kshitij.sisodia@arm.com>2022-05-06 17:11:41 +0100
commitaa4bcb14d0cbee910331545dd2fc086b58c37170 (patch)
treee67a43a43f61c6f8b6aad19018b0827baf7e31a6 /source/application/main/Classifier.cc
parentfcca863bafd5f33522bc14c23dde4540e264ec94 (diff)
downloadml-embedded-evaluation-kit-aa4bcb14d0cbee910331545dd2fc086b58c37170.tar.gz
MLECO-3183: Refactoring application sources
Platform agnostic application sources are moved into application api module with their own independent CMake projects. Changes for MLECO-3080 also included - they create CMake projects individial API's (again, platform agnostic) that dependent on the common logic. The API for KWS_API "joint" API has been removed and now the use case relies on individual KWS, and ASR API libraries. Change-Id: I1f7748dc767abb3904634a04e0991b74ac7b756d Signed-off-by: Kshitij Sisodia <kshitij.sisodia@arm.com>
Diffstat (limited to 'source/application/main/Classifier.cc')
-rw-r--r--source/application/main/Classifier.cc169
1 files changed, 0 insertions, 169 deletions
diff --git a/source/application/main/Classifier.cc b/source/application/main/Classifier.cc
deleted file mode 100644
index 6fabebe..0000000
--- a/source/application/main/Classifier.cc
+++ /dev/null
@@ -1,169 +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 "Classifier.hpp"
-
-#include "TensorFlowLiteMicro.hpp"
-#include "PlatformMath.hpp"
-#include "log_macros.h"
-
-#include <vector>
-#include <string>
-#include <set>
-#include <cstdint>
-#include <cinttypes>
-
-
-namespace arm {
-namespace app {
-
- void Classifier::SetVectorResults(std::set<std::pair<float, uint32_t>>& topNSet,
- std::vector<ClassificationResult>& vecResults,
- const std::vector <std::string>& labels)
- {
-
- /* 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;
- }
- }
-
- bool Classifier::GetTopNResults(const std::vector<float>& tensor,
- std::vector<ClassificationResult>& vecResults,
- uint32_t topNCount,
- const std::vector <std::string>& labels)
- {
-
- std::set<std::pair<float , uint32_t>> sortedSet;
-
- /* NOTE: inputVec's size verification against labels should be
- * checked by the calling/public function. */
-
- /* Set initial elements. */
- for (uint32_t i = 0; i < topNCount; ++i) {
- sortedSet.insert({tensor[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 < tensor[i]) {
- sortedSet.erase(*setFwdIter);
- sortedSet.insert({tensor[i], i});
- setFwdIter = sortedSet.begin();
- }
- }
-
- /* Final results' container. */
- vecResults = std::vector<ClassificationResult>(topNCount);
- SetVectorResults(sortedSet, vecResults, labels);
-
- return true;
- }
-
- bool Classifier::GetClassificationResults(
- TfLiteTensor* outputTensor,
- std::vector<ClassificationResult>& vecResults,
- const std::vector <std::string>& labels,
- uint32_t topNCount,
- bool useSoftmax)
- {
- if (outputTensor == nullptr) {
- printf_err("Output vector is null pointer.\n");
- return false;
- }
-
- uint32_t totalOutputSize = 1;
- for (int inputDim = 0; inputDim < outputTensor->dims->size; inputDim++) {
- totalOutputSize *= outputTensor->dims->data[inputDim];
- }
-
- /* Sanity checks. */
- if (totalOutputSize < topNCount) {
- printf_err("Output vector is smaller than %" PRIu32 "\n", topNCount);
- return false;
- } 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;
- vecResults.clear();
-
- /* De-Quantize Output Tensor */
- QuantParams quantParams = GetTensorQuantParams(outputTensor);
-
- /* Floating point tensor data to be populated
- * NOTE: The assumption here is that the output tensor size isn't too
- * big and therefore, there's neglibible impact on heap usage. */
- std::vector<float> tensorData(totalOutputSize);
-
- /* Populate the floating point buffer */
- switch (outputTensor->type) {
- case kTfLiteUInt8: {
- uint8_t *tensor_buffer = tflite::GetTensorData<uint8_t>(outputTensor);
- for (size_t i = 0; i < totalOutputSize; ++i) {
- tensorData[i] = quantParams.scale *
- (static_cast<float>(tensor_buffer[i]) - quantParams.offset);
- }
- break;
- }
- case kTfLiteInt8: {
- int8_t *tensor_buffer = tflite::GetTensorData<int8_t>(outputTensor);
- for (size_t i = 0; i < totalOutputSize; ++i) {
- tensorData[i] = quantParams.scale *
- (static_cast<float>(tensor_buffer[i]) - quantParams.offset);
- }
- break;
- }
- case kTfLiteFloat32: {
- float *tensor_buffer = tflite::GetTensorData<float>(outputTensor);
- for (size_t i = 0; i < totalOutputSize; ++i) {
- tensorData[i] = tensor_buffer[i];
- }
- break;
- }
- default:
- printf_err("Tensor type %s not supported by classifier\n",
- TfLiteTypeGetName(outputTensor->type));
- return false;
- }
-
- if (useSoftmax) {
- math::MathUtils::SoftmaxF32(tensorData);
- }
-
- /* Get the top N results. */
- resultState = GetTopNResults(tensorData, vecResults, topNCount, labels);
-
- if (!resultState) {
- printf_err("Failed to get top N results set\n");
- return false;
- }
-
- return true;
- }
-} /* namespace app */
-} /* namespace arm */ \ No newline at end of file