python类pool_2d()的实例源码

san_att_conv_twolayer_theano.py 文件源码 项目:imageqa-san 作者: zcyang 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def maxpool_layer(shared_params, x, maxpool_shape, options):
    return pool.pool_2d(x, maxpool_shape, ignore_border=False)
theano_backend.py 文件源码 项目:InnerOuterRNN 作者: Chemoinformatics 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
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
layers.py 文件源码 项目:aed-by-cnn 作者: tweihaha 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
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
layers.py 文件源码 项目:IQA_BIECON_release 作者: jongyookim 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
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
nn_layers.py 文件源码 项目:CQA-CNN 作者: 3141bishwa 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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)
theano_backend.py 文件源码 项目:odin_old 作者: trungnt13 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
poollayer.py 文件源码 项目:deep-prior 作者: moberweger 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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)
theano_layers.py 文件源码 项目:deer 作者: VinF 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
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]
theano_backend.py 文件源码 项目:keraflow 作者: ipod825 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
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]]
pool.py 文件源码 项目:lemontree 作者: khshim 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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
theano_backend.py 文件源码 项目:reading-text-in-the-wild 作者: mathDR 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
poollayer.py 文件源码 项目:deep-prior-pp 作者: moberweger 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
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)
convpool.py 文件源码 项目:lmkit 作者: jiangnanhugo 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
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]
theano_backend.py 文件源码 项目:keras_superpixel_pooling 作者: parag2489 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
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
test_dnn.py 文件源码 项目:Theano-Deep-learning 作者: GeekLiB 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
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
test_dnn.py 文件源码 项目:Theano-Deep-learning 作者: GeekLiB 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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
convpool.py 文件源码 项目:kaggle-quora-solution-8th 作者: qqgeogor 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
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]
theano_backend.py 文件源码 项目:InnerOuterRNN 作者: Chemoinformatics 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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
theano_backend.py 文件源码 项目:odin_old 作者: trungnt13 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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
# ===========================================================================


问题


面经


文章

微信
公众号

扫码关注公众号