Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #ifndef IMG_CLASS_PROCESSING_HPP |
| 18 | #define IMG_CLASS_PROCESSING_HPP |
| 19 | |
| 20 | #include "BaseProcessing.hpp" |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 21 | #include "Classifier.hpp" |
| 22 | |
| 23 | namespace arm { |
| 24 | namespace app { |
| 25 | |
| 26 | /** |
| 27 | * @brief Pre-processing class for Image Classification use case. |
| 28 | * Implements methods declared by BasePreProcess and anything else needed |
| 29 | * to populate input tensors ready for inference. |
| 30 | */ |
| 31 | class ImgClassPreProcess : public BasePreProcess { |
| 32 | |
| 33 | public: |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 34 | /** |
| 35 | * @brief Constructor |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 36 | * @param[in] inputTensor Pointer to the TFLite Micro input Tensor. |
| 37 | * @param[in] convertToInt8 Should the image be converted to Int8 range. |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 38 | **/ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 39 | explicit ImgClassPreProcess(TfLiteTensor* inputTensor, bool convertToInt8); |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 40 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 41 | /** |
| 42 | * @brief Should perform pre-processing of 'raw' input image data and load it into |
| 43 | * TFLite Micro input tensors ready for inference |
| 44 | * @param[in] input Pointer to the data that pre-processing will work on. |
| 45 | * @param[in] inputSize Size of the input data. |
| 46 | * @return true if successful, false otherwise. |
| 47 | **/ |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 48 | bool DoPreProcess(const void* input, size_t inputSize) override; |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | TfLiteTensor* m_inputTensor; |
| 52 | bool m_convertToInt8; |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * @brief Post-processing class for Image Classification use case. |
| 57 | * Implements methods declared by BasePostProcess and anything else needed |
| 58 | * to populate result vector. |
| 59 | */ |
| 60 | class ImgClassPostProcess : public BasePostProcess { |
| 61 | |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 62 | public: |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 63 | /** |
| 64 | * @brief Constructor |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 65 | * @param[in] outputTensor Pointer to the TFLite Micro output Tensor. |
| 66 | * @param[in] classifier Classifier object used to get top N results from classification. |
| 67 | * @param[in] labels Vector of string labels to identify each output of the model. |
| 68 | * @param[in] results Vector of classification results to store decoded outputs. |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 69 | **/ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 70 | ImgClassPostProcess(TfLiteTensor* outputTensor, Classifier& classifier, |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 71 | const std::vector<std::string>& labels, |
| 72 | std::vector<ClassificationResult>& results); |
| 73 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 74 | /** |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 75 | * @brief Should perform post-processing of the result of inference then |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 76 | * populate classification result data for any later use. |
| 77 | * @return true if successful, false otherwise. |
| 78 | **/ |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 79 | bool DoPostProcess() override; |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | TfLiteTensor* m_outputTensor; |
| 83 | Classifier& m_imgClassifier; |
| 84 | const std::vector<std::string>& m_labels; |
| 85 | std::vector<ClassificationResult>& m_results; |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } /* namespace app */ |
| 89 | } /* namespace arm */ |
| 90 | |
| 91 | #endif /* IMG_CLASS_PROCESSING_HPP */ |