Richard Burton | c20be97 | 2022-04-19 17:01:08 +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 | #include "VisualWakeWordProcessing.hpp" |
Richard Burton | fcca863 | 2022-05-05 17:23:43 +0100 | [diff] [blame] | 18 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 19 | #include "ImageUtils.hpp" |
| 20 | #include "VisualWakeWordModel.hpp" |
| 21 | #include "log_macros.h" |
| 22 | |
| 23 | namespace arm { |
| 24 | namespace app { |
| 25 | |
Richard Burton | fcca863 | 2022-05-05 17:23:43 +0100 | [diff] [blame] | 26 | VisualWakeWordPreProcess::VisualWakeWordPreProcess(TfLiteTensor* inputTensor, bool rgb2Gray) |
| 27 | :m_inputTensor{inputTensor}, |
| 28 | m_rgb2Gray{rgb2Gray} |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 29 | {} |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 30 | |
| 31 | bool VisualWakeWordPreProcess::DoPreProcess(const void* data, size_t inputSize) |
| 32 | { |
| 33 | if (data == nullptr) { |
| 34 | printf_err("Data pointer is null"); |
| 35 | } |
| 36 | |
| 37 | auto input = static_cast<const uint8_t*>(data); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 38 | |
Richard Burton | fcca863 | 2022-05-05 17:23:43 +0100 | [diff] [blame] | 39 | uint8_t* unsignedDstPtr = this->m_inputTensor->data.uint8; |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 40 | |
Richard Burton | fcca863 | 2022-05-05 17:23:43 +0100 | [diff] [blame] | 41 | if (this->m_rgb2Gray) { |
| 42 | image::RgbToGrayscale(input, unsignedDstPtr, inputSize); |
| 43 | } else { |
| 44 | std::memcpy(unsignedDstPtr, input, inputSize); |
| 45 | } |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 46 | |
| 47 | /* VWW model pre-processing is image conversion from uint8 to [0,1] float values, |
| 48 | * then quantize them with input quantization info. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 49 | QuantParams inQuantParams = GetTensorQuantParams(this->m_inputTensor); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 50 | |
Richard Burton | fcca863 | 2022-05-05 17:23:43 +0100 | [diff] [blame] | 51 | int8_t* signedDstPtr = this->m_inputTensor->data.int8; |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 52 | for (size_t i = 0; i < this->m_inputTensor->bytes; i++) { |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 53 | auto i_data_int8 = static_cast<int8_t>( |
| 54 | ((static_cast<float>(unsignedDstPtr[i]) / 255.0f) / inQuantParams.scale) + inQuantParams.offset |
| 55 | ); |
| 56 | signedDstPtr[i] = std::min<int8_t>(INT8_MAX, std::max<int8_t>(i_data_int8, INT8_MIN)); |
| 57 | } |
| 58 | |
| 59 | debug("Input tensor populated \n"); |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 64 | VisualWakeWordPostProcess::VisualWakeWordPostProcess(TfLiteTensor* outputTensor, Classifier& classifier, |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 65 | const std::vector<std::string>& labels, std::vector<ClassificationResult>& results) |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 66 | :m_outputTensor{outputTensor}, |
| 67 | m_vwwClassifier{classifier}, |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 68 | m_labels{labels}, |
| 69 | m_results{results} |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 70 | {} |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 71 | |
| 72 | bool VisualWakeWordPostProcess::DoPostProcess() |
| 73 | { |
| 74 | return this->m_vwwClassifier.GetClassificationResults( |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 75 | this->m_outputTensor, this->m_results, |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 76 | this->m_labels, 1, true); |
| 77 | } |
| 78 | |
| 79 | } /* namespace app */ |
| 80 | } /* namespace arm */ |