Royi commited on
Commit
add6f5e
·
verified ·
1 Parent(s): 584d5c9

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +91 -1
README.md CHANGED
@@ -3,4 +3,94 @@ license: mit
3
  pretty_name: Audio MNIST
4
  size_categories:
5
  - 10K<n<100K
6
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  pretty_name: Audio MNIST
4
  size_categories:
5
  - 10K<n<100K
6
+ ---
7
+
8
+ # Audio MNIST
9
+
10
+ Based on [`AudioMNIST`](https://github.com/soerenab/AudioMNIST).
11
+
12
+ ## Generation of the Parquet File
13
+
14
+ Given the path to the `data` folder from the [source](https://github.com/soerenab/AudioMNIST) as `audioMNISTFolderPath`:
15
+
16
+ ```python
17
+ import numpy as np
18
+ import pandas as pd
19
+
20
+ import json
21
+
22
+ # Load all wave files in AudioMNIST dataset
23
+ # Parse each file name as <digit>_<speaker>_<index>.wav
24
+
25
+ dfData = pd.DataFrame(columns = ['Digit', 'Speaker', 'Index', 'SampleRate', 'NumSamples', 'Accent', 'Age', 'Gender', 'NativeSpeaker', 'Continent', 'Country', 'City', 'Room'])
26
+
27
+ with open(os.path.join(audioMNISTFolderPath, 'audioMNIST_meta.txt'), 'r') as f:
28
+ dMetadata = json.load(f)
29
+
30
+ lWaveFiles = []
31
+
32
+ lFolders = os.listdir(audioMNISTFolderPath)
33
+ lFolders = [fld for fld in lFolders if os.path.isdir(os.path.join(audioMNISTFolderPath, fld))]
34
+ lFolders.sort()
35
+
36
+ fileIdx = -1
37
+ for fld in lFolders:
38
+ lFiles = os.listdir(os.path.join(audioMNISTFolderPath, fld))
39
+ lFiles = [f for f in lFiles if f.endswith('.wav')]
40
+ lFiles.sort()
41
+ print(f'Folder {fld}: {len(lFiles)} files')
42
+ for f in lFiles:
43
+ fileIdx += 1
44
+ # Parse File Name
45
+ digitIdx, speakerIdx, recIdx = f[:-4].split('_')
46
+ sampleRate, vAudioData = sp.io.wavfile.read(os.path.join(audioMNISTFolderPath, fld, f))
47
+ lWaveFiles.append(vAudioData)
48
+
49
+ dfData.loc[fileIdx, 'Digit'] = int(digitIdx)
50
+ dfData.loc[fileIdx, 'Speaker'] = int(speakerIdx)
51
+ dfData.loc[fileIdx, 'Index'] = int(recIdx)
52
+ dfData.loc[fileIdx, 'SampleRate'] = int(sampleRate)
53
+ dfData.loc[fileIdx, 'NumSamples'] = int(len(vAudioData))
54
+ # Parse Metadata
55
+ metaIdx = f'{int(speakerIdx):02d}'
56
+ dfData.loc[fileIdx, 'Accent'] = dMetadata[metaIdx]['accent']
57
+ dfData.loc[fileIdx, 'Age'] = int(dMetadata[metaIdx]['age'])
58
+ dfData.loc[fileIdx, 'Gender'] = dMetadata[metaIdx]['gender']
59
+ dfData.loc[fileIdx, 'NativeSpeaker'] = dMetadata[metaIdx]['native speaker']
60
+ # Parse Continent, Country, City
61
+ locationStr = dMetadata[metaIdx]['origin']
62
+ # Remove spaces, split by ','
63
+ locationStr = locationStr.replace(' ', '')
64
+ contStr, countryStr, cityStr = locationStr.split(',')
65
+ dfData.loc[fileIdx, 'Continent'] = contStr
66
+ dfData.loc[fileIdx, 'Country'] = countryStr
67
+ dfData.loc[fileIdx, 'City'] = cityStr
68
+ dfData.loc[fileIdx, 'Room'] = dMetadata[metaIdx]['recordingroom']
69
+
70
+ # Generate DataFrame of the Audio Data
71
+ maxSignals = dfData.shape[0]
72
+ maxNumSamples = dfData['NumSamples'].max()
73
+
74
+ mA = np.zeros((maxSignals, maxNumSamples), dtype = np.int16)
75
+ for ii, vA in enumerate(lWaveFiles):
76
+ mA[ii, :len(vA)] = vA
77
+
78
+ dfAudio = pd.DataFrame(data = mA, columns = [f'{sampleIdx:d}' for sampleIdx in range(maxNumSamples)])
79
+
80
+ # Generate the AudioMNIST Data Frame
81
+ dfAudioMnist = pd.concat([dfData, dfAudio], axis = 1)
82
+
83
+ # Set the Type per column
84
+ dfAudioMnist['Digit'] = dfAudioMnist['Digit'].astype(np.int8)
85
+ dfAudioMnist['Speaker'] = dfAudioMnist['Speaker'].astype(np.int8)
86
+ dfAudioMnist['Index'] = dfAudioMnist['Index'].astype(np.int32)
87
+ dfAudioMnist['SampleRate'] = dfAudioMnist['SampleRate'].astype(np.int32)
88
+ dfAudioMnist['NumSamples'] = dfAudioMnist['NumSamples'].astype(np.int32)
89
+ dfAudioMnist['Age'] = dfAudioMnist['Age'].astype(np.int32)
90
+ dfAudioMnist['Gender'] = dfAudioMnist['Gender'].map({'male': 'Male', 'female': 'Female'})
91
+ dfAudioMnist['NativeSpeaker'] = dfAudioMnist['NativeSpeaker'].map({'yes': True, 'no': False})
92
+ dfAudioMnist['NativeSpeaker'] = dfAudioMnist['NativeSpeaker'].astype(bool)
93
+
94
+ # Export to Parquet
95
+ dfAudioMnist.to_parquet(os.path.join(audioMNISTFolderPath, 'AudioMNIST.parquet'), index = False)
96
+ ```