def build_model(input_width, input_height, output_dim, batch_size=BATCH_SIZE): l_in = lasagne.layers.InputLayer( shape=(batch_size, 1, input_width, input_height), ) l_conv1 = dnn.Conv2DDNNLayer( l_in, num_filters=32, filter_size=(5, 5), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform(), ) l_pool1 = dnn.MaxPool2DDNNLayer(l_conv1, pool_size=(2, 2)) l_conv2 = dnn.Conv2DDNNLayer( l_pool1, num_filters=32, filter_size=(5, 5), nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform(), ) l_pool2 = dnn.MaxPool2DDNNLayer(l_conv2, pool_size=(2, 2)) l_hidden1 = lasagne.layers.DenseLayer( l_pool2, num_units=256, nonlinearity=lasagne.nonlinearities.rectify, W=lasagne.init.GlorotUniform(), ) l_hidden1_dropout = lasagne.layers.DropoutLayer(l_hidden1, p=0.5) l_out = lasagne.layers.DenseLayer( l_hidden1_dropout, num_units=output_dim, nonlinearity=lasagne.nonlinearities.softmax, W=lasagne.init.GlorotUniform(), ) return l_out
def build_densenet(l_in, input_var=None, first_output=64, growth_rate=32, num_blocks=4, dropout=0): """ Creates a DenseNet model in Lasagne. Parameters ---------- input_shape : tuple The shape of the input layer, as ``(batchsize, channels, rows, cols)``. Any entry except ``channels`` can be ``None`` to indicate free size. input_var : Theano expression or None Symbolic input variable. Will be created automatically if not given. classes : int The number of classes of the softmax output. first_output : int Number of channels of initial convolution before entering the first dense block, should be of comparable size to `growth_rate`. growth_rate : int Number of feature maps added per layer. num_blocks : int Number of dense blocks (defaults to 3, as in the original paper). dropout : float The dropout rate. Set to zero (the default) to disable dropout. batchsize : int or None The batch size to build the model for, or ``None`` (the default) to allow any batch size. inputsize : int, tuple of int or None Returns ------- network : Layer instance Lasagne Layer instance for the output layer. References ---------- .. [1] Gao Huang et al. (2016): Densely Connected Convolutional Networks. https://arxiv.org/abs/1608.06993 """ nb_layers = [6, 12, 32, 32] # For DenseNet-169 nb_layers = [6, 12, 24, 16] # For DenseNet-121 # initial convolution network = Conv2DLayer(l_in, first_output, filter_size=7, stride=2, pad='same', W=lasagne.init.HeNormal(gain='relu'), b=None, nonlinearity=None, name='pre_conv') network = BatchNormLayer(network, name='pre_bn', beta=None, gamma=None) network = ScaleLayer(network, name='pre_scale') network = BiasLayer(network, name='pre_shift') network = dnn.MaxPool2DDNNLayer(network, pool_size=3, stride=2) # note: The authors' implementation does *not* have a dropout after the # initial convolution. This was missing in the paper, but important. # if dropout: # network = DropoutLayer(network, dropout) # dense blocks with transitions in between for b in range(num_blocks): network = dense_block(network, nb_layers[b], growth_rate, dropout, name_prefix='block%d' % (b + 1)) if b < num_blocks - 1: network = transition(network, dropout, name_prefix='block%d_trs' % (b + 1)) # post processing until prediction network = ScaleLayer(network, name='post_scale') network = BiasLayer(network, name='post_shift') network = NonlinearityLayer(network, nonlinearity=rectify, name='post_relu') return network