-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_features.py
201 lines (159 loc) · 6.31 KB
/
load_features.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 11 08:04:34 2021
@author: bilg
"""
import librosa
import librosa.display
import numpy as np
import os
from keras.preprocessing import image
from sklearn.model_selection import train_test_split
def feature_extract(fname):
feat =0
try:
print(fname)
sr=librosa.get_samplerate(fname)
audio,sr = librosa.load(fname,sr=sr)
print('sr=',sr,)
print(' get_samplerate(fname) =',librosa.get_samplerate(fname))
print('audio.shape=',audio.shape)
# audio=audio[np.nonzero(audio>0) ]
print('audio.shape=',audio.shape)
# audio=audio/audio.std()
# audio= (audio-audio.min())/(audio.max()-audio.min())
# print('File loaded')
feat= librosa.feature.melspectrogram(y=audio,sr=sr)
feat = librosa.power_to_db(feat, ref=np.max)
# feat= feat-np.min(feat)
# feat=np.log(feat+1)
feat= (feat-feat.min())/(feat.max()-feat.min())
except:
print('File cannot open')
return feat
def load_features(dir_covid,dir_healthy):
#------------------------------------------------------------------------------
#train data
dir_content_covid = os.listdir(dir_covid)
#test data
dir_content_healthy = os.listdir(dir_healthy)
data_train=[]
max_len=0
sy=0
for fname in dir_content_covid:
if fname[-3:] =='wav':
try:
f=feature_extract(dir_covid+fname)
if f.max()>0:
data_train.append(f)
print('f.shape2=',f.shape)
if max_len<f.shape[1]:
max_len=f.shape[1]
else:
print('error covid: ')
print(fname)
sy=sy+1
#print(f.shape)
except:
print('error covid: ')
print(fname)
s1=len(data_train)
#------------------------------------------------------------------------------
for fname in dir_content_healthy:
if fname[-3:] =='wav':
try:
#print(fname)
f=feature_extract(dir_healthy+fname)
if f.max()>0:
data_train.append(f)
print('f.shape2=',f.shape)
if max_len<f.shape[1]:
max_len=f.shape[1]
else:
print('error healty: ')
print(fname)
sy=sy+1
#print(f.shape)
except:
print('error healty: ')
print(fname)
s=len(data_train)
#------------------------------------------------------------------------------
data_label=np.zeros(s,)
data_label[0:s1]=np.ones(s1,)
#------------------------------------------------------------------------------
print('num of files not opened : ',sy)
print('max_len=',max_len)
return (data_train,data_label)
#return (train_data,train_label),(test_data,test_label)
def imresize(data_train,rows,cols):
s=len(data_train)
data_trainX=np.zeros((s,rows,cols),dtype='float32')
# ** RESIZE to WxH
for i in range(0,s):
#print('data_train[i].shape=',data_train[i].shape)
shp=data_train[i].shape
if shp[1]>cols:
data_trainX[i,:,:]=image.smart_resize(
data_train[i].reshape(data_train[i].shape[0],
data_train[i].shape[1],1),
(rows,cols)).reshape(rows,cols)
else:
R=image.smart_resize(
data_train[i].reshape(data_train[i].shape[0],
data_train[i].shape[1],1),
(rows,data_train[i].shape[1])).reshape(rows,data_train[i].shape[1])
img=np.zeros((rows,cols),dtype='float')
B=np.asarray(R)
img[:B.shape[0],:B.shape[1]]=B
data_trainX[i,:,:]=img
return data_trainX
def load_dataset_5_Fold(dir_covid,dir_healthy,rows,cols):
# Extract features
(data_trainF,train_label)=load_features(dir_covid,dir_healthy)
# resize to WxH
print('** image resize to ', rows,'x',cols)
data_trainX=imresize(data_trainF,rows,cols)
b=data_trainX.shape
train_data=np.zeros((b[0],b[1],b[2],3))
train_data[:,:,:,0]=data_trainX
train_data[:,:,:,1]=data_trainX
train_data[:,:,:,2]=data_trainX
train_data=np.nan_to_num(train_data)
print('train_data.shape=',train_data.shape)
np.savez('datasetcovid.npz',train_data=train_data,train_label=train_label)
return train_data,train_label
def load_dataset(dir_covid,dir_healthy,rows,cols,split=0.2):
# Extract features
(data_trainF,data_labelF)=load_features(dir_covid,dir_healthy)
# resize to WxH
print('** image resize to ', rows,'x',cols)
data_trainX=imresize(data_trainF,rows,cols)
# ** TRAIN-VAL SPLIT
train_data, val_data, train_label , val_label=\
train_test_split(data_trainX,
data_labelF,
test_size=split,
stratify=data_labelF,
shuffle=True)
train_data=np.nan_to_num(train_data)
val_data=np.nan_to_num(val_data)
#train_data=train_data*255.0
#val_data=val_data*255.0
# ** RGB repeat
b=train_data.shape
train_dataX=np.zeros((b[0],b[1],b[2],3))
train_dataX[:,:,:,0]=train_data
train_dataX[:,:,:,1]=train_data
train_dataX[:,:,:,2]=train_data
b=val_data.shape
val_dataX=np.zeros((b[0],b[1],b[2],3))
val_dataX[:,:,:,0]=val_data
val_dataX[:,:,:,1]=val_data
val_dataX[:,:,:,2]=val_data
train_data=train_dataX
val_data=val_dataX
print('len(train_data)=',len(train_data))
print('len(val_data )=',len(val_data))
np.savez('datasetcovid.npz',train_data=train_data,train_label=train_label,val_data=val_data,val_label=val_label)
return train_data,train_label,val_data,val_label