Exemple #1
0
def collect_wrong_examples(correct, ins, rkhs, instcombs, typecombs, ids,
                           files):
    sorted_inst = tf.py_func(_sort_labels, [instcombs], tf.string)
    sorted_type = tf.py_func(_sort_labels, [typecombs], tf.string)

    whs = tf.squeeze(tf.gather(rkhs, tf.where(tf.logical_not(correct))),
                     axis=1)
    wis = tf.squeeze(tf.gather(ins, tf.where(tf.logical_not(correct))), axis=1)
    winst = tf.squeeze(tf.gather(sorted_inst,
                                 tf.where(tf.logical_not(correct))),
                       axis=1)
    wtypes = tf.squeeze(tf.gather(sorted_type,
                                  tf.where(tf.logical_not(correct))),
                        axis=1)
    wids = tf.squeeze(tf.gather(ids, tf.where(tf.logical_not(correct))),
                      axis=1)
    wfiles = tf.squeeze(tf.gather(files, tf.where(tf.logical_not(correct))),
                        axis=1)

    fig = tfplot.plot(_create_wrong_example_plot,
                      [wis, whs, winst, wtypes, wids])

    tf.summary.image('wrong_examples', tf.expand_dims(fig, 0), max_outputs=1)
    tf.summary.text('wrong_ids',
                    tf.reduce_join(tf.as_string(wids), axis=0, separator=','))
    tf.summary.text('wrong_files', wfiles)
    return
Exemple #2
0
def add_confusion_matrix(logits, labels):
    with tf.name_scope('confusionMatrix') as scope:
        predictions = tf.argmax(logits, 1)
        confusion = tf.confusion_matrix(labels=labels, predictions=predictions)

        confusion_img = tfplot.plot(_create_confusion_image, [confusion])
        tf.summary.image('confusion_matrix',
                         tf.expand_dims(confusion_img, 0),
                         max_outputs=1)
Exemple #3
0
def add_comb_stats(correct1, correct5, typecombs, train_test_table_selector):
    with tf.name_scope('combStats') as scope:
        sorted_typecomb_labels = tf.py_func(_sort_labels, [typecombs],
                                            tf.string)

        update_train_top1_table, export_train_top1_table = _add_onehot_to_hash_table_average(
            correct1, sorted_typecomb_labels)
        update_train_top5_table, export_train_top5_table = _add_onehot_to_hash_table_average(
            correct5, sorted_typecomb_labels)
        update_test_top1_table, export_test_top1_table = _add_onehot_to_hash_table_average(
            correct1, sorted_typecomb_labels)
        update_test_top5_table, export_test_top5_table = _add_onehot_to_hash_table_average(
            correct5, sorted_typecomb_labels)

        update_top1_table, export_top1_table = tf.cond(
            tf.equal(train_test_table_selector, 0),
            lambda: [update_train_top1_table, export_train_top1_table],
            lambda: [update_test_top1_table, export_test_top1_table])
        update_top5_table, export_top5_table = tf.cond(
            tf.equal(train_test_table_selector, 0),
            lambda: [update_train_top5_table, export_train_top5_table],
            lambda: [update_test_top5_table, export_test_top5_table])

        top1_plot_op = tfplot.plot(
            _create_bar_stats_figure,
            [export_top1_table[0], export_top1_table[1]])
        top5_plot_op = tfplot.plot(
            _create_bar_stats_figure,
            [export_top5_table[0], export_top5_table[1]])

        tf.summary.image('top1_typecomb',
                         tf.expand_dims(top1_plot_op, 0),
                         max_outputs=1)
        tf.summary.image('top5_typecomb',
                         tf.expand_dims(top5_plot_op, 0),
                         max_outputs=1)

        reset_tables = tf.local_variables_initializer()

    return [update_top1_table, update_top5_table], reset_tables
Exemple #4
0
def add_genre_stats(correct1, correct5, genre, train_test_table_selector):
    with tf.name_scope('genreStats') as scope:
        update_train_top1_table, export_train_top1_table = _add_onehot_to_hash_table_average(
            correct1, genre)
        update_train_top5_table, export_train_top5_table = _add_onehot_to_hash_table_average(
            correct5, genre)
        update_test_top1_table, export_test_top1_table = _add_onehot_to_hash_table_average(
            correct1, genre)
        update_test_top5_table, export_test_top5_table = _add_onehot_to_hash_table_average(
            correct5, genre)

        update_top1_table, export_top1_table = tf.cond(
            tf.equal(train_test_table_selector, 0),
            lambda: [update_train_top1_table, export_train_top1_table],
            lambda: [update_test_top1_table, export_test_top1_table])
        update_top5_table, export_top5_table = tf.cond(
            tf.equal(train_test_table_selector, 0),
            lambda: [update_train_top5_table, export_train_top5_table],
            lambda: [update_test_top5_table, export_test_top5_table])

        top1_plot_op = tfplot.plot(
            _create_bar_stats_figure,
            [export_top1_table[0], export_top1_table[1]])
        top5_plot_op = tfplot.plot(
            _create_bar_stats_figure,
            [export_top5_table[0], export_top5_table[1]])

        tf.summary.image('top1_genre',
                         tf.expand_dims(top1_plot_op, 0),
                         max_outputs=1)
        tf.summary.image('top5_genre',
                         tf.expand_dims(top5_plot_op, 0),
                         max_outputs=1)

        reset_tables = tf.local_variables_initializer()

    return [update_top1_table, update_top5_table], reset_tables
attention_tensor = tf.constant(attention_map, name='attention')
attention_tensor2 = tf.constant(attention_map2, name='attention')

print(image_tensor)
print(attention_tensor)


def figure_attention(attention):
    fig, ax = tfplot.subplots(figsize=(4, 3))
    im = ax.imshow(attention)

    return fig


plot_op = tfplot.plot(figure_attention, [attention_tensor])
execute_plot_op(plot_op)
'''
An overlay example by using tfplot
'''


def overlay_attention(attention, image, alpha=0.5, cmap='jet'):
    fig = tfplot.Figure(figsize=(4, 4))
    ax = fig.add_subplot(1, 1, 1)
    ax.axis('off')
    # fig.subplots_adjust(0, 0, 1, 1)  # get rid of margins

    H, W = attention.shape
    ax.imshow(image, extent=[0, H, 0, W])
    ax.imshow(attention, cmap=cmap, alpha=alpha, extent=[0, H, 0, W])
Exemple #6
0
def roomcomp(impresp, filter, target, ntaps, mixed_phase, opformat, trim,
             nsthresh, noplot):
    """Primary function.

    Determine a room compensation impulse response from a measured room impulse response.
    """
    print("Loading impulse response")

    # Read impulse response
    Fs, data = wavfile.read(impresp)
    data = norm(np.hstack(data))

    if trim:
        print("Removing leading silence")
        for spos, sval in enumerate(data):
            if abs(sval) > nsthresh:
                lzs = max(spos - 1, 0)
                print('Impulse starts at position ', spos, '/', len(data))
                print('Trimming ',
                      float(lzs) / float(Fs), ' seconds of silence')
                data = data[lzs:len(
                    data)]  # remove everything before sample at spos
                break

    print("\nSample rate = ", Fs)

    print("\nGenerating correction filter")

    # Number of taps

    if not ntaps:
        ntaps = len(data)

    # Logarithmic pole positioning

    fplog = np.hstack(
        (sp.logspace(sp.log10(20.), sp.log10(200.),
                     14.), sp.logspace(sp.log10(250.), sp.log10(20000.), 13.)))
    plog = freqpoles(fplog, Fs)

    # Preparing data

    # making the measured response minumum-phase
    cp, minresp = rceps(data)

    # Impulse response
    imp = np.zeros(len(data), dtype=np.float64)
    imp[0] = 1.0

    # Target
    outf = []

    if target is 'flat':
        # Make the target output a bandpass filter
        Bf, Af = sig.butter(4, 30 / (Fs / 2), 'high')
        outf = sig.lfilter(Bf, Af, imp)

    else:
        # load target file
        t = np.loadtxt(target)
        frq = t[:, 0]
        pwr = t[:, 1]

        # calculate the FIR filter via windowing method
        fir = sig.firwin2(5001,
                          frq,
                          np.power(10, pwr / 20.0),
                          fs=(frq[-1] * 2))
        # Minimum phase, zero padding
        cp, outf = rceps(np.append(fir, np.zeros(len(minresp) - len(fir))))

    # Filter design

    # Parallel filter design
    (Bm, Am, FIR) = parfiltid(minresp, outf, plog)

    # equalized loudspeaker response - filtering the
    # measured transfer function by the parallel filter
    equalizedresp = parfilt(Bm, Am, FIR, data)

    # Equalizer impulse response - filtering a unit pulse
    equalizer = norm(parfilt(Bm, Am, FIR, imp))

    # Windowing with a half hanning window in time domain
    han = np.hanning(ntaps * 2)[-ntaps:]
    equalizer = han * equalizer[:ntaps]
    """
    Mixed-phase compensation
    Based on the paper "Mixed Time-Frequency approach for Multipoint
    Room Rosponse Equalization," by A. Carini et al.
    To use this feature, your Room Impulse Response should have all
    the leading zeros removed.
    """
    if mixed_phase is True:

        # prototype function
        hp = norm(np.real(equalizedresp))

        # time integration of the human ear is ~24ms
        # See "Measuring the mixing time in auditoria," by Defrance & Polack
        hop_size = 0.024
        samples = hop_size * Fs

        bins = np.int(np.ceil(len(hp) / samples))

        tmix = 0

        # Kurtosis method
        for b in range(bins):
            start = np.int(b * samples)
            end = np.int((b + 1) * samples)
            k = kurtosis(hp[start:end])
            if k <= 0:
                tmix = b * hop_size
                break

        # truncate the prototype function
        taps = np.int(tmix * Fs)

        print("\nmixing time(secs) = ", tmix, "; taps = ", taps)

        if taps > 0:
            # Time reverse the array
            h = hp[:taps][::-1]
            # create all pass filter
            phase = np.unwrap(np.angle(h))
            h = np.exp(1j * phase)
            # convert from db to linear
            mixed = np.power(10, np.real(h) / 20.0)
            # create filter's impulse response
            mixed = np.real(ifft(mixed))

            # convolve and window to desired length
            equalizer = conv(equalizer, mixed)
            equalizer = han * equalizer[:ntaps]

        else:
            print("zero taps; skipping mixed-phase computation")

    if opformat in ('wav'):
        wav_format = 'WAV'
        subtype = 'PCM_16'
    elif opformat in ('wav24'):
        wav_format = 'WAV'
        subtype = 'PCM_24'
    elif opformat in ('wav32'):
        wav_format = 'WAV'
        subtype = 'PCM_32'
    elif opformat in ('bin'):
        wav_format = 'RAW'
        subtype = 'FLOAT'
    else:
        print('Output format not recognized, no file generated.')

    # Write data
    wavwrite(filter, Fs, norm(np.real(equalizer)), wav_format, subtype)
    print('\nOutput format is ' + opformat)
    print('Output filter length =', len(equalizer), 'taps')
    print('Output filter written to ' + filter)

    print(
        '\nUse sox to convert output .wav to raw 32 bit IEEE floating point if necessary,'
    )
    print('or to merge left and right channels into a stereo .wav')
    print('\nExample (convert): sox leq48.wav -t f32 leq48.bin')
    print('        (merge): sox -M le148.wav re48.wav output.wav\n')

    # Plots

    if not noplot:
        data *= 500
        # original loudspeaker-room response
        plot(data, fs=Fs, avg='abs')
        # 1/3 Octave smoothed
        plot(data, fs=Fs, color='r', plots=True)

        # equalizer transfer function
        plot(0.75 * equalizer, fs=Fs, color='g')
        # indicating pole frequencies
        plt.vlines(fplog, -2, 2, color='k', linestyles='solid')

        # equalized loudspeaker-room response
        plot(equalizedresp * 0.01, fs=Fs, avg='abs')
        # 1/3 Octave smoothed
        plot(equalizedresp * 0.01, fs=Fs, color='r', plots=True)

        # Add labels
        # May need to reposition these based on input data
        plt.text(325, 30, 'Unequalized loudspeaker-room response')
        plt.text(100, -15, 'Equalizer transfer function')
        plt.text(100, -21, '(Black lines: pole locations)')
        plt.text(130, -70, 'Equalized loudspeaker-room response')

        a = plt.gca()
        a.set_xlim([20, 20000])
        a.set_ylim([-80, 80])
        plt.ylabel('Amplitude (dB)', color='b')
        plt.xlabel('Frequency (Hz)')
        plt.grid()
        plt.show()