def test_one_hop(ext_test_imgs):

    # a = time.time()
    # n_samp = ext_test_imgs.shape[0]
    # num_cores = int(multiprocessing.cpu_count() / 2)
    #
    # test_one_hop_attri_saab = Parallel(n_jobs=num_cores, backend='multiprocessing')(delayed(concatenate)(ext_test_imgs, k, 2, True) for k in range(n_samp))
    # print(type(test_one_hop_attri_saab))
    # test_one_hop_attri_saab = np.array(test_one_hop_attri_saab)
    # print(test_one_hop_attri_saab.shape)
    # b = time.time()
    # print("time for concatenate 1st test:", b - a)
    #
    # # Saab dimension reduction from 27 dim to 12 dim
    # test_one_hop_attri = Saab_Getfeature(test_one_hop_attri_saab, pca_params_1)
    # print("Test 1st hop attribute shape:", test_one_hop_attri.shape)

    # gt = np.zeros((ext_test_imgs.shape[0], ext_test_imgs.shape[1], ext_test_imgs.shape[2]))
    # test_one_hop_attri = PixelHop_Unit(ext_test_imgs, gt, dilate=np.array([1]), num_AC_kernels=27, pad='reflect',
    #                               weight_root=save_dir_matFiles + 'weight',
    #                               getK=True, Pass_Ener_thrs=0.2, energy_percent=0.02, useDC=False, stride=None,
    #                               getcov=0, split_spec=1, hopidx=1)

    test_one_hop_attri_saab = PixelHop_Unit(ext_test_imgs, dilate=0, window_size=2, idx_list=None, getK=False, pad='reflect',
                                       weight_root=save_dir_matFiles + 'weight',
                                            Pass_Ener_thrs=0.1, Leaf_Ener_thrs=0.0001, num_kernels=None, PCA_ener_percent=0.99,
                                            useDC=False, stride=None, getcov=0, split_spec=0, hopidx=1)


    test_one_hop_attri_biased = PixelHop_fit(weight_name=save_dir_matFiles + 'weight',feature_ori=test_one_hop_attri_saab,
                                             split_spec=0, hopidx=1, Pass_Ener_thrs=0.1)

    return test_one_hop_attri_biased
def one_hop(extended_imgs):

    # # 8 nearest neighbors
    # a = time.time()
    # n_samp = extended_imgs.shape[0]
    #
    # num_cores = int(multiprocessing.cpu_count()/2)
    #
    # one_hop_attri_saab = Parallel(n_jobs=num_cores, backend='multiprocessing')(delayed(concatenate)(extended_imgs, k, 2, False) for k in range(n_samp))
    # print(type(one_hop_attri_saab))
    #
    # one_hop_attri_saab = np.array(one_hop_attri_saab)
    # print(one_hop_attri_saab.shape)
    #
    # b = time.time()
    # print("time for concatenate 1st :", b-a)

    # # Saab dimension reduction from 27 dim to 12 dim
    # pca_params_1 = Saab_Getkernel(one_hop_attri_saab, "27")
    # c = time.time()
    # print("time for kernel:", c-b)
    #
    # one_hop_attri = Saab_Getfeature(one_hop_attri_saab, pca_params_1)
    # d = time.time()
    # print("time for feature:", d-c)

    one_hop_attri_saab = PixelHop_Unit(extended_imgs,
                                       getK=True,
                                       idx_list=None,
                                       dilate=1,
                                       window_size=4,
                                       pad='reflect',
                                       weight_root=save_dir_matFiles +
                                       'weight',
                                       Pass_Ener_thrs=0.1,
                                       Leaf_Ener_thrs=0.0001,
                                       num_kernels=None,
                                       PCA_ener_percent=0.99,
                                       useDC=False,
                                       stride=None,
                                       getcov=0,
                                       split_spec=0,
                                       hopidx=1)

    print("Hop-1 feature ori shape:", one_hop_attri_saab.shape)
    one_hop_attri_response = PixelHop_fit(weight_name=save_dir_matFiles +
                                          'weight',
                                          feature_ori=one_hop_attri_saab,
                                          split_spec=0,
                                          hopidx=1,
                                          Pass_Ener_thrs=0.1)

    print("one hop attribute shape:", one_hop_attri_response.shape)

    return one_hop_attri_response
def test_second_hop(test_one_hop_attri_response, idx_stop_list):

    # a = time.time()
    # n_samp = test_one_hop_attri.shape[0]
    # num_cores = int(multiprocessing.cpu_count() / 2)
    #
    # test_sec_hop_attri_saab = Parallel(n_jobs=num_cores, backend='multiprocessing')(delayed(concatenate)(test_one_hop_attri, k, 3, False) for k in range(n_samp))
    # print(type(test_sec_hop_attri_saab))
    # test_sec_hop_attri_saab = np.array(test_sec_hop_attri_saab)
    # print(test_sec_hop_attri_saab.shape)
    # b = time.time()
    # print("time for concatenate 2nd :", b - a)
    #
    # # Saab dimension reduction from 27 dim to 12 dim
    # test_sec_hop_attri = Saab_Getfeature(test_sec_hop_attri_saab, pca_params_2)
    # print("Test 2nd hop attribute shape:", test_sec_hop_attri.shape)

    all_nodes = np.arange(test_one_hop_attri_response.shape[-1]).tolist()
    for i in idx_stop_list:
        if i in all_nodes:
            all_nodes.remove(i)
    intermediate_idx = all_nodes

    test_sec_hop_attri_saab = PixelHop_Unit(test_one_hop_attri_response,
                                            dilate=7,
                                            window_size=1,
                                            idx_list=intermediate_idx,
                                            getK=False,
                                            pad='reflect',
                                            weight_root=save_dir_matFiles +
                                            'weight',
                                            Pass_Ener_thrs=0.1,
                                            Leaf_Ener_thrs=0.0001,
                                            num_kernels=None,
                                            PCA_ener_percent=0.95,
                                            useDC=False,
                                            stride=None,
                                            getcov=0,
                                            split_spec=1,
                                            hopidx=2)

    print("sec hop neighborhood construction shape:",
          test_sec_hop_attri_saab.shape)

    test_sec_hop_attri_response = PixelHop_fit(
        weight_name=save_dir_matFiles + 'weight',
        feature_ori=test_sec_hop_attri_saab,
        split_spec=1,
        hopidx=2,
        Pass_Ener_thrs=0.1)

    print("sec hop attri shape:", test_sec_hop_attri_response.shape)

    return test_sec_hop_attri_response
def test_one_hop(ext_test_imgs):
    test_one_hop_attri_saab = PixelHop_Unit(ext_test_imgs, dilate=1, window_size=window_rad,
                                            idx_list=None, getK=False, pad='reflect',
                                            weight_root=os.path.join(save_dir_matFiles, 'weight'),
                                            Pass_Ener_thrs=0.1, Leaf_Ener_thrs=0.0001, num_kernels=None,
                                            PCA_ener_percent=0.99,
                                            useDC=False, stride=None, getcov=0, split_spec=0, hopidx=1)
    test_one_hop_attri_biased = PixelHop_trans(weight_name=os.path.join(save_dir_matFiles, 'weight'),
                                               feature_ori=test_one_hop_attri_saab,
                                               split_spec=0, hopidx=1, Pass_Ener_thrs=0.1)
    return test_one_hop_attri_biased
def test_forth_hop(test_third_hop_attri_response):
    test_fourth_hop_attri_saab = PixelHop_Unit(test_third_hop_attri_response, dilate=1, window_size=window_rad,
                                               idx_list=None, getK=False, pad='reflect',
                                               weight_root=os.path.join(save_dir_matFiles, 'weight'),
                                               Pass_Ener_thrs=0.1, Leaf_Ener_thrs=0.0001, num_kernels=None,
                                               PCA_ener_percent=0.97,
                                               useDC=False, stride=None, getcov=0, split_spec=1, hopidx=4)
    # print("Hop-3 feature ori shape:", test_third_hop_attri_saab.shape)
    test_fourth_hop_attri_response = PixelHop_trans(weight_name=os.path.join(save_dir_matFiles, 'weight'),
                                                    feature_ori=test_fourth_hop_attri_saab,
                                                    split_spec=1, hopidx=4, Pass_Ener_thrs=0.1)
    # print("third hop attri shape:", test_third_hop_attri_response.shape)
    return test_fourth_hop_attri_response
Beispiel #6
0
def third_hop(response, energy_prev, test_input):
    print("---- Hop 3 ----")
    sec_hop_total = []
    sec_hop_energy = []
    sec_hop_test = []
    for c in range(response.shape[-1]):
        cur_response = response[:, :, :, c][:, :, :, None]
        cur_test = test_input[:, :, :, c][:, :, :, None]
        cur_attri_saab, cur_saab = PixelHop_Unit(cur_response, getK=True, idx_list=None,
                                                 dilate=2, window_size=window_rad, pad='reflect',
                                                 weight_root=os.path.join(save_dir_matFiles, 'weight'),
                                                 Pass_Ener_thrs=0.1, Leaf_Ener_thrs=0.6, num_kernels=None,
                                                 PCA_ener_percent=1,
                                                 useDC=False, stride=None, getcov=0, split_spec=0, hopidx=1)
        cur_response = PixelHop_trans(weight_name=os.path.join(save_dir_matFiles, 'weight'),
                                      feature_ori=cur_attri_saab,
                                      split_spec=0, hopidx=1, Pass_Ener_thrs=0.1)
        cur_test = Shrink(cur_test)
        cur_test_context = PixelHop_trans(weight_name=os.path.join(save_dir_matFiles, 'weight'),
                                          feature_ori=cur_test,
                                          split_spec=0, hopidx=1, Pass_Ener_thrs=0.1)

        print("sec hop attribute shape:", cur_response.shape)
        energy = cur_saab['leaf0'].tree['ac_energy']
        energy = energy / np.sum(energy) * energy_prev[c]

        engr_th = 0.0005
        high_engr_channels = np.where((energy > engr_th))

        to_append = np.squeeze(cur_response[:, :, :, high_engr_channels])
        if len(to_append.shape) == 3:
            to_append = to_append[:, :, :, None]
        if to_append.shape[-1] == 0:
            to_append = np.squeeze(cur_response[:, :, :, :3])
        sec_hop_total.append(to_append.copy())
        sec_hop_energy.append(energy[high_engr_channels].copy() if len(high_engr_channels) != 0 else energy[:3].copy())

        to_append_test = np.squeeze(cur_test_context[:, :, :, high_engr_channels])
        if len(to_append_test.shape) == 3:
            to_append_test = to_append_test[:, :, :, None]
        if to_append_test.shape[-1] == 0:
            to_append_test = np.squeeze(cur_test_context[:, :, :, :3])
        sec_hop_test.append(to_append_test.copy())

        print("CURRENT C:", c)

    sec_hop_total = np.concatenate(sec_hop_total, axis=3)
    sec_hop_energy = np.concatenate(sec_hop_energy, axis=0)
    sec_hop_test = np.concatenate(sec_hop_test, axis=3)
    return sec_hop_total, sec_hop_energy, sec_hop_test
Beispiel #7
0
def one_hop(response, test_response):
    print("---- Hop 1 ----")
    one_hop_attri_saab, all_saab = PixelHop_Unit(response, getK=True, idx_list=None,
                                                 dilate=2, window_size=window_rad, pad='reflect',
                                                 weight_root=os.path.join(save_dir_matFiles, 'weight'),
                                                 Pass_Ener_thrs=0.1, Leaf_Ener_thrs=0.6, num_kernels=None,
                                                 PCA_ener_percent=1,
                                                 useDC=False, stride=None, getcov=0, split_spec=0, hopidx=1)
    print("Hop-1 feature ori shape:", one_hop_attri_saab.shape)
    one_hop_attri_response = PixelHop_trans(weight_name=os.path.join(save_dir_matFiles, 'weight'),
                                            feature_ori=one_hop_attri_saab,
                                            split_spec=0, hopidx=1, Pass_Ener_thrs=0.1)

    test_response = Shrink(test_response)

    test_context = PixelHop_trans(weight_name=os.path.join(save_dir_matFiles, 'weight'),
                                  feature_ori=test_response,
                                  split_spec=0, hopidx=1, Pass_Ener_thrs=0.1)

    print("one hop attribute shape:", one_hop_attri_response.shape)
    kernels = all_saab['leaf0'].tree['kernel']
    energy = all_saab['leaf0'].tree['ac_energy']
    energy = energy / np.sum(energy)
    kernel_5 = np.array(
        [[0, 0, 0, 0, 0],
         [0, 1, 1, 1, 0],
         [0, 1, 1, 1, 0],
         [0, 1, 1, 1, 0],
         [0, 0, 0, 0, 0],
         ]
    )
    high_frez_pow = []

    thres_high = 0.7
    engr_th_high = 0.005
    engr_th_low = 0.02
    for each_kernel in kernels:
        each_kernel = np.reshape(each_kernel, (5, 5))
        each_fft = np.fft.fftshift(np.fft.fft2(each_kernel))
        high_frez_pow.append(1 - np.sum(np.abs(each_fft * kernel_5)) / np.sum(np.abs(each_fft)))
    high_frez_pow = np.array(high_frez_pow)
    high_frez_channels = np.where((high_frez_pow > thres_high) & (energy > engr_th_high))
    low_frez_channels = np.where((high_frez_pow < thres_high) & (energy > engr_th_low))
    return (one_hop_attri_response[:, :, :, high_frez_channels], one_hop_attri_response[:, :, :, low_frez_channels],
            energy[high_frez_channels], energy[low_frez_channels],
            test_context[:, :, :, high_frez_channels], test_context[:, :, :, low_frez_channels])