Commit
•
8a69132
1
Parent(s):
1db723d
Sample app code with OpenVINO inference
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from speechbrain.inference.interfaces import foreign_class
|
2 |
+
from custom_interface import CustomEncoderWav2vec2Classifier
|
3 |
+
from speechbrain.pretrained import EncoderClassifier
|
4 |
+
|
5 |
+
# Function in SpeechBrain to load and use custom PyTorch models
|
6 |
+
classifier = foreign_class(
|
7 |
+
source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
|
8 |
+
pymodule_file="custom_interface.py",
|
9 |
+
classname="CustomEncoderWav2vec2Classifier"
|
10 |
+
)
|
11 |
+
|
12 |
+
# Model checkpoint files
|
13 |
+
checkpoint = EncoderClassifier.from_hparams(
|
14 |
+
source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
|
15 |
+
savedir="./" # Directory to save the model
|
16 |
+
)
|
17 |
+
|
18 |
+
# Convert hparams to a dictionary
|
19 |
+
hparams_dict = vars(checkpoint.hparams)
|
20 |
+
|
21 |
+
# OpenVINO inference optimization parameters
|
22 |
+
device = "cpu"
|
23 |
+
ov_opts = {"device_name": device, "PERFORMANCE_HINT": "LATENCY"}
|
24 |
+
|
25 |
+
instance = CustomEncoderWav2vec2Classifier(modules=checkpoint.mods,
|
26 |
+
hparams=hparams_dict, model=classifier.mods["wav2vec2"].model,
|
27 |
+
audio_file_path="speechbrain/emotion-recognition-wav2vec2-IEMOCAP/anger.wav",
|
28 |
+
backend="openvino",
|
29 |
+
ov_opts=ov_opts,
|
30 |
+
save_ov_model=False)
|
31 |
+
|
32 |
+
|
33 |
+
# OpenVINO inference
|
34 |
+
print("=" * 30)
|
35 |
+
print(f"[INFO] Inference Device: {ov_opts['device_name']}")
|
36 |
+
print("=" * 30)
|
37 |
+
print("\n[INFO] Performing OpenVINO inference...")
|
38 |
+
out_prob, score, index, text_lab = instance.classify_file("speechbrain/emotion-recognition-wav2vec2-IEMOCAP/anger.wav")
|
39 |
+
print(f"[RESULT] OpenVINO Inference Output: {text_lab[index-1]}")
|