psakamoori
commited on
Commit
•
b02e0f2
1
Parent(s):
50b4d3f
Add torch_device and update ov opts params
Browse files- custom_interface.py +13 -8
custom_interface.py
CHANGED
@@ -36,8 +36,10 @@ class CustomEncoderWav2vec2Classifier(Pretrained):
|
|
36 |
"""
|
37 |
|
38 |
def __init__(self, *args, model=None,
|
39 |
-
audio_file_path=None,
|
40 |
-
|
|
|
|
|
41 |
save_ov_model=False,
|
42 |
**kwargs):
|
43 |
super().__init__(*args, **kwargs)
|
@@ -49,23 +51,26 @@ class CustomEncoderWav2vec2Classifier(Pretrained):
|
|
49 |
|
50 |
self.core = ov.Core()
|
51 |
self.ov_model = None
|
52 |
-
|
53 |
if model:
|
54 |
print("\n[INFO] Preparing OpenVINO model...")
|
55 |
self.get_ov_model(model, audio_file_path)
|
56 |
print("[SUCCESS] OpenVINO IR model compiled for inference!\n")
|
57 |
if self.ov_model:
|
58 |
-
self.device = ov_opts["device_name"]
|
59 |
print("[INFO] Compiling OpenVINO IR model for inference...")
|
60 |
-
self.compiled_model = self.core.compile_model(self.ov_model,
|
|
|
|
|
61 |
print("[SUCCESS] OpenVINO IR model compiled for inference!\n")
|
62 |
-
|
63 |
if save_ov_model:
|
64 |
# set to default path
|
65 |
print("[INFO] Saving OpenVINO IR model to disk!\n")
|
66 |
ov_ir_file_path = "./openvino_model/fp32/speechbrain_emotion_recog_ov_ir_model.xml"
|
67 |
ov.save_model(self.ov_model, ov_ir_file_path)
|
68 |
print(f"[SUCCESS] OpenVINO IR model file saved at {ov_ir_file_path}!\n")
|
|
|
|
|
69 |
|
70 |
def encode_batch(self, wavs, wav_lens=None, normalize=False):
|
71 |
"""Encodes the input audio into a single vector embedding.
|
@@ -100,10 +105,10 @@ class CustomEncoderWav2vec2Classifier(Pretrained):
|
|
100 |
|
101 |
# Assign full length if wav_lens is not assigned
|
102 |
if wav_lens is None:
|
103 |
-
wav_lens = torch.ones(wavs.shape[0], device=self.
|
104 |
|
105 |
# Storing waveform in the specified device
|
106 |
-
wavs, wav_lens = wavs.to(self.
|
107 |
wavs = wavs.float()
|
108 |
|
109 |
if self.backend == "pytorch":
|
|
|
36 |
"""
|
37 |
|
38 |
def __init__(self, *args, model=None,
|
39 |
+
audio_file_path=None,
|
40 |
+
backend="pytorch",
|
41 |
+
opts=None,
|
42 |
+
torch_device="cpu",
|
43 |
save_ov_model=False,
|
44 |
**kwargs):
|
45 |
super().__init__(*args, **kwargs)
|
|
|
51 |
|
52 |
self.core = ov.Core()
|
53 |
self.ov_model = None
|
54 |
+
self.torch_device = torch_device
|
55 |
if model:
|
56 |
print("\n[INFO] Preparing OpenVINO model...")
|
57 |
self.get_ov_model(model, audio_file_path)
|
58 |
print("[SUCCESS] OpenVINO IR model compiled for inference!\n")
|
59 |
if self.ov_model:
|
|
|
60 |
print("[INFO] Compiling OpenVINO IR model for inference...")
|
61 |
+
self.compiled_model = self.core.compile_model(self.ov_model,
|
62 |
+
device_name=opts["ov_device"],
|
63 |
+
config=opts["config"])
|
64 |
print("[SUCCESS] OpenVINO IR model compiled for inference!\n")
|
65 |
+
# Falg to save openvino ir model file to disk
|
66 |
if save_ov_model:
|
67 |
# set to default path
|
68 |
print("[INFO] Saving OpenVINO IR model to disk!\n")
|
69 |
ov_ir_file_path = "./openvino_model/fp32/speechbrain_emotion_recog_ov_ir_model.xml"
|
70 |
ov.save_model(self.ov_model, ov_ir_file_path)
|
71 |
print(f"[SUCCESS] OpenVINO IR model file saved at {ov_ir_file_path}!\n")
|
72 |
+
elif backend == "pytorch":
|
73 |
+
self.torch_device = opts["torch_device"]
|
74 |
|
75 |
def encode_batch(self, wavs, wav_lens=None, normalize=False):
|
76 |
"""Encodes the input audio into a single vector embedding.
|
|
|
105 |
|
106 |
# Assign full length if wav_lens is not assigned
|
107 |
if wav_lens is None:
|
108 |
+
wav_lens = torch.ones(wavs.shape[0], device=self.torch_device)
|
109 |
|
110 |
# Storing waveform in the specified device
|
111 |
+
wavs, wav_lens = wavs.to(self.torch_device), wav_lens.to(self.torch_device)
|
112 |
wavs = wavs.float()
|
113 |
|
114 |
if self.backend == "pytorch":
|