summaryrefslogtreecommitdiff
path: root/source/use_case/img_class
diff options
context:
space:
mode:
Diffstat (limited to 'source/use_case/img_class')
-rw-r--r--source/use_case/img_class/include/ImgClassProcessing.hpp92
-rw-r--r--source/use_case/img_class/include/MobileNetModel.hpp55
-rw-r--r--source/use_case/img_class/src/ImgClassProcessing.cc65
-rw-r--r--source/use_case/img_class/src/MainLoop.cc24
-rw-r--r--source/use_case/img_class/src/MobileNetModel.cc56
-rw-r--r--source/use_case/img_class/usecase.cmake2
6 files changed, 24 insertions, 270 deletions
diff --git a/source/use_case/img_class/include/ImgClassProcessing.hpp b/source/use_case/img_class/include/ImgClassProcessing.hpp
deleted file mode 100644
index e931b7d..0000000
--- a/source/use_case/img_class/include/ImgClassProcessing.hpp
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (c) 2022 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 IMG_CLASS_PROCESSING_HPP
-#define IMG_CLASS_PROCESSING_HPP
-
-#include "BaseProcessing.hpp"
-#include "Model.hpp"
-#include "Classifier.hpp"
-
-namespace arm {
-namespace app {
-
- /**
- * @brief Pre-processing class for Image Classification use case.
- * Implements methods declared by BasePreProcess and anything else needed
- * to populate input tensors ready for inference.
- */
- class ImgClassPreProcess : public BasePreProcess {
-
- public:
- /**
- * @brief Constructor
- * @param[in] inputTensor Pointer to the TFLite Micro input Tensor.
- * @param[in] convertToInt8 Should the image be converted to Int8 range.
- **/
- explicit ImgClassPreProcess(TfLiteTensor* inputTensor, bool convertToInt8);
-
- /**
- * @brief Should perform pre-processing of 'raw' input image data and load it into
- * TFLite Micro input tensors ready for inference
- * @param[in] input Pointer to the data that pre-processing will work on.
- * @param[in] inputSize Size of the input data.
- * @return true if successful, false otherwise.
- **/
- bool DoPreProcess(const void* input, size_t inputSize) override;
-
- private:
- TfLiteTensor* m_inputTensor;
- bool m_convertToInt8;
- };
-
- /**
- * @brief Post-processing class for Image Classification use case.
- * Implements methods declared by BasePostProcess and anything else needed
- * to populate result vector.
- */
- class ImgClassPostProcess : public BasePostProcess {
-
- public:
- /**
- * @brief Constructor
- * @param[in] outputTensor Pointer to the TFLite Micro output Tensor.
- * @param[in] classifier Classifier object used to get top N results from classification.
- * @param[in] labels Vector of string labels to identify each output of the model.
- * @param[in] results Vector of classification results to store decoded outputs.
- **/
- ImgClassPostProcess(TfLiteTensor* outputTensor, Classifier& classifier,
- const std::vector<std::string>& labels,
- std::vector<ClassificationResult>& results);
-
- /**
- * @brief Should perform post-processing of the result of inference then
- * populate classification result data for any later use.
- * @return true if successful, false otherwise.
- **/
- bool DoPostProcess() override;
-
- private:
- TfLiteTensor* m_outputTensor;
- Classifier& m_imgClassifier;
- const std::vector<std::string>& m_labels;
- std::vector<ClassificationResult>& m_results;
- };
-
-} /* namespace app */
-} /* namespace arm */
-
-#endif /* IMG_CLASS_PROCESSING_HPP */ \ No newline at end of file
diff --git a/source/use_case/img_class/include/MobileNetModel.hpp b/source/use_case/img_class/include/MobileNetModel.hpp
deleted file mode 100644
index 503f1ac..0000000
--- a/source/use_case/img_class/include/MobileNetModel.hpp
+++ /dev/null
@@ -1,55 +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 IMG_CLASS_MOBILENETMODEL_HPP
-#define IMG_CLASS_MOBILENETMODEL_HPP
-
-#include "Model.hpp"
-
-namespace arm {
-namespace app {
-
- class MobileNetModel : public Model {
-
- public:
- /* Indices for the expected model - based on input tensor shape */
- static constexpr uint32_t ms_inputRowsIdx = 1;
- static constexpr uint32_t ms_inputColsIdx = 2;
- static constexpr uint32_t ms_inputChannelsIdx = 3;
-
- 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 = 7;
-
- /* A mutable op resolver instance. */
- tflite::MicroMutableOpResolver<ms_maxOpCnt> m_opResolver;
- };
-
-} /* namespace app */
-} /* namespace arm */
-
-#endif /* IMG_CLASS_MOBILENETMODEL_HPP */ \ No newline at end of file
diff --git a/source/use_case/img_class/src/ImgClassProcessing.cc b/source/use_case/img_class/src/ImgClassProcessing.cc
deleted file mode 100644
index adf9794..0000000
--- a/source/use_case/img_class/src/ImgClassProcessing.cc
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2022 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 "ImgClassProcessing.hpp"
-#include "ImageUtils.hpp"
-#include "log_macros.h"
-
-namespace arm {
-namespace app {
-
- ImgClassPreProcess::ImgClassPreProcess(TfLiteTensor* inputTensor, bool convertToInt8)
- :m_inputTensor{inputTensor},
- m_convertToInt8{convertToInt8}
- {}
-
- bool ImgClassPreProcess::DoPreProcess(const void* data, size_t inputSize)
- {
- if (data == nullptr) {
- printf_err("Data pointer is null");
- return false;
- }
-
- auto input = static_cast<const uint8_t*>(data);
-
- std::memcpy(this->m_inputTensor->data.data, input, inputSize);
- debug("Input tensor populated \n");
-
- if (this->m_convertToInt8) {
- image::ConvertImgToInt8(this->m_inputTensor->data.data, this->m_inputTensor->bytes);
- }
-
- return true;
- }
-
- ImgClassPostProcess::ImgClassPostProcess(TfLiteTensor* outputTensor, Classifier& classifier,
- const std::vector<std::string>& labels,
- std::vector<ClassificationResult>& results)
- :m_outputTensor{outputTensor},
- m_imgClassifier{classifier},
- m_labels{labels},
- m_results{results}
- {}
-
- bool ImgClassPostProcess::DoPostProcess()
- {
- return this->m_imgClassifier.GetClassificationResults(
- this->m_outputTensor, this->m_results,
- this->m_labels, 5, false);
- }
-
-} /* namespace app */
-} /* namespace arm */ \ No newline at end of file
diff --git a/source/use_case/img_class/src/MainLoop.cc b/source/use_case/img_class/src/MainLoop.cc
index d9fb925..de3779f 100644
--- a/source/use_case/img_class/src/MainLoop.cc
+++ b/source/use_case/img_class/src/MainLoop.cc
@@ -21,7 +21,16 @@
#include "MobileNetModel.hpp" /* Model class for running inference. */
#include "UseCaseHandler.hpp" /* Handlers for different user options. */
#include "UseCaseCommonUtils.hpp" /* Utils functions. */
-#include "log_macros.h"
+#include "BufAttributes.hpp" /* Buffer attributes to be applied */
+
+namespace arm {
+ namespace app {
+ static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
+ } /* namespace app */
+} /* namespace arm */
+
+extern uint8_t* GetModelPointer();
+extern size_t GetModelLen();
using ImgClassClassifier = arm::app::Classifier;
@@ -30,11 +39,22 @@ void main_loop()
arm::app::MobileNetModel model; /* Model wrapper object. */
/* Load the model. */
- if (!model.Init()) {
+ if (!model.Init(arm::app::tensorArena,
+ sizeof(arm::app::tensorArena),
+ GetModelPointer(),
+ GetModelLen())) {
printf_err("Failed to initialise model\n");
return;
}
+#if !defined(ARM_NPU)
+ /* If it is not a NPU build check if the model contains a NPU operator */
+ if (model.ContainsEthosUOperator()) {
+ printf_err("No driver support for Ethos-U operator found in the model.\n");
+ return;
+ }
+#endif /* ARM_NPU */
+
/* Instantiate application context. */
arm::app::ApplicationContext caseContext;
diff --git a/source/use_case/img_class/src/MobileNetModel.cc b/source/use_case/img_class/src/MobileNetModel.cc
deleted file mode 100644
index 2e48f3b..0000000
--- a/source/use_case/img_class/src/MobileNetModel.cc
+++ /dev/null
@@ -1,56 +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 "MobileNetModel.hpp"
-#include "log_macros.h"
-
-const tflite::MicroOpResolver& arm::app::MobileNetModel::GetOpResolver()
-{
- return this->m_opResolver;
-}
-
-bool arm::app::MobileNetModel::EnlistOperations()
-{
- this->m_opResolver.AddDepthwiseConv2D();
- this->m_opResolver.AddConv2D();
- this->m_opResolver.AddAveragePool2D();
- this->m_opResolver.AddAdd();
- this->m_opResolver.AddReshape();
- this->m_opResolver.AddSoftmax();
-
-#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::MobileNetModel::ModelPointer()
-{
- return GetModelPointer();
-}
-
-extern size_t GetModelLen();
-size_t arm::app::MobileNetModel::ModelSize()
-{
- return GetModelLen();
-} \ No newline at end of file
diff --git a/source/use_case/img_class/usecase.cmake b/source/use_case/img_class/usecase.cmake
index dafdbbf..2a8be09 100644
--- a/source/use_case/img_class/usecase.cmake
+++ b/source/use_case/img_class/usecase.cmake
@@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#----------------------------------------------------------------------------
+# Append the API to use for this use case
+list(APPEND ${use_case}_API_LIST "img_class")
USER_OPTION(${use_case}_FILE_PATH "Directory with custom image files to use, or path to a single image, in the evaluation application"
${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/samples/