-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path1_pattern_extraction.py
279 lines (240 loc) · 10.2 KB
/
1_pattern_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import numpy as np
import pandas as pd
import scipy as sp
import pickle
from scipy import fft
from time import localtime, strftime
import matplotlib.pyplot as plt
from skimage.morphology import disk,remove_small_objects
from skimage.filter import rank
from skimage.util import img_as_ubyte
import wave
###########################
# Folder Name Setting
###########################
folder = 'J:/DATAMINING/KAGGLE/MLSP_BirdClassification/'
essential_folder = folder+'essential_data/'
supplemental_folder = folder+'supplemental_data/'
spectro_folder =folder+'my_spectro/'
single_spectro_folder =folder+'my_spectro_single/'
dp_folder = folder+'DP/'
###################################################
## Read the Essential Data
## labels, training-test split,file_names etc.
###################################################
# Each audio file has a unique recording identifier ("rec_id"), ranging from 0 to 644.
# The file rec_id2filename.txt indicates which wav file is associated with each rec_id.
rec2f = pd.read_csv(essential_folder + 'rec_id2filename.txt', sep = ',')
# There are 19 bird species in the dataset. species_list.txt gives each a number from 0 to 18.
species = pd.read_csv(essential_folder + 'species_list.txt', sep = ',')
num_species = 19
# The dataset is split into training and test sets.
# CVfolds_2.txt gives the fold for each rec_id. 0 is the training set, and 1 is the test set.
cv = pd.read_csv(essential_folder + 'CVfolds_2.txt', sep = ',')
# This is your main label training data. For each rec_id, a set of species is listed. The format is:
# rec_id,[labels]
raw = pd.read_csv(essential_folder + 'rec_labels_test_hidden.txt', sep = ';')
label = np.zeros(len(raw)*num_species)
label = label.reshape([len(raw),num_species])
for i in range(len(raw)):
line = raw.irow(i)
labels = line[0].split(',')
labels.pop(0) # rec_id == i
for c in labels:
if(c != '?'):
label[i,c] = 1
label = pd.DataFrame(label)
label['rec_id'] = cv.rec_id
label['fold'] = cv.fold
label['filename'] = rec2f.filename
# Sparse training set
# training species 1%--5%--20%
spec_avg = label[label.fold ==0][range(num_species)].mean()
spec_avg.sort()
plt.plot(spec_avg,'go')
# Read the audio files
# /src_wavs
# This folder contains the original wav files for the dataset (both training and test sets).
# These are 10-second mono recordings sampled at 16kHz, 16 bits per sample.
def pic_to_ubyte (pic):
a = (pic-np.min(pic) ) /(np.max(pic - np.min(pic)))
a = img_as_ubyte(a)
return a
# Parameters to create the spectrogram
N = 160000
K = 512
Step = 4
wind = 0.5*(1 -np.cos(np.array(range(K))*2*np.pi/(K-1) ))
ffts = []
def wav_to_floats(filename):
s = wave.open(filename,'r')
strsig = s.readframes(s.getnframes())
y = np.fromstring(strsig, np.short)
s.close()
return y
###############################
## Create the Spectrograms
## Train + Test
###############################
print strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
for file_idx in range(len(label)):
test_flag = label.irow(file_idx)['fold']
fname = label.irow(file_idx)['filename']
species_on_pic = []
for n in range(num_species):
if(label.irow(file_idx)[n] > 0):
species_on_pic.append(n)
S = wav_to_floats(essential_folder+'src_wavs/'+fname+'.wav')
Spectogram = []
for j in range(int(Step*N/K)-Step):
vec = S[j * K/Step : (j+Step) * K/Step] * wind
Spectogram.append(abs(fft(vec,K)[:K/2]))
ffts.append(np.array(Spectogram))
print strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
SPEC_SEGMENTS = []
LOG_SPEC_SEGMENTS = []
MIN_SEGMENT_SIZE = 99
p = 90
#fig = plt.figure(figsize=(20, 10))
for file_idx in range(len(label)):
test_flag = label.irow(file_idx)['fold']
fname = label.irow(file_idx)['filename']
species_on_pic = []
for n in range(num_species):
if(label.irow(file_idx)[n] > 0):
species_on_pic.append(n)
label_count = label.irow(file_idx)[range(num_species)].sum()
bird_spec = label.irow(file_idx)[range(num_species)].argmax() # first bird
if(test_flag < 1 and label_count ==1):
mypic = np.transpose(ffts[file_idx])
mypic_rev = np.zeros_like(mypic)
for i in range(mypic.shape[0]):
mypic_rev[i] = mypic[-i - 1]
mypic_rev_small = mypic_rev[:200,:]
mypic_rev = mypic_rev_small
mypic_rev_log = np.log10(mypic_rev+ 0.001)
mypic_rev_gauss =sp.ndimage.gaussian_filter(mypic_rev, sigma=3)
mypic_rev_log_gauss = sp.ndimage.gaussian_filter(mypic_rev_log, sigma=3)
mypic_rev_gauss_bin = mypic_rev_gauss > np.percentile(mypic_rev_gauss,p)
mypic_rev_log_gauss_bin = mypic_rev_log_gauss > np.percentile(mypic_rev_log_gauss,p)
mypic_rev_gauss_bin_close =sp.ndimage.binary_closing( sp.ndimage.binary_opening(mypic_rev_gauss_bin))
mypic_rev_log_gauss_bin_close =sp.ndimage.binary_closing( sp.ndimage.binary_opening(mypic_rev_log_gauss_bin))
mypic_rev_gauss_grad = rank.gradient(pic_to_ubyte(mypic_rev_gauss), disk(3))
mypic_rev_log_gauss_grad = rank.gradient(pic_to_ubyte(mypic_rev_log_gauss), disk(3))
mypic_rev_gauss_grad_bin = mypic_rev_gauss_grad > np.percentile(mypic_rev_gauss_grad,p)
mypic_rev_log_gauss_grad_bin = mypic_rev_log_gauss_grad > np.percentile(mypic_rev_log_gauss_grad,p )
mypic_rev_gauss_grad_bin_close =sp.ndimage.binary_closing( sp.ndimage.binary_opening(mypic_rev_gauss_grad_bin))
mypic_rev_log_gauss_grad_bin_close =sp.ndimage.binary_closing( sp.ndimage.binary_opening(mypic_rev_log_gauss_grad_bin))
bfh = sp.ndimage.binary_fill_holes(mypic_rev_gauss_grad_bin_close)
bfh_rm = remove_small_objects(bfh, MIN_SEGMENT_SIZE)
log_bfh = sp.ndimage.binary_fill_holes(mypic_rev_log_gauss_grad_bin_close)
log_bfh_rm = remove_small_objects(log_bfh, MIN_SEGMENT_SIZE)
# plt.subplot(6,2,1)
# plt.imshow(mypic_rev,cmap=plt.cm.afmhot_r)
# plt.axis('off')
# plt.title('Spectrogram')
# plt.subplot(6,2,2)
# plt.imshow(mypic_rev_log,cmap=plt.cm.afmhot_r)
# plt.axis('off')
# plt.title('Spectrogram (log)')
# plt.subplot(6,2,3)
# plt.imshow(mypic_rev_log_gauss,cmap=plt.cm.afmhot_r)
# plt.axis('off')
# plt.title('+ Gaussian Filtering')
# plt.subplot(6,2,4)
# plt.imshow(mypic_rev_log,cmap=plt.cm.afmhot_r)
# plt.axis('off')
# plt.title('+ Gaussian Filtering (log)')
# plt.subplot(6,2,5)
# plt.imshow(mypic_rev_gauss_grad,cmap=plt.cm.afmhot_r)
# plt.axis('off')
# plt.title('+ Gradient')
# plt.subplot(6,2,6)
# plt.imshow(mypic_rev_log_gauss_grad,cmap=plt.cm.afmhot_r)
# plt.axis('off')
# plt.title('+ Gradient (log)')
# plt.subplot(6,2,7)
# plt.imshow(mypic_rev_gauss_grad_bin,cmap=plt.cm.gray)
# plt.axis('off')
# plt.title('+ >90%')
# plt.subplot(6,2,8)
# plt.imshow(mypic_rev_log_gauss_grad_bin,cmap=plt.cm.gray)
# plt.axis('off')
# plt.title('+ >90% (log)')
# plt.subplot(6,2,9)
# plt.imshow(mypic_rev_gauss_grad_bin_close,cmap=plt.cm.gray)
# plt.axis('off')
# plt.title('+ binary_closing + binary_opening')
# plt.subplot(6,2,10)
# plt.imshow(mypic_rev_log_gauss_grad_bin_close,cmap=plt.cm.gray)
# plt.axis('off')
# plt.title('+ binary_closing + binary_opening (log)')
#SEGMENTS
labeled_segments, num_seg = sp.ndimage.label(bfh_rm)
# plt.subplot(6,2,11)
# plt.imshow(labeled_segments)
# plt.axis('off')
# plt.title('+ binary_fill_holes + remove_small_objects')
for current_segment_id in range(1,num_seg+1):
current_segment = (labeled_segments == current_segment_id)*1
xr = current_segment.max(axis = 0)
yr = current_segment.max(axis = 1)
xr_max = np.max(xr*np.arange(len(xr)))
xr[xr==0] = xr.shape[0]
xr_min = np.argmin(xr)
yr_max = np.max(yr*np.arange(len(yr)))
yr[yr==0] = yr.shape[0]
yr_min = np.argmin(yr)
segment_frame = [yr_min, yr_max, xr_min, xr_max]
subpic = mypic_rev_gauss[yr_min:yr_max+1,xr_min:xr_max+1]
SPEC_SEGMENTS.append([file_idx, current_segment_id, segment_frame, subpic])
# LOG SEGMENTS
labeled_segments, num_seg = sp.ndimage.label(log_bfh_rm)
# plt.subplot(6,2,12)
# plt.imshow(labeled_segments)
# plt.axis('off')
# plt.title('+ binary_fill_holes + remove_small_objects (log)')
for current_segment_id in range(1,num_seg+1):
current_segment = (labeled_segments == current_segment_id)*1
xr = current_segment.max(axis = 0)
yr = current_segment.max(axis = 1)
xr_max = np.max(xr*np.arange(len(xr)))
xr[xr==0] = xr.shape[0]
xr_min = np.argmin(xr)
yr_max = np.max(yr*np.arange(len(yr)))
yr[yr==0] = yr.shape[0]
yr_min = np.argmin(yr)
segment_frame = [yr_min, yr_max, xr_min, xr_max]
subpic = mypic_rev_log_gauss[yr_min:yr_max+1,xr_min:xr_max+1]
LOG_SPEC_SEGMENTS.append([file_idx, current_segment_id, segment_frame, subpic])
#fig.savefig(single_spectro_folder+str(bird_spec)+'_'+fname+'_patterns.png',dpi = 300)
#fig.clear()
#plt.show()
print strftime("%a, %d %b %Y %H:%M:%S +0000", localtime())
## CHECK THE SEGMENTS:
#N = 10
#fig = plt.figure(figsize=(20, 10))
#for i in range(N):
# for j in range(N):
# plt.subplot(N,N,i*N+j)
# plt.imshow( SPEC_SEGMENTS[i*N+j][3])
#
#
#N = 10
#fig = plt.figure(figsize=(20, 10))
#for i in range(N):
# for j in range(N):
# plt.subplot(N,N,i*N+j)
# plt.imshow( LOG_SPEC_SEGMENTS[-(i*N+j)][3],cmap=plt.cm.afmhot_r)
#
#a = []
#for r in SPEC_SEGMENTS:
# a.append(r[2][1] - r[2][0] )
#
#plt.hist(a)
output = open(dp_folder + 'SPEC_SEGMENTS.pkl', 'wb')
pickle.dump(SPEC_SEGMENTS, output)
output.close()
output = open(dp_folder + 'LOG_SPEC_SEGMENTS.pkl', 'wb')
pickle.dump(LOG_SPEC_SEGMENTS, output)
output.close()