Spaces:
Sleeping
Sleeping
Update tasks/audio.py
Browse files- tasks/audio.py +3 -22
tasks/audio.py
CHANGED
|
@@ -9,8 +9,6 @@ import torch.nn.functional as F
|
|
| 9 |
from torch.utils.data import DataLoader, TensorDataset
|
| 10 |
from torchaudio import transforms
|
| 11 |
from torchvision import models
|
| 12 |
-
|
| 13 |
-
from .model import BlazeFaceModel, QuantizedBlazeFaceModel
|
| 14 |
from .utils.evaluation import AudioEvaluationRequest
|
| 15 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
| 16 |
|
|
@@ -87,26 +85,9 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
| 87 |
pin_memory=True,
|
| 88 |
num_workers=4
|
| 89 |
)
|
| 90 |
-
|
| 91 |
-
# Example Usage
|
| 92 |
-
model_settings = {
|
| 93 |
-
'spectrogram_length': 64,
|
| 94 |
-
'dct_coefficient_count': 481,
|
| 95 |
-
'label_count': 2
|
| 96 |
-
}
|
| 97 |
-
model_fp32 = BlazeFaceModel(input_channels=1, label_count=model_settings['label_count'], use_double_block=False, activation='relu', use_optional_block=False)
|
| 98 |
-
quantized_model_path = "./qat_int8_blazeface_model.pth"
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
int8_model = QuantizedBlazeFaceModel(model_fp32)
|
| 102 |
-
int8_model.eval()
|
| 103 |
-
|
| 104 |
-
# Load the state dictionary
|
| 105 |
-
int8_model.load_state_dict(torch.load(quantized_model_path, map_location=torch.device('cpu'), weights_only=True))
|
| 106 |
-
int8_model.eval()
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
# Start tracking emissions
|
| 111 |
tracker.start()
|
| 112 |
tracker.start_task("inference")
|
|
@@ -118,7 +99,7 @@ async def evaluate_audio(request: AudioEvaluationRequest):
|
|
| 118 |
predictions = []
|
| 119 |
with torch.inference_mode():
|
| 120 |
for data, target in test_loader:
|
| 121 |
-
outputs =
|
| 122 |
_, predicted = torch.max(outputs, 1)
|
| 123 |
predictions.extend(predicted.tolist())
|
| 124 |
#--------------------------------------------------------------------------------------------
|
|
|
|
| 9 |
from torch.utils.data import DataLoader, TensorDataset
|
| 10 |
from torchaudio import transforms
|
| 11 |
from torchvision import models
|
|
|
|
|
|
|
| 12 |
from .utils.evaluation import AudioEvaluationRequest
|
| 13 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
| 14 |
|
|
|
|
| 85 |
pin_memory=True,
|
| 86 |
num_workers=4
|
| 87 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
scripted_model = torch.jit.load("./optimized_qat_blazeface_model.pt", map_location=torch.device('cpu'))
|
| 90 |
+
scripted_model.eval()
|
| 91 |
# Start tracking emissions
|
| 92 |
tracker.start()
|
| 93 |
tracker.start_task("inference")
|
|
|
|
| 99 |
predictions = []
|
| 100 |
with torch.inference_mode():
|
| 101 |
for data, target in test_loader:
|
| 102 |
+
outputs = scripted_model(data)
|
| 103 |
_, predicted = torch.max(outputs, 1)
|
| 104 |
predictions.extend(predicted.tolist())
|
| 105 |
#--------------------------------------------------------------------------------------------
|