Mrkomiljon commited on
Commit
84fe3f1
·
verified ·
1 Parent(s): 9231d1c

Delete inference_onnx.py

Browse files
Files changed (1) hide show
  1. inference_onnx.py +0 -70
inference_onnx.py DELETED
@@ -1,70 +0,0 @@
1
- import os
2
- import librosa
3
- import numpy as np
4
- import onnxruntime as ort
5
- import torch
6
- from data_utils import pad # Import the pad function from data_utils.py
7
-
8
- # Preprocess audio for a single segment
9
- def preprocess_audio_segment(segment, cut=64600):
10
- """
11
- Preprocess a single audio segment: pad or trim as required.
12
- """
13
- if len(segment) < cut:
14
- segment = pad(segment, max_len=cut) # Pad if shorter
15
- else:
16
- segment = segment[:cut] # Trim if longer
17
- return np.expand_dims(np.array(segment, dtype=np.float32), axis=0) # Add batch dimension
18
-
19
- # Perform sliding window prediction
20
- def predict_with_sliding_window(audio_path, onnx_model_path, window_size=64600, step_size=64600, sample_rate=16000):
21
- """
22
- Use a sliding window to predict if the audio is real or fake over the entire audio.
23
- """
24
- # Load the ONNX runtime session
25
- ort_session = ort.InferenceSession(onnx_model_path)
26
-
27
- # Load the audio file
28
- waveform, _ = librosa.load(audio_path, sr=sample_rate)
29
- total_segments = []
30
- total_probabilities = []
31
-
32
- # Sliding window processing
33
- for start in range(0, len(waveform), step_size):
34
- end = start + window_size
35
- segment = waveform[start:end]
36
-
37
- # Preprocess the segment
38
- audio_tensor = preprocess_audio_segment(segment)
39
-
40
- # Perform inference
41
- inputs = {ort_session.get_inputs()[0].name: audio_tensor}
42
- outputs = ort_session.run(None, inputs)
43
- probabilities = torch.tensor(outputs[0]) # Convert to torch tensor for processing
44
- probabilities = torch.nn.functional.softmax(probabilities, dim=1) # Compute probabilities
45
- prediction = torch.argmax(probabilities, dim=1)
46
-
47
- # Store the results
48
- predicted_class = "Real" if prediction.item() == 1 else "Fake"
49
- probability = probabilities[0, prediction.item()].item() * 100
50
- total_segments.append(predicted_class)
51
- total_probabilities.append(probability)
52
-
53
- print(f"Segment {start//step_size + 1}: {predicted_class}, Probability: {probability:.2f}%")
54
-
55
- # Final aggregation
56
- majority_class = max(set(total_segments), key=total_segments.count) # Majority voting
57
- avg_probability = np.mean(total_probabilities) # Average probability
58
-
59
- return majority_class, avg_probability
60
-
61
- if __name__ == "__main__":
62
- import argparse
63
-
64
- parser = argparse.ArgumentParser(description="ONNX Model Inference")
65
- parser.add_argument("--model_path", type=str, required=True, help="Path to the ONNX model file")
66
- parser.add_argument("--audio_path", type=str, required=True, help="Path to the audio file")
67
- args = parser.parse_args()
68
-
69
- result, avg_probability = predict_with_sliding_window(args.audio_path, args.model_path)
70
- print(f"Final Result: {result}, Average Probability: {avg_probability:.2f}%")