def InceptionUpscaleLayer(incoming, param_dict, block_name): branch = [0] * len(param_dict) # Loop across branches for i, dict in enumerate(param_dict): for j, style in enumerate(dict['style']): # Loop up branch branch[i] = TC2D( incoming = branch[i] if j else incoming, num_filters = dict['num_filters'][j], filter_size = dict['filter_size'][j], crop = dict['pad'][j] if 'pad' in dict else None, stride = dict['stride'][j], W = initmethod('relu'), nonlinearity = dict['nonlinearity'][j], name = block_name+'_'+str(i)+'_'+str(j)) if style=='convolutional'\ else NL( incoming = lasagne.layers.dnn.Pool2DDNNLayer( incoming = lasagne.layers.Upscale2DLayer( incoming=incoming if j == 0 else branch[i], scale_factor = dict['stride'][j]), pool_size = dict['filter_size'][j], stride = [1,1], mode = dict['mode'][j], pad = dict['pad'][j], name = block_name+'_'+str(i)+'_'+str(j)), nonlinearity = dict['nonlinearity'][j]) # Apply Batchnorm branch[i] = BN(branch[i], name=block_name + '_bnorm_' + str(i) + '_' + str(j)) if dict['bnorm'][j] else branch[i] # Concatenate Sublayers return CL(incomings=branch, name=block_name)
def get_model(dnn=True): if dnn: import lasagne.layers.dnn from lasagne.layers.dnn import Conv2DDNNLayer as C2D from theano.sandbox.cuda.basic_ops import (as_cuda_ndarray_variable, host_from_gpu, gpu_contiguous, HostFromGpu, gpu_alloc_empty) from theano.sandbox.cuda.dnn import GpuDnnConvDesc, GpuDnnConv, GpuDnnConvGradI, dnn_conv, dnn_pool from layers import DeconvLayer else: import lasagne.layers from lasagne.layers import Conv2DLayer as C2D dims, n_channels, n_classes = tuple(cfg['dims']), cfg['n_channels'], cfg['n_classes'] shape = (None, n_channels)+dims l_in = lasagne.layers.InputLayer(shape=shape) l_enc_conv1 = C2D( incoming = l_in, num_filters = 128, filter_size = [5,5], stride = [2,2], pad = (2,2), W = initmethod(0.02), nonlinearity = lrelu(0.2), flip_filters=False, name = 'enc_conv1' ) l_enc_conv2 = BN(C2D( incoming = l_enc_conv1, num_filters = 256, filter_size = [5,5], stride = [2,2], pad = (2,2), W = initmethod(0.02), nonlinearity = lrelu(0.2), flip_filters=False, name = 'enc_conv2' ),name = 'bnorm2') l_enc_conv3 = BN(C2D( incoming = l_enc_conv2, num_filters = 512, filter_size = [5,5], stride = [2,2], pad = (2,2), W = initmethod(0.02), nonlinearity = lrelu(0.2), flip_filters=False, name = 'enc_conv3' ),name = 'bnorm3') l_enc_conv4 = BN(C2D( incoming = l_enc_conv3, num_filters = 1024, filter_size = [5,5], stride = [2,2], pad = (2,2), W = initmethod(0.02), nonlinearity = lrelu(0.2), flip_filters=False, name = 'enc_conv4' ),name = 'bnorm4') l_enc_fc1 = BN(DL( incoming = l_enc_conv4, num_units = 1000, W = initmethod(0.02), nonlinearity = elu, name = 'enc_fc1' ), name = 'bnorm_enc_fc1') l_enc_mu,l_enc_logsigma = [BN(DL(incoming = l_enc_fc1,num_units=cfg['num_latents'],nonlinearity = None,name='enc_mu'),name='mu_bnorm'), BN(DL(incoming = l_enc_fc1,num_units=cfg['num_latents'],nonlinearity = None,name='enc_logsigma'),name='ls_bnorm')] l_Z = GaussianSampleLayer(l_enc_mu, l_enc_logsigma, name='l_Z') l_dec_fc2 = BN(DL( incoming = l_Z, num_units = 1024*16, nonlinearity = relu, W=initmethod(0.02), name='l_dec_fc2'), name = 'bnorm_dec_fc2') l_unflatten = lasagne.layers.ReshapeLayer( incoming = l_dec_fc2, shape = ([0],1024,4,4), ) if dnn: l_dec_conv1 = BN(DeconvLayer( incoming = l_unflatten, num_filters = 512, filter_size = [5,5], stride = [2,2], crop = (2,2), W = initmethod(0.02), nonlinearity = relu, name = 'dec_conv1' ),name = 'bnorm_dc1') l_dec_conv2 = BN(DeconvLayer( incoming = l_dec_conv1, num_filters = 256, filter_size = [5,5], stride = [2,2], crop = (2,2), W = initmethod(0.02), nonlinearity = relu, name = 'dec_conv2' ),name = 'bnorm_dc2') l_dec_conv3 = BN(DeconvLayer( incoming = l_dec_conv2, num_filters = 128, filter_size = [5,5], stride = [2,2], crop = (2,2), W = initmethod(0.02), nonlinearity = relu, name = 'dec_conv3' ),name = 'bnorm_dc3') l_out = DeconvLayer( incoming = l_dec_conv3, num_filters = 3, filter_size = [5,5], stride = [2,2], crop = (2,2), W = initmethod(0.02), b = None, nonlinearity = lasagne.nonlinearities.tanh, name = 'dec_out' ) else: l_dec_conv1 = SL(SL(BN(TC2D( incoming = l_unflatten, num_filters = 512, filter_size = [5,5], stride = [2,2], crop = (1,1), W = initmethod(0.02), nonlinearity = relu, name = 'dec_conv1' ),name = 'bnorm_dc1'),indices=slice(1,None),axis=2),indices=slice(1,None),axis=3) l_dec_conv2 = SL(SL(BN(TC2D( incoming = l_dec_conv1, num_filters = 256, filter_size = [5,5], stride = [2,2], crop = (1,1), W = initmethod(0.02), nonlinearity = relu, name = 'dec_conv2' ),name = 'bnorm_dc2'),indices=slice(1,None),axis=2),indices=slice(1,None),axis=3) l_dec_conv3 = SL(SL(BN(TC2D( incoming = l_dec_conv2, num_filters = 128, filter_size = [5,5], stride = [2,2], crop = (1,1), W = initmethod(0.02), nonlinearity = relu, name = 'dec_conv3' ),name = 'bnorm_dc3'),indices=slice(1,None),axis=2),indices=slice(1,None),axis=3) l_out = SL(SL(TC2D( incoming = l_dec_conv3, num_filters = 3, filter_size = [5,5], stride = [2,2], crop = (1,1), W = initmethod(0.02), b = None, nonlinearity = lasagne.nonlinearities.tanh, name = 'dec_out' ),indices=slice(1,None),axis=2),indices=slice(1,None),axis=3) # l_in,num_filters=1,filter_size=[5,5],stride=[2,2],crop=[1,1],W=dc.W,b=None,nonlinearity=None) minibatch_discrim = MinibatchLayer(lasagne.layers.GlobalPoolLayer(l_enc_conv4), num_kernels=500,name='minibatch_discrim') l_discrim = DL(incoming = minibatch_discrim, num_units = 1, nonlinearity = lasagne.nonlinearities.sigmoid, b = None, W=initmethod(), name = 'discrimi') return {'l_in':l_in, 'l_out':l_out, 'l_mu':l_enc_mu, 'l_ls':l_enc_logsigma, 'l_Z':l_Z, 'l_introspect':[l_enc_conv1, l_enc_conv2,l_enc_conv3,l_enc_conv4], 'l_discrim' : l_discrim}