Build the DenseNet model :param nb_classes: int -- number of classes :param img_dim: tuple -- (channels, rows, columns) :param depth: int -- how many layers :param nb_dense_block: int -- number of dense blocks to add to end :param growth_rate: int -- number of filters to add
(nb_classes, img_dim, depth, nb_dense_block, growth_rate,
nb_filter, dropout_rate=None, weight_decay=1E-4)
| 129 | |
| 130 | |
| 131 | def DenseNet(nb_classes, img_dim, depth, nb_dense_block, growth_rate, |
| 132 | nb_filter, dropout_rate=None, weight_decay=1E-4): |
| 133 | """ Build the DenseNet model |
| 134 | |
| 135 | :param nb_classes: int -- number of classes |
| 136 | :param img_dim: tuple -- (channels, rows, columns) |
| 137 | :param depth: int -- how many layers |
| 138 | :param nb_dense_block: int -- number of dense blocks to add to end |
| 139 | :param growth_rate: int -- number of filters to add |
| 140 | :param nb_filter: int -- number of filters |
| 141 | :param dropout_rate: float -- dropout rate |
| 142 | :param weight_decay: float -- weight decay |
| 143 | |
| 144 | :returns: keras model with nb_layers of conv_factory appended |
| 145 | :rtype: keras model |
| 146 | |
| 147 | """ |
| 148 | |
| 149 | if K.image_dim_ordering() == "th": |
| 150 | concat_axis = 1 |
| 151 | elif K.image_dim_ordering() == "tf": |
| 152 | concat_axis = -1 |
| 153 | |
| 154 | model_input = Input(shape=img_dim) |
| 155 | |
| 156 | assert (depth - 4) % 3 == 0, "Depth must be 3 N + 4" |
| 157 | |
| 158 | # layers in each dense block |
| 159 | nb_layers = int((depth - 4) / 3) |
| 160 | |
| 161 | # Initial convolution |
| 162 | x = Conv2D(nb_filter, (3, 3), |
| 163 | kernel_initializer="he_uniform", |
| 164 | padding="same", |
| 165 | name="initial_conv2D", |
| 166 | use_bias=False, |
| 167 | kernel_regularizer=l2(weight_decay))(model_input) |
| 168 | |
| 169 | # Add dense blocks |
| 170 | for block_idx in range(nb_dense_block - 1): |
| 171 | x, nb_filter = denseblock(x, concat_axis, nb_layers, |
| 172 | nb_filter, growth_rate, |
| 173 | dropout_rate=dropout_rate, |
| 174 | weight_decay=weight_decay) |
| 175 | # add transition |
| 176 | x = transition(x, nb_filter, dropout_rate=dropout_rate, |
| 177 | weight_decay=weight_decay) |
| 178 | |
| 179 | # The last denseblock does not have a transition |
| 180 | x, nb_filter = denseblock(x, concat_axis, nb_layers, |
| 181 | nb_filter, growth_rate, |
| 182 | dropout_rate=dropout_rate, |
| 183 | weight_decay=weight_decay) |
| 184 | |
| 185 | x = BatchNormalization(axis=concat_axis, |
| 186 | gamma_regularizer=l2(weight_decay), |
| 187 | beta_regularizer=l2(weight_decay))(x) |
| 188 | x = Activation('relu')(x) |
nothing calls this directly
no test coverage detected