diff options
Diffstat (limited to 'python/pyarmnn/examples/common/utils.py')
-rw-r--r-- | python/pyarmnn/examples/common/utils.py | 73 |
1 files changed, 41 insertions, 32 deletions
diff --git a/python/pyarmnn/examples/common/utils.py b/python/pyarmnn/examples/common/utils.py index d4dadf80a4..beca0d37a0 100644 --- a/python/pyarmnn/examples/common/utils.py +++ b/python/pyarmnn/examples/common/utils.py @@ -8,7 +8,7 @@ import errno from pathlib import Path import numpy as np -import pyarmnn as ann +import datetime def dict_labels(labels_file_path: str, include_rgb=False) -> dict: @@ -42,67 +42,76 @@ def dict_labels(labels_file_path: str, include_rgb=False) -> dict: return labels -def prepare_input_tensors(audio_data, input_binding_info, mfcc_preprocessor): +def prepare_input_data(audio_data, input_data_type, input_quant_scale, input_quant_offset, mfcc_preprocessor): """ Takes a block of audio data, extracts the MFCC features, quantizes the array, and uses ArmNN to create the input tensors. Args: audio_data: The audio data to process - mfcc_instance: the mfcc class instance - input_binding_info: the model input binding info - mfcc_preprocessor: the mfcc preprocessor instance + mfcc_instance: The mfcc class instance + input_data_type: The model's input data type + input_quant_scale: The model's quantization scale + input_quant_offset: The model's quantization offset + mfcc_preprocessor: The mfcc preprocessor instance Returns: - input_tensors: the prepared input tensors, ready to be consumed by the ArmNN NetworkExecutor + input_data: The prepared input data """ - data_type = input_binding_info[1].GetDataType() - input_tensor = mfcc_preprocessor.extract_features(audio_data) - if data_type != ann.DataType_Float32: - input_tensor = quantize_input(input_tensor, input_binding_info) - input_tensors = ann.make_input_tensors([input_binding_info], [input_tensor]) - return input_tensors + input_data = mfcc_preprocessor.extract_features(audio_data) + if input_data_type != np.float32: + input_data = quantize_input(input_data, input_data_type, input_quant_scale, input_quant_offset) + return input_data -def quantize_input(data, input_binding_info): +def quantize_input(data, input_data_type, input_quant_scale, input_quant_offset): """Quantize the float input to (u)int8 ready for inputting to model.""" if data.ndim != 2: raise RuntimeError("Audio data must have 2 dimensions for quantization") - quant_scale = input_binding_info[1].GetQuantizationScale() - quant_offset = input_binding_info[1].GetQuantizationOffset() - data_type = input_binding_info[1].GetDataType() - - if data_type == ann.DataType_QAsymmS8: - data_type = np.int8 - elif data_type == ann.DataType_QAsymmU8: - data_type = np.uint8 - else: + if (input_data_type != np.int8) and (input_data_type != np.uint8): raise ValueError("Could not quantize data to required data type") - d_min = np.iinfo(data_type).min - d_max = np.iinfo(data_type).max + d_min = np.iinfo(input_data_type).min + d_max = np.iinfo(input_data_type).max for row in range(data.shape[0]): for col in range(data.shape[1]): - data[row, col] = (data[row, col] / quant_scale) + quant_offset + data[row, col] = (data[row, col] / input_quant_scale) + input_quant_offset data[row, col] = np.clip(data[row, col], d_min, d_max) - data = data.astype(data_type) + data = data.astype(input_data_type) return data -def dequantize_output(data, output_binding_info): +def dequantize_output(data, is_output_quantized, output_quant_scale, output_quant_offset): """Dequantize the (u)int8 output to float""" - if output_binding_info[1].IsQuantized(): + if is_output_quantized: if data.ndim != 2: raise RuntimeError("Data must have 2 dimensions for quantization") - quant_scale = output_binding_info[1].GetQuantizationScale() - quant_offset = output_binding_info[1].GetQuantizationOffset() - data = data.astype(float) for row in range(data.shape[0]): for col in range(data.shape[1]): - data[row, col] = (data[row, col] - quant_offset)*quant_scale + data[row, col] = (data[row, col] - output_quant_offset)*output_quant_scale return data + + +class Profiling: + def __init__(self, enabled: bool): + self.m_start = 0 + self.m_end = 0 + self.m_enabled = enabled + + def profiling_start(self): + if self.m_enabled: + self.m_start = datetime.datetime.now() + + def profiling_stop_and_print_us(self, msg): + if self.m_enabled: + self.m_end = datetime.datetime.now() + period = self.m_end - self.m_start + period_us = period.seconds * 1_000_000 + period.microseconds + print(f'Profiling: {msg} : {period_us:,} microSeconds') + return period_us + return 0 |