예제 #1
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
예제 #2
0
def conv_pool_cnn_c():
    net = {}
    net['input'] = InputLayer((None, 3, 32, 32))
    net['drop_in'] = DropoutLayer(net['input'], p=0.2)

    net['conv1_1'] = ConvLayer(net['drop_in'],
                               num_filters=96,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)
    net['conv1_2'] = ConvLayer(net['conv1_1'],
                               num_filters=96,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)
    net['conv1_3'] = ConvLayer(net['conv1_2'],
                               num_filters=96,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)

    net['conv2_1'] = PoolLayer(net['conv1_3'], pool_size=3, stride=2)
    net['drop2_1'] = DropoutLayer(net['conv2_1'], p=0.5)

    net['conv3_1'] = ConvLayer(net['drop2_1'],
                               num_filters=192,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)
    net['conv3_2'] = ConvLayer(net['conv3_1'],
                               num_filters=192,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)
    net['conv3_3'] = ConvLayer(net['conv3_2'],
                               num_filters=192,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)

    net['conv4_1'] = PoolLayer(net['conv3_3'], pool_size=3, stride=2)
    net['drop4_1'] = DropoutLayer(net['conv4_1'], p=0.5)

    net['conv5_1'] = ConvLayer(net['drop4_1'],
                               num_filters=192,
                               filter_size=3,
                               pad=1,
                               flip_filters=False)
    net['conv6_1'] = ConvLayer(net['conv5_1'],
                               num_filters=192,
                               filter_size=1,
                               flip_filters=False)
    net['conv7_1'] = ConvLayer(net['conv6_1'],
                               num_filters=10,
                               filter_size=1,
                               flip_filters=False)
    net['global_avg'] = GlobalPoolLayer(net['conv7_1'])
    net['output'] = NonlinearityLayer(net['global_avg'], softmax)

    return net
예제 #3
0
def makeNeuralNet(input_var=None):
    net = {}
    net['input'] = InputLayer(shape=(None, 3, 224, 224), input_var=input_var)
    net['bnorm'] = BatchNormLayer(net['input'])
    net['conv1'] = ConvLayer(net['bnorm'],
                             num_filters=96,
                             filter_size=5,
                             stride=2)  #96*112*112
    net['norm1'] = NormLayer(
        net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)  #96*37...approx
    net['conv2'] = ConvLayer(net['pool1'],
                             num_filters=256,
                             filter_size=5,
                             pad=1)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool2'], num_units=1024)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.2)
    net['_fc7'] = DenseLayer(net['drop6'], num_units=256)
    net['_drop7'] = DropoutLayer(net['_fc7'], p=0.2)
    net['_fc8out'] = DenseLayer(net['_drop7'],
                                num_units=1,
                                nonlinearity=lasagne.nonlinearities.sigmoid)
    output_layer_driver = net['_fc8out']
    return output_layer_driver, net
예제 #4
0
def add_layer(incoming, num_channels, dropout):
    layer = ScaleLayer(incoming)
    layer = BiasLayer(layer)

    # Bottleneck layer to reduce number of input channels to 4 times the number of output channels
    layer = NonlinearityLayer(layer, nonlinearity=rectify)
    layer = Conv2DLayer(layer,
                        num_filters=4 * num_channels,
                        filter_size=(1, 1),
                        stride=(1, 1),
                        W=HeNormal(gain='relu'), b=None,
                        flip_filters=False,
                        nonlinearity=None)
    layer = BatchNormLayer(layer, beta=None, gamma=None)
    if dropout > 0:
        layer = DropoutLayer(layer, p=dropout)

    # Convolutional layer (using padding to keep same dimensions)
    layer = NonlinearityLayer(layer, nonlinearity=rectify)
    layer = Conv2DLayer(layer,
                        num_filters=num_channels,
                        filter_size=(3, 3),
                        stride=(1, 1),
                        W=HeNormal(gain='relu'), b=None,
                        pad='same',
                        flip_filters=False,
                        nonlinearity=None)
    layer = BatchNormLayer(layer, beta=None, gamma=None)
    if dropout > 0:
        layer = DropoutLayer(layer, p=dropout)

    # Concatenate the input filters with the new filters
    layer = ConcatLayer([incoming, layer], axis=1)

    return layer
def Fully_Conv_4(num_of_classes, input_var=None):
    net = {}
    net['input'] = InputLayer(shape=(None, 3, 224, 224), input_var=input_var)
    net['conv1_1'] = ConvLayer(net['input'],
                               32,
                               4, (2, 2),
                               pad=0,
                               flip_filters=False)
    net['conv1_2'] = ConvLayer(net['conv1_1'],
                               64,
                               4,
                               pad=0,
                               flip_filters=False)
    net['conv2_1'] = ConvLayer(net['conv1_2'],
                               128,
                               6, (2, 2),
                               pad=0,
                               flip_filters=False)
    net['conv2_2'] = ConvLayer(net['conv2_1'],
                               256,
                               6, (2, 2),
                               pad=0,
                               flip_filters=False)
    net['fc3'] = DenseLayer(net['conv2_2'], num_units=1024)
    net['fc3_dropout'] = DropoutLayer(net['fc3'], p=0.5)
    net['fc4'] = DenseLayer(net['fc3_dropout'], num_units=512)
    net['fc4_dropout'] = DropoutLayer(net['fc4'], p=0.5)
    net['fc5'] = DenseLayer(net['fc4_dropout'],
                            num_units=num_of_classes,
                            nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc5'], softmax)

    return net
예제 #6
0
def build_small_model():
    net = {}
    net['input_layer'] = InputLayer((None,3,128,128))
    net['conv1_1'] = ConvLayer(
        net['input_layer'], 64, 3, pad=1)#, flip_filters=False)
    net['conv1_2'] = ConvLayer(
        net['conv1_1'], 64, 3, pad=1)#, flip_filters=False)
    net['pool1'] = PoolLayer(net['conv1_2'], 2)
    net['conv2_1'] = ConvLayer(
        net['pool1'], 128, 3, pad=1)#, flip_filters=False)
    net['conv2_2'] = ConvLayer(
        net['conv2_1'], 128, 3, pad=1 )#, flip_filters=False)
    net['pool2'] = PoolLayer(net['conv2_2'], 2)
    net['conv3_1'] = ConvLayer(
        net['pool2'], 256, 3, pad=1 )#, flip_filters=False)
    net['conv3_2'] = ConvLayer(
        net['conv3_1'], 256, 3, pad=1)#, flip_filters=False)
    net['pool3'] = PoolLayer(net['conv3_2'], 2)
    net['conv4_1'] = ConvLayer(
        net['pool3'], 512, 3, pad=1)#, flip_filters=False)
    net['pool4'] = PoolLayer(net['conv4_1'], 2)
    net['fc6'] = DenseLayer(net['pool4'], num_units=200)
    net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=100)
    net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(
        net['fc7_dropout'], num_units=2, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)
    return net
def construct_vgg16net(channels=1, no_f_base=64, f_size=3, dropout=False, bs=None, num_classes=3, pad=1,c_nonlinearity=lasagne.nonlinearities.rectify,f_nonlinearity=lasagne.nonlinearities.tanh, input_dim=[512,512], flip=False, fcn=4096):
    net = {}
    net["input"] = InputLayer(shape=(bs, channels, input_dim[0], input_dim[1]))
    net["conv_11"] = Conv2DLayer(net["input"], no_f_base, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_12"] = Conv2DLayer(net["conv_11"], no_f_base, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool1"] = Pool2DLayer(net["conv_12"], 2)
    net["conv_21"] = Conv2DLayer(net["pool1"], no_f_base*2, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_22"] = Conv2DLayer(net["conv_21"], no_f_base*2, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool2"] = Pool2DLayer(net["conv_22"], 2)
    net["conv_31"] = Conv2DLayer(net["pool2"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_32"] = Conv2DLayer(net["conv_31"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_33"] = Conv2DLayer(net["conv_32"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool3"] = Pool2DLayer(net["conv_33"], 2)
    net["conv_41"] = Conv2DLayer(net["pool3"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_42"] = Conv2DLayer(net["conv_41"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_43"] = Conv2DLayer(net["conv_42"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool4"] = Pool2DLayer(net["conv_43"], 2)
    net["conv_51"] = Conv2DLayer(net["pool4"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_52"] = Conv2DLayer(net["conv_51"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_53"] = Conv2DLayer(net["conv_52"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool5"] = Pool2DLayer(net["conv_53"], 2)

    net["full_con1"] = DenseLayer(net["pool5"], num_units=fcn, nonlinearity=f_nonlinearity)
    net["drop_full_con1"] = DropoutLayer(net["full_con1"], p=0.5)
    net["full_con2"] = DenseLayer(net["drop_full_con1"], num_units=fcn, nonlinearity=f_nonlinearity)
    net["drop_full_con2"] = DropoutLayer(net["full_con2"], p=0.5)
    net["full_con3"] = DenseLayer(net["drop_full_con2"], num_units=num_classes, nonlinearity=None)
    net["out"] = NonlinearityLayer(net["full_con3"], nonlinearity=lasagne.nonlinearities.softmax)
    return net
    del net
예제 #8
0
def build_lstm_decorer():
    net = collections.OrderedDict()
    net['sent_input'] = InputLayer((None, CFG['SEQUENCE LENGTH'] - 1),
                                   input_var=T.imatrix())
    net['word_emb'] = EmbeddingLayer(net['sent_input'], input_size=CFG['VOCAB SIZE'],\
                                    output_size=CFG['EMBEDDING SIZE'])
    net['vis_input'] = InputLayer((None, CFG['VIS SIZE']),
                                  input_var=T.matrix())
    net['vis_emb'] = DenseLayer(net['vis_input'],
                                num_units=CFG['EMBEDDING SIZE'],
                                nonlinearity=lasagne.nonlinearities.identity)
    net['vis_emb_reshp'] = ReshapeLayer(net['vis_emb'],
                                        (-1, 1, CFG['EMBEDDING SIZE']))
    net['decorder_input'] = ConcatLayer(
        [net['vis_emb_reshp'], net['word_emb']])
    net['feat_dropout'] = DropoutLayer(net['decorder_input'], p=0.5)

    net['mask_input'] = InputLayer((None, CFG['SEQUENCE LENGTH']))
    net['lstm'] = LSTMLayer(net['feat_dropout'],num_units=CFG['EMBEDDING SIZE'], \
                            mask_input=net['mask_input'], grad_clipping=5.)
    net['lstm_dropout'] = DropoutLayer(net['lstm'], p=0.5)
    net['lstm_reshp'] = ReshapeLayer(net['lstm_dropout'],
                                     (-1, CFG['EMBEDDING SIZE']))
    net['word_prob'] = DenseLayer(net['lstm_reshp'],
                                  num_units=CFG['VOCAB SIZE'] + 2,
                                  nonlinearity=softmax)
    net['sent_prob'] = ReshapeLayer(
        net['word_prob'], (-1, CFG['SEQUENCE LENGTH'], CFG['VOCAB SIZE'] + 2))
    return net
def construct_vgg13net(channels=1, no_f_base=64, f_size=3, dropout=False, bs=None, num_classes=3, pad=1,nonlinearity=lasagne.nonlinearities.rectify, input_dim=[512,512], flip=True, fcn=4096):
    net = {}
    net["input"] = InputLayer(shape=(bs, channels, input_dim[0], input_dim[1]))
    net["conv_11"] = Conv2DLayer(net["input"], no_f_base, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["conv_12"] = Conv2DLayer(net["conv_11"], no_f_base, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["pool1"] = Pool2DLayer(net["conv_12"], 2)
    net["conv_21"] = Conv2DLayer(net["pool1"], no_f_base*2, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["conv_22"] = Conv2DLayer(net["conv_21"], no_f_base*2, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["pool2"] = Pool2DLayer(net["conv_22"], 2)
    net["conv_31"] = Conv2DLayer(net["pool2"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["conv_32"] = Conv2DLayer(net["conv_31"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["conv_33"] = Conv2DLayer(net["conv_32"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["pool3"] = Pool2DLayer(net["conv_33"], 2)
    net["conv_41"] = Conv2DLayer(net["pool3"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["conv_42"] = Conv2DLayer(net["conv_41"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["conv_43"] = Conv2DLayer(net["conv_42"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=nonlinearity)
    net["pool4"] = Pool2DLayer(net["conv_43"], 2)

    drop1 = net["full_con1"] = DenseLayer(net["pool4"], num_units=fcn, nonlinearity=nonlinearity)
    if dropout:
        drop1 = DropoutLayer(drop1, p=0.5)
    drop2 = net["full_con2"] = DenseLayer(drop1, num_units=fcn, nonlinearity=nonlinearity)
    if dropout:
        drop2 = DropoutLayer(drop2, p=0.5)
    net["full_con3"] = DenseLayer(drop2, num_units=num_classes, nonlinearity=None)
    net["out"] = NonlinearityLayer(net["full_con3"], nonlinearity=lasagne.nonlinearities.softmax)
    return net
    del net
예제 #10
0
def skel_encoder(l_in, tconv_sz, filter_dilation, num_tc_filters, dropout):
    warmup = 16

    l1 = lasagne.layers.DenseLayer(
        l_in, num_units=480,
        num_leading_axes=2,
        nonlinearity=None, b=None)
    l1 = BatchNormLayer(l1, axes=(0, 1))
    l1 = NonlinearityLayer(l1, leaky_rectify)

    d1 = DropoutLayer(l1, p=dropout)

    l2 = lasagne.layers.DenseLayer(
        d1, num_units=480,
        num_leading_axes=2,
        nonlinearity=None, b=None)
    l2 = BatchNormLayer(l2, axes=(0, 1))
    l2 = NonlinearityLayer(l2, leaky_rectify)

    d2 = DropoutLayer(l2, p=dropout)

    l3 = TemporalConv(d2, num_filters=num_tc_filters, filter_size=tconv_sz,
                      filter_dilation=filter_dilation, pad='same',
                      conv_type='regular',
                      nonlinearity=None, b=None)
    l3 = BatchNormLayer(l3, axes=(0, 1))
    l3 = NonlinearityLayer(l3, leaky_rectify)

    return {
        'l_out': l3,
        'warmup': warmup
    }
def feedforward_dropout(train_config):
    sequence_length = train_config['context_frames']
    input_dim = train_config['input_dim']

    num_labels = train_config['num_labels']
    batch_size = train_config.get('batch_size', None)
    layer_sizes = train_config['layer_sizes']
    nonlinearity = train_config.get('nonlinearity', 'tanh')
    nonlinearity = getattr(lasagne.nonlinearities, nonlinearity)

    input_layer = InputLayer(shape=(batch_size, sequence_length, input_dim))
    input_var = input_layer.input_var

    input_layer_reshaped = ReshapeLayer(
        incoming=input_layer,
        shape=(-1 if batch_size is None else batch_size,
               sequence_length * input_dim))
    cur_input = input_layer_reshaped

    [[shouldnull, input_dropout], *hidden_layers] = layer_sizes
    if shouldnull is not None:
        raise Exception("first should be null for dropout")
    cur_input = DropoutLayer(cur_input, input_dropout)
    for size, dropout in hidden_layers:
        cur_input = DenseLayer(incoming=cur_input,
                               num_units=size,
                               nonlinearity=nonlinearity)
        cur_input = DropoutLayer(incoming=cur_input, p=dropout)

    output_layer = DenseLayer(cur_input,
                              num_units=num_labels,
                              nonlinearity=lasagne.nonlinearities.softmax)
    return locals()
예제 #12
0
def build_model():
    net = {}
    net['input'] = InputLayer((None, 3, 224, 224))
    net['conv1_1'] = ConvLayer(net['input'], 64, 3, pad=1)
    net['conv1_2'] = ConvLayer(net['conv1_1'], 64, 3, pad=1)
    net['pool1'] = PoolLayer(net['conv1_2'], 2)
    net['conv2_1'] = ConvLayer(net['pool1'], 128, 3, pad=1)
    net['conv2_2'] = ConvLayer(net['conv2_1'], 128, 3, pad=1)
    net['pool2'] = PoolLayer(net['conv2_2'], 2)
    net['conv3_1'] = ConvLayer(net['pool2'], 256, 3, pad=1)
    net['conv3_2'] = ConvLayer(net['conv3_1'], 256, 3, pad=1)
    net['conv3_3'] = ConvLayer(net['conv3_2'], 256, 3, pad=1)
    net['pool3'] = PoolLayer(net['conv3_3'], 2)
    net['conv4_1'] = ConvLayer(net['pool3'], 512, 3, pad=1)
    net['conv4_2'] = ConvLayer(net['conv4_1'], 512, 3, pad=1)
    net['conv4_3'] = ConvLayer(net['conv4_2'], 512, 3, pad=1)
    net['pool4'] = PoolLayer(net['conv4_3'], 2)
    net['conv5_1'] = ConvLayer(net['pool4'], 512, 3, pad=1)
    net['conv5_2'] = ConvLayer(net['conv5_1'], 512, 3, pad=1)
    net['conv5_3'] = ConvLayer(net['conv5_2'], 512, 3, pad=1)
    net['pool5'] = PoolLayer(net['conv5_3'], 2)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(net['drop7'], num_units=1000, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)

    return net
예제 #13
0
    def _pre_residual_(self, model, num_filters=None, dim_inc=False):
        """Residual block for pre-resnet."""
        num_filters *= self.width
        num_filters, first_stride, out_filters = get_dimensions(
            model, num_filters, dim_inc, self.bottleneck)

        residual = self.nonlinearity(BatchNormLayer(model))
        residual = self.convolution(residual,
                                    num_filters,
                                    stride=first_stride,
                                    filter_size=self.block_config[0])
        if self.bottleneck:
            for filter_size in self.block_config[1:-1]:
                residual = self.nonlinearity(BatchNormLayer(residual))
                if self.dropout > 0:
                    residual = DropoutLayer(residual, self.dropout)
                residual = self.convolution(residual,
                                            num_filters,
                                            filter_size=filter_size)
            residual = self.nonlinearity(BatchNormLayer(residual))
            if self.dropout > 0:
                residual = DropoutLayer(residual, self.dropout)
            residual = self.convolution(residual,
                                        out_filters,
                                        filter_size=self.block_config[-1])
        else:
            for filter_size in self.block_config[1:]:
                residual = self.nonlinearity(BatchNormLayer(residual))
                if self.dropout > 0:
                    residual = DropoutLayer(residual, self.dropout)
                residual = self.convolution(residual,
                                            num_filters,
                                            filter_size=filter_size)
        return residual
예제 #14
0
def build_generator(input_var=None):
    from lasagne.layers import (InputLayer, ReshapeLayer, DenseLayer,
                                batch_norm, DropoutLayer)
    from lasagne.nonlinearities import LeakyRectify, sigmoid
    lrelu = LeakyRectify(0.2)
    # input: 100dim
    layer = InputLayer(shape=(None, 100), input_var=input_var)
    # fully-connected layer
    layer = batch_norm(DenseLayer(layer, 4096))
    # project and reshape
    layer = batch_norm(DropoutLayer(layer, 0.5))
    layer = batch_norm(DenseLayer(layer, 512*4*4))
    layer = batch_norm(DropoutLayer(layer, 0.5))
    layer = ReshapeLayer(layer, ([0], 512, 4, 4))
    # 2 fractional-stride convolutions
    layer = batch_norm(Deconv2DLayer(layer, 256, 3, stride=1, pad=1))
    layer = batch_norm(Deconv2DLayer(layer, 256, 3, stride=2, pad=1))
    # 3 fractional-stride convolutions
    layer = batch_norm(Deconv2DLayer(layer, 128, 3, stride=1, pad=1))
    layer = batch_norm(Deconv2DLayer(layer, 128, 3, stride=1, pad=1))
    layer = batch_norm(Deconv2DLayer(layer, 128, 3, stride=2, pad=1))
    # 3 fractional-stride convolutions
    layer = batch_norm(Deconv2DLayer(layer, 64, 3, stride=1, pad=1))
    layer = batch_norm(Deconv2DLayer(layer, 64, 3, stride=1, pad=1))
    layer = batch_norm(Deconv2DLayer(layer, 64, 3, stride=2, pad=1))
    # 4 fractional-stride convolutions
    layer = batch_norm(Deconv2DLayer(layer, 32, 5, stride=1, pad=2))
    layer = batch_norm(Deconv2DLayer(layer, 32, 5, stride=1, pad=2))
    layer = batch_norm(Deconv2DLayer(layer, 32, 5, stride=1, pad=2))
    layer = Deconv2DLayer(layer, 3, 5, stride=2, pad=2,
                          nonlinearity=sigmoid)
    #layer = ReshapeLayer(layer, (None, 3, 64, 64))
    print ("Generator output:", layer.output_shape)
    return layer
예제 #15
0
def lasagne_model():
    l_in = InputLayer(shape=(None, 1, 28, 28))

    l_conv1 = Conv2DLayer(l_in,
                          num_filters=128,
                          filter_size=(3, 3),
                          nonlinearity=rectify)
    l_conv1b = Conv2DLayer(l_conv1,
                           num_filters=128,
                           filter_size=(3, 3),
                           nonlinearity=rectify)
    l_pool1 = MaxPool2DLayer(l_conv1b, pool_size=(2, 2))
    l_dropout1 = DropoutLayer(l_pool1, p=0.2)

    l_conv2 = Conv2DLayer(l_pool1,
                          num_filters=256,
                          filter_size=(3, 3),
                          nonlinearity=rectify)
    l_conv2b = Conv2DLayer(l_conv2,
                           num_filters=256,
                           filter_size=(3, 3),
                           nonlinearity=rectify)
    l_pool2 = MaxPool2DLayer(l_conv2b, pool_size=(2, 2))
    l_dropout2 = DropoutLayer(l_pool2, p=0.2)

    l_hidden3 = DenseLayer(l_dropout2, num_units=1024, nonlinearity=rectify)
    l_dropout3 = DropoutLayer(l_hidden3, p=0.5)

    l_hidden4 = DenseLayer(l_dropout3, num_units=1024, nonlinearity=rectify)
    l_dropout4 = DropoutLayer(l_hidden4, p=0.5)

    l_out = DenseLayer(l_dropout4, num_units=10, nonlinearity=softmax)

    return l_out
예제 #16
0
    def build_network(self, net, input_var):
        if net is 'vgg19':
            network = InputLayer(shape=(self.num_images, 3, 224, 224),
                                 input_var=input_var)
            network = ConvLayer(network, 64, 3, pad=1)
            network = ConvLayer(network, 64, 3, pad=1)
            network = PoolLayer(network, 2)
            network = ConvLayer(network, 128, 3, pad=1)
            network = ConvLayer(network, 128, 3, pad=1)
            network = PoolLayer(network, 2)
            network = ConvLayer(network, 256, 3, pad=1)
            network = ConvLayer(network, 256, 3, pad=1)
            network = ConvLayer(network, 256, 3, pad=1)
            network = ConvLayer(network, 256, 3, pad=1)
            network = PoolLayer(network, 2)
            network = ConvLayer(network, 512, 3, pad=1)
            network = ConvLayer(network, 512, 3, pad=1)
            network = ConvLayer(network, 512, 3, pad=1)
            network = ConvLayer(network, 512, 3, pad=1)
            network = PoolLayer(network, 2)
            network = ConvLayer(network, 512, 3, pad=1)
            network = ConvLayer(network, 512, 3, pad=1)
            network = ConvLayer(network, 512, 3, pad=1)
            network = ConvLayer(network, 512, 3, pad=1)
            network = PoolLayer(network, 2)
            network = DenseLayer(network, num_units=4096)
            network = DropoutLayer(network, p=0.5)
            network = DenseLayer(network, num_units=4096)
            network = DropoutLayer(network, p=0.5)
            network = DenseLayer(network, num_units=1000, nonlinearity=None)
            network = NonlinearityLayer(network, softmax)
        else:
            print "Input valid Model"

        return network
예제 #17
0
    def get_net(self):
        net = {}
        net['input'] = InputLayer((None, 3, 224, 224))
        net['conv1_1'] = ConvLayer(net['input'], 64, 3, pad=1)
        net['conv1_2'] = ConvLayer(net['conv1_1'], 64, 3, pad=1)
        net['pool1'] = PoolLayer(net['conv1_2'], 2)
        net['conv2_1'] = ConvLayer(net['pool1'], 128, 3, pad=1)
        net['conv2_2'] = ConvLayer(net['conv2_1'], 128, 3, pad=1)
        net['pool2'] = PoolLayer(net['conv2_2'], 2)
        net['conv3_1'] = ConvLayer(net['pool2'], 256, 3, pad=1)
        net['conv3_2'] = ConvLayer(net['conv3_1'], 256, 3, pad=1)
        net['conv3_3'] = ConvLayer(net['conv3_2'], 256, 3, pad=1)
        net['pool3'] = PoolLayer(net['conv3_3'], 2)
        net['conv4_1'] = ConvLayer(net['pool3'], 512, 3, pad=1)
        net['conv4_2'] = ConvLayer(net['conv4_1'], 512, 3, pad=1)
        net['conv4_3'] = ConvLayer(net['conv4_2'], 512, 3, pad=1)
        net['pool4'] = PoolLayer(net['conv4_3'], 2)
        net['conv5_1'] = ConvLayer(net['pool4'], 512, 3, pad=1)
        net['conv5_2'] = ConvLayer(net['conv5_1'], 512, 3, pad=1)
        net['conv5_3'] = ConvLayer(net['conv5_2'], 512, 3, pad=1)
        net['pool5'] = PoolLayer(net['conv5_3'], 2)
        net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
        net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
        net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=4096)
        net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
        net['fc8'] = DenseLayer(net['fc7_dropout'],
                                num_units=1000,
                                nonlinearity=None)
        net['prob'] = NonlinearityLayer(net['fc8'], softmax)
        output_layer = net['prob']

        return net, net['prob']  # the whole net and the output layer
예제 #18
0
def build_fc_net(input_var=None):
    # Input : imagees in 3 channels
    l_in = InputLayer(shape=(None, 3, 48, 48), input_var=input_var)

    # droupout of 20%
    l_in_drop = DropoutLayer(l_in, p=0.2)

    l_hid1 = DenseLayer(l_in_drop,
                        num_units=800,
                        nonlinearity=lasagne.nonlinearities.rectify,
                        W=lasagne.init.GlorotUniform())

    # dropout of 50%:
    l_hid1_drop = DropoutLayer(l_hid1, p=0.5)

    l_hid2 = DenseLayer(l_hid1_drop,
                        num_units=800,
                        nonlinearity=lasagne.nonlinearities.rectify)

    # 50% dropout again:
    l_hid2_drop = DropoutLayer(l_hid2, p=0.5)

    l_out = DenseLayer(l_hid2_drop,
                       num_units=7,
                       nonlinearity=lasagne.nonlinearities.softmax)

    return l_out
예제 #19
0
def build_model(input_var):
    """ 
    Builds the classic VGG-19 model using Lasagne wrapper to Theano.
    """
    net = {}
    net['input'] = InputLayer((None, 3, 224, 224), input_var=input_var)
    net['conv1_1'] = ConvLayer(
        net['input'], 64, 3, pad=1)
    input = np.random.randn(2, 3, 224, 224).astype(np.float32)
    # out1 = lasagne.layers.get_output(net['conv1_1'], input)
    # print "Output: ", out1.eval()
    # print "Output: ", out1.shape.eval()
#    fout1 = theano.function([input_var], out1)
#    print
    net['conv1_2'] = ConvLayer(
        net['conv1_1'], 64, 3, pad=1)
    net['pool1'] = PoolLayer(net['conv1_2'], 2)
    net['conv2_1'] = ConvLayer(
        net['pool1'], 128, 3, pad=1)
    net['conv2_2'] = ConvLayer(
        net['conv2_1'], 128, 3, pad=1)
    net['pool2'] = PoolLayer(net['conv2_2'], 2)
    net['conv3_1'] = ConvLayer(
        net['pool2'], 256, 3, pad=1)
    net['conv3_2'] = ConvLayer(
        net['conv3_1'], 256, 3, pad=1)
    net['conv3_3'] = ConvLayer(
        net['conv3_2'], 256, 3, pad=1)
    net['conv3_4'] = ConvLayer(
        net['conv3_3'], 256, 3, pad=1)
    net['pool3'] = PoolLayer(net['conv3_4'], 2)
    net['conv4_1'] = ConvLayer(
        net['pool3'], 512, 3, pad=1)
    net['conv4_2'] = ConvLayer(
        net['conv4_1'], 512, 3, pad=1)
    net['conv4_3'] = ConvLayer(
        net['conv4_2'], 512, 3, pad=1)
    net['conv4_4'] = ConvLayer(
        net['conv4_3'], 512, 3, pad=1)
    net['pool4'] = PoolLayer(net['conv4_4'], 2)
    net['conv5_1'] = ConvLayer(
        net['pool4'], 512, 3, pad=1)
    net['conv5_2'] = ConvLayer(
        net['conv5_1'], 512, 3, pad=1)
    net['conv5_3'] = ConvLayer(
        net['conv5_2'], 512, 3, pad=1)
    net['conv5_4'] = ConvLayer(
        net['conv5_3'], 512, 3, pad=1)
    net['pool5'] = PoolLayer(net['conv5_4'], 2)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=4096)
    net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(
        net['fc7_dropout'], num_units=1000, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)

    return net
예제 #20
0
def get_model(input_var, target_var, multiply_var):

    # input layer with unspecified batch size
    layer_input     = InputLayer(shape=(None, 30, 80, 80), input_var=input_var) #InputLayer(shape=(None, 1, 30, 64, 64), input_var=input_var)
    layer_0         = DimshuffleLayer(layer_input, (0, 'x', 1, 2, 3))

    # Z-score?

    # Convolution then batchNormalisation then activation layer, then zero padding layer followed by a dropout layer
    layer_1         = batch_norm(Conv3DDNNLayer(incoming=layer_0, num_filters=16, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_2         = batch_norm(Conv3DDNNLayer(incoming=layer_1, num_filters=16, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_3         = MaxPool3DDNNLayer(layer_2, pool_size=(2, 2, 2), stride=(2, 2, 2), pad=(1, 1, 1))
    layer_4         = DropoutLayer(layer_3, p=0.25)

    # Convolution then batchNormalisation then activation layer, then zero padding layer followed by a dropout layer
    layer_5         = batch_norm(Conv3DDNNLayer(incoming=layer_4, num_filters=32, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_6         = batch_norm(Conv3DDNNLayer(incoming=layer_5, num_filters=32, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_7         = MaxPool3DDNNLayer(layer_6, pool_size=(2, 2, 2), stride=(2, 2, 2), pad=(1, 1, 1))
    layer_8         = DropoutLayer(layer_7, p=0.25)
    
    # Convolution then batchNormalisation then activation layer, then zero padding layer followed by a dropout layer
    layer_5         = batch_norm(Conv3DDNNLayer(incoming=layer_8, num_filters=64, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_6         = batch_norm(Conv3DDNNLayer(incoming=layer_5, num_filters=64, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_7         = batch_norm(Conv3DDNNLayer(incoming=layer_6, num_filters=64, filter_size=(3,3,3), stride=(1,1,1), pad='same', nonlinearity=leaky_rectify))
    layer_8         = MaxPool3DDNNLayer(layer_7, pool_size=(2, 2, 2), stride=(2, 2, 2), pad=(1, 1, 1))
    layer_9         = DropoutLayer(layer_8, p=0.25)

    # LSTM
    layer         = DimshuffleLayer(layer_9, (0,2,1,3,4))
#    layer_prediction  = LSTMLayer(layer, num_units=2, only_return_final=True, learn_init=True, cell=Gate(linear))
    layer = LSTMLayer(layer, num_units=2, only_return_final=True, learn_init=True)
    layer_prediction = DenseLayer(layer, 2, nonlinearity=linear)

    # Output Layer
    # layer_hidden         = DenseLayer(layer_flatten, 500, nonlinearity=linear)
    # layer_prediction     = DenseLayer(layer_hidden, 2, nonlinearity=linear)

    # Loss
    prediction           = get_output(layer_prediction) / multiply_var**2
    loss                 = T.abs_(prediction - target_var)
    loss                 = loss.mean()

    #Updates : Stochastic Gradient Descent (SGD) with Nesterov momentum
    params               = get_all_params(layer_prediction, trainable=True)

    # Create a loss expression for validation/testing. The crucial difference
    # here is that we do a deterministic forward pass through the network, disabling dropout layers.
    test_prediction      = get_output(layer_prediction, deterministic=True) / multiply_var**2
    test_loss            = T.abs_(test_prediction - target_var)
    test_loss            = test_loss.mean()

    # crps estimate
    crps                 = T.abs_(test_prediction - target_var).mean()/600
    
    return test_prediction, crps, loss, params
def build_network(input_var, num_input_channels, num_classes):
    conv_defs = {
        'W': lasagne.init.HeNormal('relu'),
        'b': lasagne.init.Constant(0.0),
        'filter_size': (3, 3),
        'stride': (1, 1),
        'nonlinearity': lasagne.nonlinearities.LeakyRectify(0.1)
    }

    nin_defs = {
        'W': lasagne.init.HeNormal('relu'),
        'b': lasagne.init.Constant(0.0),
        'nonlinearity': lasagne.nonlinearities.LeakyRectify(0.1)
    }

    dense_defs = {
        'W': lasagne.init.HeNormal(1.0),
        'b': lasagne.init.Constant(0.0),
        'nonlinearity': lasagne.nonlinearities.softmax
    }

    wn_defs = {
        'momentum': config.batch_normalization_momentum
    }

    net = InputLayer        (     name='input',    shape=(None, num_input_channels, 28, 28), input_var=input_var)
    net = GaussianNoiseLayer(net, name='noise',    sigma=config.augment_noise_stddev)
    net = WN(Conv2DLayer    (net, name='conv1a',   num_filters=32, pad='same', **conv_defs), **wn_defs)
    net = WN(Conv2DLayer    (net, name='conv1b',   num_filters=64, pad='same', **conv_defs), **wn_defs)
#    net = WN(Conv2DLayer    (net, name='conv1c',   num_filters=128, pad='same', **conv_defs), **wn_defs)
    net = MaxPool2DLayer    (net, name='pool1',    pool_size=(2, 2))
    net = DropoutLayer      (net, name='drop1',    p=.5)
    net = WN(Conv2DLayer    (net, name='conv2a',   num_filters=32, pad='same', **conv_defs), **wn_defs)
    net = WN(Conv2DLayer    (net, name='conv2b',   num_filters=64, pad='same', **conv_defs), **wn_defs)
#    net = WN(Conv2DLayer    (net, name='conv2c',   num_filters=256, pad='same', **conv_defs), **wn_defs)
    net = MaxPool2DLayer    (net, name='pool2',    pool_size=(2, 2))
    net = DropoutLayer      (net, name='drop2',    p=.5)
    net = WN(Conv2DLayer    (net, name='conv3a',   num_filters=32, pad=0,      **conv_defs), **wn_defs)
#    net = WN(NINLayer       (net, name='conv3b',   num_units=256,               **nin_defs),  **wn_defs)
    net = WN(NINLayer       (net, name='conv3c',   num_units=256,               **nin_defs),  **wn_defs)
    net = GlobalPoolLayer   (net, name='pool3')    
    net = WN(DenseLayer     (net, name='dense',    num_units=num_classes,       **dense_defs), **wn_defs)
    
    
#    net = GaussianNoiseLayer(net, name='noise',    sigma=config.augment_noise_stddev)
#    net = WN(DenseLayer     (net, name='dense1',    num_units=256,       **dense_defs), **wn_defs)
#    net = DropoutLayer      (net, name='drop1',    p=.5)
#    net = WN(DenseLayer     (net, name='dense2',    num_units=256,       **dense_defs), **wn_defs)
#    net = DropoutLayer      (net, name='drop2',    p=.5)
#    net = WN(DenseLayer     (net, name='dense3',    num_units=256,       **dense_defs), **wn_defs)
#     
#    net = WN(DenseLayer     (net, name='dense4',    num_units=num_classes,       **dense_defs), **wn_defs)


    return net
예제 #22
0
def mycnn1(library, img_channels, img_rows, img_cols, nb_classes):
	nb_filters = 32
	nb_pool = 2
	nb_conv = 3

	if library == 'keras':
		model = Sequential()

		# First convolutional layer
		model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
		                        border_mode='valid',
		                        input_shape=(img_channels, img_rows, img_cols)))
		model.add(Activation('relu'))

		# Second convolutional layer
		model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
		model.add(Activation('relu'))
		model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
		model.add(Dropout(0.25))

		# Fully connected layer
		model.add(Flatten())
		model.add(Dense(128))
		model.add(Activation('relu'))
		model.add(Dropout(0.5))

		# Softmax prediction layer
		model.add(Dense(nb_classes))
		model.add(Activation('softmax'))

		return model
	else:
		net = {}
		net['input'] = InputLayer((img_channels, img_rows, img_cols))

		# First convolutional layer
		net['conv1'] = ConvLayer(
				net['input'], nb_filters, nb_conv, flip_filters=False)

		# Second convolutional layer
		net['conv2'] = ConvLayer(
				net['conv1'], nb_filters, nb_conv, flip_filters=False)
		net['pool2'] = PoolLayer(net['conv2'], nb_pool)
		net['pool2_dropout'] = DropoutLayer(net['pool2'], p=0.25)

		# Fully connected layer
		net['fc1'] = DenseLayer(net['pool2_dropout'], num_units=128)
		net['fc1_dropout'] = DropoutLayer(net['fc1'], p=0.5)

		# Softmax prediction layer
		net['fc2'] = DenseLayer(
				net['fc1_dropout'], num_units=nb_classes, nonlinearity=None)
		net['prob'] = NonlinearityLayer(net['fc2'], softmax)

		return net
def build_indrnn_network(X_sym):
    net = {}
    net['input0'] = InputLayer((batch_size, seq_len, indim, 3), X_sym)
    net['input'] = ReshapeLayer(net['input0'],
                                (batch_size, seq_len, indim * 3))
    net['rnn0'] = DimshuffleLayer(net['input'], (1, 0, 2))
    for l in range(1, num_layers + 1):
        hidini = 0
        if l == num_layers:
            hidini = U_lowbound
        net['rnn%d' % (l - 1)] = ReshapeLayer(net['rnn%d' % (l - 1)],
                                              (batch_size * seq_len, -1))
        net['rnn%d' % (l - 1)] = DenseLayer(net['rnn%d' % (l - 1)],
                                            hidden_units,
                                            W=ini_W,
                                            b=lasagne.init.Constant(
                                                args.ini_b),
                                            nonlinearity=None)  #
        net['rnn%d' % (l - 1)] = ReshapeLayer(net['rnn%d' % (l - 1)],
                                              (seq_len, batch_size, -1))
        if args.conv_drop:
            net['rnn%d' % (l - 1)] = DropoutLayer(net['rnn%d' % (l - 1)],
                                                  p=droprate,
                                                  shared_axes=(0, ))
        net['rnn%d' % l] = net['rnn%d' % (l - 1)]
        if not args.use_bn_afterrnn:
            net['rnn%d' % l] = BatchNormLayer(net['rnn%d' % l],
                                              beta=lasagne.init.Constant(
                                                  args.ini_b),
                                              axes=(0, 1))

        net['rnn%d' % l] = rnnmodel(net['rnn%d' % l],
                                    hidden_units,
                                    W_hid_to_hid=Uniform(range=(hidini,
                                                                U_bound)),
                                    nonlinearity=act,
                                    only_return_final=False,
                                    grad_clipping=gradclipvalue)

        if args.use_bn_afterrnn:
            net['rnn%d' % l] = BatchNormLayer(net['rnn%d' % l], axes=(0, 1))
        if args.use_dropout and l % args.drop_layers == 0:
            net['rnn%d' % l] = DropoutLayer(net['rnn%d' % l],
                                            p=droprate,
                                            shared_axes=(0, ))

    net['rnn%d' % num_layers] = lasagne.layers.SliceLayer(net['rnn%d' %
                                                              num_layers],
                                                          indices=-1,
                                                          axis=0)
    net['out'] = DenseLayer(net['rnn%d' % num_layers],
                            outputclass,
                            nonlinearity=softmax)
    return net
def build_vggpool5model(input_var=None):
    net = {}
    net['input_layer'] = InputLayer((None, 512, 7, 7), input_var=input_var)
    net['fc6'] = DenseLayer(net['input_layer'], num_units=4096)
    net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=4096)
    net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
    net['prob'] = DenseLayer(net['fc7_dropout'],
                             num_units=300,
                             nonlinearity=None)
    return net
예제 #25
0
def build_model(input_shape):

    net = {}
    net['input'] = InputLayer(input_shape)
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=96,
                             filter_size=7,
                             stride=2,
                             flip_filters=False)
    net['norm1'] = LRNLayer(
        net['conv1'], alpha=0.0001)  # caffe has alpha = alpha * pool_size
    net['pool1'] = PoolLayer(net['norm1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'],
                             num_filters=256,
                             filter_size=5,
                             flip_filters=False)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv4'] = ConvLayer(net['conv3'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv5'] = ConvLayer(net['conv4'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['pool5'] = PoolLayer(net['conv5'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(net['drop7'],
                            num_units=1000,
                            nonlinearity=lasagne.nonlinearities.softmax)

    for layer in net.values():
        print str(lasagne.layers.get_output_shape(layer))

    return net
    def build_model(self, input_dim):
        l_in = InputLayer(shape=(self.batch_size, input_dim))

        l_hidden1 = DenseLayer(l_in, num_units=self.n_hidden, nonlinearity=rectify)
        l_hidden1_dropout = DropoutLayer(l_hidden1, p=self.dropout)

        l_hidden2 = DenseLayer(l_hidden1_dropout, num_units=self.n_hidden, nonlinearity=rectify)
        l_hidden2_dropout = DropoutLayer(l_hidden2, p=self.dropout)

        l_out = DenseLayer(l_hidden2_dropout, num_units=self.n_classes_, nonlinearity=softmax)

        return l_out
예제 #27
0
def build_discrim_net(batch_size, n_feats, input_var_sup, n_hidden_t_enc,
                      n_hidden_s, embedding, disc_nonlinearity, n_targets,
                      batchnorm=False, input_dropout=1.0):
    # Supervised network
    discrim_net = InputLayer((None, n_feats), input_var_sup)
    
    if input_dropout < 1.0:
        discrim_net = DropoutLayer(discrim_net, p=input_dropout)
    
    """
    # Code for convolutional encoder
    discrim_net = ReshapeLayer(discrim_net, (batch_size, 1, n_feats))
    discrim_net = Conv1DLayer(discrim_net, 8, 9, pad='same')
    discrim_net = ReshapeLayer(discrim_net, (batch_size, 8 * n_feats))
    """
    
    discrim_net = DenseLayer(discrim_net, num_units=n_hidden_t_enc[-1],
                             W=embedding, nonlinearity=rectify)
    hidden_rep = discrim_net

    # Supervised hidden layers
    for hid in n_hidden_s:
        if batchnorm:
            discrim_net = BatchNormLayer(discrim_net)
        discrim_net = DropoutLayer(discrim_net)
        # discrim_net = BatchNormLayer(discrim_net)
        discrim_net = DenseLayer(discrim_net, num_units=hid)

    # Predicting labels
    assert disc_nonlinearity in ["sigmoid", "linear", "rectify",
                                 "softmax", "softmax_hierarchy"]
    if batchnorm:
        discrim_net = BatchNormLayer(discrim_net)
    discrim_net = DropoutLayer(discrim_net)

    if n_targets == 2 and disc_nonlinearity == 'sigmoid':
        n_targets = 1

    if disc_nonlinearity != "softmax_hierarchy":
        discrim_net = DenseLayer(discrim_net, num_units=n_targets,
                                 nonlinearity=eval(disc_nonlinearity))
    else:
        cont_labels = create_1000_genomes_continent_labels()
        hierarch_softmax_1000_genomes = HierarchicalSoftmax(cont_labels)
        discrim_net_e= DenseLayer(discrim_net, num_units=n_targets,
                                  nonlinearity=hierarch_softmax_1000_genomes)
        discrim_net_c= DenseLayer(discrim_net, num_units=len(cont_labels),
                                  nonlinearity=softmax)
        discrim_net = HierarchicalMergeSoftmaxLayer([discrim_net_e,
                                                     discrim_net_c],
                                                     cont_labels)
    return discrim_net, hidden_rep
def build_model_VGG_CNN_S(input_var=None):
    net = {}

    net['input'] = InputLayer((None, 3, 224, 224), input_var=input_var)
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=96,
                             filter_size=7,
                             stride=2,
                             flip_filters=False)
    # caffe has alpha = alpha * pool_size
    #net['norm1'] = NormLayer(net['conv1'], alpha=0.0001)
    net['pool1'] = PoolLayer(net['conv1'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['conv2'] = ConvLayer(net['pool1'],
                             num_filters=256,
                             filter_size=5,
                             flip_filters=False)
    net['pool2'] = PoolLayer(net['conv2'],
                             pool_size=2,
                             stride=2,
                             ignore_border=False)
    net['conv3'] = ConvLayer(net['pool2'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv4'] = ConvLayer(net['conv3'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['conv5'] = ConvLayer(net['conv4'],
                             num_filters=512,
                             filter_size=3,
                             pad=1,
                             flip_filters=False)
    net['pool5'] = PoolLayer(net['conv5'],
                             pool_size=3,
                             stride=3,
                             ignore_border=False)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    net['drop7'] = DropoutLayer(net['fc7'], p=0.5)
    net['fc8'] = DenseLayer(net['drop7'],
                            num_units=num_categories,
                            nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)

    return net['prob']
예제 #29
0
    def _get_l_out(self, input_vars):
        listener.check_options(self.options)
        id_tag = (self.id + '/') if self.id else ''

        input_var = input_vars[0]

        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')

        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 = cell(l_in_embed, 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_hidden = DenseLayer(
            l_rec1_drop,
            num_units=self.options.listener_cell_size,
            nonlinearity=NONLINEARITIES[self.options.listener_nonlinearity],
            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_out = DenseLayer(l_hidden_drop,
                           num_units=3,
                           nonlinearity=softmax,
                           name=id_tag + 'scores')

        return l_out, [l_in]
예제 #30
0
def build_model(input_var=None):
    net = {}
    net['input'] = InputLayer((None, 3, 32, 32), input_var=input_var)
    net['conv1'] = ConvLayer(net['input'],
                             num_filters=192,
                             filter_size=5,
                             pad=2)
    net['cccp1'] = ConvLayer(net['conv1'], num_filters=160, filter_size=1)
    net['cccp2'] = ConvLayer(net['cccp1'], num_filters=96, filter_size=1)
    net['pool1'] = PoolLayer(net['cccp2'],
                             pool_size=3,
                             stride=2,
                             mode='max',
                             ignore_border=False)
    net['drop3'] = DropoutLayer(net['pool1'], p=0.5)
    net['conv2'] = ConvLayer(net['drop3'],
                             num_filters=192,
                             filter_size=5,
                             pad=2)
    net['cccp3'] = ConvLayer(net['conv2'], num_filters=192, filter_size=1)
    net['cccp4'] = ConvLayer(net['cccp3'], num_filters=192, filter_size=1)
    net['pool2'] = PoolLayer(net['cccp4'],
                             pool_size=3,
                             stride=2,
                             mode='average_exc_pad',
                             ignore_border=False)
    net['drop6'] = DropoutLayer(net['pool2'], p=0.5)
    net['conv3'] = ConvLayer(net['drop6'],
                             num_filters=192,
                             filter_size=3,
                             pad=1)
    net['cccp5'] = ConvLayer(net['conv3'], num_filters=192, filter_size=1)
    net['cccp6'] = ConvLayer(net['cccp5'], num_filters=10, filter_size=1)
    net['pool3'] = PoolLayer(net['cccp6'],
                             pool_size=8,
                             mode='average_exc_pad',
                             ignore_border=False)
    net['output'] = FlattenLayer(net['pool3'])
    
    print 'Loading network pretrained with CIFAR-10'
    with open('save/cifar10-nin.pkl', 'rb') as f:
        params = cPickle.load(f)    
    lasagne.layers.set_all_param_values(net['output'], params)  
    
    # Modify model output
    #net['fc1'] = DenseLayer(net['pool3'], num_units=2, nonlinearity=None)
    #net['output'] = NonlinearityLayer(net['fc1'], softmax)
    

    return net