def create_model_bricks(): convnet = ConvolutionalSequence( layers=[ Convolutional( filter_size=(4, 4), num_filters=32, name='conv1'), SpatialBatchNormalization(name='batch_norm1'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=32, name='conv2'), SpatialBatchNormalization(name='batch_norm2'), Rectifier(), Convolutional( filter_size=(4, 4), num_filters=64, name='conv3'), SpatialBatchNormalization(name='batch_norm3'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=64, name='conv4'), SpatialBatchNormalization(name='batch_norm4'), Rectifier(), Convolutional( filter_size=(3, 3), num_filters=128, name='conv5'), SpatialBatchNormalization(name='batch_norm5'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=128, name='conv6'), SpatialBatchNormalization(name='batch_norm6'), Rectifier(), ], num_channels=3, image_size=(64, 64), use_bias=False, weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='convnet') convnet.initialize() mlp = BatchNormalizedMLP( activations=[Rectifier(), Logistic()], dims=[numpy.prod(convnet.get_dim('output')), 1000, 40], weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='mlp') mlp.initialize() return convnet, mlp
def bn_brick(): """Instantiates a SpatialBatchNormalization brick.""" return SpatialBatchNormalization(name=name_generator())
def create_model_bricks(z_dim, image_size, depth): g_image_size = image_size g_image_size2 = g_image_size / 2 g_image_size3 = g_image_size / 4 g_image_size4 = g_image_size / 8 g_image_size5 = g_image_size / 16 encoder_layers = [] if depth > 0: encoder_layers = encoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv1'), SpatialBatchNormalization(name='batch_norm1'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv2'), SpatialBatchNormalization(name='batch_norm2'), Rectifier(), Convolutional( filter_size=(2, 2), step=(2, 2), num_filters=32, name='conv3'), SpatialBatchNormalization(name='batch_norm3'), Rectifier() ] if depth > 1: encoder_layers = encoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv4'), SpatialBatchNormalization(name='batch_norm4'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv5'), SpatialBatchNormalization(name='batch_norm5'), Rectifier(), Convolutional( filter_size=(2, 2), step=(2, 2), num_filters=64, name='conv6'), SpatialBatchNormalization(name='batch_norm6'), Rectifier() ] if depth > 2: encoder_layers = encoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv7'), SpatialBatchNormalization(name='batch_norm7'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv8'), SpatialBatchNormalization(name='batch_norm8'), Rectifier(), Convolutional( filter_size=(2, 2), step=(2, 2), num_filters=128, name='conv9'), SpatialBatchNormalization(name='batch_norm9'), Rectifier() ] if depth > 3: encoder_layers = encoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv10'), SpatialBatchNormalization(name='batch_norm10'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv11'), SpatialBatchNormalization(name='batch_norm11'), Rectifier(), Convolutional(filter_size=(2, 2), step=(2, 2), num_filters=256, name='conv12'), SpatialBatchNormalization(name='batch_norm12'), Rectifier(), ] if depth > 4: encoder_layers = encoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=512, name='conv13'), SpatialBatchNormalization(name='batch_norm13'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=512, name='conv14'), SpatialBatchNormalization(name='batch_norm14'), Rectifier(), Convolutional(filter_size=(2, 2), step=(2, 2), num_filters=512, name='conv15'), SpatialBatchNormalization(name='batch_norm15'), Rectifier() ] decoder_layers = [] if depth > 4: decoder_layers = decoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=512, name='conv_n3'), SpatialBatchNormalization(name='batch_norm_n3'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=512, name='conv_n2'), SpatialBatchNormalization(name='batch_norm_n2'), Rectifier(), ConvolutionalTranspose( filter_size=(2, 2), step=(2, 2), original_image_size=(g_image_size5, g_image_size5), num_filters=512, name='conv_n1'), SpatialBatchNormalization(name='batch_norm_n1'), Rectifier() ] if depth > 3: decoder_layers = decoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv1'), SpatialBatchNormalization(name='batch_norm1'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv2'), SpatialBatchNormalization(name='batch_norm2'), Rectifier(), ConvolutionalTranspose( filter_size=(2, 2), step=(2, 2), original_image_size=(g_image_size4, g_image_size4), num_filters=256, name='conv3'), SpatialBatchNormalization(name='batch_norm3'), Rectifier() ] if depth > 2: decoder_layers = decoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv4'), SpatialBatchNormalization(name='batch_norm4'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv5'), SpatialBatchNormalization(name='batch_norm5'), Rectifier(), ConvolutionalTranspose( filter_size=(2, 2), step=(2, 2), original_image_size=(g_image_size3, g_image_size3), num_filters=128, name='conv6'), SpatialBatchNormalization(name='batch_norm6'), Rectifier() ] if depth > 1: decoder_layers = decoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv7'), SpatialBatchNormalization(name='batch_norm7'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv8'), SpatialBatchNormalization(name='batch_norm8'), Rectifier(), ConvolutionalTranspose( filter_size=(2, 2), step=(2, 2), original_image_size=(g_image_size2, g_image_size2), num_filters=64, name='conv9'), SpatialBatchNormalization(name='batch_norm9'), Rectifier() ] if depth > 0: decoder_layers = decoder_layers + [ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv10'), SpatialBatchNormalization(name='batch_norm10'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv11'), SpatialBatchNormalization(name='batch_norm11'), Rectifier(), ConvolutionalTranspose( filter_size=(2, 2), step=(2, 2), original_image_size=(g_image_size, g_image_size), num_filters=32, name='conv12'), SpatialBatchNormalization(name='batch_norm12'), Rectifier() ] decoder_layers = decoder_layers + [ Convolutional(filter_size=(1, 1), num_filters=3, name='conv_out'), Logistic() ] print("creating model of depth {} with {} encoder and {} decoder layers". format(depth, len(encoder_layers), len(decoder_layers))) encoder_convnet = ConvolutionalSequence( layers=encoder_layers, num_channels=3, image_size=(g_image_size, g_image_size), use_bias=False, weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='encoder_convnet') encoder_convnet.initialize() encoder_filters = numpy.prod(encoder_convnet.get_dim('output')) encoder_mlp = MLP( dims=[encoder_filters, 1000, z_dim], activations=[ Sequence([BatchNormalization(1000).apply, Rectifier().apply], name='activation1'), Identity().apply ], weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='encoder_mlp') encoder_mlp.initialize() decoder_mlp = BatchNormalizedMLP( activations=[Rectifier(), Rectifier()], dims=[encoder_mlp.output_dim // 2, 1000, encoder_filters], weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='decoder_mlp') decoder_mlp.initialize() decoder_convnet = ConvolutionalSequence( layers=decoder_layers, num_channels=encoder_convnet.get_dim('output')[0], image_size=encoder_convnet.get_dim('output')[1:], use_bias=False, weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='decoder_convnet') decoder_convnet.initialize() return encoder_convnet, encoder_mlp, decoder_convnet, decoder_mlp
def create_model_bricks(): encoder_convnet = ConvolutionalSequence( layers=[ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv1'), SpatialBatchNormalization(name='batch_norm1'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv2'), SpatialBatchNormalization(name='batch_norm2'), Rectifier(), Convolutional(filter_size=(2, 2), step=(2, 2), num_filters=32, name='conv3'), SpatialBatchNormalization(name='batch_norm3'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv4'), SpatialBatchNormalization(name='batch_norm4'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv5'), SpatialBatchNormalization(name='batch_norm5'), Rectifier(), Convolutional(filter_size=(2, 2), step=(2, 2), num_filters=64, name='conv6'), SpatialBatchNormalization(name='batch_norm6'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv7'), SpatialBatchNormalization(name='batch_norm7'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv8'), SpatialBatchNormalization(name='batch_norm8'), Rectifier(), Convolutional(filter_size=(2, 2), step=(2, 2), num_filters=128, name='conv9'), SpatialBatchNormalization(name='batch_norm9'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv10'), SpatialBatchNormalization(name='batch_norm10'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv11'), SpatialBatchNormalization(name='batch_norm11'), Rectifier(), Convolutional(filter_size=(2, 2), step=(2, 2), num_filters=256, name='conv12'), SpatialBatchNormalization(name='batch_norm12'), Rectifier(), ], num_channels=3, image_size=(64, 64), use_bias=False, weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='encoder_convnet') encoder_convnet.initialize() encoder_filters = numpy.prod(encoder_convnet.get_dim('output')) encoder_mlp = MLP( dims=[encoder_filters, 1000, 1000], activations=[ Sequence([BatchNormalization(1000).apply, Rectifier().apply], name='activation1'), Identity().apply ], weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='encoder_mlp') encoder_mlp.initialize() decoder_mlp = BatchNormalizedMLP( activations=[Rectifier(), Rectifier()], dims=[encoder_mlp.output_dim // 2, 1000, encoder_filters], weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='decoder_mlp') decoder_mlp.initialize() decoder_convnet = ConvolutionalSequence( layers=[ Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv1'), SpatialBatchNormalization(name='batch_norm1'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=256, name='conv2'), SpatialBatchNormalization(name='batch_norm2'), Rectifier(), ConvolutionalTranspose(filter_size=(2, 2), step=(2, 2), original_image_size=(8, 8), num_filters=256, name='conv3'), SpatialBatchNormalization(name='batch_norm3'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv4'), SpatialBatchNormalization(name='batch_norm4'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=128, name='conv5'), SpatialBatchNormalization(name='batch_norm5'), Rectifier(), ConvolutionalTranspose(filter_size=(2, 2), step=(2, 2), original_image_size=(16, 16), num_filters=128, name='conv6'), SpatialBatchNormalization(name='batch_norm6'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv7'), SpatialBatchNormalization(name='batch_norm7'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=64, name='conv8'), SpatialBatchNormalization(name='batch_norm8'), Rectifier(), ConvolutionalTranspose(filter_size=(2, 2), step=(2, 2), original_image_size=(32, 32), num_filters=64, name='conv9'), SpatialBatchNormalization(name='batch_norm9'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv10'), SpatialBatchNormalization(name='batch_norm10'), Rectifier(), Convolutional(filter_size=(3, 3), border_mode=(1, 1), num_filters=32, name='conv11'), SpatialBatchNormalization(name='batch_norm11'), Rectifier(), ConvolutionalTranspose(filter_size=(2, 2), step=(2, 2), original_image_size=(64, 64), num_filters=32, name='conv12'), SpatialBatchNormalization(name='batch_norm12'), Rectifier(), Convolutional(filter_size=(1, 1), num_filters=3, name='conv_out'), Logistic(), ], num_channels=encoder_convnet.get_dim('output')[0], image_size=encoder_convnet.get_dim('output')[1:], use_bias=False, weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='decoder_convnet') decoder_convnet.initialize() return encoder_convnet, encoder_mlp, decoder_convnet, decoder_mlp
def create_model_bricks(image_size, depth): # original celebA64 was depth=3 (went to bach_norm6) layers = [] if(depth > 0): layers = layers + [ Convolutional( filter_size=(4, 4), num_filters=32, name='conv1'), SpatialBatchNormalization(name='batch_norm1'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=32, name='conv2'), SpatialBatchNormalization(name='batch_norm2'), Rectifier(), ] if(depth > 1): layers = layers + [ Convolutional( filter_size=(4, 4), num_filters=64, name='conv3'), SpatialBatchNormalization(name='batch_norm3'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=64, name='conv4'), SpatialBatchNormalization(name='batch_norm4'), Rectifier(), ] if(depth > 2): layers = layers + [ Convolutional( filter_size=(3, 3), num_filters=128, name='conv5'), SpatialBatchNormalization(name='batch_norm5'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=128, name='conv6'), SpatialBatchNormalization(name='batch_norm6'), Rectifier(), ] if(depth > 3): layers = layers + [ Convolutional( filter_size=(3, 3), num_filters=256, name='conv7'), SpatialBatchNormalization(name='batch_norm7'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=256, name='conv8'), SpatialBatchNormalization(name='batch_norm8'), Rectifier(), ] if(depth > 4): layers = layers + [ Convolutional( filter_size=(3, 3), num_filters=512, name='conv9'), SpatialBatchNormalization(name='batch_norm9'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=512, name='conv10'), SpatialBatchNormalization(name='batch_norm10'), Rectifier(), ] if(depth > 5): layers = layers + [ Convolutional( filter_size=(3, 3), num_filters=512, name='conv11'), SpatialBatchNormalization(name='batch_norm11'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=512, name='conv12'), SpatialBatchNormalization(name='batch_norm12'), Rectifier(), ] if(depth > 6): layers = layers + [ Convolutional( filter_size=(3, 3), num_filters=512, name='conv13'), SpatialBatchNormalization(name='batch_norm13'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=512, name='conv14'), SpatialBatchNormalization(name='batch_norm14'), Rectifier(), ] if(depth > 7): layers = layers + [ Convolutional( filter_size=(3, 3), num_filters=512, name='conv15'), SpatialBatchNormalization(name='batch_norm15'), Rectifier(), Convolutional( filter_size=(3, 3), step=(2, 2), num_filters=512, name='conv16'), SpatialBatchNormalization(name='batch_norm16'), Rectifier(), ] print("creating model of depth {} with {} layers".format(depth, len(layers))) convnet = ConvolutionalSequence( layers=layers, num_channels=3, image_size=(image_size, image_size), use_bias=False, weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='convnet') convnet.initialize() mlp = BatchNormalizedMLP( activations=[Rectifier(), Logistic()], dims=[numpy.prod(convnet.get_dim('output')), 1000, 64], weights_init=IsotropicGaussian(0.033), biases_init=Constant(0), name='mlp') mlp.initialize() return convnet, mlp, len(layers)