def feature_extractor(input_data):
        # conv stage 0 (64x64=>32x32)
        h0_0 = dnn_conv(input_data, conv_w0_0, border_mode=(1, 1)) + conv_b0_0.dimshuffle("x", 0, "x", "x")
        h0_1 = dnn_conv(relu(h0_0), conv_w0_1, border_mode=(1, 1)) + conv_b0_1.dimshuffle("x", 0, "x", "x")
        h0 = dnn_pool(relu(h0_1), ws=(2, 2), stride=(2, 2))
        # conv stage 1 (32x32=>16x16)
        h1_0 = dnn_conv(h0, conv_w1_0, border_mode=(1, 1)) + conv_b1_0.dimshuffle("x", 0, "x", "x")
        h1_1 = dnn_conv(relu(h1_0), conv_w1_1, border_mode=(1, 1)) + conv_b1_1.dimshuffle("x", 0, "x", "x")
        h1 = dnn_pool(relu(h1_1), ws=(2, 2), stride=(2, 2))
        # conv stage 2 (16x16=>8x8)
        h2_0 = dnn_conv(h1, conv_w2_0, border_mode=(1, 1)) + conv_b2_0.dimshuffle("x", 0, "x", "x")
        h2_1 = dnn_conv(relu(h2_0), conv_w2_1, border_mode=(1, 1)) + conv_b2_1.dimshuffle("x", 0, "x", "x")
        h2_2 = dnn_conv(relu(h2_1), conv_w2_2, border_mode=(1, 1)) + conv_b2_2.dimshuffle("x", 0, "x", "x")
        h2 = dnn_pool(relu(h2_2), ws=(2, 2), stride=(2, 2))
        # conv stage 3 (8x8=>4x4)
        h3_0 = dnn_conv(h2, conv_w3_0, border_mode=(1, 1)) + conv_b3_0.dimshuffle("x", 0, "x", "x")
        h3_1 = dnn_conv(relu(h3_0), conv_w3_1, border_mode=(1, 1)) + conv_b3_1.dimshuffle("x", 0, "x", "x")
        h3_2 = dnn_conv(relu(h3_1), conv_w3_2, border_mode=(1, 1)) + conv_b3_2.dimshuffle("x", 0, "x", "x")
        h3 = dnn_pool(relu(h3_2), ws=(2, 2), stride=(2, 2))
        # conv stage 4 (4x4=>2x2)
        h4_0 = dnn_conv(h3, conv_w4_0, border_mode=(1, 1)) + conv_b4_0.dimshuffle("x", 0, "x", "x")
        h4_1 = dnn_conv(relu(h4_0), conv_w4_1, border_mode=(1, 1)) + conv_b4_1.dimshuffle("x", 0, "x", "x")
        h4_2 = dnn_conv(relu(h4_1), conv_w4_2, border_mode=(1, 1)) + conv_b4_2.dimshuffle("x", 0, "x", "x")
        h4 = dnn_pool(relu(h4_2), ws=(2, 2), stride=(2, 2))

        return T.flatten(h4, 2)
예제 #2
0
    def apply(self, input_):
        """Apply the pooling (subsampling) transformation.

        Parameters
        ----------
        input_ : :class:`~tensor.TensorVariable`
            An tensor with dimension greater or equal to 3. The last two
            dimensions will be downsampled. For example, with videos this
            means that the last three dimensions should represent the
            duration, height and width of your image.

        Returns
        -------
        output : :class:`~tensor.TensorVariable`
            A tensor with the same number of dimensions as `input_`, but
            with the last two dimensions downsampled.

        """
        if self.pooling_size == (1, 1, 1):
            return input_
        # Pooling on last two dimensions
        input__shape = input_.shape
        input_ = input_.reshape((input__shape[0], input__shape[1] * input__shape[2], input__shape[3], input__shape[4]))
        p = dnn_pool(img=input_, ws=tuple(self.pooling_size[1:]), stride=tuple(self.step[1:]))
        p_shape = p.shape
        p = p.reshape((p_shape[0], input__shape[1], input__shape[2], p_shape[2], p_shape[3]))
        # Pooling on first dimension
        p_shape = p.shape
        p = p.reshape((p_shape[0], p_shape[1], p_shape[2], p_shape[3] * p_shape[4]))
        output = dnn_pool(img=p, ws=(self.pooling_size[0], 1), stride=(self.step[0], 1))
        output_shape = output.shape
        output = output.reshape((output_shape[0], output_shape[1], output_shape[2], p_shape[3] , p_shape[4]))
        return output
예제 #3
0
def dnn_pool3d2d(inputs, pool_shape, pool_stride, image_shape, mode='max'):
    """ Pool first all time-slices, so 2d-poolings over width and height.
    Then do a 1dpooling over the time (done as fake2d pooling with pooling shape
    1 for the ignored dimension."""
    for i in xrange(3):
        assert pool_shape[i] <= image_shape[i], ("pool shape should be less"
            " or equal than image shape, {:d} > {:d} for "
            "pool_shape: {:s}, image_shape:{:s}").format(pool_shape[i],
                image_shape[i], pool_shape, image_shape)
    output_shape = [((image_shape[i] - pool_shape[i]) // pool_stride[i]) + 1 
        for i in xrange(3)]
    output2d_pooled = gpu_alloc_empty(inputs.shape[0], inputs.shape[1],
        output_shape[0], output_shape[1], image_shape[2])
    for z in range(image_shape[2]):
        pooled_slice = dnn_pool(inputs[:,:,:,:,z], ws=pool_shape[0:2], 
            stride=pool_stride[0:2], mode=mode)
        output2d_pooled = T.set_subtensor(output2d_pooled[:,:,:,:,z], pooled_slice)
    
    
    # now 1d-pool over last dimension...
    # could use first or second dimension as input of pool1d..
    # compute maximum y index after first pooling
    output = gpu_alloc_empty(inputs.shape[0], inputs.shape[1],
        output_shape[0], output_shape[1], output_shape[2])
    max_y = output_shape[1]
    for y in range(max_y):
        # ignore first=0 dimension, alrdy pooled in loop before
        # so set stride and shape to 1 there
        final_pooled_slice = dnn_pool(output2d_pooled[:,:,:,y,:], 
            ws=(1, pool_shape[2]), 
            stride=(1, pool_stride[2]), mode=mode)
        output = T.set_subtensor(output[:,:,:,y,:], final_pooled_slice)

    return output
    
예제 #4
0
 def __init__(self,h_low,h_high,method="adascan_mod"):
     kern = build_filters(h_low,h_high)    
     sharedKern = theano.shared(kern,name='sharedKern')
     input = theano.tensor.tensor4(name='input')
     self.conv_fun   = theano.function([input],dnn_conv(input,sharedKern))
     self.down_x = theano.function([input],dnn_pool(input,(2,1),stride=(2,1),mode='average_inc_pad'))
     self.down_y = theano.function([input],dnn_pool(input,(1,2),stride=(1,2),mode='average_inc_pad'))
     self.h_low, self.h_high, self.method = h_low, h_high, method
예제 #5
0
 def __call__(self, x):
     if self.glob:
         return dnn.dnn_pool(x, (x.shape[2], x.shape[3]),
                             stride=(1, 1),
                             mode=self.mode,
                             pad=(0, 0))
     return dnn.dnn_pool(x, (self.size, 1),
                         stride=(self.stride, 1),
                         mode=self.mode,
                         pad=(self.pad, 0))
예제 #6
0
def test_dnn_pool_desc_merge():
    if not cuda.dnn.dnn_available():
        raise SkipTest(cuda.dnn.dnn_available.msg)

    x = theano.tensor.ftensor4("x")
    y = dnn.dnn_pool(x, (2, 2))
    z = dnn.dnn_pool(x, (2, 2))
    f = theano.function([x], [y, z])
    descs = [n for n in f.maker.fgraph.apply_nodes if isinstance(n.op, dnn.GpuDnnPoolDesc)]
    assert len(descs) == 1, f.maker.fgraph
예제 #7
0
def model(X,
    h2_u, h3_u,
    h2_s, h3_s,
    w, w2, g2, b2, w3, g3, b3, wy
    ):
    h = lrelu(dnn_conv(X, w, subsample=(2, 2), border_mode=(2, 2)))
    h2 = lrelu(batchnorm(dnn_conv(h, w2, subsample=(2, 2), border_mode=(2, 2)), g=g2, b=b2, u=h2_u, s=h2_s))
    h3 = lrelu(batchnorm(dnn_conv(h2, w3, subsample=(2, 2), border_mode=(2, 2)), g=g3, b=b3, u=h3_u, s=h3_s))
    h = T.flatten(dnn_pool(h, (4, 4), (4, 4), mode='max'), 2)
    h2 = T.flatten(dnn_pool(h2, (2, 2), (2, 2), mode='max'), 2)
    h3 = T.flatten(dnn_pool(h3, (1, 1), (1, 1), mode='max'), 2)
    f = T.concatenate([h, h2, h3], axis=1)
    return [f]
예제 #8
0
def pool2d(x,
           pool_size,
           strides=(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, 3, 1, 2))

    if pool_mode == 'max':
        if _on_gpu() and dnn.dnn_available():
            pool_out = dnn_pool(x, pool_size, stride=strides, mode='max')
        else:
            pool_out = downsample.max_pool_2d(x,
                                              ds=pool_size,
                                              st=strides,
                                              ignore_border=ignore_border,
                                              padding=padding,
                                              mode='max')
    elif pool_mode == 'avg':
        if _on_gpu() and dnn.dnn_available():
            pool_out = dnn_pool(x,
                                pool_size,
                                stride=strides,
                                mode='average_exc_pad')
        else:
            pool_out = downsample.max_pool_2d(x,
                                              ds=pool_size,
                                              st=strides,
                                              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, 1))
    return pool_out
예제 #9
0
def pool2d(x, pool_size, strides=(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, 3, 1, 2))

    if pool_mode == 'max':
        if _on_gpu() and dnn.dnn_available():
            pool_out = dnn_pool(x,
                                pool_size,
                                stride=strides,
                                mode='max')
        else:
            pool_out = downsample.max_pool_2d(x,
                                              ds=pool_size,
                                              st=strides,
                                              ignore_border=ignore_border,
                                              padding=padding,
                                              mode='max')
    elif pool_mode == 'avg':
        if _on_gpu() and dnn.dnn_available():
            pool_out = dnn_pool(x,
                                pool_size,
                                stride=strides,
                                mode='average_exc_pad')
        else:
            pool_out = downsample.max_pool_2d(x,
                                              ds=pool_size,
                                              st=strides,
                                              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, 1))
    return pool_out
예제 #10
0
    def apply(self, input_):
        """Apply the pooling (subsampling) transformation.
        """
        if self.pooling_size == (1, 1, 1):
            return input_

        # Pooling on last two dimensions
        input_ = input_.reshape((input_.shape[0], input_.shape[1] * input_.shape[2], input_.shape[3], input_.shape[4]))
        p = dnn_pool(img=input_, ws=tuple(self.pooling_size[1:]), stride=tuple(self.step[1:]))
        p = p.reshape((p.shape[0], input_.shape[1], input_.shape[2], p.shape[2], p.shape[3]))
        # Pooling on first dimension
        p = p.reshape((p.shape[0], p.shape[1], p.shape[2], p.shape[3] * p.shape[4]))
        output = dnn_pool(img=p, ws=(self.pooling_size[0], 1), stride=(self.step[0], 1))
        output = output.reshape((output.shape[0], output.shape[1], output.shape[2], p.shape[3], p.shape[4]))
        return output
예제 #11
0
 def apply(self, input):
     """
     Apply this discriminator module to the given input. This produces a
     collection of filter responses for feedforward and a spatial grid of
     discriminator outputs.
     """
     bm = int((self.filt_dim - 1) / 2) # use "same" mode convolutions
     ss = self.ds_stride               # stride for "learned downsampling"
     # apply first conv layer
     h1 = dnn_conv(input, self.w1, subsample=(1, 1), border_mode=(bm, bm))
     if self.apply_bn_1:
         h1 = batchnorm(h1, g=self.g1, b=self.b1)
     h1 = lrelu(h1)
     # apply second conv layer (may include downsampling)
     if self.use_pooling:
         h2 = dnn_conv(h1, self.w2, subsample=(1, 1), border_mode=(bm, bm))
         if self.apply_bn_2:
             h2 = batchnorm(h2, g=self.g2, b=self.b2)
         h2 = lrelu(h2)
         h2 = dnn_pool(h2, (ss,ss), stride=(ss, ss), mode='max', pad=(0, 0))
     else:
         h2 = dnn_conv(h1, self.w2, subsample=(ss, ss), border_mode=(bm, bm))
         if self.apply_bn_2:
             h2 = batchnorm(h2, g=self.g2, b=self.b2)
         h2 = lrelu(h2)
     
     # apply discriminator layer
     y = dnn_conv(h2, self.wd, subsample=(1, 1), border_mode=(bm, bm))
     y = sigmoid(T.flatten(y, 2)) # flatten to (batch_size, num_preds)
     return h2, y
예제 #12
0
파일: dnn.py 프로젝트: rewonc/treeano
    def compute_output(self, network, in_vw):
        mode = network.find_hyperparameter(["mode"])
        pool_size = network.find_hyperparameter(["pool_size"])
        dim = len(pool_size)
        # works for sizes 2 and 3
        assert dim in [2, 3]
        stride = network.find_hyperparameter(["pool_stride",
                                              "stride"],
                                             None)
        if stride is None:
            stride = pool_size
        pad = network.find_hyperparameter(["pool_pad", "pad"], (0,) * dim)
        assert dim == len(stride) == len(pad)
        if dim == 2:
            pool_axes = (2, 3)
        elif dim == 3:
            pool_axes = (2, 3, 4)
        out_shape = downsample.pool_output_shape(
            input_shape=in_vw.shape,
            axes=pool_axes,
            pool_shape=pool_size,
            strides=stride,
            pads=pad)
        out_var = dnn.dnn_pool(img=in_vw.variable,
                               ws=pool_size,
                               stride=stride,
                               pad=pad,
                               mode=mode)

        network.create_vw(
            "default",
            variable=out_var,
            shape=out_shape,
            tags={"output"},
        )
예제 #13
0
파일: dnn.py 프로젝트: shaoxuan92/treeano
    def compute_output(self, network, in_vw):
        mode = network.find_hyperparameter(["mode"])
        pool_size = network.find_hyperparameter(["pool_size"])
        dim = len(pool_size)
        # works for sizes 2 and 3
        assert dim in [2, 3]
        stride = network.find_hyperparameter(["pool_stride",
                                              "stride"],
                                             None)
        if stride is None:
            stride = pool_size
        pad = network.find_hyperparameter(["pool_pad", "pad"], (0,) * dim)
        assert dim == len(stride) == len(pad)
        if dim == 2:
            pool_axes = (2, 3)
        elif dim == 3:
            pool_axes = (2, 3, 4)
        out_shape = downsample.pool_output_shape(
            input_shape=in_vw.shape,
            axes=pool_axes,
            pool_shape=pool_size,
            strides=stride,
            pads=pad)
        out_var = dnn.dnn_pool(img=in_vw.variable,
                               ws=pool_size,
                               stride=stride,
                               pad=pad,
                               mode=mode)

        network.create_vw(
            "default",
            variable=out_var,
            shape=out_shape,
            tags={"output"},
        )
예제 #14
0
    def get_fprop(self, video, video_mask, label, popstats=None):
        if self.options.rec_bn and self.options.use_popstats:
            popstats_enc = popstats
        else:
            popstats_enc = None

        ''' Encoder '''
        m, t, x, y = video.shape
        state_vid = video.dimshuffle(1,0,2,3,4)
        state_mask = video_mask.dimshuffle(1,0)
        for l in xrange(0, len(self.encoders)):
            ### Conv RNN
            rvals = self.layers[0].fprop(state_vid, state_mask, popstats=popstats_enc)
            state_vid = rvals[0]
            ### Spatial Pool
            if self.options.layer_pool[0] != (0, 0):
                state_vid = dnn_pool(state_vid.reshape((t*m,x, y)),
                                    size=self.options.pool[0], stride=self.options.pool_stride[0]))
                state_vid = state_vid.reshape(t, m, x, y)


        ''' Classifier '''
        video_ctx = state_vid[0].flatten(2) # (m,d)
        output = T.dot(logit, self.W) + self.b
        y_hat = T.nnet.softmax(output)

        '''cost'''
        ce = tensor.nnet.categorical_crossentropy(y_hat, label)
        cost = ce.mean()

        ### Returns Err as well
        return [cost, ce]
예제 #15
0
    def get_convpool(self, img, kerns, conv_b, subsample, border_mode, pooling, ws=None, stride=None, normalizing=False):

        conv_out = dnn.dnn_conv(
                img=img,
                kerns=kerns,
                subsample=subsample,
                border_mode=border_mode
                )

        conv_out += conv_b.dimshuffle('x',0,'x','x')
        conv_out = T.maximum(conv_out, 0)

        if pooling:
            pool_out = dnn.dnn_pool(
                    conv_out,
                    ws=ws,
                    stride=stride
                    )
        else:
            pool_out = conv_out

        if normalizing:
            norm_out = CrossChannelNormalization()(pool_out)
        else:
            norm_out = pool_out
        return norm_out
예제 #16
0
    def get_output_for(self, input, **kwargs):
        dimshuffle = range(len(self.input_shape))
        for axis in self.pool_axes:
            dimshuffle.remove(axis)
        for axis in self.pool_axes:
            dimshuffle.append(axis)

        new_shape = [
            -1,
            1,
        ] + [self.input_shape[axis] for axis in self.pool_axes]

        input = input.dimshuffle(*dimshuffle).reshape(new_shape)

        pooled = dnn.dnn_pool(input, self.pool_size, self.stride, self.mode,
                              self.pad)

        non_pool_output_shape = list(self.input_shape)
        non_pool_output_shape = [
            i for j, i in enumerate(non_pool_output_shape)
            if j not in self.pool_axes
        ]

        pooled = pooled.reshape(
            replace_None(non_pool_output_shape +
                         [self.output_shape[i] for i in self.pool_axes]))

        reverse_dimshuffle = [
            dimshuffle.index(i) for i in xrange(len(self.input_shape))
        ]
        pooled = pooled.dimshuffle(*reverse_dimshuffle)
        return pooled
예제 #17
0
파일: Conv1D.py 프로젝트: caomw/MLFun
    def apply(self, input_):
        """Apply the pooling (subsampling) transform

        Parameters
        ----------
        input_ : :class:`~tensor.TensorVariable`
            3D tensor with axes batch size, sequence, features

        Returns
        -------
        output: :class:`~tensor.TensorVariable`
            3D tensor with axes batch size, sequence, features
        """
        shuffled = input_.dimshuffle(0, 2, 1, 'x')

        # batch_size, num_filters, x_map, 1
        if self.step == None:
            st = (1,1)
        else:
            st = (self.step, 1)
        #output = max_pool_2d(shuffled, (self.pooling_length, 1), st=st)
        output = dnn_pool(shuffled, (self.pooling_length, 1), stride=st)

        sequence_out = output[:, :, :, 0].dimshuffle(0, 2, 1)

        return sequence_out
예제 #18
0
def spp_predict(fmaps, pyramid):
    """ From input confidence maps, perform "SPP" prediction across a scale pyramid and using
        spatial pruning of labels and confidences.

    Arguments:
        fmaps
            theano symbolic 4D tensor with shape (nb_images, nb_labels, nb_rows, nb_cols)
        pyramid
            python list of average pooling kernel sizes, e.g. [3, 5].
    Returns:
        symbolic (nb_images, nb_labels) tensor of spatially pooled multi-scale predictions.
    """
    # Step 1: average pooling of the confidences across multiple scales, then average pooling
    # of that using spatial information to get multi-scale spatial confidences.
    pooled_maps = fmaps
    nb_images, nb_labels, nb_rows, nb_cols = fmaps.shape
    
    for ws in pyramid:
        pooled_maps += resize(
            dnn_pool(fmaps, (ws, ws), (1, 1), mode='average'),
            (nb_rows, nb_cols)
        )
    pooled_maps /= len(pyramid) + 1
    # Step 2: spatial max-pooling across labels.
    label_conf, label_map = T.max_and_argmax(pooled_maps, axis=1, keepdims=True)
    bcast_labels = T.addbroadcast(T.arange(nb_labels).reshape([1, nb_labels, 1, 1]), 0, 2, 3)
    label_mask = T.eq(bcast_labels, label_map)

    return T.mean(label_mask * label_conf, axis=[2,3])
예제 #19
0
    def output(self, input):
        '''
        dnn
        '''
        if self.border_mode == 'same':
            conv_out = dnn.dnn_conv(
                img=input,
                kerns=self.W,
                subsample=(1, 1),
                border_mode=self.border,
                #conv_mode='cross'
            )
        else:
            raise Exception('Unknown conv type')

        # downsample each feature map individually, using maxpooling

        if self.poolsize[0] == 1 and self.poolsize[1] == 1:
            pooled_out = conv_out
        else:
            pooled_out = dnn.dnn_pool(img=conv_out,
                                      ws=self.poolsize,
                                      stride=self.poolsize,
                                      mode='max',
                                      pad=(0, 0))

        # 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
        lin_output = pooled_out + self.b.dimshuffle('x', 0, 'x', 'x')
        return (lin_output
                if self.activation is None else self.activation(lin_output))
예제 #20
0
    def apply(self, input_):
        """Apply the pooling (subsampling) transform

        Parameters
        ----------
        input_ : :class:`~tensor.TensorVariable`
            3D tensor with axes batch size, sequence, features

        Returns
        -------
        output: :class:`~tensor.TensorVariable`
            3D tensor with axes batch size, sequence, features
        """
        shuffled = input_.dimshuffle(0, 2, 1, 'x')

        # batch_size, num_filters, x_map, 1
        if self.step == None:
            st = (1, 1)
        else:
            st = (self.step, 1)
        #output = max_pool_2d(shuffled, (self.pooling_length, 1), st=st)
        output = dnn_pool(shuffled, (self.pooling_length, 1), stride=st)

        sequence_out = output[:, :, :, 0].dimshuffle(0, 2, 1)

        return sequence_out
예제 #21
0
    def __init__(self,
                 input,
                 poolsize,
                 poolstride,
                 poolpad,
                 mode='max',
                 printinfo=True,
                 input_shape=None,
                 output_shape=None):

        self.get_input_shape(input, input_shape)
        self.poolsize = poolsize
        self.poolstride = poolstride
        self.poolpad = poolpad

        if self.poolsize != 1:
            self.output = dnn.dnn_pool(self.input,
                                       ws=(poolsize, poolsize),
                                       stride=(poolstride, poolstride),
                                       mode=mode,
                                       pad=(poolpad, poolpad))
        else:
            self.output = input

        if output_shape:
            self.output_shape = output_shape
        else:
            self.output_shape = self.get_output_shape(self.input_shape)

        self.name = 'Pool\t'
        if printinfo: self.print_shape()
예제 #22
0
    def __init__(self, inputs=None, size=(1, 1), stride=None, pad=(0, 0), mode='max', ignore_border=True):
        """
        Parameters
        ----------
        inputs : tuple(shape, `Theano.TensorType`)
            tuple(shape, `Theano.TensorType`) or None describing the input to use for this layer.
            `shape` will be a monad tuple representing known sizes for each dimension in the `Theano.TensorType`.
            If 4D images as input, expect formatted as (batch_size, #channels, rows, cols).
        size : tuple(int) or int
            Downsample factor over (rows, columns). If it is an int, it will be the same size for rows and cols.
        stride : tuple(int) or int
            Stride size (step size), which is the number of shifts over rows/cols to get the
            next pool region. If it is an int, it will be the same size for rows and cols.
        pad : tuple(int) or int
            (pad_h, pad_w), pad zeros to extend beyond four borders
            of the images, pad_h is the size of the top and bottom margins,
            and pad_w is the size of the left and right margins. If it is an int, it will be the same
            size for rows and cols.
        mode : 'max', 'sum', 'average_inc_pad', 'average_exc_pad'
            Operation executed on each window. `max` and `sum` always exclude
            the padding in the computation. `average` gives you the choice to
            include or exclude it.
        ignore_border : bool
            If `size` doesn't divide the input `shape`, do we include an extra row/col of
            partial downsampling (False) or ignore it (True). When True, (5,5) input with size=(2,2)
            will generate a (2,2) output. (3,3) otherwise.
        """
        super(Pool2D, self).__init__(inputs=inputs, size=size, stride=stride, pad=pad)
        input_shape, self.input = self.inputs[0]
        if isinstance(size, int):
            size = (size, ) * 2
        if stride is None:
            stride = size
        if isinstance(stride, int):
            stride = (stride, ) * 2
        if isinstance(pad, int):
            pad = (pad, ) * 2

        assert len(size) == len(stride) == len(pad), "Size, stride, and pad must have the same number of dimensions."

        self.output_size = tuple(_pool_out_size(imgshape=input_shape,
                                                ds=size,
                                                st=stride,
                                                ignore_border=ignore_border,
                                                padding=pad))

        cudnn_modes = ['max', 'average_inc_pad', 'average_exc_pad']
        if has_cudnn and mode in cudnn_modes and ignore_border and self.input.ndim == 4:
            self.output = dnn_pool(img=self.input,
                                   ws=size,
                                   stride=stride,
                                   mode=mode,
                                   pad=pad)
        else:
            self.output = max_pool_2d(input=self.input,
                                      ds=size,
                                      st=stride,
                                      padding=pad,
                                      mode=mode,
                                      ignore_border=ignore_border)
예제 #23
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
예제 #24
0
파일: pool.py 프로젝트: zhjpqq/denet
    def __init__(self,
                 layers,
                 size=(2, 2),
                 stride=None,
                 pad=(0, 0),
                 mode="max",
                 ignore_border=True,
                 json_param={}):
        super().__init__(layer_index=len(layers))

        self.input = layers[-1].output
        self.input_shape = layers[-1].output_shape

        self.size = json_param.get("size", size)
        self.pad = json_param.get("pad", pad)
        self.ignore_border = json_param.get("ignoreBorder", ignore_border)
        self.mode = json_param.get("mode", mode)
        self.stride = json_param.get("stride", stride)
        if self.stride is None:
            self.stride = self.size

        #output dim
        if self.ignore_border:
            h = int(
                math.floor(
                    (self.input_shape[2] + 2 * self.pad[0] - self.size[0]) /
                    self.stride[0])) + 1
            w = int(
                math.floor(
                    (self.input_shape[3] + 2 * self.pad[1] - self.size[1]) /
                    self.stride[1])) + 1
        else:
            h = int(
                math.ceil(
                    (self.input_shape[2] + 2 * self.pad[0]) / self.stride[0]))
            w = int(
                math.ceil(
                    (self.input_shape[3] + 2 * self.pad[1]) / self.stride[1]))

        #theano optimizer is sometimes failing to use cudnn pooling!
        use_cudnn = (dnn.dnn_available() and dnn.version() >= (4000, 4000)
                     and self.ignore_border)
        if use_cudnn:
            self.output = dnn.dnn_pool(self.input,
                                       ws=self.size,
                                       pad=self.pad,
                                       stride=self.stride,
                                       mode=self.mode)
        else:
            self.output = tensor.signal.pool.pool_2d(
                self.input,
                ds=self.size,
                padding=self.pad,
                ignore_border=self.ignore_border,
                st=self.stride,
                mode=self.mode)

        self.output_shape = (self.input_shape[0], self.input_shape[1], h, w)
        logging.verbose("Adding", self)
예제 #25
0
파일: googlenet.py 프로젝트: hma02/platoon
    def __init__(self, input, image_shape, filter_shape, convstride, padsize,
                 poolsize, poolstride,poolpad, W, b, lrn=False,
                 lib_conv='cudnn',
                 ):
        self.filter_size = filter_shape
        self.convstride = convstride
        self.padsize = padsize


        self.channel = image_shape[0]
        self.lrn = lrn
        self.lib_conv = lib_conv

        self.filter_shape = np.asarray(filter_shape)
        self.image_shape = np.asarray(image_shape)

        
        self.W = W#Weight(self.filter_shape)
        self.b = b#Weight(self.filter_shape[3])#, bias_init, std=0)
        
        input_shuffled = input.dimshuffle(3, 0, 1, 2)  # c01b to bc01
            # in01out to outin01
            # print image_shape_shuffled
            # print filter_shape_shuffled

        W_shuffled = self.W.val.dimshuffle(3, 0, 1, 2)  # c01b to bc01
        conv_out = dnn.dnn_conv(img=input_shuffled,
                                kerns=W_shuffled,
                                subsample=(convstride, convstride),
                                border_mode=padsize,
                                )
        conv_out = conv_out + self.b.val.dimshuffle('x', 0, 'x', 'x')        
        
        # ReLu
        self.output = T.maximum(conv_out, 0)
        
        # Pool
        self.poolsize = poolsize
        self.poolstride = poolstride 
        self.poolpad = poolpad      
        
        if self.poolsize != 1:
            self.output = dnn.dnn_pool(self.output,
                                       ws=(poolsize, poolsize),
                                       stride=(poolstride, poolstride),
                                       mode='max', pad=(poolpad, poolpad))

        self.output = self.output.dimshuffle(1, 2, 3, 0)  # bc01 to c01b
        
        # LRN
        if self.lrn:
            self.lrn_func = CrossChannelNormalization()
            # lrn_input = gpu_contiguous(self.output)
            self.output = self.lrn_func(self.output)
               
        self.params = [self.W.val, self.b.val]
        self.weight_type = ['W', 'b'] 
        print "conv ({}) layer with shape_in: {}".format(lib_conv,
                                                         str(image_shape))
예제 #26
0
    def __init__(self, input, poolsize, poolstride, padsize=0):

        input_shuffled = input.dimshuffle(3, 0, 1, 2)
        self.output = dnn.dnn_pool(input_shuffled,
                                   ws=(poolsize, poolsize),
                                   stride=(poolstride, poolstride),
                                   pad=(padsize, padsize))
        self.output = self.output.dimshuffle(1, 2, 3, 0)
예제 #27
0
 def apply(self, input_):
     stride = self.stride
     if not stride:
         stride = self.pooling_size
     return dnn_pool(input_,
                     ws=self.pooling_size,
                     stride=stride,
                     pad=self.pad)
 def symb_forward(self, symb_input):
     return _dnn.dnn_pool(
         img=symb_input,
         ws=(self.k_w, self.k_h),
         stride=(self.d_w, self.d_h),
         mode='max',
         pad=(self.pad_w, self.pad_h)
     )
예제 #29
0
 def grad(self, inputs, grads):
     out, inp, inp_grad, ws, stride, pad = inputs
     mode = self.mode
     if mode not in ("average_inc_pad", "average_exc_pad"):
         raise NotImplementedError("Unsupported pooling mode for grad.")
     g_out = dnn.dnn_pool(grads[0], ws, stride=stride, pad=pad, mode=mode)
     def d(): return theano.gradient.DisconnectedType()()
     return d(), d(), g_out, d(), d(), d()
예제 #30
0
 def apply(self, input_):
     stride = self.stride
     if not stride:
         stride = self.pooling_size
     inter = input_**2
     inter = dnn_pool(inter, ws=self.pooling_size, stride=stride,
                      pad=self.pad, mode='average_inc_pad')
     return T.sqrt(inter)
예제 #31
0
def classify(X, w, g, b, w2, g2, b2, w3, g3, b3, w4, g4, b4, wfc1, wfc2, wy):

    h = relu(
        batchnorm3d(dnn_conv3d(X,
                               w,
                               subsample=(1, 1, 1),
                               border_mode=(1, 1, 1)),
                    g=g,
                    b=b))
    h = dnn_pool(h, ws=(2, 2, 2), stride=(2, 2, 2), mode='max')

    h2 = relu(
        batchnorm3d(dnn_conv3d(h,
                               w2,
                               subsample=(1, 1, 1),
                               border_mode=(1, 1, 1)),
                    g=g2,
                    b=b2))
    h2 = dnn_pool(h2, ws=(2, 2, 2), stride=(2, 2, 2), mode='max')

    h3 = relu(
        batchnorm3d(dnn_conv3d(h2,
                               w3,
                               subsample=(1, 1, 1),
                               border_mode=(1, 1, 1)),
                    g=g3,
                    b=b3))
    h3 = dnn_pool(h3, ws=(2, 2, 2), stride=(2, 2, 2), mode='max')

    h4 = relu(
        batchnorm3d(dnn_conv3d(h3,
                               w4,
                               subsample=(1, 1, 1),
                               border_mode=(1, 1, 1)),
                    g=g4,
                    b=b4))
    h4 = dnn_pool(h4, ws=(2, 2, 2), stride=(2, 2, 2), mode='max')

    h5 = T.flatten(h4, 2)

    h6 = relu(T.dot(h5, wfc1))
    h7 = relu(T.dot(h6, wfc2))

    y = softmax(T.dot(h7, wy))
    return y
예제 #32
0
 def apply(self, input_):
     stride = self.stride
     if not stride:
         stride = self.pooling_size
     return dnn_pool(input_,
                     ws=self.pooling_size,
                     stride=stride,
                     pad=self.pad,
                     mode='average_inc_pad')
예제 #33
0
파일: conv3d.py 프로젝트: wavelets/tsa-rnn
 def apply(self, input_):
     """Apply the pooling (subsampling) transformation.
     """
     if self.pooling_size == (1, 1, 1):
         return input_
     # Pooling on last two dimensions
     input__shape = input_.shape
     input_ = input_.reshape((input__shape[0], input__shape[1] * input__shape[2], input__shape[3], input__shape[4]))
     p = dnn_pool(img=input_, ws=tuple(self.pooling_size[1:]), stride=tuple(self.step[1:]))
     p_shape = p.shape
     p = p.reshape((p_shape[0], input__shape[1], input__shape[2], p_shape[2], p_shape[3]))
     # Pooling on first dimension
     p_shape = p.shape
     p = p.reshape((p_shape[0], p_shape[1], p_shape[2], p_shape[3] * p_shape[4]))
     output = dnn_pool(img=p, ws=(self.pooling_size[0], 1), stride=(self.step[0], 1))
     output_shape = output.shape
     output = output.reshape((output_shape[0], output_shape[1], output_shape[2], p_shape[3] , p_shape[4]))
     return output
def model(X, h2_u, h3_u, h2_s, h3_s, w, w2, g2, b2, w3, g3, b3, wy):
    h = lrelu(dnn_conv(X, w, subsample=(2, 2), border_mode=(2, 2)))
    h2 = lrelu(
        batchnorm(dnn_conv(h, w2, subsample=(2, 2), border_mode=(2, 2)),
                  g=g2,
                  b=b2,
                  u=h2_u,
                  s=h2_s))
    h3 = lrelu(
        batchnorm(dnn_conv(h2, w3, subsample=(2, 2), border_mode=(2, 2)),
                  g=g3,
                  b=b3,
                  u=h3_u,
                  s=h3_s))
    h = T.flatten(dnn_pool(h, (4, 4), (4, 4), mode='max'), 2)
    h2 = T.flatten(dnn_pool(h2, (2, 2), (2, 2), mode='max'), 2)
    h3 = T.flatten(dnn_pool(h3, (1, 1), (1, 1), mode='max'), 2)
    f = T.concatenate([h, h2, h3], axis=1)
    return [f]
예제 #35
0
 def apply(self, input_):
     stride = self.stride
     if not stride:
         stride = self.pooling_size
     inter = input_**2
     inter = dnn_pool(inter,
                      ws=self.pooling_size,
                      stride=stride,
                      pad=self.pad,
                      mode='average_inc_pad')
     return T.sqrt(inter)
예제 #36
0
파일: layers.py 프로젝트: wufangjie/dnn
 def process(self, input, tparams, BNparams):
     b, f, h0, w0 = input.shape
     result = []
     for h, w in self.pymamid:
         win_h = T.ceil(h0 / h).astype('int32')
         win_w = T.ceil(w0 / w).astype('int32')
         str_h = T.floor(h0 / h).astype('int32')
         str_w = T.floor(w0 / w).astype('int32')
         result.append(dnn_pool(
             img=input, ws=(win_h, win_w), mode=self.mode,
             stride=(str_h, str_w), pad=(0, 0)).reshape([b, -1]))
     return T.concatenate(result, axis=1)
예제 #37
0
def dnn_pool3d2d(inputs, pool_shape, pool_stride, image_shape, mode='max'):
    """ Pool first all time-slices, so 2d-poolings over width and height.
    Then do a 1dpooling over the time (done as fake2d pooling with pooling shape
    1 for the ignored dimension."""
    for i in xrange(3):
        assert pool_shape[i] <= image_shape[i], (
            "pool shape should be less"
            " or equal than image shape, {:d} > {:d} for "
            "pool_shape: {:s}, image_shape:{:s}").format(
                pool_shape[i], image_shape[i], pool_shape, image_shape)
    output_shape = [((image_shape[i] - pool_shape[i]) // pool_stride[i]) + 1
                    for i in xrange(3)]
    output2d_pooled = gpu_alloc_empty(inputs.shape[0], inputs.shape[1],
                                      output_shape[0], output_shape[1],
                                      image_shape[2])
    for z in range(image_shape[2]):
        pooled_slice = dnn_pool(inputs[:, :, :, :, z],
                                ws=pool_shape[0:2],
                                stride=pool_stride[0:2],
                                mode=mode)
        output2d_pooled = T.set_subtensor(output2d_pooled[:, :, :, :, z],
                                          pooled_slice)

    # now 1d-pool over last dimension...
    # could use first or second dimension as input of pool1d..
    # compute maximum y index after first pooling
    output = gpu_alloc_empty(inputs.shape[0], inputs.shape[1], output_shape[0],
                             output_shape[1], output_shape[2])
    max_y = output_shape[1]
    for y in range(max_y):
        # ignore first=0 dimension, alrdy pooled in loop before
        # so set stride and shape to 1 there
        final_pooled_slice = dnn_pool(output2d_pooled[:, :, :, y, :],
                                      ws=(1, pool_shape[2]),
                                      stride=(1, pool_stride[2]),
                                      mode=mode)
        output = T.set_subtensor(output[:, :, :, y, :], final_pooled_slice)

    return output
예제 #38
0
파일: pool_layer.py 프로젝트: ybzhou/Gemini
    def fprop(self, x):
        pooled_out = cuDNN.dnn_pool(
            img=x,
            ws=(self.poolSize, self.poolSize),
            stride=(self.poolStride,self.poolStride),
            pad = (self.pad, self.pad),
            mode=self.mode
            )
        pooled_out = pooled_out if self.actFunc is None else self.actFunc(pooled_out)
        return pooled_out
    
# End PoolLayer
#-------------------------------------------------------------------------------
예제 #39
0
파일: googlenet.py 프로젝트: hma02/platoon
    def __init__(self, input, poolsize, poolstride, poolpad, mode = 'max' ):                 

        self.poolsize = poolsize
        self.poolstride = poolstride
        self.poolpad = poolpad
        
        input_shuffled = input.dimshuffle(3, 0, 1, 2)  # c01b to bc01
        if self.poolsize != 1:
            self.output = dnn.dnn_pool(input_shuffled,
                                       ws=(poolsize, poolsize),
                                       stride=(poolstride, poolstride),
                                       mode=mode, pad=(poolpad, poolpad))
        else:
            self.output = input
        self.output = self.output.dimshuffle(1, 2, 3, 0)  # bc01 to c01b
예제 #40
0
    def get_output_for(self, input, **kwargs):
        input_size = tuple(
            symb if fixed is None else fixed
            for fixed, symb in zip(self.input_shape[2:], input.shape[2:]))
        pool_list = []
        for pool_dim in self.pool_dims:
            win_size = tuple(
                (i + pool_dim - 1) // pool_dim for i in input_size)
            str_size = tuple(i // pool_dim for i in input_size)

            pool = dnn.dnn_pool(input, win_size, str_size, self.mode, (0, 0))
            pool = pool.flatten(3)
            pool_list.append(pool)

        return theano.tensor.concatenate(pool_list, axis=2)
예제 #41
0
파일: dnn.py 프로젝트: Lasagne/Lasagne
    def get_output_for(self, input, **kwargs):
        input_size = tuple(symb if fixed is None else fixed
                           for fixed, symb
                           in zip(self.input_shape[2:], input.shape[2:]))
        pool_list = []
        for pool_dim in self.pool_dims:
            win_size = tuple((i + pool_dim - 1) // pool_dim
                             for i in input_size)
            str_size = tuple(i // pool_dim for i in input_size)

            pool = dnn.dnn_pool(input, win_size, str_size, self.mode, (0, 0))
            pool = pool.flatten(3)
            pool_list.append(pool)

        return theano.tensor.concatenate(pool_list, axis=2)
예제 #42
0
    def __init__(self, input, poolsize, poolstride, poolpad, mode='max'):

        self.poolsize = poolsize
        self.poolstride = poolstride
        self.poolpad = poolpad

        input_shuffled = input.dimshuffle(3, 0, 1, 2)  # c01b to bc01
        if self.poolsize != 1:
            self.output = dnn.dnn_pool(input_shuffled,
                                       ws=(poolsize, poolsize),
                                       stride=(poolstride, poolstride),
                                       mode=mode,
                                       pad=(poolpad, poolpad))
        else:
            self.output = input
        self.output = self.output.dimshuffle(1, 2, 3, 0)  # bc01 to c01b
예제 #43
0
    def __init__(self, rstream, index, x, 
                params, useRglrz, bnPhase,
                poolShape, inFilters, outFilters, stride, ignore_border = False, 
                b=None, a=None, normParam=None, rglrzParam=None):
    
        ''' 
            Averaging layer + BN + noise 
        '''                
        # noise   
        self.paramsT2 = []
        if 'addNoise' in params.rglrz and params.convLayers[index].noise:
            if rglrzParam is None:
                self.rglrzParam = {}
                tempValue = params.rglrzInitial['addNoise'][index]            
                tempParam = np.asarray(tempValue, dtype=theano.config.floatX)
                noizParam = theano.shared(value=tempParam, name='%s_%d' % ('addNoise', index), borrow=True)
                self.rglrzParam['addNoise']=noizParam
            if params.useT2 and 'addNoise' in params.rglrzTrain:
                self.paramsT2 = [noizParam]
            #self.output = noiseup(self.output, splitPoint, noizParam, params.noiseT1, params, index, rstream)
            x = noiseup(x, useRglrz, noizParam, params.noiseT1, params, index, rstream)

        # averaging
        if cudasConv:
            self.output = cudnn.dnn_pool(x, poolShape, stride = stride, mode = 'average_exc_pad')#, ignore_border = ignore_border)                                                                                                
        else:   
            self.output = pool.pool_2d(x, ds = poolShape, st = stride, ignore_border = ignore_border, mode = 'average_exc_pad')

        # if batch normalization                                             
        if params.batchNorm and params.convLayers[index].bn:                        
            _, b, a = t1_shared(params=params, rng=0, index=index, nIn=0, nOut=0, 
                                outFilters=outFilters, filterShape=0, defineW=0) 

            self.b = b; self.a = a    
            self.paramsT1 = [b]
                        
            if normParam is None: 
                normParam, paramsBN = bn_shared(params, outFilters, index)                                                  
            self.normParam = normParam         
            self.paramsBN = paramsBN
            self.output, updateBN = bn_layer(self.output, self.a, self.b, self.normParam, params, bnPhase)
            self.updateBN = updateBN 
      
        # flattening and softmax 
        self.output = T.flatten(self.output, outdim = 2)                                     
        if params.convLayers[index].type == 'average+softmax':
            self.output = activation(self.output, 'softmax')
예제 #44
0
    def apply(self, v, **kwargs):
        input = v.output
        #input = utils.PrintShapeOp(input, 'pool')
        if kwargs.get('use_cpu', False):
            output = downsample.max_pool_2d(input=input,
                                            ds=self.pooling_size,
                                            st=self.pooling_stride)
        else:
            output = dnn.dnn_pool(img=input,
                                  ws=self.pooling_size,
                                  stride=self.pooling_stride,
                                  pad=self.padding)

        print self.input_height, self.pooling_size, self.pooling_stride
        nv = vcopy(v)
        nv.update(output=output)
        return self.post_apply(nv, **kwargs)
예제 #45
0
    def __init__(self, input, image_shape):
        """
        Allocate a SumPoolLayer.

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

        :type image_shape: tuple or list of length 2
        :param image_shape: (image height, image width)
        """
        self.input = input

        #downsample each feature map individually, usin sum over all the image.
        poolsize = (image_shape[0], image_shape[1])
        total_cells = image_shape[0] * image_shape[1]
        pooled_out = dnn.dnn_pool(img=self.input, ws=poolsize, mode='sum')
        self.output = pooled_out
예제 #46
0
파일: ops.py 프로젝트: yancz1989/tunas
def pool2d(x, pool_size, strides, padding, dim_order, mode):
  if dim_order not in dim_orders or padding  not in paddings:
    raise Exception('Error dim_order or padding parameter.')
  x = _shuffle(x, dim2d[dim_order], dim2d[AR])
  if padding == 'same':
    shp = x.shape.eval()
    pad = tuple([(pool_size[i] - 2) if pool_size[i] % 2 == 1 else (pool_size[i] - 1) for i in range(len(pool_size))])
    expected = [shp[i + 2] + strides[i] - 1 // strides[i] for i in range(len(strides))]
  else:
    pad = (0, 0)
  if env.get_device() == '':
    pool_out = pool.pool_2d(x, ds = pool_size, st = strides, ignore_border = True, padding = pad, mode = mode)
  else:
    pool_out = dnn.dnn_pool(x, pool_size, stride = strides, mode = mode, pad = pad)
  if padding == 'same':
    pool_out = pool_out[:, :, : expected[0], : expected[1]]
  return _shuffle(pool_out, dim2d[AR], dim2d[dim_order])
예제 #47
0
파일: ops.py 프로젝트: yancz1989/tunas
def pool3d(x, pool_size, strides, padding, dim_order, mode):
  if dim_order not in dim_orders or padding  not in paddings:
    raise Exception('Error dim_order or padding parameter.')
  x = _shuffle(x, dim3d[dim_order], dim3d[AR])
  if padding == 'same':
    shp = x.shape.eval()
    pad = tuple([(s - 2) if s % 2 == 1 else (s - 1) for s in pool_size])
    expected = [shp[i + 2] + strides[i] - 1 // strides[i] for i in range(len(strides))]
  else:
    pad = (0, 0, 0)
  if env.get_device() == '':
    raise NotImplementedError
  else:
    output = dnn.dnn_pool(x, pool_size, stride = strides, mode = mode, pad = pad)
  if padding == 'same':
    output = output[:, :, : expected[0], : expected[1], : expected[2]]
  return _shuffle(output, dim3d[AR], dim3d[dim_order])
예제 #48
0
    def build_computation_graph(self):
        if self.group == 1:
            conv_out = self.convolution(img=self.input,
                                        kerns=self.W,
                                        subsample=(self.convstride,
                                                   self.convstride),
                                        border_mode=(self.padsize,
                                                     self.padsize))
            conv_out = conv_out + self.b.dimshuffle('x', 0, 'x', 'x')

        else:
            conv_out0 = self.convolution(
                img=self.input[:, :self.channel / 2, :, :],
                kerns=self.W0,
                subsample=(self.convstride, self.convstride),
                border_mode=(self.padsize, self.padsize))
            conv_out0 = conv_out0 + self.b0.dimshuffle('x', 0, 'x', 'x')

            conv_out1 = self.convolution(
                img=self.input[:, self.channel / 2:, :, :],
                kerns=self.W1,
                subsample=(self.convstride, self.convstride),
                border_mode=(self.padsize, self.padsize))
            conv_out1 = conv_out1 + self.b1.dimshuffle('x', 0, 'x', 'x')

            conv_out = T.concatenate([conv_out0, conv_out1], axis=1)

        # ReLu by default
        output = self.activation_func(conv_out)

        # Pooling
        if self.poolsize != 1:
            if has_cudnn:
                output = dnn.dnn_pool(output,
                                      ws=(self.poolsize, self.poolsize),
                                      stride=(self.poolstride,
                                              self.poolstride))
            else:
                output = downsample.max_pool_2d(output,
                                                ds=(self.poolsize,
                                                    self.poolsize),
                                                st=(self.poolstride,
                                                    self.poolstride))

        return output
예제 #49
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
예제 #50
0
파일: lrn.py 프로젝트: shaoxuan92/treeano
def local_response_normalization_2d_dnn(in_vw, alpha, k, beta, n):
    """
    using cudnn mean pooling
    """
    from theano.sandbox.cuda import dnn
    assert n % 2 == 1, "n must be odd"
    in_var = in_vw.variable
    b, ch, r, c = in_vw.symbolic_shape()
    squared = T.sqr(in_var)
    reshaped = squared.reshape((b, 1, ch, r * c))
    pooled = dnn.dnn_pool(img=reshaped,
                          ws=(n, 1),
                          stride=(1, 1),
                          pad=(n // 2, 0),
                          mode="average_inc_pad")
    unreshaped = pooled.reshape((b, ch, r, c))
    # multiply by n, since we did a mean pool instead of a sum pool
    return in_var / (((alpha * n) * unreshaped + k) ** beta)
예제 #51
0
    def _build_computation_graph(self):
        if self.group == 1:
            conv_out = self.convolution_func(
                img=self.input,
                kerns=self.W,
                subsample=(self.convstride, self.convstride),
                border_mode=(self.padsize, self.padsize)
            )
            conv_out = conv_out + self.b.dimshuffle('x', 0, 'x', 'x')

        else:
            conv_out0 = self.convolution_func(
                img=self.input[:, :self.channel / 2, :, :],
                kerns=self.W0,
                subsample=(self.convstride, self.convstride),
                border_mode=(self.padsize, self.padsize)
            )
            conv_out0 = conv_out0 + self.b0.dimshuffle('x', 0, 'x', 'x')


            conv_out1 = self.convolution_func(
                img=self.input[:, self.channel / 2:, :, :],
                kerns=self.W1,
                subsample=(self.convstride, self.convstride),
                border_mode=(self.padsize, self.padsize)
            )
            conv_out1 = conv_out1 + self.b1.dimshuffle('x', 0, 'x', 'x')

            conv_out = T.concatenate([conv_out0, conv_out1], axis=1)

        # ReLu by default
        output = self.activation_func(conv_out)

        # Pooling
        if self.poolsize != 1:
            if has_cudnn:
                output = dnn.dnn_pool(output,
                                      ws=(self.poolsize, self.poolsize),
                                      stride=(self.poolstride, self.poolstride))
            else:
                output = downsample.max_pool_2d(output,
                                                ds=(self.poolsize, self.poolsize),
                                                st=(self.poolstride, self.poolstride))
        return output
    def __init__(self,
                 input,
                 poolsize,
                 poolstride,
                 lib_conv,
                 poolpad=0,
                 poolmode='max',
                 caffe_style=False):
        '''
        lib_conv can be cudnn (recommended)or cudaconvnet
        '''
        self.poolsize = poolsize
        if lib_conv == 'cudaconvnet':
            # Pooling
            if self.poolsize != 1:
                self.pool_op = MaxPool(ds=poolsize, stride=poolstride)
                self.output = self.pool_op(input)

        elif lib_conv == 'cudnn':

            input_shuffled = input.dimshuffle(3, 0, 1, 2)  # c01b to bc01
            # in01out to outin01
            # print image_shape_shuffled
            # print filter_shape_shuffled
            if caffe_style:
                self.output = input_shuffled[:, :, ::-1, ::-1]
            else:
                self.output = input_shuffled
                # poolpad=1
            if self.poolsize != 1:
                self.output = dnn.dnn_pool(self.output,
                                           ws=(poolsize, poolsize),
                                           stride=(poolstride, poolstride),
                                           mode=poolmode,
                                           pad=(poolpad, poolpad))
            if caffe_style:
                self.output = self.output[:, :, ::-1, ::-1]

            self.output = self.output.dimshuffle(1, 2, 3, 0)  # bc01 to c01b
            # self.pool_out=self.output
        else:
            NotImplementedError("lib_conv can only be cudaconvnet or cudnn")
예제 #53
0
    def drop_output(self, input, drop=0, rng=None, p=0.5):
        # convolve input feature maps with filters
        '''
        dnn
        '''
        if self.border_mode == 'same':
            conv_out = dnn.dnn_conv(
                img=input,
                kerns=self.W,
                subsample=(1,1),
                border_mode=self.border,
                #conv_mode='cross'
            )
        else:
            raise Exception('Unknown conv type')

        # downsample each feature map individually, using maxpooling
        
        if self.poolsize[0] == 1 and self.poolsize[1] == 1:
            pooled_out = conv_out
        else:
            pooled_out = dnn.dnn_pool(
                img=conv_out,
                ws=self.poolsize,
                stride=self.poolsize,
                mode='max',
                pad=(0,0)
            )

        # 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
        lin_output = pooled_out + self.b.dimshuffle('x', 0, 'x', 'x')
        output = (
            lin_output if self.activation is None
            else self.activation(lin_output)
        )
        droppedOutput = nonlinearity.dropout(rng, output, p)
        return T.switch(T.neq(drop, 0), droppedOutput, output)
예제 #54
0
    def _output(self, input,  *args, **kwargs):
        origin = input[:, - self.n_in:]
        r_3_r, r_5_r, r_7_r = origin, origin, origin

        c = 0
        if self.n_out_3x3_r > 0:
            r_3_r = input[:, c:c + self.n_out_3x3_r]
            c += self.n_out_3x3_r
        if self.n_out_5x5_r > 0:
            r_5_r = input[:, c:c + self.n_out_5x5_r]
            c += self.n_out_5x5_r
        if self.n_out_7x7_r > 0:
            r_7_r = input[:, c:c + self.n_out_7x7_r]
            c += self.n_out_7x7_r
        input = origin

        r_1 = None
        if not None == self.W_1x1:
            r_1 = self._conv(self.W_1x1, self.b_1x1, img=input)
            if not self.W_mp_r == None:
                r_1 = r_1[:, :, 1:-1, 1:-1]
        pad_ = 1 if self.W_mp_r == None else 0

        r_3 = self._conv(self.W_3x3, self.b_3x3, img=r_3_r, pad=pad_ + 0)
        r_5 = self._conv(self.W_5x5, self.b_5x5, img=r_5_r, pad=pad_ + 1)
        r_7 = self._conv(self.W_7x7, self.b_7x7, img=r_7_r, pad=pad_ + 2)
        r_mp = None
        if not None == self.W_mp_r:
            r_mp = dnn.dnn_pool(input, ws=(3, 3), stride=(1, 1))
            r_mp = self._conv(self.W_mp_r, self.b_mp_r, img=r_mp)
        results = []
        for r in [r_1, r_3, r_5, r_7, r_mp]:
            if not None == r:
                results.append(r)
        r = T.concatenate(results, axis=1)
        return r
예제 #55
0
    def _output(self, input,  *args, **kwargs):
        results = []

        def conv(w, b, pad=0, img=None):
            if None == img:
                img = input
            if None == w:
                return None
            out = dnn.dnn_conv(
                img=img, kerns=w, subsample=(1, 1), border_mode=pad)
            out += b.dimshuffle('x', 0, 'x', 'x')
            out = T.maximum(out, 0)
            return out

        r_1 = None
        if not None == self.W_1x1:
            r_1 = conv(self.W_1x1, self.b_1x1)
            if not self.W_mp_r == None:
                r_1 = r_1[:, :, 1:-1, 1:-1]
        pad_ = 1 if self.W_mp_r == None else 0

        r_3_r = conv(self.W_3x3_r, self.b_3x3_r)
        r_3 = conv(self.W_3x3, self.b_3x3, img=r_3_r, pad=pad_ + 0)
        r_5_r = conv(self.W_5x5_r, self.b_5x5_r)
        r_5 = conv(self.W_5x5, self.b_5x5, img=r_5_r, pad=pad_ + 1)
        r_7_r = conv(self.W_7x7_r, self.b_7x7_r)
        r_7 = conv(self.W_7x7, self.b_7x7, img=r_7_r, pad=pad_ + 2)
        r_mp = None
        if not None == self.W_mp_r:
            r_mp = dnn.dnn_pool(input, ws=(3, 3), stride=(1, 1))
            r_mp = conv(self.W_mp_r, self.b_mp_r, img=r_mp)
        for r in [r_1, r_3, r_5, r_7, r_mp]:
            if not None == r:
                results.append(r)
        r = T.concatenate(results, axis=1)
        return r