提交 388dba8b authored 作者: Frederic Bastien's avatar Frederic Bastien

added some preleminary test for DownsampleFactorMax op and added a compatibility fct max_pool()

上级 093f823c
...@@ -152,13 +152,13 @@ class DownsampleFactorMax(Op): ...@@ -152,13 +152,13 @@ class DownsampleFactorMax(Op):
@staticmethod @staticmethod
def out_shape(imgshape, ds, ignore_border=False): def out_shape(imgshape, ds, ignore_border=False):
#old code not tested (not evenread) #old code not tested (not evenread)
a, b, c, d = imgshape a, b = imgshape[-2:]
rval = [a, b, c/ds[0], d/ds[1]] rval = list(imgshape[:-2])+[ a/ds[0], b/ds[1]]
if not ignore_border: if not ignore_border:
if c % ds[0]: if a % ds[0]:
rval[2] += 1 rval[-2] += 1
if d % ds[1]: if b % ds[1]:
rval[3] += 1 rval[-1] += 1
return rval return rval
def __init__(self, ds, ignore_border=False): def __init__(self, ds, ignore_border=False):
...@@ -276,3 +276,38 @@ class DownsampleFactorMax(Op): ...@@ -276,3 +276,38 @@ class DownsampleFactorMax(Op):
def c_code_cache_version(self): def c_code_cache_version(self):
return () return ()
def max_pool(images=None, imshp=None, maxpoolshp=None, ignore_border=True):
"""Implements a max pooling layer
Uses the same API as sp.max_pool but uses the Downsample op instead.
Takes as input a 2D tensor of shape batch_size x img_size and performs max pooling.
Max pooling downsamples by taking the max value in a given area, here defined by
maxpoolshp. Outputs a 2D tensor of shape batch_size x output_size.
Parameters are keyword arguments in order to use func_to_mod.
@param images: 2D tensor containing images on which to apply convolution.
Assumed to be of shape batch_size x img_size
@param imgshp: tuple containing image dimensions
@param maxpoolshp: tuple containing shape of area to max pool over
@output out1: symbolic result (2D tensor)
@output out2: logical shape of the output
"""
if len(imshp) == 2:
imshp = (1,) + imshp
elif len(imshp)!=3:
raise NotImplementedError("!")
# all these reshapes should happen in place
imrshp = tensor.stack(images.shape[0],
*[tensor.as_tensor(x) for x in imshp])
imtensor = tensor.reshape(images, imrshp)
maxpop = DownsampleFactorMax(maxpoolshp, ignore_border)
rval = maxpop(imtensor)
return tensor.flatten(rval,2), maxpop.out_shape(imshp, maxpoolshp, ignore_border)
import unittest, sys, time
import numpy as N
import theano.tensor as T
from theano.tests import unittest_tools as utt
from theano.sandbox.downsample import DownsampleFactorMax, max_pool
from theano import function, Mode
class TestDownSample(unittest.TestCase):
def test_maxpool(self):
# generate flatted images
maxpoolshps = ((1,1),(2,2),(3,3),(2,3))
imval = N.random.rand(4,10,64,64)
do_theano=True
images = T.dmatrix()
dmatrix4=T.TensorType('float64', (False, False, False, False))
images4=dmatrix4()
tctot, tpytot, ntot, gtot= [],[],[],[]
for maxpoolshp in maxpoolshps:
print 'maxpoolshp', maxpoolshp
# numeric verification
my_output_val = N.zeros((imval.shape[0], imval.shape[1],
imval.shape[2]/maxpoolshp[0],
imval.shape[3]/maxpoolshp[1]))
time1=time.time()
for n in range(imval.shape[0]):
for k in range(imval.shape[1]):
for i in range(my_output_val.shape[2]):
ii = i*maxpoolshp[0]
for j in range(my_output_val.shape[3]):
jj = j*maxpoolshp[1]
patch = imval[n,k,ii:ii+maxpoolshp[0],jj:jj+maxpoolshp[1]]
my_output_val[n,k,i,j] = N.max(patch)
my_output_val = my_output_val.reshape(imval.shape[0],-1)
ntot+=[time.time()-time1]
# symbolic stuff
if do_theano:
#### wrapper to DownsampleFactorMax op ####
output, outshp = max_pool(images, imval.shape[1:], maxpoolshp)
assert N.prod(my_output_val.shape[1:]) == N.prod(outshp)
print outshp
print my_output_val.shape
assert N.prod(my_output_val.shape[1:]) == N.prod(outshp)
f = function([images,],[output,])
imval2=imval.reshape(imval.shape[0],-1)
output_val = f(imval2)
assert N.all(output_val == my_output_val)
else:
tctot=-1
output_val = my_output_val.copy()
#DownsampleFactorMax op
maxpool_op = DownsampleFactorMax(maxpoolshp, ignore_border=True)(images4)
f = function([images4],maxpool_op,mode=Mode(linker="py"))
f2 = function([images4],maxpool_op,mode=Mode(linker="c"))
time1=time.time()
output_val2 = f(imval)
tpytot+=[time.time()-time1]
assert (N.abs(my_output_val.flatten()-output_val2.flatten())<1e-5).all()
time1=time.time()
output_val2 = f2(imval)
tctot+=[time.time()-time1]
assert (N.abs(my_output_val.flatten()-output_val2.flatten())<1e-5).all()
def mp(input):
output, outshp = max_pool(input, imval.shape[1:], maxpoolshp)
return output
print 'Numpy processing time: %.3fs'%sum(ntot),ntot
print 'c Theano(DownsampleFactorMax) processing time: %.3fs'%sum(tctot),tctot
print 'py Theano(DownsampleFactorMax) processing time: %.3fs'%sum(tpytot),tpytot
d=N.asarray(ntot)/tctot
print 'speed up c theano(DownsampleFactorMax) vs manual: %.3f'%d.mean(),d
d=N.asarray(ntot)/tpytot
print 'speed up py theano(DownsampleFactorMax) vs manual: %.3f'%d.mean(),d
print 'verify_grad time %.3f'%sum(gtot),
def test_maxpool_grad(self):
# generate flatted images
maxpoolshps = ((1,1),(2,2),(3,3),(2,3))
imval = N.random.rand(3,7,10,10) * 10.0 #more variance means numeric gradient will be more accurate
do_theano=True
images = T.dmatrix()
dmatrix4=T.TensorType('float64', (False, False, False, False))
images4=dmatrix4()
for maxpoolshp in maxpoolshps:
print 'maxpoolshp', maxpoolshp
def mp(input):
output, outshp = max_pool(input, imval.shape[1:], maxpoolshp)
return output
print >> sys.stderr, 'max_pool verify_grad requires unusually large tolerance... is it correct?'
utt.verify_grad(mp, [imval.reshape(imval.shape[0],-1)], tol=1e-2)
if __name__ == '__main__':
t = TestSP("test_convolution")
t = TestSP("test_maxpool").run()
# t.test_convolution()
# t.test_multilayer_conv()
#t.test_maxpool()
from theano.tests import main
# main("test_sp")
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论