Compare commits
2 Commits
fabd882664
...
046343680e
| Author | SHA1 | Date |
|---|---|---|
|
|
046343680e | |
|
|
c187fbe1ca |
|
|
@ -1,7 +1,8 @@
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pandas_parallel import apply_by_multiprocessing
|
from pandas_parallel import apply_by_multiprocessing
|
||||||
import dask as dd
|
# import dask as dd
|
||||||
import dask.dataframe as ddf
|
# import dask.dataframe as ddf
|
||||||
|
import tensorflow as tf
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from spectro_gen import generate_aiff_spectrogram
|
from spectro_gen import generate_aiff_spectrogram
|
||||||
from sklearn.model_selection import train_test_split
|
from sklearn.model_selection import train_test_split
|
||||||
|
|
@ -22,6 +23,15 @@ def get_siamese_pairs(groupF1, groupF2):
|
||||||
# return (random.sample(same,10), random.sample(diff,10))
|
# return (random.sample(same,10), random.sample(diff,10))
|
||||||
return same[:10],diff[:10]
|
return same[:10],diff[:10]
|
||||||
|
|
||||||
|
def siamese_pairs(rightGroup, wrongGroup):
|
||||||
|
group1 = [r for (i, r) in rightGroup.iterrows()]
|
||||||
|
group2 = [r for (i, r) in wrongGroup.iterrows()]
|
||||||
|
rightWrongPairs = [(g1, g2) for g2 in group2 for g1 in group1]
|
||||||
|
rightRightPairs = [i for i in itertools.combinations(group1, 2)]
|
||||||
|
random.shuffle(rightWrongPairs)
|
||||||
|
random.shuffle(rightRightPairs)
|
||||||
|
# return (random.sample(same,10), random.sample(diff,10))
|
||||||
|
return rightRightPairs[:10],rightWrongPairs[:10]
|
||||||
|
|
||||||
def append_zeros(spgr, max_samples):
|
def append_zeros(spgr, max_samples):
|
||||||
return np.lib.pad(spgr, [(0, max_samples - spgr.shape[0]), (0, 0)],
|
return np.lib.pad(spgr, [(0, max_samples - spgr.shape[0]), (0, 0)],
|
||||||
|
|
@ -96,12 +106,9 @@ def create_spectrogram_tfrecords(audio_group='audio'):
|
||||||
, quoting=csv.QUOTE_NONE)
|
, quoting=csv.QUOTE_NONE)
|
||||||
# audio_samples = audio_samples.loc[audio_samples['word'] ==
|
# audio_samples = audio_samples.loc[audio_samples['word'] ==
|
||||||
# 'sunflowers'].reset_index(drop=True)
|
# 'sunflowers'].reset_index(drop=True)
|
||||||
audio_samples['file_paths'] = audio_samples.loc[:, 'file'].apply(lambda x: 'outputs/' + audio_group + '/' + x)
|
audio_samples['file_path'] = audio_samples.loc[:, 'file'].apply(lambda x: 'outputs/' + audio_group + '/' + x)
|
||||||
audio_samples['file_exists'] = apply_by_multiprocessing(audio_samples['file_paths'], os.path.exists)
|
audio_samples['file_exists'] = apply_by_multiprocessing(audio_samples['file_path'], os.path.exists)
|
||||||
audio_samples = audio_samples[audio_samples['file_exists'] == True].reset_index()
|
audio_samples = audio_samples[audio_samples['file_exists'] == True].reset_index()
|
||||||
# audio_samples['spectrogram'] = apply_by_multiprocessing(audio_samples['file_paths'],generate_aiff_spectrogram)#.apply(
|
|
||||||
# audio_samples['window_count'] = audio_samples.loc[:,'spectrogram'].apply(lambda x: x.shape[0])
|
|
||||||
# audio_samples.to_pickle('outputs/{}-spectrogram.pkl'.format(audio_group))
|
|
||||||
|
|
||||||
def _float_feature(value):
|
def _float_feature(value):
|
||||||
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
|
return tf.train.Feature(float_list=tf.train.FloatList(value=value))
|
||||||
|
|
@ -111,17 +118,46 @@ def create_spectrogram_tfrecords(audio_group='audio'):
|
||||||
|
|
||||||
def _bytes_feature(value):
|
def _bytes_feature(value):
|
||||||
return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
|
return tf.train.Feature(bytes_list=tf.train.BytesList(value=value))
|
||||||
writer = tf.python_io.TFRecordWriter(output_path)
|
|
||||||
|
|
||||||
for sample in audio_samples:
|
writer = tf.python_io.TFRecordWriter('./outputs/' + audio_group + '.tfrecords')
|
||||||
example = tf.train.Example(features=tf.train.Features(
|
# audio_samples = audio_samples[:100]
|
||||||
feature={
|
for (w, word_group) in audio_samples.groupby(audio_samples['word']):
|
||||||
'label': _int64_feature([label]),
|
g = word_group.reset_index()
|
||||||
'path': _bytes_feature([image_path]),
|
g['spectrogram'] = apply_by_multiprocessing(g['file_path'],generate_aiff_spectrogram)
|
||||||
'instance' : _bytes_feature([instance_id])
|
sample_right = g.loc[audio_samples['variant'] == 'low']
|
||||||
}
|
sample_wrong = g.loc[audio_samples['variant'] == 'medium']
|
||||||
))
|
same, diff = siamese_pairs(sample_right, sample_wrong)
|
||||||
writer.write(example.SerializeToString())
|
groups = [([0,1],same),([1,0],diff)]
|
||||||
|
for (output,group) in groups:
|
||||||
|
for sample1,sample2 in group:
|
||||||
|
spectro1,spectro2 = sample1['spectrogram'],sample2['spectrogram']
|
||||||
|
spec_n1,spec_n2 = spectro1.shape[0],spectro2.shape[0]
|
||||||
|
spec_w1,spec_w2 = spectro1.shape[1],spectro2.shape[1]
|
||||||
|
spec1,spec2 = spectro1.reshape(-1),spectro2.reshape(-1)
|
||||||
|
example = tf.train.Example(features=tf.train.Features(
|
||||||
|
feature={
|
||||||
|
'word': _bytes_feature([w.encode('utf-8')]),
|
||||||
|
'phoneme1': _bytes_feature([sample1['phonemes'].encode('utf-8')]),
|
||||||
|
'phoneme2': _bytes_feature([sample2['phonemes'].encode('utf-8')]),
|
||||||
|
'voice1': _bytes_feature([sample1['voice'].encode('utf-8')]),
|
||||||
|
'voice2': _bytes_feature([sample2['voice'].encode('utf-8')]),
|
||||||
|
'language': _bytes_feature([sample1['language'].encode('utf-8')]),
|
||||||
|
'rate1':_int64_feature([sample1['rate']]),
|
||||||
|
'rate2':_int64_feature([sample2['rate']]),
|
||||||
|
'variant1': _bytes_feature([sample1['variant'].encode('utf-8')]),
|
||||||
|
'variant2': _bytes_feature([sample2['variant'].encode('utf-8')]),
|
||||||
|
'file1': _bytes_feature([sample1['file'].encode('utf-8')]),
|
||||||
|
'file2': _bytes_feature([sample2['file'].encode('utf-8')]),
|
||||||
|
'spec1':_float_feature(spec1),
|
||||||
|
'spec2':_float_feature(spec2),
|
||||||
|
'spec_n1':_int64_feature([spec_n1]),
|
||||||
|
'spec_w1':_int64_feature([spec_w1]),
|
||||||
|
'spec_n2':_int64_feature([spec_n2]),
|
||||||
|
'spec_w2':_int64_feature([spec_w2]),
|
||||||
|
'output':_int64_feature(output)
|
||||||
|
}
|
||||||
|
))
|
||||||
|
writer.write(example.SerializeToString())
|
||||||
writer.close()
|
writer.close()
|
||||||
|
|
||||||
def create_tagged_data(audio_samples):
|
def create_tagged_data(audio_samples):
|
||||||
|
|
@ -177,7 +213,8 @@ def speech_model_data():
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# sunflower_pairs_data()
|
# sunflower_pairs_data()
|
||||||
# create_spectrogram_data()
|
# create_spectrogram_data()
|
||||||
create_spectrogram_data('story_words')
|
# create_spectrogram_data('story_words')
|
||||||
|
create_spectrogram_tfrecords('story_words')
|
||||||
# create_padded_spectrogram()
|
# create_padded_spectrogram()
|
||||||
# create_speech_pairs_data()
|
# create_speech_pairs_data()
|
||||||
# print(speech_model_data())
|
# print(speech_model_data())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue