gpt4 book ai didi

python - 如何使用 theano 应用高斯模糊

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:06 26 4
gpt4 key购买 nike

我无法理解 theano conv2D 函数我想用高斯内核模糊一组图像:

sigma = 10
size = int(6*sigma+1) if int(6*sigma+1)%2 else int(6*sigma) #61 when sigma = 10
k = int(size/2)
images = mpimg.imread('Lenna.png') #512x512x3

#Use openCV to compare with a working implementation
blurred_lena = cv2.GaussianBlur(images, (size,size), sigma)
plt.imshow(blurred_lena)
plt.show()

#Use theano
images = images.transpose(2,0,1)
x = np.arange(-k,k+1)
y = np.arange(-k,k+1)
X,Y=np.meshgrid(x,y)
M = T.ftensor3()
Gv=np.exp(-(X**2+Y**2)/(2*sigma**2))
Gv=(Gv/np.sum(Gv.reshape(-1))).astype(np.float32)
G_kernel = theano.shared(Gv)

R2 = conv2d(M,G_kernel)
conv = theano.function(
inputs=[M],
outputs=R2,
)

res = conv(images)
res = res.transpose(1,2,0)
plt.figure(2)
plt.imshow(res)
plt.show()

上面的代码不起作用,它给出了错误:

Error allocating 595360000 bytes of device memory (out of memory). Driver report 394424320 bytes free and 1072889856 bytes total 
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/joao/Dropbox/physological/Juggler/SaliencyExp/conv.py", line 50, in <module>
res = conv(images)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 610, in __call__
storage_map=self.fn.storage_map)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 599, in __call__
outputs = self.fn()
RuntimeError: GpuCorrMM failed to allocate working memory of 625 x 238144

Apply node that caused the error: GpuCorrMM{valid, (1, 1)}(GpuContiguous.0, GpuContiguous.0)
Inputs types: [CudaNdarrayType(float32, (False, True, False, False)), CudaNdarrayType(float32, (True, True, False, False))]
Inputs shapes: [(3, 1, 512, 512), (1, 1, 25, 25)]
Inputs strides: [(262144, 0, 512, 1), (0, 0, 25, 1)]
Inputs values: ['not shown', 'not shown']

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

无论哪种方式,因为高斯滤波器是可分离的,我宁愿应用两个线性滤波器,一个在图像的 x 中,一个在 y 中。但我不明白如何解决这个问题以及 conv2d 的文档对我帮助不大。

最佳答案

您的过滤器比您的输入图像大。当 border_mode='valid' 时,conv2d 无法处理这种情况,这是默认的边框模式。

为什么?因为图像的有效区域大小为 1 x 1 x 37 x -2。负尺寸没有意义。

解决方案:使用R2 = conv2d(M,G_kernel,border_mode='full')

至于问题的第二部分,您可以使用 R2 = conv2d(conv2d(M,G_x_kernel,border_mode='full'),G_y_kernel,border_mode='full')但是,我不确定使用这些小过滤器并在 GPU 上运行是否会提高速度。

关于python - 如何使用 theano 应用高斯模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30501043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com