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
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])
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