Ejemplo n.º 1
0
def build_convnet(path_to_vgg):
    """
    Construct VGG-19 convnet
    """
    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['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['fc7'] = DenseLayer(net['fc6'], num_units=4096)
    net['fc8'] = DenseLayer(net['fc7'], num_units=1000, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)

    print 'Loading parameters...'
    output_layer = net['prob']
    model = pkl.load(open(path_to_vgg))
    lasagne.layers.set_all_param_values(output_layer, model['param values'])

    return net
Ejemplo n.º 2
0
    def _get_l_out(self, input_vars):
        id_tag = (self.id + '/') if self.id else ''

        input_var = input_vars[0]

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

        if self.options.listener_cell_size == 0:
            l_scores = l_in_embed  # BiasLayer(l_in_embed, name=id_tag + 'bias')
        else:
            l_hidden = DenseLayer(l_in_embed,
                                  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_scores = DenseLayer(l_hidden_drop,
                                  num_units=self.color_vec.num_types,
                                  nonlinearity=None,
                                  name=id_tag + 'scores')
        l_out = NonlinearityLayer(l_scores,
                                  nonlinearity=softmax,
                                  name=id_tag + 'out')

        return l_out, [l_in]
Ejemplo n.º 3
0
 def _create_net_structure(self):
     linear_initializer = lasagne.init.GlorotUniform()
     relu_initializer = lasagne.init.GlorotUniform(gain='relu')
     # Input layer
     in_lay = InputLayer(self.input_shape, input_var=self.input_var)
     # First set of encoding layers
     conv0_lay = batch_norm(Conv2DLayer(in_lay, 64, 7, pad=3, W=relu_initializer, nonlinearity=lasagne.nonlinearities.rectify))
     pool0_lay = MaxPool2DLayer(conv0_lay, 2, stride=2)
     # Second set of encoding layers
     conv1_lay = batch_norm(Conv2DLayer(pool0_lay, 64, 7, pad=3, W=relu_initializer, nonlinearity=lasagne.nonlinearities.rectify))
     pool1_lay = MaxPool2DLayer(conv1_lay, 2, stride=2) 
     # Third set of encoding layers
     conv2_lay = batch_norm(Conv2DLayer(pool1_lay, 64, 7, pad=3, W=relu_initializer, nonlinearity=lasagne.nonlinearities.rectify))
     pool2_lay = MaxPool2DLayer(conv2_lay, 2, stride=2) 
     dropenc2_lay = DropoutLayer(pool2_lay, p=0.5)
     # Fourth set of encoding layers
     conv3_lay = batch_norm(Conv2DLayer(dropenc2_lay, 64, 7, pad=3, W=relu_initializer, nonlinearity=lasagne.nonlinearities.rectify))
     pool3_lay = MaxPool2DLayer(conv3_lay, 2, stride=2) 
     dropenc3_lay = DropoutLayer(pool3_lay, p=0.5)
     # First set of decoding layers
     upsample3_lay = InverseLayer(dropenc3_lay, pool3_lay)
     deconv3_lay = batch_norm(Conv2DLayer(upsample3_lay, 64, 7, pad=3, W=linear_initializer, nonlinearity=None))
     dropdec3_lay = DropoutLayer(deconv3_lay, p=0.5)
     # Second set of decoding layers
     upsample2_lay = InverseLayer(dropdec3_lay, pool2_lay)
     deconv2_lay = batch_norm(Conv2DLayer(upsample2_lay, 64, 7, pad=3, W=linear_initializer, nonlinearity=None))
     dropdec2_lay = DropoutLayer(deconv2_lay, p=0.5)
     # Third set of decoding layers
     upsample1_lay = InverseLayer(dropdec2_lay, pool1_lay)
     deconv1_lay = batch_norm(Conv2DLayer(upsample1_lay, 64, 7, pad=3, W=linear_initializer, nonlinearity=None))
     # Fourth set of decoding layers
     upsample0_lay = InverseLayer(deconv1_lay, pool0_lay)
     deconv0_lay = batch_norm(Conv2DLayer(upsample0_lay, 64, 7, pad=3, W=linear_initializer, nonlinearity=None))
     # Classifying (softmax layer)
     classes_lay = Conv2DLayer(deconv0_lay, 2, 1)
     outshape_lay = ReshapeLayer(classes_lay, (BATCH_SIZE, 2, INPUT_SIZE * INPUT_SIZE))
     softmax_lay = NonlinearityLayer(outshape_lay, nonlinearity=log_softmax)
     self.network = softmax_lay 
Ejemplo n.º 4
0
def build_medium_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['conv4_2'] = ConvLayer(
        net['conv4_1'], 512, 3, pad=1)#, flip_filters=False)
    net['pool4'] = PoolLayer(net['conv4_2'], 2)
    net['conv5_1'] = ConvLayer(
        net['pool4'], 512, 3, pad=1)#, flip_filters=False)
    net['conv5_2'] = ConvLayer(
        net['conv5_1'], 512, 3, pad=1)#, flip_filters=False)
    net['conv5_3'] = ConvLayer(
        net['conv5_2'], 512, 3, pad=1)#, flip_filters=False)
    net['pool5'] = PoolLayer(net['conv5_3'], 2)
    net['fc6'] = DenseLayer(net['pool5'], num_units=10)
    net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
    net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=10)
    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
Ejemplo n.º 5
0
def build_cnn(input_var, num_classes):
    #Get back the convolutional part of a VGG network trained on ImageNet
    network = build_model(input_var)

    with open('vgg_cnn_s.pkl', 'rb') as f:
        params = pickle.load(f, encoding='latin-1')
    set_all_param_values(network.values(), params['values'])
    # pool5 shape:  (None, 512, 6, 6)

    del network['fc6']
    del network['drop6']
    del network['fc7']
    del network['drop7']
    del network['fc8']

    network['shuff6'] = DimshuffleLayer(network['pool5'], (0, 2, 1, 3))
    network['lstm7'] = LSTMLayer((None, 512, 6, 6), network['shuff6'])
    network['rshp8'] = ReshapeLayer(network['lstm7'], (-1, 64))
    network['fc9'] = DenseLayer(network['rshp8'], num_units=4096)
    network['drop9'] = DropoutLayer(network['fc9'], p=0.5)
    network['fc10'] = DenseLayer(network['drop9'], num_units=num_classes, nonlinearity=None)
    network['prob'] = NonlinearityLayer(network['fc10'], softmax)
    return network
Ejemplo n.º 6
0
def model_a():
    net = {}
    net['input'] = InputLayer((None, 3, 32, 32))
    #net['drop_in'] =  DropoutLayer(net['input'], p=0.2)
    net['conv1_1'] = ConvLayer(net['input'], num_filters=96, filter_size=5, pad=1, flip_filters=False)

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


    net['conv3_1'] = ConvLayer(net['conv2_1'], num_filters=192, filter_size=5, pad=1, flip_filters=False)

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

    net['conv5_1'] = ConvLayer(net['conv4_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['fc10'] = DenseLayer(net['conv7_1'], num_units=10, nonlinearity=None)
    net['output'] = NonlinearityLayer(net['global_avg'], softmax)
    return net
def init_model():
    # Build the model and select layers we need - the features are taken from the final network layer,
    # before the softmax nonlinearity.
    cnn_layers = googlenet.build_model()
    cnn_input_var = cnn_layers['input'].input_var
    cnn_feature_layer = DenseLayer(cnn_layers['pool5/7x7_s1'],
                                   num_units=len(CLASSES),
                                   nonlinearity=linear)
    cnn_output_layer = NonlinearityLayer(cnn_feature_layer,
                                         nonlinearity=softmax)
    get_cnn_features = theano.function(
        [cnn_input_var], lasagne.layers.get_output(cnn_feature_layer))
    logging.info('Compiled new functions')
    # Define the function for label prediction.
    X_sym = T.tensor4()
    prediction = lasagne.layers.get_output(cnn_output_layer, X_sym)
    pred_fn = theano.function([X_sym], prediction)

    # Load the pretrained weights into the network
    with np.load(r'./data/googlenet_model.npz') as f:
        model_param_values = [f['arr_%d' % i] for i in range(len(f.files))]

    lasagne.layers.set_all_param_values(cnn_output_layer, model_param_values)
    return get_cnn_features, pred_fn
def Tiny_VGG(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, 3, pad=1, flip_filters=False)
    net['conv1_2'] = ConvLayer(net['conv1_1'],
                               32,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool1'] = PoolLayer(net['conv1_2'], 2)
    net['conv2_1'] = ConvLayer(net['pool1'], 32, 3, pad=1, flip_filters=False)
    net['conv2_2'] = ConvLayer(net['conv2_1'],
                               32,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool2'] = PoolLayer(net['conv2_2'], 2)
    net['conv3_1'] = ConvLayer(net['pool2'], 32, 3, pad=1, flip_filters=False)
    net['conv3_2'] = ConvLayer(net['conv3_1'],
                               32,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool3'] = PoolLayer(net['conv3_2'], 2)
    net['fc4'] = DenseLayer(net['pool3'], num_units=1024)
    net['fc4_dropout'] = DropoutLayer(net['fc4'], p=0)
    net['fc5'] = DenseLayer(net['fc4_dropout'], num_units=512)
    net['fc5_dropout'] = DropoutLayer(net['fc5'], p=0)
    net['fc6'] = DenseLayer(net['fc5_dropout'], num_units=256)
    net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0)
    net['fc7'] = DenseLayer(net['fc6_dropout'],
                            num_units=num_of_classes,
                            nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc7'], softmax)

    return net
Ejemplo n.º 9
0
def hw3_cnn():
    net = {}
    net['input'] = InputLayer((None, 3, 32, 32))
    #net['drop_in'] =  DropoutLayer(net['input'], p=0.2)

    net['conv1_1'] = ConvLayer(net['input'],
                               num_filters=16,
                               filter_size=5,
                               flip_filters=False)

    net['conv2_1'] = PoolLayer(net['conv1_1'], pool_size=2)
    #net['drop2_1'] =  DropoutLayer(net['conv2_1'], p=0.5)

    net['conv3_1'] = ConvLayer(net['conv2_1'],
                               num_filters=512,
                               filter_size=5,
                               flip_filters=False)

    net['conv4_1'] = PoolLayer(net['conv3_1'], pool_size=2)
    #net['drop4_1'] =  DropoutLayer(net['conv4_1'], p=0.5)

    net['fc_5'] = DenseLayer(net['conv4_1'], 500)
    net['output'] = NonlinearityLayer(net['fc_5'], softmax)
    return net
Ejemplo n.º 10
0
    def bn_relu_conv(self, network, channels, filter_size, dropout,
                     name_prefix):
        nam = name_prefix + '_bn'
        network = BatchNormLayer(network, name=nam)
        self.layers.append(layer_info('bn', num_params=4))

        nam = name_prefix + '_relu'
        network = NonlinearityLayer(network, nonlinearity=rectify, name=nam)
        self.layers.append(layer_info('relu'))

        nam = name_prefix + '_conv'
        network = Conv2DLayer(network,
                              channels,
                              filter_size,
                              pad='same',
                              W=lasagne.init.HeNormal(gain='relu'),
                              b=None,
                              nonlinearity=None,
                              name=nam)
        self.layers.append(layer_info('conv', (channels, channels, 3, 3), 1))

        if dropout:
            network = DropoutLayer(network, dropout)
        return network
Ejemplo n.º 11
0
def all_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['conv2_1'] = ConvLayer(net['conv1_2'], num_filters=96, filter_size=3, stride=2, pad=1, flip_filters=False)
    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['conv4_1'] = ConvLayer(net['conv3_2'], num_filters=192, filter_size=3, stride=2,pad=1, flip_filters=False)
    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
Ejemplo n.º 12
0
def SonoNet64(input_var, image_size, num_labels):

    net = {}
    net['input'] = InputLayer(shape=(None, 1, image_size[0], image_size[1]), input_var=input_var)
    net['conv1_1'] = batch_norm(Conv2DLayer(net['input'], 64, 3, pad=1, flip_filters=False))
    net['conv1_2'] = batch_norm(Conv2DLayer(net['conv1_1'], 64, 3, pad=1, flip_filters=False))
    net['pool1'] = MaxPool2DLayer(net['conv1_2'], 2)

    net['conv2_1'] = batch_norm(Conv2DLayer(net['pool1'], 128, 3, pad=1, flip_filters=False))
    net['conv2_2'] = batch_norm(Conv2DLayer(net['conv2_1'], 128, 3, pad=1, flip_filters=False))
    net['pool2'] = MaxPool2DLayer(net['conv2_2'], 2)

    net['conv3_1'] = batch_norm(Conv2DLayer(net['pool2'], 256, 3, pad=1, flip_filters=False))
    net['conv3_2'] = batch_norm(Conv2DLayer(net['conv3_1'], 256, 3, pad=1, flip_filters=False))
    net['conv3_3'] = batch_norm(Conv2DLayer(net['conv3_2'], 256, 3, pad=1, flip_filters=False))
    net['pool3'] = MaxPool2DLayer(net['conv3_3'], 2)

    net['conv4_1'] = batch_norm(Conv2DLayer(net['pool3'], 512, 3, pad=1, flip_filters=False))
    net['conv4_2'] = batch_norm(Conv2DLayer(net['conv4_1'], 512, 3, pad=1, flip_filters=False))
    net['conv4_3'] = batch_norm(Conv2DLayer(net['conv4_2'], 512, 3, pad=1, flip_filters=False))
    net['pool4'] = MaxPool2DLayer(net['conv4_3'], 2)

    net['conv5_1'] = batch_norm(Conv2DLayer(net['pool4'], 512, 3, pad=1, flip_filters=False))
    net['conv5_2'] = batch_norm(Conv2DLayer(net['conv5_1'], 512, 3, pad=1, flip_filters=False))
    net['conv5_3'] = batch_norm(Conv2DLayer(net['conv5_2'], 512, 3, pad=1, flip_filters=False))

    net['conv5_p'] = batch_norm(Conv2DLayer(net['conv5_3'], num_filters=256, filter_size=(1, 1)))
    net['conv6_p'] = batch_norm(Conv2DLayer(net['conv5_p'], num_filters=num_labels, filter_size=(1, 1), nonlinearity=linear))
    net['average_pool_p'] = GlobalPoolLayer(net['conv6_p'], pool_function=T.mean)
    net['softmax_p'] = NonlinearityLayer(net['average_pool_p'], nonlinearity=softmax)

    net['output'] = net['softmax_p']
    net['feature_maps'] = net['conv6_p']
    net['last_activation'] = net['average_pool_p']

    return net
Ejemplo n.º 13
0
def G_paper(
        num_channels=1,  # Overridden based on dataset.
        resolution=32,  # Overridden based on dataset.
        label_size=0,  # Overridden based on dataset.
        fmap_base=4096,
        fmap_decay=1.0,
        fmap_max=256,
        latent_size=None,
        normalize_latents=True,
        use_wscale=True,
        use_pixelnorm=True,
        use_leakyrelu=True,
        use_batchnorm=False,
        tanh_at_end=None,
        **kwargs):

    R = int(np.log2(resolution))
    assert resolution == 2**R and resolution >= 4
    cur_lod = theano.shared(np.float32(0.0))

    def nf(stage):
        return min(int(fmap_base / (2.0**(stage * fmap_decay))), fmap_max)

    def PN(layer):
        return PixelNormLayer(layer, name=layer.name +
                              'pn') if use_pixelnorm else layer

    def BN(layer):
        return lasagne.layers.batch_norm(layer) if use_batchnorm else layer

    def WS(layer):
        return WScaleLayer(layer, name=layer.name +
                           'S') if use_wscale else layer

    if latent_size is None: latent_size = nf(0)
    (act, iact) = (lrelu, ilrelu) if use_leakyrelu else (relu, irelu)

    input_layers = [InputLayer(name='Glatents', shape=[None, latent_size])]
    net = input_layers[-1]
    if normalize_latents:
        net = PixelNormLayer(net, name='Glnorm')
    if label_size:
        input_layers += [InputLayer(name='Glabels', shape=[None, label_size])]
        net = ConcatLayer(name='Gina', incomings=[net, input_layers[-1]])

    net = ReshapeLayer(name='Ginb', incoming=net, shape=[[0], [1], 1, 1])
    net = PN(
        BN(
            WS(
                Conv2DLayer(net,
                            name='G1a',
                            num_filters=nf(1),
                            filter_size=4,
                            pad='full',
                            nonlinearity=act,
                            W=iact))))
    net = PN(
        BN(
            WS(
                Conv2DLayer(net,
                            name='G1b',
                            num_filters=nf(1),
                            filter_size=3,
                            pad=1,
                            nonlinearity=act,
                            W=iact))))
    lods = [net]

    for I in xrange(2, R):  # I = 2, 3, ..., R-1
        net = Upscale2DLayer(net, name='G%dup' % I, scale_factor=2)
        net = PN(
            BN(
                WS(
                    Conv2DLayer(net,
                                name='G%da' % I,
                                num_filters=nf(I),
                                filter_size=3,
                                pad=1,
                                nonlinearity=act,
                                W=iact))))
        net = PN(
            BN(
                WS(
                    Conv2DLayer(net,
                                name='G%db' % I,
                                num_filters=nf(I),
                                filter_size=3,
                                pad=1,
                                nonlinearity=act,
                                W=iact))))
        lods += [net]

    lods = [
        WS(
            NINLayer(l,
                     name='Glod%d' % i,
                     num_units=num_channels,
                     nonlinearity=linear,
                     W=ilinear)) for i, l in enumerate(reversed(lods))
    ]
    output_layer = LODSelectLayer(name='Glod',
                                  incomings=lods,
                                  cur_lod=cur_lod,
                                  first_incoming_lod=0)
    if tanh_at_end is not None:
        output_layer = NonlinearityLayer(output_layer,
                                         name='Gtanh',
                                         nonlinearity=tanh)
        if tanh_at_end != 1.0:
            output_layer = non_trainable(
                ScaleLayer(output_layer,
                           name='Gtanhs',
                           scales=lasagne.init.Constant(tanh_at_end)))
    return dict(input_layers=input_layers,
                output_layers=[output_layer],
                cur_lod=cur_lod)
Ejemplo n.º 14
0
    def __init__(self,
                 l_in=InputLayer((None, 3, 224, 224)),
                 get_layer='prob',
                 padded=True,
                 trainable=False,
                 regularizable=False,
                 name='vgg'):

        super(Vgg16Layer, self).__init__(l_in, name)
        self.l_in = l_in
        self.get_layer = get_layer
        self.padded = padded
        self.trainable = trainable
        self.regularizable = regularizable

        if padded:
            ConvLayer = PaddedConv2DLayer
            PoolLayer = PaddedPool2DLayer
        else:
            try:
                ConvLayer = lasagne.layers.dnn.Conv2DDNNLayer
            except AttributeError:
                ConvLayer = lasagne.layers.Conv2DLayer
            PoolLayer = lasagne.layers.Pool2DLayer

        net = OrderedDict()
        net['input'] = l_in
        net['bgr'] = RGBtoBGRLayer(net['input'])
        net['conv1_1'] = ConvLayer(net['bgr'],
                                   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['conv3_3'] = ConvLayer(net['conv3_2'],
                                   256,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['pool3'] = PoolLayer(net['conv3_3'], 2)
        net['conv4_1'] = ConvLayer(net['pool3'],
                                   512,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['conv4_2'] = ConvLayer(net['conv4_1'],
                                   512,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['conv4_3'] = ConvLayer(net['conv4_2'],
                                   512,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['pool4'] = PoolLayer(net['conv4_3'], 2)
        net['conv5_1'] = ConvLayer(net['pool4'],
                                   512,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['conv5_2'] = ConvLayer(net['conv5_1'],
                                   512,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['conv5_3'] = ConvLayer(net['conv5_2'],
                                   512,
                                   3,
                                   pad=1,
                                   flip_filters=False)
        net['pool5'] = PoolLayer(net['conv5_3'], 2)

        if 'fc' in get_layer or get_layer == 'prob':
            net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
            net['fc7'] = DenseLayer(net['fc6'], num_units=4096)
            net['fc8'] = DenseLayer(net['fc7'],
                                    num_units=1000,
                                    nonlinearity=None)
            net['prob'] = NonlinearityLayer(net['fc8'], softmax)

        self.concat_sublayers = []
        if 'concat' in get_layer:
            n_pool = get_layer[6:]
            get_layer = 'pool' + str(n_pool)
            l_concat = net['conv1_1']
            for i in range(int(n_pool)):
                l_conv = net['conv' + str(i + 1) + '_1']
                l_pool = net['pool' + str(i + 1)]

                l_new = ConvLayer(l_concat,
                                  l_conv.num_filters,
                                  2,
                                  pad=0,
                                  stride=2,
                                  flip_filters=True,
                                  name='vgg16_skipconnection_conv_' +
                                  str(i + 1))
                self.concat_sublayers.append(l_new)
                l_concat = ConcatLayer(
                    (l_pool, l_new),
                    axis=1,
                    name='vgg16_skipconnection_concat_' + str(i))
                self.concat_sublayers.append(l_concat)
            out_layer = l_concat
        else:
            out_layer = net[get_layer]

        reached = False
        # Collect garbage
        for el in net.iteritems():
            if reached:
                del (net[el[0]])
            if el[0] == get_layer:
                reached = True
        self.sublayers = net

        # Set names to layers
        for name in net.keys():
            if not net[name].name:
                net[name].name = 'vgg16_' + name

        # Reload weights
        nparams = len(lasagne.layers.get_all_params(net.values()))
        with open('w_vgg16.pkl', 'rb') as f:
            # Note: in python3 use the pickle.load parameter
            # `encoding='latin-1'`
            vgg16_w = pickle.load(f)['param values']
        lasagne.layers.set_all_param_values(net.values(), vgg16_w[:nparams])

        # Do not train or regularize vgg
        if not trainable or not regularizable:
            all_layers = net.values()
            for vgg_layer in all_layers:
                if 'concat' not in vgg_layer.name:
                    layer_params = vgg_layer.get_params()
                    for p in layer_params:
                        if not regularizable:
                            try:
                                vgg_layer.params[p].remove('regularizable')
                            except KeyError:
                                pass
                        if not trainable:
                            try:
                                vgg_layer.params[p].remove('trainable')
                            except KeyError:
                                pass

        # save the vgg sublayers
        self.out_layer = out_layer

        # HACK LASAGNE
        # This will set `self.input_layer`, which is needed by Lasagne to find
        # the layers with the get_all_layers() helper function in the
        # case of a layer with sublayers
        if isinstance(self.out_layer, tuple):
            self.input_layer = None
        else:
            self.input_layer = self.out_layer
def construct_unet(channels=1, no_f_base=8, f_size=3, dropout=False, bs=None, class_nums=2, pad="same",nonlinearity=lasagne.nonlinearities.rectify, input_dim=[512,512]):
    net={}
    net["input"]= InputLayer(shape=(bs, channels, input_dim[0], input_dim[1]))

    # Moving downwards the U-shape. Simplified:
    net["conv_down11"] = Conv2DLayer(net["input"],no_f_base,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_down12"] = Conv2DLayer(net["conv_down11"],no_f_base,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool1"]      = Pool2DLayer(net["conv_down12"],pool_size=2)

    net["conv_down21"] = Conv2DLayer(net["pool1"],no_f_base*2,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_down22"] = Conv2DLayer(net["conv_down21"],no_f_base*2,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool2"]      = Pool2DLayer(net["conv_down22"],pool_size=2)

    net["conv_down31"] = Conv2DLayer(net["pool2"],no_f_base*4,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_down32"] = Conv2DLayer(net["conv_down31"],no_f_base*4,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool3"]      = Pool2DLayer(net["conv_down32"],pool_size=2)

    net["conv_down41"] = Conv2DLayer(net["pool3"],no_f_base*8,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_down42"] = Conv2DLayer(net["conv_down41"],no_f_base*8,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    to_drop1 = net["pool4"]      = Pool2DLayer(net["conv_down42"],pool_size=2)

    if dropout:
        to_drop1 = DropoutLayer(to_drop1, p=0.5)

    #vvvv bottom vvvv
    net["conv_bottom1"] = Conv2DLayer(to_drop1,no_f_base*16,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_bottom2"] = Conv2DLayer(net["conv_bottom1"],no_f_base*16,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["deconv_bottom1"]      = Deconv2DLayer(net["conv_bottom2"], no_f_base*8, 2, 2)
    #^^^^ bottom ^^^^

    # Moving upwards the U-shape. Simplified:
    net["concat1"] = concat([net["deconv_bottom1"], net["conv_down42"]], cropping=(None, None, "center", "center"))
    net["conv_up11"]= Conv2DLayer(net["concat1"], no_f_base*8, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_up11"]= Conv2DLayer(net["conv_up11"], no_f_base*8, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["deconv_up1"] = Deconv2DLayer(net["conv_up11"], no_f_base*4, 2, 2)

    net["concat2"] = concat([net["deconv_up1"], net["conv_down32"]], cropping=(None, None, "center", "center"))
    net["conv_up21"]= Conv2DLayer(net["concat2"], no_f_base*4, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_up22"]= Conv2DLayer(net["conv_up21"], no_f_base*4, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["deconv_up2"] = Deconv2DLayer(net["conv_up22"], no_f_base*2, 2, 2)

    net["concat3"] = concat([net["deconv_up2"], net["conv_down22"]], cropping=(None, None, "center", "center"))
    net["conv_up31"]= Conv2DLayer(net["concat3"], no_f_base*2, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_up32"]= Conv2DLayer(net["conv_up31"], no_f_base*2, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["deconv_up3"] = Deconv2DLayer(net["conv_up32"], no_f_base, 2, 2)

    net["concat4"] = concat([net["deconv_up3"], net["conv_down12"]], cropping=(None, None, "center", "center"))
    net["conv_up41"]= Conv2DLayer(net["concat4"], no_f_base, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_up42"]= Conv2DLayer(net["conv_up41"], no_f_base, f_size, pad=pad, nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    # Class layer: Work around standard softmax bc. it doesn't work with tensor4/3.
    # Hence, we reshape and feed it to an external Nonlinearity layer.
    # net["class_ns"] is the output in image-related shape.

    net["out"] = Conv2DLayer(net["conv_up42"], class_nums, 1, nonlinearity=None,W=lasagne.init.HeNormal(gain='relu'))
    net["layer_shuffle_dim"] = DimshuffleLayer(net["out"], (1, 0, 2, 3))
    net["reshape_layer"] = ReshapeLayer(net["layer_shuffle_dim"], (class_nums, -1))
    net["layer_shuffle_dim2"] = DimshuffleLayer(net["reshape_layer"], (1, 0))
    # Flattened output to be able to feed it to lasagne.objectives.categorical_crossentropy.
    net["out_optim"] = NonlinearityLayer(net["layer_shuffle_dim2"], nonlinearity=lasagne.nonlinearities.softmax)

    return net
    net = None
Ejemplo n.º 16
0
net['expand_4_2'] = batch_norm(
    ConvLayer(net['expand_4_1'],
              base_n_filters,
              3,
              nonlinearity=nonlinearity,
              pad=pad,
              W=HeNormal(gain="relu")))

net['output_segmentation'] = ConvLayer(net['expand_4_2'],
                                       num_output_classes,
                                       1,
                                       nonlinearity=None)
net['dimshuffle'] = DimshuffleLayer(net['output_segmentation'], (1, 0, 2, 3))
net['reshapeSeg'] = ReshapeLayer(net['dimshuffle'], (num_output_classes, -1))
net['dimshuffle2'] = DimshuffleLayer(net['reshapeSeg'], (1, 0))
net['output_flattened'] = NonlinearityLayer(
    net['dimshuffle2'], nonlinearity=lasagne.nonlinearities.softmax)

x_sym = T.tensor4()
seg_sym = T.ivector()
w_sym = T.vector()

output_layer_for_loss = net["output_flattened"]

# add some weight decay
l2_loss = lasagne.regularization.regularize_network_params(
    output_layer_for_loss, lasagne.regularization.l2) * 1e-4

# the distinction between prediction_train and test is important only if we enable dropout
prediction_train = lasagne.layers.get_output(output_layer_for_loss,
                                             x_sym,
                                             deterministic=False,
Ejemplo n.º 17
0
def main():

    print("Building model and compiling functions...")
    X_batch = [T.tensor4('x')]
    y_batch = [T.tensor4('y')]

    net = {}
    net['input'] = InputLayer((None, 3, 256, 256), input_var= X_batch[0])
    net['conv1_1'] = ConvLayer(
        net['input'], 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['conv3_3'] = ConvLayer(
        net['conv3_2'], 256, 3, pad=1, flip_filters=False)

    net['pool3'] = PoolLayer(net['conv3_3'], 2)
    net['conv4_1'] = ConvLayer(
        net['pool3'], 512, 3, pad=1, flip_filters=False)
    net['conv4_2'] = ConvLayer(
        net['conv4_1'], 512, 3, pad=1, flip_filters=False)
    net['conv4_3'] = ConvLayer(
        net['conv4_2'], 512, 3, pad=1, flip_filters=False)

    net['pool4'] = PoolLayer(net['conv4_3'], 2)
    net['conv5_1'] = ConvLayer(
        net['pool4'], 512, 3, pad=1, flip_filters=False)
    net['conv5_2'] = ConvLayer(
        net['conv5_1'], 512, 3, pad=1, flip_filters=False)
    net['conv5_3'] = ConvLayer(
        net['conv5_2'], 512, 3, pad=1, flip_filters=False)

    initSal = {}
    initSal['up'] = Upscale2DLayer(net['conv5_3'], (2, 2))
    initSal['concat'] = ConcatLayer([initSal['up'], net['conv4_3']])

    initSal['conv1'] = ConvLayer(initSal['concat'], 1024, (3, 3), pad=1, flip_filters=False)
    initSal['conv2'] = ConvLayer(initSal['conv1'], 512, (1, 1), pad=0, flip_filters=False)
    initSal['conv3'] = ConvLayer(initSal['conv2'], 256, (5, 5), pad=2, flip_filters=False)
    initSal['output'] = ConvLayer(initSal['conv3'], 1, (1, 1), nonlinearity=lasagne.nonlinearities.sigmoid, flip_filters=False)

    # ***************************************************************************************************
    recurent = {}
    recurent['1-sal'] = BatchNormLayer(Upscale2DLayer(initSal['conv3'], (2, 2)))
    recurent['1-vgg'] = BatchNormLayer(ConvLayer(net['conv3_3'], 256, 1))
    recurent['1-input'] = ConcatLayer([recurent['1-sal'], recurent['1-vgg']])

    recurent['1-NIN1'] = ConvLayer(recurent['1-input'], 256, 1, pad=0, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['1-NIN1'] = NonlinearityLayer(BatchNormLayer(recurent['1-NIN1']))
    recurent['1-rconv1'] = ConvLayer(recurent['1-NIN1'], 256, 3, pad=1, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['1-rconv1'] = NonlinearityLayer(BatchNormLayer(recurent['1-rconv1']))
    recurent['1-rconv2'] = ConvLayer(recurent['1-rconv1'], 256, 3, pad=1, flip_filters=False)
    recurent['1-rconv2'] = NonlinearityLayer(BatchNormLayer(recurent['1-rconv2']))
    recurent['1-rconv3'] = ConvLayer(recurent['1-rconv2'], 256, 1, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['1-sum']    = ElemwiseSumLayer([recurent['1-rconv3'], recurent['1-sal']])
    recurent['1-sum']    = NonlinearityLayer(recurent['1-sum'])
    # ***************************************************************************************************
    recurent['2-sal'] = BatchNormLayer(Upscale2DLayer(recurent['1-sum'], (2, 2)))
    recurent['2-vgg'] = BatchNormLayer(ConvLayer(net['conv2_2'], 256, 1))
    recurent['2-input'] = ConcatLayer([recurent['2-sal'], recurent['2-vgg']])

    recurent['2-NIN1'] = ConvLayer(recurent['2-input'], 256, 1, pad=0, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['2-NIN1'] = NonlinearityLayer(BatchNormLayer(recurent['2-NIN1']))
    recurent['2-rconv1'] = ConvLayer(recurent['2-NIN1'], 256, 3, pad=1, flip_filters=False)
    recurent['2-rconv1'] = NonlinearityLayer(BatchNormLayer(recurent['2-rconv1']))
    recurent['2-rconv2'] = ConvLayer(recurent['2-rconv1'], 256, 3, pad=1, flip_filters=False)
    recurent['2-rconv2'] = NonlinearityLayer(BatchNormLayer(recurent['2-rconv2']))
    recurent['2-rconv3'] = ConvLayer(recurent['2-rconv2'], 256, 1, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['2-sum']    = ElemwiseSumLayer([recurent['2-rconv3'], recurent['2-sal']])
    recurent['2-sum']    = NonlinearityLayer(recurent['2-sum'])
    # ***************************************************************************************************
    recurent['3-sal'] = BatchNormLayer(Upscale2DLayer(recurent['2-sum'], (2, 2)))
    recurent['3-vgg'] = ConvLayer(net['conv1_2'], 128, 3, pad=1)
    recurent['3-vgg'] = BatchNormLayer(ConvLayer(recurent['3-vgg'], 256, 1))
    recurent['3-input'] = ConcatLayer([recurent['3-sal'], recurent['3-vgg']])
    recurent['3-NIN1'] = ConvLayer(recurent['3-input'], 256, 1, pad=0, flip_filters=False)
    recurent['3-NIN1'] = NonlinearityLayer(BatchNormLayer(recurent['3-NIN1']))
    recurent['3-rconv1'] = ConvLayer(recurent['3-NIN1'], 256, 3, pad=1, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['3-rconv1'] = NonlinearityLayer(BatchNormLayer(recurent['3-rconv1']))
    recurent['3-rconv2'] = ConvLayer(recurent['3-rconv1'], 256, 3, pad=1, flip_filters=False)
    recurent['3-rconv2'] = NonlinearityLayer(BatchNormLayer(recurent['3-rconv2']))
    recurent['3-rconv3'] = ConvLayer(recurent['3-rconv2'], 256, 1, nonlinearity=lasagne.nonlinearities.linear, flip_filters=False)
    recurent['3-sum']    = ElemwiseSumLayer([recurent['3-rconv3'], recurent['3-sal']])
    recurent['3-sum']    = NonlinearityLayer(recurent['3-sum'])
    recurent['3-output'] = ConvLayer(recurent['3-sum'], 1, (1, 1), nonlinearity=lasagne.nonlinearities.sigmoid, flip_filters=False)



    prediction = []
    # loss_train = []
    # all_params = []
    # accuracy = []
    output_layer = recurent['3-output']
    prediction.append(lasagne.layers.get_output(output_layer))
    # loss_train.append(T.mean(lasagne.objectives.binary_crossentropy(prediction[0], y_batch[0])))
    # all_params.append(lasagne.layers.get_all_params(output_layer, trainable=True))
    # accuracy.append(T.mean(T.square(prediction[0]-y_batch[0]))),
    # updates = OrderedDict()
    # update = lasagne.updates.sgd(loss_train[0], all_params[0][32::], LEARNING_RATE)
    # updates.update(update)
    # updates = lasagne.updates.apply_nesterov_momentum(updates, momentum=MOMENTUM)

    FitSetting = dict(
        Input_Shape=Input_Shape,
        Output_Shape=Output_Shape,
        output_layer=output_layer,
        saveParamName=saveParamName,
    )
    output_layer = [output_layer]
    output = lasagne.layers.get_output(output_layer)
    Fun_test(['./image/'], ['.jpg'], FitSetting, X_batch, output, writeimg=['TPRPN'])
Ejemplo n.º 18
0
def build_fan_reworked(input_var,
                       nb_filter=16,
                       input_size=(None, 3, tools.INP_PSIZE, tools.INP_PSIZE)):
    net = OrderedDict()

    # Input, standardization
    last = net['input'] = InputLayer(input_size, input_var=input_var)
    last = net['norm'] = ExpressionLayer(last, lambda x: normalize(x))

    # load feature encoder
    feats = get_features(last)
    net['features_s8_1'] = feats["conv4_4"]
    net['features_s8_2'] = feats["conv4_1"]
    net['features_s4'] = feats["conv3_3"]

    # Pretrained Encoder as before
    last = net["conv1_1"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_1"] = layers.NonUpdateBatchNormLayer(last)
    last = net["relu1_1"] = NonlinearityLayer(last, nonlinearity=rectify)
    last = net["conv1_2"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_2"] = layers.NonUpdateBatchNormLayer(last)
    last = net["relu1_2"] = NonlinearityLayer(last, nonlinearity=rectify)

    # feature aggregation at multiple scales
    last = net["bn1"] = layers.NonUpdateBatchNormLayer(last,
                                                       beta=None,
                                                       gamma=None)
    last = fan_module_improved(last,
                               net,
                               "s8_1",
                               net['features_s8_1'],
                               nb_filter=nb_filter,
                               scale=8,
                               upsampling_strategy="repeat")
    last = net["bn2"] = layers.NonUpdateBatchNormLayer(last,
                                                       beta=None,
                                                       gamma=None)
    last = fan_module_improved(last,
                               net,
                               "s8_2",
                               net['features_s8_2'],
                               nb_filter=nb_filter,
                               scale=8,
                               upsampling_strategy="repeat")
    last = net["bn3"] = layers.NonUpdateBatchNormLayer(last,
                                                       beta=None,
                                                       gamma=None)
    last = fan_module_improved(last,
                               net,
                               "s4",
                               net['features_s4'],
                               nb_filter=nb_filter,
                               scale=4,
                               upsampling_strategy="repeat")
    # unclear if Fixed, NonUpdate or Regular Layer will work best...
    last = net["bn4"] = BatchNormLayer(last)

    # Decoder as before
    last = net["deconv1_2"] = transpose(last,
                                        net["conv1_2"],
                                        nonlinearity=None)
    last = net["deconv1_1"] = transpose(last,
                                        net["conv1_1"],
                                        nonlinearity=None)

    return last, net
Ejemplo n.º 19
0
def G_mnist_mode_recovery(num_channels=1,
                          resolution=32,
                          fmap_base=64,
                          fmap_decay=1.0,
                          fmap_max=256,
                          latent_size=None,
                          label_size=10,
                          normalize_latents=True,
                          use_wscale=False,
                          use_pixelnorm=False,
                          use_batchnorm=True,
                          tanh_at_end=True,
                          progressive=False,
                          **kwargs):

    R = int(np.log2(resolution))
    assert resolution == 2**R and resolution >= 4
    cur_lod = theano.shared(np.float32(0.0))

    def nf(stage):
        return min(int(fmap_base / (2.0**(stage * fmap_decay))), fmap_max)

    def PN(layer):
        return PixelNormLayer(layer, name=layer.name +
                              'pn') if use_pixelnorm else layer

    def BN(layer):
        return lasagne.layers.batch_norm(layer) if use_batchnorm else layer

    def WS(layer):
        return WScaleLayer(layer, name=layer.name +
                           'S') if use_wscale else layer

    if latent_size is None: latent_size = nf(0)

    input_layers = [InputLayer(name='Glatents', shape=[None, latent_size])]
    net = input_layers[-1]
    if normalize_latents:
        net = PixelNormLayer(net, name='Glnorm')
    if label_size:
        input_layers += [InputLayer(name='Glabels', shape=[None, label_size])]
        net = ConcatLayer(name='Gina', incomings=[net, input_layers[-1]])

    net = ReshapeLayer(name='Ginb', incoming=net, shape=[[0], [1], 1, 1])
    net = PN(
        BN(
            WS(
                Conv2DLayer(net,
                            name='G1a',
                            num_filters=64,
                            filter_size=4,
                            pad='full',
                            nonlinearity=vlrelu,
                            W=irelu))))

    lods = [net]
    for I in xrange(2, R):  # I = 2, 3, ..., R-1
        net = Upscale2DLayer(net, name='G%dup' % I, scale_factor=2)
        net = PN(
            BN(
                WS(
                    Conv2DLayer(net,
                                name='G%da' % I,
                                num_filters=nf(I - 1),
                                filter_size=3,
                                pad=1,
                                nonlinearity=vlrelu,
                                W=irelu))))
        lods += [net]

    if progressive:
        lods = [
            WS(
                Conv2DLayer(l,
                            name='Glod%d' % i,
                            num_filters=num_channels,
                            filter_size=3,
                            pad=1,
                            nonlinearity=linear,
                            W=ilinear)) for i, l in enumerate(reversed(lods))
        ]  # Should be this
        #lods = [WS(NINLayer(l, name='Glod%d' % i, num_units=num_channels, nonlinearity=linear, W=ilinear)) for i, l in enumerate(reversed(lods))]                                  # .. but this is better
        output_layer = LODSelectLayer(name='Glod',
                                      incomings=lods,
                                      cur_lod=cur_lod,
                                      first_incoming_lod=0)
    else:
        net = WS(
            Conv2DLayer(net,
                        name='toRGB',
                        num_filters=num_channels,
                        filter_size=3,
                        pad=1,
                        nonlinearity=linear,
                        W=ilinear))  # Should be this
        #net = WS(NINLayer(net, name='toRGB', num_units=num_channels, nonlinearity=linear, W=ilinear))                                                                              # .. but this is better
        output_layer = net

    if tanh_at_end:
        output_layer = NonlinearityLayer(output_layer,
                                         name='Gtanh',
                                         nonlinearity=tanh)

    return dict(input_layers=input_layers,
                output_layers=[output_layer],
                cur_lod=cur_lod)
Ejemplo n.º 20
0
def build_model():
    net = {}
    net['input'] = InputLayer((None, 3, 224, 224))
    net['conv1_1'] = ConvLayer(net['input'], 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['conv3_3'] = ConvLayer(net['conv3_2'],
                               256,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv3_4'] = ConvLayer(net['conv3_3'],
                               256,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool3'] = PoolLayer(net['conv3_4'], 2)
    net['conv4_1'] = ConvLayer(net['pool3'], 512, 3, pad=1, flip_filters=False)
    net['conv4_2'] = ConvLayer(net['conv4_1'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv4_3'] = ConvLayer(net['conv4_2'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv4_4'] = ConvLayer(net['conv4_3'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool4'] = PoolLayer(net['conv4_4'], 2)
    net['conv5_1'] = ConvLayer(net['pool4'], 512, 3, pad=1, flip_filters=False)
    net['conv5_2'] = ConvLayer(net['conv5_1'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv5_3'] = ConvLayer(net['conv5_2'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv5_4'] = ConvLayer(net['conv5_3'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool5'] = PoolLayer(net['conv5_4'], 2)
    net['fc6'] = DenseLayer(net['pool5'], num_units=4096)
    net['drop6'] = DropoutLayer(net['fc6'], p=0.6)
    net['fc7'] = DenseLayer(net['drop6'], num_units=4096)
    net['drop7'] = DropoutLayer(net['fc7'], p=0.6)
    net['fc8'] = DenseLayer(net['drop7'], num_units=1000, nonlinearity=None)
    net['prob'] = NonlinearityLayer(net['fc8'], softmax)

    return net
    def build_network_final_layer(cls,
                                  input_shape=None,
                                  pool_layers_to_expand=None,
                                  pool_layers_to_remove=None,
                                  full_conv=False,
                                  pad_fc6=False,
                                  last_layer_name=None,
                                  input_layer_constructor=None,
                                  **kwargs):
        if pool_layers_to_expand is None:
            pool_layers_to_expand = set()
        elif isinstance(pool_layers_to_expand, six.string_types):
            pool_layers_to_expand = {pool_layers_to_expand}
        elif isinstance(pool_layers_to_expand, set):
            pass
        else:
            raise ValueError(
                'pool_layers_to_expand should be None, a string or a set, not a {}'
                .format(type(pool_layers_to_expand)))

        if pool_layers_to_remove is None:
            pool_layers_to_remove = set()
        elif isinstance(pool_layers_to_remove, six.string_types):
            pool_layers_to_remove = {pool_layers_to_remove}
        elif isinstance(pool_layers_to_remove, set):
            pass
        else:
            raise ValueError(
                'pool_layers_to_remove should be None, a string or a set, not a {}'
                .format(type(pool_layers_to_remove)))

        if input_shape is None:
            # Default input shape: 3 channel images of size 224 x 224.
            input_shape = (3, 224, 224)

        dilation = 1

        try:
            if input_layer_constructor is None:
                # Input layer: shape is of the form (sample, channel, height, width).
                # We leave the sample dimension with no size (`None`) so that the
                # minibatch size is whatever we need it to be when we use it
                net = InputLayer((None, ) + input_shape, name='input')
            else:
                net = input_layer_constructor()
            if last_layer_name == 'input':
                raise StopIteration

            # First two convolutional layers: 64 filters, 3x3 convolution, 1 pixel padding
            net, dilation = cls.conv_2d_layer(net, 'conv1_1', 64, 3, dilation)
            if last_layer_name == 'conv1_1':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv1_2', 64, 3, dilation)
            if last_layer_name == 'conv1_2':
                raise StopIteration
            # 2x2 max-pooling; will reduce size from 224x224 to 112x112
            net, dilation = cls.pool_2d_layer(net, 'pool1', 2, dilation,
                                              pool_layers_to_expand,
                                              pool_layers_to_remove)
            if last_layer_name == 'pool1':
                raise StopIteration

            # Two convolutional layers, 128 filters
            net, dilation = cls.conv_2d_layer(net, 'conv2_1', 128, 3, dilation)
            if last_layer_name == 'conv2_1':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv2_2', 128, 3, dilation)
            if last_layer_name == 'conv2_2':
                raise StopIteration
            # 2x2 max-pooling; will reduce size from 112x112 to 56x56
            net, dilation = cls.pool_2d_layer(net, 'pool2', 2, dilation,
                                              pool_layers_to_expand,
                                              pool_layers_to_remove)
            if last_layer_name == 'pool2':
                raise StopIteration

            # Three convolutional layers, 256 filters
            net, dilation = cls.conv_2d_layer(net, 'conv3_1', 256, 3, dilation)
            if last_layer_name == 'conv3_1':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv3_2', 256, 3, dilation)
            if last_layer_name == 'conv3_2':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv3_3', 256, 3, dilation)
            if last_layer_name == 'conv3_3':
                raise StopIteration
            # 2x2 max-pooling; will reduce size from 56x56 to 28x28
            net, dilation = cls.pool_2d_layer(net, 'pool3', 2, dilation,
                                              pool_layers_to_expand,
                                              pool_layers_to_remove)
            if last_layer_name == 'pool3':
                raise StopIteration

            # Three convolutional layers, 512 filters
            net, dilation = cls.conv_2d_layer(net, 'conv4_1', 512, 3, dilation)
            if last_layer_name == 'conv4_1':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv4_2', 512, 3, dilation)
            if last_layer_name == 'conv4_2':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv4_3', 512, 3, dilation)
            if last_layer_name == 'conv4_3':
                raise StopIteration
            # 2x2 max-pooling; will reduce size from 28x28 to 14x14
            net, dilation = cls.pool_2d_layer(net, 'pool4', 2, dilation,
                                              pool_layers_to_expand,
                                              pool_layers_to_remove)
            if last_layer_name == 'pool4':
                raise StopIteration

            # Three convolutional layers, 512 filters
            net, dilation = cls.conv_2d_layer(net, 'conv5_1', 512, 3, dilation)
            if last_layer_name == 'conv5_1':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv5_2', 512, 3, dilation)
            if last_layer_name == 'conv5_2':
                raise StopIteration
            net, dilation = cls.conv_2d_layer(net, 'conv5_3', 512, 3, dilation)
            if last_layer_name == 'conv5_3':
                raise StopIteration
            # 2x2 max-pooling; will reduce size from 14x14 to 7x7
            net, dilation = cls.pool_2d_layer(net, 'pool5', 2, dilation,
                                              pool_layers_to_expand,
                                              pool_layers_to_remove)
            if last_layer_name == 'pool5':
                raise StopIteration

            if dilation == 1 and not full_conv:
                # Dense layer, 4096 units
                net = DenseLayer(net, num_units=4096, name='fc6')
                if last_layer_name == 'fc6':
                    raise StopIteration
                # 50% dropout (only applied during training, turned off during prediction)
                net = DropoutLayer(net, p=0.5, name='fc6_dropout')
                if last_layer_name == 'fc6_dropout':
                    raise StopIteration

                # Dense layer, 4096 units
                net = DenseLayer(net, num_units=4096, name='fc7')
                if last_layer_name == 'fc7':
                    raise StopIteration
                # 50% dropout (only applied during training, turned off during prediction)
                net = DropoutLayer(net, p=0.5, name='fc7_dropout')
                if last_layer_name == 'fc7_dropout':
                    raise StopIteration

                # Final dense layer, 1000 units: 1 for each class
                net = DenseLayer(net,
                                 num_units=1000,
                                 nonlinearity=None,
                                 name='fc8')
                if last_layer_name == 'fc8':
                    raise StopIteration
                # Softmax non-linearity that will generate probabilities
                net = NonlinearityLayer(net, softmax, name='prob')
            elif full_conv:
                # Dense layer as 7x7 convolution, 4096 units
                fc6_padding = 3 if pad_fc6 else 0
                net, dilation = cls.conv_2d_layer(net,
                                                  'fc6',
                                                  4096,
                                                  7,
                                                  dilation,
                                                  pad=fc6_padding)
                if last_layer_name == 'fc6':
                    raise StopIteration
                # 50% dropout (only applied during training, turned off during prediction)
                net = DropoutLayer(net, p=0.5, name='fc6_dropout')
                if last_layer_name == 'fc6_dropout':
                    raise StopIteration

                # Dense layer, 4096 units
                net, dilation = cls.nin_layer(net, 'fc7', 4096, dilation)
                if last_layer_name == 'fc7':
                    raise StopIteration
                # 50% dropout (only applied during training, turned off during prediction)
                net = DropoutLayer(net, p=0.5, name='fc7_dropout')
                if last_layer_name == 'fc7_dropout':
                    raise StopIteration

                # Final dense layer, 1000 units: 1 for each class
                net, dilation = cls.nin_layer(net,
                                              'fc8',
                                              1000,
                                              dilation,
                                              nonlinearity=None)
                if last_layer_name == 'fc8':
                    raise StopIteration
                # Softmax non-linearity that will generate probabilities
                net = NonlinearityLayer(net,
                                        functools.partial(
                                            util.flexible_softmax, axis=1),
                                        name='prob')
        except StopIteration:
            pass

        return net
Ejemplo n.º 22
0
def build_vgg_model(prefix,
                    inputLayer=net_input,
                    classificationFlag=False,
                    stnFlag=False,
                    dropout_ratio=0.5):
    net = {}

    net[prefix + 'input'] = inputLayer

    net[prefix + 'conv1_1'] = ConvLayer(net[prefix + 'input'],
                                        64,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv1_1')

    net[prefix + 'conv1_2'] = ConvLayer(net[prefix + 'conv1_1'],
                                        64,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv1_2')

    net[prefix + 'pool1'] = PoolLayer(net[prefix + 'conv1_2'],
                                      2,
                                      name=prefix + 'pool1')

    net[prefix + 'conv2_1'] = ConvLayer(net[prefix + 'pool1'],
                                        128,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv2_1')

    net[prefix + 'conv2_2'] = ConvLayer(net[prefix + 'conv2_1'],
                                        128,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv2_2')

    net[prefix + 'pool2'] = PoolLayer(net[prefix + 'conv2_2'],
                                      2,
                                      name=prefix + 'pool2')

    net[prefix + 'conv3_1'] = ConvLayer(net[prefix + 'pool2'],
                                        256,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv3_1')

    net[prefix + 'conv3_2'] = ConvLayer(net[prefix + 'conv3_1'],
                                        256,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv3_2')

    net[prefix + 'conv3_3'] = ConvLayer(net[prefix + 'conv3_2'],
                                        256,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv3_3')

    net[prefix + 'pool3'] = PoolLayer(net[prefix + 'conv3_3'],
                                      2,
                                      name=prefix + 'pool3')

    net[prefix + 'conv4_1'] = ConvLayer(net[prefix + 'pool3'],
                                        512,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv4_1')

    net[prefix + 'conv4_2'] = ConvLayer(net[prefix + 'conv4_1'],
                                        512,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv4_2')

    net[prefix + 'conv4_3'] = ConvLayer(net[prefix + 'conv4_2'],
                                        512,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv4_3')

    net[prefix + 'pool4'] = PoolLayer(net[prefix + 'conv4_3'],
                                      2,
                                      name=prefix + 'pool4')

    net[prefix + 'conv5_1'] = ConvLayer(net[prefix + 'pool4'],
                                        512,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv5_1')

    net[prefix + 'conv5_2'] = ConvLayer(net[prefix + 'conv5_1'],
                                        512,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv5_2')

    net[prefix + 'conv5_3'] = ConvLayer(net[prefix + 'conv5_2'],
                                        512,
                                        3,
                                        pad=1,
                                        flip_filters=False,
                                        name=prefix + 'conv5_3')

    net[prefix + 'pool5'] = PoolLayer(net[prefix + 'conv5_3'],
                                      2,
                                      name=prefix + 'pool5')

    if stnFlag == False:
        net[prefix + 'fc6'] = DenseLayer(net[prefix + 'pool5'],
                                         num_units=4096,
                                         name=prefix + 'fc6')

        net[prefix + 'fc6_dropout'] = DropoutLayer(net[prefix + 'fc6'],
                                                   p=dropout_ratio,
                                                   name=prefix + 'fc6_dropout')

        net[prefix + 'fc7'] = DenseLayer(net[prefix + 'fc6_dropout'],
                                         num_units=4096,
                                         name=prefix + 'fc7')

        net[prefix + 'fc7_dropout'] = DropoutLayer(net[prefix + 'fc7'],
                                                   p=dropout_ratio,
                                                   name=prefix + 'fc7_dropout')

        if classificationFlag == True:
            net[prefix + 'fc8'] = DenseLayer(net[prefix + 'fc7_dropout'],
                                             num_units=67,
                                             nonlinearity=None,
                                             name=prefix + 'fc8')

            net[prefix + 'prob'] = NonlinearityLayer(net[prefix + 'fc8'],
                                                     softmax,
                                                     name=prefix + 'prob')

    return net
Ejemplo n.º 23
0
 def nonlinearity(incoming):
     """Apply the standard nonlinearity."""
     return NonlinearityLayer(incoming, nonlinearity=rectify)
Ejemplo n.º 24
0
def build_baseline8_fan_bilinear(input_var, nb_filter=96):
    net = OrderedDict()

    # Input, standardization
    last = net['input'] = InputLayer(
        (None, 3, tools.INP_PSIZE, tools.INP_PSIZE), input_var=input_var)
    last = net['norm'] = ExpressionLayer(last, lambda x: normalize(x))

    # load feature encoder
    net['features_s8'] = get_features(last)["conv4_1"]
    net['features_s4'] = get_features(last)["conv3_3"]

    # Pretrained Encoder as before
    last = net["conv1_1"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_1"] = BatchNormLayer(last)
    last = net["relu1_1"] = NonlinearityLayer(last, nonlinearity=rectify)
    last = net["conv1_2"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_2"] = BatchNormLayer(last)
    last = net["relu1_2"] = NonlinearityLayer(last, nonlinearity=rectify)

    # Modified Middle Part
    last = net["middle"] = ConvLayer(last, nb_filter, 1, nonlinearity=linear)

    # feature aggregation at multiple scales
    last = net["bn1"] = BatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s8",
                             net['features_s8'],
                             nb_filter=nb_filter,
                             scale=8,
                             upsampling_strategy="bilinear")
    last = net["bn2"] = BatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s4",
                             net['features_s4'],
                             nb_filter=nb_filter,
                             scale=4,
                             upsampling_strategy="bilinear")

    # Decoder as before
    last = net["deconv1_2"] = transpose(last,
                                        net["conv1_2"],
                                        nonlinearity=None)
    last = net["deconv1_1"] = transpose(last,
                                        net["conv1_1"],
                                        nonlinearity=None)

    last = net["bn"] = BatchNormLayer(last,
                                      beta=nn.init.Constant(128.),
                                      gamma=nn.init.Constant(25.))

    return last, net
Ejemplo n.º 25
0
def build_model_vgg16(input_shape, verbose):
    '''
    See Lasagne Modelzoo:
    https://github.com/Lasagne/Recipes/blob/master/modelzoo/vgg16.py
    
    '''

    if verbose: print('VGG16 (from lasagne model zoo)')

    net = {}
    net['input'] = InputLayer(input_shape)
    net['conv1_1'] = ConvLayer(net['input'], 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['conv3_3'] = ConvLayer(net['conv3_2'],
                               256,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool3'] = PoolLayer(net['conv3_3'], 2)
    net['conv4_1'] = ConvLayer(net['pool3'], 512, 3, pad=1, flip_filters=False)
    net['conv4_2'] = ConvLayer(net['conv4_1'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv4_3'] = ConvLayer(net['conv4_2'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['pool4'] = PoolLayer(net['conv4_3'], 2)
    net['conv5_1'] = ConvLayer(net['pool4'], 512, 3, pad=1, flip_filters=False)
    net['conv5_2'] = ConvLayer(net['conv5_1'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    net['conv5_3'] = ConvLayer(net['conv5_2'],
                               512,
                               3,
                               pad=1,
                               flip_filters=False)
    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)

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

    return net
Ejemplo n.º 26
0
def build_baseline9_fan_fan_bilinear(input_var, nb_filter=96):
    net = OrderedDict()

    import theano.tensor as T
    import numpy as np

    # Input, standardization
    last = net['input'] = InputLayer(
        (None, 3, tools.INP_PSIZE, tools.INP_PSIZE), input_var=input_var)
    last = net['norm'] = ExpressionLayer(last, lambda x: normalize(x))

    # load feature encoder
    net['features_s8'] = get_features(last)["conv4_1"]
    net['features_s4'] = get_features(last)["conv3_3"]
    net['mask'] = ExpressionLayer(
        layers.upsample(net["features_s8"], 8, mode="bilinear"),
        lambda x: 1. * T.eq(x, x.max(axis=1, keepdims=True)))

    # Pretrained Encoder as before
    last = net["conv1_1"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_1"] = BatchNormLayer(last)
    last = net["relu1_1"] = NonlinearityLayer(last, nonlinearity=rectify)
    last = net["conv1_2"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_2"] = BatchNormLayer(last)
    last = net["relu1_2"] = NonlinearityLayer(last, nonlinearity=rectify)

    # Modified Middle Part
    last = net["middle"] = ConvLayer(last, nb_filter, 1, nonlinearity=linear)

    # feature aggregation at multiple scales
    last = net["fan1"] = FeatureAwareNormLayer((last, net['mask']))
    last = fan_module_simple(last,
                             net,
                             "s8",
                             net['features_s8'],
                             nb_filter=nb_filter,
                             scale=8,
                             upsampling_strategy="bilinear")
    last = net["fan2"] = FeatureAwareNormLayer((last, net['mask']))
    last = fan_module_simple(last,
                             net,
                             "s4",
                             net['features_s4'],
                             nb_filter=nb_filter,
                             scale=4,
                             upsampling_strategy="bilinear")

    # Decoder as before
    last = net["deconv1_2"] = transpose(last,
                                        net["conv1_2"],
                                        nonlinearity=None)
    last = net["deconv1_1"] = transpose(last,
                                        net["conv1_1"],
                                        nonlinearity=None)

    last = net["fan"] = FeatureAwareNormLayer(
        (last, net['mask']),
        beta=nn.init.Constant(np.float32(128.)),
        gamma=nn.init.Constant(np.float32(25.)))

    return last, net
    def build_network_final_layer(cls, input_shape=None):
        if input_shape is None:
            # Default input shape: 3 channel images of size 224 x 224.
            input_shape = (3, 224, 224)

        # Input layer: shape is of the form (sample, channel, height, width).
        # We leave the sample dimension with no size (`None`) so that the
        # minibatch size is whatever we need it to be when we use it
        net = InputLayer((None, ) + input_shape, name='input')

        # First two convolutional layers: 64 filters, 3x3 convolution, 1 pixel padding
        net = Conv2DLayer(net,
                          64,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv1_1')
        net = Conv2DLayer(net,
                          64,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv1_2')
        # 2x2 max-pooling; will reduce size from 224x224 to 112x112
        net = Pool2DLayer(net, 2, name='pool1')

        # Two convolutional layers, 128 filters
        net = Conv2DLayer(net,
                          128,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv2_1')
        net = Conv2DLayer(net,
                          128,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv2_2')
        # 2x2 max-pooling; will reduce size from 112x112 to 56x56
        net = Pool2DLayer(net, 2, name='pool2')

        # Four convolutional layers, 256 filters
        net = Conv2DLayer(net,
                          256,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv3_1')
        net = Conv2DLayer(net,
                          256,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv3_2')
        net = Conv2DLayer(net,
                          256,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv3_3')
        net = Conv2DLayer(net,
                          256,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv3_4')
        # 2x2 max-pooling; will reduce size from 56x56 to 28x28
        net = Pool2DLayer(net, 2, name='pool3')

        # Four convolutional layers, 512 filters
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv4_1')
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv4_2')
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv4_3')
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv4_4')
        # 2x2 max-pooling; will reduce size from 28x28 to 14x14
        net = Pool2DLayer(net, 2, name='pool4')

        # Four convolutional layers, 512 filters
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv5_1')
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv5_2')
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv5_3')
        net = Conv2DLayer(net,
                          512,
                          3,
                          pad=1,
                          flip_filters=False,
                          name='conv5_4')
        # 2x2 max-pooling; will reduce size from 14x14 to 7x7
        net = Pool2DLayer(net, 2, name='pool5')

        # Dense layer, 4096 units
        net = DenseLayer(net, num_units=4096, name='fc6')
        # 50% dropout (only applied during training, turned off during prediction)
        net = DropoutLayer(net, p=0.5, name='fc6_dropout')

        # Dense layer, 4096 units
        net = DenseLayer(net, num_units=4096, name='fc7')
        # 50% dropout (only applied during training, turned off during prediction)
        net = DropoutLayer(net, p=0.5, name='fc7_dropout')

        # Final dense layer, 1000 units: 1 for each class
        net = DenseLayer(net, num_units=1000, nonlinearity=None, name='fc8')
        # Softmax non-linearity that will generate probabilities
        net = NonlinearityLayer(net, softmax, name='prob')

        return net
Ejemplo n.º 28
0
def build_finetuned2_fan(input_var,
                         nb_filter=96,
                         input_size=(None, 3, tools.INP_PSIZE,
                                     tools.INP_PSIZE)):
    net = OrderedDict()

    # Input, standardization
    last = net['input'] = InputLayer(input_size, input_var=input_var)
    last = net['norm'] = ExpressionLayer(last, lambda x: normalize(x))

    # load feature encoder
    # TODO this is clearly a bug. only for compatibility reasons. remove once all weights are converted
    net['features_s8'] = get_features(last)["conv4_1"]
    net['features_s4'] = get_features(last)["conv3_3"]

    # Pretrained Encoder as before
    last = net["conv1_1"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_1"] = layers.NonUpdateBatchNormLayer(last)
    last = net["relu1_1"] = NonlinearityLayer(last, nonlinearity=rectify)
    last = net["conv1_2"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_2"] = layers.NonUpdateBatchNormLayer(last)
    last = net["relu1_2"] = NonlinearityLayer(last, nonlinearity=rectify)

    # Modified Middle Part
    last = net["middle"] = ConvLayer(last, nb_filter, 1, nonlinearity=linear)

    # feature aggregation at multiple scales
    last = net["bn1"] = layers.NonUpdateBatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s8",
                             net['features_s8'],
                             nb_filter=nb_filter,
                             scale=8)
    last = net["bn2"] = layers.NonUpdateBatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s4",
                             net['features_s4'],
                             nb_filter=nb_filter,
                             scale=4)

    # Decoder as before
    last = net["deconv1_2"] = transpose(last,
                                        net["conv1_2"],
                                        nonlinearity=None)
    last = net["deconv1_1"] = transpose(last,
                                        net["conv1_1"],
                                        nonlinearity=None)

    last = net["bn"] = layers.FixedBatchNormLayer(last)

    weights = "170123_runs/run_H.E.T._1485012575.4045253/3.npz"
    data = tools.load_weights(last, weights)

    return last, net
Ejemplo n.º 29
0
def build_model():
    net = {}
    net['input'] = InputLayer((None, 3, None, None))
    net['conv1/7x7_s2'] = ConvLayer(net['input'],
                                    64,
                                    7,
                                    stride=2,
                                    pad=3,
                                    flip_filters=False)
    net['pool1/3x3_s2'] = PoolLayer(net['conv1/7x7_s2'],
                                    pool_size=3,
                                    stride=2,
                                    ignore_border=False)
    net['pool1/norm1'] = LRNLayer(net['pool1/3x3_s2'], alpha=0.00002, k=1)
    net['conv2/3x3_reduce'] = ConvLayer(net['pool1/norm1'],
                                        64,
                                        1,
                                        flip_filters=False)
    net['conv2/3x3'] = ConvLayer(net['conv2/3x3_reduce'],
                                 192,
                                 3,
                                 pad=1,
                                 flip_filters=False)
    net['conv2/norm2'] = LRNLayer(net['conv2/3x3'], alpha=0.00002, k=1)
    net['pool2/3x3_s2'] = PoolLayer(net['conv2/norm2'],
                                    pool_size=3,
                                    stride=2,
                                    ignore_border=False)

    net.update(
        build_inception_module('inception_3a', net['pool2/3x3_s2'],
                               [32, 64, 96, 128, 16, 32]))
    net.update(
        build_inception_module('inception_3b', net['inception_3a/output'],
                               [64, 128, 128, 192, 32, 96]))
    net['pool3/3x3_s2'] = PoolLayer(net['inception_3b/output'],
                                    pool_size=3,
                                    stride=2,
                                    ignore_border=False)

    net.update(
        build_inception_module('inception_4a', net['pool3/3x3_s2'],
                               [64, 192, 96, 208, 16, 48]))
    net.update(
        build_inception_module('inception_4b', net['inception_4a/output'],
                               [64, 160, 112, 224, 24, 64]))
    net.update(
        build_inception_module('inception_4c', net['inception_4b/output'],
                               [64, 128, 128, 256, 24, 64]))
    net.update(
        build_inception_module('inception_4d', net['inception_4c/output'],
                               [64, 112, 144, 288, 32, 64]))
    net.update(
        build_inception_module('inception_4e', net['inception_4d/output'],
                               [128, 256, 160, 320, 32, 128]))
    net['pool4/3x3_s2'] = PoolLayer(net['inception_4e/output'],
                                    pool_size=3,
                                    stride=2,
                                    ignore_border=False)

    net.update(
        build_inception_module('inception_5a', net['pool4/3x3_s2'],
                               [128, 256, 160, 320, 32, 128]))
    net.update(
        build_inception_module('inception_5b', net['inception_5a/output'],
                               [128, 384, 192, 384, 48, 128]))

    net['pool5/7x7_s1'] = GlobalPoolLayer(net['inception_5b/output'])
    net['loss3/classifier'] = DenseLayer(net['pool5/7x7_s1'],
                                         num_units=1000,
                                         nonlinearity=linear)
    net['prob'] = NonlinearityLayer(net['loss3/classifier'],
                                    nonlinearity=softmax)
    return net
Ejemplo n.º 30
0
def build_big_fan(input_var,
                  nb_filter=96,
                  input_size=(None, 3, tools.INP_PSIZE, tools.INP_PSIZE)):
    net = OrderedDict()

    # Input, standardization
    last = net['input'] = InputLayer(input_size, input_var=input_var)
    last = net['norm'] = ExpressionLayer(last, lambda x: normalize(x))

    # load feature encoder
    f = get_features(last)
    net['features_s8'] = f["conv4_1"]
    net['features_s4'] = f["conv3_3"]

    # Pretrained Encoder as before
    last = net["conv1_1"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_1"] = layers.NonUpdateBatchNormLayer(last)
    last = net["relu1_1"] = NonlinearityLayer(last, nonlinearity=rectify)
    last = net["conv1_2"] = ConvLayer(last,
                                      nb_filter,
                                      1,
                                      pad=0,
                                      flip_filters=False,
                                      nonlinearity=linear)
    last = net["bn1_2"] = layers.NonUpdateBatchNormLayer(last)
    last = net["relu1_2"] = NonlinearityLayer(last, nonlinearity=rectify)

    # Modified Middle Part
    last = net["middle"] = ConvLayer(last, nb_filter, 1, nonlinearity=linear)

    # feature aggregation at multiple scales
    last = net["bn1"] = layers.NonUpdateBatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s8",
                             net['features_s8'],
                             nb_filter=nb_filter,
                             scale=8)
    last = net["bn1"] = layers.NonUpdateBatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s8",
                             net['features_s8'],
                             nb_filter=nb_filter,
                             scale=8)
    last = net["bn3"] = layers.NonUpdateBatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s4",
                             net['features_s4'],
                             nb_filter=nb_filter,
                             scale=4)
    last = net["bn4"] = layers.NonUpdateBatchNormLayer(last)
    last = fan_module_simple(last,
                             net,
                             "s4",
                             net['features_s4'],
                             nb_filter=nb_filter,
                             scale=4)
    last = net["bn5"] = layers.NonUpdateBatchNormLayer(last)

    # Decoder as before
    last = net["deconv1_2"] = transpose(last,
                                        net["conv1_2"],
                                        nonlinearity=None)
    last = net["deconv1_1"] = transpose(last,
                                        net["conv1_1"],
                                        nonlinearity=None)

    return last, net