如何在 Theano 中的矩阵上按元素执行模板计算?
How to perform stencil computations element-wise on a matrix in 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 + \
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
我曾尝试搜索与此类似的问题,但从未在计算中找到此类数据访问。
如何对 Theano 张量矩阵执行上述功能?
您可以使用 Conv2D
功能完成此任务。请参阅参考资料 here and may be you also can read the example tutorial here。此解决方案的注释:
- 因为你的内核是对称的,可以忽略
filter_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
如果你对代码有什么想讨论的,请评论。希望对你有帮助。
我需要将以下模糊内核应用于 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
我曾尝试搜索与此类似的问题,但从未在计算中找到此类数据访问。
如何对 Theano 张量矩阵执行上述功能?
您可以使用 Conv2D
功能完成此任务。请参阅参考资料 here and may be you also can read the example tutorial here。此解决方案的注释:
- 因为你的内核是对称的,可以忽略
filter_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
如果你对代码有什么想讨论的,请评论。希望对你有帮助。