From: Mart Lubbers Date: Tue, 16 May 2017 12:53:14 +0000 (+0200) Subject: local tensorflow installation, add multiclass X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=eff7cbe1e9529cabe56396135cbb17e6ba2941a2;p=asr1617data.git local tensorflow installation, add multiclass --- diff --git a/experiments.py b/experiments.py index 911de6b..354f787 100644 --- a/experiments.py +++ b/experiments.py @@ -11,7 +11,7 @@ import numpy as np # 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 @@ -23,7 +23,7 @@ def get_datafiles(): # 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 @@ -62,18 +62,39 @@ def features_from_wav(tg, wavp, typ='mfcc', winlen=0.025, winstep=0.01): 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) @@ -98,34 +119,36 @@ def run(typ, winlen, winstep, modelfun, modelname): 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()) diff --git a/makevenv.sh b/makevenv.sh index 0146460..dfb2ab7 100755 --- a/makevenv.sh +++ b/makevenv.sh @@ -3,5 +3,4 @@ deactivate || true virtualenv -p python3 --system-site-packages . . bin/activate pip install --upgrade keras h5py python_speech_features pympi-ling scipy -#pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.1.0-cp36-none-linux_x86_64.whl deactivate