VANILA_EVALUATE.ipynb
10.2 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'\\n평가방법 : positive를 높이는방식\\n\\n본논문\\n- STFT magnitude Spectrun\\n- n=40 log mel filter bank\\n\\n다른논문\\n-STFT maginitude spectogram\\n- n=80 mel scaled filter bank\\n- scale log magnitude\\n- batch nomalization (0,1)\\n- subtract mean overtime on spectogram (for remove frequency dependency noise = colored noise)\\n'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'''\n",
"평가방법 : positive를 높이는방식\n",
"\n",
"본논문\n",
"- STFT magnitude Spectrun\n",
"- n=40 log mel filter bank\n",
"\n",
"다른논문\n",
"-STFT maginitude spectogram\n",
"- n=80 mel scaled filter bank\n",
"- scale log magnitude\n",
"- batch nomalization (0,1)\n",
"- subtract mean overtime on spectogram (for remove frequency dependency noise = colored noise)\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[name: \"/device:CPU:0\"\n",
"device_type: \"CPU\"\n",
"memory_limit: 268435456\n",
"locality {\n",
"}\n",
"incarnation: 6180456371412583691\n",
"]\n"
]
}
],
"source": [
"from tensorflow.python.client import device_lib\n",
"print(device_lib.list_local_devices())\n",
"\n",
"from keras.utils.training_utils import multi_gpu_model\n",
"gpunum = 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"#path 관련 라이브러리\n",
"import glob\n",
"import csv\n",
"\n",
"#csv저장 라이브러리\n",
"import pandas as pd\n",
"\n",
"# Scientific Math 라이브러리 \n",
"import numpy as np\n",
"import librosa\n",
"import librosa.display\n",
"import os\n",
"\n",
"# Visualization 라이브러리\n",
"import matplotlib.pyplot as plt\n",
"import IPython.display as ipd\n",
"\n",
"#keras\n",
"from keras.utils import np_utils\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Conv2D, MaxPooling2D, GRU,Dropout, Flatten,Reshape,BatchNormalization\n",
"from keras.callbacks import ModelCheckpoint, EarlyStopping\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"audio_path2= './SOUNDS/warbler/'\n",
"n_mels = 40\n",
"n_frame = 500\n",
"window_size=1024\n",
"hop_size=512\n",
"sample_rate=25600"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Preparing walber_test_labels.\n",
"Done.\n"
]
}
],
"source": [
"print(\"Preparing walber_test_labels.\")\n",
"labels_2 = []\n",
"with open(audio_path2+'labels.csv', mode='r',encoding='utf-8') as f:\n",
" reader = csv.reader(f)\n",
" for row in reader : \n",
" labels_2.append(row)\n",
"labels_2.sort(key=lambda x:x[0])\n",
"labels_2 = np.array(labels_2) #아...그냥이렇게하면 넘피배열로 바꿀수있구나ㅠ\n",
"labels_2 = labels_2[6000:-1,1]\n",
"print(\"Done.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Preparing walber_melspectogram.\n",
"Done.\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-9-8d8dc96f9568>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Done.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mmel_spectogram_2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmel_spectogram_2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mmel_spectogram_2\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0mmel_spectogram_2\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# prepare waober_test_melspectogram\n",
"print(\"Preparing walber_melspectogram.\")\n",
"mel_spectogram_2 = []\n",
"with open(audio_path2+'mel_spec_4.csv', mode='r',encoding='utf-8') as f:\n",
" reader = csv.reader(f)\n",
" next(reader)\n",
" for row in reader : \n",
" mel_spectogram_2.append(row)\n",
"print(\"Done.\")\n",
"\n",
"mel_spectogram_2 = np.array(mel_spectogram_2)\n",
"mel_spectogram_2= mel_spectogram_2[:,1:]\n",
"\n",
"np.shape(mel_spectogram_2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"X_test = np.reshape(mel_spectogram_2,(2000,40,500,1))\n",
"np.shape(X_test)\n",
"\n",
"Y_test = labels_2\n",
"np.shape(Y_test)\n",
"\n",
"MODEL_SAVE_FOLDER_PATH = './model/'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(None, 40, 500, 96)\n",
"(None, 8, 500, 96)\n",
"(None, 8, 500, 96)\n",
"(None, 4, 500, 96)\n",
"(None, 4, 500, 96)\n",
"(None, 2, 500, 96)\n",
"(None, 2, 500, 96)\n",
"(None, 1, 500, 96)\n",
"(None, 96, 500)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/ipykernel_launcher.py:37: UserWarning: Update your `GRU` call to the Keras 2 API: `GRU(return_sequences=True, units=500)`\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(None, 96, 500)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/ubuntu/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/ipykernel_launcher.py:40: UserWarning: Update your `GRU` call to the Keras 2 API: `GRU(return_sequences=True, units=500)`\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(None, 96, 500)\n",
"(None, 96, 500, 1)\n",
"(None, 96, 1, 1)\n",
"[0.40238386690616607, 0.8035000026226043]\n"
]
}
],
"source": [
"\n",
"import keras.backend.tensorflow_backend as K\n",
"with K.tf.device('/device:GPU:0'):\n",
"\n",
" model = Sequential()\n",
" model.add(Conv2D(96, kernel_size=(5, 5), input_shape=(40, 500,1), padding='same',activation='relu')) #어쩌면 40,500만해야할지두\n",
" print(model.output_shape)\n",
" model.add(BatchNormalization())\n",
" model.add(MaxPooling2D(pool_size=(5,1)))\n",
" model.add(Dropout(0.25))\n",
" print(model.output_shape)\n",
"\n",
" model.add(Conv2D(96, (5, 5), padding='same',activation='relu'))\n",
" print(model.output_shape)\n",
" model.add(BatchNormalization())\n",
" model.add(MaxPooling2D(pool_size=(2,1)))\n",
" model.add(Dropout(0.25))\n",
" print(model.output_shape)\n",
"\n",
" model.add(Conv2D(96, (5, 5), padding='same',activation='relu'))\n",
" print(model.output_shape)\n",
" model.add(BatchNormalization())\n",
" model.add(MaxPooling2D(pool_size=(2,1)))\n",
" model.add(Dropout(0.25))\n",
" print(model.output_shape)\n",
"\n",
" model.add(Conv2D(96, (5, 5), padding='same', activation='relu'))\n",
" print(model.output_shape)\n",
" model.add(BatchNormalization())\n",
" model.add(MaxPooling2D(pool_size=(2,1)))\n",
" model.add(Dropout(0.25))\n",
" print(model.output_shape)\n",
"\n",
" model.add(Reshape((96,500))) #문제될거같은데..\n",
" print(model.output_shape)\n",
"\n",
" model.add(GRU(output_dim=500, return_sequences=True))\n",
" print(model.output_shape)\n",
"\n",
" model.add(GRU(output_dim=500, return_sequences=True))\n",
" print(model.output_shape)\n",
"\n",
" model.add(Reshape((96,500,1))) #문제될거같은데..2\n",
" print(model.output_shape)\n",
"\n",
" model.add(MaxPooling2D(pool_size=(1,500)))\n",
" print(model.output_shape)\n",
"\n",
" model.add(Flatten())\n",
" model.add(Dense(1, activation='sigmoid'))\n",
"\n",
" model.compile(loss='binary_crossentropy',\n",
" optimizer='adam',\n",
" metrics=['accuracy'])\n",
"\n",
" model.load_weights(MODEL_SAVE_FOLDER_PATH + 'bird_sound-' + '17-0.3943.hdf5')\n",
"\n",
" score = model.evaluate(X_test,Y_test,batch_size=200,verbose=2)\n",
" print(\"%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}