#!/usr/bin/env python3 # Copyright 2023-2024,2026 NXP # #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. import argparse import sys import librosa import numpy as np import tensorflow as tf def file_load(wav_name): """ Load a .wav file. wav_name : str target .wav file return : numpy.array( float ) """ try: return librosa.load(wav_name, sr=None, mono=False) except Exception: raise RuntimeError("file broken or not exists!! : {}".format(wav_name)) def quantize(data, scale, zero_point): return (data / scale) + zero_point def file_to_vector_array( file_name, n_mels=128, frames=5, n_fft=1024, hop_length=512, power=2.0, method="librosa", ): """ Convert a .wav file to a vector array of log-mel spectrogram features. return : numpy.array( numpy.array( float ) ) vector array, shape = (dataset_size, feature_vector_length) """ # 01 calculate the number of dimensions dims = n_mels * frames # 02 generate melspectrogram y, sr = file_load(file_name) if method == "librosa": mel_spectrogram = librosa.feature.melspectrogram( y=y, sr=sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels, power=power ) # 03 convert melspectrogram to log mel energy log_mel_spectrogram = ( 20.0 / power * np.log10(mel_spectrogram + sys.float_info.epsilon) ) else: print("spectrogram method not supported: {}".format(method)) return np.empty((0, dims)) # 3b take central part only log_mel_spectrogram = log_mel_spectrogram[:, 50:250] # 04 calculate total vector size vector_array_size = len(log_mel_spectrogram[0, :]) - frames + 1 # 05 skip too short clips if vector_array_size < 1: return np.empty((0, dims)) # 06 generate feature vectors by concatenating multiframes vector_array = np.zeros((vector_array_size, dims)) for t in range(frames): vector_array[:, n_mels * t : n_mels * (t + 1)] = log_mel_spectrogram[ :, t : t + vector_array_size ].T return vector_array def predict(interpreter, input_data): input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() output_data = np.empty_like(input_data) for i in range(input_data.shape[0]): interpreter.set_tensor(input_details[0]["index"], input_data[i : i + 1, :]) interpreter.invoke() output_data[i : i + 1, :] = interpreter.get_tensor(output_details[0]["index"]) scale, zero_point = output_details[0]["quantization"] out = output_data.astype(np.float32) out = scale * (out - zero_point) return output_data, out def inference(model_path, input_path, output_path=None): data_fp = file_to_vector_array(input_path) interpreter = tf.lite.Interpreter(model_path=model_path) interpreter.allocate_tensors() print("Loaded model:", model_path) input_details = interpreter.get_input_details()[0] scale, zero_point = input_details["quantization"] data = quantize(data_fp, scale, zero_point) input_data = data.astype(input_details["dtype"]) _, out_dequant = predict(interpreter, input_data) errors = np.mean(np.square(data_fp - out_dequant), axis=1) anomaly_score = float(np.mean(errors)) line = "Anomaly score (reconstruction error): {:.6f}".format(anomaly_score) print(line) if output_path: with open(output_path, "w") as f: f.write(line + "\n") print("Wrote prediction to", output_path) return anomaly_score if __name__ == "__main__": parser = argparse.ArgumentParser( description="Deep autoencoder anomalous sound detection inference example" ) parser.add_argument( "-m", "--model", default="assets/quantized_tflite_model/ad01_int8.tflite", help="Path to the TFLite model file.", ) parser.add_argument( "-i", "--input", default="example_input.wav", help="Path to the input .wav file.", ) parser.add_argument( "-o", "--output", default=None, help="Optional path to write the anomaly score as text.", ) args = parser.parse_args() inference(args.model, args.input, args.output)