Beispiel #1
0
    def __init__(self, params, model_inp, layer_inp, corruption_type=None, corruption_level=0):

        # Parameters
        self.params = params
        self.model_inp = model_inp
        self.layer_inp = layer_inp
        self.corruption_type = corruption_type
        self.corruption_level = corruption_level

        # corrupt input
        corr_inp = corrupt(layer_inp, corruption_type, corruption_level)

        out = []
        self.nb_channels = 0

        if params.mean_filter_size!= 0:
            out += [params.m_act(conv(corr_inp, params.mean_filters) + params.mean_b.dimshuffle('x', 0, 'x', 'x'))]
            self.nb_channels += params.mean_filter_size[0]

        if params.cov_filter_size != 0:
            f = conv(corr_inp, params.cov_filters)**2
            out += [params.c_act(conv(f, params.cov_mapping) + params.cov_b.dimshuffle('x', 0, 'x', 'x'))]
            self.nb_channels += params.map_filter_size[0]

        self.out = T.concatenate(out, axis=1)
Beispiel #2
0
    def dec(self, inp, corruption_type=None, corruption_level=0, border_mode = 'valid'):

        # Corrupt input
        inp = corrupt(inp, corruption_type, corruption_level)

        # Apply filter and return
        return conv(inp, self.W[:,:,::-1,::-1].transpose(1,0,2,3), border_mode=border_mode) * self.scale
    def rec_error(X,Z,D):
        # Calculates the reconstruction rec_i = sum_j Z_ij * D_j
        # and the corresponding (mean) square reconstruction error
        # rec_error = (X_i - rec_i) ** 2

        rec = conv(Z,D,border_mode='valid',
                    image_shape=config['Zinit'],filter_shape=_D.shape) 
        rec_error = 0.5*T.mean(((X-rec)**2).sum(axis=-1).sum(axis=-1))
        return rec_error, rec
Beispiel #4
0
    def __call__(self, inp, corruption_type=None, corruption_level=0, border_mode = 'valid'):

        # Corrupt input
        inp = corrupt(inp, corruption_type, corruption_level)

        # Scale if using biased noise
        inp = inp / T.cast(1-corruption_level, th.config.floatX) if corruption_type == 'zeromask' and corruption_level > 0 else inp

        # Filtered output
        f = conv(inp, self.W, border_mode=border_mode) * self.scale

        return step(f-self.threshold) * f
Beispiel #5
0
    def _model(self,
               inp,
               inp_corruption_type=None,
               inp_corruption_level=0,
               hid_corruption_type=None,
               hid_corruption_level=0,
               L1_hiddens=0,
               L2_weights=0):

        # Encoder
        #enc_input_sz = self.input_sz
        #enc_filter_sz = self.filter_sz
        corr_inp = corrupt(inp, inp_corruption_type, inp_corruption_level)
        hid = self.act(
            conv(corr_inp, self.params.w_enc, border_mode='valid') +
            self.params.b_enc.dimshuffle('x', 0, 'x', 'x'))

        # Decoder
        #dec_input_sz = (enc_input_sz[0], enc_filter_sz[0], enc_input_sz[2]-enc_filter_sz[2]+1, enc_input_sz[3]-enc_filter_sz[3]+1)
        #dec_filter_sz = (int(np.prod(enc_input_sz[1:])), enc_filter_sz[0], 1, 1)
        corr_hid = corrupt(hid, hid_corruption_type, hid_corruption_level)
        out = conv(corr_hid, self.params.w_dec, border_mode='valid')

        # Make cost function
        cost = T.mean((0.5 * (out.flatten(2) - inp.flatten(2))**2).sum(axis=1))

        # Add L1 hiddens cost
        if L1_hiddens > 0:
            cost += L1_hiddens * abs(hid).sum(1).mean()

        # Add L2 weight cost
        if L2_weights > 0:
            cost += L2_weights * ((self.params.w_enc**2.).sum() +
                                  (self.params.w_dec**2.).sum())

        return hid, cost
Beispiel #6
0
    def __init__(self,
                 params,
                 act,
                 model_inp,
                 layer_inp,
                 corruption_type=None,
                 corruption_level=0):

        # Parameters
        self.params = params
        self.model_inp = model_inp
        self.layer_inp = layer_inp
        self.corruption_type = corruption_type
        self.corruption_level = corruption_level

        # Model
        corr_inp = corrupt(layer_inp, corruption_type, corruption_level)
        self.out = act(
            conv(corr_inp, params.W, border_mode='valid') +
            params.B.dimshuffle('x', 0, 'x', 'x'))
Beispiel #7
0
    def _update_model(self):

        # Corrupt input
        corr_inp = corrupt(self.inp, 'zeromask', self.dropout_level)

        # Apply convolution
        out = conv(corr_inp, self.params.W,
                   border_mode='valid') + self.params.B.dimshuffle(
                       'x', 0, 'x', 'x')

        # Remember convolution output shape
        out_shape = out.shape
        #out_shape = thprint("out.shape = ")(out.shape)

        # Reshape to softmax format (2D)
        out = out.dimshuffle(0, 2, 3, 1).reshape(
            (out.size // self.nb_classes, self.nb_classes))

        # Compute class probability
        self.prob = softmax(out)

        # Class prediction
        self.pred = T.argmax(self.prob, axis=1).reshape(
            (out_shape[0], out_shape[2], out_shape[3],
             out_shape[1] // self.nb_classes)).dimshuffle(0, 3, 1, 2)

        # Compute softmax cost
        self.cost = -T.mean(
            T.log(
                self.prob[T.arange(self.prob.shape[0]),
                          self.conv_labels.dimshuffle(0, 2, 3, 1).flatten()]))

        # Reshape class prob to convolutional format
        self.prob = self.prob.reshape(
            (out_shape[0], out_shape[2], out_shape[3],
             out_shape[1])).dimshuffle(0, 3, 1, 2)

        # Prediction error
        self.error = T.mean(T.neq(self.pred, self.conv_labels))
Beispiel #8
0
    def _update_labels(self):
        if self._lab_filter_sz[2] == self._lab_image_sz[
                2] and self._lab_filter_sz[3] == self._lab_image_sz[3]:
            self.conv_labels = self.labels.flatten(2).dimshuffle(
                0, 1, 'x', 'x')

        elif self._lab_filter_sz[2] < self._lab_image_sz[
                2] or self._lab_filter_sz[3] < self._lab_image_sz[3]:

            # Make sure input lab_filter_sz is correct
            assert (self._lab_filter_sz[0] == self._lab_filter_sz[2] *
                    self._lab_filter_sz[3])
            assert (self._lab_filter_sz[1] == 1)

            # Build selection array
            lab_filters = np.zeros(self._lab_filter_sz, dtype=th.config.floatX)

            # Set filters
            s = np.arange(self._lab_filter_sz[0])
            lab_filters[s, 0,
                        (s // self._lab_filter_sz[3]) % self._lab_filter_sz[2],
                        s % self._lab_filter_sz[3]] = 1

            # Create shared variable
            lab_filters = th.shared(lab_filters)

            # Modify input labels
            self.conv_labels = conv(self.labels.astype(th.config.floatX),
                                    lab_filters,
                                    image_shape=self._lab_image_sz,
                                    filter_shape=self._lab_filter_sz,
                                    border_mode='valid')
            self.conv_labels = self.conv_labels.astype(self.labels.dtype)

        else:
            raise ValueError, 'Unhandeled case'
def lconvista(config, shrinkage):
    """Learned Convolutional ISTA   

    Returns TODO
    """
    print "[LConvISTA]"

    layers = config['layers']
    btsz = config['btsz']
    
      
    _D = config['D']
    D = theano.shared(value=np.asarray(_D, 
            dtype=theano.config.floatX),borrow=True,name='D')
    _theta = config['theta']
    theta = theano.shared(value=np.asarray(_theta, 
            dtype=theano.config.floatX),borrow=True,name="theta")
    _L = config['L']
    L = theano.shared(value=np.asarray(_L, 
            dtype=theano.config.floatX),borrow=True,name="L")

    params = [D,theta,L]

    #filter shape information for speed up of convolution
    fs1 = _D.shape
    fs2 = (fs1[1],fs1[0],fs1[2],fs1[3])

    #need tensor4:s due to interface of nnet.conv.conv2d
    #X.shape = (btsz, singleton dimension, h, w)
    X = T.tensor4('X', dtype=theano.config.floatX)
    #Z.shape = (btsz, n_filters, h+s-1, w+s-1)
    Z = T.zeros(config['Zinit'],dtype=theano.config.floatX)




    # The combination of for loop and 'hand calculated' gradient was tested
    # on CPU with 2 layers and 16 filters as well as 5 layers and 49 filters.
    # Note though that T.grad catches up with increasing parameters.
    # Hand calculated grad is preferred due to higher flexibility. 
    for i in range(layers):    
        gradZ = conv(
                    conv(
                            Z,D,border_mode='valid',
                            image_shape=config['Zinit'],filter_shape=fs1
                        ) - X,
                    D[:,:,::-1,::-1].dimshuffle(1,0,2,3),border_mode='full',
                    image_shape=config['Xshape'], filter_shape=fs2
                    ) / btsz
        Z = shrinkage(Z - 1/L * gradZ,theta)


    def rec_error(X,Z,D):
        # Calculates the reconstruction rec_i = sum_j Z_ij * D_j
        # and the corresponding (mean) square reconstruction error
        # rec_error = (X_i - rec_i) ** 2

        rec = conv(Z,D,border_mode='valid',
                    image_shape=config['Zinit'],filter_shape=_D.shape) 
        rec_error = 0.5*T.mean(((X-rec)**2).sum(axis=-1).sum(axis=-1))
        return rec_error, rec


    sparsity = T.mean(T.sum(T.sum(T.abs_(Z),axis=-1),axis=-1))
    re, rec = rec_error(X,Z,D)    

    return X, params, Z, rec, re, sparsity
Beispiel #10
0
 def __call__(self, inp, mode=None):
     act = activation(self.act)
     return act(conv(inp, self.W) + self.b.dimshuffle('x', 0, 'x', 'x'))