Ejemplo n.º 1
0
 def get_network(self):
     network = lasagne.layers.InputLayer(shape=(None, self.num_features),input_var=self.input_var)
     for i in xrange(0, self.num_layers):
         network = DenseLayer(network,nonlinearity=rectify,num_units=self.num_nodes)
         if i != 0:
             network = FeaturePoolLayer(incoming=network, pool_size=2,axis=1, pool_function=theano.tensor.mean)
         for _ in xrange(0, 1):
             network = DenseLayer(network,nonlinearity=rectify,num_units=self.num_nodes)
             layers = [network]
             for _ in xrange(0, 4):
                 network = batch_norm(self.add_dense_maxout_block(network, self.num_nodes, self.dropout))
                 layers.append(network)
                 network = ConcatLayer(layers, axis=1)
     maxout = FeaturePoolLayer(incoming=network, pool_size=2,axis=1, pool_function=theano.tensor.mean)
     return lasagne.layers.DenseLayer(network, num_units=2,nonlinearity=lasagne.nonlinearities.softmax)
Ejemplo n.º 2
0
def broadcast_dot_layer(l_pred, l_targets, feature_dim, id_tag):
    l_broadcast = dimshuffle(l_pred, (0, 1, 'x'), name=id_tag + 'dot_broadcast')
    l_forget = ForgetSizeLayer(l_broadcast, axis=2, name=id_tag + 'dot_nosize')
    l_merge = ElemwiseMergeLayer((l_forget, l_targets), T.mul, name=id_tag + 'dot_elemwise_mul')
    l_pool = FeaturePoolLayer(l_merge, pool_size=feature_dim, axis=1,
                              pool_function=T.sum, name=id_tag + 'dot_pool')
    return reshape(l_pool, ([0], [2]), name=id_tag + 'broadcast_dot')
Ejemplo n.º 3
0
def build_res_stafg():

    net = collections.OrderedDict()
    # INPUTS----------------------------------------
    net['sent_input'] = InputLayer((None, CFG['SEQUENCE LENGTH']),
                                   input_var=T.imatrix())
    net['word_emb'] = EmbeddingLayer(net['sent_input'], input_size=CFG['VOCAB SIZE']+3,\
                                    output_size=CFG['WORD VECTOR SIZE'],W=np.copy(CFG['wemb']))

    net['vis_input'] = InputLayer((None,CFG['VISUAL LENGTH'], CFG['VIS SIZE']))
    # key words model-------------------------------------
    net['vis_mean_pool'] = FeaturePoolLayer(net['vis_input'],
                                                CFG['VISUAL LENGTH'],pool_function=T.mean)
    net['ctx_vis_reshp'] = ReshapeLayer(net['vis_mean_pool'],(-1,CFG['VIS SIZE']))
    net['global_vis'] = DenseLayer(net['ctx_vis_reshp'],num_units=CFG['EMBEDDING SIZE'],nonlinearity=linear)
    net['key_words_prob'] = DenseLayer(DropoutLayer(net['global_vis']), num_units=CFG['VOCAB SIZE']+3,nonlinearity=sigmoid)
    # gru model--------------------------------------
    net['mask_input'] = InputLayer((None, CFG['SEQUENCE LENGTH']))
    net['sgru'] = GRULayer(net['word_emb'],num_units=CFG['EMBEDDING SIZE'], \
                            mask_input=net['mask_input'],hid_init=net['global_vis'])
    net['sta_gru'] = CTXAttentionGRULayer([net['sgru'],net['vis_input'],net['global_vis']],
                                           num_units=CFG['EMBEDDING SIZE'],
                                           mask_input=net['mask_input'])
    net['fusion'] = DropoutLayer(ConcatLayer([net['sta_gru'],net['gru']],axis=2), p=0.5)
    net['fusion_reshp'] = ReshapeLayer(net['fusion'], (-1,CFG['EMBEDDING SIZE']*2))
    net['word_prob'] = DenseLayer(net['fusion_reshp'], num_units=CFG['VOCAB SIZE']+3,
                                  nonlinearity=softmax)
    net['sent_prob'] = ReshapeLayer(net['word_prob'],(-1,CFG['SEQUENCE LENGTH'], CFG['VOCAB SIZE']+3))
    return net
Ejemplo n.º 4
0
 def add_residual_dense_maxout_block(self,
                                     network,
                                     num_nodes=240,
                                     dropout=0.5):
     network = lasagne.layers.DropoutLayer(network, p=self.dropout)
     identity = network
     network = DenseLayer(network,
                          nonlinearity=rectify,
                          num_units=self.num_nodes,
                          W=HeNormal(gain=.01))
     network = FeaturePoolLayer(incoming=network,
                                pool_size=2,
                                axis=1,
                                pool_function=theano.tensor.max)
     return NonlinearityLayer(ElemwiseSumLayer(
         [identity, network.input_layer]),
                              nonlinearity=rectify)
Ejemplo n.º 5
0
def choosy(network, cropsz, batchsz):
    # 1st. Data size 117 -> 111 -> 55
    network = Conv2DLayer(network, 64, (7, 7), stride=1, W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (3, 3), stride=2)

    # 2nd. Data size 55 -> 27
    network = Conv2DLayer(network,
                          112, (5, 5),
                          stride=1,
                          pad='same',
                          W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (3, 3), stride=2)

    # 3rd.  Data size 27 -> 13
    network = Conv2DLayer(network,
                          192, (3, 3),
                          stride=1,
                          pad='same',
                          W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (3, 3), stride=2)

    # 4th.  Data size 11 -> 5
    network = Conv2DLayer(network, 320, (3, 3), stride=1, W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (3, 3), stride=2)

    # 5th. Data size 5 -> 3
    network = Conv2DLayer(network, 512, (3, 3), nonlinearity=None)
    network = prelu(network)
    network = BatchNormLayer(network)

    # 6th. Data size 3 -> 1
    network = lasagne.layers.DenseLayer(network, 512, nonlinearity=None)
    network = DropoutLayer(network)
    network = FeaturePoolLayer(network, 2)

    return network
Ejemplo n.º 6
0
def cslim(network, cropsz, batchsz):
    # 1st
    network = Conv2DLayer(network, 64, (5, 5), stride=2, W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (5, 5), stride=2)
    # 2nd
    network = Conv2DLayer(network,
                          96, (5, 5),
                          stride=1,
                          pad='same',
                          W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (5, 5), stride=2)
    # 3rd
    network = Conv2DLayer(network,
                          128, (3, 3),
                          stride=1,
                          pad='same',
                          W=HeUniform('relu'))
    network = prelu(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (3, 3), stride=2)
    # 4th
    network = Conv2DLayer(network,
                          128, (3, 3),
                          stride=1,
                          pad='same',
                          W=HeUniform('relu'))
    network = prelu(network)
    network = DropoutLayer(network)
    network = BatchNormLayer(network)
    network = MaxPool2DLayer(network, (3, 3), stride=2)
    # 5th
    network = lasagne.layers.DenseLayer(network, 512, nonlinearity=None)
    network = DropoutLayer(network)
    network = FeaturePoolLayer(network, 2)

    return network
Ejemplo n.º 7
0
 def slice_mean_layer(incoming, pool_size, axis=-1):
     l_slice = SliceLayer(incoming, -1)
     return FeaturePoolLayer(l_slice,
                             pool_size=pool_size,
                             axis=axis,
                             pool_function=T.mean)
Ejemplo n.º 8
0
 def mean_layer(incoming, pool_size, axis=-1):
     l_out = FeaturePoolLayer(incoming,
                              pool_size=pool_size,
                              axis=axis,
                              pool_function=T.mean)
     return SliceLayer(l_out, indices=0, axis=axis)
Ejemplo n.º 9
0
    def _get_l_out(self, input_vars):
        check_options(self.options)
        id_tag = (self.id + '/') if self.id else ''

        input_var = input_vars[0]
        context_vars = input_vars[1:]

        l_in = InputLayer(shape=(None, self.seq_vec.max_len),
                          input_var=input_var,
                          name=id_tag + 'desc_input')
        l_in_embed = EmbeddingLayer(
            l_in,
            input_size=len(self.seq_vec.tokens),
            output_size=self.options.listener_cell_size,
            name=id_tag + 'desc_embed')

        # Context repr has shape (batch_size, seq_len, context_len * repr_size)
        l_context_repr, context_inputs = self.color_vec.get_input_layer(
            context_vars,
            recurrent_length=self.seq_vec.max_len,
            cell_size=self.options.listener_cell_size,
            context_len=self.context_len,
            id=self.id)
        l_context_repr = reshape(
            l_context_repr,
            ([0], [1], self.context_len, self.color_vec.output_size))
        l_hidden_context = dimshuffle(l_context_repr, (0, 3, 1, 2),
                                      name=id_tag + 'shuffle_in')
        for i in range(1, self.options.listener_hidden_color_layers + 1):
            l_hidden_context = NINLayer(
                l_hidden_context,
                num_units=self.options.listener_cell_size,
                nonlinearity=NONLINEARITIES[
                    self.options.listener_nonlinearity],
                b=Constant(0.1),
                name=id_tag + 'hidden_context%d' % i)
        l_pool = FeaturePoolLayer(l_hidden_context,
                                  pool_size=self.context_len,
                                  axis=3,
                                  pool_function=T.mean,
                                  name=id_tag + 'pool')
        l_pool_squeezed = reshape(l_pool, ([0], [1], [2]),
                                  name=id_tag + 'pool_squeezed')
        l_pool_shuffle = dimshuffle(l_pool_squeezed, (0, 2, 1),
                                    name=id_tag + 'shuffle_out')
        l_concat = ConcatLayer([l_pool_shuffle, l_in_embed],
                               axis=2,
                               name=id_tag + 'concat_inp_context')

        cell = CELLS[self.options.listener_cell]
        cell_kwargs = {
            'grad_clipping': self.options.listener_grad_clipping,
            'num_units': self.options.listener_cell_size,
        }
        if self.options.listener_cell == 'LSTM':
            cell_kwargs['forgetgate'] = Gate(
                b=Constant(self.options.listener_forget_bias))
        if self.options.listener_cell != 'GRU':
            cell_kwargs['nonlinearity'] = NONLINEARITIES[
                self.options.listener_nonlinearity]

        # l_rec1_drop = l_concat
        l_rec1 = cell(l_concat, name=id_tag + 'rec1', **cell_kwargs)
        if self.options.listener_dropout > 0.0:
            l_rec1_drop = DropoutLayer(l_rec1,
                                       p=self.options.listener_dropout,
                                       name=id_tag + 'rec1_drop')
        else:
            l_rec1_drop = l_rec1
        l_rec2 = cell(l_rec1_drop,
                      name=id_tag + 'rec2',
                      only_return_final=True,
                      **cell_kwargs)
        if self.options.listener_dropout > 0.0:
            l_rec2_drop = DropoutLayer(l_rec2,
                                       p=self.options.listener_dropout,
                                       name=id_tag + 'rec2_drop')
        else:
            l_rec2_drop = l_rec2

        l_rec2_drop = NINLayer(l_rec2_drop,
                               num_units=self.options.listener_cell_size,
                               nonlinearity=None,
                               name=id_tag + 'rec2_dense')

        # Context is fed into the RNN as one copy for each time step; just use
        # the first time step for output.
        # Input shape: (batch_size, repr_size, seq_len, context_len)
        # Output shape: (batch_size, repr_size, context_len)
        l_context_nonrec = SliceLayer(l_hidden_context,
                                      indices=0,
                                      axis=2,
                                      name=id_tag + 'context_nonrec')
        l_pool_nonrec = SliceLayer(l_pool_squeezed,
                                   indices=0,
                                   axis=2,
                                   name=id_tag + 'pool_nonrec')

        # Output shape: (batch_size, repr_size, context_len)
        l_sub = broadcast_sub_layer(
            l_pool_nonrec,
            l_context_nonrec,
            feature_dim=self.options.listener_cell_size,
            id_tag=id_tag)
        # Output shape: (batch_size, repr_size * 2, context_len)
        l_concat_sub = ConcatLayer([l_context_nonrec, l_sub],
                                   axis=1,
                                   name=id_tag + 'concat_inp_context')
        # Output shape: (batch_size, cell_size, context_len)
        l_hidden = NINLayer(l_concat_sub,
                            num_units=self.options.listener_cell_size,
                            nonlinearity=None,
                            name=id_tag + 'hidden')
        if self.options.listener_dropout > 0.0:
            l_hidden_drop = DropoutLayer(l_hidden,
                                         p=self.options.listener_dropout,
                                         name=id_tag + 'hidden_drop')
        else:
            l_hidden_drop = l_hidden

        l_dot = broadcast_dot_layer(
            l_rec2_drop,
            l_hidden_drop,
            feature_dim=self.options.listener_cell_size,
            id_tag=id_tag)
        l_dot_bias = l_dot  # BiasLayer(l_dot, name=id_tag + 'dot_bias')
        l_dot_clipped = NonlinearityLayer(
            l_dot_bias,
            nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
            name=id_tag + 'dot_clipped')
        l_scores = NonlinearityLayer(l_dot_clipped,
                                     nonlinearity=softmax,
                                     name=id_tag + 'scores')

        return l_scores, [l_in] + context_inputs
Ejemplo n.º 10
0
    def predict(self, input, hidden_state, Ws, bs):

        npx = self.npx  # image size
        nc = self.input_seqlen
        filter_size = self.dynamic_filter_size[0]
        f = 0

        ###############################
        #  filter-generating network  #
        ###############################
        ## encoder
        output = ConvLayer(input,
                           num_filters=64,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify,
                           untie_biases=True)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1
        output = ConvLayer(output,
                           num_filters=64,
                           filter_size=(3, 3),
                           stride=(2, 2),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1
        output = ConvLayer(output,
                           num_filters=128,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1
        # output = ConvLayer(output, num_filters=64, filter_size=(3,3), stride=(1,1), pad='same', W=Ws[f], b=bs[f], nonlinearity=leaky_rectify); Ws[f] = output.W; bs[f] = output.b; f = f+1

        ## mid
        output = ConvLayer(output,
                           num_filters=128,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1

        hidden = ConvLayer(hidden_state,
                           num_filters=128,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = hidden.W
        bs[f] = hidden.b
        f = f + 1
        hidden = ConvLayer(hidden,
                           num_filters=128,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = hidden.W
        bs[f] = hidden.b
        f = f + 1
        output = ElemwiseSumLayer([output, hidden])
        hidden_state = output

        ## decoder
        # output = ConvLayer(output, num_filters=64, filter_size=(3,3), stride=(1,1), pad='same', W=Ws[f], b=bs[f], nonlinearity=leaky_rectify); Ws[f] = output.W; bs[f] = output.b; f = f+1
        output = ConvLayer(output,
                           num_filters=128,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1
        output = Upscale2DLayer(output, scale_factor=2)
        output = ConvLayer(output,
                           num_filters=64,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1
        output = ConvLayer(output,
                           num_filters=128,
                           filter_size=(3, 3),
                           stride=(1, 1),
                           pad='same',
                           W=Ws[f],
                           b=bs[f],
                           nonlinearity=leaky_rectify)
        Ws[f] = output.W
        bs[f] = output.b
        f = f + 1

        # output = ConvLayer(output, num_filters=128, filter_size=(1,1), stride=(1,1), pad='same', W=Ws[f], b=bs[f], nonlinearity=leaky_rectify); Ws[f] = output.W; bs[f] = output.b; f = f+1

        ## filter-generating layers
        l_filter = ConvLayer(output,
                             num_filters=filter_size**2 + self.dynamic_bias,
                             filter_size=(1, 1),
                             stride=(1, 1),
                             pad=(0, 0),
                             W=Ws[f],
                             b=bs[f],
                             nonlinearity=identity)
        Ws[f] = l_filter.W
        bs[f] = l_filter.b
        f = f + 1

        #########################
        #  transformer network  #
        #########################
        ## get inputs
        output = SliceLayer(
            input, indices=slice(self.nInputs - 1, self.nInputs),
            axis=1)  # select the last (most recent) frame from the inputs

        ## add a bias
        if self.dynamic_bias:
            filters_biases = SliceLayer(l_filter,
                                        indices=slice(filter_size**2,
                                                      filter_size**2 + 1),
                                        axis=1)
            output = ConcatLayer([output, filters_biases])
            output = FeaturePoolLayer(output,
                                      pool_size=2,
                                      pool_function=theano.tensor.sum)

        ## dynamic convolution
        filters = SliceLayer(l_filter,
                             indices=slice(0, filter_size**2),
                             axis=1)

        # filters = FeaturePoolLayer(filters, pool_size=9*9, pool_function=theano.tensor.nnet.softmax)
        filters = DimshuffleLayer(filters, (0, 2, 3, 1))
        filters = ReshapeLayer(filters, shape=(-1, filter_size**2))
        filters = NonlinearityLayer(filters, nonlinearity=softmax)
        filters = ReshapeLayer(filters, shape=(-1, npx, npx, filter_size**2))
        filters = DimshuffleLayer(filters, (0, 3, 1, 2))

        output_dynconv = DynamicFilterLayer([output, filters],
                                            filter_size=(filter_size,
                                                         filter_size, 1),
                                            pad=(filter_size // 2,
                                                 filter_size // 2))

        ########################
        #  refinement network  #
        ########################
        if self.refinement_network:
            output = ConcatLayer([output_dynconv, input])
            output = ConvLayer(output,
                               num_filters=32,
                               filter_size=(3, 3),
                               stride=(1, 1),
                               pad='same',
                               W=Ws[f],
                               b=bs[f],
                               nonlinearity=leaky_rectify)
            Ws[f] = output.W
            bs[f] = output.b
            f = f + 1
            output = ConvLayer(output,
                               num_filters=64,
                               filter_size=(3, 3),
                               stride=(1, 1),
                               pad='same',
                               W=Ws[f],
                               b=bs[f],
                               nonlinearity=leaky_rectify)
            Ws[f] = output.W
            bs[f] = output.b
            f = f + 1
            output = ConvLayer(output,
                               num_filters=32,
                               filter_size=(3, 3),
                               stride=(1, 1),
                               pad='same',
                               W=Ws[f],
                               b=bs[f],
                               nonlinearity=leaky_rectify)
            Ws[f] = output.W
            bs[f] = output.b
            f = f + 1
            output = ConvLayer(output,
                               num_filters=1,
                               filter_size=(3, 3),
                               stride=(1, 1),
                               pad='same',
                               W=Ws[f],
                               b=bs[f],
                               nonlinearity=leaky_rectify)
            Ws[f] = output.W
            bs[f] = output.b
            f = f + 1
            output = ElemwiseSumLayer([output_dynconv, output
                                       ])  # this is a residual connection
        else:
            output = output_dynconv

        return output, hidden_state, filters
Ejemplo n.º 11
0
    def build_model(width=512, height=512, filename=None,
                    n_classes=5, batch_size=None, p_conv=0.0):
        """Setup network structure for the original formulation of JeffreyDF's
           network and optionally load pretrained weights

        Parameters
        ----------
        width : Optional[int]
            image width
        height : Optional[int]
            image height
        filename : Optional[str]
            if filename is not None, weights are loaded from filename
        n_classes : Optional[int]
            default 5 for transfer learning on Kaggle DR data
        batch_size : should only be set if all batches have the same size!
        p_conv: dropout applied to conv. layers, by default turned off (0.0)

        Returns
        -------
        dict
            one lasagne layer per key

        Notes
        -----
            Reference: Jeffrey De Fauw, 2015:
            http://jeffreydf.github.io/diabetic-retinopathy-detection/

            Download pretrained weights from:
            https://github.com/JeffreyDF/kaggle_diabetic_retinopathy/blob/
            master/dumps/2015_07_17_123003_PARAMSDUMP.pkl

           original net has leaky rectifier units

        """

        net = OrderedDict()

        net['0'] = InputLayer((batch_size, 3, width, height), name='images')
        net['1'] = ConvLayer(net['0'], 32, 7, stride=(2, 2), pad='same',
                             untie_biases=True,
                             nonlinearity=LeakyRectify(leakiness=0.5),
                             W=lasagne.init.Orthogonal(1.0),
                             b=lasagne.init.Constant(0.1))
        net['1d'] = DropoutLayer(net['1'], p=p_conv)
        net['2'] = MaxPool2DLayer(net['1d'], 3, stride=(2, 2))
        net['3'] = ConvLayer(net['2'], 32, 3, stride=(1, 1), pad='same',
                             untie_biases=True,
                             nonlinearity=LeakyRectify(leakiness=0.5),
                             W=lasagne.init.Orthogonal(1.0),
                             b=lasagne.init.Constant(0.1))
        net['3d'] = DropoutLayer(net['3'], p=p_conv)
        net['4'] = ConvLayer(net['3d'], 32, 3, stride=(1, 1), pad='same',
                             untie_biases=True,
                             nonlinearity=LeakyRectify(leakiness=0.5),
                             W=lasagne.init.Orthogonal(1.0),
                             b=lasagne.init.Constant(0.1))
        net['4d'] = DropoutLayer(net['4'], p=p_conv)
        net['5'] = MaxPool2DLayer(net['4d'], 3, stride=(2, 2))
        net['6'] = ConvLayer(net['5'], 64, 3, stride=(1, 1), pad='same',
                             untie_biases=True,
                             nonlinearity=LeakyRectify(leakiness=0.5),
                             W=lasagne.init.Orthogonal(1.0),
                             b=lasagne.init.Constant(0.1))
        net['6d'] = DropoutLayer(net['6'], p=p_conv)
        net['7'] = ConvLayer(net['6d'], 64, 3, stride=(1, 1), pad='same',
                             untie_biases=True,
                             nonlinearity=LeakyRectify(leakiness=0.5),
                             W=lasagne.init.Orthogonal(1.0),
                             b=lasagne.init.Constant(0.1))
        net['7d'] = DropoutLayer(net['7'], p=p_conv)
        net['8'] = MaxPool2DLayer(net['7d'], 3, stride=(2, 2))
        net['9'] = ConvLayer(net['8'], 128, 3, stride=(1, 1), pad='same',
                             untie_biases=True,
                             nonlinearity=LeakyRectify(leakiness=0.5),
                             W=lasagne.init.Orthogonal(1.0),
                             b=lasagne.init.Constant(0.1))
        net['9d'] = DropoutLayer(net['9'], p=p_conv)
        net['10'] = ConvLayer(net['9d'], 128, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['10d'] = DropoutLayer(net['10'], p=p_conv)
        net['11'] = ConvLayer(net['10d'], 128, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['11d'] = DropoutLayer(net['11'], p=p_conv)
        net['12'] = ConvLayer(net['11d'], 128, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['12d'] = DropoutLayer(net['12'], p=p_conv)
        net['13'] = MaxPool2DLayer(net['12d'], 3, stride=(2, 2))
        net['14'] = ConvLayer(net['13'], 256, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['14d'] = DropoutLayer(net['14'], p=p_conv)
        net['15'] = ConvLayer(net['14d'], 256, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['15d'] = DropoutLayer(net['15'], p=p_conv)
        net['16'] = ConvLayer(net['15'], 256, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['16d'] = DropoutLayer(net['16'], p=p_conv)
        net['17'] = ConvLayer(net['16d'], 256, 3, stride=(1, 1), pad='same',
                              untie_biases=True,
                              nonlinearity=LeakyRectify(leakiness=0.5),
                              W=lasagne.init.Orthogonal(1.0),
                              b=lasagne.init.Constant(0.1))
        net['17d'] = DropoutLayer(net['17'], p=p_conv)
        net['18'] = MaxPool2DLayer(net['17d'], 3, stride=(2, 2),
                                   name='coarse_last_pool')
        net['19'] = DropoutLayer(net['18'], p=0.5)
        net['20'] = DenseLayer(net['19'], num_units=1024, nonlinearity=None,
                               W=lasagne.init.Orthogonal(1.0),
                               b=lasagne.init.Constant(0.1),
                               name='first_fc_0')
        net['21'] = FeaturePoolLayer(net['20'], 2)
        net['22'] = InputLayer((batch_size, 2), name='imgdim')
        net['23'] = ConcatLayer([net['21'], net['22']])
        # Combine representations of both eyes
        net['24'] = ReshapeLayer(net['23'],
                                 (-1, net['23'].output_shape[1] * 2))
        net['25'] = DropoutLayer(net['24'], p=0.5)
        net['26'] = DenseLayer(net['25'], num_units=1024, nonlinearity=None,
                               W=lasagne.init.Orthogonal(1.0),
                               b=lasagne.init.Constant(0.1),
                               name='combine_repr_fc')
        net['27'] = FeaturePoolLayer(net['26'], 2)
        net['28'] = DropoutLayer(net['27'], p=0.5)
        net['29'] = DenseLayer(net['28'],
                               num_units=n_classes * 2,
                               nonlinearity=None,
                               W=lasagne.init.Orthogonal(1.0),
                               b=lasagne.init.Constant(0.1))
        # Reshape back to the number of desired classes
        net['30'] = ReshapeLayer(net['29'], (-1, n_classes))
        net['31'] = NonlinearityLayer(net['30'], nonlinearity=softmax)

        if filename is not None:
            with open(filename, 'r') as f:
                weights = pickle.load(f)
            set_all_param_values(net['31'], weights)

        return net
Ejemplo n.º 12
0
 def add_dense_maxout_block(self, network, num_nodes=240, dropout=0.5):
     network = lasagne.layers.DropoutLayer(network, p=self.dropout)
     network = DenseLayer(network,nonlinearity=rectify,num_units=self.num_nodes)
     maxout = FeaturePoolLayer(incoming=network, pool_size=2,axis=1, pool_function=theano.tensor.max)
     return ConcatLayer([network, maxout], axis=1)