示例#1
0
def test_max_pool():
    """
    Test max pooling for known result.
    """
    X_sym = tensor.tensor4('X')
    pool_it = max_pool(X_sym, pool_shape=(2, 2), pool_stride=(2, 2),
                       image_shape=(6, 4))

    f = theano.function(inputs=[X_sym], outputs=pool_it)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 7],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 12, 12]],
                 dtype=theano.config.floatX)[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [6, 8],
                         [10, 12]],
                        dtype=theano.config.floatX)[np.newaxis,
                                                    np.newaxis,
                                                    ...]

    actual = f(X)
    assert np.allclose(expected, actual)
示例#2
0
    def __init__(self, input, input_shape=None):
        if isinstance(input, Layer):
            self.input = input.output
            if input_shape == None:
                input_shape = input.output_shape
        else:
            self.input = input
        self.input_shape = input_shape

        ##Only square image allowed
        #assert input_shape[2]==input_shape[3]

        #Extend one pixel at each direction
        shapeext = input_shape[0], input_shape[
            1], input_shape[2] + 1, input_shape[3] + 1
        inputext = T.alloc(dtypeX(-INF), *shapeext)

        inputext = T.set_subtensor(
            inputext[:, :, 1:input_shape[2] + 1, 1:input_shape[3] + 1],
            self.input)

        self.output = max_pool(inputext, (3, 3), (2, 2), shapeext[2:])
        self.output_shape = input_shape[0], input_shape[1], (
            input_shape[2] + 1) / 2, (input_shape[3] + 1) / 2
        print self.output_shape
示例#3
0
def test_max_pool():
    """
    Test max pooling for known result.
    """
    X_sym = tensor.tensor4('X')
    pool_it = max_pool(X_sym, pool_shape=(2, 2), pool_stride=(2, 2),
                       image_shape=(6, 4))

    f = theano.function(inputs=[X_sym], outputs=pool_it)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 7],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 12, 12]],
                 dtype=theano.config.floatX)[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [6, 8],
                         [10, 12]],
                        dtype=theano.config.floatX)[np.newaxis,
                                                    np.newaxis,
                                                    ...]

    actual = f(X)
    assert np.allclose(expected, actual)
示例#4
0
    def fprop(self, state_below):

        #self.input_space.validate(state_below)

        z = self.transformer.lmul(state_below)
        if not hasattr(self, 'tied_b'):
            self.tied_b = False

        #assert self.tied_b
        if self.tied_b:
            b = self.b.dimshuffle('x', 0, 'x', 'x')
        else:
            b = self.b.dimshuffle('x', 0, 1, 2)

        z = z + b
        d = self.nonlin.apply(z)

        if self.layer_name is not None:
            d.name = self.layer_name + '_z'
            self.detector_space.validate(d)

        if self.pool_type is not None:
            # Format the input to be supported by max pooling


            if not hasattr(self, 'detector_normalization'):
                self.detector_normalization = None

            if self.detector_normalization:
                d = self.detector_normalization(d)

            assert self.pool_type in ['max', 'mean'], ("pool_type should be"
                                                      "either max or mean"
                                                      "pooling.")

            if self.pool_type == 'max':
                #p = downsample.max_pool_2d(d, self.pool_shape,
                #                           ignore_border=False)

                p = max_pool(bc01=d, pool_shape=self.pool_shape,
                             pool_stride=self.pool_stride,
                             image_shape=self.detector_space.shape)
            elif self.pool_type == 'mean':
                p = mean_pool(bc01=d, pool_shape=self.pool_shape,
                        pool_stride=self.pool_stride,
                        image_shape=self.detector_space.shape)

                #self.output_space.validate(p)
        else:
            p = d

        if not hasattr(self, 'output_normalization'):
           self.output_normalization = None

        if self.output_normalization:
           p = self.output_normalization(p)

        return p
    def __init__(self, input, input_shape=None, feedval=0.0):
        if isinstance(input, Layer):
            self.input = input.output
            if input_shape == None:
                input_shape = input.output_shape
        else:
            self.input = input
        self.input_shape = input_shape

        #Only square image allowed
        assert input_shape[2] == input_shape[3]

        #Extend one pixel at each direction
        shapeext = input_shape[0], input_shape[
            1], input_shape[2] + 2, input_shape[3] + 2
        inputext = T.alloc(dtypeX(-INF), *shapeext)

        inputext = T.set_subtensor(
            inputext[:, :, 1:input_shape[2] + 1, 1:input_shape[3] + 1],
            self.input)

        output_cmb = max_pool(inputext, (3, 3), (1, 1), shapeext[2:])
        self.output_cmb = output_cmb
        #Separate output to 4 channels
        c00 = output_cmb[:, :, ::2, ::2]
        c01 = output_cmb[:, :, ::2, 1::2]
        c10 = output_cmb[:, :, 1::2, ::2]
        c11 = output_cmb[:, :, 1::2, 1::2]
        self.one_channel = input_shape[0], input_shape[1], (
            input_shape[2] + 1) / 2, (input_shape[3] + 1) / 2

        #Combine, 2 conditions: even/odd
        if (input_shape[2] & 1) == 0:
            joined = T.concatenate([c00, c01, c10, c11], axis=0)
        else:
            joined = T.alloc(dtypeX(feedval),
                             *((input_shape[0] * 4, ) + self.one_channel[1:4]))
            joined = T.set_subtensor(joined[0:self.one_channel[0], :, :, :],
                                     c00)
            joined = T.set_subtensor(
                joined[self.one_channel[0]:self.one_channel[0] * 2, :, :, :-1],
                c01)
            joined = T.set_subtensor(
                joined[self.one_channel[0] * 2:self.one_channel[0] *
                       3, :, :-1, :], c10)
            joined = T.set_subtensor(
                joined[self.one_channel[0] * 3:self.one_channel[0] *
                       4, :, :-1, :-1], c11)

        self.output = joined
        self.output_shape = input_shape[0] * 4, self.one_channel[
            1], self.one_channel[2], self.one_channel[3]
示例#6
0
def test_pooling_with_anon_variable():
    """
    Ensure that pooling works with anonymous
    variables.
    """
    X_sym = tensor.ftensor4()
    shp = (3, 3)
    strd = (1, 1)
    im_shp = (6, 6)
    pool_0 = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                      image_shape=im_shp, try_dnn=False)
    pool_1 = mean_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp)
示例#7
0
    def initialize_output_space(self):
        """
        Initializes the output space of the ConvElemwise layer by taking
        pooling operator and the hyperparameters of the convolutional layer
        into consideration as well.
        """
        dummy_batch_size = self.mlp.batch_size

        if dummy_batch_size is None:
            dummy_batch_size = 2
        dummy_detector =\
                sharedX(self.detector_space.get_origin_batch(dummy_batch_size))

        # Redefine num channels of the outut space
        if isinstance(self.nonlin, MaxoutBC01):
            num_channels = self.output_channels / self.nonlin.num_pieces
        else:
            num_channels = self.output_channels

        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = max_pool(bc01=dummy_detector,
                                   pool_shape=self.pool_shape,
                                   pool_stride=self.pool_stride,
                                   image_shape=self.detector_space.shape)
            elif self.pool_type == 'mean':
                dummy_p = mean_pool(bc01=dummy_detector,
                                    pool_shape=self.pool_shape,
                                    pool_stride=self.pool_stride,
                                    image_shape=self.detector_space.shape)
            dummy_p = dummy_p.eval()
            print dummy_p.shape
            self.output_space = Conv2DSpace(shape=[dummy_p.shape[2],
                                                   dummy_p.shape[3]],
                                            num_channels=num_channels,
                                            axes=('b', 'c', 0, 1))
        else:
            dummy_detector = dummy_detector.eval()
            print dummy_detector.shape
            self.output_space = Conv2DSpace(shape=[dummy_detector.shape[2],
                                            dummy_detector.shape[3]],
                                            num_channels=num_channels,
                                            axes=('b', 'c', 0, 1))


	print "Input:", self.layer_name, self.input_space.shape, self.input_space.num_channels
	print "Detector:", self.layer_name, self.detector_space.shape, self.detector_space.num_channels
        print "Output:", self.layer_name, self.output_space.shape, self.output_space.num_channels
    def fprop(self, state_below):

        self.input_space.validate(state_below)

        z = self.transformer.lmul(state_below) + self.b
        if self.layer_name is not None:
            z.name = self.layer_name + '_z'

        if self.activation_function is None:
            d = z
        elif self.activation_function == 'tanh':
            d = T.tanh(z)
        elif self.activation_function == 'sigmoid':
            d = T.nnet.sigmoid(z)
        elif self.activation_function == 'softmax':
            d = T.nnet.softmax(z)
        else:
            raise NotImplementedError()

        self.detector_space.validate(d)

        if not hasattr(self, 'detector_normalization'):
            self.detector_normalization = None

        if self.detector_normalization:
            d = self.detector_normalization(d)

        assert self.pool_type in ['max', 'mean']
        if self.pool_type == 'max':
            p = max_pool(bc01=d,
                         pool_shape=self.pool_shape,
                         pool_stride=self.pool_stride,
                         image_shape=self.detector_space.shape)
        elif self.pool_type == 'mean':
            p = mean_pool(bc01=d,
                          pool_shape=self.pool_shape,
                          pool_stride=self.pool_stride,
                          image_shape=self.detector_space.shape)

        self.output_space.validate(p)

        if not hasattr(self, 'output_normalization'):
            self.output_normalization = None

        if self.output_normalization:
            p = self.output_normalization(p)

        return p
示例#9
0
    def initialize_output_space(self):
        """
        Initializes the output space of the ConvElemwise layer by taking
        pooling operator and the hyperparameters of the convolutional layer
        into consideration as well.
        """
        dummy_batch_size = self.mlp.batch_size

        if dummy_batch_size is None:
            dummy_batch_size = 2
        dummy_detector =\
                sharedX(self.detector_space.get_origin_batch(dummy_batch_size))

        # Redefine num channels of the outut space
        if isinstance(self.nonlin, MaxoutBC01):
            num_channels = self.output_channels / self.nonlin.num_pieces
        else:
            num_channels = self.output_channels

        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = max_pool(bc01=dummy_detector,
                                   pool_shape=self.pool_shape,
                                   pool_stride=self.pool_stride,
                                   image_shape=self.detector_space.shape)
            elif self.pool_type == 'mean':
                dummy_p = mean_pool(bc01=dummy_detector,
                                    pool_shape=self.pool_shape,
                                    pool_stride=self.pool_stride,
                                    image_shape=self.detector_space.shape)
            dummy_p = dummy_p.eval()
            print dummy_p.shape
            self.output_space = Conv2DSpace(
                shape=[dummy_p.shape[2], dummy_p.shape[3]],
                num_channels=num_channels,
                axes=('b', 'c', 0, 1))
        else:
            dummy_detector = dummy_detector.eval()
            print dummy_detector.shape
            self.output_space = Conv2DSpace(
                shape=[dummy_detector.shape[2], dummy_detector.shape[3]],
                num_channels=num_channels,
                axes=('b', 'c', 0, 1))

        print "Input:", self.layer_name, self.input_space.shape, self.input_space.num_channels
        print "Detector:", self.layer_name, self.detector_space.shape, self.detector_space.num_channels
        print "Output:", self.layer_name, self.output_space.shape, self.output_space.num_channels
    def __init__(self, input, input_shape = None, feedval = 0.0):
        if isinstance(input, Layer):
            self.input = input.output
            if input_shape == None:
                input_shape = input.output_shape
        else:
            self.input = input
        self.input_shape = input_shape

        ##Only square image allowed
        #assert input_shape[2]==input_shape[3]

        #Extend one pixel at each direction
        shapeext = input_shape[0], input_shape[1], input_shape[2]+2, input_shape[3]+2
        inputext = T.alloc(dtypeX(-INF), *shapeext)

        inputext = T.set_subtensor(inputext[:,:,1:input_shape[2]+1,1:input_shape[3]+1], self.input)
        
        output_cmb = max_pool(inputext, (3,3), (1,1), shapeext[2:])
        self.output_cmb = output_cmb
        #Separate output to 4 channels
        c00 = output_cmb[:,:,::2,::2]
        c01 = output_cmb[:,:,::2,1::2]
        c10 = output_cmb[:,:,1::2,::2]
        c11 = output_cmb[:,:,1::2,1::2]
        self.one_channel = input_shape[0], input_shape[1], (input_shape[2]+1)/2, (input_shape[3]+1)/2

        #Combine, 2 conditions: even/odd
        odd2 = (input_shape[2]&1)==1
        odd3 = (input_shape[3]&1)==1
        joined = T.alloc(dtypeX(feedval), *((input_shape[0]*4,)+self.one_channel[1:4]))
        joined = T.set_subtensor(joined[0:self.one_channel[0],:,:,:], c00)
        joined = T.set_subtensor(joined[self.one_channel[0]:self.one_channel[0]*2,:,:,:-1] if odd3 else joined[self.one_channel[0]:self.one_channel[0]*2], c01)
        joined = T.set_subtensor(joined[self.one_channel[0]*2:self.one_channel[0]*3,:,:-1,:] if odd2 else joined[self.one_channel[0]*2:self.one_channel[0]*3], c10)
        if odd2:
            if odd3:
                joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:-1,:-1], c11)
            else:
                joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:-1,:], c11)
        else:
            if odd3:
                joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:,:-1], c11)
            else:
                joined = T.set_subtensor(joined[self.one_channel[0]*3:self.one_channel[0]*4,:,:,:], c11)

        self.output = joined
        self.output_shape = input_shape[0]*4, self.one_channel[1], self.one_channel[2], self.one_channel[3]
    def fprop(self, state_below):

        self.input_space.validate(state_below)

        z = self.transformer.lmul(state_below) + self.b
        if self.layer_name is not None:
            z.name = self.layer_name + '_z'

        if self.activation_function is None:
            d = z
        elif self.activation_function == 'tanh':
            d = T.tanh(z)
        elif self.activation_function == 'sigmoid':
            d = T.nnet.sigmoid(z)
        elif self.activation_function == 'softmax':
            d = T.nnet.softmax(z)
        else:
            raise NotImplementedError()

        self.detector_space.validate(d)

        if not hasattr(self, 'detector_normalization'):
            self.detector_normalization = None

        if self.detector_normalization:
            d = self.detector_normalization(d)

        assert self.pool_type in ['max', 'mean']
        if self.pool_type == 'max':
            p = max_pool(bc01=d, pool_shape=self.pool_shape,
                    pool_stride=self.pool_stride,
                    image_shape=self.detector_space.shape)
        elif self.pool_type == 'mean':
            p = mean_pool(bc01=d, pool_shape=self.pool_shape,
                    pool_stride=self.pool_stride,
                    image_shape=self.detector_space.shape)

        self.output_space.validate(p)

        if not hasattr(self, 'output_normalization'):
            self.output_normalization = None

        if self.output_normalization:
            p = self.output_normalization(p)

        return p
示例#12
0
def test_pooling_with_anon_variable():
    """
    Ensure that pooling works with anonymous
    variables.
    """
    X_sym = tensor.ftensor4()
    shp = (3, 3)
    strd = (1, 1)
    im_shp = (6, 6)
    pool_0 = max_pool(X_sym,
                      pool_shape=shp,
                      pool_stride=strd,
                      image_shape=im_shp,
                      try_dnn=False)
    pool_1 = mean_pool(X_sym,
                       pool_shape=shp,
                       pool_stride=strd,
                       image_shape=im_shp)
示例#13
0
def test_pool2d_pylearn2():
    '''
    Tests that pool2d's 'pylearn2' padding mode indeed creates the same results
    as pylearn2's pooling operator.
    '''
    if not pylearn2_installed:
        return

    floatX = theano.config.floatX

    batch_size = 2
    num_channels = 2  # num. of conv filters
    image_shape = (2, 3)  # size of conv+bias output
    input_node = InputNode(DenseFormat(axes=('b', 'c', '0', '1'),
                                       shape=(-1, num_channels) + image_shape,
                                       dtype=floatX))

    pool_shape = (2, 2)
    pool_strides = (2, 2)
    pl_pooled_symbol = mlp.max_pool(bc01=input_node.output_symbol,
                                    pool_shape=pool_shape,
                                    pool_stride=pool_strides,
                                    image_shape=image_shape)

    pl_pool_func = theano.function([input_node.output_symbol],
                                   pl_pooled_symbol)

    sl_pool_node = Pool2D(input_node=input_node, window_shape=pool_shape,
                          strides=pool_strides, mode='max',
                          pad='pylearn2')

    sl_pool_func = theano.function([input_node.output_symbol],
                                   sl_pool_node.output_symbol)

    input_batch = numpy.arange(batch_size *
                               numpy.prod(input_node.output_format.shape[1:]))
    input_batch = numpy.cast[floatX](input_batch)
    input_batch = input_batch.reshape(input_node.output_format.shape)

    pl_pooled_batch = pl_pool_func(input_batch)
    sl_pooled_batch = sl_pool_func(input_batch)
    assert_array_equal(sl_pooled_batch, pl_pooled_batch)
    def __init__(self,input,input_shape = None):
        if isinstance(input, Layer):
            self.input = input.output
            if input_shape == None:
                input_shape = input.output_shape
        else:
            self.input = input
        self.input_shape = input_shape

        ##Only square image allowed
        #assert input_shape[2]==input_shape[3]

        #Extend one pixel at each direction
        shapeext = input_shape[0], input_shape[1], input_shape[2]+1, input_shape[3]+1
        inputext = T.alloc(dtypeX(-INF), *shapeext)

        inputext = T.set_subtensor(inputext[:,:,1:input_shape[2]+1,1:input_shape[3]+1], self.input)
        
        self.output = max_pool(inputext, (3,3), (2,2), shapeext[2:])
        self.output_shape = input_shape[0], input_shape[1], (input_shape[2]+1)/2, (input_shape[3]+1)/2
        print self.output_shape
示例#15
0
def test_max_pool_options():
    """
    Compare gpu max pooling methods with various shapes
    and strides.
    """
    if not cuda.cuda_available:
        raise SkipTest('Optional package cuda disabled.')
    if not dnn_available():
        raise SkipTest('Optional package cuDNN disabled.')

    mode = copy.copy(theano.compile.get_default_mode())
    mode.check_isfinite = False

    X_sym = tensor.ftensor4('X')
    # Case 1: shape > stride
    shp = (3, 3)
    strd = (2, 2)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op
    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[7, 8],
                         [11, 12],
                         [14, 15]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 2: shape < stride
    shp = (2, 2)
    strd = (3, 3)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [10, 12]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 3: shape == stride
    shp = (2, 2)
    strd = (2, 2)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [6, 8],
                         [10, 15]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 4: row shape < row stride
    shp = (2, 2)
    strd = (3, 2)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [10, 12]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 5: col shape < col stride
    shp = (2, 2)
    strd = (2, 3)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [6, 8],
                         [10, 15]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)
    def set_input_space(self, space):
        """ Note: this resets parameters! """

        self.input_space = space
        rng = self.mlp.rng

        if self.border_mode == 'valid':
            output_shape = [self.input_space.shape[0] - self.kernel_shape[0] + 1,
                self.input_space.shape[1] - self.kernel_shape[1] + 1]
        elif self.border_mode == 'full':
            output_shape = [self.input_space.shape[0] + self.kernel_shape[0] - 1,
                    self.input_space.shape[1] + self.kernel_shape[1] - 1]

        self.detector_space = Conv2DSpace(shape=output_shape,
                num_channels = self.output_channels,
                axes = ('b', 'c', 0, 1))

        if self.irange is not None:
            assert self.sparse_init is None
            self.transformer = conv2d.make_random_conv2D(
                    irange = self.irange,
                    input_space = self.input_space,
                    output_space = self.detector_space,
                    kernel_shape = self.kernel_shape,
                    batch_size = self.mlp.batch_size,
                    subsample = (1,1),
                    border_mode = self.border_mode,
                    rng = rng)
        elif self.sparse_init is not None:
            self.transformer = conv2d.make_sparse_random_conv2D(
                    num_nonzero = self.sparse_init,
                    input_space = self.input_space,
                    output_space = self.detector_space,
                    kernel_shape = self.kernel_shape,
                    batch_size = self.mlp.batch_size,
                    subsample = (1,1),
                    border_mode = self.border_mode,
                    rng = rng)
        W, = self.transformer.get_params()
        W.name = 'W'

        self.b = sharedX(self.detector_space.get_origin() + self.init_bias)
        self.b.name = 'b'

        print 'Input shape: ', self.input_space.shape
        print 'Detector space: ', self.detector_space.shape

        if self.mlp.batch_size is None:
            raise ValueError("Tried to use a convolutional layer with an MLP that has "
                    "no batch size specified. You must specify the batch size of the "
                    "model because theano requires the batch size to be known at "
                    "graph construction time for convolution.")

        assert self.pool_type in ['max', 'mean']

        dummy_detector = sharedX(self.detector_space.get_origin_batch(self.mlp.batch_size))
        if self.pool_type == 'max':
            dummy_p = max_pool(bc01=dummy_detector, pool_shape=self.pool_shape,
                    pool_stride=self.pool_stride,
                    image_shape=self.detector_space.shape)
        elif self.pool_type == 'mean':
            dummy_p = mean_pool(bc01=dummy_detector, pool_shape=self.pool_shape,
                    pool_stride=self.pool_stride,
                    image_shape=self.detector_space.shape)
        dummy_p = dummy_p.eval()
        self.output_space = Conv2DSpace(shape=[dummy_p.shape[2], dummy_p.shape[3]],
                num_channels = self.output_channels, axes = ('b', 'c', 0, 1) )

        print 'Output space: ', self.output_space.shape
    def set_input_space(self, space):
        """ Note: this resets parameters! """

        self.input_space = space
        rng = self.mlp.rng

        if self.border_mode == 'valid':
            output_shape = [
                self.input_space.shape[0] - self.kernel_shape[0] + 1,
                self.input_space.shape[1] - self.kernel_shape[1] + 1
            ]
        elif self.border_mode == 'full':
            output_shape = [
                self.input_space.shape[0] + self.kernel_shape[0] - 1,
                self.input_space.shape[1] + self.kernel_shape[1] - 1
            ]

        self.detector_space = Conv2DSpace(shape=output_shape,
                                          num_channels=self.output_channels,
                                          axes=('b', 'c', 0, 1))

        if self.irange is not None:
            assert self.sparse_init is None
            self.transformer = conv2d.make_random_conv2D(
                irange=self.irange,
                input_space=self.input_space,
                output_space=self.detector_space,
                kernel_shape=self.kernel_shape,
                batch_size=self.mlp.batch_size,
                subsample=(1, 1),
                border_mode=self.border_mode,
                rng=rng)
        elif self.sparse_init is not None:
            self.transformer = conv2d.make_sparse_random_conv2D(
                num_nonzero=self.sparse_init,
                input_space=self.input_space,
                output_space=self.detector_space,
                kernel_shape=self.kernel_shape,
                batch_size=self.mlp.batch_size,
                subsample=(1, 1),
                border_mode=self.border_mode,
                rng=rng)
        W, = self.transformer.get_params()
        W.name = 'W'

        self.b = sharedX(self.detector_space.get_origin() + self.init_bias)
        self.b.name = 'b'

        print 'Input shape: ', self.input_space.shape
        print 'Detector space: ', self.detector_space.shape

        if self.mlp.batch_size is None:
            raise ValueError(
                "Tried to use a convolutional layer with an MLP that has "
                "no batch size specified. You must specify the batch size of the "
                "model because theano requires the batch size to be known at "
                "graph construction time for convolution.")

        assert self.pool_type in ['max', 'mean']

        dummy_detector = sharedX(
            self.detector_space.get_origin_batch(self.mlp.batch_size))
        if self.pool_type == 'max':
            dummy_p = max_pool(bc01=dummy_detector,
                               pool_shape=self.pool_shape,
                               pool_stride=self.pool_stride,
                               image_shape=self.detector_space.shape)
        elif self.pool_type == 'mean':
            dummy_p = mean_pool(bc01=dummy_detector,
                                pool_shape=self.pool_shape,
                                pool_stride=self.pool_stride,
                                image_shape=self.detector_space.shape)
        dummy_p = dummy_p.eval()
        self.output_space = Conv2DSpace(
            shape=[dummy_p.shape[2], dummy_p.shape[3]],
            num_channels=self.output_channels,
            axes=('b', 'c', 0, 1))

        print 'Output space: ', self.output_space.shape
示例#18
0
def test_max_pool_options():
    """
    Compare gpu max pooling methods with various shapes
    and strides.
    """
    if not cuda.cuda_available:
        raise SkipTest('Optional package cuda disabled.')
    if not dnn_available():
        raise SkipTest('Optional package cuDNN disabled.')

    mode = copy.copy(theano.compile.get_default_mode())
    mode.check_isfinite = False

    X_sym = tensor.ftensor4('X')
    # Case 1: shape > stride
    shp = (3, 3)
    strd = (2, 2)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op
    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[7, 8],
                         [11, 12],
                         [14, 15]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 2: shape < stride
    shp = (2, 2)
    strd = (3, 3)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [10, 12]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 3: shape == stride
    shp = (2, 2)
    strd = (2, 2)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [6, 8],
                         [10, 15]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 4: row shape < row stride
    shp = (2, 2)
    strd = (3, 2)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [10, 12]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)

    # Case 5: col shape < col stride
    shp = (2, 2)
    strd = (2, 3)
    im_shp = (6, 4)
    pool_it = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                       image_shape=im_shp, try_dnn=False)
    pool_dnn = max_pool(X_sym, pool_shape=shp, pool_stride=strd,
                        image_shape=im_shp)
    # Make sure that different ops were used.
    assert pool_it.owner.op != pool_dnn.owner.op

    f = theano.function(inputs=[X_sym], outputs=[pool_it, pool_dnn],
                        mode=mode)

    X = np.array([[2, 1, 3, 4],
                  [1, 1, 3, 3],
                  [5, 5, 7, 8],
                  [5, 6, 8, 7],
                  [9, 10, 11, 12],
                  [9, 10, 14, 15]],
                 dtype="float32")[np.newaxis, np.newaxis, ...]

    expected = np.array([[2, 4],
                         [6, 8],
                         [10, 15]],
                        dtype="float32")[np.newaxis,
                                         np.newaxis,
                                         ...]
    actual, actual_dnn = f(X)
    actual_dnn = np.array(actual_dnn)
    assert np.allclose(expected, actual)
    assert np.allclose(actual, actual_dnn)