def write(path, data, rate):
    import soundfile as sf
    from deep_audio import Directory

    Directory.create_directory(path, file=True)

    sf.write(path, data, rate, subtype='PCM_16')
Exemple #2
0
def plot_cepstrals(data, title=None, x_label='Frame Index', y_label='Index', cmap='magma', size=(10, 6),
                   caption=None,
                   fig_name=None,
                   show=False, close=True):
    if size:
        plt.figure(figsize=(10, 6), frameon=True)
    plt.imshow(data.T,
               origin='lower',
               aspect='auto',
               cmap=cmap,
               interpolation='nearest')
    plt.title(title)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    # plt.colorbar(format='%+2.0f')
    # plt.clim(vmin, vmax)

    if caption:
        plt.figtext(0.5, 0.01, caption, wrap=True,
                    horizontalalignment='center')

    if fig_name:
        Directory.create_directory(fig_name, True)
        plt.savefig(fig_name, transparent=False)

    if show:
        plt.show()

    if close:
        plt.close()
Exemple #3
0
def plot(data, title=None, x_label='Time (s)', y_label='Amplitude', size=(10, 6), caption=None,
         fig_name=None,
         show=False,
         close=True):
    if size:
        plt.figure(figsize=(10, 6), frameon=True)

    plt.plot(list(range(0, len(data))), data)
    plt.ylabel(y_label)
    plt.xlabel(x_label)
    plt.title(title)
    # Remove a margem no eixo x
    plt.margins(x=0)

    if caption:
        plt.figtext(0.5, 0.01, caption, wrap=True,
                    horizontalalignment='center')

    if fig_name:
        Directory.create_directory(fig_name, True)
        plt.savefig(fig_name, transparent=False)

    if show:
        plt.show()

    if close:
        plt.close()
Exemple #4
0
def plot_subplots(audio, mfccs, lpccs, rate, title=None, size_multiplier=2, cmap='magma', caption=None,
                  fig_name=None, show=False):
    from matplotlib import rcParams, rcParamsDefault

    small_size = 8
    medium_size = 10
    bigger_size = 12

    image_size = (10 * size_multiplier, 6 * size_multiplier)

    # controls default text sizes
    plt.rc('font', size=small_size * size_multiplier)
    # fontsize of the axes title
    plt.rc('axes', titlesize=small_size * size_multiplier)
    # fontsize of the x and y labels
    plt.rc('axes', labelsize=medium_size * size_multiplier)
    # fontsize of the tick labels
    plt.rc('xtick', labelsize=small_size * size_multiplier)
    # fontsize of the tick labels
    plt.rc('ytick', labelsize=small_size * size_multiplier)
    plt.rc('legend', fontsize=small_size *
           size_multiplier)  # legend fontsize
    # fontsize of the figure title
    plt.rc('figure', titlesize=bigger_size * size_multiplier)

    plt.subplots(2, 2, figsize=image_size)
    plt.subplots_adjust(left=0.125, right=0.9, bottom=0.1,
                        top=0.9, wspace=0.3, hspace=0.3)
    plt.suptitle(title)

    plt.subplot(2, 2, 1)
    plot_audio(audio, rate, close=False, size=None)

    plt.subplot(2, 2, 2)
    plot_spectrogram(
        audio, rate, cmap=cmap, close=False, size=None)

    plt.subplot(2, 2, 3)
    plot_cepstrals(
        data=lpccs, y_label='LPCC Index', cmap=cmap, size=None, close=False)

    plt.subplot(2, 2, 4)
    plot_cepstrals(
        data=mfccs, y_label='MFCC Index', cmap=cmap, size=None, close=False)

    if caption:
        plt.figtext(0.5, 0.01, caption, wrap=True,
                    horizontalalignment='center')

    if fig_name:
        Directory.create_directory(fig_name, True)
        plt.savefig(fig_name, transparent=False)

    if show:
        plt.show()

    plt.close()

    # Reseta todo o estilo configurado no inicio da função
    rcParams.update(rcParamsDefault)
def create_json_file(file, data, indent=2, cls=None):
    from deep_audio import Directory
    import json

    directory = '/'.join(file.split('/')[:-1])

    Directory.create_directory(directory)

    with open(file, "w") as fp:
        json.dump(data, fp, indent=indent, cls=cls)
Exemple #6
0
def plot_spectrogram(data, rate, n_fft=1024, title=None, x_label='Time (s)', y_label='Frequency (kHz)',
                     cmap='magma', size=(10, 6), caption=None, fig_name=None, show=False, close=True):
    if size:
        plt.figure(figsize=(10, 6), frameon=True)
    plt.specgram(data, NFFT=n_fft, Fs=rate, cmap=cmap)
    plt.title(title)
    plt.ylabel(y_label)
    plt.xlabel(x_label)

    if caption:
        plt.figtext(0.5, 0.01, caption, wrap=True,
                    horizontalalignment='center')

    if fig_name:
        Directory.create_directory(fig_name, True)
        plt.savefig(fig_name, transparent=False)

    if show:
        plt.show()

    if close:
        plt.close()
Exemple #7
0
                  metrics=['accuracy'])

    return model


# %%
# CRIA O MODELO
learning_rate = 0.0001
model = build_model(learning_rate=learning_rate)

# %%
# SALVA A ESTRUTURA DO MODELO

timestamp = int(time.time())

Directory.create_directory(
    f'{language}/models/{model_algo}/{library}/{filename_ps}{timestamp}')

JSON.create_json_file(
    f'{language}/models/{model_algo}/{library}/{filename_ps}{timestamp}/model_structure.json',
    model.to_json())

model_save_filename = f'{language}/models/{model_algo}/{library}/{filename_ps}{timestamp}/model_weight.h5'

# DECIDE QUANDO PARAR
earlystopping_cb = EarlyStopping(patience=300, restore_best_weights=True)

# SALVA OS PESOS
mdlcheckpoint_cb = ModelCheckpoint(model_save_filename,
                                   monitor="val_accuracy",
                                   save_best_only=True)
Exemple #8
0
model = SVC(C=10, kernel='linear', decision_function_shape='ovo')

filename_holder = Directory.model_filename(
    method=method,
    language=language,
    library=library,
    normalization=normalization,
    augmentation=augment,
    json=False,
    models=True
)

model.fit(X_train, y_train)

Directory.create_directory(filename_holder)
dump(model, open(filename_holder + 'model.h5', 'wb'))
dump(scaler, open(filename_holder + 'scaler.pkl', 'wb'))

# SALVA OS PARAMETROS
Model.dump_model(
    filename_holder + 'info.json',
    language=language,
    method=method,
    normalization=normalization,
    sampling_rate=sampling_rate,
    augmentation=augment,
    shape=X_train.shape,
    seed=random_state,
    library=library,
    extra={