<s> import theano <EOL> from theano import tensor <EOL> import numpy <EOL> from pylearn2 . linear . conv2d import Conv2D , make_random_conv2D <EOL> from pylearn2 . space import Conv2DSpace <EOL> from pylearn2 . utils import sharedX <EOL> import unittest <EOL> try : <EOL> scipy_available = True <EOL> import scipy . ndimage <EOL> except ImportError : <EOL> scipy_available = False <EOL> class TestConv2D ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . image = numpy . random . rand ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:1> ) . astype ( theano . config . floatX ) <EOL> self . image_tensor = tensor . tensor4 ( ) <EOL> self . input_space = Conv2DSpace ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , <NUM_LIT:1> ) <EOL> self . filters_values = numpy . ones ( <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> ) , dtype = theano . config . floatX <EOL> ) <EOL> self . filters = sharedX ( self . filters_values , name = '<STR_LIT>' ) <EOL> self . conv2d = Conv2D ( self . filters , <NUM_LIT:1> , self . input_space ) <EOL> def test_value_errors ( self ) : <EOL> """<STR_LIT>""" <EOL> bad_filters = sharedX ( numpy . zeros ( ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:2> ) ) ) <EOL> self . assertRaises ( ValueError , Conv2D , bad_filters , <NUM_LIT:1> , self . input_space ) <EOL> self . assertRaises ( AssertionError , Conv2D , self . filters , <NUM_LIT:0> , <EOL> self . input_space ) <EOL> def test_get_params ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . conv2d . get_params ( ) == [ self . filters ] <EOL> def test_lmul ( self ) : <EOL> """<STR_LIT>""" <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> self . conv2d . lmul ( self . image_tensor ) ) <EOL> if scipy_available : <EOL> numpy . allclose ( <EOL> f ( self . image ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) , <EOL> scipy . ndimage . filters . convolve ( <EOL> self . image . reshape ( ( <NUM_LIT:3> , <NUM_LIT:3> ) ) , <EOL> self . filters_values . reshape ( ( <NUM_LIT:2> , <NUM_LIT:2> ) ) <EOL> ) [ : <NUM_LIT:2> , : <NUM_LIT:2> ] <EOL> ) <EOL> def test_lmul_T ( self ) : <EOL> """<STR_LIT>""" <EOL> conv2d = self . conv2d . lmul ( self . image_tensor ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> self . conv2d . lmul_T ( conv2d ) ) <EOL> assert f ( self . image ) . shape == self . image . shape <EOL> def test_lmul_sq_T ( self ) : <EOL> """<STR_LIT>""" <EOL> conv2d_sq = Conv2D ( sharedX ( numpy . square ( self . filters_values ) ) , <EOL> <NUM_LIT:1> , self . input_space <EOL> ) . lmul ( self . image_tensor ) <EOL> conv2d = self . conv2d . lmul ( self . image_tensor ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> self . conv2d . lmul_T ( conv2d_sq ) ) <EOL> f2 = theano . function ( [ self . image_tensor ] , <EOL> self . conv2d . lmul_sq_T ( conv2d ) ) <EOL> numpy . testing . assert_allclose ( f ( self . image ) , f2 ( self . image ) ) <EOL> def test_set_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> cur_img_shape = self . conv2d . _img_shape <EOL> cur_batch_size = self . conv2d . _img_shape [ <NUM_LIT:0> ] <EOL> self . conv2d . set_batch_size ( cur_batch_size + <NUM_LIT:10> ) <EOL> assert self . conv2d . _img_shape [ <NUM_LIT:0> ] == cur_batch_size + <NUM_LIT:10> <EOL> assert self . conv2d . _img_shape [ <NUM_LIT:1> : ] == cur_img_shape [ <NUM_LIT:1> : ] <EOL> def test_axes ( self ) : <EOL> """<STR_LIT>""" <EOL> default_axes = ( '<STR_LIT:b>' , <NUM_LIT:0> , <NUM_LIT:1> , '<STR_LIT:c>' ) <EOL> axes = ( <NUM_LIT:0> , '<STR_LIT:b>' , <NUM_LIT:1> , '<STR_LIT:c>' ) <EOL> mapping = tuple ( axes . index ( axis ) for axis in default_axes ) <EOL> input_space = Conv2DSpace ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , num_channels = <NUM_LIT:1> , axes = axes ) <EOL> conv2d = Conv2D ( self . filters , <NUM_LIT:1> , input_space , output_axes = axes ) <EOL> f_axes = theano . function ( [ self . image_tensor ] , <EOL> conv2d . lmul ( self . image_tensor ) ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> self . conv2d . lmul ( self . image_tensor ) ) <EOL> output_axes = f_axes ( numpy . transpose ( self . image , mapping ) ) <EOL> output = f ( self . image ) <EOL> output_axes = numpy . transpose ( output_axes , mapping ) <EOL> numpy . testing . assert_allclose ( output , output_axes ) <EOL> assert output . shape == output_axes . shape <EOL> def test_channels ( self ) : <EOL> """<STR_LIT>""" <EOL> input_space = Conv2DSpace ( ( <NUM_LIT:3> , <NUM_LIT:3> ) , num_channels = <NUM_LIT:3> ) <EOL> filters_values = numpy . ones ( <EOL> ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:2> ) , dtype = theano . config . floatX <EOL> ) <EOL> filters = sharedX ( filters_values ) <EOL> image = numpy . random . rand ( <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:3> ) . astype ( theano . config . floatX ) <EOL> conv2d = Conv2D ( filters , <NUM_LIT:1> , input_space ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> conv2d . lmul ( self . image_tensor ) ) <EOL> assert f ( image ) . shape == ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> def test_make_random_conv2D ( self ) : <EOL> """<STR_LIT>""" <EOL> output_space = Conv2DSpace ( ( <NUM_LIT:2> , <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> conv2d = make_random_conv2D ( <NUM_LIT:1> , self . input_space , output_space , <EOL> ( <NUM_LIT:2> , <NUM_LIT:2> ) , <NUM_LIT:1> ) <EOL> f = theano . function ( [ self . image_tensor ] , <EOL> conv2d . lmul ( self . image_tensor ) ) <EOL> assert f ( self . image ) . shape == ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:1> ) <EOL> assert conv2d . input_space == self . input_space <EOL> assert conv2d . output_axes == output_space . axes </s>
