File size: 4,833 Bytes
c0321ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/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)