def mixture_analysis(pi, mu, cov, name, gs=None): # k components: pi = (k) , mu = (k), cov = (k,k,k) print(pi.shape, mu.shape, cov.shape) print(mu) print(pi) if pi.shape[0] == 8: filters = mu.reshape((8, 3, 3)) filters = filters.reshape(4, 2, 3, 3) plot_weights(filters, name) else: filters = np.zeros((4, 3, 7, 7)) for i in range(4): for s, e in ((0, 10), (10, 20), (20, 30)): beta = mu[i, s:e] filter = gabor_kernel_3(beta[0], theta=np.arctan2(beta[1], beta[2]) / 2, sigma_x=beta[3], sigma_y=beta[4], offset=np.arctan2(beta[5], beta[6]), x_c=beta[7], y_c=beta[8], scale=beta[9], ks=7) filters[i, int(s / 10)] = filter show_kernels(filters, name, gs)
def do_distribution_gabor_init(weights, config, index, shape, **kwargs): if index != 0: rel = config[f'file_{index}'] file = path.join(path.dirname(__file__), f'..{rel}') params = np.load(file) else: rel = config["file"] file = path.join(path.dirname(__file__), f'..{rel}') params = np.load(file) param, tuples = prepare_gabor_params(params) # np.random.seed(0) components = config[f'comp_{index}'] if f'comp_{index}' in config else 0 samples = mixture_gaussian(param, shape[0], components, f'gabor_{index}') new_weights = np.zeros(shape) for i in range(shape[0]): for s, e in tuples: beta = samples[i, s:e] filter = gabor_kernel_3(beta[0], theta=np.arctan2(beta[1], beta[2]) / 2, sigma_x=beta[3], sigma_y=beta[4], offset=np.arctan2(beta[5], beta[6]), x_c=beta[7], y_c=beta[8], scale=beta[9], ks=shape[-1]) new_weights[i, int(s / 10)] = filter # if weights.shape[1] == 3: # show_kernels(new_weights, 'distribution_init') # else: # plot_conv_weights(weights, 'distribution_init_kernel') return new_weights
def objective_function_2(beta, X, size=3): kernel = gabor_kernel_3(beta[0], theta=beta[1], sigma_x=beta[2], sigma_y=beta[3], offset=beta[4], x_c=beta[5], y_c=beta[6], scale=beta[7], ks=size) kernel = normalize(kernel) error = mean_squared_error(kernel, X) return (error)
def do_fit_gabor_init(weights, config, **kwargs): gabor_params = np.load(config["file"]) idx = 0 for kernels in gabor_params: for beta in kernels: kernel = gabor_kernel_3(beta[0], theta=beta[1], sigma_x=beta[2], sigma_y=beta[3], offset=beta[4], x_c=beta[5], y_c=beta[6], ks=7) if config['reshape']: kernel = reshape_with_project(kernel) weights[int(idx / 3), idx % 3] = kernel idx += 1 show_kernels(weights, 'fit_gabors') return weights
def do_fit_gabor_dist(weights, config, **kwargs): # fit independent gaussians for each gabor parameter params = np.load(config["file"]) idx = 0 samples = [] for i in range(params.shape[2] - 1): param = params[:, :, i] mu, std = norm.fit(param) samples.append(np.random.normal(mu, std, weights.shape[0])) for k in range(weights.shape[0]): for f in range(weights.shape[1]): kernel = gabor_kernel_3(samples[0][k], theta=samples[1][k], sigma_x=samples[2][k], sigma_y=samples[3][k], offset=samples[4][k], x_c=samples[5][k], y_c=samples[6][k], ks=weights.shape[2]) if config['reshape']: kernel = reshape_with_project(kernel) weights[k, f] = kernel show_kernels(weights, 'independent_dist') return weights
def do_scrumble_gabor_init(weights, config, **kwargs): gabor_params = np.load(config["file"]) idx = 0 gabor_params = prepare_gabor_params(gabor_params) for i in range(gabor_params.shape[1]): old_params = gabor_params[:, i] randoms = random_state.permutation(gabor_params.shape[0]) gabor_params[:, i] = gabor_params[randoms, i] for filter_params in gabor_params: for s, e in ((0, 10), (10, 20), (20, 30)): beta = filter_params[s:e] filter = gabor_kernel_3(beta[0], theta=np.arctan2(beta[1], beta[2]) / 2, sigma_x=beta[3], sigma_y=beta[4], offset=np.arctan2(beta[5], beta[6]), x_c=beta[7], y_c=beta[8], scale=beta[9], ks=7) weights[idx, int(s / 10)] = filter idx += 1 show_kernels(weights, config, 'scrumble_gabor') return weights
def do_distribution_gabor_init_channel(weights, config, index, shape, **kwargs): if index != 0: params = np.load(config[f'file_{index}']) else: params = np.load(f'{config["file"]}') param, tuples = prepare_gabor_params_channel(params) # np.random.seed(0) components = config[f'comp_{index}'] if f'comp_{index}' in config else 0 samples = mixture_gaussian(param, shape[0], components) for i in range(shape[0]): beta = samples[i] filter = gabor_kernel_3(beta[0], theta=np.arctan2(beta[1], beta[2]) / 2, sigma_x=beta[3], sigma_y=beta[4], offset=np.arctan2(beta[5], beta[6]), x_c=beta[7], y_c=beta[8], scale=beta[9], ks=shape[-1]) weights[int(i / shape[0]), int(i % shape[0])] = filter # if weights.shape[1] == 3: # show_kernels(weights, 'distribution_init_channel_color') # else: # plot_conv_weights(weights, 'distribution_init_channel') return weights
def compare_two_values(): # ((-0.5, 1.5), (-np.pi, 2 * np.pi), (-4, 4), (-4, 4), (-3, 3), (-5, 5)) params = [-0.5, -2 * np.pi / 8, 0, 0, 0, 0] params = [0.5, np.pi + ((-2 * np.pi / 8) % np.pi), 0, 0, 0, 0] gabor_kernel_3(*params)
def analyze_param_dist(name, plot=False): params = np.load(f'{name}.npy') names = [ 'Frequency', 'Theta', 'Sigma X', 'Sigma Y', 'Offset', 'Center X', 'Center Y' ] variables = np.zeros((7, 192)) # for i in range(params.shape[2]-1): # param = params[:,:,i] # variables[i] = param.flatten() param = params[:, :, :-1] param = param.reshape(64, -1) pca_res = pca(param, n_components=21) principal_components = pca_res.transform(param) # principal_components shape: (192,3) if plot: plot_2d(principal_components[:, 0], principal_components[:, 1], 'PC 1 & 2') plot_2d(principal_components[:, 1], principal_components[:, 2], 'PC 2 & 3') plot_2d(principal_components[:, 0], principal_components[:, 2], 'PC 1 & 3') plot_2d(principal_components[:, 0], principal_components[:, 3], 'PC 1 & 4') plot_2d(principal_components[:, 0], principal_components[:, 4], 'PC 1 & 5') plot_3d(principal_components[:, 0], principal_components[:, 1], principal_components[:, 2], 'Principal components of gabor filter params') reg = fit_data(principal_components, variables.T) small_samples = multivariate_gaussian(principal_components.T, 10) # small_samples shape (3, 10) full_params = reg.predict(small_samples.T) # shape (10, 7) small_samples_hat = pca_res.transform(full_params) # shape (10,3) full_params_hat = reg.predict(small_samples_hat) print(mean_squared_error(small_samples.T, small_samples_hat)) idx = 0 plt.figure(figsize=(5, 10)) gs = gridspec.GridSpec(20, 3, width_ratios=[1] * 3, wspace=0.5, hspace=0.5, top=0.95, bottom=0.05, left=0.1, right=0.95) for i in range(10): alpha = full_params[i] beta = full_params_hat[i] kernel2 = gabor_kernel_3(beta[0], theta=beta[1], sigma_x=beta[2], sigma_y=beta[3], offset=beta[4], x_c=beta[5], y_c=beta[6], ks=7) kernel1 = gabor_kernel_3(alpha[0], theta=alpha[1], sigma_x=alpha[2], sigma_y=alpha[3], offset=alpha[4], x_c=alpha[5], y_c=alpha[6], ks=7) ax = plt.subplot(gs[i, 0]) ax.set_xticks([]) ax.set_yticks([]) ax.set_title(f'Samples parameter set', pad=3, fontsize=5) idx += 1 plt.imshow(kernel2, cmap='gray') ax = plt.subplot(gs[i, 1]) ax.set_title(f'Reconstruction', pad=10, fontsize=5) plt.imshow(kernel1, cmap='gray') ax.set_xticks([]) ax.set_yticks([]) plt.tight_layout() idx += 1 plt.savefig(f'reconstructions.png') plt.show() if plot: principal_components = principal_components.T corr_pca = generate_correlation_map(principal_components, principal_components) corr_map = generate_correlation_map(variables, variables) mask = np.zeros_like(corr_map) mask[np.triu_indices_from(mask)] = True plot_heatmap(corr_map, names, names, title='Gabor parameter correlation', mask=mask) mask = np.zeros_like(corr_pca) mask[np.triu_indices_from(mask)] = True plot_heatmap(corr_pca, names, names, title='PCA correlations', mask=mask)
def mixture_base(param, tuples, shape, size=3): new_param = np.zeros((shape[0], 10 * shape[1])) for i in range(shape[0]): for s, e in tuples: p = param[i, int(s * 8 / 10):int(e * 8 / 10)] new_param[i, s:e] = (p[0], np.sin(2 * p[1]), np.cos(2 * p[1]), p[2], p[3], np.sin(p[4]), np.cos(p[4]), p[5], p[6], p[7]) # plot_bic(best_gmm, param) best_gmm = mixture_gaussian(new_param) samples = best_gmm.sample(new_param.shape[0])[0] idx = 1 plt.figure(figsize=(10, 20)) gs = gridspec.GridSpec(22, 3, width_ratios=[1] * 3, wspace=0.5, hspace=0.5, top=0.95, bottom=0.05, left=0.1, right=0.95) for i in range(shape[0]): for s, e in tuples: beta = samples[i, s:e] kernel2 = gabor_kernel_3(beta[0], theta=np.arctan2(beta[1], beta[2]), sigma_x=beta[3], sigma_y=beta[4], offset=np.arctan2(beta[5], beta[6]), x_c=beta[7], y_c=beta[8], scale=beta[9], ks=size) ax = plt.subplot(gs[i, int(s / 10)]) ax.set_xticks([]) ax.set_yticks([]) plt.imshow(kernel2, cmap='gray') idx += 1 plt.figure(figsize=(15, 15)) length = 64 * 3 matrix = np.empty([length, 0]) weights = samples.reshape(64, 64, 10) for i in range(0, 64): row = np.empty([0, 3]) for j in range(0, 64): beta = weights[i, j] channel = gabor_kernel_3(beta[0], theta=np.arctan2(beta[1], beta[2]), sigma_x=beta[3], sigma_y=beta[4], offset=np.arctan2(beta[5], beta[6]), x_c=beta[7], y_c=beta[8], scale=beta[9], ks=size) row = np.concatenate((row, channel), axis=0) f_min, f_max = np.min(row), np.max(row) row = (row - f_min) / (f_max - f_min) matrix = np.concatenate((matrix, row), axis=1) plot_matrixImage(matrix, 'Gabor_conv2') plt.tight_layout() plt.show()
def compare_gabors(version='V1', file='gabor_params_basinhopping_bound_6_params'): # file = 'gabor_params_basinhopping_bound_6_params' gabor_params = np.load(f'{file}.npy') # model = get_model('CORnet-S_base', True) model = get_model('CORnet-S_full_epoch_43', True) # model = get_resnet50(True) counter = 0 plt.figure(figsize=(4, 25)) gs = gridspec.GridSpec(20, 3, width_ratios=[1] * 3, wspace=0.5, hspace=0.5, top=0.95, bottom=0.05, left=0.1, right=0.95) for name, m in model.named_modules(): if type(m) == nn.Conv2d and counter == 0: weights = m.weight.data.squeeze().numpy() index = 1 for i in range(0, 10): for j in range(0, 3): beta = gabor_params[i, j] kernel2 = weights[i, j] kernel2 = normalize(kernel2) if version is "V1": kernel1 = np.real( gabor_kernel(beta[0], theta=beta[1], sigma_x=beta[2], sigma_y=beta[3], offset=beta[4], n_stds=beta[5])) kernel1 = np.nan_to_num(kernel1).astype(np.float32) kernel1 = resize(kernel1, (kernel2.shape[0], kernel2.shape[0]), anti_aliasing=True, preserve_range=True) kernel1 = normalize(kernel1) else: kernel1 = gabor_kernel_3(beta[0], theta=beta[1], sigma_x=beta[2], sigma_y=beta[3], offset=beta[4], x_c=beta[5], y_c=beta[6], scale=beta[7], ks=7) kernel1 = normalize(kernel1) ax = plt.subplot(gs[i * 2, j]) ax.set_xticks([]) ax.set_yticks([]) ax.set_title(f'K {i}, F {j}', pad=3) plt.imshow(kernel2, cmap='gray') ax = plt.subplot(gs[(i * 2) + 1, j]) ax.set_title(f'Error {beta[-1]:.3}', pad=10) plt.imshow(kernel1, cmap='gray') index += 1 index += 3 # plt.tight_layout() plt.savefig(f'{file}.png') plt.show() return