-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_validation.py
1292 lines (1187 loc) · 57.9 KB
/
lib_validation.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
whislte classification using audio signals
4-fold cross-validation
Created on 12/9/19
@author: atoultaro
"""
import os
import glob
import numpy as np
import matplotlib.pyplot as plt
from math import floor, ceil
import gc
import lib_feature
import datetime
import re
from sklearn.metrics import confusion_matrix, balanced_accuracy_score, classification_report, f1_score
from sklearn.model_selection import train_test_split
import librosa
from tensorflow.keras import backend
from tensorflow.keras.losses import categorical_crossentropy
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import Adadelta
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard, EarlyStopping
from tensorflow.keras.models import load_model
from tensorflow.keras.callbacks import Callback
from lib_feature import data_generator
# from whistle_classifier.capecod_classifier import metrics_two_fold, find_best_model
from contextlib import redirect_stdout
import itertools
from lib_model import resnet34_expt, resnet18_expt
def contour_data(file_contour, time_reso):
print('Retrieving contours...')
contour_target_ff = []
len_contour = len(file_contour)
print('len_contour: '+str(len_contour))
time_min = 86400.0
time_max = 0.0
freq_high = 0.0
freq_low = 192000.0
# read contours into the var contour_target_ff
for cc in range(len_contour):
time_contour = file_contour[cc]['Time']
freq_contour = file_contour[cc]['Freq']
if time_contour.shape[0] > 1:
# linear interpolation
# time_contour_interp = np.arange(time_contour[0], time_contour[-1],
# time_reso)
new_start_time = round(time_contour[0]/time_reso)*time_reso
new_step = ceil((time_contour[-1] - time_contour[0])/time_reso)
time_contour_interp = np.arange(new_start_time, new_start_time+new_step*time_reso, time_reso)
time_min = np.min((time_contour_interp[0], time_min))
time_max = np.max((time_contour_interp[-1], time_max))
freq_contour_interp = np.interp(time_contour_interp, time_contour,
freq_contour)
freq_high = np.max((np.max(freq_contour_interp), freq_high))
freq_low = np.min((np.min(freq_contour_interp), freq_low))
contour_target_ff_cc = dict()
contour_target_ff_cc['Time'] = time_contour_interp
contour_target_ff_cc['Freq'] = freq_contour_interp
contour_target_ff.append(contour_target_ff_cc)
return contour_target_ff, time_min, time_max, freq_low, freq_high
def fea_ext_dcldc2011(contour_target_list, sound_dir, conf, img_folder,
save_file, plot=False, freq_ind_low=64,
fmin=4000.0, bins_per_octave=36, n_bins=144):
'''
Convert whistle contours into sequences of fixed length
:param contour_target_list:
time_reso = 0.1, context_winsize=10.0, ratio_thre=0.02
:return:
df_target:
'''
freq_low_all = 192000.0 # an realistic upperbound
freq_high_all = 0.0
whistle_image_list = []
label_list = []
if plot:
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
if not os.path.exists(img_folder):
os.mkdir(img_folder)
for ss in conf['species_name']:
os.mkdir(os.path.join(img_folder, ss))
data_count = 1
for ff in range(len(contour_target_list)):
filename = contour_target_list[ff][0]
print('\n'+filename)
label_contour = contour_target_list[ff][1]
file_contour = contour_target_list[ff][2]
contour_target_ff, start_time, end_time, freq_low, freq_high = \
contour_data(file_contour, conf['time_reso'])
freq_high_all = np.max((freq_high, freq_high_all))
freq_low_all = np.min((freq_low, freq_low_all))
timesteps = ceil((end_time - start_time)/conf['time_reso'])+1
print("Start time: "+str(start_time))
print("Stop time: " + str(end_time))
# spectrogram named whistle_freq for each file
sound_path = os.path.join(sound_dir, label_contour, filename+'.wav')
samples, _ = librosa.load(sound_path, sr=conf['sample_rate'], offset=start_time, duration=end_time-start_time+2.*conf['time_reso'])
whistle_freq = librosa.feature.melspectrogram(samples,
sr=conf['sample_rate'],
hop_length=conf['hop_length'],
power=1)
whistle_presence = np.zeros((int(timesteps)))
for cc in contour_target_ff:
time_ind_start = int(floor((cc['Time'][0]-start_time)/conf['time_reso']))
for ii in range(cc['Time'].shape[0]):
whistle_presence[time_ind_start+ii] = 1.0
# cut whistle_freq into segments for data samples
size_time = int(conf['context_winsize']/conf['time_reso'])
size_hop = int(conf['context_hopsize']/conf['time_reso'])
# freq_high = whistle_freq.shape[0]
# freq_low = conf['freq_ind_low']
# freq_low = conf['freq_low']
for tt in range(floor((whistle_freq.shape[1]-size_time)/size_hop)):
# whistle_image = whistle_freq[freq_low:freq_high, tt*size_hop:tt*size_hop+size_time]
whistle_image = whistle_freq[:, tt*size_hop:tt*size_hop+size_time]
whistle_presence_seg = whistle_presence[tt * size_hop:tt * size_hop + size_time]
if whistle_presence_seg.sum() >= conf['contour_timethre']:
# feature extraction
whistle_image = lib_feature.feature_whistleness(whistle_image)
# whistle_median = species_lib.nopulse_median(whistle_image)
# whistle_image = (species_lib.avg_sub(whistle_median)).T
whistle_image_list.append(whistle_image)
label_list.append(conf['species_id'][label_contour])
if plot is True:
ax.matshow(whistle_image, origin='lower')
ax.title.set_text(str(data_count)+': '+label_contour)
ax.xaxis.tick_bottom()
fig.canvas.draw()
plt.savefig(os.path.join(img_folder, label_contour, str(data_count)+'_'+label_contour+'.png'))
data_count += 1 # ? complex when having noise class
# print('stop for images')
elif conf['class_noise'] & (whistle_presence_seg.sum() == 0.0): # no labels here!
# feature extraction
whistle_image = lib_feature.feature_whistleness(whistle_image)
# whistle_median = species_lib.nopulse_median(whistle_image)
# whistle_image = (species_lib.avg_sub(whistle_median)).T
whistle_image_list.append(whistle_image)
label_list.append(conf['species_id']['noise'])
data_count += 1
# noise class image here!
whistle_image = np.asarray(whistle_image_list)
np.savez(save_file, whistle_image=whistle_image, label=label_list)
return whistle_image, label_list, freq_high_all, freq_low_all
# def prepare_data(contour_target_list, conf, img_folder, save_file, plot=False):
# '''
# Convert whistle contours into sequences of fixed length
# :param contour_target_list:
# time_reso = 0.1, context_winsize=10.0, ratio_thre=0.02
# :return:
# df_target:
# '''
# freq_low_all = 192000.0
# freq_high_all = 0.0
# whistle_image_list = []
# label_list = []
#
# if plot:
# plt.ion()
# fig = plt.figure()
# ax = fig.add_subplot(111)
#
# if not os.path.exists(img_folder):
# os.mkdir(img_folder)
# for ss in conf['species_name']:
# os.mkdir(os.path.join(img_folder, ss))
#
# data_count = 1
# for ff in range(len(contour_target_list)):
# filename = contour_target_list[ff][0]
# print('\n'+filename)
# label_contour = contour_target_list[ff][1]
# file_contour = contour_target_list[ff][2]
#
# contour_target_ff, start_time, end_time, freq_low, freq_high = \
# contour_data(file_contour, conf['time_reso'])
# freq_high_all = np.max((freq_high, freq_high_all))
# freq_low_all = np.min((freq_low, freq_low_all))
#
# timesteps = ceil((end_time - start_time)/conf['time_reso'])+1
# print("Start time: "+str(start_time))
# print("Stop time: " + str(end_time))
#
# # Binary spectrogram
# # convert whistle freq into into a 2d feature map: whistle_freq for each file
# whistle_freq = np.zeros((conf['fft_size'], int(timesteps)))
# for cc in contour_target_ff:
# time_ind_start = int(floor((cc['Time'][0]-start_time)/conf['time_reso']))
# freq_ind = (np.floor(cc['Freq']/conf['sample_rate']*conf['fft_size'])).astype('int')
# for ii in range(cc['Time'].shape[0]):
# try:
# whistle_freq[freq_ind[ii], time_ind_start+ii] = 1.0
# except:
# print('stop!')
#
# if conf['spectro_dilation']:
# # whistle_freq_smooth = cv2.GaussianBlur(whistle_freq, (3, 3), 0, 0, cv2.BORDER_DEFAULT)
# kernel = np.ones((3, 3)).astype(np.uint8)
# kernel[0, 0] = 0
# kernel[0, 2] = 0
# kernel[2, 0] = 0
# kernel[2, 2] = 0
# whistle_freq_smooth = cv2.dilate(whistle_freq, kernel, cv2.BORDER_DEFAULT)
# whistle_freq = whistle_freq_smooth
#
# # plt.matshow(whistle_freq); plt.show()
# # print('whistle_freq shape: '+str(whistle_freq.shape[0])+', '+str(whistle_freq.shape[1]) )
#
# # cut whistle_freq into segments for data samples
# size_time = int(conf['context_winsize']/conf['time_reso'])
# size_hop = int(conf['context_hopsize']/conf['time_reso'])
# freq_high = conf['freq_ind_high']
# freq_low = conf['freq_ind_low']
# for tt in range(floor((whistle_freq.shape[1]-size_time)/size_hop)):
# whistle_image = whistle_freq[freq_low:freq_high, tt*size_hop:tt*size_hop+size_time]
# # if whistle_image.sum()/whistle_image.shape[0]/whistle_image.shape[1] >= 0.01:
# # if whistle_image.sum() >= 0.1*conf['img_t']:
# # print('whistle_image.sum: '+str(whistle_image.sum()))
# # plt.draw()
# # plt.pause(0.0001)
# # plt.clf()
# # plt.show()
# if (whistle_image.sum(axis=0) > 0).sum() >= conf['contour_timethre']:
# whistle_image_list.append(whistle_image)
# label_list.append(conf['species_id'][label_contour])
#
# if plot is True:
# ax.matshow(whistle_image, origin='lower')
# ax.title.set_text(str(data_count)+': '+label_contour)
# ax.xaxis.tick_bottom()
# fig.canvas.draw()
# plt.savefig(os.path.join(img_folder, label_contour, str(data_count)+'_'+label_contour+'.png'))
# data_count += 1
# # print('stop for images')
#
# whistle_image_arr = np.asarray(whistle_image_list)
# # save image array
# if conf['numpy_data_output']:
# np.savez(save_file, whistle_image=whistle_image_arr, label=label_list)
#
# return whistle_image_arr, label_list, freq_high_all, freq_low_all
#
#
# def prepare_data_mask(contour_target_list, sound_dir, conf, img_folder, save_file, plot=False):
# '''
# Convert whistle contours into sequences of fixed length
# :param contour_target_list:
# time_reso = 0.1, context_winsize=10.0, ratio_thre=0.02
# :return:
# df_target:
# '''
# freq_low_all = 192000.0 # an realistic upperbound
# freq_high_all = 0.0
# whistle_image_list = []
# label_list = []
#
# if plot:
# plt.ion()
# fig = plt.figure()
# ax = fig.add_subplot(111)
#
# if not os.path.exists(img_folder):
# os.mkdir(img_folder)
# for ss in conf['species_name']:
# os.mkdir(os.path.join(img_folder, ss))
#
# data_count = 1
# for ff in range(len(contour_target_list)):
# filename = contour_target_list[ff][0]
# print('\n'+filename)
# label_contour = contour_target_list[ff][1]
# file_contour = contour_target_list[ff][2]
#
# contour_target_ff, start_time, end_time, freq_low, freq_high = \
# contour_data(file_contour, conf['time_reso'])
# freq_high_all = np.max((freq_high, freq_high_all))
# freq_low_all = np.min((freq_low, freq_low_all))
#
# timesteps = ceil((end_time - start_time)/conf['time_reso'])+1
# print("Start time: "+str(start_time))
# print("Stop time: " + str(end_time))
#
# # spectrogram named whistle_freq for each file
# sound_path = os.path.join(sound_dir, label_contour, filename+'.wav')
# samples, _ = librosa.load(sound_path, sr=conf['sample_rate'], offset=start_time, duration=end_time-start_time+2.*conf['time_reso'])
# whistle_freq0 = np.abs(librosa.pseudo_cqt(samples, sr=conf['sample_rate'],
# hop_length=conf['hop_length'],
# fmin=4000.0, bins_per_octave=36,
# n_bins=144))
#
# # Masked spectrogram
# whistle_freq = np.zeros((whistle_freq0.shape[0], whistle_freq0.shape[1]))
# for cc in contour_target_ff:
# time_ind_start = int(floor((cc['Time'][0]-start_time)/conf['time_reso']))
# # freq_ind = (np.floor(cc['Freq']/conf['sample_rate']*conf['fft_size'])).astype('int')
# freq_ind = (np.log2(cc['Freq']/4000.0)*36.).astype('int')
# freq_ind[freq_ind<0] = 0.0
# freq_ind[freq_ind >= 144] = 143
#
# if False: # width-1 mask
# for ii in range(cc['Time'].shape[0]):
# whistle_freq[freq_ind[ii]-1:freq_ind[ii]+1+1, time_ind_start+ii] = whistle_freq0[freq_ind[ii]-1:freq_ind[ii]+1+1, time_ind_start+ii]
#
# # rasterization mask
# for ii in range(1, cc['Time'].shape[0]):
# if freq_ind[ii] > freq_ind[ii-1]:
# whistle_freq[freq_ind[ii-1]-1:freq_ind[ii]+1+1, time_ind_start+ii-1:time_ind_start+ii+1] = whistle_freq0[freq_ind[ii-1]-1:freq_ind[ii]+1+1, time_ind_start+ii-1:time_ind_start+ii+1]
# elif freq_ind[ii] < freq_ind[ii-1]:
# whistle_freq[freq_ind[ii]-1:freq_ind[ii-1]+1+1, time_ind_start+ii-1:time_ind_start+ii+1] = whistle_freq0[freq_ind[ii]-1:freq_ind[ii-1]+1+1, time_ind_start+ii-1:time_ind_start+ii+1]
# else: # freq_ind[ii] == freq_ind[ii-1]
# whistle_freq[freq_ind[ii]-1:freq_ind[ii]+1+1, time_ind_start+ii-1:time_ind_start+ii+1] = whistle_freq0[freq_ind[ii]-1:freq_ind[ii]+1+1, time_ind_start+ii-1:time_ind_start+ii+1]
#
# whistle_presence = np.zeros((int(timesteps)))
# for cc in contour_target_ff:
# time_ind_start = int(floor((cc['Time'][0]-start_time)/conf['time_reso']))
# for ii in range(cc['Time'].shape[0]):
# whistle_presence[time_ind_start+ii] = 1.0
#
# # cut whistle_freq into segments for data samples
# size_time = int(conf['context_winsize']/conf['time_reso'])
# size_hop = int(conf['context_hopsize']/conf['time_reso'])
# freq_high = conf['freq_ind_high']
# freq_low = conf['freq_ind_low']
# for tt in range(floor((whistle_freq.shape[1]-size_time)/size_hop)):
# whistle_image = whistle_freq[freq_low:freq_high, tt*size_hop:tt*size_hop+size_time]
# whistle_presence_seg = whistle_presence[tt * size_hop:tt * size_hop + size_time]
# # plt.draw()
# # plt.pause(0.0001)
# # plt.clf()
# # plt.show()
# # if (whistle_image.sum(axis=0) > 0).sum() >= conf['contour_timethre']:
# if whistle_presence_seg.sum() >= conf['contour_timethre']:
# whistle_image_list.append(whistle_image)
# label_list.append(conf['species_id'][label_contour])
#
# if plot is True:
# ax.matshow(whistle_image, origin='lower')
# ax.title.set_text(str(data_count)+': '+label_contour)
# ax.xaxis.tick_bottom()
# fig.canvas.draw()
# plt.savefig(os.path.join(img_folder, label_contour, str(data_count)+'_'+label_contour+'.png'))
# # plt.clf()
# data_count += 1 # ? complex when having noise class
# # print('stop for images')
# elif conf['class_noise'] & (whistle_presence_seg.sum() == 0.0): # no labels here!
# whistle_image_list.append(whistle_image)
# label_list.append(conf['species_id']['noise'])
# data_count += 1
# # noise class image here!
# whistle_image = np.asarray(whistle_image_list)
# # save image array
# if conf['numpy_data_output']:
# np.savez(save_file, whistle_image=whistle_image, label=label_list)
#
# # Shuffle: working on
# # label_idx = [ii for ii in range(len(label_list))]
# # random.shuffle(label_idx)
# # whistle_image_new = [whistle_image[tt] for tt in label_idx]
# # label_list_new = to_categorical(label_list)[label_idx, :]
#
# return whistle_image, label_list, freq_high_all, freq_low_all
class ConfusionMatrixPlotter(Callback):
"""Plot the confusion matrix on a graph and update after each epoch
# Arguments
X_val: The input values
Y_val: The expected output values
classes: The categories as a list of string names
normalize: True - normalize to [0,1], False - keep as is
cmap: Specify matplotlib colour map
title: Graph Title
"""
def __init__(self, X_val, Y_val, classes, normalize=False,
cmap=plt.cm.Blues, title='Confusion Matrix'):
self.X_val = X_val
self.Y_val = Y_val
self.title = title
self.classes = classes
self.normalize = normalize
self.cmap = cmap
plt.ion()
# plt.show()
plt.figure()
plt.title(self.title)
def on_train_begin(self, logs={}):
pass
def on_epoch_end(self, epoch, logs={}):
plt.clf()
pred = self.model.predict(self.X_val)
max_pred = np.argmax(pred, axis=1)
max_y = np.argmax(self.Y_val, axis=1)
cnf_mat = confusion_matrix(max_y, max_pred)
if self.normalize:
cnf_mat = cnf_mat.astype('float') / cnf_mat.sum(axis=1)[:,
np.newaxis]
thresh = cnf_mat.max() / 2.
for i, j in itertools.product(range(cnf_mat.shape[0]),
range(cnf_mat.shape[1])):
plt.text(j, i, cnf_mat[i, j], horizontalalignment="center",
color="white" if cnf_mat[i, j] > thresh else "black")
plt.imshow(cnf_mat, interpolation='nearest', cmap=self.cmap)
# Labels
tick_marks = np.arange(len(self.classes))
plt.xticks(tick_marks, self.classes, rotation=45)
plt.yticks(tick_marks, self.classes)
plt.colorbar()
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
# plt.draw()
plt.show()
plt.pause(0.001)
def one_fold_validate(model_name, whistle_image_target_4d, label_target,
whistle_image_validate_4d, label_validate,
conf, fold_id=None):
label_target_cat = to_categorical(label_target)
label_validate_cat = to_categorical(label_validate)
model_name_func = globals()[model_name]
# model = model_name_func((conf['img_f'], conf['img_t'], 1), depth=20, num_class=4, num_stack=3, num_filters=32)
model = model_name_func(conf)
# model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}.hdf5'
model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}_valacc_{val_accuracy:.4f}.hdf5'
if conf['comment'] is None:
conf['comment']=''
log_dir = make_folder_time_now(folder_out=conf['log_dir'], folder_comment=conf['comment'])
# if not os.path.exists(log_dir1):
# os.mkdir(log_dir1)
check_path = os.path.join(log_dir, model_name_format)
if fold_id == 1:
with open(os.path.join(log_dir, 'architecture.txt'), 'w') as f:
with redirect_stdout(f):
# print('')
for kk in sorted(list(conf.keys())):
print(kk + ' ==>> ' + str(conf[kk]))
model.summary()
# checkpoint
checkpoint = ModelCheckpoint(check_path, monitor='val_loss', verbose=1,
save_best_only=True)
early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1,
patience=conf['patience'])
if conf['confusion_callback']:
cm_plot = ConfusionMatrixPlotter(whistle_image_validate_4d,
label_validate_cat, conf['species_name'])
# model compile
model.compile(loss=categorical_crossentropy,
optimizer=Adadelta(lr=conf['learning_rate']),
# optimizer=Adam(lr=conf['learning_rate']),
metrics=['accuracy'])
model.summary()
count_species = label_target_cat.sum(axis=0)+1e-6
weight_curr = (count_species.max() / count_species).tolist()
conf["class_weight"] = {0: weight_curr[0], 1: weight_curr[1]}
if conf['confusion_callback']:
callback_list = [checkpoint, TensorBoard(log_dir=log_dir), cm_plot,
early_stop]
else:
callback_list = [checkpoint, TensorBoard(log_dir=log_dir), early_stop]
model.fit(whistle_image_target_4d, label_target_cat,
batch_size=conf['batch_size'], epochs=conf['epoch'],
verbose=1, validation_split=0.2,
callbacks=callback_list, class_weight=conf["class_weight"])
# re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4}).hdf5'
re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4})_valacc_\d+.\d{4}.hdf5'
best_model_path, _ = find_best_model(log_dir, re_model_name_format,
is_max=False, purge=True)
conf['best_model'] = best_model_path
model = load_model(best_model_path)
y_pred_prob = model.predict(whistle_image_validate_4d)
y_pred2 = np.argmax(y_pred_prob, axis=1)
metrics_two_fold(label_validate, y_pred2, log_dir, 'accuracy_fold.txt',
conf, mode='fold')
np.savetxt(os.path.join(log_dir, 'pred_label.txt'), y_pred2, delimiter=',', fmt='%d')
np.savetxt(os.path.join(log_dir, 'pred_prob.txt'), y_pred_prob, delimiter=',', fmt='%.6f')
del model
gc.collect()
backend.clear_session()
return y_pred2, y_pred_prob, best_model_path
# def one_fold_validate_fit_only(model_name, whistle_image_target_4d,
# label_target, conf, fold_id=1):
# label_target_cat = to_categorical(label_target)
# # label_validate_cat = to_categorical(label_validate)
#
# model_name_func = globals()[model_name]
# # model = model_name_func((conf['img_f'], conf['img_t'], 1), depth=20, num_class=4, num_stack=3, num_filters=32)
# model = model_name_func(conf)
# model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}_valacc_{val_acc:.4f}.hdf5'
# log_dir1 = os.path.join(conf['log_dir'], 'fold'+str(fold_id))
# if not os.path.exists(log_dir1):
# os.mkdir(log_dir1)
# check_path = os.path.join(log_dir1, model_name_format)
#
# if fold_id == 1:
# with open(os.path.join(conf['log_dir'], 'architecture.txt'), 'w') as f:
# with redirect_stdout(f):
# # print('')
# for kk in sorted(list(conf.keys())):
# print(kk + ' ==>> ' + str(conf[kk]))
# model.summary()
#
# # checkpoint
# checkpoint = ModelCheckpoint(check_path, monitor='val_loss', verbose=0,
# save_best_only=True)
# early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1,
# patience=conf['patience'])
#
# # model compile
# model.compile(loss=categorical_crossentropy,
# optimizer=Adadelta(lr=conf['learning_rate']),
# # optimizer=Adam(lr=conf['learning_rate']),
# metrics=['accuracy'])
# model.summary()
#
# count_species1 = label_target_cat.sum(axis=0).tolist()
# conf["class_weight"] = (
# max(count_species1) / np.array(count_species1)).tolist()
#
# callback_list = [checkpoint, TensorBoard(log_dir=log_dir1), early_stop]
#
# model.fit(whistle_image_target_4d, label_target_cat,
# batch_size=conf['batch_size'], epochs=conf['epoch'],
# verbose=1, validation_split=0.2,
# callbacks=callback_list, class_weight=conf["class_weight"])
# re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4})_valacc_\d+.\d{4}.hdf5'
# best_model_path, _ = find_best_model(log_dir1, re_model_name_format,
# is_max=False, purge=True)
# conf['best_model'] = best_model_path
# # model = load_model(best_model_path)
#
# return best_model_path
#
#
# def one_fold_validate_fit_only_talos(model_name, whistle_image_target_4d,
# label_target, conf, params, fold_id=1):
# label_target_cat = to_categorical(label_target)
# # label_validate_cat = to_categorical(label_validate)
#
# model_name_func = globals()[model_name]
# # model = model_name_func((conf['img_f'], conf['img_t'], 1), depth=20, num_class=4, num_stack=3, num_filters=32)
# model = model_name_func(conf)
# model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}_valacc_{val_acc:.4f}.hdf5'
# log_dir1 = os.path.join(conf['log_dir'], 'fold'+str(fold_id))
# if not os.path.exists(log_dir1):
# os.mkdir(log_dir1)
# check_path = os.path.join(log_dir1, model_name_format)
#
# if fold_id == 1:
# with open(os.path.join(conf['log_dir'], 'architecture.txt'), 'w') as f:
# with redirect_stdout(f):
# # print('')
# for kk in sorted(list(conf.keys())):
# print(kk + ' ==>> ' + str(conf[kk]))
# model.summary()
#
# # checkpoint
# checkpoint = ModelCheckpoint(check_path, monitor='val_loss', verbose=0,
# save_best_only=True)
# early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1,
# patience=conf['patience'])
#
# # model compile
# model.compile(loss=categorical_crossentropy,
# optimizer=Adadelta(lr=conf['learning_rate']),
# # optimizer=Adam(lr=conf['learning_rate']),
# metrics=['accuracy'])
# model.summary()
#
# count_species1 = label_target_cat.sum(axis=0).tolist()
# conf["class_weight"] = (
# max(count_species1) / np.array(count_species1)).tolist()
#
# callback_list = [checkpoint, TensorBoard(log_dir=log_dir1), early_stop]
#
# model.fit(whistle_image_target_4d, label_target_cat,
# batch_size=conf['batch_size'], epochs=conf['epoch'],
# verbose=1, validation_split=0.2,
# callbacks=callback_list, class_weight=conf["class_weight"])
# re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4})_valacc_\d+.\d{4}.hdf5'
# best_model_path, _ = find_best_model(log_dir1, re_model_name_format,
# is_max=False, purge=True)
# conf['best_model'] = best_model_path
# # model = load_model(best_model_path)
#
# return best_model_path
# def validate_on_files(best_model_path_train, fea_out_fold_validate, conf, fold_id):
# model = load_model(best_model_path_train)
#
# fea_species_file_list = glob.glob(os.path.join(fea_out_fold_validate, '*.npz'))
# label_pred = []
# label_truth = []
# file_test = []
# y_pred_prob_tot_list = []
# for ff in fea_species_file_list:
# species_filename = os.path.basename(ff)
# print(species_filename)
# fea_curr = np.load(ff)
# # features
# fea_file_4d = fea_curr['fea_pos']
# # fea_file_4d = fea_pos_4d[:, conf['img_f']:, :, :] + np.finfo(float).eps
# # fea_file_4d = species_lib.unit_vector(fea_file_4d)
#
# # classification
# if fea_file_4d.shape[0] == 0:
# continue
# else:
# y_pred_prob = model.predict(fea_file_4d)
# y_pred_prob_tot = y_pred_prob.mean(axis=0)
# y_pred_prob_tot_list.append(y_pred_prob_tot)
# y_pred2 = np.argmax(y_pred_prob_tot[:-1]) # predicted species
# label_pred.append(y_pred2)
#
# # find the truth label
# file_test.append(species_filename)
# species = species_filename.split('_')[0]
# label_truth.append(conf['species_id'][species])
#
# log_dir = os.path.join(conf['log_dir'], 'fold' + str(fold_id))
#
# # make a dataframe by combining file_test, label_truth, label_pred
# df_validate = pd.DataFrame(list(zip(file_test, label_truth, label_pred)), columns=['sound_file', 'label_truth', 'label_pred'])
# df_validate.to_csv(os.path.join(log_dir, 'pred.csv'), index=False)
#
# metrics_two_fold(label_truth, label_pred, log_dir, 'accuracy_fold.txt',
# conf, mode='fold')
# label_pred = np.array(label_pred)
# np.savetxt(os.path.join(log_dir, 'pred_label.txt'), label_pred, delimiter=',', fmt='%d')
# y_pred_prob_arr = np.stack(y_pred_prob_tot_list)
# np.savetxt(os.path.join(log_dir, 'pred_prob.txt'), y_pred_prob_arr, delimiter=',', fmt='%.6f')
# label_truth = np.array(label_truth)
#
# return df_validate, label_pred, y_pred_prob_arr, label_truth
def one_fold_validate_generator(model_name, whistle_image_target_4d,
label_target, whistle_image_test_4d,
label_test, conf, fold_id=1):
label_target_cat = to_categorical(label_target)
label_test_cat = to_categorical(label_test)
whistle_image_train_4d, whistle_image_validate_4d, label_train_cat, \
label_validate_cat = train_test_split(whistle_image_target_4d,
label_target_cat, test_size=0.2)
if conf['network_type'] == 'rnn':
gen_train = data_generator(whistle_image_train_4d, label_train_cat,
batch_size=conf['batch_size'], network_type='rnn')
elif conf['network_type'] == 'conv2d_lstm':
gen_train = data_generator(whistle_image_train_4d, label_train_cat,
batch_size=conf['batch_size'], network_type='conv2d_lstm')
else: # cnn
gen_train = data_generator(whistle_image_train_4d, label_train_cat,
batch_size=conf['batch_size'])
# gen_validate = data_generator(whistle_image_validate_4d, label_validate_cat,
# batch_size=conf['batch_size'])
model_name_func = globals()[model_name]
model = model_name_func(conf)
model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}_valacc_{val_acc:.4f}.hdf5'
log_dir1 = os.path.join(conf['log_dir'], 'fold'+str(fold_id))
if not os.path.exists(log_dir1):
os.mkdir(log_dir1)
check_path = os.path.join(log_dir1, model_name_format)
if fold_id == 1:
with open(os.path.join(conf['log_dir'], 'architecture.txt'), 'w') as f:
with redirect_stdout(f):
# print('')
for kk in sorted(list(conf.keys())):
print(kk + ' ==>> ' + str(conf[kk]))
model.summary()
# checkpoint
checkpoint = ModelCheckpoint(check_path, monitor='val_loss', verbose=1,
save_best_only=True)
early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1,
patience=conf['patience'])
if conf['confusion_callback']:
cm_plot = ConfusionMatrixPlotter(whistle_image_validate_4d,
label_validate_cat, conf['species_name'])
# model compile
model.compile(loss=categorical_crossentropy,
optimizer=Adadelta(lr=conf['learning_rate']),
# optimizer=Adam(lr=conf['learning_rate']),
metrics=['accuracy'])
model.summary()
count_species1 = label_target_cat.sum(axis=0).tolist()
conf["class_weight"] = (
max(count_species1) / np.array(count_species1)).tolist()
if conf['confusion_callback']:
callback_list = [checkpoint, TensorBoard(log_dir=log_dir1), cm_plot,
early_stop]
else:
callback_list = [checkpoint, TensorBoard(log_dir=log_dir1), early_stop]
steps = int(floor(whistle_image_train_4d.shape[0]/conf['batch_size']))
model.fit_generator(gen_train,
epochs=conf['epoch'], verbose=1,
# samples_per_epoch=conf['batch_size']*steps,
# validation_data=(x_test, y_test_onehot),
validation_data=(whistle_image_validate_4d, label_validate_cat),
# validation_data=gen_validate,
# validation_steps=label_validate_cat.shape[0],
# validation_steps=steps_validate,
steps_per_epoch=steps, callbacks=callback_list,
class_weight=conf["class_weight"])
re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4})_valacc_\d+.\d{4}.hdf5'
best_model_path, _ = find_best_model(log_dir1, re_model_name_format,
is_max=False, purge=True)
conf['best_model'] = best_model_path
model = load_model(best_model_path)
y_pred_prob = model.predict(whistle_image_test_4d)
y_pred2 = np.argmax(y_pred_prob, axis=1)
metrics_two_fold(label_test, y_pred2, log_dir1, 'accuracy_fold.txt',
conf, mode='fold')
np.savetxt(os.path.join(log_dir1, 'pred_label.txt'), y_pred2, delimiter=',', fmt='%d')
np.savetxt(os.path.join(log_dir1, 'pred_prob.txt'), y_pred_prob, delimiter=',', fmt='%.6f')
del model
gc.collect()
backend.clear_session()
return y_pred2, y_pred_prob, best_model_path
# def four_fold_validate(model_type, whistle_image_pie1_4d,
# whistle_image_pie2_4d,
# whistle_image_pie3_4d,
# whistle_image_pie4_4d, label_pie1, label_pie2,
# label_pie3, label_pie4, conf):
# start_time = timeit.default_timer()
#
# # fold 1: pie 2, 3, 4 as training and pie 1 as testing
# whistle_image_train_fold1_4d = np.vstack(
# (whistle_image_pie2_4d, whistle_image_pie3_4d, whistle_image_pie4_4d))
# label_train_fold1 = label_pie2 + label_pie3 + label_pie4
# y_pred1, y_pred_prob1, best_model1 = one_fold_validate(model_type,
# whistle_image_train_fold1_4d,
# label_train_fold1,
# whistle_image_pie1_4d, label_pie1,
# conf, fold_id=1)
# # fold 2: pie 1, 3, 4 as training and pie 2 as testing
# whistle_image_train_fold2_4d = np.vstack(
# (whistle_image_pie1_4d, whistle_image_pie3_4d, whistle_image_pie4_4d))
# label_train_fold2 = label_pie1 + label_pie3 + label_pie4
# y_pred2, y_pred_prob2, best_model2 = one_fold_validate(model_type,
# whistle_image_train_fold2_4d,
# label_train_fold2,
# whistle_image_pie2_4d, label_pie2,
# conf, fold_id=2)
# # fold 3: pie 1, 2, 4 as training and pie 3 as testing
# whistle_image_train_fold3_4d = np.vstack(
# (whistle_image_pie1_4d, whistle_image_pie2_4d, whistle_image_pie4_4d))
# label_train_fold3 = label_pie1 + label_pie2 + label_pie4
# y_pred3, y_pred_prob3, best_model3 = one_fold_validate(model_type,
# whistle_image_train_fold3_4d,
# label_train_fold3,
# whistle_image_pie3_4d, label_pie3,
# conf, fold_id=3)
# # fold 4: pie 1, 2, 3 as training and pie 4 as testing
# whistle_image_train_fold4_4d = np.vstack(
# (whistle_image_pie1_4d, whistle_image_pie2_4d, whistle_image_pie3_4d))
# label_train_fold4 = label_pie1 + label_pie2 + label_pie3
# y_pred4, y_pred_prob4, best_model4 = one_fold_validate(model_type,
# whistle_image_train_fold4_4d,
# label_train_fold4,
# whistle_image_pie4_4d, label_pie4,
# conf, fold_id=4)
#
# # collect all
# y_pred_tot = np.concatenate((y_pred1, y_pred2, y_pred3, y_pred4))
# label_total = label_pie1 + label_pie2 + label_pie3 + label_pie4
# metrics_two_fold(label_total, y_pred_tot, conf['log_dir'],
# 'accuracy_total.txt', conf, mode='total')
#
# stop_time = timeit.default_timer()
# with open(os.path.join(conf['log_dir'], 'run_time.txt'), 'w') as f:
# run_time = stop_time-start_time
# with redirect_stdout(f):
# print("Run time is: {0:.3f} s.".format(run_time))
# print("Run time is: {0:.3f} m.".format(run_time/60.0))
#
# return y_pred1, y_pred2, y_pred3, y_pred4, y_pred_prob1, y_pred_prob2, \
# y_pred_prob3, y_pred_prob4
#
#
# def four_fold_validate_generator(model_type, whistle_image_pie_4d_list,
# label_pie_list, conf):
# start_time = timeit.default_timer()
#
# # fold 1: pie 2, 3, 4 as training and pie 1 as testing
# whistle_image_train_fold1_4d = np.vstack(
# (whistle_image_pie_4d_list[1], whistle_image_pie_4d_list[2], whistle_image_pie_4d_list[3]))
# label_train_fold1 = label_pie_list[1] + label_pie_list[2] + label_pie_list[3]
# y_pred1, y_pred_prob1, best_model1 = one_fold_validate_generator(model_type,
# whistle_image_train_fold1_4d,
# label_train_fold1,
# whistle_image_pie_4d_list[0], label_pie_list[0],
# conf, fold_id=1)
# # fold 2: pie 1, 3, 4 as training and pie 2 as testing
# whistle_image_train_fold2_4d = np.vstack(
# (whistle_image_pie_4d_list[0], whistle_image_pie_4d_list[2], whistle_image_pie_4d_list[3]))
# label_train_fold2 = label_pie_list[0] + label_pie_list[2] + label_pie_list[3]
# y_pred2, y_pred_prob2, best_model2 = one_fold_validate_generator(model_type,
# whistle_image_train_fold2_4d,
# label_train_fold2,
# whistle_image_pie_4d_list[1], label_pie_list[1],
# conf, fold_id=2)
# # fold 3: pie 1, 2, 4 as training and pie 3 as testing
# whistle_image_train_fold3_4d = np.vstack(
# (whistle_image_pie_4d_list[0], whistle_image_pie_4d_list[1], whistle_image_pie_4d_list[3]))
# label_train_fold3 = label_pie_list[0] + label_pie_list[1] + label_pie_list[3]
# y_pred3, y_pred_prob3, best_model3 = one_fold_validate_generator(model_type,
# whistle_image_train_fold3_4d,
# label_train_fold3,
# whistle_image_pie_4d_list[2], label_pie_list[2],
# conf, fold_id=3)
# # fold 4: pie 1, 2, 3 as training and pie 4 as testing
# whistle_image_train_fold4_4d = np.vstack(
# (whistle_image_pie_4d_list[0], whistle_image_pie_4d_list[1], whistle_image_pie_4d_list[2]))
# label_train_fold4 = label_pie_list[0] + label_pie_list[1] + label_pie_list[2]
# y_pred4, y_pred_prob4, best_model4 = one_fold_validate_generator(model_type,
# whistle_image_train_fold4_4d,
# label_train_fold4,
# whistle_image_pie_4d_list[3], label_pie_list[3],
# conf, fold_id=4)
#
# # collect all
# y_pred_tot = np.concatenate((y_pred1, y_pred2, y_pred3, y_pred4))
# label_total = label_pie_list[0] + label_pie_list[1] + label_pie_list[2] + label_pie_list[3]
# metrics_two_fold(label_total, y_pred_tot, conf['log_dir'],
# 'accuracy_total.txt', conf, mode='total')
#
# stop_time = timeit.default_timer()
# with open(os.path.join(conf['log_dir'], 'run_time.txt'), 'w') as f:
# run_time = stop_time-start_time
# with redirect_stdout(f):
# print("Run time is: {0:.3f} s.".format(run_time))
# print("Run time is: {0:.3f} m.".format(run_time/60.0))
#
# return y_pred1, y_pred2, y_pred3, y_pred4, y_pred_prob1, y_pred_prob2, \
# y_pred_prob3, y_pred_prob4
def all_data_train_validate(model_name, whistle_image_target_4d, label_target,
conf):
label_target_cat = to_categorical(label_target)
model_name_func = globals()[model_name]
model = model_name_func(conf)
# model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}.hdf5'
# model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}_valacc_{val_acc:.4f}.hdf5'
model_name_format = 'epoch_{epoch:02d}_valloss_{val_loss:.4f}_valacc_{val_accuracy:.4f}.hdf5'
# if conf['comment'] is None:
# conf['comment'] = ''
log_dir = make_folder_time_now(folder_out=conf['log_dir'], folder_comment=conf['comment'])
# if not os.path.exists(log_dir):
# os.mkdir(log_dir)
check_path = os.path.join(log_dir, model_name_format)
with open(os.path.join(log_dir, 'architecture.txt'), 'w') as f:
with redirect_stdout(f):
# print('')
for kk in sorted(list(conf.keys())):
print(kk + ' ==>> ' + str(conf[kk]))
model.summary()
# checkpoint
checkpoint = ModelCheckpoint(check_path, monitor='val_loss', verbose=1,
save_best_only=True)
early_stop = EarlyStopping(monitor='val_loss', mode='min', verbose=1,
patience=conf['patience'])
# model compile
model.compile(loss=categorical_crossentropy,
optimizer=Adadelta(lr=conf['learning_rate']),
# optimizer=Adam(lr=conf['learning_rate']),
metrics=['accuracy'])
model.summary()
count_species = label_target_cat.sum(axis=0)+1e-6
weight_curr = (count_species.max() / count_species).tolist()
conf["class_weight"] = {0: weight_curr[0], 1: weight_curr[1]}
callback_list = [checkpoint, TensorBoard(log_dir=log_dir), early_stop]
model.fit(whistle_image_target_4d, label_target_cat,
batch_size=conf['batch_size'], epochs=conf['epoch'],
verbose=1, validation_split=0.2,
callbacks=callback_list, class_weight=conf["class_weight"])
# re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4}).hdf5'
re_model_name_format = 'epoch_\d+_valloss_(\d+.\d{4})_valacc_\d+.\d{4}.hdf5'
best_model_path, _ = find_best_model(log_dir, re_model_name_format,
is_max=False, purge=True)
conf['best_model'] = best_model_path
model = load_model(best_model_path)
y_pred_prob = model.predict(whistle_image_target_4d)
y_pred2 = np.argmax(y_pred_prob, axis=1)
metrics_two_fold(label_target, y_pred2, log_dir, 'accuracy_fold.txt',
conf, mode='fold')
np.savetxt(os.path.join(log_dir, 'pred_label.txt'), y_pred2, delimiter=',', fmt='%d')
np.savetxt(os.path.join(log_dir, 'pred_prob.txt'), y_pred_prob, delimiter=',', fmt='%.6f')
del model
gc.collect()
backend.clear_session()
return best_model_path
def make_folder_time_now(folder_out='./', folder_comment='model_unknown'):
current = datetime.datetime.now()
right_now = current.strftime("%Y-%m-%d_%H%M%S")
print(right_now)
folder_out_now = os.path.join(folder_out, right_now+'_'+folder_comment)
if not os.path.exists(folder_out_now):
os.makedirs(folder_out_now)
return folder_out_now
def find_best_model(classifier_path, fmt, is_max=False, purge=True):
"""
Return the path to the model with the best accuracy, given the path to
all the trained classifiers
Args:
classifier_path: path to all the trained classifiers
fmt: e.g. "epoch_\d+_[0-1].\d+_(\d+.\d{4}).hdf5"
'epoch_\d+_valloss_(\d+.\d{4})_valacc_\d+.\d{4}.hdf5'
is_max: use max; otherwise, min
purge: True to purge models files except the best one
Return:
the path of the model with the best accuracy