Python theano.tensor.signal.pool 模块,pool_2d() 实例源码

我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用theano.tensor.signal.pool.pool_2d()

项目:python-machine-learning    作者:sho-87    | 项目源码 | 文件源码
def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
        # Reshape the input to 2D
        self.inpt = inpt.reshape(self.image_shape)

        # Do convolution
        self.conv_out = conv2d(
            input=self.inpt, filters=self.w, filter_shape=self.filter_shape,
            input_shape=self.image_shape, border_mode=self.border_mode,
            subsample=self.stride)

        # Get the feature maps for this layer
        self.feature_maps = theano.function([self.inpt], self.conv_out)

        # Max pooling
        pooled_out = pool.pool_2d(input=self.conv_out, ds=self.poolsize,
                                  ignore_border=True, mode='max')

        # Apply bias and activation and set as output
        self.output = self.activation_fn(
            pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
        self.output_dropout = self.output # no dropout in convolutional layers
项目:NNBuilder    作者:aeloyq    | 项目源码 | 文件源码
def pool(self, input, window, mode, stride, pad, autopad):
            if mode == 'max':
                mode = 'max'
            elif mode == 'sum':
                mode = 'sum'
            elif mode == 'avg':
                mode = 'average_exc_pad'
            elif mode == 'avgpad':
                mode = 'average_inc_pad'
            else:
                mode = 'sum'
            if input.ndim == 4:
                return P.pool_2d(input=input, ws=window, ignore_border=not autopad, stride=stride, pad=pad, mode=mode)
            elif input.ndim == 5:
                return P.pool_3d(input=input, ws=window, ignore_border=not autopad, stride=stride, pad=pad, mode=mode)
            else:
                basic.defaultreturn()
项目:pyextremelm    作者:tobifinn    | 项目源码 | 文件源码
def _generate_conv(self):
        input = T.tensor4(name='input')
        if self.pooling == 'squareroot':
            conv_out = Pool.pool_2d(
                T.power(input,2),
                ds=(self.spatial[0], self.spatial[1]),
                ignore_border=self.ignore_border,
                mode='sum',
                padding=self.pad,
                st=None if self.stride is None else (self.stride, self.stride))
            conv_out = T.sqrt(conv_out)
        else:
            conv_out = Pool.pool_2d(
                input,
                ds=(self.spatial[0], self.spatial[1]),
                ignore_border=self.ignore_border,
                mode=self.pooling,
                padding=self.pad,
                st=None if self.stride is None else (self.stride, self.stride))
        if self.activation_fct is None:
            output = conv_out
        else:
            output = self.activation_fct(conv_out)
        self.conv = theano.function([input], output)
项目:lemontree    作者:khshim    | 项目源码 | 文件源码
def get_output(self, input_):
        """
        This function overrides the parents' one.
        Creates symbolic function to compute output from an input.

        Parameters
        ----------
        input_: TensorVariable

        Returns
        -------
        TensorVariable
        """
        return pool_2d(input_,
                       ws=self.kernel_shape,
                       ignore_border=True,  # if you don't want to ignore border, use padding
                       stride=self.stride,
                       pad=self.padding,
                       mode=self.pool_mode)
项目:lemontree    作者:khshim    | 项目源码 | 文件源码
def get_output(self, input_):
        """
        This function overrides the parents' one.
        Creates symbolic function to compute output from an input.

        Parameters
        ----------
        input_: TensorVariable

        Returns
        -------
        TensorVariable
        """
        result = pool_2d(input_,
                         ws=self.input_shape[1:],
                         ignore_border=True,
                         stride=self.input_shape[1:],
                         pad=self.padding,
                         mode='average_exc_pad')  # result is 4D tensor yet, (batch size, output channel, 1, 1)
        return T.reshape(result, (input_.shape[0], input_.shape[1]))  # flatten to 2D matrix
项目:dsde-deep-learning    作者:broadinstitute    | 项目源码 | 文件源码
def cnn_model(X, w, w2, w3, w4, w_o, p_drop_conv, p_drop_hidden):
    l1a = rectify(T.nnet.conv2d(X, w, border_mode='full'))
    l1 = pool.pool_2d(l1a, (2, 2))
    l1 = dropout(l1, p_drop_conv)

    l2a = rectify(T.nnet.conv2d(l1, w2))
    l2 = pool.pool_2d(l2a, (2, 2))
    l2 = dropout(l2, p_drop_conv)

    l3a = rectify(T.nnet.conv2d(l2, w3))
    l3b = pool.pool_2d(l3a, (2, 2))
    l3 = T.flatten(l3b, outdim=2)
    l3 = dropout(l3, p_drop_conv)

    l4 = rectify(T.dot(l3, w4))
    l4 = dropout(l4, p_drop_hidden)

    pyx = softmax(T.dot(l4, w_o))
    return l1, l2, l3, l4, pyx
项目:amdtk    作者:amdtkdev    | 项目源码 | 文件源码
def __init__(self, inputs, maxpool_height, maxpool_width):
        if inputs is None:
            self.inputs = T.tensor4(dtype=theano.config.floatX)
        else:
            self.inputs = inputs.flatten(4)
        self.maxpool_height = maxpool_height
        self.maxpool_width = maxpool_width
        self.outputs = pool.pool_2d(
            self.inputs,
            (maxpool_height, maxpool_width),
            ignore_border=True
        )

        # Pooling layer has no learnable parameters.
        self.params = []


# Possible layer types.
项目:structured-output-ae    作者:sbelharbi    | 项目源码 | 文件源码
def __init__(self, input, poolsize, ignore_border, mode="max"):
        """Implement a pooling layer."""
        self.input = input
        self.x = input
        self.output = pool.pool_2d(
            input=self.x,
            ws=poolsize,
            ignore_border=ignore_border,
            mode=mode)
        self.params = []
项目:sesame-paste-noodle    作者:aissehust    | 项目源码 | 文件源码
def forward(self, inputtensor):
        inputactivation = inputtensor[0]
        return (pool_2d(inputactivation
                        , self.size
                        , ignore_border=True
                        , stride=self.stride
                        , pad=self.pad
                        , mode=self.mode),)
项目:3D-R2N2    作者:chrischoy    | 项目源码 | 文件源码
def set_output(self):
        pooled_out = pool.pool_2d(
            input=self._prev_layer.output,
            ds=self._pool_size,
            ignore_border=True,
            padding=self._padding)
        self._output = pooled_out
项目:yadll    作者:pchavanne    | 项目源码 | 文件源码
def pool(self, input, ws):
        return pool.pool_2d(input=input, ws=ws, st=self.stride, ignore_border=self.ignore_border,
                            pad=self.pad, mode=self.mode)
项目:vaegan    作者:anitan0925    | 项目源码 | 文件源码
def max_pool( x, size, ignore_border=False ):
    return pool_2d( x, size, ignore_border=ignore_border )
项目:deeplearning    作者:wangzhics    | 项目源码 | 文件源码
def __init__(self, rng, input, input_shape, filter_shape, pool_shape=(2, 2)):
        """
        ???????????????????????
        :param input: ?????
        :param input_shape: ????????(batch_size, image_channel, image_weight, image_height)
        :param filter_shape: ???????(filter_count, filter_channel, filter_weight, filter_height)
        :param pool_shape: ??????
        :return:
        """
        #
        assert input_shape[1] == filter_shape[1]
        self.input = input
        self.input_shape = input_shape
        self.filter_shape = filter_shape
        self.pool_shape = pool_shape
        # ?????????
        n_in = numpy.prod(input_shape[1:])
        n_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) // numpy.prod(pool_shape))
        weight_max = numpy.sqrt(6. / (n_in + n_out))
        self.w = theano.shared(
            numpy.asarray(
                rng.uniform(low=-weight_max, high=weight_max, size=filter_shape),
                dtype=theano.config.floatX
            ),
            borrow=True
        )
        self.b = theano.shared(numpy.zeros((filter_shape[0],), dtype=theano.config.floatX), borrow=True)
        self.params = [self.w, self.b]
        # calculate the output
        self.conv_out = conv2d(
            input=self.input,
            filters=self.w,
            filter_shape=self.filter_shape,
            image_shape=self.input_shape
        )
        self.pool_out = pool_2d(
            input=self.conv_out,
            ds=pool_shape,
            ignore_border=True
        )
        self.output = T.tanh(self.pool_out + self.b.dimshuffle('x', 0, 'x', 'x'))
项目:LeNet5    作者:LukaszObara    | 项目源码 | 文件源码
def __init__(self, input, pool_shape=(2, 2), ignore_border=True,
                 activation_fn=None):
        self.input = input
        self.pool_shape = pool_shape
        self.ignore_border = ignore_border

        l_output = pool.pool_2d(input=input, ds=pool_shape, 
                                ignore_border=ignore_border)

        self.output = (l_output if activation_fn is None 
                       else activation_fn(l_output))
项目:LeNet5    作者:LukaszObara    | 项目源码 | 文件源码
def __init__(self, input, pool_shape=(2, 2), ignore_border=True,
                 activation_fn=None):
        self.input = input
        self.pool_shape = pool_shape
        self.ignore_border = ignore_border

        l_output = pool.pool_2d(input=input, ds=pool_shape, 
                                ignore_border=ignore_border)

        self.output = (l_output if activation_fn is None 
                       else activation_fn(l_output))
项目:CNNbasedMedicalSegmentation    作者:BRML    | 项目源码 | 文件源码
def max_pool_3d(inpt, inpt_shape, ds, ignore_border=True):
    # Downsize 'into the depth' by downsizing twice.
    inpt_shape_4d = (
        inpt_shape[0] * inpt_shape[1],
        inpt_shape[2],
        inpt_shape[3],
        inpt_shape[4]
    )

    inpt_as_tensor4 = T.reshape(inpt, inpt_shape_4d, ndim=4)

    # The first pooling only downsizes the height and the width.
    pool_out1 = pool.pool_2d(inpt_as_tensor4, (ds[1], ds[2]),
                                       ignore_border=True)
    out_shape1 = T.join(0, inpt_shape[:-2], pool_out1.shape[-2:])

    inpt_pooled_once = T.reshape(pool_out1, out_shape1, ndim=5)

    # Shuffle dimensions so the depth is the last dimension.
    inpt_shuffled = inpt_pooled_once.dimshuffle(0, 4, 2, 3, 1)

    shuffled_shape = inpt_shuffled.shape
    # Reshape input to be 4 dimensional.
    shuffle_shape_4d = (
        shuffled_shape[0] * shuffled_shape[1],
        shuffled_shape[2],
        shuffled_shape[3],
        shuffled_shape[4]
    )

    inpt_shuffled_4d = T.reshape(inpt_shuffled, shuffle_shape_4d, ndim=4)

    pool_out2 = pool.pool_2d(inpt_shuffled_4d, (1, ds[0]),
                                       ignore_border=True)
    out_shape2 = T.join(0, shuffled_shape[:-2], pool_out2.shape[-2:])

    inpt_pooled_twice = T.reshape(pool_out2, out_shape2, ndim=5)
    pool_output_fin = inpt_pooled_twice.dimshuffle(0, 4, 2, 3, 1)

    return pool_output_fin
项目:DeepFashion    作者:DevilC    | 项目源码 | 文件源码
def __init__(self, Layer_num, layer_input,
                 mini_batch_size, input_feature_num, img_shp,
                 this_layer_feature_num, last_layer_feature_num, filter_shp,
                 pool_shp
                 ):
        self.num = Layer_num
        #function conv2d(input, W),ip_shp = input.shape(); w_shp = W.shape()
        self.ip_shp = (mini_batch_size, input_feature_num, img_shp[0], img_shp[1])
        self.w_shp = (this_layer_feature_num, last_layer_feature_num, filter_shp[0], filter_shp[1])

        #input is a tensor4, input's shape = ip_shp
        self.input = layer_input

        #weight is a shared type, it saves the filters, its shape = w_shp
        self.W = theano.shared(init_weight(Layer_num, self.w_shp), 'Layer_'+str(Layer_num)+'_weight')

        #bias of this layer, size depend on the number of layer's feature maps
        b_shp = (this_layer_feature_num, )
        self.B = theano.shared(init_B(Layer_num, b_shp), 'Layer_'+str(Layer_num)+'_bias')

        self.conv_result = T.nnet.conv2d(self.input, self.W)

        self.pool_result = pool.pool_2d(self.conv_result, ds = pool_shp, ignore_border=False)#shape(batch_size, layer0_feature_num, 14, 14)

        #output of this layer
        self.output = T.nnet.relu(self.pool_result + self.B.dimshuffle('x', 0, 'x', 'x'))

        #parament
        self.para = [self.W, self.B]
项目:DeepRepICCV2015    作者:tomrunia    | 项目源码 | 文件源码
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):

        assert image_shape[1] == filter_shape[1]
        self.input = input

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = numpy.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
                   numpy.prod(poolsize))
        # initialize weights with random weights
        W_bound = numpy.sqrt(6. / (fan_in + fan_out))
        self.W = theano.shared(numpy.asarray(
            rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
            dtype=theano.config.floatX),
                               borrow=True)

        # the bias is a 1D tensor -- one bias per output feature map
        b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, borrow=True)

        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,
                filter_shape=filter_shape, image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = pool.pool_2d(input=conv_out, ds=poolsize, ignore_border=True)

        self.output = T.maximum(0.0, pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
项目:DeepRepICCV2015    作者:tomrunia    | 项目源码 | 文件源码
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):

        assert image_shape[1] == filter_shape[1]
        self.input = input

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = numpy.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
                   numpy.prod(poolsize))
        # initialize weights with random weights
        W_bound = numpy.sqrt(6. / (fan_in + fan_out))
        self.W = theano.shared(numpy.asarray(
            rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
            dtype=theano.config.floatX),
                               borrow=True)

        # the bias is a 1D tensor -- one bias per output feature map
        b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, borrow=True)

        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,
                filter_shape=filter_shape, image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = pool.pool_2d(input=conv_out, ds=poolsize, ignore_border=True)

        self.output = T.maximum(0.0, pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
项目:reading-text-in-the-wild    作者:mathDR    | 项目源码 | 文件源码
def pool2d(x, pool_size, strides=(1, 1), border_mode='valid',
           dim_ordering='th', pool_mode='max'):
    if border_mode == 'same':
        w_pad = pool_size[0] - 2 if pool_size[0] % 2 == 1 else pool_size[0] - 1
        h_pad = pool_size[1] - 2 if pool_size[1] % 2 == 1 else pool_size[1] - 1
        padding = (w_pad, h_pad)
    elif border_mode == 'valid':
        padding = (0, 0)
    else:
        raise Exception('Invalid border mode: ' + str(border_mode))

    if dim_ordering not in {'th', 'tf'}:
        raise Exception('Unknown dim_ordering ' + str(dim_ordering))

    if dim_ordering == 'tf':
        x = x.dimshuffle((0, 3, 1, 2))

    if pool_mode == 'max':
        pool_out = pool.pool_2d(x, ds=pool_size, st=strides,
                                ignore_border=True,
                                padding=padding,
                                mode='max')
    elif pool_mode == 'avg':
        pool_out = pool.pool_2d(x, ds=pool_size, st=strides,
                                ignore_border=True,
                                padding=padding,
                                mode='average_exc_pad')
    else:
        raise Exception('Invalid pooling mode: ' + str(pool_mode))

    if border_mode == 'same':
        expected_width = (x.shape[2] + strides[0] - 1) // strides[0]
        expected_height = (x.shape[3] + strides[1] - 1) // strides[1]

        pool_out = pool_out[:, :,
                            : expected_width,
                            : expected_height]

    if dim_ordering == 'tf':
        pool_out = pool_out.dimshuffle((0, 2, 3, 1))
    return pool_out
项目:Theano-NN_Starter    作者:nightinwhite    | 项目源码 | 文件源码
def get_output(self):
        return pool.pool_2d(self.input, self.pool_size, ignore_border=True)
项目:DBQA    作者:nanfeng1101    | 项目源码 | 文件源码
def __call__(self, q_input, a_input, *args, **kwargs):
        # convolve input feature maps with filters
        q_conv_out = conv2d(
            input=q_input,
            filters=self.W,
            filter_shape=self.filter_shape
        )
        a_conv_out = conv2d(
            input=a_input,
            filters=self.W,
            filter_shape=self.filter_shape
        )
        # add the bias term. Since the bias is a vector (1D array), we first
        # reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will
        # thus be broadcasted across mini-batches and feature map
        # width & height
        if self.non_linear == "tanh":
            q_conv_out_tanh = Tanh(q_conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            a_conv_out_tanh = Tanh(a_conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            q_output = pool.pool_2d(input=q_conv_out_tanh, ws=self.pool_size, ignore_border=True) # max
            a_output = pool.pool_2d(input=a_conv_out_tanh, ws=self.pool_size, ignore_border=True)
        elif self.non_linear == "relu":
            q_conv_out_relu = ReLU(q_conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            a_conv_out_relu = ReLU(a_conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            q_output = pool.pool_2d(input=q_conv_out_relu, ws=self.pool_size, ignore_border=True)
            a_output = pool.pool_2d(input=a_conv_out_relu, ws=self.pool_size, ignore_border=True)
        else:
            q_output = pool.pool_2d(input=q_conv_out, ws=self.pool_size, ignore_border=True)
            a_output = pool.pool_2d(input=a_conv_out, ws=self.pool_size, ignore_border=True)

        return q_output, a_output
项目:sentence_classification    作者:zhegan27    | 项目源码 | 文件源码
def encoder(tparams, layer0_input, filter_shape, pool_size,
                      prefix='cnn_encoder'):

    """ filter_shape: (number of filters, num input feature maps, filter height,
                        filter width)
        image_shape: (batch_size, num input feature maps, image height, image width)
    """

    conv_out = conv.conv2d(input=layer0_input, filters=tparams[_p(prefix,'W')], 
                            filter_shape=filter_shape)

    conv_out_tanh = tensor.tanh(conv_out + tparams[_p(prefix,'b')].dimshuffle('x', 0, 'x', 'x'))
    output = pool.pool_2d(input=conv_out_tanh, ds=pool_size, ignore_border=True)

    return output.flatten(2)
项目:ObjRecPoseEst    作者:paroj    | 项目源码 | 文件源码
def __init__(self, rng, inputVar, cfgParams, copyLayer=None, layerNum=None):    
        """
        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type inputVar: theano.tensor.dtensor4
        :param inputVar: symbolic image tensor, of shape image_shape

        :type cfgParams: ConvLayerParams
        """
        self.cfgParams = cfgParams

        poolsize  = cfgParams.poolsize
        poolType  = cfgParams.poolType

        self.inputVar = inputVar

        if poolType == 0:
            pooled_out = pool.pool_2d(input = inputVar,
                                      ds = poolsize, ignore_border=True)

        self.output = pooled_out

        # store parameters of this layer; has none
        self.params = []
        self.weights = []
项目:rnn-theano    作者:wangxggc    | 项目源码 | 文件源码
def get_output(self, input):
        """
        input is

        :param input: A 4-D tensor with shape(batch_size, channels, sen_len, embedding_size),
                      usually, embedding_size == filter_width
        :return: A 4-D tensor with shape(batch_size, filter_size, sen_len-filter_height+1, embedding_size-filter_width+1)
        """
        # usually output is a 4-D tensor with shape(batch_size, filters, sen_len-filter_height+1, 1)
        output = T.nnet.conv2d(input=input,
                      filters=self.params[self.id + "conv_w"],
                      input_shape=self.input_shape,
                      filter_shape=self.filter_shape,
                      border_mode="valid")
        #  output = output.reshape([self.batch_size, self.filter_size, self.pooling_shape[0], self.pooling_shape[1]])
        # add a bias to each filter
        output += self.params[self.id + "conv_b"].dimshuffle("x", 0, "x", "x")

        if self.pooling_mode != "average": #self.pooling_mode == "max":
            output = pool.pool_2d(input=output,
                                 ignore_border=True,
                                 ds=self.pooling_shape,
                                 st=self.pooling_shape,
                                 padding=(0, 0),    # padding shape
                                 mode="max")
            # output = theano.printing.Print("Conv Pool Out")(output)
            return output.flatten().reshape([self.batch_size, self.filter_size])
        elif self.pooling_mode == "average":
            output = pool.pool_2d(input=output,
                                 ignore_border=True,
                                 ds=self.pooling_shape,
                                 st=self.pooling_shape,
                                 padding=(0, 0),    # padding shape
                                 mode="average_inc_pad")

            return output.flatten().reshape([self.batch_size, self.filter_size])
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_dnn_tag():
    """
    Test that if cudnn isn't avail we crash and that if it is avail, we use it.
    """
    x = T.ftensor4()
    old = theano.config.on_opt_error
    theano.config.on_opt_error = "raise"

    sio = StringIO()
    handler = logging.StreamHandler(sio)
    logging.getLogger('theano.compile.tests.test_dnn').addHandler(handler)
    # Silence original handler when intentionnally generating warning messages
    logging.getLogger('theano').removeHandler(theano.logging_default_handler)
    raised = False
    try:
        f = theano.function(
            [x],
            pool_2d(x, ds=(2, 2), ignore_border=True),
            mode=mode_with_gpu.including("cudnn"))
    except (AssertionError, RuntimeError):
        assert not dnn.dnn_available(test_ctx_name)
        raised = True
    finally:
        theano.config.on_opt_error = old
        logging.getLogger(
            'theano.compile.tests.test_dnn').removeHandler(handler)
        logging.getLogger('theano').addHandler(theano.logging_default_handler)

    if not raised:
        assert dnn.dnn_available(test_ctx_name)
        assert any([isinstance(n.op, dnn.GpuDnnPool)
                    for n in f.maker.fgraph.toposort()])
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_dnn_tag():
    """
    Test that if cudnn isn't avail we crash and that if it is avail, we use it.
    """
    x = T.ftensor4()
    old = theano.config.on_opt_error
    theano.config.on_opt_error = "raise"

    sio = StringIO()
    handler = logging.StreamHandler(sio)
    logging.getLogger('theano.compile.tests.test_dnn').addHandler(handler)
    # Silence original handler when intentionnally generating warning messages
    logging.getLogger('theano').removeHandler(theano.logging_default_handler)
    raised = False
    try:
        f = theano.function(
            [x],
            pool_2d(x, ds=(2, 2), ignore_border=True),
            mode=mode_with_gpu.including("cudnn"))
    except (AssertionError, RuntimeError):
        assert not cuda.dnn.dnn_available()
        raised = True
    finally:
        theano.config.on_opt_error = old
        logging.getLogger(
            'theano.compile.tests.test_dnn').removeHandler(handler)
        logging.getLogger('theano').addHandler(theano.logging_default_handler)

    if not raised:
        assert cuda.dnn.dnn_available()
        assert any([isinstance(n.op, cuda.dnn.GpuDnnPool)
                    for n in f.maker.fgraph.toposort()])
项目:Hat    作者:qiuqiangkong    | 项目源码 | 文件源码
def pool2d(input, ds, ignore_border=True, st=None, padding=(0, 0), mode='max'):
    if mode=='avg': mode='average_exc_pad'
    return pool.pool_2d(input, ds, ignore_border, st, padding, mode)
项目:insuranceQA-cnn-lstm    作者:cszhz    | 项目源码 | 文件源码
def _cnn_net(self, tparams, cnn_input, batch_size, sequence_len, num_filters, filter_sizes, proj_size):
    outputs = []
    for filter_size in filter_sizes:
        filter_shape = (num_filters, 1, filter_size, proj_size)
        image_shape = (batch_size, 1, sequence_len, proj_size)
        W = tparams['cnn_W_' + str(filter_size)]
        b = tparams['cnn_b_' + str(filter_size)]
        conv_out = conv2d(input=cnn_input, filters=W, filter_shape=filter_shape, input_shape=image_shape)
        pooled_out = pool.pool_2d(input=conv_out, ds=(sequence_len - filter_size + 1, 1), ignore_border=True, mode='max')
        pooled_active = T.tanh(pooled_out + b.dimshuffle('x', 0, 'x', 'x'))
        outputs.append(pooled_active)
    num_filters_total = num_filters * len(filter_sizes)
    output_tensor = T.reshape(T.concatenate(outputs, axis=1), [batch_size, num_filters_total])
    return output_tensor
项目:DeepLearningPython35    作者:MichalDanielDobrzanski    | 项目源码 | 文件源码
def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
        self.inpt = inpt.reshape(self.image_shape)
        conv_out = conv.conv2d(
            input=self.inpt, filters=self.w, filter_shape=self.filter_shape,
            image_shape=self.image_shape)
        pooled_out = pool_2d(
            input=conv_out, ws=self.poolsize, ignore_border=True)
        self.output = self.activation_fn(
            pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
        self.output_dropout = self.output # no dropout in the convolutional layers
项目:imageqa-san    作者:zcyang    | 项目源码 | 文件源码
def maxpool_layer(shared_params, x, maxpool_shape, options):
    return pool.pool_2d(x, maxpool_shape, ignore_border=False)
项目:InnerOuterRNN    作者:Chemoinformatics    | 项目源码 | 文件源码
def pool2d(x, pool_size, strides=(1, 1), border_mode='valid',
           dim_ordering='th', pool_mode='max'):
    if border_mode == 'same':
        w_pad = pool_size[0] - 2 if pool_size[0] % 2 == 1 else pool_size[0] - 1
        h_pad = pool_size[1] - 2 if pool_size[1] % 2 == 1 else pool_size[1] - 1
        padding = (w_pad, h_pad)
    elif border_mode == 'valid':
        padding = (0, 0)
    else:
        raise Exception('Invalid border mode: ' + str(border_mode))

    if dim_ordering not in {'th', 'tf'}:
        raise Exception('Unknown dim_ordering ' + str(dim_ordering))

    if dim_ordering == 'tf':
        x = x.dimshuffle((0, 3, 1, 2))

    if pool_mode == 'max':
        pool_out = pool.pool_2d(x, ds=pool_size, st=strides,
                                ignore_border=True,
                                padding=padding,
                                mode='max')
    elif pool_mode == 'avg':
        pool_out = pool.pool_2d(x, ds=pool_size, st=strides,
                                ignore_border=True,
                                padding=padding,
                                mode='average_exc_pad')
    else:
        raise Exception('Invalid pooling mode: ' + str(pool_mode))

    if border_mode == 'same':
        expected_width = (x.shape[2] + strides[0] - 1) // strides[0]
        expected_height = (x.shape[3] + strides[1] - 1) // strides[1]

        pool_out = pool_out[:, :,
                            : expected_width,
                            : expected_height]

    if dim_ordering == 'tf':
        pool_out = pool_out.dimshuffle((0, 2, 3, 1))
    return pool_out
项目:aed-by-cnn    作者:tweihaha    | 项目源码 | 文件源码
def get_output_for(self, input, **kwargs):
        if self.pad == 'strictsamex':
            assert(self.stride[0] == 1)
            kk = self.pool_size[0]
            ll = int(np.ceil(kk/2.))
            # rr = kk-ll
            # pad = (ll, 0)
            pad = [(ll, 0)]

            length = input.shape[2]

            self.ignore_border = True
            input = padding.pad(input, pad, batch_ndim=2)
            pad = (0, 0)
        else:
            pad = self.pad

        pooled = pool.pool_2d(input,
                              ds=self.pool_size,
                              st=self.stride,
                              ignore_border=self.ignore_border,
                              padding=pad,
                              mode=self.mode,
                              )

        if self.pad == 'strictsamex':
            pooled = pooled[:, :, :length or None, :]

        return pooled


# add 'strictsamex' method for pad
项目:IQA_BIECON_release    作者:jongyookim    | 项目源码 | 文件源码
def get_output(self, input, **kwargs):
        pooled = pool_2d(input,
                         ws=self.pool_size,
                         stride=self.stride,
                         ignore_border=self.ignore_border,
                         pad=self.pad,
                         mode=self.mode,
                         )
        return pooled
项目:CQA-CNN    作者:3141bishwa    | 项目源码 | 文件源码
def output_func(self, input):
    # In input we get a tensor (batch_size, nwords, ndim)
    return pool.pool_2d(input=input, ds=self.pool_size, ignore_border=True)
项目:odin_old    作者:trungnt13    | 项目源码 | 文件源码
def pool2d(x, pool_size, strides=(1, 1), border_mode='valid',
           dim_ordering='th', pool_mode='max'):
    # ====== dim ordering ====== #
    if dim_ordering not in {'th', 'tf'}:
        raise Exception('Unknown dim_ordering ' + str(dim_ordering))
    if dim_ordering == 'tf':
        x = x.dimshuffle((0, 3, 1, 2))
    # ====== border mode ====== #
    if border_mode == 'same':
        w_pad = pool_size[0] - 2 if pool_size[0] % 2 == 1 else pool_size[0] - 1
        h_pad = pool_size[1] - 2 if pool_size[1] % 2 == 1 else pool_size[1] - 1
        padding = (w_pad, h_pad)
    elif border_mode == 'valid':
        padding = (0, 0)
    elif isinstance(border_mode, (tuple, list)):
        padding = tuple(border_mode)
    else:
        raise Exception('Invalid border mode: ' + str(border_mode))

    # ====== pooling ====== #
    if _on_gpu() and dnn.dnn_available():
        pool_out = dnn.dnn_pool(x, pool_size,
                                stride=strides,
                                mode=pool_mode,
                                pad=padding)
    else: # CPU veresion support by theano
        pool_out = pool.pool_2d(x, ds=pool_size, st=strides,
                                ignore_border=True,
                                padding=padding,
                                mode=pool_mode)

    if dim_ordering == 'tf':
        pool_out = pool_out.dimshuffle((0, 2, 3, 1))
    return pool_out
项目:deep-prior    作者:moberweger    | 项目源码 | 文件源码
def __init__(self, rng, inputVar, cfgParams, copyLayer=None, layerNum=None):
        """
        Allocate a PoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type inputVar: theano.tensor.dtensor4
        :param inputVar: symbolic image tensor, of shape image_shape

        :type cfgParams: PoolLayerParams
        """

        floatX = theano.config.floatX  # @UndefinedVariable

        outputDim = cfgParams.outputDim
        poolsize = cfgParams.poolsize
        inputDim = cfgParams.inputDim
        activation = cfgParams.activation
        poolType = cfgParams.poolType

        self.cfgParams = cfgParams
        self.layerNum = layerNum

        self.inputVar = inputVar

        if inputVar.type.ndim != 4:
            raise TypeError()

        self.params = []
        self.weights = []

        # downsample each feature map individually, using maxpooling
        if poolType == 0:
            # use maxpooling
            pooled_out = pool_2d(input=self.inputVar, ds=poolsize, ignore_border=True)
        elif poolType == 1:
            # use average pooling
            pooled_out = theano.sandbox.neighbours.images2neibs(ten4=self.inputVar, neib_shape=poolsize, mode='ignore_borders').mean(axis=-1)
            new_shape = T.cast(T.join(0, self.inputVar.shape[:-2], T.as_tensor([self.inputVar.shape[2]//poolsize[0]]), T.as_tensor([self.inputVar.shape[3]//poolsize[1]])), 'int64')
            pooled_out = T.reshape(pooled_out, new_shape, ndim=4)
        elif poolType == 3:
            # use subsampling and ignore border
            pooled_out = self.inputVar[:, :, :(inputDim[2]//poolsize[0])*poolsize[0], :(inputDim[3]//poolsize[1])*poolsize[1]][:, :, ::poolsize[0], ::poolsize[1]]
        elif poolType == -1:
            # no pooling at all
            pooled_out = self.inputVar
        else:
            raise ValueError("Unknown pool type!")

        self.output = (pooled_out if activation is None
                       else activation(pooled_out))

        self.output.name = 'output_layer_{}'.format(self.layerNum)
项目:deer    作者:VinF    | 项目源码 | 文件源码
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(1, 1), stride=(1,1)):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """

        assert image_shape[1] == filter_shape[1]
        self.input = input

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = numpy.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
                   numpy.prod(poolsize))
        # initialize weights with random weights
        W_bound = numpy.sqrt(6. / (fan_in + fan_out))
        self.W = theano.shared(numpy.asarray(
            rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
            dtype=theano.config.floatX),
                               borrow=True)

        # the bias is a 1D tensor -- one bias per output feature map
        b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, borrow=True)

        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,
                filter_shape=filter_shape, image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = pool.pool_2d(input=conv_out,
                                            ds=poolsize, ignore_border=True, st=stride)

        # add the bias term. Since the bias is a vector (1D array), we first
        # reshape it to a tensor of shape (1,n_filters,1,1). Each bias will
        # thus be broadcasted across mini-batches and feature map
        # width & height
        self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
项目:keraflow    作者:ipod825    | 项目源码 | 文件源码
def pool(self, x, mode, pool_size, strides, padding=(0,0)):

        if strides is None:
            strides = pool_size
        assert len(strides)==len(pool_size)
        do2D = len(pool_size)==2

        if mode=='avg':
            mode='average_exc_pad'

        # theano requires symmetric padding
        # We pad the larger on when two sides' padding are unequal
        max_padding = list(padding)
        for i, p in enumerate(padding):
            if isinstance(p, tuple):
                assert p[1]==p[0]+1
                max_padding[i] = p[1]
            else:
                max_padding[i] = p

        if do2D:
            pool_out = pool.pool_2d(x, ds=pool_size, st=strides,
                                    ignore_border=True,
                                    padding=max_padding,
                                    mode=mode)
        else:
            # pool over HW
            pool_out = pool.pool_2d(x.dimshuffle(0,1,4,2,3),
                                    ds=pool_size[:2],
                                    st=strides[:2],
                                    ignore_border=True,
                                    padding=max_padding[:2],
                                    mode=mode)

            # pool over Z
            pool_out = pool.pool_2d(pool_out.dimshuffle(0,1,3,4,2),
                                    ds=(1,pool_size[2]),
                                    st=(1,strides[2]),
                                    ignore_border=True,
                                    padding=(0, max_padding[2]),
                                    mode=mode)

        # theano might output more than expected output shape (due to max padding). We truncate them here
        exp_l = []
        for i in range(len(strides)):
            l = T.ceil(self.cast(x.shape[i+2], _FLOATX)/strides[i])
            exp_l.append(self.cast(l, 'int32'))

        if do2D:
            return pool_out[:, :, :exp_l[0], :exp_l[1]]
        else:
            return pool_out[:, :, :exp_l[0], :exp_l[1], :exp_l[2]]
项目:lemontree    作者:khshim    | 项目源码 | 文件源码
def __init__(self, input_shape, output_shape,
                 kernel_shape=(2, 2), pool_mode='max', stride=(2, 2), padding = (0,0)):
        """
        This function initializes the class.
        Input is 4D tensor, output is 4D tensor.

        Parameters
        ----------
        input_shape: tuple
            a tuple of three values, i.e., (input channel, input width, input height).
        output_shape: tuple
            a tuple of three values, i.e., (output channel, output width, output height).
            output width and height should be match to real convolution output.
        kernel_shape: tuple, default: (2, 2)
            a tuple of two values, i.e., (kernel width, kernel height).
        pool_mode: string {'max', 'sum', 'average_inc_pad', 'average_exc_pad'}, default: 'max'
            a string to determine which mode theano pooling will use.
            'max': max pooling
            'sum': sum pooling
            'average_inc_pad': average pooling contains padding
            'average_exc_pad': average pooling does not contain padding
            'half': pad input with (kernel width //2, kernel height //2) symmetrically and do 'valid'.
                if kernel width and height is odd number, output = input
            int: pad input with (int, int) symmetrically.
            (int1, int2): pad input with (int1, int2) symmetrically.
        stride: tuple, default: (1,1)
            a tuple of two value, i.e., (stride width, stride height).
            also known as subsample.
        padding: tuple, default: (0, 0)
            a tuple of two value, i.e., (padding updown, padding leftright).
            a symmetric padding. padding first, pooling second.
        """
        super(Pooling3DLayer, self).__init__()
        # check asserts
        assert isinstance(input_shape, tuple) and len(input_shape) == 3, '"input_shape" should be a tuple with three values.'
        assert isinstance(output_shape, tuple) and len(output_shape) == 3, '"output_shape" should be a tuple with three values.'
        assert isinstance(kernel_shape, tuple) and len(kernel_shape) == 2, '"kernel_shape" should be a tuple with two values.'
        assert isinstance(stride, tuple) and len(stride) == 2, '"stride" should be a tuple with two values.'
        assert isinstance(padding, tuple) and len(padding) == 2, '"padding" should be a tuple with two values.'
        assert pool_mode in ['max', 'sum', 'average_inc_pad', 'average_exc_pad'], '"poolmode should be a string mode. see theano.tensor.signal.pool.pool_2d for details.'

        # set members
        self.input_shape = input_shape
        self.output_shape = output_shape
        self.kernel_shape = kernel_shape
        self.pool_mode = pool_mode
        self.stride = stride
        self.padding = padding
项目:reading-text-in-the-wild    作者:mathDR    | 项目源码 | 文件源码
def pool3d(x, pool_size, strides=(1, 1, 1), border_mode='valid',
           dim_ordering='th', pool_mode='max'):
    if border_mode == 'same':
        # TODO: add implementation for border_mode="same"
        raise Exception('border_mode="same" not supported with Theano.')
    elif border_mode == 'valid':
        ignore_border = True
        padding = (0, 0)
    else:
        raise Exception('Invalid border mode: ' + str(border_mode))

    if dim_ordering not in {'th', 'tf'}:
        raise Exception('Unknown dim_ordering ' + str(dim_ordering))

    if dim_ordering == 'tf':
        x = x.dimshuffle((0, 4, 1, 2, 3))

    if pool_mode == 'max':
        # pooling over conv_dim2, conv_dim1 (last two channels)
        output = pool.pool_2d(input=x.dimshuffle(0, 1, 4, 3, 2),
                              ds=(pool_size[1], pool_size[0]),
                              st=(strides[1], strides[0]),
                              ignore_border=ignore_border,
                              padding=padding,
                              mode='max')

        # pooling over conv_dim3
        pool_out = pool.pool_2d(input=output.dimshuffle(0, 1, 4, 3, 2),
                                ds=(1, pool_size[2]),
                                st=(1, strides[2]),
                                ignore_border=ignore_border,
                                padding=padding,
                                mode='max')

    elif pool_mode == 'avg':
        # pooling over conv_dim2, conv_dim1 (last two channels)
        output = pool.pool_2d(input=x.dimshuffle(0, 1, 4, 3, 2),
                              ds=(pool_size[1], pool_size[0]),
                              st=(strides[1], strides[0]),
                              ignore_border=ignore_border,
                              padding=padding,
                              mode='average_exc_pad')

        # pooling over conv_dim3
        pool_out = pool.pool_2d(input=output.dimshuffle(0, 1, 4, 3, 2),
                                ds=(1, pool_size[2]),
                                st=(1, strides[2]),
                                ignore_border=ignore_border,
                                padding=padding,
                                mode='average_exc_pad')
    else:
        raise Exception('Invalid pooling mode: ' + str(pool_mode))

    if dim_ordering == 'tf':
        pool_out = pool_out.dimshuffle((0, 2, 3, 4, 1))
    return pool_out


# RANDOMNESS
项目:deep-prior-pp    作者:moberweger    | 项目源码 | 文件源码
def __init__(self, rng, inputVar, cfgParams, copyLayer=None, layerNum=None):
        """
        Allocate a PoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type inputVar: theano.tensor.dtensor4
        :param inputVar: symbolic image tensor, of shape image_shape

        :type cfgParams: PoolLayerParams
        """
        import theano
        import theano.sandbox.neighbours
        import theano.tensor as T
        from theano.tensor.signal.pool import pool_2d

        super(PoolLayer, self).__init__(rng)

        floatX = theano.config.floatX  # @UndefinedVariable

        outputDim = cfgParams.outputDim
        poolsize = cfgParams.poolsize
        inputDim = cfgParams.inputDim
        activation = cfgParams.activation
        poolType = cfgParams.poolType

        self.cfgParams = cfgParams
        self.layerNum = layerNum

        self.inputVar = inputVar

        if inputVar.type.ndim != 4:
            raise TypeError()

        self.params = []
        self.weights = []

        # downsample each feature map individually, using maxpooling
        if poolType == 0:
            # use maxpooling
            pooled_out = pool_2d(input=self.inputVar, ds=poolsize, ignore_border=True, mode='max')
        elif poolType == 1:
            # use average pooling
            pooled_out = pool_2d(input=self.inputVar, ds=poolsize, ignore_border=True, mode='average_inc_pad')
        elif poolType == 3:
            # use subsampling and ignore border
            pooled_out = self.inputVar[:, :, :(inputDim[2]//poolsize[0])*poolsize[0], :(inputDim[3]//poolsize[1])*poolsize[1]][:, :, ::poolsize[0], ::poolsize[1]]
        elif poolType == -1:
            # no pooling at all
            pooled_out = self.inputVar
        else:
            raise NotImplementedError()
        self.output_pre_act = pooled_out

        self.output = (pooled_out if activation is None
                       else activation(pooled_out))

        self.output.name = 'output_layer_{}'.format(self.layerNum)
项目:lmkit    作者:jiangnanhugo    | 项目源码 | 文件源码
def __init__(self,input,
               filter_shape,   # 2*3*3
               input_shape,
               pool_size=(2,2)):
        """

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                                      filter height, filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                                     image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows, #cols)
        """
        #print input_shape
        #print filter_shape
        #assert input_shape[1] == filter_shape[1]

        self.input=input
        fan_in=np.prod(filter_shape[1:])

        fan_out=(filter_shape[0]*np.prod(filter_shape[2:])/
                 np.prod(pool_size))


        init_W=np.asarray(np.random.uniform(low=-np.sqrt(6./(fan_in+fan_out)),
                                      high=np.sqrt(6./(fan_in+fan_out)),
                                      size=filter_shape),
                          dtype=theano.config.floatX)
        self.W=theano.shared(value=init_W,name='W',borrow=True)

        init_b=np.zeros((filter_shape[0],),dtype=theano.config.floatX)
        self.b=theano.shared(value=init_b,borrow=True)

        conv_out=T.nnet.conv2d(
            input=input,
            filters=self.W,
            filter_shape=filter_shape,
            input_shape=input_shape
        )
        pool_out=pool.pool_2d(
            input=conv_out,
            ws=pool_size,
            ignore_border=True)

        self.activation=T.tanh(pool_out+self.b.dimshuffle('x',0,'x','x')).flatten(2)
        self.params=[self.W,self.b]
项目:keras_superpixel_pooling    作者:parag2489    | 项目源码 | 文件源码
def pool2d(x, pool_size, strides=(1, 1), padding='valid',
           data_format=None, pool_mode='max'):
    if data_format is None:
        data_format = image_data_format()
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Unknown data_format:', data_format)

    assert pool_size[0] >= 1 and pool_size[1] >= 1

    if padding == 'same':
        w_pad = pool_size[0] - 2 if pool_size[0] > 2 and pool_size[0] % 2 == 1 else pool_size[0] - 1
        h_pad = pool_size[1] - 2 if pool_size[1] > 2 and pool_size[1] % 2 == 1 else pool_size[1] - 1
        pad = (w_pad, h_pad)
    elif padding == 'valid':
        pad = (0, 0)
    else:
        raise ValueError('Invalid border mode:', padding)

    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Unknown data_format:', data_format)

    if data_format == 'channels_last':
        x = x.dimshuffle((0, 3, 1, 2))

    if pool_mode == 'max':
        pool_out = pool.pool_2d(x, ws=pool_size, stride=strides,
                                ignore_border=True,
                                pad=pad,
                                mode='max')
    elif pool_mode == 'avg':
        pool_out = pool.pool_2d(x, ws=pool_size, stride=strides,
                                ignore_border=True,
                                pad=pad,
                                mode='average_exc_pad')
    else:
        raise ValueError('Invalid pooling mode:', pool_mode)
    if padding == 'same':
        expected_width = (x.shape[2] + strides[0] - 1) // strides[0]
        expected_height = (x.shape[3] + strides[1] - 1) // strides[1]
        pool_out = pool_out[:, :,
                            : expected_width,
                            : expected_height]

    if data_format == 'channels_last':
        pool_out = pool_out.dimshuffle((0, 2, 3, 1))
    return pool_out
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_pooling_with_tensor_vars():
    if not dnn.dnn_available(test_ctx_name):
        raise SkipTest(dnn.dnn_available.msg)
    x = T.ftensor4()
    ws = theano.shared(numpy.array([2, 2], dtype='int32'))
    st = theano.shared(numpy.array([1, 1], dtype='int32'))
    pad = theano.shared(numpy.array([0, 0], dtype='int32'))
    mode = 'max'

    def fn(x):
        dnn_op = dnn.dnn_pool(
            x, ws=ws,
            stride=st,
            pad=pad,
            mode=mode)
        return dnn_op

    for shp in [(1, 1, 2, 2),
                (1, 1, 3, 3)]:
        data = numpy.random.normal(0, 1, shp).astype("float32") * 10
        theano.tests.unittest_tools.verify_grad(
            fn, [data], mode=mode_with_gpu)

    mode_without_gpu2 = mode_without_gpu.including()
    mode_without_gpu2.check_isfinite = False

    # GPU implementation
    f_gpu = theano.function([x], fn(x), mode=mode_with_gpu)
    assert any([isinstance(node.op, dnn.GpuDnnPool)
                for node in f_gpu.maker.fgraph.apply_nodes])

    # CPU implementation
    out_cpu = pool_2d(x, ws, ignore_border=True, st=st, padding=pad, mode=mode)
    f_cpu = theano.function([x], out_cpu, mode=mode_without_gpu2)
    assert not any([isinstance(node.op, dnn.GpuDnnPool)
                   for node in f_cpu.maker.fgraph.apply_nodes])
    assert any([isinstance(node.op, Pool)
                for node in f_cpu.maker.fgraph.apply_nodes])

    i = 1
    for shp in [(1, 10, 100, 100),
                (1, 3, 99, 99),
                (32, 1, 147, 197)]:
        data = numpy.random.normal(0, 1, shp).astype("float32")

        # Change the window size dynamically
        ws.set_value(numpy.array([i, i]).astype('int32'))
        a = f_gpu(data).__array__()
        b = f_cpu(data).__array__()
        utt.assert_allclose(a, b)
        i += 1
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def test_pooling_with_tensor_vars():
    if not cuda.dnn.dnn_available():
        raise SkipTest(cuda.dnn.dnn_available.msg)
    x = T.ftensor4()
    ws = theano.shared(numpy.array([2, 2], dtype='int32'))
    st = theano.shared(numpy.array([1, 1], dtype='int32'))
    pad = theano.shared(numpy.array([0, 0], dtype='int32'))
    mode = 'max'

    def fn(x):
        dnn_op = cuda.dnn.dnn_pool(
            x, ws=ws,
            stride=st,
            pad=pad,
            mode=mode)
        return dnn_op

    for shp in [(1, 1, 2, 2),
                (1, 1, 3, 3)]:
        data = numpy.random.normal(0, 1, shp).astype("float32") * 10
        theano.tests.unittest_tools.verify_grad(
            fn, [data], mode=mode_with_gpu)

    mode_without_gpu2 = mode_without_gpu.including()
    mode_without_gpu2.check_isfinite = False

    # GPU implementation
    f_gpu = theano.function([x], fn(x), mode=mode_with_gpu)
    assert any([isinstance(node.op, cuda.dnn.GpuDnnPool)
                for node in f_gpu.maker.fgraph.apply_nodes])

    # CPU implementation
    out_cpu = pool_2d(x, ws, ignore_border=True, st=st, padding=pad, mode=mode)
    f_cpu = theano.function([x], out_cpu, mode=mode_without_gpu2)
    assert not any([isinstance(node.op, cuda.dnn.GpuDnnPool)
                   for node in f_cpu.maker.fgraph.apply_nodes])
    assert any([isinstance(node.op, Pool)
                for node in f_cpu.maker.fgraph.apply_nodes])

    i = 1
    for shp in [(1, 10, 100, 100),
                (1, 3, 99, 99),
                (32, 1, 147, 197)]:
        data = numpy.random.normal(0, 1, shp).astype("float32")

        # Change the window size dynamically
        ws.set_value(numpy.array([i, i]).astype('int32'))
        a = f_gpu(data).__array__()
        b = f_cpu(data).__array__()
        utt.assert_allclose(a, b)
        i += 1
项目:kaggle-quora-solution-8th    作者:qqgeogor    | 项目源码 | 文件源码
def __init__(self,input,
               filter_shape,   # 2*3*3
               input_shape,
               pool_size=(2,2)):
        """

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                                      filter height, filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                                     image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows, #cols)
        """
        #print input_shape
        #print filter_shape
        #assert input_shape[1] == filter_shape[1]

        self.input=input
        fan_in=np.prod(filter_shape[1:])

        fan_out=(filter_shape[0]*np.prod(filter_shape[2:])/
                 np.prod(pool_size))


        init_W=np.asarray(np.random.uniform(low=-np.sqrt(6./(fan_in+fan_out)),
                                      high=np.sqrt(6./(fan_in+fan_out)),
                                      size=filter_shape),
                          dtype=theano.config.floatX)
        self.W=theano.shared(value=init_W,name='W',borrow=True)

        init_b=np.zeros((filter_shape[0],),dtype=theano.config.floatX)
        self.b=theano.shared(value=init_b,borrow=True)

        conv_out=T.nnet.conv2d(
            input=input,
            filters=self.W,
            filter_shape=filter_shape,
            input_shape=input_shape
        )
        pool_out=pool.pool_2d(
            input=conv_out,
            ws=pool_size,
            ignore_border=True)

        self.activation=T.tanh(pool_out+self.b.dimshuffle('x',0,'x','x')).flatten(2)
        self.params=[self.W,self.b]
项目:InnerOuterRNN    作者:Chemoinformatics    | 项目源码 | 文件源码
def pool3d(x, pool_size, strides=(1, 1, 1), border_mode='valid',
           dim_ordering='th', pool_mode='max'):
    if border_mode == 'same':
        # TODO: add implementation for border_mode="same"
        raise Exception('border_mode="same" not supported with Theano.')
    elif border_mode == 'valid':
        ignore_border = True
        padding = (0, 0)
    else:
        raise Exception('Invalid border mode: ' + str(border_mode))

    if dim_ordering not in {'th', 'tf'}:
        raise Exception('Unknown dim_ordering ' + str(dim_ordering))

    if dim_ordering == 'tf':
        x = x.dimshuffle((0, 4, 1, 2, 3))

    if pool_mode == 'max':
        # pooling over conv_dim2, conv_dim1 (last two channels)
        output = pool.pool_2d(input=x.dimshuffle(0, 1, 4, 3, 2),
                              ds=(pool_size[1], pool_size[0]),
                              st=(strides[1], strides[0]),
                              ignore_border=ignore_border,
                              padding=padding,
                              mode='max')

        # pooling over conv_dim3
        pool_out = pool.pool_2d(input=output.dimshuffle(0, 1, 4, 3, 2),
                                ds=(1, pool_size[2]),
                                st=(1, strides[2]),
                                ignore_border=ignore_border,
                                padding=padding,
                                mode='max')

    elif pool_mode == 'avg':
        # pooling over conv_dim2, conv_dim1 (last two channels)
        output = pool.pool_2d(input=x.dimshuffle(0, 1, 4, 3, 2),
                              ds=(pool_size[1], pool_size[0]),
                              st=(strides[1], strides[0]),
                              ignore_border=ignore_border,
                              padding=padding,
                              mode='average_exc_pad')

        # pooling over conv_dim3
        pool_out = pool.pool_2d(input=output.dimshuffle(0, 1, 4, 3, 2),
                                ds=(1, pool_size[2]),
                                st=(1, strides[2]),
                                ignore_border=ignore_border,
                                padding=padding,
                                mode='average_exc_pad')
    else:
        raise Exception('Invalid pooling mode: ' + str(pool_mode))

    if dim_ordering == 'tf':
        pool_out = pool_out.dimshuffle((0, 2, 3, 4, 1))
    return pool_out


# RANDOMNESS
项目:odin_old    作者:trungnt13    | 项目源码 | 文件源码
def pool3d(x, pool_size, strides=(1, 1, 1), border_mode='valid',
           dim_ordering='th', pool_mode='max'):
    # ====== dim ordering ====== #
    if dim_ordering not in {'th', 'tf'}:
        raise Exception('Unknown dim_ordering ' + str(dim_ordering))
    if dim_ordering == 'tf':
        x = x.dimshuffle((0, 4, 1, 2, 3))
    # ====== border mode ====== #
    if border_mode == 'same':
        w_pad = pool_size[0] - 2 if pool_size[0] % 2 == 1 else pool_size[0] - 1
        h_pad = pool_size[1] - 2 if pool_size[1] % 2 == 1 else pool_size[1] - 1
        d_pad = pool_size[2] - 2 if pool_size[2] % 2 == 1 else pool_size[2] - 1
        padding = (w_pad, h_pad, d_pad)
    elif border_mode == 'valid':
        padding = (0, 0, 0)
    elif isinstance(border_mode, (tuple, list)):
        padding = tuple(border_mode)
    else:
        raise Exception('Invalid border mode: ' + str(border_mode))
    # ====== pooling ====== #
    if _on_gpu() and dnn.dnn_available():
        pool_out = dnn.dnn_pool(x, pool_size,
                                stride=strides,
                                mode=pool_mode,
                                pad=padding)
    else:
        padding = padding[:2]
        # pooling over conv_dim2, conv_dim1 (last two channels)
        output = pool.pool_2d(input=x.dimshuffle(0, 1, 4, 3, 2),
                              ds=(pool_size[1], pool_size[0]),
                              st=(strides[1], strides[0]),
                              ignore_border=True,
                              padding=padding,
                              mode=pool_mode)
        # pooling over conv_dim3
        pool_out = pool.pool_2d(input=output.dimshuffle(0, 1, 4, 3, 2),
                                ds=(1, pool_size[2]),
                                st=(1, strides[2]),
                                ignore_border=True,
                                padding=padding,
                                mode=pool_mode)
    # ====== output ====== #
    if dim_ordering == 'tf':
        pool_out = pool_out.dimshuffle((0, 2, 3, 4, 1))
    return pool_out

# ===========================================================================
# RANDOMNESS
# ===========================================================================