Exemple #1
0
    def layer(self, input_layer, pool_dims, mode='max', implementation='fast'):
        from lasagne.layers import SpatialPyramidPoolingLayer

        if implementation != 'kaiming':
            try:
                import theano.tensor as T
                from lasagne.layers.pool import pool_2d
                pool_2d(T.tensor4(),
                        ws=T.ivector(),
                        stride=T.ivector(),
                        ignore_border=True,
                        pad=None)
            except ValueError:
                pytest.skip('Old theano version')

        return SpatialPyramidPoolingLayer(input_layer,
                                          pool_dims=pool_dims,
                                          mode=mode,
                                          implementation=implementation)
Exemple #2
0
    def layer(self, input_layer, pool_dims, mode='max', implementation='fast'):
        from lasagne.layers import SpatialPyramidPoolingLayer

        if implementation != 'kaiming':
            try:
                import theano.tensor as T
                from lasagne.layers.pool import pool_2d
                pool_2d(T.tensor4(),
                        ws=T.ivector(),
                        stride=T.ivector(),
                        ignore_border=True,
                        pad=None)
            except ValueError:
                pytest.skip('Old theano version')

        return SpatialPyramidPoolingLayer(input_layer,
                                          pool_dims=pool_dims,
                                          mode=mode,
                                          implementation=implementation)
Exemple #3
0
    def get_output_for(self, input, **kwargs):
        #Add one additional dimension in the rigth and give the 5d tensor for the
        #pooling. the 2d pooling will do the polling in the last 2 dimensiotns
        #the rows and the new additional one. then we kick it out in the return [:,:,:,:,0]
        input_5d = T.shape_padright(input, 1)
        pool=pool_2d(input_5d,
                         ws=(self.pool_size[0], 1),
                         stride=(self.stride[0], 1),
                         ignore_border=self.ignore_border,
                         pad=(self.pad[0], 0),
                         mode=self.mode,
                         )

        return pool[:,:, :, :, 0]
Exemple #4
0
    def get_output_for(self, input, **kwargs):
        """ Uses pool_2d to pool each dimension."""
        input_shape = input.shape
        n = self.n
        if n % 2 == 1:
            n += 1
            input = T.shape_padright(input, 1)

        # aggregate axis for clearer code?
        n_axis = input.ndim - n
        # map_shape = input.shape[axis:]
        # new_shape = T.cast(T.join(0, T.as_tensor([-1]), map_shape), 'int64')
        # input = T.reshape(input, new_shape, n+1)

        # loop  reshape -> pool for n trailing axis
        for i in np.arange(0, n, 2):

            # extract parameters for the corresponding axes
            i1 = (n - 2 + i) % n
            i2 = (n - 1 + i) % n

            # pool last 2 axis
            input = pool.pool_2d(
                input,
                ds=(self.pool_size[i1], self.pool_size[i2]),
                st=(self.stride[i1], self.stride[i2]),
                ignore_border=self.ignore_border,
                padding=(self.pad[i1], self.pad[i2]),
                mode=self.mode,
            )

            # Get next permutation, which shifts by 2 (+1 is for first axis)
            fixed = tuple(np.arange(n_axis))
            perm = tuple((np.arange(2, n + 2) % n) + n_axis)

            # include the first axis from input
            shuffle = fixed + perm

            # shuffle
            input = input.dimshuffle(shuffle)

        # restore original shape
        input = input.reshape(self.get_output_shape_for(input_shape))

        return input
    def get_output_for(self, input, **kwargs):
        """ Uses pool_2d to pool each dimension."""
        input_shape = input.shape
        n = self.n
        if n % 2 == 1:
            n += 1
            input = T.shape_padright(input, 1)

        # aggregate axis for clearer code?
        n_axis = input.ndim - n
        # map_shape = input.shape[axis:]
        # new_shape = T.cast(T.join(0, T.as_tensor([-1]), map_shape), 'int64')
        # input = T.reshape(input, new_shape, n+1)

        # loop  reshape -> pool for n trailing axis
        for i in np.arange(0, n, 2):

            # extract parameters for the corresponding axes
            i1 = (n-2 + i) % n
            i2 = (n-1 + i) % n

            # pool last 2 axis
            input = pool.pool_2d(input,
                                 ds=(self.pool_size[i1], self.pool_size[i2]),
                                 st=(self.stride[i1], self.stride[i2]),
                                 ignore_border=self.ignore_border,
                                 padding=(self.pad[i1], self.pad[i2]),
                                 mode=self.mode, )

            # Get next permutation, which shifts by 2 (+1 is for first axis)
            fixed = tuple(np.arange(n_axis))
            perm = tuple((np.arange(2, n+2) % n) + n_axis)

            # include the first axis from input
            shuffle = fixed + perm

            # shuffle
            input = input.dimshuffle(shuffle)

        # restore original shape
        input = input.reshape(self.get_output_shape_for(input_shape))

        return input