Пример #1
0
def plot_history_to_image_file(pickle_path):
    with open(pickle_path, 'rb') as input:
        trainLoss = pickle.load(input)
        validLoss = pickle.load(input)
        trainAcc = pickle.load(input)
        validAcc = pickle.load(input)

        x = range(len(validAcc))
        y = validAcc
        z = np.polyfit(x, y, 3)
        p = np.poly1d(z)

        fig = plt.figure(1)
        plt.subplot(211)
        axes = plt.gca()
        axes.set_ylim([0, 5])
        plt.ylabel("Loss")
        plt.plot(trainLoss, 'o-', label="train")
        plt.plot(validLoss, 'o-', label="valid")
        plt.legend(loc="upper_left")
        plt.subplot(212)
        axes = plt.gca()
        axes.set_ylim([0, 1])
        plt.ylabel("Accuracy")
        plt.xlabel("Epoch")
        plt.plot(trainAcc, 'o-', label="train")
        plt.plot(validAcc, 'o-', label="valid")
        # plt.plot(p(x), 'o-', label="trend")
        plt.legend(loc="upper_left")

        basename = utils.get_basename_without_ext(pickle_path)
        fig.savefig(basename + ".png")
        plt.clf()
        plt.close()
Пример #2
0
def signal_and_noise_spectrogram_from_wave_file(filepath):

    (fs, wave) = utils.read_wave_file(filepath)
    spectrogram = sp.wave_to_sample_spectrogram(wave, fs)
    signal_wave, noise_wave = pp.preprocess_wave(wave, fs)
    spectrogram_signal = sp.wave_to_sample_spectrogram(signal_wave, fs)
    spectrogram_noise = sp.wave_to_sample_spectrogram(noise_wave, fs)

    fig = plt.figure(1)
    cmap = plt.cm.get_cmap('jet')
    gs = gridspec.GridSpec(2, 2)
    # whole spectrogram
    ax1 = fig.add_subplot(gs[0, :])
    ax1.pcolormesh(spectrogram, cmap=cmap)
    ax1.set_title("Sound")

    ax2 = fig.add_subplot(gs[1, 0])
    ax2.pcolormesh(spectrogram_signal, cmap=cmap)
    ax2.set_title("Signal")

    ax3 = fig.add_subplot(gs[1, 1])
    ax3.pcolormesh(spectrogram_noise, cmap=cmap)
    ax3.set_title("Noise")

    gs.update(wspace=0.5, hspace=0.5)

    basename = utils.get_basename_without_ext(filepath)
    fig.savefig(basename + "_noise_signal.png")

    fig.clf()
    plt.close(fig)
Пример #3
0
def noise_augmentation_from_dirs(noise_dir, class_dir):
    sig_paths = glob.glob(os.path.join(class_dir, "*.wav"))
    sig_path = np.random.choice(sig_paths, 1, replace=False)[0]
    (fs, sig) = utils.read_wave_file(sig_path)
    aug_sig = da.noise_augmentation(sig, noise_dir)

    spectrogram_sig = sp.wave_to_sample_spectrogram(sig, fs)
    spectrogram_aug_sig = sp.wave_to_sample_spectrogram(aug_sig, fs)

    fig = plt.figure(1)
    cmap = plt.cm.get_cmap('jet')
    gs = gridspec.GridSpec(2, 1)
    # whole spectrogram
    ax1 = fig.add_subplot(gs[0, 0])
    ax1.pcolormesh(spectrogram_sig, cmap=cmap)
    ax1.set_title("Original Signal")

    ax2 = fig.add_subplot(gs[1, 0])
    ax2.pcolormesh(spectrogram_aug_sig, cmap=cmap)
    ax2.set_title("Noise Augmented signal")

    gs.update(wspace=0.5, hspace=0.5)

    basename = utils.get_basename_without_ext(sig_path)
    fig.savefig(basename + "_noise_augmentation.png")

    fig.clf()
    plt.close(fig)
Пример #4
0
def sprengel_binary_mask_from_wave_file(filepath):
    fs, x = utils.read_wave_file(filepath)
    Sxx = sp.wave_to_amplitude_spectrogram(x, fs)
    Sxx_log = sp.wave_to_log_amplitude_spectrogram(x, fs)

    # plot spectrogram
    fig = plt.figure(1)
    subplot_image(Sxx_log, 411, "Spectrogram")

    Sxx = pp.normalize(Sxx)
    binary_image = pp.median_clipping(Sxx, 3.0)

    subplot_image(binary_image + 0, 412, "Median Clipping")

    binary_image = morphology.binary_erosion(binary_image,
                                             selem=np.ones((4, 4)))

    subplot_image(binary_image + 0, 413, "Erosion")

    binary_image = morphology.binary_dilation(binary_image,
                                              selem=np.ones((4, 4)))

    subplot_image(binary_image + 0, 414, "Dilation")

    mask = np.array([np.max(col) for col in binary_image.T])
    mask = morphology.binary_dilation(mask, np.ones(4))
    mask = morphology.binary_dilation(mask, np.ones(4))

    # plot_vector(mask, "Mask")

    fig.set_size_inches(10, 12)
    plt.tight_layout()
    fig.savefig(utils.get_basename_without_ext(filepath) + "_binary_mask.png",
                dpi=100)
def preprocess_sound_file(filename, class_dir, noise_dir,
                          segment_size_seconds):
    """ Preprocess sound file. Loads sound file from filename, downsampels,
    extracts signal/noise parts from sound file, splits the signal/noise parts
    into equally length segments of size segment size seconds.

    # Arguments
        filename : the sound file to preprocess
        class_dir : the directory to save the extracted signal segments in
        noise_dir : the directory to save the extracted noise segments in
        segment_size_seconds : the size of each segment in seconds
    # Returns
        nothing, simply saves the preprocessed sound segments
    """

    samplerate, wave = utils.read_wave_file(filename)
    signal_wave, noise_wave = preprocess_wave(wave, samplerate)
    basename = utils.get_basename_without_ext(filename)

    if signal_wave.shape[0] > 0:
        signal_segments = split_into_segments(signal_wave, samplerate,
                                              segment_size_seconds)
        save_segments_to_file(class_dir, signal_segments, basename, samplerate)
    if noise_wave.shape[0] > 0:
        noise_segments = split_into_segments(noise_wave, samplerate,
                                             segment_size_seconds)
        save_segments_to_file(noise_dir, noise_segments, basename, samplerate)
Пример #6
0
def preprocess_sound_file(filename, class_dir, noise_dir,
                          segment_size_seconds):
    """ Preprocess sound file. Loads sound file from filename, downsampels,
    extracts signal/noise parts from sound file, splits the signal/noise parts
    into equally length segments of size segment size seconds.

    # Arguments
        filename : the sound file to preprocess
        class_dir : the directory to save the extracted signal segments in
        noise_dir : the directory to save the extracted noise segments in
        segment_size_seconds : the size of each segment in seconds
    # Returns
        nothing, simply saves the preprocessed sound segments
    """

    samplerate, wave = utils.read_wave_file_not_normalized(filename)
    print(wave)

    if len(wave) == 0:
        print("An empty sound file..")
        wave = np.zeros(samplerate * segment_size_seconds, dtype=np.int16)

    wave = wave.astype(np.float)

    signal_wave, noise_wave = preprocess_wave(wave, samplerate)

    if signal_wave.shape[0] == 0:
        signal_wave = np.zeros(samplerate * segment_size_seconds,
                               dtype=np.int16)

    basename = utils.get_basename_without_ext(filename)

    # print(filename)
    # print(class_dir)
    # print(noise_dir)

    if signal_wave.shape[0] > 0:
        signal_segments = split_into_segments(signal_wave, samplerate,
                                              segment_size_seconds)
        save_segments_to_file(class_dir, signal_segments, basename, samplerate)
    if noise_wave.shape[0] > 0:
        noise_segments = split_into_segments(noise_wave, samplerate,
                                             segment_size_seconds)
        save_segments_to_file(noise_dir, noise_segments, basename, samplerate)
Пример #7
0
def same_class_augmentation_from_dir(class_dir):
    sig_paths = glob.glob(os.path.join(class_dir, "*.wav"))
    sig_path = np.random.choice(sig_paths, 1, replace=False)[0]
    (fs, sig) = utils.read_wave_file(sig_path)

    aug_sig_path = np.random.choice(sig_paths, 1, replace=False)[0]
    (fs, aug_sig) = utils.read_wave_file(aug_sig_path)
    alpha = np.random.rand()
    combined_sig = (1.0 - alpha) * sig + alpha * aug_sig

    spectrogram_sig = sp.wave_to_sample_spectrogram(sig, fs)
    spectrogram_aug_sig = sp.wave_to_sample_spectrogram(aug_sig, fs)
    spectrogram_combined_sig = sp.wave_to_sample_spectrogram(combined_sig, fs)

    fig = plt.figure(1)
    cmap = plt.cm.get_cmap('jet')
    gs = gridspec.GridSpec(3, 1)
    # whole spectrogram
    ax1 = fig.add_subplot(gs[0, 0])
    ax1.pcolormesh(spectrogram_sig, cmap=cmap)
    ax1.set_title("Signal 1")

    ax2 = fig.add_subplot(gs[1, 0])
    ax2.pcolormesh(spectrogram_aug_sig, cmap=cmap)
    ax2.set_title("Signal 2")

    ax3 = fig.add_subplot(gs[2, 0])
    ax3.pcolormesh(spectrogram_combined_sig, cmap=cmap)
    ax3.set_title("Augmented Signal (alpha=" + str(alpha) + ")")

    gs.update(wspace=0.5, hspace=0.5)

    basename = utils.get_basename_without_ext(sig_path)
    fig.savefig(basename + "_same_class_augmentation.png")

    fig.clf()
    plt.close(fig)
Пример #8
0
def img_spectrogram_from_wave_file(filepath):
    fs, x = utils.read_gzip_wave_file(filepath)
    Sxx = sp.wave_to_log_amplitude_spectrogram(x, fs, 512, 128)[:256]
    baseName = utils.get_basename_without_ext(filepath)
    save_matrix_to_file(Sxx, "Log Amplitude Spectrogram",
                        baseName + "_AMP.png")