def do_distribution_weight_init(weights, config, index, shape, **kwargs):
    # dimension = 0, kernel level, dimension 1 channel level
    # assume weights are untrained weights
    # trained = cornet.cornet_s(pretrained=True, map_location=torch.device('cpu'))
    # for sub in ['module'] + layers[index].split('.'):
    #     trained = trained._modules.get(sub)
    # weights = trained.weight.data.cpu().numpy()
    dim = config['dim'] if 'dim' in config else config[f'dim_{index}']
    components = config[f'comp_{index}'] if f'comp_{index}' in config else 0
    if dim == 0:
        params = weights.reshape(weights.shape[0], weights.shape[1], -1)
        params = params.reshape(params.shape[0], -1)
        samples = shape[0]
    else:
        params = weights.reshape(-1, weights.shape[2], weights.shape[3])
        params = params.reshape(params.shape[0], -1)
        samples = shape[0] * shape[1]

    # np.random.seed(0)
    samples = mixture_gaussian(params, samples, components, f'weight_dim{dim}_{index}')
    idx = 0
    new_weights = np.zeros(shape)
    if dim is 0:
        new_weights = samples.reshape(shape[0], shape[1], shape[2] * shape[3])
        new_weights = new_weights.reshape(shape[0], shape[1], shape[2], shape[3])
    else:
        for i in range(shape[0]):
            for j in range(shape[1]):
                new_weights[i, j] = samples[idx].reshape(shape[2], shape[3])
                idx += 1
    # if weights.shape[1] == 3:
    #     show_kernels(weights, 'distribution_init_channel_color')
    # else:
    #     plot_conv_weights(weights, 'distribution_init_channel')
    return new_weights
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
Exemplo n.º 3
0
def do_distribution_gabor_init_channel(weights, config, index, **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
    if components != 0:
        return components + components * param.shape[1], components * param.shape[1] * param.shape[1]
    best_gmm = mixture_gaussian(param, weights.shape[0], components, f'gabor_{index}', analyze=True)
    return len(best_gmm.weights_.flatten()) + len(best_gmm.means_.flatten()) + len(best_gmm.covariances_.flatten())
Exemplo n.º 4
0
def do_distribution_gabor_init(weights, config, index, **kwargs):
    if index != 0:
        rel = config[f'file_{index}']
        file = path.join(path.dirname(__file__), f'..{rel}')
        params = np.load(file)
    else:
        rel = config[f'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
    if components != 0:
        return components + components * param.shape[1] + components * param.shape[1] * param.shape[1]
    best_gmm = mixture_gaussian(param, weights.shape[0], components, f'gabor_{index}', analyze=True)
    return (best_gmm.weights_.size + best_gmm.means_.size + best_gmm.covariances_.size)
Exemplo n.º 5
0
def do_distribution_weight_init(weights, config, index, **kwargs):
    # dimension = 0, kernel level, dimension 1 channel level
    # assume weights are untrained weights
    trained = cornet.cornet_s(pretrained=True, map_location=torch.device('cpu'))
    for sub in ['module'] + layers[index].split('.'):
        trained = trained._modules.get(sub)
    weights = trained.weight.data.cpu().numpy()
    dim = config['dim'] if 'dim' in config else config[f'dim_{index}']
    components = config[f'comp_{index}'] if f'comp_{index}' in config else 0
    if dim == 0:
        params = weights.reshape(weights.shape[0], weights.shape[1], -1)
        params = params.reshape(params.shape[0], -1)
    else:
        params = weights.reshape(-1, weights.shape[2], weights.shape[3])
        params = params.reshape(params.shape[0], -1)
    if components != 0:
        return components + components * params.shape[1] + components * params.shape[1] * params.shape[1]

    best_gmm = mixture_gaussian(params, params.shape[0], components, f'weight_dim{dim}_{index}', analyze=True)
    return len(best_gmm.weights_.flatten()) + len(best_gmm.means_.flatten()) + len(best_gmm.covariances_.flatten())
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
Exemplo n.º 7
0
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()