如果输入图像和输出图像已知,Python找到卷积核

标签 python numpy tensorflow convolution

我在 python 中遇到卷积核问题。这是关于简单的卷积运算符。我有输入矩阵和输出矩阵。我想找到一个可能的尺寸为(5x5)的卷积核。如何用python、numpy或tensorflow解决这个问题?

import scipy.signal as ss

input_img = np.array([[94, 166, 76, 106, 152, 232],
                      [48, 242, 30, 98, 46, 210],
                      [52, 60, 86, 60, 216, 248],
                      [52, 236, 116, 240, 224, 184],
                      [138, 160, 146, 254, 236, 252],
                      [94, 100, 224, 246, 152, 74]], dtype=float)

output_img = np.array([[15, 49, 23, 105, 0, 0],
                       [43,30, 108, 124, 0, 0],
                       [58, 120, 112, 92, 0, 0],
                       [73, 127, 118, 126, 0, 0],
                       [112, 123, 76, 37, 0, 0],
                       [0, 0, 0, 0, 0, 0]], dtype=float)

# I want to find this kernel
conv = np.zeros((5,5), dtype=int)

# So if I do convolution operator, output_img will resulting a value same as I defined above
output_img = ss.convolve2d(input_img, conv, padding='same')

最佳答案

据我了解,您需要通过给定的输入、输出数组和窗口大小来重建窗口权重。我认为这是可能的,特别是如果输入数组(图像)足够大。

看下面的代码:

import scipy.signal as ss
import numpy as np

source_dataset = np.random.rand(20, 10)
sample_convolution = np.diag([1, 1, 1])
output_dataset = ss.convolve2d(data, sample_convolution, mode='same')
conv_size = c.shape[0]

# Given output_dataset, source_datset, and conv_size we need to reconstruct
# window weights.

def reconstruct(data, output, csize):
    half_size = int(csize / 2)
    min_row_ind = half_size
    max_row_ind = int(data.shape[0]) - half_size
    min_col_ind = half_size
    max_col_ind = int(data.shape[1]) - half_size
    A = list()
    b = list()
    for i in np.arange(min_row_ind, max_row_ind, dtype=int):
        for j in np.arange(min_col_ind, max_col_ind, dtype=int):
            A.append(data[(i - half_size):(i + half_size + 1), (j - half_size):(j + half_size + 1)].ravel().tolist())
            b.append(output[i, j])
            if len(A) == csize * csize and np.linalg.matrix_rank(A) == csize * csize:
                return (np.linalg.pinv(A)@np.array(b)[:, np.newaxis]).reshape(csize, csize)
    if len(A) < csize*csize:
        raise Exception("Insufficient data")

result = reconstruct(source_dataset, output_dataset, 3)

我得到了以下结果

array([[ 1.00000000e+00, -1.77635684e-15, -1.11022302e-16],
       [ 0.00000000e+00,  1.00000000e+00, -8.88178420e-16],
       [ 0.00000000e+00, -1.22124533e-15,  1.00000000e+00]])

所以,它按预期工作;但肯定需要改进以考虑边缘效应、窗口大小均匀的情况等。

关于如果输入图像和输出图像已知,Python找到卷积核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55560333/

相关文章:

python - df.iloc() 的赋值返回 nan

python - 在 numpy 数组中查找最后一个值

python - 使用 %s 格式化输入/输出

python - 如何矢量化这个 python 代码?

python - 神经网络没有经过训练,交叉熵保持不变

tensorflow - 将 keras 模型另存为 .h5

缓存模块时找不到 Tensorflow Hub 目录

python - 使用log in numpy求解贷款偿还公式

python - 阴谋冲刺 : filter DataTable based on plot selection

python - libhdf5_serial.so.100 上的 h5py 导入错误