Thomas commited on
Commit
04e024e
·
1 Parent(s): 0014ea1

resample all audios to be on the sample SR

Browse files
Files changed (1) hide show
  1. notebooks/template-audio.ipynb +64 -4
notebooks/template-audio.ipynb CHANGED
@@ -10,17 +10,20 @@
10
  },
11
  {
12
  "cell_type": "code",
13
- "execution_count": null,
14
  "metadata": {},
15
  "outputs": [],
16
  "source": [
17
  "from fastapi import APIRouter\n",
18
  "from datetime import datetime\n",
19
  "from datasets import load_dataset\n",
 
20
  "from sklearn.metrics import accuracy_score\n",
21
  "import random\n",
22
- "\n",
 
23
  "import sys\n",
 
24
  "sys.path.append('../tasks')\n",
25
  "\n",
26
  "from utils.evaluation import AudioEvaluationRequest\n",
@@ -53,7 +56,7 @@
53
  },
54
  {
55
  "cell_type": "code",
56
- "execution_count": null,
57
  "metadata": {},
58
  "outputs": [],
59
  "source": [
@@ -67,13 +70,68 @@
67
  "test_dataset = train_test[\"test\"]"
68
  ]
69
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  {
71
  "cell_type": "code",
72
  "execution_count": null,
73
  "metadata": {},
74
  "outputs": [],
75
  "source": [
76
- "train_test.shape"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  ]
78
  },
79
  {
@@ -108,6 +166,8 @@
108
  "\n",
109
  "# Make random predictions (placeholder for actual model inference)\n",
110
  "true_labels = test_dataset[\"label\"]\n",
 
 
111
  "predictions = [random.randint(0, 1) for _ in range(len(true_labels))]\n",
112
  "\n",
113
  "predictions\n",
 
10
  },
11
  {
12
  "cell_type": "code",
13
+ "execution_count": 31,
14
  "metadata": {},
15
  "outputs": [],
16
  "source": [
17
  "from fastapi import APIRouter\n",
18
  "from datetime import datetime\n",
19
  "from datasets import load_dataset\n",
20
+ "import librosa\n",
21
  "from sklearn.metrics import accuracy_score\n",
22
  "import random\n",
23
+ "import pandas as pd\n",
24
+ "import numpy as np\n",
25
  "import sys\n",
26
+ "import json\n",
27
  "sys.path.append('../tasks')\n",
28
  "\n",
29
  "from utils.evaluation import AudioEvaluationRequest\n",
 
56
  },
57
  {
58
  "cell_type": "code",
59
+ "execution_count": 2,
60
  "metadata": {},
61
  "outputs": [],
62
  "source": [
 
70
  "test_dataset = train_test[\"test\"]"
71
  ]
72
  },
73
+ {
74
+ "cell_type": "markdown",
75
+ "metadata": {},
76
+ "source": [
77
+ "## Analysis"
78
+ ]
79
+ },
80
+ {
81
+ "cell_type": "code",
82
+ "execution_count": null,
83
+ "metadata": {},
84
+ "outputs": [],
85
+ "source": [
86
+ "train = dataset[\"train\"]\n",
87
+ "test = dataset['test']\n",
88
+ "\n",
89
+ "train_df = pd.DataFrame(train)"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "code",
94
+ "execution_count": 24,
95
+ "metadata": {},
96
+ "outputs": [],
97
+ "source": [
98
+ "train_df[\"path\"] = train_df[\"audio\"].apply(lambda x: x['path'])\n",
99
+ "train_df[\"array\"] = train_df[\"audio\"].apply(lambda x: x['array'])\n",
100
+ "train_df[\"sampling_rate\"] = train_df[\"audio\"].apply(lambda x: x['sampling_rate'])"
101
+ ]
102
+ },
103
  {
104
  "cell_type": "code",
105
  "execution_count": null,
106
  "metadata": {},
107
  "outputs": [],
108
  "source": [
109
+ "# Target sampling rate\n",
110
+ "target_sr = 12000\n",
111
+ "\n",
112
+ "# Function to resample the audio array\n",
113
+ "def resample_audio(array, orig_sr, target_sr):\n",
114
+ " array = np.array(array) # Ensure it's a numpy array\n",
115
+ " if orig_sr != target_sr:\n",
116
+ " array = librosa.resample(array, orig_sr=orig_sr, target_sr=target_sr)\n",
117
+ " return array\n",
118
+ "\n",
119
+ "# Apply resampling to each row\n",
120
+ "train_df[\"resampled_array\"] = train_df.apply(\n",
121
+ " lambda row: resample_audio(row[\"array\"], row[\"sampling_rate\"], target_sr), axis=1\n",
122
+ ")\n",
123
+ "\n",
124
+ "# Update the sampling rate column to reflect the target rate\n",
125
+ "train_df[\"sampling_rate\"] = target_sr\n"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "code",
130
+ "execution_count": null,
131
+ "metadata": {},
132
+ "outputs": [],
133
+ "source": [
134
+ "train_df.sampling_rate.describe()"
135
  ]
136
  },
137
  {
 
166
  "\n",
167
  "# Make random predictions (placeholder for actual model inference)\n",
168
  "true_labels = test_dataset[\"label\"]\n",
169
+ "\n",
170
+ "\n",
171
  "predictions = [random.randint(0, 1) for _ in range(len(true_labels))]\n",
172
  "\n",
173
  "predictions\n",