Ejemplo n.º 1
0
def SmallNet(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'] = Conv2DLayer(net['input'], num_filters=32, filter_size=(7, 7), stride=(2, 2))
    net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(2, 2))
    net['conv2'] = Conv2DLayer(net['pool1'], num_filters=64, filter_size=(5, 5), stride=(2, 2))
    net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(2, 2))
    net['conv3'] = Conv2DLayer(net['pool2'], num_filters=128, filter_size=(3, 3), pad=(1, 1))
    net['conv4'] = Conv2DLayer(net['conv3'], num_filters=128, filter_size=(3, 3), pad=(1, 1))

    net['conv5_p'] = Conv2DLayer(net['conv4'], num_filters=64, filter_size=(1, 1))
    net['conv6_p'] = 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.º 2
0
def build_nets(input_var, channels=1, do_batchnorm=True, z_dim=100):
    
    def ns(shape):
        ret=list(shape)
        ret[0]=[0]
        return tuple(ret)
    
    ret = {}
    bn = batch_norm if do_batchnorm else lambda x:x
    ret['ae_in'] = layer = InputLayer(shape=(None,channels,28,28), input_var=input_var)
    ret['ae_conv1'] = layer = bn(Conv2DLayer(layer, num_filters=64, filter_size=5))
    ret['ae_pool1'] = layer = MaxPool2DLayer(layer, pool_size=2)
    ret['ae_conv2'] = layer = bn(Conv2DLayer(layer, num_filters=128, filter_size=3))
    ret['ae_pool2'] = layer = MaxPool2DLayer(layer, pool_size=2)
    ret['ae_enc'] = layer = DenseLayer(layer, num_units=z_dim,
            nonlinearity=nn.nonlinearities.tanh)
    ret['ae_unenc'] = layer = bn(nn.layers.DenseLayer(layer,
        num_units = np.product(nn.layers.get_output_shape(ret['ae_pool2'])[1:])))
    ret['ae_resh'] = layer = ReshapeLayer(layer,
            shape=ns(nn.layers.get_output_shape(ret['ae_pool2'])))
    ret['ae_depool2'] = layer = Upscale2DLayer(layer, scale_factor=2)
    ret['ae_deconv2'] = layer = bn(Conv2DLayer(layer, num_filters=64, filter_size=3,
        pad='full'))
    ret['ae_depool1'] = layer = Upscale2DLayer(layer, scale_factor=2)
    ret['ae_out'] = Conv2DLayer(layer, num_filters=1, filter_size=5, pad='full',
            nonlinearity=nn.nonlinearities.sigmoid)
    
    ret['disc_in'] = layer = InputLayer(shape=(None,channels,28,28), input_var=input_var)
    ret['disc_conv1'] = layer = bn(Conv2DLayer(layer, num_filters=64, filter_size=5))
    ret['disc_pool1'] = layer = MaxPool2DLayer(layer, pool_size=2)
    ret['disc_conv2'] = layer = bn(Conv2DLayer(layer, num_filters=128, filter_size=3))
    ret['disc_pool2'] = layer = MaxPool2DLayer(layer, pool_size=2)
    ret['disc_hid'] = layer = bn(DenseLayer(layer, num_units=100))
    ret['disc_out'] = DenseLayer(layer, num_units=1, nonlinearity=nn.nonlinearities.sigmoid)
    
    return ret
Ejemplo n.º 3
0
def build_discriminator(input_var=None):
    from lasagne.layers import (InputLayer, Conv2DLayer, ReshapeLayer,
                                DenseLayer, batch_norm, DropoutLayer)
    from lasagne.layers.dnn import Conv2DDNNLayer as Conv2DLayer  # override
    from lasagne.nonlinearities import LeakyRectify, sigmoid
    lrelu = LeakyRectify(0.2)
    # input: (None, 3, 64, 64)
    layer = InputLayer(shape=(None, 3, 64, 64), input_var=input_var)
    # 2 convolutions
    layer = batch_norm(Conv2DLayer(layer, 128, 3, stride=1, pad=1, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 128, 3, stride=2, pad=1, nonlinearity=lrelu))
    # 2 convolutions
    layer = batch_norm(Conv2DLayer(layer, 192, 3, stride=1, pad=1, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 192, 3, stride=1, pad=1, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 192, 3, stride=2, pad=1, nonlinearity=lrelu))
    # 3 convolutions
    layer = batch_norm(Conv2DLayer(layer, 256, 5, stride=1, pad=2, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 256, 5, stride=1, pad=2, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 192, 5, stride=2, pad=2, nonlinearity=lrelu))
    # 4 convolutions
    layer = batch_norm(Conv2DLayer(layer, 384, 5, stride=1, pad=2, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 384, 5, stride=1, pad=2, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 384, 5, stride=1, pad=2, nonlinearity=lrelu))
    layer = batch_norm(Conv2DLayer(layer, 384, 5, stride=2, pad=2, nonlinearity=lrelu))
    # fully-connected layer
    layer = batch_norm(DenseLayer(layer, 4096, nonlinearity=lrelu)) 
    # output layer
    layer = batch_norm(DropoutLayer(layer, 0.5))
    # After FC layerm addth Global Average Pooling layer
    #layer = lasagne.layers.GlobalPoolLayer(layer)
    layer = DenseLayer(layer, 1, nonlinearity=sigmoid)
    print ("Discriminator output:", layer.output_shape)
    return layer
def define_net(input_var):
    net = {}
    net['data'] = InputLayer(shape=(None, 3, IMAGE_SHAPE[0], IMAGE_SHAPE[1]),
                             input_var=input_var)

    net['patch'] = sample_layer.Sample2DLayer(net['data'],
                                              5, (227, 227),
                                              pad=False)

    # conv1
    net['conv1'] = Conv2DLayer(net['patch'],
                               num_filters=96,
                               filter_size=(11, 11),
                               stride=4,
                               nonlinearity=lasagne.nonlinearities.rectify)

    # pool1
    net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(3, 3), stride=2)

    # norm1
    net['norm1'] = LocalResponseNormalization2DLayer(net['pool1'],
                                                     n=5,
                                                     alpha=0.0001 / 5.0,
                                                     beta=0.75,
                                                     k=1)

    # before conv2 split the data
    net['conv2_data1'] = SliceLayer(net['norm1'], indices=slice(0, 48), axis=1)
    net['conv2_data2'] = SliceLayer(net['norm1'],
                                    indices=slice(48, 96),
                                    axis=1)

    # now do the convolutions
    net['conv2_part1'] = Conv2DLayer(net['conv2_data1'],
                                     num_filters=128,
                                     filter_size=(5, 5),
                                     pad=2)
    net['conv2_part2'] = Conv2DLayer(net['conv2_data2'],
                                     num_filters=128,
                                     filter_size=(5, 5),
                                     pad=2)

    # now combine
    net['conv2'] = concat((net['conv2_part1'], net['conv2_part2']), axis=1)

    # pool2
    net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(3, 3), stride=2)

    # norm2
    net['norm2'] = LocalResponseNormalization2DLayer(net['pool2'],
                                                     n=5,
                                                     alpha=0.0001 / 5.0,
                                                     beta=0.75,
                                                     k=1)

    # conv3
    # no group
    net['conv3'] = Conv2DLayer(net['norm2'],
                               num_filters=384,
                               filter_size=(3, 3),
                               pad=1)

    # conv4
    # group = 2
    net['conv4_data1'] = SliceLayer(net['conv3'],
                                    indices=slice(0, 192),
                                    axis=1)
    net['conv4_data2'] = SliceLayer(net['conv3'],
                                    indices=slice(192, 384),
                                    axis=1)
    net['conv4_part1'] = Conv2DLayer(net['conv4_data1'],
                                     num_filters=192,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv4_part2'] = Conv2DLayer(net['conv4_data2'],
                                     num_filters=192,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv4'] = concat((net['conv4_part1'], net['conv4_part2']), axis=1)

    # conv5
    # group 2
    net['conv5_data1'] = SliceLayer(net['conv4'],
                                    indices=slice(0, 192),
                                    axis=1)
    net['conv5_data2'] = SliceLayer(net['conv4'],
                                    indices=slice(192, 384),
                                    axis=1)
    net['conv5_part1'] = Conv2DLayer(net['conv5_data1'],
                                     num_filters=128,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv5_part2'] = Conv2DLayer(net['conv5_data2'],
                                     num_filters=128,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv5'] = concat((net['conv5_part1'], net['conv5_part2']), axis=1)

    # pool 5
    net['pool5'] = MaxPool2DLayer(net['conv5'], pool_size=(3, 3), stride=2)

    # fc6
    net['fc6'] = DenseLayer(net['pool5'],
                            num_units=4096,
                            nonlinearity=lasagne.nonlinearities.rectify)

    # fc7
    net['fc7'] = DenseLayer(net['fc6'],
                            num_units=4096,
                            nonlinearity=lasagne.nonlinearities.rectify)

    # fc8
    net['out'] = DenseLayer(net['fc7'],
                            num_units=1,
                            nonlinearity=lasagne.nonlinearities.linear)

    # print ('Objective layer shapes:')
    # print (lasagne.layers.get_output_shape(net['pool5']))
    # # fc6
    # net['fc6'] = Conv2DLayer(
    #     net['pool5'], num_filters=4096, filter_size=(6, 6),
    #     nonlinearity=lasagne.nonlinearities.rectify, flip_filters=False)
    # print (lasagne.layers.get_output_shape(net['fc6']))
    # # fc7
    # net['fc7'] = Conv2DLayer(
    #     net['fc6'],
    #     num_filters=4096, filter_size=(1, 1),
    #     nonlinearity=lasagne.nonlinearities.rectify)
    # print (lasagne.layers.get_output_shape(net['fc7']))
    # # fc8
    # net['out'] = Conv2DLayer(
    #     net['fc7'],
    #     num_filters=1, filter_size=(1, 1),
    #     nonlinearity=lasagne.nonlinearities.linear)
    # print (lasagne.layers.get_output_shape(net['out']))
    return net
Ejemplo n.º 5
0
def Encoder(input_var, use_batch_norm=False):
    input_var = input_var.dimshuffle(0, 'x', 1, 2)
    net = InputLayer(shape=(None, 1, 28, 28), input_var=input_var)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(2, 2),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(3, 3),
                      stride=(2, 2),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(3, 3),
                      nonlinearity=lasagne.nonlinearities.elu)

    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(3, 3),
                      stride=(2, 2),
                      nonlinearity=lasagne.nonlinearities.elu)

    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(2, 2),
                      nonlinearity=lasagne.nonlinearities.elu)

    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(1, 1),
                      nonlinearity=lasagne.nonlinearities.elu)

    net = Conv2DLayer(net,
                      num_filters=128,
                      filter_size=(1, 1),
                      nonlinearity=lasagne.nonlinearities.elu)
    net = FlattenLayer(net, outdim=2)

    net = DenseLayer(net,
                     num_units=128,
                     nonlinearity=lasagne.nonlinearities.rectify)

    net = DenseLayer(net, num_units=40, nonlinearity=None)

    return net
Ejemplo n.º 6
0
def D_mnist_mode_recovery(
        num_channels=1,
        resolution=32,
        fmap_base=64,
        fmap_decay=1.0,
        fmap_max=256,
        mbstat_func='Tstdeps',
        mbstat_avg=None,  #'all',
        label_size=0,
        use_wscale=False,
        use_gdrop=False,
        use_layernorm=False,
        use_batchnorm=True,
        X=2,
        progressive=False,
        **kwargs):

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

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

    def GD(layer):
        return GDropLayer(layer,
                          name=layer.name + 'gd',
                          mode='prop',
                          strength=gdrop_strength) if use_gdrop else layer

    def LN(layer):
        return LayerNormLayer(layer, name=layer.name +
                              'ln') if use_layernorm else layer

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

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

    net = input_layer = InputLayer(name='Dimages',
                                   shape=[None, num_channels, 2**R, 2**R])
    for I in xrange(R - 1, 1, -1):  # I = R-1, R-2, ..., 2     (i.e. 4,3,2)
        net = BN(
            LN(
                WS(
                    Conv2DLayer(GD(net),
                                name='D%da' % I,
                                num_filters=nf(I - 1),
                                filter_size=3,
                                pad=1,
                                nonlinearity=lrelu,
                                W=ilrelu))))
        net = Downscale2DLayer(net, name='D%ddn' % I, scale_factor=2)
        if progressive:
            lod = Downscale2DLayer(input_layer,
                                   name='D%dxs' % (I - 1),
                                   scale_factor=2**(R - I))
            lod = WS(
                NINLayer(lod,
                         name='D%dx' % (I - 1),
                         num_units=nf(I - 1),
                         nonlinearity=lrelu,
                         W=ilrelu))
            net = LODSelectLayer(name='D%dlod' % (I - 1),
                                 incomings=[net, lod],
                                 cur_lod=cur_lod,
                                 first_incoming_lod=R - I - 1)

    if mbstat_avg is not None:
        net = MinibatchStatConcatLayer(net,
                                       name='Dstat',
                                       func=globals()[mbstat_func],
                                       averaging=mbstat_avg)

    net = FlattenLayer(GD(net), name='Dflatten')
    output_layers = [
        WS(
            DenseLayer(net,
                       name='Dscores',
                       num_units=1,
                       nonlinearity=linear,
                       W=ilinear))
    ]

    if label_size:
        output_layers += [
            WS(
                DenseLayer(net,
                           name='Dlabels',
                           num_units=label_size,
                           nonlinearity=linear,
                           W=ilinear))
        ]
    return dict(input_layers=[input_layer],
                output_layers=output_layers,
                cur_lod=cur_lod,
                gdrop_strength=gdrop_strength)
Ejemplo n.º 7
0
def D_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,
        mbstat_func='Tstdeps',
        mbstat_avg='all',
        mbdisc_kernels=None,
        use_wscale=True,
        use_gdrop=True,
        use_layernorm=False,
        **kwargs):

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

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

    def GD(layer):
        return GDropLayer(layer,
                          name=layer.name + 'gd',
                          mode='prop',
                          strength=gdrop_strength) if use_gdrop else layer

    def LN(layer):
        return LayerNormLayer(layer, name=layer.name +
                              'ln') if use_layernorm else layer

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

    input_layer = InputLayer(name='Dimages',
                             shape=[None, num_channels, 2**R, 2**R])
    net = WS(
        NINLayer(input_layer,
                 name='D%dx' % (R - 1),
                 num_units=nf(R - 1),
                 nonlinearity=lrelu,
                 W=ilrelu))

    for I in xrange(R - 1, 1, -1):  # I = R-1, R-2, ..., 2
        net = LN(
            WS(
                Conv2DLayer(GD(net),
                            name='D%db' % I,
                            num_filters=nf(I),
                            filter_size=3,
                            pad=1,
                            nonlinearity=lrelu,
                            W=ilrelu)))
        net = LN(
            WS(
                Conv2DLayer(GD(net),
                            name='D%da' % I,
                            num_filters=nf(I - 1),
                            filter_size=3,
                            pad=1,
                            nonlinearity=lrelu,
                            W=ilrelu)))
        net = Downscale2DLayer(net, name='D%ddn' % I, scale_factor=2)
        lod = Downscale2DLayer(input_layer,
                               name='D%dxs' % (I - 1),
                               scale_factor=2**(R - I))
        lod = WS(
            NINLayer(lod,
                     name='D%dx' % (I - 1),
                     num_units=nf(I - 1),
                     nonlinearity=lrelu,
                     W=ilrelu))
        net = LODSelectLayer(name='D%dlod' % (I - 1),
                             incomings=[net, lod],
                             cur_lod=cur_lod,
                             first_incoming_lod=R - I - 1)

    if mbstat_avg is not None:
        net = MinibatchStatConcatLayer(net,
                                       name='Dstat',
                                       func=globals()[mbstat_func],
                                       averaging=mbstat_avg)

    net = LN(
        WS(
            Conv2DLayer(GD(net),
                        name='D1b',
                        num_filters=nf(1),
                        filter_size=3,
                        pad=1,
                        nonlinearity=lrelu,
                        W=ilrelu)))
    net = LN(
        WS(
            Conv2DLayer(GD(net),
                        name='D1a',
                        num_filters=nf(0),
                        filter_size=4,
                        pad=0,
                        nonlinearity=lrelu,
                        W=ilrelu)))

    if mbdisc_kernels:
        import minibatch_discrimination
        net = minibatch_discrimination.MinibatchLayer(
            net, name='Dmd', num_kernels=mbdisc_kernels)

    output_layers = [
        WS(
            DenseLayer(net,
                       name='Dscores',
                       num_units=1,
                       nonlinearity=linear,
                       W=ilinear))
    ]
    if label_size:
        output_layers += [
            WS(
                DenseLayer(net,
                           name='Dlabels',
                           num_units=label_size,
                           nonlinearity=linear,
                           W=ilinear))
        ]
    return dict(input_layers=[input_layer],
                output_layers=output_layers,
                cur_lod=cur_lod,
                gdrop_strength=gdrop_strength)
def conv_block(net,no_f_base,f_size,pad,flip,nonlinearity,depth):
    for i in xrange(depth):
        net = Conv2DLayer(net,no_f_base,f_size,pad=pad,flip_filters=flip,nonlinearity=nonlinearity)
    net = Pool2DLayer(net,pool_size=2)
    return net
Ejemplo n.º 9
0
    def input_fused_convnets(self,
                             fusion_type,
                             input_var1=None,
                             input_var2=None,
                             bottleneck_W=None):
        net = OrderedDict()
        net['input_rgb'] = InputLayer((None, 4, 128, 128),
                                      input_var=input_var1)
        layer = 0
        net['input_depth'] = InputLayer((None, 1, 128, 128),
                                        input_var=input_var2)
        layer += 1

        if fusion_type == self.CONCAT:
            net['merge'] = concat([net['input_rgb'], net['input_depth']])
            layer += 1
        elif fusion_type == self.CONCATCONV:
            net['concat'] = concat([net['input_rgb'], net['input_depth']])
            layer += 1
            net['merge'] = Conv2DLayer(net['concat'],
                                       num_filters=1,
                                       filter_size=(1, 1),
                                       nonlinearity=None)
            layer += 1

        for i in range(self._net_specs_dict['num_conv_layers']):
            # Add convolution layers
            net['conv{0:d}'.format(i + 1)] = Conv2DLayer(
                net.values()[layer],
                num_filters=self._net_specs_dict['num_conv_filters'][i],
                filter_size=(self._net_specs_dict['conv_filter_size'][i], ) *
                2,
                pad='same')
            layer += 1
            if self._net_specs_dict['num_conv_layers'] <= 2:
                # Add pooling layers
                net['pool{0:d}'.format(i + 1)] = MaxPool2DLayer(
                    net.values()[layer], pool_size=(3, 3))
                layer += 1
            else:
                if i < 4:
                    if (i + 1) % 2 == 0:
                        # Add pooling layers
                        net['pool{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1
                else:
                    if (i + 1) == 7:
                        # Add pooling layers
                        net['pool{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1

        # Add fc-layers
        net['fc1'] = DenseLayer(net.values()[layer],
                                self._net_specs_dict['num_fc_units'][0])
        # Add dropout layer
        net['dropout1'] = dropout(net['fc1'], p=self._model_hp_dict['p'])
        net['fc2'] = DenseLayer(net['dropout1'],
                                self._net_specs_dict['num_fc_units'][1])
        # Add dropout layer
        net['dropout2'] = dropout(net['fc2'], p=self._model_hp_dict['p'])
        if bottleneck_W is not None:
            # Add bottleneck layer
            net['bottleneck'] = DenseLayer(net['dropout2'], 30)
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['bottleneck'],
                3 * self._num_joints,
                W=bottleneck_W[0:30],
                nonlinearity=lasagne.nonlinearities.tanh)
        else:
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['dropout2'],
                3 * self._num_joints,
                nonlinearity=lasagne.nonlinearities.tanh)
        return net
Ejemplo n.º 10
0
    return image


infilename = sys.argv[1]
outfilename = sys.argv[2]
if len(sys.argv) > 3:
    scale = int(sys.argv[3])
else:
    scale = 3

net = {}
# once a channel among 'R','G','B',
# if all channels are fed to laplacian conv at the same time,
# the output will be sum of the outputs from all input channels
net['img'] = InputLayer((None, 1, None, None))
net['laplacian'] = Conv2DLayer(net['img'], 1, 3, pad=1)
laplacian = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]], dtype=np.float32)
W = np.zeros((1, 1, 3, 3), dtype=np.float32)
W[0, 0] = laplacian
net['laplacian'].W.set_value(W)

orig_img = scipy.ndimage.imread(infilename, mode='RGB')
img = prepare_image(orig_img)

output = []
for i in xrange(3):
    img_chan = img[:, [i], :, :]
    tensor_input = {net['img']: img_chan}
    out_chan = lasagne.layers.get_output(net['laplacian'], tensor_input)
    output.append(out_chan.eval()[0, 0])
Ejemplo n.º 11
0
    def build_network(cls):
        net = {}
        # Input layer: shape is of the form (sample, channel, height, width).
        # We are using 3 channel images of size 224 x 224.
        # 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['input'] = InputLayer((None, 3, 224, 224))

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

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

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

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

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

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

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

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

        return net, 'prob'
Ejemplo n.º 12
0
def create_model(incoming, options):
    conv_num_filters1 = 100
    conv_num_filters2 = 150
    conv_num_filters3 = 200
    filter_size1 = 5
    filter_size2 = 5
    filter_size3 = 3
    pool_size = 2
    encode_size = options['BOTTLENECK']
    dense_mid_size = options['DENSE']
    pad_in = 'valid'
    pad_out = 'full'
    scaled_tanh = create_scaled_tanh()

    conv2d1 = Conv2DLayer(incoming,
                          num_filters=conv_num_filters1,
                          filter_size=filter_size1,
                          pad=pad_in,
                          name='conv2d1',
                          nonlinearity=scaled_tanh)
    maxpool2d2 = MaxPool2DLayer(conv2d1,
                                pool_size=pool_size,
                                name='maxpool2d2')
    conv2d3 = Conv2DLayer(maxpool2d2,
                          num_filters=conv_num_filters2,
                          filter_size=filter_size2,
                          pad=pad_in,
                          name='conv2d3',
                          nonlinearity=scaled_tanh)
    maxpool2d4 = MaxPool2DLayer(conv2d3,
                                pool_size=pool_size,
                                name='maxpool2d4',
                                pad=(1, 0))
    conv2d5 = Conv2DLayer(maxpool2d4,
                          num_filters=conv_num_filters3,
                          filter_size=filter_size3,
                          pad=pad_in,
                          name='conv2d5',
                          nonlinearity=scaled_tanh)
    reshape6 = ReshapeLayer(conv2d5, shape=([0], -1), name='reshape6')  # 3000
    reshape6_output = reshape6.output_shape[1]
    dense7 = DenseLayer(reshape6,
                        num_units=dense_mid_size,
                        name='dense7',
                        nonlinearity=scaled_tanh)
    bottleneck = DenseLayer(dense7,
                            num_units=encode_size,
                            name='bottleneck',
                            nonlinearity=linear)
    # print_network(bottleneck)
    dense8 = DenseLayer(bottleneck,
                        num_units=dense_mid_size,
                        W=bottleneck.W.T,
                        name='dense8',
                        nonlinearity=linear)
    dense9 = DenseLayer(dense8,
                        num_units=reshape6_output,
                        W=dense7.W.T,
                        nonlinearity=scaled_tanh,
                        name='dense9')
    reshape10 = ReshapeLayer(dense9,
                             shape=([0], conv_num_filters3, 3, 5),
                             name='reshape10')  # 32 x 4 x 7
    deconv2d11 = Deconv2DLayer(reshape10,
                               conv2d5.input_shape[1],
                               conv2d5.filter_size,
                               stride=conv2d5.stride,
                               W=conv2d5.W,
                               flip_filters=not conv2d5.flip_filters,
                               name='deconv2d11',
                               nonlinearity=scaled_tanh)
    upscale2d12 = Upscale2DLayer(deconv2d11,
                                 scale_factor=pool_size,
                                 name='upscale2d12')
    deconv2d13 = Deconv2DLayer(upscale2d12,
                               conv2d3.input_shape[1],
                               conv2d3.filter_size,
                               stride=conv2d3.stride,
                               W=conv2d3.W,
                               flip_filters=not conv2d3.flip_filters,
                               name='deconv2d13',
                               nonlinearity=scaled_tanh)
    upscale2d14 = Upscale2DLayer(deconv2d13,
                                 scale_factor=pool_size,
                                 name='upscale2d14')
    deconv2d15 = Deconv2DLayer(upscale2d14,
                               conv2d1.input_shape[1],
                               conv2d1.filter_size,
                               stride=conv2d1.stride,
                               crop=(1, 0),
                               W=conv2d1.W,
                               flip_filters=not conv2d1.flip_filters,
                               name='deconv2d14',
                               nonlinearity=scaled_tanh)
    reshape16 = ReshapeLayer(deconv2d15, ([0], -1), name='reshape16')
    return reshape16, bottleneck
Ejemplo n.º 13
0
def build_densenet(
        input_var,
        input_shape=(None, 3, 224, 224),
        num_filters_init=64,
        growth_rate=32,
        dropout=0.2,
        num_classes=1000,
        stages=[6, 12, 24, 16]):
    if input_shape[2] % (2 ** len(stages)) != 0:
        raise ValueError("input_shape[2] must be a multiple of {}.".format(2 ** len(stages)))

    if input_shape[3] % (2 ** len(stages)) != 0:
        raise ValueError("input_shape[3] must be a multiple of {}.".format(2 ** len(stages)))

        # Input should be (BATCH_SIZE, NUM_CHANNELS, WIDTH, HEIGHT)
    # NUM_CHANNELS is usually 3 (R,G,B) and for the ImageNet example the width and height are 224
    network = InputLayer(input_shape, input_var)

    # Apply 2D convolutions with a 7x7 filter (pad by 3 on each side)
    # Because of the 2x2 stride the shape of the last two dimensions will be half the size of the input (112x112)
    network = Conv2DLayer(network,
                          num_filters=num_filters_init,
                          filter_size=(7, 7),
                          stride=(2, 2),
                          pad=(3, 3),
                          W=HeNormal(gain='relu'), b=None,
                          flip_filters=False,
                          nonlinearity=None)

    # Batch normalize
    network = BatchNormLayer(network, beta=None, gamma=None)

    # If dropout is enabled, apply after every convolutional and dense layer
    if dropout > 0:
        network = DropoutLayer(network, p=dropout)

    # Apply ReLU
    network = NonlinearityLayer(network, nonlinearity=rectify)

    # Keep the maximum value of a 3x3 pool with a 2x2 stride
    # This operation again divides the size of the last two dimensions by two (56x56)
    network = MaxPool2DLayer(network,
                             pool_size=(3, 3),
                             stride=(2, 2),
                             pad=(1, 1))

    # Add dense blocks
    for i, num_layers in enumerate(stages):
        # Except for the first block, we add a transition layer before the dense block that halves the number of filters, width and height
        if i > 0:
            network = add_transition(network, math.floor(network.output_shape[1] / 2), dropout)
        network = build_block(network, num_layers, growth_rate, dropout)

    # Apply global pooling and add a fully connected layer with softmax function
    network = ScaleLayer(network)
    network = BiasLayer(network)
    network = NonlinearityLayer(network, nonlinearity=rectify)
    network = GlobalPoolLayer(network)
    network = DenseLayer(network,
                         num_units=num_classes,
                         W=HeNormal(gain=1),
                         nonlinearity=softmax)

    return network
def build_stereo_cnn(input_var=None):
    
    conv_num_filters1 = 16
    conv_num_filters2 = 32
    conv_num_filters3 = 64
    conv_num_filters4 = 128
    filter_size1 = 7
    filter_size2 = 5
    filter_size3 = 3
    filter_size4 = 3
    pool_size = 2
    scale_factor = 2
    pad_in = 'valid'
    pad_out = 'full'

    # Input layer, as usual:                                                                                                                                                                                
    network = InputLayer(shape=(None,2, X_train.shape[2], X_train.shape[3]),input_var=input_var,name="input_layer")                                                                                                                             
        
    network = batch_norm(Conv2DLayer(
            network, num_filters=conv_num_filters1, filter_size=(filter_size1, filter_size1),pad=pad_in,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="conv1"))
    
    network = MaxPool2DLayer(network, pool_size=(pool_size, pool_size),name="pool1")
    
    network = batch_norm(Conv2DLayer(
            network, num_filters=conv_num_filters2, filter_size=(filter_size2, filter_size2),pad=pad_in,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="conv2"))
    
    network = MaxPool2DLayer(network, pool_size=(pool_size, pool_size),name="pool2")
                                                                                                                                     
    network = batch_norm(Conv2DLayer(
            network, num_filters=conv_num_filters3, filter_size=(filter_size3, filter_size3),pad=pad_in,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="conv3"))
    
    network = MaxPool2DLayer(network, pool_size=(pool_size, pool_size),name="pool3")
                                                                                                                                     
    network = batch_norm(Conv2DLayer(
            network, num_filters=conv_num_filters4, filter_size=(filter_size4, filter_size4),pad=pad_in,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="conv4"))
    
    network = batch_norm(Conv2DLayer(
            network, num_filters=32, filter_size=(filter_size4, filter_size4),pad=pad_out,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="deconv1"))
    
    network = Upscale2DLayer(network, scale_factor=(pool_size, pool_size),name="upscale1")
    
    network = batch_norm(Conv2DLayer(
            network, num_filters=16, filter_size=(filter_size3, filter_size3),pad=pad_out,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="deconv2"))
    
    network = Upscale2DLayer(network, scale_factor=(pool_size, pool_size),name="upscale2")
    
    network = batch_norm(Conv2DLayer(
            network, num_filters=8, filter_size=(filter_size2, filter_size2),pad=pad_out,
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(),name="deconv3"))
    
    network = Upscale2DLayer(network, scale_factor=(pool_size, pool_size),name="upscale3")
    
    network = batch_norm(Conv2DLayer(
            network, num_filters=1, filter_size=(filter_size1, filter_size1),pad=pad_out,
            nonlinearity=lasagne.nonlinearities.sigmoid,
            W=lasagne.init.GlorotUniform(),name="deconv4"))
                                 
    return network
Ejemplo n.º 15
0
    def __init__(self,
                 n_in,
                 n_filters,
                 filter_sizes,
                 n_out,
                 pool_sizes=None,
                 n_hidden=(512),
                 ccf=False,
                 trans_func=rectify,
                 out_func=softmax,
                 dense_dropout=0.0,
                 stats=2,
                 input_noise=0.0,
                 batch_norm=False,
                 conv_dropout=0.0):
        super(CNN, self).__init__(n_in, n_hidden, n_out, trans_func)
        self.outf = out_func
        self.log = ""

        # Define model using lasagne framework
        dropout = True if not dense_dropout == 0.0 else False

        # Overwrite input layer
        sequence_length, n_features = n_in
        self.l_in = InputLayer(shape=(None, sequence_length, n_features))
        l_prev = self.l_in

        # Separate into raw values and statistics
        sequence_length -= stats
        stats_layer = SliceLayer(l_prev,
                                 indices=slice(sequence_length, None),
                                 axis=1)
        stats_layer = ReshapeLayer(stats_layer, (-1, stats * n_features))
        print('Stats layer shape', stats_layer.output_shape)
        l_prev = SliceLayer(l_prev, indices=slice(0, sequence_length), axis=1)
        print('Conv input layer shape', l_prev.output_shape)

        # Apply input noise
        l_prev = GaussianNoiseLayer(l_prev, sigma=input_noise)

        if ccf:
            self.log += "\nAdding cross-channel feature layer"
            l_prev = ReshapeLayer(l_prev, (-1, 1, sequence_length, n_features))
            l_prev = Conv2DLayer(l_prev,
                                 num_filters=4 * n_features,
                                 filter_size=(1, n_features),
                                 nonlinearity=None)
            n_features *= 4
            if batch_norm:
                l_prev = batch_norm_layer(l_prev)
            l_prev = ReshapeLayer(l_prev, (-1, n_features, sequence_length))
            l_prev = DimshuffleLayer(l_prev, (0, 2, 1))

        # 2D Convolutional layers
        l_prev = ReshapeLayer(l_prev, (-1, 1, sequence_length, n_features))
        l_prev = DimshuffleLayer(l_prev, (0, 3, 2, 1))

        # Add the convolutional filters
        for n_filter, filter_size, pool_size in zip(n_filters, filter_sizes,
                                                    pool_sizes):
            self.log += "\nAdding 2D conv layer: %d x %d" % (n_filter,
                                                             filter_size)
            l_prev = Conv2DLayer(l_prev,
                                 num_filters=n_filter,
                                 filter_size=(filter_size, 1),
                                 nonlinearity=self.transf,
                                 pad=filter_size // 2)
            if batch_norm:
                l_prev = batch_norm_layer(l_prev)
            if pool_size > 1:
                self.log += "\nAdding max pooling layer: %d" % pool_size
                l_prev = Pool2DLayer(l_prev, pool_size=(pool_size, 1))
            self.log += "\nAdding dropout layer: %.2f" % conv_dropout
            l_prev = TiedDropoutLayer(l_prev, p=conv_dropout)
            print("Conv out shape", get_output_shape(l_prev))

        # Global pooling layer
        l_prev = GlobalPoolLayer(l_prev,
                                 pool_function=T.mean,
                                 name='Global Mean Pool')
        print("GlobalPoolLayer out shape", get_output_shape(l_prev))

        # Concatenate stats
        l_prev = ConcatLayer((l_prev, stats_layer), axis=1)

        for n_hid in n_hidden:
            self.log += "\nAdding dense layer with %d units" % n_hid
            print("Dense input shape", get_output_shape(l_prev))
            l_prev = DenseLayer(l_prev, n_hid, init.GlorotNormal(),
                                init.Normal(1e-3), self.transf)
            if batch_norm:
                l_prev = batch_norm_layer(l_prev)
            if dropout:
                self.log += "\nAdding dense dropout with probability: %.2f" % dense_dropout
                l_prev = DropoutLayer(l_prev, p=dense_dropout)

        if batch_norm:
            self.log += "\nUsing batch normalization"

        self.model = DenseLayer(l_prev, num_units=n_out, nonlinearity=out_func)
        self.model_params = get_all_params(self.model)

        self.sym_x = T.tensor3('x')
        self.sym_t = T.matrix('t')
def conv_pool_up(net, no_f_base,f_size,conv_depth,pad,nonlinearity,halt=False):
    for i in xrange(conv_depth):
        net = Conv2DLayer(net,no_f_base,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    if not halt:
        net = Deconv2DLayer(net,no_f_base/2,2,2)
    return net
def construct_vgg16net(channels=1, no_f_base=64, f_size=3, dropout=False, bs=None, num_classes=3, pad=1,c_nonlinearity=lasagne.nonlinearities.rectify,f_nonlinearity=lasagne.nonlinearities.tanh, input_dim=[512,512], flip=False, fcn=4096):
    net = {}
    net["input"] = InputLayer(shape=(bs, channels, input_dim[0], input_dim[1]))
    net["conv_11"] = Conv2DLayer(net["input"], no_f_base, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_12"] = Conv2DLayer(net["conv_11"], no_f_base, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool1"] = Pool2DLayer(net["conv_12"], 2)
    net["conv_21"] = Conv2DLayer(net["pool1"], no_f_base*2, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_22"] = Conv2DLayer(net["conv_21"], no_f_base*2, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool2"] = Pool2DLayer(net["conv_22"], 2)
    net["conv_31"] = Conv2DLayer(net["pool2"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_32"] = Conv2DLayer(net["conv_31"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_33"] = Conv2DLayer(net["conv_32"], no_f_base*4, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool3"] = Pool2DLayer(net["conv_33"], 2)
    net["conv_41"] = Conv2DLayer(net["pool3"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_42"] = Conv2DLayer(net["conv_41"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_43"] = Conv2DLayer(net["conv_42"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool4"] = Pool2DLayer(net["conv_43"], 2)
    net["conv_51"] = Conv2DLayer(net["pool4"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_52"] = Conv2DLayer(net["conv_51"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["conv_53"] = Conv2DLayer(net["conv_52"], no_f_base*8, f_size, pad=pad, flip_filters=flip, nonlinearity=c_nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    net["pool5"] = Pool2DLayer(net["conv_53"], 2)

    net["full_con1"] = DenseLayer(net["pool5"], num_units=fcn, nonlinearity=f_nonlinearity)
    net["drop_full_con1"] = DropoutLayer(net["full_con1"], p=0.5)
    net["full_con2"] = DenseLayer(net["drop_full_con1"], num_units=fcn, nonlinearity=f_nonlinearity)
    net["drop_full_con2"] = DropoutLayer(net["full_con2"], p=0.5)
    net["full_con3"] = DenseLayer(net["drop_full_con2"], num_units=num_classes, nonlinearity=None)
    net["out"] = NonlinearityLayer(net["full_con3"], nonlinearity=lasagne.nonlinearities.softmax)
    return net
    del net
Ejemplo n.º 18
0
    def dense_fused_convnets(self,
                             fusion_level,
                             fusion_type,
                             input_var1=None,
                             input_var2=None,
                             bottleneck_W=None,
                             weights_dir=None):

        net = OrderedDict()
        net['input_rgb'] = InputLayer((None, 4, 128, 128),
                                      input_var=input_var1)
        layer = 0
        for i in range(self._net_specs_dict['num_conv_layers']):
            # Add convolution layers
            net['conv_rgb{0:d}'.format(i + 1)] = Conv2DLayer(
                net.values()[layer],
                num_filters=self._net_specs_dict['num_conv_filters'][i],
                filter_size=(self._net_specs_dict['conv_filter_size'][i], ) *
                2,
                pad='same')
            layer += 1
            if self._net_specs_dict['num_conv_layers'] <= 2:
                # Add pooling layers
                net['pool_rgb{0:d}'.format(i + 1)] = MaxPool2DLayer(
                    net.values()[layer], pool_size=(3, 3))
                layer += 1
            else:
                if i < 4:
                    if (i + 1) % 2 == 0:
                        # Add pooling layers
                        net['pool_rgb{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1
                else:
                    if (i + 1) == 7:
                        # Add pooling layers
                        net['pool_rgb{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1
        # Fc-layers
        net['fc1_rgb'] = DenseLayer(net.values()[layer],
                                    self._net_specs_dict['num_fc_units'][0])
        layer += 1
        if fusion_level == 2:
            # Add dropout layer
            net['dropout1_rgb'] = dropout(net['fc1_rgb'],
                                          p=self._model_hp_dict['p'])
            layer += 1
            net['fc2_rgb'] = DenseLayer(
                net['dropout1_rgb'], self._net_specs_dict['num_fc_units'][1])
            layer += 1

        net['input_depth'] = InputLayer((None, 1, 128, 128),
                                        input_var=input_var2)
        layer += 1
        for i in range(self._net_specs_dict['num_conv_layers']):
            # Add convolution layers
            net['conv_depth{0:d}'.format(i + 1)] = Conv2DLayer(
                net.values()[layer],
                num_filters=self._net_specs_dict['num_conv_filters'][i],
                filter_size=(self._net_specs_dict['conv_filter_size'][i], ) *
                2,
                pad='same')
            layer += 1
            if self._net_specs_dict['num_conv_layers'] <= 2:
                # Add pooling layers
                net['pool_depth{0:d}'.format(i + 1)] = MaxPool2DLayer(
                    net.values()[layer], pool_size=(3, 3))
                layer += 1
            else:
                if i < 4:
                    if (i + 1) % 2 == 0:
                        # Add pooling layers
                        net['pool_depth{0:d}'.format(i+1)] =\
                            MaxPool2DLayer(net.values()[layer],
                                           pool_size=(3, 3))
                        layer += 1
                else:
                    if (i + 1) == 7:
                        # Add pooling layers
                        net['pool_depth{0:d}'.format(i+1)] =\
                            MaxPool2DLayer(net.values()[layer],
                                           pool_size=(3, 3))
                        layer += 1
        # Fc-layers
        net['fc1_depth'] = DenseLayer(net.values()[layer],
                                      self._net_specs_dict['num_fc_units'][0])
        layer += 1
        if fusion_level == 2:
            # Add dropout layer
            net['dropout1_depth'] = dropout(net['fc1_depth'],
                                            p=self._model_hp_dict['p'])
            layer += 1
            net['fc2_depth'] = DenseLayer(
                net['dropout1_depth'], self._net_specs_dict['num_fc_units'][1])
            layer += 1

        # Fuse ConvNets by fusion_level and fusion_type
        if fusion_type == self.MAX:
            net['merge'] =\
                ElemwiseMergeLayer([net['fc%i_rgb' % fusion_level],
                                    net['fc%i_depth' % fusion_level]],
                                   T.maximum)
            layer += 1
        elif fusion_type == self.SUM:
            net['merge'] =\
                ElemwiseMergeLayer([net['fc%i_rgb' % fusion_level],
                                    net['fc%i_depth' % fusion_level]],
                                   T.add)
            layer += 1
        elif fusion_type == self.CONCAT:
            net['merge'] = concat([
                net['fc%i_rgb' % fusion_level],
                net['fc%i_depth' % fusion_level]
            ])
            layer += 1
        elif fusion_type == self.CONCATCONV:
            net['fc%i_rgb_res' % fusion_level] =\
                reshape(net['fc%i_rgb' % fusion_level], ([0], 1, [1]))
            layer += 1
            net['fc%i_depth_res' % fusion_level] =\
                reshape(net['fc%i_depth' % fusion_level], ([0], 1, [1]))
            layer += 1
            net['concat'] = concat([
                net['fc%i_rgb_res' % fusion_level],
                net['fc%i_depth_res' % fusion_level]
            ])
            layer += 1
            net['merge_con'] = Conv1DLayer(net['concat'],
                                           num_filters=1,
                                           filter_size=(1, ),
                                           nonlinearity=None)
            layer += 1
            net['merge'] = reshape(net['merge_con'], ([0], [2]))
            layer += 1

        if fusion_level == 1:
            # Add dropout layer
            net['dropout1'] = dropout(net['merge'], p=self._model_hp_dict['p'])
            layer += 1
            net['fc2'] = DenseLayer(net['dropout1'],
                                    self._net_specs_dict['num_fc_units'][1])
            layer += 1
            # Add dropout layer
            net['dropout2'] = dropout(net['fc2'], p=self._model_hp_dict['p'])
            layer += 1
        else:
            # Add dropout layer
            net['dropout2'] = dropout(net['merge'], p=self._model_hp_dict['p'])
            layer += 1
        # Add output layer(linear activation because it's regression)
        if bottleneck_W is not None:
            # Add bottleneck layer
            net['bottleneck'] = DenseLayer(net['dropout2'], 30)
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['bottleneck'],
                3 * self._num_joints,
                W=bottleneck_W[0:30],
                nonlinearity=lasagne.nonlinearities.tanh)
        else:
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['dropout2'],
                3 * self._num_joints,
                nonlinearity=lasagne.nonlinearities.tanh)
        if weights_dir is not None:
            lw = LoadWeights(weights_dir, net)
            lw.load_weights_numpy()
        return net
Ejemplo n.º 19
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.º 20
0
    def simple_convnet(self,
                       input_channels,
                       input_var=None,
                       bottleneck_W=None):
        """
        This is a classical convnet. It contains convolution and
        fully-connected(fc) layers.

        Keyword arguments:
        input_var -- theano variable that specifies the type and dimension of
        the input(default None)

        Return:
        net -- dictionary that contains all the network layers
        """
        net = OrderedDict()
        net['input'] = InputLayer((None, input_channels, 128, 128),
                                  input_var=input_var)
        layer = 0
        for i in range(self._net_specs_dict['num_conv_layers']):
            # Add convolution layers
            net['conv{0:d}'.format(i + 1)] = Conv2DLayer(
                net.values()[layer],
                num_filters=self._net_specs_dict['num_conv_filters'][i],
                filter_size=(self._net_specs_dict['conv_filter_size'][i], ) *
                2,
                pad='same')
            layer += 1
            if self._net_specs_dict['num_conv_layers'] <= 2:
                # Add pooling layers
                net['pool{0:d}'.format(i + 1)] = MaxPool2DLayer(
                    net.values()[layer], pool_size=(3, 3))
                layer += 1
            else:
                if i < 4:
                    if (i + 1) % 2 == 0:
                        # Add pooling layers
                        net['pool{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1
                else:
                    if (i + 1) == 7:
                        # Add pooling layers
                        net['pool{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1

        # Add fc-layers
        net['fc1'] = DenseLayer(net.values()[layer],
                                self._net_specs_dict['num_fc_units'][0])
        # Add dropout layer
        net['dropout1'] = dropout(net['fc1'], p=self._model_hp_dict['p'])
        net['fc2'] = DenseLayer(net['dropout1'],
                                self._net_specs_dict['num_fc_units'][1])
        # Add dropout layer
        net['dropout2'] = dropout(net['fc2'], p=self._model_hp_dict['p'])
        if bottleneck_W is not None:
            # Add bottleneck layer
            net['bottleneck'] = DenseLayer(net['dropout2'], 30)
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['bottleneck'],
                3 * self._num_joints,
                W=bottleneck_W[0:30],
                nonlinearity=lasagne.nonlinearities.tanh)
        else:
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['dropout2'],
                3 * self._num_joints,
                nonlinearity=lasagne.nonlinearities.tanh)
        return net
Ejemplo n.º 21
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.º 22
0
    def fused_convnets(self,
                       fusion_level,
                       fusion_type,
                       input_var1=None,
                       input_var2=None,
                       bottleneck_W=None,
                       weights_dir=None):

        net = OrderedDict()
        net['input_rgb'] = InputLayer((None, 4, 128, 128),
                                      input_var=input_var1)
        layer = 0
        for i in range(fusion_level):
            # Add convolution layers
            net['conv_rgb{0:d}'.format(i + 1)] = Conv2DLayer(
                net.values()[layer],
                num_filters=self._net_specs_dict['num_conv_filters'][i],
                filter_size=(self._net_specs_dict['conv_filter_size'][i], ) *
                2,
                pad='same')
            layer += 1
            if self._net_specs_dict['num_conv_layers'] <= 2 and\
                    i != fusion_level - 1:
                # Add pooling layers
                net['pool_rgb{0:d}'.format(i + 1)] = MaxPool2DLayer(
                    net.values()[layer], pool_size=(3, 3))
                layer += 1
            else:
                if i < 4:
                    if (i + 1) % 2 == 0 and i != fusion_level - 1:
                        # Add pooling layers
                        net['pool_rgb{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1
                else:
                    if (i + 1) == 7 and i != fusion_level - 1:
                        # Add pooling layers
                        net['pool_rgb{0:d}'.format(i + 1)] = MaxPool2DLayer(
                            net.values()[layer], pool_size=(3, 3))
                        layer += 1

        net['input_depth'] = InputLayer((None, 1, 128, 128),
                                        input_var=input_var2)
        layer += 1
        for i in range(fusion_level):
            # Add convolution layers
            net['conv_depth{0:d}'.format(i + 1)] = Conv2DLayer(
                net.values()[layer],
                num_filters=self._net_specs_dict['num_conv_filters'][i],
                filter_size=(self._net_specs_dict['conv_filter_size'][i], ) *
                2,
                pad='same')
            layer += 1
            if self._net_specs_dict['num_conv_layers'] <= 2 and\
                    i != fusion_level - 1:
                # Add pooling layers
                net['pool_depth{0:d}'.format(i + 1)] = MaxPool2DLayer(
                    net.values()[layer], pool_size=(3, 3))
                layer += 1
            else:
                if i < 4:
                    if (i + 1) % 2 == 0 and i != fusion_level - 1:
                        # Add pooling layers
                        net['pool_depth{0:d}'.format(i+1)] =\
                            MaxPool2DLayer(net.values()[layer],
                                           pool_size=(3, 3))
                        layer += 1
                else:
                    if (i + 1) == 7 and i != fusion_level - 1:
                        # Add pooling layers
                        net['pool_depth{0:d}'.format(i+1)] =\
                            MaxPool2DLayer(net.values()[layer],
                                           pool_size=(3, 3))
                        layer += 1
        # Fuse ConvNets by fusion_level and fusion_type
        if fusion_type == self.MAX:
            net['merge'] =\
                ElemwiseMergeLayer([net['conv_rgb{0:d}'.format(fusion_level)],
                                    net['conv_depth{0:d}'.format(fusion_level)]
                                    ], T.maximum)
            layer += 1
        elif fusion_type == self.SUM:
            net['merge'] =\
                ElemwiseMergeLayer([net['conv_rgb{0:d}'.format(fusion_level)],
                                    net['conv_depth{0:d}'.format(fusion_level)]
                                    ], T.add)
            layer += 1
        elif fusion_type == self.CONCAT:
            net['merge'] = concat([
                net['conv_rgb{0:d}'.format(fusion_level)],
                net['conv_depth{0:d}'.format(fusion_level)]
            ])
            layer += 1
        elif fusion_type == self.CONCATCONV:
            net['concat'] = concat([
                net['conv_rgb{0:d}'.format(fusion_level)],
                net['conv_depth{0:d}'.format(fusion_level)]
            ])
            layer += 1
            net['merge'] = Conv2DLayer(
                net['concat'],
                num_filters=self._net_specs_dict['num_conv_filters'][
                    fusion_level - 1],
                filter_size=(1, 1),
                nonlinearity=None)
            layer += 1
        # Max-pooling to the merged
        if fusion_level in [2, 4, 7]:
            net['pool_merged'] = MaxPool2DLayer(net['merge'], pool_size=(3, 3))
            layer += 1
        # Continue the rest of the convolutional part of the network,
        # if the fusion took place before the last convolutional layer,
        # else just connect the convolutional part with the fully connected
        # part
        if self._net_specs_dict['num_conv_layers'] > fusion_level:
            for i in range(fusion_level,
                           self._net_specs_dict['num_conv_layers']):
                # Add convolution layers
                net['conv_merged{0:d}'.format(i + 1)] = Conv2DLayer(
                    net.values()[layer],
                    num_filters=self._net_specs_dict['num_conv_filters'][i],
                    filter_size=(self._net_specs_dict['conv_filter_size'][i], )
                    * 2,
                    pad='same')
                layer += 1
                if self._net_specs_dict['num_conv_layers'] <= 2:
                    # Add pooling layers
                    net['pool_merged{0:d}'.format(i + 1)] = MaxPool2DLayer(
                        net.values()[layer], pool_size=(3, 3))
                    layer += 1
                else:
                    if i < 4:
                        if (i + 1) % 2 == 0:
                            # Add pooling layers
                            net['pool_merged{0:d}'.format(i+1)] =\
                                MaxPool2DLayer(net.values()[layer],
                                               pool_size=(3, 3))
                            layer += 1
                    else:
                        if (i + 1) == 7:
                            # Add pooling layers
                            net['pool_merged{0:d}'.format(i+1)] =\
                                MaxPool2DLayer(net.values()[layer],
                                               pool_size=(3, 3))
                            layer += 1
        # Fc-layers
        net['fc1'] = DenseLayer(net.values()[layer],
                                self._net_specs_dict['num_fc_units'][0])
        # Add dropout layer
        net['dropout1'] = dropout(net['fc1'], p=self._model_hp_dict['p'])
        net['fc2'] = DenseLayer(net['dropout1'],
                                self._net_specs_dict['num_fc_units'][1])
        # Add dropout layer
        net['dropout2'] = dropout(net['fc2'], p=self._model_hp_dict['p'])
        if bottleneck_W is not None:
            # Add bottleneck layer
            net['bottleneck'] = DenseLayer(net['dropout2'], 30)
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['bottleneck'],
                3 * self._num_joints,
                W=bottleneck_W[0:30],
                nonlinearity=lasagne.nonlinearities.tanh)
        else:
            # Add output layer(linear activation because it's regression)
            net['output'] = DenseLayer(
                net['dropout2'],
                3 * self._num_joints,
                nonlinearity=lasagne.nonlinearities.tanh)
        if weights_dir is not None:
            lw = LoadWeights(weights_dir, net)
            lw.load_weights_numpy()
        return net
Ejemplo n.º 23
0
def Decoder(latent_var, use_batch_norm=False):
    net = InputLayer(shape=(None, 20), input_var=latent_var)

    net = DenseLayer(net,
                     num_units=64,
                     nonlinearity=lasagne.nonlinearities.elu)

    net = DenseLayer(net,
                     num_units=64 * 16,
                     nonlinearity=lasagne.nonlinearities.elu)

    if use_batch_norm:
        net = BatchNormLayer(net)

    net = ReshapeLayer(net, (-1, 16, 8, 8))

    net = Conv2DLayer(net,
                      num_filters=32,
                      filter_size=(3, 3),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=32,
                      filter_size=(3, 3),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = TransposedConv2DLayer(net,
                                8,
                                4,
                                stride=(2, 2),
                                nonlinearity=lasagne.nonlinearities.elu)
    net = Conv2DLayer(net,
                      num_filters=32,
                      filter_size=(3, 3),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = TransposedConv2DLayer(net,
                                8,
                                4,
                                stride=(2, 2),
                                nonlinearity=lasagne.nonlinearities.elu)
    net = Conv2DLayer(net,
                      num_filters=32,
                      filter_size=(3, 3),
                      nonlinearity=lasagne.nonlinearities.elu)
    net = TransposedConv2DLayer(net,
                                8,
                                4,
                                stride=(2, 2),
                                nonlinearity=lasagne.nonlinearities.elu)
    net = Conv2DLayer(net,
                      num_filters=32,
                      filter_size=(5, 5),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=32,
                      filter_size=(3, 3),
                      nonlinearity=lasagne.nonlinearities.elu)

    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=8,
                      filter_size=(1, 1),
                      nonlinearity=lasagne.nonlinearities.elu)
    if use_batch_norm:
        net = BatchNormLayer(net)

    net = Conv2DLayer(net,
                      num_filters=1,
                      filter_size=(1, 1),
                      nonlinearity=lasagne.nonlinearities.sigmoid)

    return net
def bn_conv(input_layer, **kwargs):
    l = Conv2DLayer(input_layer, **kwargs)
    l = batch_norm(l, epsilon=0.001)
    return l
Ejemplo n.º 25
0
def reference_model():
    net = {}
    net['data'] = InputLayer(shape=(None, 3, 227, 227))

    # conv1
    net['conv1'] = Conv2DLayer(
        net['data'],
        num_filters=96,
        filter_size=(11, 11),
        stride = 4,
        nonlinearity=lasagne.nonlinearities.rectify)

    
    # pool1
    net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(3, 3), stride=2)

    # norm1
    net['norm1'] = LocalResponseNormalization2DLayer(net['pool1'],
                                                     n=5,
                                                     alpha=0.0001/5.0,
                                                     beta = 0.75,
                                                     k=1)

    # conv2
    # The caffe reference model uses a parameter called group.
    # This parameter splits input to the convolutional layer.
    # The first half of the filters operate on the first half
    # of the input from the previous layer. Similarly, the
    # second half operate on the second half of the input.
    #
    # Lasagne does not have this group parameter, but we can
    # do it ourselves.
    #
    # see https://github.com/BVLC/caffe/issues/778
    # also see https://code.google.com/p/cuda-convnet/wiki/LayerParams
    
    # before conv2 split the data
    net['conv2_data1'] = SliceLayer(net['norm1'], indices=slice(0, 48), axis=1)
    net['conv2_data2'] = SliceLayer(net['norm1'], indices=slice(48,96), axis=1)

    # now do the convolutions
    net['conv2_part1'] = Conv2DLayer(net['conv2_data1'],
                                     num_filters=128,
                                     filter_size=(5, 5),
                                     pad = 2)
    net['conv2_part2'] = Conv2DLayer(net['conv2_data2'],
                                     num_filters=128,
                                     filter_size=(5,5),
                                     pad = 2)

    # now combine
    net['conv2'] = concat((net['conv2_part1'],net['conv2_part2']),axis=1)
    
    # pool2
    net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(3, 3), stride = 2)
    
    # norm2
    net['norm2'] = LocalResponseNormalization2DLayer(net['pool2'],
                                                     n=5,
                                                     alpha=0.0001/5.0,
                                                     beta = 0.75,
                                                     k=1)
    
    # conv3
    # no group
    net['conv3'] = Conv2DLayer(net['norm2'],
                               num_filters=384,
                               filter_size=(3, 3),
                               pad = 1)

    # conv4
    # group = 2
    net['conv4_data1'] = SliceLayer(net['conv3'], indices=slice(0, 192), axis=1)
    net['conv4_data2'] = SliceLayer(net['conv3'], indices=slice(192,384), axis=1)
    net['conv4_part1'] = Conv2DLayer(net['conv4_data1'],
                                     num_filters=192,
                                     filter_size=(3, 3),
                                     pad = 1)
    net['conv4_part2'] = Conv2DLayer(net['conv4_data2'],
                                     num_filters=192,
                                     filter_size=(3,3),
                                     pad = 1)
    net['conv4'] = concat((net['conv4_part1'],net['conv4_part2']),axis=1)
    
    # conv5
    # group 2
    net['conv5_data1'] = SliceLayer(net['conv4'], indices=slice(0, 192), axis=1)
    net['conv5_data2'] = SliceLayer(net['conv4'], indices=slice(192,384), axis=1)
    net['conv5_part1'] = Conv2DLayer(net['conv5_data1'],
                                     num_filters=128,
                                     filter_size=(3, 3),
                                     pad = 1)
    net['conv5_part2'] = Conv2DLayer(net['conv5_data2'],
                                     num_filters=128,
                                     filter_size=(3,3),
                                     pad = 1)
    net['conv5'] = concat((net['conv5_part1'],net['conv5_part2']),axis=1)
    
    # pool 5
    net['pool5'] = MaxPool2DLayer(net['conv5'], pool_size=(3, 3), stride = 2)

    # fc6
    net['fc6'] = DenseLayer(
            net['pool5'],num_units=4096,
            nonlinearity=lasagne.nonlinearities.rectify)

    # fc7
    net['fc7'] = DenseLayer(
        net['fc6'],
        num_units=4096,
        nonlinearity=lasagne.nonlinearities.rectify)

    # fc8
    net['fc8'] = DenseLayer(
        net['fc7'],
        num_units=1000,
        nonlinearity=lasagne.nonlinearities.softmax)
    
    return net
Ejemplo n.º 26
0
def build_fcn_segmenter(input_var, shape, version=2):
    ret = {}

    if version == 2:
        ret['input'] = la = InputLayer(shape, input_var)
        ret['conv%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=8, filter_size=7))
        ret['conv%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=16, filter_size=3))
        ret['pool%d' % len(ret)] = la = MaxPool2DLayer(la, pool_size=2)
        ret['conv%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=32, filter_size=3))
        ret['pool%d' % len(ret)] = la = MaxPool2DLayer(la, pool_size=2)
        ret['conv%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=64, filter_size=3))
        ret['pool%d' % len(ret)] = la = MaxPool2DLayer(la, pool_size=2)
        ret['conv%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=64, filter_size=3))
        ret['dec%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=64, filter_size=3, pad='full'))
        ret['ups%d' % len(ret)] = la = Upscale2DLayer(la, scale_factor=2)
        ret['dec%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=64, filter_size=3, pad='full'))
        ret['ups%d' % len(ret)] = la = Upscale2DLayer(la, scale_factor=2)
        ret['dec%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=32, filter_size=7, pad='full'))
        ret['ups%d' % len(ret)] = la = Upscale2DLayer(la, scale_factor=2)
        ret['dec%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=16, filter_size=3, pad='full'))
        ret['conv%d' % len(ret)] = la = bn(
            Conv2DLayer(la, num_filters=8, filter_size=7))
        ret['output'] = la = Conv2DLayer(
            la,
            num_filters=1,
            filter_size=7,
            pad='full',
            nonlinearity=nn.nonlinearities.sigmoid)

    return ret, nn.layers.get_output(ret['output']), \
            nn.layers.get_output(ret['output'], deterministic=True)
Ejemplo n.º 27
0
def build_model(x=None, layer='fc8', shape=(None, 3, 227, 227), up_scale=4):
    net = {'data': InputLayer(shape=shape, input_var=x)}
    net['data_s'] = Upscale2DLayer(net['data'], up_scale)
    net['conv1'] = Conv2DLayer(net['data_s'],
                               num_filters=96,
                               filter_size=(11, 11),
                               stride=4,
                               nonlinearity=lasagne.nonlinearities.rectify)

    if layer is 'conv1':
        return net

    # pool1
    net['pool1'] = MaxPool2DLayer(net['conv1'], pool_size=(3, 3), stride=2)

    # norm1
    net['norm1'] = LocalResponseNormalization2DLayer(net['pool1'],
                                                     n=5,
                                                     alpha=0.0001 / 5.0,
                                                     beta=0.75,
                                                     k=1)

    # conv2
    # before conv2 split the data
    net['conv2_data1'] = SliceLayer(net['norm1'], indices=slice(0, 48), axis=1)
    net['conv2_data2'] = SliceLayer(net['norm1'],
                                    indices=slice(48, 96),
                                    axis=1)

    # now do the convolutions
    net['conv2_part1'] = Conv2DLayer(net['conv2_data1'],
                                     num_filters=128,
                                     filter_size=(5, 5),
                                     pad=2)
    net['conv2_part2'] = Conv2DLayer(net['conv2_data2'],
                                     num_filters=128,
                                     filter_size=(5, 5),
                                     pad=2)

    # now combine
    net['conv2'] = concat((net['conv2_part1'], net['conv2_part2']), axis=1)
    if layer is 'conv2':
        return net
    # pool2
    net['pool2'] = MaxPool2DLayer(net['conv2'], pool_size=(3, 3), stride=2)

    # norm2
    net['norm2'] = LocalResponseNormalization2DLayer(net['pool2'],
                                                     n=5,
                                                     alpha=0.0001 / 5.0,
                                                     beta=0.75,
                                                     k=1)
    # conv3
    # no group
    net['conv3'] = Conv2DLayer(net['norm2'],
                               num_filters=384,
                               filter_size=(3, 3),
                               pad=1)
    if layer is 'conv3':
        return net

    # conv4
    net['conv4_data1'] = SliceLayer(net['conv3'],
                                    indices=slice(0, 192),
                                    axis=1)
    net['conv4_data2'] = SliceLayer(net['conv3'],
                                    indices=slice(192, 384),
                                    axis=1)
    net['conv4_part1'] = Conv2DLayer(net['conv4_data1'],
                                     num_filters=192,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv4_part2'] = Conv2DLayer(net['conv4_data2'],
                                     num_filters=192,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv4'] = concat((net['conv4_part1'], net['conv4_part2']), axis=1)
    if layer is 'conv4':
        return net

    # conv5
    # group 2
    net['conv5_data1'] = SliceLayer(net['conv4'],
                                    indices=slice(0, 192),
                                    axis=1)
    net['conv5_data2'] = SliceLayer(net['conv4'],
                                    indices=slice(192, 384),
                                    axis=1)
    net['conv5_part1'] = Conv2DLayer(net['conv5_data1'],
                                     num_filters=128,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv5_part2'] = Conv2DLayer(net['conv5_data2'],
                                     num_filters=128,
                                     filter_size=(3, 3),
                                     pad=1)
    net['conv5'] = concat((net['conv5_part1'], net['conv5_part2']), axis=1)
    if layer is 'conv5':
        return net

    # pool 5
    net['pool5'] = MaxPool2DLayer(net['conv5'], pool_size=(3, 3), stride=2)

    # fc6
    net['fc6'] = DenseLayer(net['pool5'],
                            num_units=4096,
                            nonlinearity=lasagne.nonlinearities.rectify)
    if layer is 'fc6':
        return net

    # fc7
    net['fc7'] = DenseLayer(net['fc6'],
                            num_units=4096,
                            nonlinearity=lasagne.nonlinearities.rectify)
    if layer is 'fc7':
        return net

    # fc8
    net['fc8'] = DenseLayer(net['fc7'],
                            num_units=1000,
                            nonlinearity=lasagne.nonlinearities.softmax)
    if layer is 'fc8':
        # st()
        return net
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
    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
def conv_pool_down(net, no_f_base,f_size,conv_depth,pad,nonlinearity,dropout):
    for i in xrange(conv_depth):
        net = Conv2DLayer(net,no_f_base,f_size,pad=pad,nonlinearity=nonlinearity,W=lasagne.init.HeNormal(gain='relu'))
    if dropout:
        net = DropoutLayer(net,p=dropout)
    return net