longtc commited on
Commit
c57306b
·
verified ·
1 Parent(s): b0094bb

Upload predict_model.py

Browse files
Files changed (1) hide show
  1. predict_model.py +71 -0
predict_model.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tensorflow import keras
2
+ import keras.layers
3
+ import librosa
4
+ import numpy as np
5
+ import tensorflow as tf
6
+
7
+ frame_length = 256
8
+ frame_step = 160
9
+ fft_length = 384
10
+
11
+
12
+ def CTCLoss(y_true, y_pred):
13
+ batch_len = tf.cast(tf.shape(y_true)[0], dtype="int64")
14
+ input_length = tf.cast(tf.shape(y_pred)[1], dtype="int64")
15
+ label_length = tf.cast(tf.shape(y_true)[1], dtype="int64")
16
+
17
+ input_length = input_length * tf.ones(shape=(batch_len, 1), dtype="int64")
18
+ label_length = label_length * tf.ones(shape=(batch_len, 1), dtype="int64")
19
+
20
+ loss = keras.backend.ctc_batch_cost(y_true, y_pred, input_length, label_length)
21
+ return loss
22
+
23
+
24
+ # Tải mô hình
25
+ loaded_model = keras.models.load_model(r'D:\MyCode\Python\saved_model\my_model.h5', custom_objects={'CTCLoss': CTCLoss})
26
+
27
+ characters = [x for x in "abcdefghijklmnopqrstuvwxyzăâêôơưđ'?! "]
28
+ char_to_num = keras.layers.StringLookup(vocabulary=characters, oov_token="")
29
+ num_to_char = keras.layers.StringLookup(vocabulary=char_to_num.get_vocabulary(), oov_token="", invert=True)
30
+
31
+
32
+ def decode_batch_predictions(pred):
33
+ input_len = np.ones(pred.shape[0]) * pred.shape[1]
34
+ results = keras.backend.ctc_decode(pred, input_len=input_len, greedy=True)[0][0]
35
+ output_texts = []
36
+ for result in results:
37
+ result = tf.strings.reduce_join(num_to_char(result)).numpy().decode('utf-8')
38
+ output_texts.append(result)
39
+ return output_texts
40
+
41
+
42
+ # Hàm để xử lý và dự đoán cho một tệp âm thanh
43
+ def predict_from_audio(file_name):
44
+ # Tiền xử lý tệp âm thanh
45
+ audio, _ = librosa.load(file_name, sr=None) # Đọc tệp âm thanh
46
+ audio = tf.convert_to_tensor(audio, dtype=tf.float32)
47
+
48
+ # Tính toán spectrogram
49
+ spectrogram = tf.signal.stft(audio, frame_length=frame_length, frame_step=frame_step, fft_length=fft_length)
50
+ spectrogram = tf.abs(spectrogram)
51
+ spectrogram = tf.math.pow(spectrogram, 0.5)
52
+
53
+ # Chuẩn hóa
54
+ mean = tf.math.reduce_mean(spectrogram, axis=1, keepdims=True)
55
+ stddevs = tf.math.reduce_std(spectrogram, axis=1, keepdims=True)
56
+ spectrogram = (spectrogram - mean) / (stddevs + 1e-10)
57
+
58
+ # Thêm chiều cho "channels" và "batch"
59
+ spectrogram = tf.expand_dims(spectrogram, axis=-1) # Thêm chiều cho kênh
60
+ spectrogram = tf.expand_dims(spectrogram, axis=0) # Thêm chiều batch
61
+
62
+ # Dự đoán
63
+ predictions = loaded_model.predict(spectrogram)
64
+ decoded_predictions = decode_batch_predictions(predictions)
65
+
66
+ return decoded_predictions
67
+
68
+
69
+ # Dự đoán cho một tệp âm thanh
70
+ result = predict_from_audio(r'D:\MyCode\Python\dataset\test_audio.wav')
71
+ print("Dự đoán:", result)