我无法理解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的文档对我没什么帮助。
答案 0 :(得分:3)
您的过滤器大于输入图像。当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上运行来提高速度。
答案 1 :(得分:1)
像素尺寸需要是Theano中卷积的最后尺寸(因为这些卷积通常用于神经网络,而不是图像)。在theano中,这些特征映射的约定是(batch_size,图像通道,x,y)。而不是
images = mpimg.imread('Lenna.png')
试
images = np.rollaxis(mpimg.imread('Lenna.png'), 2, 0)
将图像转换为3x512x512而不是512x512x3。