def MDBLOCK(incoming, num_filters, scales, name, nonlinearity): return NL( BN(ESL([ incoming, MDCL( NL( BN(MDCL( NL(BN(incoming, name=name + 'bnorm0'), nonlinearity), num_filters, scales, name), name=name + 'bnorm1'), nonlinearity), num_filters, scales, name + '2') ]), name=name + 'bnorm2'), nonlinearity)
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 InceptionLayer(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] = C2D( incoming = branch[i] if j else incoming, num_filters = dict['num_filters'][j], filter_size = dict['filter_size'][j], pad = 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(lasagne.layers.dnn.Pool2DDNNLayer( incoming=incoming if j == 0 else branch[i], pool_size = dict['filter_size'][j], mode = dict['mode'][j], stride = dict['stride'][j], pad = dict['pad'][j], name = block_name+'_'+str(i)+'_'+str(j)), nonlinearity = dict['nonlinearity'][j]) if style=='pool'\ else lasagne.layers.DilatedConv2DLayer( incoming = lasagne.layers.PadLayer(incoming = incoming if j==0 else branch[i],width = dict['pad'][j]) if 'pad' in dict else incoming if j==0 else branch[i], num_filters = dict['num_filters'][j], filter_size = dict['filter_size'][j], dilation = dict['dilation'][j], # pad = dict['pad'][j] if 'pad' in dict else None, W = initmethod('relu'), nonlinearity = dict['nonlinearity'][j], name = block_name+'_'+str(i)+'_'+str(j)) if style== 'dilation'\ else DL( incoming = incoming if j==0 else branch[i], num_units = dict['num_filters'][j], W = initmethod('relu'), b = None, nonlinearity = dict['nonlinearity'][j], name = block_name+'_'+str(i)+'_'+str(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(): lasagne.random.set_rng(np.random.RandomState(1234)) 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_conv0 = lasagne.layers.dnn.Conv3DDNNLayer( incoming = l_in, num_filters = 32, filter_size = (7,7,7), stride = (2,2,2), pad = 'same', W = initmethod(), nonlinearity = None, name = 'l_conv0') l_conv1 = ResDrop(incoming = l_conv0, IB = InceptionLayer(incoming = NL(BN(l_conv0,name='bn_conv0'),elu), param_dict = [{'num_filters':[16,16,32], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv1'),p=0.95) l_conv2 = ResDrop(incoming = l_conv1, IB = InceptionLayer(incoming = NL(BN(l_conv1,name='bn_conv1'),elu), param_dict = [{'num_filters':[16,16,32], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv2'),p=0.9) l_conv3 = ResDrop(incoming = l_conv2, IB = InceptionLayer(incoming = NL(BN(l_conv2,name='bn_conv2'),elu), param_dict = [{'num_filters':[16,16,32], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv3'),p=0.8) l_conv4 = InceptionLayer(incoming = NL(BN(l_conv3,name='bn_conv3'),elu), param_dict = [{'num_filters':[16], 'filter_size':[(3,3,3)], 'border_mode':['same'], 'strides':[(2,2,2)], 'nonlinearity': [None], 'style': ['convolutional'], 'bnorm':[0]}, {'num_filters':[16], 'filter_size':[(1,1,1)], 'pad':[(0,0,0)], 'strides':[(2,2,2)], 'nonlinearity': [None], 'style': ['convolutional'], 'bnorm':[0]}, {'num_filters':[16,1], 'mode': [0,'max'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,None], 'style': ['convolutional','pool'], 'bnorm':[0,0]}, {'num_filters':[16,1], 'mode': [0,'average_inc_pad'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,None], 'style': ['convolutional','pool'], 'bnorm':[0,0]}], block_name = 'conv4') l_conv5 = ResDrop(incoming = l_conv4, IB = InceptionLayer(incoming = NL(BN(l_conv4,name='bn_conv4'),elu), param_dict = [{'num_filters':[32,32,64], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv5'),p=0.7) l_conv6 = ResDrop(incoming = l_conv5, IB = InceptionLayer(incoming = NL(BN(l_conv5,name='bn_conv5'),elu), param_dict = [{'num_filters':[32,32,64], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv6'),p=0.6) l_conv7 = ResDrop(incoming = l_conv6, IB = InceptionLayer(incoming = NL(BN(l_conv6,name='bn_conv6'),elu), param_dict = [{'num_filters':[32,32,64], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv7'),p=0.6) l_conv8 = InceptionLayer(incoming = NL(BN(l_conv7,name='bn_conv7'),elu), param_dict = [{'num_filters':[32], 'filter_size':[(3,3,3)], 'border_mode':['same'], 'strides':[(2,2,2)], 'nonlinearity': [None], 'style': ['convolutional'], 'bnorm':[0]}, {'num_filters':[32], 'filter_size':[(1,1,1)], 'pad':[(0,0,0)], 'strides':[(2,2,2)], 'nonlinearity': [None], 'style': ['convolutional'], 'bnorm':[0]}, {'num_filters':[32,1], 'mode': [0,'max'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,None], 'style': ['convolutional','pool'], 'bnorm':[0,0]}, {'num_filters':[32,1], 'mode': [0,'average_inc_pad'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,None], 'style': ['convolutional','pool'], 'bnorm':[0,0]}], block_name = 'conv8') l_conv9 = ResDrop(incoming = l_conv8, IB = InceptionLayer(incoming = NL(BN(l_conv8,name='bn_conv8'),elu), param_dict = [{'num_filters':[64,64,128], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv9'),p=0.5) l_conv10 = ResDrop(incoming = l_conv9, IB = InceptionLayer(incoming = NL(BN(l_conv9,name='bn_conv9'),elu), param_dict = [{'num_filters':[64,64,128], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv10'),p=0.5) l_conv11 = ResDrop(incoming = l_conv8, IB = InceptionLayer(incoming = NL(BN(l_conv10,name='bn_conv10'),elu), param_dict = [{'num_filters':[64,64,128], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv11'),p=0.5) l_conv12 = InceptionLayer(incoming = NL(BN(l_conv11,name='bn_conv11'),elu), param_dict = [{'num_filters':[64], 'filter_size':[(3,3,3)], 'border_mode':['same'], 'strides':[(2,2,2)], 'nonlinearity': [None], 'style': ['convolutional'], 'bnorm':[0]}, {'num_filters':[64], 'filter_size':[(1,1,1)], 'pad':[(0,0,0)], 'strides':[(2,2,2)], 'nonlinearity': [None], 'style': ['convolutional'], 'bnorm':[0]}, {'num_filters':[64,1], 'mode': [0,'max'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,None], 'style': ['convolutional','pool'], 'bnorm':[0,0]}, {'num_filters':[64,1], 'mode': [0,'average_inc_pad'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,None], 'style': ['convolutional','pool'], 'bnorm':[0,0]}], block_name = 'conv12') l_conv13 = ResDrop(incoming = l_conv12, IB = InceptionLayer(incoming = NL(BN(l_conv12,name='bn_conv12'),elu), param_dict = [{'num_filters':[128,128,256], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv13'),p=0.5) l_conv14 = ResDrop(incoming = l_conv13, IB = InceptionLayer(incoming = NL(BN(l_conv13,name='bn_conv13'),elu), param_dict = [{'num_filters':[128,128,256], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv14'),p=0.4) l_conv15 = ResDrop(incoming = l_conv14, IB = InceptionLayer(incoming = NL(BN(l_conv14,name='bn_conv14'),elu), param_dict = [{'num_filters':[128,128,256], 'filter_size':[(1,1,1),(3,3,3),(1,1,1)], 'border_mode':['same']*3, 'strides':[(1,1,1)]*3, 'nonlinearity': [elu,elu,None], 'style': ['convolutional']*3, 'bnorm':[1,1,0]}], block_name = 'conv15'),p=0.3) l_conv16 = InceptionLayer(incoming = NL(BN(l_conv15,name='bn_conv15'),elu), param_dict = [{'num_filters':[128], 'filter_size':[(3,3,3)], 'border_mode':['same'], 'strides':[(2,2,2)], 'nonlinearity': [elu], 'style': ['convolutional'], 'bnorm':[1]}, {'num_filters':[128], 'filter_size':[(1,1,1)], 'pad':[(0,0,0)], 'strides':[(2,2,2)], 'nonlinearity': [elu], 'style': ['convolutional'], 'bnorm':[1]}, {'num_filters':[128,1], 'mode': [0,'max'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,elu], 'style': ['convolutional','pool'], 'bnorm':[0,1]}, {'num_filters':[128,1], 'mode': [0,'average_inc_pad'], 'filter_size':[(3,3,3),(3,3,3)], 'pad':[(1,1,1),(1,1,1)], 'strides':[(1,1,1),(2,2,2)], 'nonlinearity': [None,elu], 'style': ['convolutional','pool'], 'bnorm':[0,1]}], block_name = 'conv16') l_conv17 = ResDropNoPre(l_conv16,BN(lasagne.layers.dnn.Conv3DDNNLayer( incoming = l_conv16, num_filters = 512, filter_size = (3,3,3), pad = 'same', W = initmethod('relu'), nonlinearity = None, name = 'l_conv17'),name='bn_conv17'),0.2) l_pool = BN(lasagne.layers.GlobalPoolLayer(l_conv17),name='l_pool') l_fc1 = BN(lasagne.layers.DenseLayer( incoming = l_pool, num_units = 512, W = initmethod('relu'), nonlinearity = elu, name = 'fc1' ),name = 'bnorm_fc1') l_fc2 = lasagne.layers.DenseLayer( incoming = l_fc1, num_units = n_classes, W = initmethod(), nonlinearity = None, name = 'fc2' ) return {'l_in':l_in, 'l_out':l_fc2}
def get_model(interp=False): dims, n_channels = tuple(cfg['dims']), cfg['n_channels'] 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), 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), 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), 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), name='enc_conv4'), name='bnorm4') print(lasagne.layers.get_output_shape(l_enc_conv4, (196, 3, 64, 64))) l_enc_fc1 = BN(DL(incoming=l_enc_conv4, num_units=1000, W=initmethod(0.02), nonlinearity=relu, name='enc_fc1'), name='bnorm_enc_fc1') # Define latent values 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_IAF = GaussianSampleLayer(l_enc_mu, l_enc_logsigma, name='l_Z_IAF') l_IAF_mu, l_IAF_logsigma = [ MADE(l_Z_IAF, [cfg['num_latents']], 'l_IAF_mu'), MADE(l_Z_IAF, [cfg['num_latents']], 'l_IAF_ls') ] l_Z = IAFLayer(l_Z_IAF, l_IAF_mu, l_IAF_logsigma, name='l_Z') l_dec_fc2 = DL(incoming=l_Z, num_units=512 * 16, nonlinearity=lrelu(0.2), W=initmethod(0.02), name='l_dec_fc2') l_unflatten = lasagne.layers.ReshapeLayer( incoming=l_dec_fc2, shape=([0], 512, 4, 4), ) l_dec_conv1 = DeconvLayer(incoming=l_unflatten, num_filters=512, filter_size=[5, 5], stride=[2, 2], crop=(2, 2), W=initmethod(0.02), nonlinearity=None, name='dec_conv1') l_dec_conv2a = MDBLOCK(incoming=l_dec_conv1, num_filters=512, scales=[0, 2], name='dec_conv2a', nonlinearity=lrelu(0.2)) l_dec_conv2 = DeconvLayer(incoming=l_dec_conv2a, num_filters=256, filter_size=[5, 5], stride=[2, 2], crop=(2, 2), W=initmethod(0.02), nonlinearity=None, name='dec_conv2') l_dec_conv3a = MDBLOCK(incoming=l_dec_conv2, num_filters=256, scales=[0, 2, 3], name='dec_conv3a', nonlinearity=lrelu(0.2)) l_dec_conv3 = DeconvLayer(incoming=l_dec_conv3a, num_filters=128, filter_size=[5, 5], stride=[2, 2], crop=(2, 2), W=initmethod(0.02), nonlinearity=None, name='dec_conv3') l_dec_conv4a = MDBLOCK(incoming=l_dec_conv3, num_filters=128, scales=[0, 2, 3], name='dec_conv4a', nonlinearity=lrelu(0.2)) l_dec_conv4 = BN(DeconvLayer(incoming=l_dec_conv4a, num_filters=128, filter_size=[5, 5], stride=[2, 2], crop=(2, 2), W=initmethod(0.02), nonlinearity=lrelu(0.2), name='dec_conv4'), name='bnorm_dc4') R = NL(MDCL(l_dec_conv4, num_filters=2, scales=[2, 3, 4], name='R'), sigmoid) G = NL( ESL([ MDCL(l_dec_conv4, num_filters=2, scales=[2, 3, 4], name='G_a'), MDCL(R, num_filters=2, scales=[2, 3, 4], name='G_b') ]), sigmoid) B = NL( ESL([ MDCL(l_dec_conv4, num_filters=2, scales=[2, 3, 4], name='B_a'), MDCL(CL([R, G]), num_filters=2, scales=[2, 3, 4], name='B_b') ]), sigmoid) l_out = CL([ beta_layer(SL(R, slice(0, 1), 1), SL(R, slice(1, 2), 1)), beta_layer(SL(G, slice(0, 1), 1), SL(G, slice(1, 2), 1)), beta_layer(SL(B, slice(0, 1), 1), SL(B, slice(1, 2), 1)) ]) minibatch_discrim = MinibatchLayer( lasagne.layers.GlobalPoolLayer(l_enc_conv4), num_kernels=500, name='minibatch_discrim') l_discrim = DL(incoming=minibatch_discrim, num_units=3, nonlinearity=lasagne.nonlinearities.softmax, b=None, W=initmethod(0.02), 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_IAF_mu': l_IAF_mu, 'l_IAF_ls': l_IAF_logsigma, 'l_Z_IAF': l_Z_IAF, 'l_introspect': [l_enc_conv1, l_enc_conv2, l_enc_conv3, l_enc_conv4], 'l_discrim': l_discrim }
def get_model(interp=False): 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), 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), 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), 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), 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), ) 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') 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_latents': l_Z, 'l_introspect': [l_enc_conv1, l_enc_conv2, l_enc_conv3, l_enc_conv4], 'l_discrim': l_discrim }
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}