def kernel_similarity(): model = get_model('CORnet-S_train_second_kernel_conv_epoch_00', 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) kernel_avgs = [] for name, m in model.named_modules(): if type(m) == nn.Conv2d: if counter == 1: weights = m.weight.data.squeeze().numpy().transpose(1, 0, 2, 3) for i in range(0, 64): avgs = [] for j in range(0, 64): for k in range(j, 64): if k != j: c1 = weights[i, j] c2 = weights[i, k] avgs.append(similarity(c2, c1)) kernel_avgs.append(np.mean(avgs)) plot_histogram(kernel_avgs, 'Channel similarity within a kernel', bins=20) print('Similarity avg:' + str(np.mean(kernel_avgs))) return counter += 1
def fit_linear_regression(): input = np.random.random([1, 8]) output = np.random.random([1, 9]) lin = LinearRegression() lin.fit(input, output) model = get_model('CORnet-S_base', True) counter = 0 np.random.seed(1) def print_fun(x, f, accepted): print("at minima %.4f accepted %d" % (f, int(accepted))) fun = lambda x, y: linear_regression(x, y, lin) for name, m in model.named_modules(): if type(m) == nn.Conv2d and counter == 0: weights = m.weight.data.cpu().numpy() lin_params = np.zeros([weights.shape[0], weights.shape[1], 8]) for i in range(0, weights.shape[0]): for j in range(0, weights.shape[1]): kernel = weights[i, j].flatten() minimizer_kwargs = { "method": "L-BFGS-B", 'args': (kernel), 'options': { 'maxiter': 200000, 'gtol': 1e-25 } } result = basinhopping(fun, [0, 0, 0, 0, 0, 0, 0, 0], niter=15, minimizer_kwargs=minimizer_kwargs, callback=print_fun, T=0.00001) y_hat = result.x lin_params[i, j] = y_hat
def hyperparam_gabor(): model = get_model('CORnet-S_base', True) counter = 0 gabor_params = np.zeros([64, 3, 5]) np.random.seed(1) for name, m in model.named_modules(): if type(m) == nn.Conv2d and counter == 0: weights = m.weight.data.cpu().numpy() for i in range(0, 10): for j in range(0, 3): kernel = weights[i, j] kernel = normalize(kernel) tuned_params = { "theta": np.arange(0, 1, 0.05), "frequency": np.arange(0, np.pi, np.pi / 12), "sigma": np.arange(0, 4, 0.25), "offset": np.arange(-2, 2, 0.5), "stds": np.arange(1, 4, 0.5), } best_score = np.NINF best_params = {} for g in ParameterGrid(tuned_params): score = score_kernel(kernel, **g) if score > best_score: best_score = score print(f'Update best score: {score}') best_params = g print( f'Best grid:{best_params} for kernel {i}, filter {j}') gabor_params[i, j] = np.fromiter(best_params.values(), dtype=float) np.save('gabor_params_grid_search_long.npy', gabor_params) return
def mutual_information(): model = get_model('CORnet-S_base', True) # model = get_model('CORnet-S_random', False) # model = get_model('CORnet-S_train_second_kernel_conv_epoch_00', 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) kernel_avgs = [] for name, m in model.named_modules(): if type(m) == nn.Conv2d and 'V1' not in name: weights = m.weight.data.squeeze().numpy() scores = np.zeros([weights.shape[0], weights.shape[0]]) kernels = weights.shape[0] for i in range(kernels): for j in range(kernels): # print(f'Score kernel {i} and {j}') scores[i, j] = feature_selection.mutual_info_regression( weights[i].flatten().reshape(-1, 1), weights[j].flatten()) print(f'Kernel mean mutual information {np.mean(scores)}') plot_heatmap(scores, title=f'Kernel mutual information {name}', col_labels='Kernels', row_labels='Kernels') if len(weights.shape) > 2: channels = weights.shape[1] scores = np.zeros([kernels, channels, channels]) for i in range(kernels): for j in range(channels): for k in range(channels): # print(f'Score channel {k} and {j} in kernel {i}') scores[ i, j, k] = feature_selection.mutual_info_regression( weights[i, j].flatten().reshape(-1, 1), weights[i, k].flatten()) scores = np.mean(scores, axis=0) # print(f'Channel mean mutual information {np.mean(scores)}') plot_heatmap(scores, title=f'Channel mean mutual information {name}', col_labels='Channel', row_labels='Channel')
def measure_performance(identifier, title, do_epoch=False, do_analysis=False): config = get_config(identifier) model = get_model(identifier, False, config) if do_epoch: time = run_model_training(model, identifier, config, train) # time = 0 # flops, params = get_model_complexity_info(model, (3, 224, 224), as_strings=False, print_per_layer_stat=True) # train_flops = flops * 2 * 3 * (1.2 * 1000000) * 1 # print( # f' Flops model {identifier}: {flops/1000000:.2f}, training flops {train_flops/1000000:.2f} params {params/1000000:.2f}') path = os.path.abspath(__file__) dir_path = os.path.dirname(path) path = f'{dir_path}/../scores.sqlite' db = create_connection(path) # model_id, train_time, weights_fixed, weights_to_train, additional_params, flops store_analysis(db, (title, time['time'], 0, 0, 0, 0))
def get_fist_layer_weights(): model = get_model('CORnet-S_train_second_kernel_conv', True) # model = get_model('CORnet-S_base', True) # model = get_model('CORnet-S_full_epoch_43', True) counter = 0 plt.figure(figsize=(20, 20)) gs = gridspec.GridSpec(10, 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() idx = 1 for i in range(0, 10): kernel1 = weights[i, 0] kernel2 = weights[i, 1] kernel3 = weights[i, 2] ax = plt.subplot(gs[i, 0]) ax.set_xticks([]) ax.set_yticks([]) ax.set_title(f'Samples parameter K1', pad=2, fontsize=5) idx += 1 plt.imshow(kernel2, cmap='gray') ax = plt.subplot(gs[i, 1]) ax.set_title(f'Samples parameter K2', pad=2, fontsize=5) idx += 1 plt.imshow(kernel1, cmap='gray') ax.set_xticks([]) ax.set_yticks([]) ax = plt.subplot(gs[i, 2]) ax.set_title(f'Samples parameter K3', pad=2, fontsize=5) plt.imshow(kernel3, cmap='gray') ax.set_xticks([]) ax.set_yticks([]) idx += 1 plt.tight_layout() plt.savefig('full_kernels.png') plt.show() return
def get_params(identifier, hyperparams=True): if 'hmax' in identifier: return 0 if identifier in param_list: return param_list[identifier] if identifier.startswith('mobilenet'): if identifier.startswith('mobilenet_v1_1.0'): params = get_mobilenet_params() print(f'{identifier} has {params} parameter') return params else: model = get_mobilenet(f'{identifier}_random')._model mapping = mobilenet_mapping if '_v5' in identifier: config = get_config(identifier.split('_v5_')[1]) mapping = mobilenet_mapping_5 if '_v6' in identifier: config = get_config(identifier.split('_v6_')[1]) mapping = mobilenet_mapping_6 if '_v1' in identifier: config = get_config(identifier.split('_v1_')[1]) if '_v7' in identifier: config = get_config(identifier.split('_v7_')[1]) mapping = mobilenet_mapping_6 config = create_config(mapping, config, model) if 'v5' in identifier or 'v6' in identifier: add_layers = ['model.2.0', 'model.2.1', 'model.6.0', 'model.6.1', 'model.12.0', 'model.12.1', 'fc', 'decoder'] config['layers'] = config['layers'] + add_layers del config['bn_init'] del config['batchnorm'] model = apply_generic_other(model, config) elif identifier.startswith('resnet'): model = get_resnet50(False) base = identifier.split('_v3_')[1] if '_v3_' in identifier else identifier.split('_v1_')[1] config = get_config(base) config = create_config(mapping_1, config, model) model = apply_generic_other(model, config) else: config = get_config(identifier) model = get_model(identifier, False, config) values = 0 hyper_params = 0 all = 0 hyp = [] hyp_w = [] all_w = [] layer = [] idx = 0 for name, m in model.named_modules(): if type(m) == torch.nn.Conv2d: size = 1 for dim in np.shape(m.weight.data.cpu().numpy()): size *= dim if any(value in name for value in config['layers']): values += size print(f'layer {name} is trained, size {size}') elif name in config and hyperparams: this_mod = sys.modules[__name__] if identifier.startswith('mobilenet') or identifier.startswith('resnet'): func = getattr(this_mod, config[config[name]].__name__) idx = layers.index(config[name]) else: func = getattr(this_mod, config[name].__name__) params = func(m.weight.data.cpu().numpy(), config=config, index=idx) values += params hyper_params += params print( f'layer {name} saves {size} weights and replaces it with {params} so {params / size} params') all += size idx += 1 layer.append(name) all_w.append(all) hyp.append(hyper_params) hyp_w.append(values) if type(m) == nn.BatchNorm2d and 'batchnorm' in config: size = 1 # name = config[name] if identifier.startswith('mobilenet') else name if name in conv_to_norm: for dim in np.shape(m.weight.data.cpu().numpy()): size *= dim if any(value in conv_to_norm[name] for value in config['layers']): this_mod = sys.modules[__name__] values += size if conv_to_norm[name] in config and hyperparams: this_mod = sys.modules[__name__] str(config['bn_init']) func = getattr(this_mod, config['bn_init'].__name__) params = func(m.weight.data.cpu().numpy(), config=config, index=idx) values += params hyper_params += params # if type(m) == nn.Linear and name is not 'decoder' and name is not 'fc': # size=1 # for dim in np.shape(m.weight.data.cpu().numpy()): size *= dim # values += size param_list[identifier] = values print(f'{identifier} has {values} parameter') return values
def benchmark_epoch(identifier): config = get_config(identifier) model = get_model(identifier, False, config) time = run_model_training(model, identifier, False, config, train)
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
def fit_gabors(version='V1', file='gabor_params_basinhopping'): # model = get_model('CORnet-S_base', True) model = get_model('CORnet-S_full_epoch_43', True) # model = get_resnet50(True) counter = 0 length = 7 if version is 'V1' else 9 np.random.seed(1) for name, m in model.named_modules(): if type(m) == nn.Conv2d: if name == 'V2.conv2': weights = m.weight.data.cpu().numpy() gabor_params = np.zeros( [weights.shape[0], weights.shape[1], length]) for i in range(0, weights.shape[0]): for j in range(0, weights.shape[1]): kernel = weights[i, j] kernel = normalize(kernel) bnds = ((0, 0.5), (-np.pi, 2 * np.pi), (-4, 4), (-4, 4), (-3, 3)) # params = np.random.random(5) params = [0.5, np.pi / 2, 2, 2, 0] def print_fun(x, f, accepted): print("at minima %.4f accepted %d" % (f, int(accepted))) if version is 'V1': print('Use sklearn version') bnds = ((-0.5, 1.5), (-np.pi, 2 * np.pi), (-4, 4), (-4, 4), (-3, 3), (-5, 5)) # params = np.random.random(5) params = [0.5, np.pi / 2, 2, 2, 0, 3] minimizer_kwargs = { "method": "L-BFGS-B", "bounds": bnds, 'args': (kernel), 'options': { 'maxiter': 200000, 'gtol': 1e-25 } } result = basinhopping( objective_function, params, niter=15, minimizer_kwargs=minimizer_kwargs, callback=print_fun, T=0.00001) else: print('Use Tiagos version') bnds = ((1 / 14, 0.5), (-2 * np.pi, 2 * np.pi), (2, 14), (2, 14), (-2 * np.pi, 2 * np.pi), (-2, 2), (-2, 2), (1e-5, 2)) params = [0.2, 0, 4, 4, 0, 0, 0, 1] minimizer_kwargs = { "method": "L-BFGS-B", "bounds": bnds, 'args': (kernel), 'options': { 'maxiter': 200000, 'gtol': 1e-25 } } result = basinhopping( objective_function_2, params, niter=15, minimizer_kwargs=minimizer_kwargs, callback=print_fun, T=0.00001) # result = minimize(objective_function, params, args=(kernel), # method='L-BFGS-B', bounds=bnds, options={'maxiter': 20000,'gtol': 1e-25, 'dist':True}) # result = minimize(objective_function, params, args=(kernel), # method='BFGS', options={'maxiter': 200000, 'gtol': 1e-25, 'dist': True}) beta_hat = result.x gabor_params[i, j] = np.append(beta_hat, result.fun) print(f'Kernel {i}, filter {j}:') print(beta_hat) np.save(f'{file}.npy', gabor_params) return counter += 1