-
Notifications
You must be signed in to change notification settings - Fork 25
/
esc10_extraction.py
69 lines (58 loc) · 2.57 KB
/
esc10_extraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
"""
@author: Jason Zhang
@github: https://github.com/JasonZhang156/Sound-Recognition-Tutorial
"""
import numpy as np
from glob import glob
import os
import librosa
import feature_extraction as fe
import warnings
warnings.filterwarnings('ignore')
def extract_esc10_feat():
# 5-fold cross validation settings
cv_index = np.load('cvindex.npz')
TR = cv_index['TR'] # train fold
TE = cv_index['TE'] # test fold
# 5 fold extracting
for fold in range(1, 6):
print('Extracting training set and test set for fold {}'.format(fold))
train_feats = []
train_labels = []
test_feats = []
test_labels = []
class_list = np.sort(glob('./data/esc10/audio/*'))
for index, classpath in enumerate(class_list):
print('Total numbers of audio classes: ', len(class_list))
audio_list = np.sort(glob(os.path.join(classpath, '*.ogg')))
# for training
train_audio = audio_list[TR[fold-1]]
for i in range(len(train_audio)):
print('Processing sound class: ', os.path.basename(classpath), index+1, '/', len(class_list),
' --- training set: ', i+1, '/', len(train_audio))
y, fs = librosa.load(train_audio[i], sr=22050)
# feat = fe.extract_mfcc(y, fs, 3)
feat = fe.extract_logmel(y, fs, 3)
train_feats.append(feat)
train_labels.append(index)
# for test
test_audio = audio_list[TE[fold - 1]]
for j in range(len(test_audio)):
print('Processing sound class: ', os.path.basename(classpath), index+1, '/', len(class_list),
' --- test set: ', j+1, '/', len(test_audio))
y, fs = librosa.load(test_audio[j], sr=22050)
# feat = fe.extract_mfcc(y, fs, 3)
feat = fe.extract_logmel(y, fs, 3)
test_feats.append(feat)
test_labels.append(index)
train_feats = np.array(train_feats)
train_labels = np.array(train_labels)
test_feats = np.array(test_feats)
test_labels = np.array(test_labels)
# np.savez('./data/esc10/feature/esc10_mfcc_fold{}.npz'.format(fold),
# train_x=train_feats, train_y=train_labels, test_x=test_feats, test_y=test_labels)
np.savez('./data/esc10/feature/esc10_logmel_fold{}.npz'.format(fold),
train_x=train_feats, train_y=train_labels, test_x=test_feats, test_y=test_labels)
if __name__ == '__main__':
extract_esc10_feat()