Example #1
0
 def createMaxPoolLayer(self, conv_out=None, poolsize=None):
     if conv_out is None: conv_out = self.conv_out
     if poolsize is None: poolsize = self.poolsize
     # pool each feature map individually, using maxpooling
     #pooled_out = pool.pool_2d(
     self.pooled_out = max_pool_3d(input=conv_out.dimshuffle(0, 2, 1, 3, 4),
                                   ds=poolsize,
                                   ignore_border=True)
     return self.pooled_out.dimshuffle(0, 2, 1, 3, 4)
Example #2
0
    def __init__(self, input, pool_shape, method="max"):
        """
        method: "max", "avg", "L2", "L4", ...
        """

        self.__dict__.update(locals())
        del self.self

        if method=="max":
            out = max_pool_3d(input,pool_shape)
        else:
            raise NotImplementedError()

        self.output = out
Example #3
0
    def __init__(self, input, pool_shape, method="max"):
        """
        method: "max", "avg", "L2", "L4", ...
        """

        self.__dict__.update(locals())
        del self.self

        if method == "max":
            out = max_pool_3d(input, pool_shape)
        else:
            raise NotImplementedError()

        self.output = out
Example #4
0
    def __init__(self, input, n_in_maps, n_out_maps, kernel_shape, video_shape, pool_shape,
                 batch_size, layer_name="Conv", rng=RandomState(1234),
                 borrow=True, W=None, b=None):

        """
        video_shape: (frames, height, width)
        kernel_shape: (frames, height, width)
        W_shape: (out, in, kern_frames, kern_height, kern_width)
        """

        # init W
        if W is not None:
            self.W = W
        else:
            # fan in: filter time x filter height x filter width x input maps
            fan_in = prod(kernel_shape) * n_in_maps
            norm_scale = 2. * sqrt(1. / fan_in)
            W_shape = (n_out_maps, n_in_maps) + kernel_shape
            W_val = _asarray(rng.normal(loc=0, scale=norm_scale, size=W_shape), dtype=floatX)
            self.W = shared(value=W_val, borrow=borrow, name=layer_name + '_W')

        # init bias
        if b is not None:
            self.b = b
        else:
            b_val = zeros((n_out_maps,), dtype=floatX)
            self.b = shared(b_val, name=layer_name + "_b", borrow=borrow)

        self.params = [self.W, self.b]

        # 3D convolution; dimshuffle: last 3 dimensions must be (in, h, w)
        n_fr, h, w = video_shape
        n_fr_k, h_k, w_k = kernel_shape
        signals = input.dimshuffle([0, 2, 1, 3, 4])
        out = conv3d(
            signals=signals,
            filters=self.W,
            signals_shape=(batch_size, n_fr, n_in_maps, h, w),
            filters_shape=(n_out_maps, n_fr_k, n_in_maps, h_k, w_k),
            border_mode='valid').dimshuffle([0, 2, 1, 3, 4])

        pooled_out = max_pool_3d(out, pool_shape, ignore_border=True)
        pooled_out += self.b.dimshuffle('x', 0, 'x', 'x', 'x')

        self.output = T.tanh(pooled_out)
Example #5
0
    def __init__(self, rng, input, filter_shape, image_shape, pool_shape):

        self.input = input

        # initialize weights with random weights
        self.W = theano.shared(numpy.asarray(rng.uniform(low=-.1,
                                                         high=.1,
                                                         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)

        # filter stride
        d = theano.shared(numpy.ndarray(shape=(3, ), dtype=int))
        d.set_value([1, 1, 1])

        # convolve input feature maps with filters
        # # theano.tensor.nnet.conv3D
        # conv_out = conv3D(
        #     V=self.input,
        #     W=self.W,
        #     b=self.b,
        #     d=d)
        # theano.tensor.nnet.conv3d2d.conv3d
        conv_out = T.nnet.conv3d2d.conv3d(signals=self.input,
                                          filters=self.W,
                                          signals_shape=image_shape,
                                          filters_shape=filter_shape,
                                          border_mode='valid')

        activation = T.tanh(conv_out +
                            self.b.dimshuffle('x', 'x', 0, 'x', 'x'))

        if pool_shape == (1, 1, 1):
            self.output = activation
        else:
            self.output = max_pool_3d(activation, pool_shape)

        # store parameters of this layer
        self.params = [self.W, self.b]
    def __init__(self, rng, input, filter_shape, image_shape, pool_shape):

        self.input = input

        # initialize weights with random weights
        self.W = theano.shared(
            numpy.asarray(
                rng.uniform(low=-.1, high=.1, 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)

        # filter stride
        d = theano.shared(numpy.ndarray(shape=(3,),dtype=int))
        d.set_value([1, 1, 1])

        # convolve input feature maps with filters
        # # theano.tensor.nnet.conv3D
        # conv_out = conv3D(
        #     V=self.input,
        #     W=self.W,
        #     b=self.b,
        #     d=d)
        # theano.tensor.nnet.conv3d2d.conv3d
        conv_out = T.nnet.conv3d2d.conv3d(
            signals=self.input,
            filters=self.W,
            signals_shape=image_shape,
            filters_shape=filter_shape,
            border_mode='valid')

        activation = T.tanh(conv_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x'))

        if pool_shape == (1,1,1):
            self.output = activation
        else:
            self.output = max_pool_3d(activation, pool_shape)

        # store parameters of this layer
        self.params = [self.W, self.b]
    def __init__(self,
                 rng,
                 input,
                 signal_shape,
                 filter_shape,
                 poolsize=(2, 2, 2),
                 stride=None,
                 if_pool=False,
                 if_hidden_pool=False,
                 act=None,
                 share_with=None,
                 tied=None,
                 border_mode='valid'):
        self.input = input

        if share_with:
            self.W = share_with.W
            self.b = share_with.b

            self.W_delta = share_with.W_delta
            self.b_delta = share_with.b_delta

        elif tied:
            self.W = tied.W.dimshuffle(1, 0, 2, 3)
            self.b = tied.b

            self.W_delta = tied.W_delta.dimshuffle(1, 0, 2, 3)
            self.b_delta = tied.b_delta

        else:
            fan_in = np.prod(filter_shape[1:])
            poolsize_size = np.prod(poolsize) if poolsize else 1
            fan_out = (filter_shape[0] * np.prod(filter_shape[2:]) /
                       poolsize_size)
            W_bound = np.sqrt(6. / (fan_in + fan_out))
            self.W = theano.shared(np.asarray(rng.uniform(low=-W_bound,
                                                          high=W_bound,
                                                          size=filter_shape),
                                              dtype=theano.config.floatX),
                                   borrow=True)
            b_values = np.zeros((filter_shape[0], ),
                                dtype=theano.config.floatX)
            self.b = theano.shared(value=b_values, borrow=True)

            self.W_delta = theano.shared(np.zeros(filter_shape,
                                                  dtype=theano.config.floatX),
                                         borrow=True)

            self.b_delta = theano.shared(value=b_values, borrow=True)

        # convolution
        conv_out = conv3d2d.conv3d(signals=input,
                                   filters=self.W,
                                   signals_shape=signal_shape,
                                   filters_shape=filter_shape,
                                   border_mode=border_mode)

        #if poolsize:
        if if_pool:
            conv_out = conv_out.dimshuffle(
                0, 2, 1, 3, 4)  #maxpool3d works on last 3 dimesnions
            pooled_out = maxpool3d.max_pool_3d(input=conv_out,
                                               ds=poolsize,
                                               ignore_border=True)
            tmp_out = pooled_out.dimshuffle(0, 2, 1, 3, 4)
            tmp = tmp_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x')
        elif if_hidden_pool:
            pooled_out = downsample.max_pool_2d(input=conv_out,
                                                ds=poolsize[:2],
                                                st=stride,
                                                ignore_border=True)
            tmp = pooled_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x')
        else:
            tmp = conv_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x')

        if act == 'tanh':
            self.output = T.tanh(tmp)
        elif act == 'sigmoid':
            self.output = nnet.sigmoid(tmp)
        elif act == 'relu':
            # self.output = tmp * (tmp>0)
            self.output = 0.5 * (tmp + abs(tmp)) + 1e-9
        elif act == 'softplus':
            # self.output = T.log2(1+T.exp(tmp))
            self.output = nnet.softplus(tmp)
        else:
            self.output = tmp

        self.get_activation = theano.function([self.input],
                                              self.output,
                                              updates=None,
                                              name='get hidden activation')

        # store parameters of this layer
        self.params = [self.W, self.b]
        self.deltas = [self.W_delta, self.b_delta]
    def __init__(self, signal_shape, filter_shape, poolsize, activation=None):
        rng = np.random.RandomState(None)
        dtensor5 = T.TensorType('float32', (False, ) * 5)
        self.inputs = dtensor5(name='inputs')
        self.image_shape = signal_shape
        self.batchsize = signal_shape[0]
        self.in_channels = signal_shape[2]
        self.in_depth = signal_shape[1]
        self.in_width = signal_shape[4]
        self.in_height = signal_shape[3]
        self.flt_channels = filter_shape[0]
        self.flt_time = filter_shape[1]
        self.flt_width = filter_shape[4]
        self.flt_height = filter_shape[3]
        self.activation = activation

        self.hidden_layer = ConvolutionLayer3D(rng,
                                               input=self.inputs,
                                               signal_shape=signal_shape,
                                               filter_shape=filter_shape,
                                               act=activation,
                                               border_mode='full',
                                               if_hidden_pool=False)

        self.hidden_image_shape = (self.batchsize, self.in_depth,
                                   self.flt_channels,
                                   self.in_height + self.flt_height - 1,
                                   self.in_width + self.flt_width - 1)

        self.hidden_pooled_image_shape = (
            self.batchsize, self.in_depth / 2, self.flt_channels,
            (self.in_height + self.flt_height - 1) / 2,
            (self.in_width + self.flt_width - 1) / 2)

        self.hidden_filter_shape = (self.in_channels, self.flt_time,
                                    self.flt_channels, self.flt_height,
                                    self.flt_width)

        self.recon_layer = ConvolutionLayer3D(
            rng,
            input=self.hidden_layer.output,
            signal_shape=self.hidden_image_shape,
            filter_shape=self.hidden_filter_shape,
            act=activation,
            border_mode='valid')

        self.layers = [self.hidden_layer, self.recon_layer]
        self.params = sum([layer.params for layer in self.layers], [])
        L = T.sum(T.pow(T.sub(self.recon_layer.output, self.inputs), 2),
                  axis=(1, 2, 3, 4))
        self.cost = 0.5 * T.mean(L)
        self.grads = T.grad(self.cost, self.params)
        self.updates = adadelta_updates(self.params,
                                        self.grads,
                                        rho=0.95,
                                        eps=1e-6)

        self.train = theano.function([self.inputs],
                                     self.cost,
                                     updates=self.updates,
                                     name="train cae model")

        self.activation = maxpool3d.max_pool_3d(
            input=self.hidden_layer.output.dimshuffle(0, 2, 1, 3, 4),
            ds=poolsize,
            ignore_border=True)
        self.activation = self.activation.dimshuffle(0, 2, 1, 3, 4)
        self.get_activation = theano.function([self.inputs],
                                              self.activation,
                                              updates=None,
                                              name='get hidden activation')
    def __init__(self, rng, input, signal_shape, filter_shape, poolsize=(2, 2, 2), stride=None, if_pool=False, if_hidden_pool=False,
                 act=None,
                 share_with=None,
                 tied=None,
                 border_mode='valid'):
        self.input = input

        if share_with:
            self.W = share_with.W
            self.b = share_with.b

            self.W_delta = share_with.W_delta
            self.b_delta = share_with.b_delta

        elif tied:
            self.W = tied.W.dimshuffle(1,0,2,3)
            self.b = tied.b

            self.W_delta = tied.W_delta.dimshuffle(1,0,2,3)
            self.b_delta = tied.b_delta

        else:
            fan_in = np.prod(filter_shape[1:])
            poolsize_size = np.prod(poolsize) if poolsize else 1
            fan_out = (filter_shape[0] * np.prod(filter_shape[2:]) / poolsize_size)
            W_bound = np.sqrt(6. / (fan_in + fan_out))
            self.W = theano.shared(
                np.asarray(
                    rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
                    dtype=theano.config.floatX
                ),
                borrow=True
            )
            b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX)
            self.b = theano.shared(value=b_values, borrow=True)

            self.W_delta = theano.shared(
                np.zeros(filter_shape, dtype=theano.config.floatX),
                borrow=True
            )

            self.b_delta = theano.shared(value=b_values, borrow=True)

        # convolution
        conv_out = conv3d2d.conv3d(
            signals=input,
            filters=self.W,
            signals_shape=signal_shape,
            filters_shape=filter_shape,
            border_mode=border_mode)

        #if poolsize:
        if if_pool:
            conv_out = conv_out.dimshuffle(0,2,1,3,4) #maxpool3d works on last 3 dimesnions
            pooled_out = maxpool3d.max_pool_3d(
                input=conv_out,
                ds=poolsize,
                ignore_border=True)
            tmp_out = pooled_out.dimshuffle(0,2,1,3,4)
            tmp = tmp_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x')
        elif if_hidden_pool:
            pooled_out = downsample.max_pool_2d(
                input=conv_out,
                ds=poolsize[:2],
                st=stride,
                ignore_border=True)
            tmp = pooled_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x')
        else:
            tmp = conv_out + self.b.dimshuffle('x', 'x', 0, 'x', 'x')

        if act == 'tanh':
            self.output = T.tanh(tmp)
        elif act == 'sigmoid':
            self.output = nnet.sigmoid(tmp)
        elif act == 'relu':
            # self.output = tmp * (tmp>0)
            self.output = 0.5 * (tmp + abs(tmp)) + 1e-9
        elif act == 'softplus':
            # self.output = T.log2(1+T.exp(tmp))
            self.output = nnet.softplus(tmp)
        else:
            self.output = tmp

        self.get_activation = theano.function(
            [self.input],
            self.output,
            updates=None,
            name='get hidden activation')

        # store parameters of this layer
        self.params = [self.W, self.b]
        self.deltas = [self.W_delta, self.b_delta]
    def __init__(self, signal_shape, filter_shape, poolsize, activation=None):
        rng = np.random.RandomState(None)
        dtensor5 = T.TensorType('float32', (False,)*5)
        self.inputs = dtensor5(name='inputs')
        self.image_shape = signal_shape
        self.batchsize = signal_shape[0]
        self.in_channels   = signal_shape[2]
        self.in_depth      = signal_shape[1]
        self.in_width      = signal_shape[4]
        self.in_height     = signal_shape[3]
        self.flt_channels  = filter_shape[0]
        self.flt_time      = filter_shape[1]
        self.flt_width     = filter_shape[4]
        self.flt_height    = filter_shape[3]
        self.activation = activation

        self.hidden_layer=ConvolutionLayer3D(rng,
                                             input=self.inputs,
                                             signal_shape=signal_shape,
                                             filter_shape=filter_shape,
                                             act=activation,
                                             border_mode='full',
                                             if_hidden_pool=False)

        self.hidden_image_shape = (self.batchsize,
                                   self.in_depth,
                                   self.flt_channels,
                                   self.in_height+self.flt_height-1,
                                   self.in_width+self.flt_width-1)

        self.hidden_pooled_image_shape = (self.batchsize,
                                          self.in_depth/2,
                                          self.flt_channels,
                                          (self.in_height+self.flt_height-1)/2,
                                          (self.in_width+self.flt_width-1)/2)

        self.hidden_filter_shape = (self.in_channels, self.flt_time, self.flt_channels, self.flt_height,
                                    self.flt_width)

        self.recon_layer=ConvolutionLayer3D(rng,
                                 input=self.hidden_layer.output,
                                 signal_shape=self.hidden_image_shape,
                                 filter_shape=self.hidden_filter_shape,
                                 act=activation,
                                 border_mode='valid')

        self.layers = [self.hidden_layer, self.recon_layer]
        self.params = sum([layer.params for layer in self.layers], [])
        L=T.sum(T.pow(T.sub(self.recon_layer.output, self.inputs), 2), axis=(1,2,3,4))
        self.cost = 0.5*T.mean(L)
        self.grads = T.grad(self.cost, self.params)
        self.updates = adadelta_updates(self.params, self.grads, rho=0.95, eps=1e-6)

        self.train = theano.function(
        [self.inputs],
        self.cost,
        updates=self.updates,
        name = "train cae model"
        )

        self.activation = maxpool3d.max_pool_3d(
                input=self.hidden_layer.output.dimshuffle(0,2,1,3,4),
                ds=poolsize,
                ignore_border=True)
        self.activation = self.activation.dimshuffle(0,2,1,3,4)
        self.get_activation = theano.function(
            [self.inputs],
            self.activation,
            updates=None,
            name='get hidden activation')