Python 如何在Theano中的矩阵上按元素执行模具计算?

Python 如何在Theano中的矩阵上按元素执行模具计算?,python,image,numpy,kernel,theano,Python,Image,Numpy,Kernel,Theano,我有下面的模糊内核,我需要应用到RGB图像中的每个像素 [ 0.0625 0.025 0.375 0.025 0.0625 ] 因此,伪代码在Numpy中看起来像这样 for i in range(rows): for j in range(cols): for k in range(3): final[i][j][k] = image[i-2][j][k]*0.0625 + \ im

我有下面的模糊内核,我需要应用到RGB图像中的每个像素

[ 0.0625 0.025 0.375 0.025 0.0625 ]  
因此,伪代码在Numpy中看起来像这样

for i in range(rows):
    for j in range(cols):
        for k in range(3):
            final[i][j][k] = image[i-2][j][k]*0.0625 + \
                             image[i-1][j][k]*0.25 + \
                             image[i][j][k]*0.375 + \
                             image[i+1][j][k]*0.25 + \
                             image[i+2][j][k]*0.0625
我曾尝试搜索类似的问题,但在计算中从未找到此类数据访问。

如何对无张量矩阵执行上述函数?

您可以使用
Conv2D
函数执行此任务。请参阅参考资料,您也可以阅读示例教程。此解决方案的注意事项:

  • 因为内核是对称的,所以可以忽略
    filter\u flip
    参数
  • Conv2D使用4D输入和内核形状作为参数,因此您需要首先对其进行整形
  • Conv2D求和每个通道(我认为在你的例子中'k'变量是RGB的,对吗?它被称为通道),所以你应该先把它分开
这是我的示例代码,我在这里使用更简单的内核:

import numpy as np
import theano
import theano.tensor as T
from theano.tensor.nnet import conv2d

# original image
img = [[[1, 2, 3, 4], #R channel
       [1, 1, 1, 1],  #
       [2, 2, 2, 2]], #

      [[1, 1, 1, 1],  #G channel
       [2, 2, 2, 2],  #
       [1, 2, 3, 4]], #

      [[1, 1, 1, 1],  #B channel
       [1, 2, 3, 4],  #
       [2, 2, 2, 2],]]#

# separate and reshape each channel to 4D 
R = np.asarray([[img[0]]], dtype='float32')
G = np.asarray([[img[1]]], dtype='float32')
B = np.asarray([[img[2]]], dtype='float32')       

# 4D kernel from the original : [1,0,1] 
kernel = np.asarray([[[[1],[0],[1]]]], dtype='float32')

# theano convolution
t_img = T.ftensor4("t_img")
t_kernel = T.ftensor4("t_kernel")
result = conv2d(
            input = t_img,
            filters=t_kernel,
            filter_shape=(1,1,1,3),
            border_mode = 'half')
f = theano.function([t_img,t_kernel],result)

# compute each channel
R = f(R,kernel)
G = f(G,kernel)
B = f(B,kernel)

# reshape again
img = np.asarray([R,G,B])
img = np.reshape(img,(3,3,4))
print img

如果您有任何关于代码的讨论,请发表评论。希望有帮助。

嘿,我运行了上面的代码,得到了以下错误-
ValueError:新数组的总大小必须保持不变
另一个错误是covolution函数中的'half'模式未实现-
异常:模式half未实现
-Numpy和Scipy已安装了最新的更新,但是错误仍然存在。对于第二个错误,请尝试更新您的theano我的版本是0.8,我将检查第一个错误。我注意到您使用了nnet的conv2d函数,该函数采用4D张量。为什么不改用
theano.tensor.signal.conv.conv2d
函数,它接受2D矩阵和3D张量()看起来像
信号。conv.conv2d
不实现“半”模式。无论如何,我现在注意到你的R,G,B值实际上只是图像的前三行,而不是通道。为此,在进行任何进一步处理之前,必须先执行
np.rollaxis(img,2)
以将颜色维度移到外部。我在我的图像上这样做了,但是我仍然得到一个错误的输出。