Load FLAC file in python same as scipy or librosa

Harry Moreno picture Harry Moreno · Jun 11, 2018 · Viewed 9k times · Source

I would like to feed some flac sound files into a keras model. With wavfiles I can do (contrived example with one audio file used twice)

import scipy.io.wavfile
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

path = 'path/to/file.wav'
_, audio = scipy.io.wavfile.read(path)
dataset = [audio, audio]
x_train = np.array(dataset)
y_train = keras.utils.to_categorical([0, 1], num_classes=2)

model = Sequential()
model.add(Dense(32, activation='relu', input_shape=x_train[0].shape))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=32)

How do I do this with flac files instead?

Answer

Harry Moreno picture Harry Moreno · Jun 13, 2018

The soundfile package can load flac files in a numpy array compatible format

import numpy as np                                                             
import soundfile as sf                                                      
import keras                                                                
from keras.models import Sequential                                         
from keras.layers import Dense, Dropout, Activation                         
from keras.optimizers import SGD                                            

path = 'path/to/file.flac'                                                  
data, samplerate = sf.read(path)                                            
dataset = [data, data]                                                      
x_train = np.array(dataset)                                                 
y_train = keras.utils.to_categorical([0, 1], num_classes=2)                 

model = Sequential()                                                        
model.add(Dense(32, activation='relu', input_shape=x_train[0].shape))       
model.add(Dense(2, activation='softmax'))                                   
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=32)    

forkable sscce https://www.kaggle.com/morenoh149/flac-keras-hello-world