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?
Data can be written to the file using soundfile. write() , or read from the file using soundfile. read() . PySoundFile can open all file formats that libsndfile supports, for example WAV, FLAC, OGG and MAT files.
It returns floating type audio time series.
Load an audio file as a floating point time series. Audio will be automatically resampled to the given rate (default sr=22050 ). To preserve the native sampling rate of the file, use sr=None . path to the input file.
Since librosa can load MPEG4 audio (when suitable dependencies are installed), I would recommend downloading that, and loading it directly.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With