# keras
from keras.models import Sequential
from keras.layers import Dense, Dropout # , Activation
-from keras import backend
+from keras import backend, utils
# Testset ratio
testset = 0.10
# Loop over all datafiles and make wavefile string
for i, tg in enumerate(files):
num = re.match('^.*/(\\d+).TextGrid$', tg).group(1)
- yield (tg, 'wav/{:02d}.wav'.format(int(num)))
+ yield (tg, 'wav/{:02d}.wav'.format(int(num)), int(num))
def label_from_annotation(ann):
return 0 if ann.strip() == '' else 1
i += 1
return (data, labels)
-def run(typ, winlen, winstep, modelfun, modelname):
+def singerfun(num, l):
+ if l == 1:
+ if 0 <= num <= 11:
+ return 1
+ elif 12 <= num <= 21:
+ return 2
+ elif 22 <= num <= 28:
+ return 3
+ else:
+ raise Exception("halp")
+ else:
+ return 0
+
+def run(typ, winlen, winstep, modelfun, modelname, multiclass=False):
datas = []
labels = []
- for tg, wavp in get_datafiles():
+ for tg, wavp, num in get_datafiles():
(d, l) = features_from_wav(
tg, wavp, winlen=winlen, winstep=winstep, typ=typ)
datas.append(d)
- labels.append(l)
+ if multiclass:
+ labels.append(list(map(lambda x: singerfun(int(num), x), l)))
+ else:
+ labels.append(l)
+
datas = np.concatenate(datas)
labels = np.concatenate(labels)
+ print(np.unique(labels, return_counts=True))
+ if multiclass:
+ labels = utils.to_categorical(labels, num_classes=4)
+
rng_state = np.random.get_state()
np.random.shuffle(datas)
winlen, winstep, modelname, loss, acc))
return model
-def simplemodel(d):
+def bottlemodel(d):
model = Sequential()
- model.add(
- Dense(d.shape[1]*2, input_shape=(d.shape[1],), activation='relu'))
- model.add(Dense(100, activation='relu'))
+ model.add(Dense(13, activation='relu', input_shape=(d.shape[1],)))
model.add(Dense(1, activation='sigmoid'))
+# model.add(
+# Dense(d.shape[1]*2, input_shape=(d.shape[1],), activation='relu'))
+# model.add(Dense(13, activation='relu'))
+# model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
return model
-def bottlemodel(d):
+def multimodel(d):
model = Sequential()
- model.add(
- Dense(d.shape[1]*2, input_shape=(d.shape[1],), activation='relu'))
- model.add(Dense(13, activation='relu'))
- model.add(Dense(1, activation='sigmoid'))
+# model.add(Dense(d.shape[1]*2, input_shape=(d.shape[1],), activation='relu'))
+ model.add(Dense(13, activation='relu', input_shape=(d.shape[1],)))
+ model.add(Dense(4, activation='softmax'))
model.compile(optimizer='rmsprop',
- loss='binary_crossentropy',
+ loss='categorical_crossentropy',
metrics=['accuracy'])
return model
+
if __name__ == '__main__':
print('winlen\twinstep\tmodel\tloss\taccuracy\n')
with backend.get_session():
for winlen, winstep in ((0.025, 0.01), (0.1, 0.04), (0.2, 0.08)):
- for name, model in (('simple', simplemodel), ('bottle', bottlemodel)):
- m = run('mfcc', winlen, winstep, model, name)
+ for name, model, multi in reversed((('bottle', bottlemodel, False), ('multi', multimodel, True))):
+ m = run('mfcc', winlen, winstep, model, name, multi)
fproot = 'model_{}_{}_{}'.format(winlen, winstep, name)
with open('{}.json'.format(fproot), 'w') as f:
f.write(m.to_json())