def projection_block(x, **metaparameters): """ Create a residual block using Depthwise Separable Convolutions with Projection shortcut x : input into residual block n_filters: number of filters """ n_filters = metaparameters['n_filters'] del metaparameters['n_filters'] # Remember the input shortcut = x # Strided convolution to double number of filters in identity link to # match output of residual block for the add operation (projection shortcut) shortcut = Composable.Conv2D(x, n_filters, (1, 1), strides=(2, 2), padding='same', **metaparameters) shortcut = BatchNormalization()(shortcut) # First Depthwise Separable Convolution x = Composable.SeparableConv2D(x, n_filters, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Second depthwise Separable Convolution x = Composable.SeparableConv2D(x, n_filters, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Create pooled feature maps, reduce size by 75% x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) # Add the projection shortcut to the output of the block x = Add()([x, shortcut]) return x
def residual_block(x, **metaparameters): """ Create a residual block using Depthwise Separable Convolutions x : input into residual block n_filters: number of filters """ n_filters = metaparameters['n_filters'] del metaparameters['n_filters'] # Remember the input shortcut = x # First Depthwise Separable Convolution x = Composable.SeparableConv2D(x, n_filters, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Second depthwise Separable Convolution x = Composable.SeparableConv2D(x, n_filters, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Third depthwise Separable Convolution x = Composable.SeparableConv2D(x, n_filters, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Add the identity link to the output of the block x = Add()([x, shortcut]) return x
def exitFlow(x, n_classes, **metaparameters): """ Create the exit flow section x : input to the exit flow section n_classes : number of output classes """ # Remember the input shortcut = x # Strided convolution to double number of filters in identity link to # match output of residual block for the add operation (projection shortcut) shortcut = Composable.Conv2D(x, 1024, (1, 1), strides=(2, 2), padding='same', **metaparameters) shortcut = BatchNormalization()(shortcut) # First Depthwise Separable Convolution # Dimensionality reduction - reduce number of filters x = Composable.SeparableConv2D(x, 728, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) # Second Depthwise Separable Convolution # Dimensionality restoration x = Composable.SeparableConv2D(x, 1024, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Create pooled feature maps, reduce size by 75% x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) # Add the projection shortcut to the output of the pooling layer x = Add()([x, shortcut]) # Third Depthwise Separable Convolution x = Composable.SeparableConv2D(x, 1556, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Fourth Depthwise Separable Convolution x = Composable.SeparableConv2D(x, 2048, (3, 3), padding='same', **metaparameters) x = BatchNormalization()(x) x = Composable.ReLU(x) # Create classifier section x = Composable.classifier(x, n_classes, **metaparameters) return x