forked from abhyantrika/Transfer_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre_process.py
42 lines (32 loc) · 962 Bytes
/
pre_process.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
import glob
import os
import numpy as np
import vggish_inference_demo
#labels = np.array([total_number_of_samples])
def generate_features(parent_dir):
sub_dirs = os.listdir(parent_dir)
success = 0
failures = 0
for folder in sub_dirs:
files = os.listdir(parent_dir + folder)
for f in files:
try:
label = f.split('-')[1]
print(parent_dir+folder+'/'+ f)
outcome = vggish_inference_demo.main(parent_dir+folder+'/'+f,'features_npz/c'+str(label)+'/'+f+'.npz')
if outcome==1:
success+=1
else:
failures+=1
except:
failures+=1
pass
return success,failures
def one_hot_encode(labels):
n_labels = len(labels)
n_unique_labels = len(np.unique(labels))
one_hot_encode = np.zeros((n_labels,n_unique_labels))
one_hot_encode[np.arange(n_labels), labels] = 1
return one_hot_encode
success,failures = generate_features('UrbanSound8K/audio/')
print(success,failures)