Beispiel #1
0
def plot_result():
    cp_all = np.zeros((n_room, n_mic_pos, n_azi_tar, n_n_inter, n_test))
    rmse_all = np.zeros((n_room, n_mic_pos, n_azi_tar, n_n_inter, n_test))
    for room_i, room in enumerate(room_all):
        for mic_pos_i, mic_pos in enumerate(mic_pos_all):
            result_fpath = '../result/{}_{}.npy'.format(room, mic_pos)
            [cp_all[room_i, mic_pos_i],
             rmse_all[room_i, mic_pos_i]] = np.load(result_fpath)

    fig, ax = plt.subplots(1,
                           2,
                           figsize=[8, 3],
                           sharex=True,
                           tight_layout=True)
    for n_inter_i in range(n_n_inter):
        ax[0].plot(range(n_room),
                   np.mean(rmse_all[:, :, :, n_inter_i, :], axis=(1, 2, 3)),
                   alpha=0.5,
                   label=f'{n_inter_i+1} src')
        ax[1].plot(
            range(n_room),
            100 * (1 - np.mean(cp_all[:, :, :, n_inter_i, :], axis=(1, 2, 3))),
            alpha=0.5,
            label=f'{n_inter_i+1} src')

    ax[0].plot(range(n_room),
               np.mean(rmse_all, axis=(1, 2, 3, 4)),
               alpha=1,
               color='black',
               label=f'mean')
    ax[0].xaxis.set_ticks(range(n_room))
    ax[0].set_xticklabels([room[3:] for room in room_all])
    ax[0].set_xlabel('Room')
    ax[0].set_ylabel('RMSE($^o$)')
    # ax[0].spines['top'].set_visible(False)

    ax[1].plot(range(n_room),
               100 * (1 - np.mean(cp_all, axis=(1, 2, 3, 4))),
               alpha=1,
               color='black',
               label=f'mean')
    ax[1].legend()
    ax[1].set_xlabel('Room')
    ax[1].set_ylim([0, 60])
    ax[1].set_ylabel('Anomalies(%)')
    plot_tools.savefig(fig,
                       fig_name='result_room.png',
                       fig_dir='../images/result')

    with open('../result/result.txt', 'w') as result_file:
        result_file.write('RMSE')
        result_file.write('\t {}'.format(np.mean(rmse_all, axis=(1, 2, 3, 4))))

        result_file.write('Anomalies')
        result_file.write('\t {}'.format(1 -
                                         np.mean(cp_all, axis=(1, 2, 3, 4))))
Beispiel #2
0
def k_means_test():
    x, Y = make_blobs(cluster_std=1,
                      random_state=np.random.randint(100),
                      n_samples=500,
                      centers=3)
    # Stratch dataset to get ellipsoid data
    # x = np.dot(x,np.random.RandomState(0).randn(2,2))

    centers, fig = k_means(x, k=3, is_plot=True)
    plot_tools.savefig(fig)
def plot_train_process(model_dir):

    fig, ax = plt.subplots(1, 1)
    plot_settings = {'linewidth': 4}
    n_epoch_max = 0
    for room_i, room in enumerate(reverb_room_all):
        record_fpath = os.path.join(model_dir, room, 'train_record.npz')
        record_info = np.load(record_fpath)
        cost_record_valid = record_info['cost_record_valid']
        # rmse_record_valid = record_info['azi_rmse_record_valid']
        n_epoch = np.nonzero(cost_record_valid)[0][-1] + 1
        if n_epoch > n_epoch_max:
            n_epoch_max = n_epoch
        ax.plot(cost_record_valid[:n_epoch], **plot_settings, label=room[-1])

    ax.xaxis.set_major_locator(MaxNLocator(integer=True))
    ax.legend()
    ax.set_ylabel('Cross entrophy')
    ax.set_xlabel('Epoch(n)')

    plot_tools.savefig(fig, 'learning_curve_all.png', '../images')
                             std_37d]] = cal_norm_coef(dataset_room_dir_all)

        param_name = 'mct_{}'.format(room_tar)
        # np.save(os.path.join(norm_param_dir, '{}_37d.npy'.format(param_name)),
        #         [mean_37d, std_37d])

        # np.save(os.path.join(norm_param_dir, '{}_1d.npy'.format(param_name)),
        #         [mean_1d, std_1d])

        ax.errorbar(x=tau + (room_i - 1) * bar_width,
                    y=mean_37d,
                    yerr=std_37d,
                    label=room_tar)
    ax.set_xlabel('Delay(sample)')
    ax.legend()
    plot_tools.savefig(fig, fig_name='norm_coef.png', fig_dir='../images')

    # normalization parameters of all train data
    if True:
        dataset_room_dir_all = [
            os.path.join(dataset_dir, room) for room in room_all
        ]
        [[mean_1d, std_1d], [mean_37d,
                             std_37d]] = cal_norm_coef(dataset_room_dir_all)

        np.save(os.path.join(norm_param_dir, 'all_room_37d.npy'),
                [mean_37d, std_37d])

        np.save(os.path.join(norm_param_dir, 'all_room_1d.npy'),
                [mean_1d, std_1d])
Beispiel #5
0
                               is_middle_ear=True,
                               ihc_type='Roman')
    t_frame = np.arange(n_frame) * shift_len + int(frame_len / 2)
    fig = plt.figure(figsize=(8, 4), tight_layout=True)
    ax1 = plt.subplot(221)
    ax1.plot(t_frame, flag_frame_all, color='red', label='Criteria')
    ax1.legend(loc='upper right')
    ax_twin = ax1.twinx()
    ax_twin.plot(np.sum(front_end.filter(tar_record)[band_i], axis=1),
                 label='tar')
    ax_twin.plot(np.sum(front_end.filter(inter_record)[band_i], axis=1),
                 label='inter')
    ax_twin.legend(loc='lower right')

    ax2 = plt.subplot(223)
    ax2.plot(t_frame, vad_flag_frame_all + 0.09, label='vad')
    ax2.plot(t_frame, snr_flag_frame_all + 0.06, label='snr')
    ax2.plot(t_frame, ccf_flag_frame_all + 0.03, label='ccf')
    ax2.plot(t_frame, itd_flag_frame_all, label='itd')
    ax2.legend()

    ax3 = plt.subplot(122)
    plot_cue_sample(cue_frame_all[band_i], ax3, label='all')
    plot_cue_sample(cue_frame_all[band_i, flag_frame_all, :],
                    ax3,
                    label='preserve')
    ax3.legend()
    plot_tools.savefig(fig,
                       fig_name=f'criterias_eg_{band_i}.png',
                       fig_dir='../images/train')
Beispiel #6
0
def file_reader(fea_dir,
                band_tar=None,
                azi_tar=None,
                is_screen=False,
                record_dir=None,
                is_plot=False,
                fig_name=None,
                is_pb=False):
    #
    theta_vad = 40
    theta_corr_coef = 0.3
    theta_itd = 44.0 / 44.1

    if is_screen:
        src_fpath_all = load_src_fpath(record_dir)

    fea_fpath_all = get_fpath(fea_dir, suffix='.npz', is_absolute=True)
    pb = ProcessBar(len(fea_fpath_all))
    for fea_fpath in fea_fpath_all:
        if is_pb:
            pb.update()
        *_, room, mic_pos, fname = fea_fpath[:-4].split('/')
        azi, wav_i, snr = [np.int16(item) for item in fname.split('_')]
        if (azi_tar is not None) and (azi != azi_tar):
            continue

        fea_file = np.load(fea_fpath)
        cue_frame_all = fea_file['cue_frame_all']
        ccf_frame_all = fea_file['ccf_frame_all']
        snr_frame_all = fea_file['snr_frame_all']

        if not is_screen:
            if band_tar is None:
                yield np.transpose(cue_frame_all, axes=(1, 0, 2))
            else:
                yield cue_frame_all[band_tar]
        else:
            n_frame = cue_frame_all.shape[1]
            flag_frame_all_band_all = []

            # feature selection
            # 1. vad, on one channel(L)
            src_fpath_tar = \
                src_fpath_all[room][mic_pos][f'{azi}_{wav_i}_{snr}'][0]
            src_fpath_tar = src_fpath_tar.replace('Data/TIMIT',
                                                  'Data/TIMIT_wav')
            src_tar, fs = wav_tools.read_wav(src_fpath_tar)

            tar_fpath = ''.join((f'{record_dir}/{room}/{mic_pos}/',
                                 f'{azi}_{wav_i}_{snr}_tar.wav'))
            tar, fs = wav_tools.read_wav(tar_fpath)
            if tar.shape[0] != src_tar.shape[0]:
                raise Exception()

            # time delay between source and recording, about 70 samples
            delay = 190
            src_tar = np.concatenate((src_tar[delay:], np.zeros(delay)))
            vad_flag_frame_all = wav_tools.vad(x=src_tar,
                                               frame_len=frame_len,
                                               shift_len=shift_len,
                                               theta=theta_vad,
                                               is_plot=False)
            vad_flag_frame_all = np.squeeze(vad_flag_frame_all[:n_frame])

            for band_i in range(n_band):
                if band_tar is not None:
                    if band_i != band_tar:
                        continue

                # 2. SNR in each frequency band
                snr_flag_frame_all = snr_frame_all[band_i] > 0.0

                # 3. correlation coefficients
                ccf_flag_frame_all = np.greater(
                    np.max(ccf_frame_all[band_i], axis=1), theta_corr_coef)

                # 4. ITDs range
                itd_flag_frame_all = np.less(
                    np.abs(cue_frame_all[band_i, :, 0]), theta_itd)  # itd ms

                # combine all criteras
                flag_frame_all = np.logical_and.reduce(
                    (vad_flag_frame_all, snr_flag_frame_all,
                     ccf_flag_frame_all, itd_flag_frame_all))
                flag_frame_all_band_all.append(flag_frame_all)

            # plot waveform and corresponding criteria result
            if is_plot:
                tar_fpath = os.path.join('{}_{}.wav'.format(azi, wav_i))
                tar, fs = wav_tools.read_wav(tar_fpath)

                inter_fpath = '{}_{}_{}.wav'.format(azi, wav_i, snr)
                inter, fs = wav_tools.read_wav(inter_fpath)

                front_end = Auditory_model(fs=fs,
                                           cf_low=freq_low,
                                           cf_high=freq_high,
                                           n_band=n_band,
                                           is_middle_ear=True,
                                           ihc_type='Roman')
                t_frame = np.arange(n_frame) * shift_len + int(frame_len / 2)
                fig = plt.figure(figsize=(8, 4), tight_layout=True)
                ax1 = plt.subplot(221)
                ax1.plot(np.sum(front_end.filter(inter)[band_i], axis=1))
                ax1.plot(np.sum(front_end.filter(tar)[band_i], axis=1))
                ax_twin = ax1.twinx()
                ax_twin.plot(t_frame, flag_frame_all, color='red')

                ax2 = plt.subplot(223)
                ax2.plot(t_frame, vad_flag_frame_all + 0.09, label='vad')
                ax2.plot(t_frame, snr_flag_frame_all + 0.06, label='snr')
                ax2.plot(t_frame, ccf_flag_frame_all + 0.03, label='ccf')
                ax2.plot(t_frame, itd_flag_frame_all, label='itd')
                ax2.legend()

                ax3 = plt.subplot(122)
                plot_cue_sample(cue_frame_all[band_i], ax3)
                plot_cue_sample(cue_frame_all[band_i, flag_frame_all, :], ax3)

                plot_tools.savefig(fig, fig_name=fig_name, fig_dir='./')
                return

            if band_i is None:
                flag_frame_all = np.logical_and.reduce(flag_frame_all_band_all)
                yield np.transpose(cue_frame_all[:, flag_frame_all, :],
                                   axes=(1, 0, 2))
            else:
                flag_frame_all = flag_frame_all_band_all[0]
                yield cue_frame_all[band_tar, flag_frame_all, :]
        record_fpath = f'{model_dir_base}/{room}/train_record.npz'
        record_info = np.load(record_fpath)
        cost_record_valid = record_info['cost_record_valid']
        n_epoch = np.nonzero(cost_record_valid)[0][-1] + 1
        if n_epoch > n_epoch_max:
            n_epoch_max = n_epoch
        ax[0].plot(cost_record_valid[:n_epoch],
                   **plot_settings,
                   label=room[-1])

    # rmse
    rmse_all = np.load(f'{model_dir_base}/result.npy')
    rmse_mean_all = np.mean(rmse_all, axis=0)
    rmse_std_all = np.std(rmse_all, axis=0)

    ax[0].xaxis.set_major_locator(MaxNLocator(integer=True))
    ax[0].legend()
    ax[0].set_ylabel('Cross entrophy')
    ax[0].set_xlabel('Epoch(n)')
    ax[0].set_title('Learning Curve')

    ax[1].bar([room[-1] for room in reverb_room_all],
              rmse_mean_all,
              yerr=rmse_std_all / np.sqrt(n_test))
    plt.xticks(rotation=45)
    ax[1].set_xlabel('Room')
    ax[1].set_ylabel('RMSE')
    ax[1].set_title('Test')
    man_std = [[rmse_mean_all[i], rmse_std_all[i]] for i in range(4)]
    plot_tools.savefig(fig, 'result.png', model_dir_base)