提交 03f2e31f authored 作者: Mathieu Germain's avatar Mathieu Germain

Some tests cleanup

上级 ff91168e
...@@ -2,8 +2,9 @@ from __future__ import absolute_import, print_function, division ...@@ -2,8 +2,9 @@ from __future__ import absolute_import, print_function, division
import logging import logging
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
from nose_parameterized import parameterized
import numpy import numpy
from itertools import product from itertools import product, chain
import theano import theano
from six import StringIO from six import StringIO
...@@ -392,6 +393,9 @@ def test_dnn_tag(): ...@@ -392,6 +393,9 @@ def test_dnn_tag():
class TestDnnInferShapes(utt.InferShapeTester): class TestDnnInferShapes(utt.InferShapeTester):
border_modes = ['valid', 'full', 'half']
conv_modes = ['conv', 'cross']
def setUp(self): def setUp(self):
super(TestDnnInferShapes, self).setUp() super(TestDnnInferShapes, self).setUp()
self.mode = mode_with_gpu self.mode = mode_with_gpu
...@@ -426,27 +430,23 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -426,27 +430,23 @@ class TestDnnInferShapes(utt.InferShapeTester):
dnn.GpuDnnSoftmaxGrad dnn.GpuDnnSoftmaxGrad
) )
def _test_conv(self, img, kerns, out, img_val, kern_vals, subsample): def _test_conv(self, img, kerns, out, img_val, kern_vals, border_mode, conv_mode, subsamples):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
img_val = numpy.asarray(img_val, dtype='float32') img_val = numpy.asarray(img_val, dtype='float32')
kern_vals = numpy.asarray(kern_vals, dtype='float32') kern_vals = numpy.asarray(kern_vals, dtype='float32')
for params in product( for subsample in subsamples:
['valid', 'full', 'half'],
subsample,
['conv', 'cross']
):
out_vals = numpy.zeros( out_vals = numpy.zeros(
dnn.GpuDnnConv.get_out_shape(img_val.shape, kern_vals.shape, dnn.GpuDnnConv.get_out_shape(img_val.shape, kern_vals.shape,
border_mode=params[0], border_mode=border_mode,
subsample=params[1]), subsample=subsample),
dtype='float32') dtype='float32')
desc = dnn.GpuDnnConvDesc( desc = dnn.GpuDnnConvDesc(
border_mode=params[0], border_mode=border_mode,
subsample=params[1], subsample=subsample,
conv_mode=params[2] conv_mode=conv_mode
)(kerns.shape) )(kerns.shape)
conv = dnn.GpuDnnConv()(img, kerns, out, desc) conv = dnn.GpuDnnConv()(img, kerns, out, desc)
self._compile_and_check( self._compile_and_check(
...@@ -456,71 +456,99 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -456,71 +456,99 @@ class TestDnnInferShapes(utt.InferShapeTester):
dnn.GpuDnnConv dnn.GpuDnnConv
) )
def test_conv(self): @parameterized.expand(chain(product([SUPPORTED_DNN_CONV_ALGO_FWD[0]],
self._test_conv(T.ftensor4('img'), border_modes,
T.ftensor4('kerns'), conv_modes),
T.ftensor4('out'), product(SUPPORTED_DNN_CONV_ALGO_FWD[1:],
numpy.random.rand(7, 2, 6, 4), [border_modes[0]],
numpy.random.rand(8, 2, 4, 3), [conv_modes[0]])),
[(1, 1), (2, 2)]) testcase_func_name=utt.custom_name_func)
def test_conv(self, algo, border_mode, conv_mode):
def test_conv3d(self): try:
ftensor5 = T.TensorType(dtype="float32", broadcastable=(False,) * 5) default_algo = theano.config.dnn.conv.algo_fwd
self._test_conv(ftensor5('img'), theano.config.dnn.conv.algo_fwd = algo
ftensor5('kerns'),
ftensor5('out'), self._test_conv(T.ftensor4('img'),
numpy.random.rand(10, 2, 6, 4, 11), T.ftensor4('kerns'),
numpy.random.rand(8, 2, 4, 3, 1), T.ftensor4('out'),
[(1, 1, 1), (2, 2, 2)]) numpy.random.rand(7, 2, 8, 4),
numpy.random.rand(8, 2, 4, 3),
def test_conv_gradw(self): border_mode,
conv_mode,
[(1, 1), (2, 2)])
finally:
theano.config.dnn.conv.algo_fwd = default_algo
@parameterized.expand(product(border_modes, conv_modes), utt.custom_name_func)
def test_conv3d_none(self, border_mode, conv_mode):
try:
default_algo = theano.config.dnn.conv.algo_fwd
theano.config.dnn.conv.algo_fwd = 'none'
ftensor5 = T.TensorType(dtype="float32", broadcastable=(False,) * 5)
self._test_conv(ftensor5('img'),
ftensor5('kerns'),
ftensor5('out'),
numpy.random.rand(10, 2, 6, 4, 11),
numpy.random.rand(8, 2, 4, 3, 1),
border_mode,
conv_mode,
[(1, 1, 1), (2, 2, 2)])
finally:
theano.config.dnn.conv.algo_fwd = default_algo
def _test_conv_gradw(self, img, kerns, out, img_val, kern_vals, border_mode, conv_mode, subsample):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg) raise SkipTest(dnn.dnn_available.msg)
img = T.ftensor4('img')
kerns = T.ftensor4('kerns')
out = T.ftensor4('out')
img_val = numpy.asarray( img_val = numpy.asarray(
numpy.random.rand(2, 5, 6, 8), img_val,
dtype='float32' dtype='float32'
) )
kern_vals = numpy.asarray( kern_vals = numpy.asarray(
numpy.random.rand(2, 1, 5, 6), kern_vals,
dtype='float32' dtype='float32'
) )
for params in product( temp_img = img.dimshuffle(1, 0, 2, 3)
['valid', 'full', 'half'], temp_kerns = kerns
[(1, 1)], # strides besides (1, 1) if conv_mode == 'conv':
['conv', 'cross'] temp_kerns = temp_kerns[:, :, ::-1, ::-1]
): temp_kerns = temp_kerns.dimshuffle(1, 0, 2, 3)
temp_img = img.dimshuffle(1, 0, 2, 3) shape = (
temp_kerns = kerns kern_vals.shape[1], img_val.shape[1],
if params[2] == 'conv': img_val.shape[2] - kern_vals.shape[2] + 1,
temp_kerns = temp_kerns[:, :, ::-1, ::-1] img_val.shape[3] - kern_vals.shape[3] + 1
temp_kerns = temp_kerns.dimshuffle(1, 0, 2, 3) )
shape = ( out_vals = numpy.zeros(shape, dtype='float32')
kern_vals.shape[1], img_val.shape[1], desc = dnn.GpuDnnConvDesc(
img_val.shape[2] - kern_vals.shape[2] + 1, border_mode=border_mode,
img_val.shape[3] - kern_vals.shape[3] + 1 subsample=subsample,
) conv_mode=conv_mode
out_vals = numpy.zeros(shape, dtype='float32') )(out.shape)
desc = dnn.GpuDnnConvDesc( conv_grad_w = dnn.GpuDnnConvGradW()(
border_mode=params[0], temp_img,
subsample=params[1], temp_kerns,
conv_mode=params[2] out,
)(out.shape) desc,
conv_grad_w = dnn.GpuDnnConvGradW()( )
temp_img, self._compile_and_check(
temp_kerns, [temp_img, temp_kerns, out],
out, [conv_grad_w],
desc, [img_val, kern_vals, out_vals],
) dnn.GpuDnnConvGradW
self._compile_and_check( )
[temp_img, temp_kerns, out],
[conv_grad_w], @parameterized.expand(product(border_modes, conv_modes), utt.custom_name_func)
[img_val, kern_vals, out_vals], def test_conv_gradw(self, border_mode, conv_mode):
dnn.GpuDnnConvGradW self._test_conv_gradw(T.ftensor4('img'),
) T.ftensor4('kerns'),
T.ftensor4('out'),
numpy.random.rand(2, 5, 6, 8),
numpy.random.rand(2, 1, 5, 6),
border_mode,
conv_mode,
(1, 1))
def test_conv_gradi(self): def test_conv_gradi(self):
if not dnn.dnn_available(test_ctx_name): if not dnn.dnn_available(test_ctx_name):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论