def Encoder(input_shape, embedding_dimension, drop_out=0.05):
    #Input placeholders
    inp_placeholder = tf.keras.Input(shape=input_shape)
    
    #Convolution layer 1
    layer1 = layers.Conv2D(40, 25, activation='relu', name="L11")(inp_placeholder)
    norm1 = layers.BatchNormalization()(layer1)
    drops1 = layers.SpatialDropout2D(drop_out)(norm1)
    out1 = layers.MaxPooling2D(5)(drops1)
    
    #Convolution layer 2
    layer2 = layers.Conv2D(20, 15, activation='relu', name="L21")(out1)
    norm2 = layers.BatchNormalization()(layer2)
    drops2 = layers.SpatialDropout2D(drop_out)(norm2)
    out2 = layers.MaxPooling2D(2)(drops2)
    
    #Convolution layer 3
    layer3 = layers.Conv2D(10, 5, activation='relu', name="L31")(out2)
    norm3 = layers.BatchNormalization()(layer3)
    out3 = layers.SpatialDropout2D(drop_out)(norm3)
    
    #Flattened layer
    flatten = layers.Flatten()(out3)
    
    #Dense Layer
    embeds = layers.Dense(embedding_dimension, activation = "sigmoid", name="D11")(flatten)
    
    #Definining our model
    encoder = tf.keras.Model(inputs=inp_placeholder, outputs = embeds)
    return encoder
    def __init__(self,
                 num_channels,
                 num_conv_layers=2,
                 kernel_size=(3, 3),
                 pool_size=(2, 2),
                 nonlinearity='relu',
                 use_batchnorm=True,
                 use_bias=True,
                 use_dropout=False,
                 dropout_rate=0.25,
                 use_spatial_dropout=True,
                 data_format='channels_last',
                 **kwargs):

        super(SegNet_Conv2D_Block, self).__init__(**kwargs)

        for _ in range(num_conv_layers):
            self.add(tfkl.Conv2D(num_channels,
                                 kernel_size,
                                 padding='same',
                                 use_bias=use_bias,
                                 data_format=data_format))
            if use_batchnorm:
                self.add(tfkl.BatchNormalization(axis=-1,
                                                 momentum=0.95,
                                                 epsilon=0.001))
            self.add(tfkl.Activation(nonlinearity))

        if use_dropout:
            if use_spatial_dropout:
                self.add(tfkl.SpatialDropout2D(rate=dropout_rate))
            else:
                self.add(tfkl.Dropout(rate=dropout_rate))

        self.add(tfkl.MaxPool2D(pool_size))
示例#3
0
 def wrapper(input_tensor):
     
     x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name='cls/zeropad1')(input_tensor)
     x = layers.Conv2D(
             filters=filters,
             kernel_size=(3, 3),
             padding='valid',
             kernel_initializer='glorot_uniform',
             name='cls/conv1',
         )(x)
     if(use_batchnorm):
         x = layers.BatchNormalization(axis=bn_axis, name='cls/bn1')(x)
     x = layers.Activation('relu', name='cls/relu1')(x)
     # model regularization
     if dropout is not None:
         x = layers.SpatialDropout2D(dropout, name='cls/drp1')(x)
     
     # model head
     x = layers.Conv2D(
             filters=n_classes,
             kernel_size=(1, 1),
             padding='valid',
             kernel_initializer='glorot_uniform',
             name='final_conv',
         )(x)
     return x
示例#4
0
def neural_network_spatial():
    input_ = layers.Input(shape=(32, 32, 3))
    cnn = layers.Conv2D(16, (3, 3), activation="relu")(input_)
    cnn = layers.SpatialDropout2D(______)(cnn)
    cnn = layers.MaxPooling2D()(cnn)

    cnn = layers.Conv2D(32, (3, 3), activation="relu")(cnn)
    cnn = layers.____________(cnn)
    cnn = layers.MaxPooling2D()(cnn)

    flatten = layers.GlobalMaxPooling2D()(cnn)

    dense = layers.Dense(32, activation="relu")(flatten)
    dense = layers.Dropout(_______)(dense)
    dense = layers.Dense(16, activation="relu")(dense)

    output = layers.Dense(10, activation="softmax")(dense)

    opt = optimizers.Adam()

    m = models.Model(input_, output)
    m.compile(optimizer=opt,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

    return m
def get_model(in_shape, out_classes, dropout_rate=0.2, noise=1, activation='relu', combo='add', regression=False):
    in_tensor = layers.Input(shape=in_shape, name='input')
    in_tensor = add_features(in_tensor)

    vgg19 = keras.applications.VGG19(include_top=False, weights=None, input_tensor=in_tensor)

    base_in = vgg19.input
    base_out = vgg19.output
    concat_layers = ['block5_conv4', 'block4_conv4', 'block3_conv4', 'block2_conv2', 'block1_conv2']
    concat_tensors = [vgg19.get_layer(layer).output for layer in concat_layers]

    decoder0 = decoder_block(
        base_out, n_filters=1028, n_convs=3, i=0,
        rate=dropout_rate, noise=noise, activation=activation
    )  # 64
    decoder1 = decoder_block(
        decoder0, concat_tensor=concat_tensors[0], n_filters=512, n_convs=3, i=2,
        rate=dropout_rate, noise=noise, activation=activation
    )
    decoder2 = decoder_block(
        decoder1, concat_tensor=concat_tensors[1], n_filters=512, n_convs=3, i=3,
        rate=dropout_rate, noise=noise, activation=activation
    )
    decoder3 = decoder_block(
        decoder2, concat_tensor=concat_tensors[2], n_filters=256, n_convs=2, i=4,
        rate=dropout_rate, noise=noise, activation=activation
    )
    decoder4 = decoder_block(
        decoder3, concat_tensor=concat_tensors[3], n_filters=128, n_convs=2, i=5,
        rate=dropout_rate, noise=noise, activation=activation
    )

    out_branch = conv_layer(64, (3, 3), name=f'out_block_conv1')(decoder4)
    out_branch = layers.BatchNormalization(name=f'out_block_batchnorm1')(out_branch)
    out_branch = layers.Activation(activation, name='out_block_activation1')(out_branch)
    if combo == 'add':
        out_branch = layers.add([out_branch, concat_tensors[4]], name='out_block_residual')
    elif combo == 'concat':
        out_branch = layers.concatenate([out_branch, concat_tensors[4]], name='out_block_concat')
    out_branch = layers.SpatialDropout2D(rate=dropout_rate, seed=0, name='out_block_spatialdrop')(out_branch)
    out_branch = conv_layer(64, (5, 5), name='out_block_conv2')(out_branch)
    out_branch = layers.BatchNormalization(name='out_block_batchnorm2')(out_branch)
    out_branch = layers.Activation(activation, name='out_block_activation2')(out_branch)

    output = layers.Conv2D(out_classes, (1, 1), name='final_conv')(out_branch)
    if regression:
        out_activation = "linear"
    else:
        if out_classes == 1:
            out_activation = "sigmoid"
        else:
            out_activation = "softmax"
    output = layers.Activation(out_activation, name='final_out')(output)

    model = models.Model(inputs=[base_in], outputs=[output], name='vgg19-unet')

    return model
示例#6
0
def conv_block(filters, kernal, padding='same', stride=1, activation=None, add_bn=True, drop_rate=0):
    block = [layers.Conv2D(filters, kernal, activation=None, padding='same', strides=stride)]
    if add_bn:
        block.append(layers.BatchNormalization())
    if activation is not None:
        block.append(activation)
    if drop_rate > 0:
        block.append(layers.SpatialDropout2D(rate=drop_rate))
    return models.Sequential(block)
示例#7
0
def _conv_block(filters, kernel_size, x):
    x = ly.Conv2D(
        filters,
        kernel_size,
        kernel_constraint=tf.keras.constraints.max_norm(max_value=3.36))(x)
    x = ly.SpatialDropout2D(0.25)(x)
    x = ly.BatchNormalization(axis=-1)(x)
    x = ly.Activation("relu")(x)
    x = ly.MaxPooling2D()(x)
    return x
    def __init__(self,
                 num_channels,
                 use_2d=True,
                 num_conv_layers=2,
                 kernel_size=3,
                 activation='relu',
                 use_batchnorm=False,
                 use_bias=True,
                 use_dropout=False,
                 dropout_rate=0.25,
                 use_spatial_dropout=True,
                 data_format='channels_last',
                 name="convolution_block",
                 **kwargs):

        super(Conv_Block, self).__init__(name=name)

        for _ in range(num_conv_layers):
            if use_2d:
                self.add(
                    tfkl.Conv2D(num_channels,
                                kernel_size,
                                padding='same',
                                use_bias=use_bias,
                                data_format=data_format))
            else:
                self.add(
                    tfkl.Conv3D(num_channels,
                                kernel_size,
                                padding='same',
                                use_bias=use_bias,
                                data_format=data_format))
            if use_batchnorm:
                self.add(
                    tfkl.BatchNormalization(
                        axis=-1 if data_format == 'channels_last' else 1,
                        momentum=0.95,
                        epsilon=0.001))
            if activation == 'prelu':
                self.add(tfkl.PReLU())
            else:
                self.add(tfkl.Activation(activation))

        if use_dropout:
            if use_spatial_dropout:
                if use_2d:
                    self.add(tfkl.SpatialDropout2D(rate=dropout_rate))
                else:
                    self.add(tfkl.SpatialDropout3D(rate=dropout_rate))
            else:
                self.add(tfkl.Dropout(rate=dropout_rate))
示例#9
0
def build_psp(
    backbone,
    psp_layer_idx,
    pooling_type='avg',
    conv_filters=512,
    use_batchnorm=True,
    final_upsampling_factor=8,
    classes=21,
    activation='softmax',
    dropout=None,
):
    input_ = backbone.input
    x = (backbone.get_layer(name=psp_layer_idx).output if isinstance(
        psp_layer_idx, str) else backbone.get_layer(
            index=psp_layer_idx).output)
    # x = (get_layer_number(backbone, psp_layer_idx) if isinstance(psp_layer_idx, str) else psp_layer_idx)

    # build spatial pyramid
    x1 = SpatialContextBlock(1, conv_filters, pooling_type, use_batchnorm)(x)
    x2 = SpatialContextBlock(2, conv_filters, pooling_type, use_batchnorm)(x)
    x3 = SpatialContextBlock(3, conv_filters, pooling_type, use_batchnorm)(x)
    x6 = SpatialContextBlock(6, conv_filters, pooling_type, use_batchnorm)(x)

    # aggregate spatial pyramid
    concat_axis = 3 if backend.image_data_format() == 'channels_last' else 1
    x = layers.Concatenate(axis=concat_axis,
                           name='psp_concat')([x, x1, x2, x3, x6])
    x = Conv1x1BnReLU(conv_filters, use_batchnorm, name='aggregation')(x)

    # model regularization
    if dropout is not None:
        x = layers.SpatialDropout2D(dropout, name='spatial_dropout')(x)

    # model head
    x = layers.Conv2D(
        filters=classes,
        kernel_size=(3, 3),
        padding='same',
        kernel_initializer='glorot_uniform',
        name='final_conv',
    )(x)

    x = layers.UpSampling2D(final_upsampling_factor,
                            name='final_upsampling',
                            interpolation='bilinear')(x)
    if activation in {'softmax', 'sigmoid'}:
        x = layers.Activation(activation, name=activation)(x)

    model = models.Model(input_, x)

    return model
    def get_discriminator_block(out_filters, bn=True):
        model = keras.Sequential()
        model.add(
            layers.Conv2D(filters=out_filters,
                          kernel_size=3,
                          strides=2,
                          padding='same'))
        model.add(layers.LeakyReLU())
        model.add(layers.SpatialDropout2D(0.25))

        if bn:
            model.add(layers.BatchNormalization())

        return model
示例#11
0
文件: model.py 项目: Anuj040/cifar
    def encoder(self, features=[8], name="encoder") -> KM.Model:
        """Creates an encoder model object

        Args:
            features (list, optional): list of features in successive hidden layers. Defaults to [8].
            name (str, optional): name for the model object. Defaults to "encoder".

        Returns:
            KM.Model: Encoder model
        """
        input_tensor = KL.Input(
            shape=(32, 32, 3))  # shape of images for cifar10 dataset
        encoded = KL.Conv2D(
            features[0],
            3,
            strides=(2, 2),
            padding="same",
            use_bias=False,
            name=name + f"_conv_{1}",
        )(input_tensor)
        encoded = KL.Activation("relu")(KL.BatchNormalization()(encoded))
        encoded_list = [encoded]

        # Prepare the skip tensor from input
        skip_input_tensor = KL.Activation("relu")(
            KL.BatchNormalization()(KL.Conv2D(features[0],
                                              1,
                                              strides=1,
                                              use_bias=False)(input_tensor)))
        skip_input_tensor = KL.SpatialDropout2D(rate=0.2)(skip_input_tensor)
        skip_input_tensor = KL.AveragePooling2D(pool_size=(2, 2),
                                                strides=2)(skip_input_tensor)
        skip_tensors = tf.concat(
            [
                skip_input_tensor,  # Routing info from input tensor to next levels
                encoded,  # Routes info from second level to next levels
            ],
            axis=-1,
        )
        for i, feature_num in enumerate(features[1:], start=2):
            encoded, skip_tensors = conv_block(
                encoded,
                skip_tensors,
                features_in=features[i - 2],
                features_out=feature_num,
                name=name + f"_conv_{i}",
            )
            encoded_list.append(encoded)
        return KM.Model(inputs=input_tensor, outputs=encoded_list, name=name)
示例#12
0
def keras_dropout(layer, rate):
    """
    Keras dropout layer.
    """

    from keras import layers

    input_dim = len(layer.input.shape)
    if input_dim == 2:
        return layers.SpatialDropout1D(rate)
    elif input_dim == 3:
        return layers.SpatialDropout2D(rate)
    elif input_dim == 4:
        return layers.SpatialDropout3D(rate)
    else:
        return layers.Dropout(rate)
示例#13
0
def conv_block(inputs, filters, spatial_dropout=0.0, max_pool=True):

    x = layers.Conv2D(filters=filters,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(inputs)
    x = layers.Conv2D(filters=filters,
                      kernel_size=(3, 3),
                      padding='same',
                      activation=None)(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation('relu')(x)
    if (spatial_dropout > 0.0):
        x = layers.SpatialDropout2D(spatial_dropout)(x)
    if (max_pool == True):
        x = layers.MaxPool2D(pool_size=(2, 2))(x)

    return x
示例#14
0
    def conv_block(name,
                   filters,
                   kernel_size=4,
                   pad="same",
                   t=False,
                   act="relu",
                   bn=True):
        block = Sequential(name=name)

        if act == "relu":
            block.add(L.ReLU())
        elif act == "leaky_relu":
            block.add(L.LeakyReLU(0.2))

        if not t:
            block.add(
                L.Conv2D(
                    filters,
                    kernel_size=kernel_size,
                    strides=(2, 2),
                    padding=pad,
                    use_bias=True,
                    activation=None,
                    kernel_initializer=RandomNormal(0.0, 0.2),
                ))
        else:
            block.add(L.UpSampling2D(interpolation="bilinear"))
            block.add(
                L.Conv2DTranspose(
                    filters=filters,
                    kernel_size=kernel_size - 1,
                    padding=pad,
                    activation=None,
                ))

        if dropout > 0:
            block.add(L.SpatialDropout2D(dropout))

        if bn:
            block.add(
                L.BatchNormalization(axis=-1, epsilon=1e-05, momentum=0.9))

        return block
示例#15
0
def perceptual_layer(weights: float = 1.0, drop: float = 0.5) -> KM.Model:
    """
    Preceptual loss on latent features as in Johnson et al 2015 https://arxiv.org/abs/1603.08155

    Args:
        weights (float, optional): weighing for the loss. Defaults to 1.0.
        drop (float, optional): randomly drop channels for regularization. Defaults to 0.5.

    Returns:
        KM.Model: perceptual error calculating model
    """
    # pylint: disable = E1123, E1124, E1120

    # Ground truth
    input1 = KL.Input(shape=(None, None, 3))
    # Model output
    input2 = KL.Input(shape=(None, None, 3))
    # concat the two tensors above
    input_tensor = tf.concat([input1, input2], axis=0)

    # VGG19 feature extractor trained on Imagenet
    feature_extractor = tf.keras.applications.VGG19(include_top=False,
                                                    weights="imagenet")

    # input_tensor is normalized between (-1.0, 1.0)
    # extractor compatible format
    inputs = 127.5 * (input_tensor + 1.0)
    inputs = tf.keras.applications.vgg19.preprocess_input(inputs,
                                                          data_format=None)
    feature_maps = feature_extractor(inputs)

    # Extract deep features for the GT and generated image
    content, generated = tf.split(feature_maps, num_or_size_splits=2, axis=0)

    # Randomly zero-out some feature differences
    drop_features = KL.SpatialDropout2D(rate=drop)(content - generated)
    error = (weights / (1 - drop)) * K.mean(K.square(drop_features))
    return KM.Model(inputs=[input1, input2], outputs=error, name="vgg")
示例#16
0
def dilated_cnn(inputs,
                num_filters,
                max_dilation,
                dilation_rep,
                filter_size,
                dropout=0.0,
                bn_decay=0.999,
                bn_epsilon=1.0e-8,
                bn_scale=False,
                is_training=True):
    """Constructs a base dilated convolutional network.

  Args:
    inputs: Typically [batch, h, w, [3,6]] Input RGB images. Two 3-channel
      images are concatenated for stereo.
    num_filters: The number of filters for all layers.
    max_dilation: Size of the last dilation of a series.  Must be a power of
      two.fine
    dilation_rep: Number of times to repeat the last dilation.
    filter_size: kernel size for CNNs.
    dropout: >0 if dropout is to be applied.
    bn_decay: batchnorm parameter.
    bn_epsilon: batchnorm parameter.
    bn_scale: True to scale batchnorm.
    is_training: True if this function is called during training.

  Returns:
    Output of this dilated CNN.
  """
    # Progression of powers of 2: [1, 2, 4, 8 ... max_dliation].
    maxlog = int(math.log(max_dilation, 2))
    seq = [2**x for x in range(maxlog + 1)]
    seq += [1] * (dilation_rep - 1)
    net = inputs
    for i, r in enumerate([1] + seq + seq + [1]):
        # Split off head before the last dilation 1 convolutions.
        if i == (len(seq) * 2 - dilation_rep + 2):
            head = net

        fs = filter_size
        net = layers.Conv2D(num_filters,
                            fs,
                            dilation_rate=r,
                            padding='same',
                            kernel_regularizer=tf.keras.regularizers.l2(0.001),
                            use_bias=False,
                            trainable=is_training,
                            name='dil_conv_%d_%d' % (r, i))(net)
        # From https://arxiv.org/pdf/1905.05928.pdf; batchnorm then dropout
        # slim layers has decay/momentum = 0.999, not 0.99.
        # slim layers has scale=False, not scale=True.
        net = layers.BatchNormalization(scale=bn_scale,
                                        epsilon=bn_epsilon,
                                        trainable=is_training,
                                        momentum=bn_decay)(
                                            net, training=is_training)
        net = layers.LeakyReLU(alpha=0.1)(net)
        if dropout and i == len(seq):
            net = layers.SpatialDropout2D(dropout)(net, training=is_training)

    return net, head
示例#17
0
文件: model.py 项目: Anuj040/cifar
def conv_block(
    input_tensor: tf.Tensor,
    skip_tensors: List[tf.Tensor],
    features_in: int,
    features_out: int,
    name: str,
) -> Tuple[tf.Tensor, List[tf.Tensor]]:
    """Bottleneck style convolutional block with skip connections across blocks. Applies downsample convolution in lower features space.
    Followed by single kernel convolution to higher feature space. Also, applies skip connections across blocks for routing information
    across levels.

    Args:
        input_tensor (tf.Tensor): tensor to apply convolution to
        skip_tensors (List[tf.Tensor]): list of output tensors from previous blocks
        features_in (int): number of features for incoming layer
        features_out (int): number of features for outgoing layer
        name (str): name for the tensors

    Returns:
        Tuple[tf.Tensor, List[tf.Tensor]]: output tensor, list of tensors to route info to next levels
    """

    out = KL.Conv2D(
        features_in,
        1,
        strides=(1, 1),
        padding="same",
        use_bias=False,
        name=name + f"_c{1}",
    )(input_tensor)
    out = KL.Activation("relu")(KL.BatchNormalization()(out))

    out = KL.Conv2D(
        features_in,
        3,
        strides=(2, 2),
        padding="same",
        use_bias=False,
        name=name + f"_c{2}",
    )(out)
    out = KL.Activation("relu")(KL.BatchNormalization()(out))

    out = KL.Conv2D(
        features_out,
        1,
        strides=(1, 1),
        padding="same",
        use_bias=False,
        name=name + f"_c{3}",
    )(out)
    out = KL.BatchNormalization()(out)

    # Calculate skip tensor from previous levels
    skip_connection = KL.AveragePooling2D(pool_size=(2, 2),
                                          strides=2)(skip_tensors)
    out = KL.Activation("relu", name=name + "_relu")(out + skip_connection)
    out = KL.SpatialDropout2D(rate=0.2)(out)

    # skip tensor for next level
    skip_tensors = tf.concat([skip_connection, out], axis=-1)
    return out, skip_tensors
示例#18
0
def build_fpn(
    backbone,
    skip_connection_layers,
    pyramid_filters=256,
    segmentation_filters=128,
    classes=1,
    activation='sigmoid',
    use_batchnorm=True,
    aggregation='sum',
    dropout=None,
):
    input_ = backbone.input
    x = backbone.output

    # building decoder blocks with skip connections
    ls = ([
        get_layer_number(backbone, l) if isinstance(l, str) else l
        for l in skip_connection_layers
    ])
    skips = ([backbone.layers[c].output for c in ls])
    # x = (backbone.get_layer(name=psp_layer_idx).output if isinstance(psp_layer_idx, str)
    #      else backbone.get_layer(index=psp_layer_idx).output)

    # build FPN pyramid
    p5 = FPNBlock(pyramid_filters, stage=5)(x, skips[0])
    p4 = FPNBlock(pyramid_filters, stage=4)(p5, skips[1])
    p3 = FPNBlock(pyramid_filters, stage=3)(p4, skips[2])
    p2 = FPNBlock(pyramid_filters, stage=2)(p3, skips[3])

    # add segmentation head to each
    s5 = DoubleConv3x3BnReLU(segmentation_filters,
                             use_batchnorm,
                             name='segm_stage5')(p5)
    s4 = DoubleConv3x3BnReLU(segmentation_filters,
                             use_batchnorm,
                             name='segm_stage4')(p4)
    s3 = DoubleConv3x3BnReLU(segmentation_filters,
                             use_batchnorm,
                             name='segm_stage3')(p3)
    s2 = DoubleConv3x3BnReLU(segmentation_filters,
                             use_batchnorm,
                             name='segm_stage2')(p2)

    # upsampling to same resolution
    s5 = layers.UpSampling2D((8, 8),
                             interpolation='nearest',
                             name='upsampling_stage5')(s5)
    s4 = layers.UpSampling2D((4, 4),
                             interpolation='nearest',
                             name='upsampling_stage4')(s4)
    s3 = layers.UpSampling2D((2, 2),
                             interpolation='nearest',
                             name='upsampling_stage3')(s3)

    # aggregating results
    if aggregation == 'sum':
        x = layers.Add(name='aggregation_sum')([s2, s3, s4, s5])
    elif aggregation == 'concat':
        concat_axis = 3 if backend.image_data_format(
        ) == 'channels_last' else 1
        x = layers.Concatenate(axis=concat_axis,
                               name='aggregation_concat')([s2, s3, s4, s5])
    else:
        raise ValueError(
            'Aggregation parameter should be in ("sum", "concat"), '
            'got {}'.format(aggregation))

    if dropout:
        x = layers.SpatialDropout2D(dropout, name='pyramid_dropout')(x)

    # final stage
    x = Conv3x3BnReLU(segmentation_filters, use_batchnorm,
                      name='final_stage')(x)
    x = layers.UpSampling2D(size=(2, 2),
                            interpolation='bilinear',
                            name='final_upsampling')(x)

    # model head (define number of output classes)
    x = layers.Conv2D(
        filters=classes,
        kernel_size=(3, 3),
        padding='same',
        use_bias=True,
        kernel_initializer='glorot_uniform',
        name='head_conv',
    )(x)

    if activation in {'softmax', 'sigmoid'}:
        x = layers.Activation(activation, name=activation)(x)
    # x = layers.Activation(activation, name=activation)(x)

    # create keras model instance
    model = models.Model(input_, x)

    return model
示例#19
0
def get_base_model(leaky_relu_slope,
                   dropout_rate,
                   regularization_rate,
                   input_shape = (48, 48, 1),
                   n_classes = 8,
                   logits = False):

    regularization = l2(regularization_rate)

    model = keras.Sequential([
        layers.SeparableConv2D(48,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same',
                               input_shape = input_shape),
        layers.BatchNormalization(),
        layers.LeakyReLU(leaky_relu_slope),
        layers.SeparableConv2D(48,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same'),
        layers.BatchNormalization(),
        layers.MaxPooling2D((2, 2)),
        layers.SpatialDropout2D(dropout_rate),
        layers.LeakyReLU(leaky_relu_slope),

        layers.SeparableConv2D(96,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same'),
        layers.BatchNormalization(),
        layers.LeakyReLU(leaky_relu_slope),
        layers.SeparableConv2D(96,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same'),
        layers.BatchNormalization(),
        layers.MaxPooling2D((2, 2)),
        layers.SpatialDropout2D(dropout_rate),
        layers.LeakyReLU(leaky_relu_slope),

        layers.SeparableConv2D(192,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same'),
        layers.BatchNormalization(),
        layers.LeakyReLU(leaky_relu_slope),
        layers.SeparableConv2D(192,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same'),
        layers.BatchNormalization(),
        layers.MaxPooling2D((2, 2)),
        layers.SpatialDropout2D(dropout_rate),
        layers.LeakyReLU(leaky_relu_slope),

        layers.SeparableConv2D(384,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same'),
        layers.BatchNormalization(),
        layers.MaxPooling2D((2, 2)),
        layers.SpatialDropout2D(dropout_rate),
        layers.LeakyReLU(leaky_relu_slope),

        layers.SeparableConv2D(n_classes, (1, 1), padding = 'same'),
        layers.GlobalAveragePooling2D()
    ])

    if not logits:
        model.append(layers.Softmax())

    return model
示例#20
0
def keypose_model(mparams, is_training):
    """Constructs a Keras model that predicts 3D keypoints.

  Model input is left and optionally right image, modelx x modely x 3, float32.
  Values are from 0.0 to 1.0

  Args:
    mparams: ConfigParams object use_stereo - True if right image is input.
      num_filters - The number of filters for all layers. num_kp - The number of
      keypoints. ...
    is_training: True if training the model.

  Returns:
    uv: [batch, num_kp, 2] 2D locations of keypoints.
    d: [batch, num_kp] The inverse depth of keypoints.
    prob_viz: A visualization of all predicted keypoints.
    prob_vizs: A list of visualizations of each keypoint.
    kp_viz: A visualization of GT keypoints.
    img_out: The photometrically and geometrically altered image.
    rot: rotation matrix modifying input: [batch, 3, 3].
  """
    print('Mparams in keypose_model:\n', mparams)
    use_stereo = mparams.use_stereo
    num_filters = mparams.num_filters
    max_dilation = mparams.max_dilation
    dilation_rep = mparams.dilation_rep
    dropout = mparams.dropout
    num_kp = mparams.num_kp
    modelx = mparams.modelx
    modely = mparams.modely
    filter_size = mparams.filter_size
    bn_decay = mparams.batchnorm[0]
    bn_epsilon = mparams.batchnorm[1]
    bn_scale = mparams.batchnorm[2]

    # Aux params input to the model.
    offsets = keras.Input(shape=(3, ), name='offsets', dtype='float32')
    hom = keras.Input(shape=(3, 3), name='hom', dtype='float32')
    to_world = keras.Input(shape=(4, 4), name='to_world_L', dtype='float32')

    # Images input to the model.
    img_l = keras.Input(shape=(modely, modelx, 3),
                        name='img_L',
                        dtype='float32')
    img_l_norm = layers.Lambda(norm_image)(img_l)
    img_r = keras.Input(shape=(modely, modelx, 3),
                        name='img_R',
                        dtype='float32')
    if use_stereo:
        img_r_norm = layers.Lambda(norm_image)(img_r)
        img_l_norm = layers.concatenate([img_l_norm, img_r_norm])

    net, _ = dilated_cnn(img_l_norm,
                         num_filters,
                         max_dilation,
                         dilation_rep,
                         filter_size,
                         bn_decay=bn_decay,
                         bn_epsilon=bn_epsilon,
                         bn_scale=bn_scale,
                         dropout=dropout,
                         is_training=is_training)

    print('Dilation net shape:', net.shape)

    # Regression to keypoint values.
    if mparams.use_regress:
        if dropout:
            net = layers.SpatialDropout2D(dropout)(net, training=is_training)

        net = layers.Conv2D(64,
                            1,
                            kernel_regularizer=tf.keras.regularizers.l2(0.001),
                            use_bias=False,
                            padding='valid')(net)
        net = layers.BatchNormalization(scale=bn_scale,
                                        epsilon=bn_epsilon,
                                        momentum=bn_decay)(
                                            net, training=is_training)
        net = layers.LeakyReLU(alpha=0.1)(net)
        net = layers.Conv2D(64,
                            1,
                            kernel_regularizer=tf.keras.regularizers.l2(0.001),
                            use_bias=False,
                            padding='valid')(net)
        net = layers.BatchNormalization(scale=bn_scale,
                                        epsilon=bn_epsilon,
                                        momentum=bn_decay)(
                                            net, training=is_training)
        net = layers.LeakyReLU(alpha=0.1)(net)
        net = layers.Conv2D(num_kp * 3,
                            1,
                            kernel_regularizer=tf.keras.regularizers.l2(0.001),
                            padding='valid')(net)  # [batch, h, w, num_kp * 3]
        net = tf.reduce_mean(net, axis=[-3, -2])  # [batch, num_kp * 3]
        print('Regress reduce mean shape:', net.shape)

        uvd = tf.reshape(net, [-1, num_kp, 3])  # [batch, num_kp, 3]
        print('Regress uvd shape:', uvd.shape)
        prob = tf.stack([tf.zeros_like(img_l[Ellipsis, 0])] * num_kp, axis=1)
        disp_map = prob
        # [batch, num_kp, h, w]

    else:
        # The probability distribution map for keypoints.  No activation.
        prob = layers.Conv2D(
            num_kp,
            filter_size,
            dilation_rate=1,
            kernel_regularizer=tf.keras.regularizers.l2(0.001),
            padding='same')(net)

        # Disparity map.
        disp_map = layers.Conv2D(
            num_kp,
            filter_size,
            dilation_rate=1,
            kernel_regularizer=tf.keras.regularizers.l2(0.001),
            padding='same')(net)

        # [batch_size, h, w, num_kp]
        prob = layers.Permute((3, 1, 2))(prob)
        disp_map = layers.Permute((3, 1, 2))(disp_map)

        # [batch_size, num_kp, h, w]
        prob = layers.Reshape((num_kp, modely * modelx))(prob)
        prob = layers.Softmax()(prob)
        prob = layers.Reshape((num_kp, modely, modelx), name='prob')(prob)
        disp = layers.multiply([prob, disp_map])
        disp = k_reduce_sum(disp, axis=[-1, -2], name='disp_out')

        ranx, rany = meshgrid(modelx, modely)
        # Use centroid to find indices.
        sx = k_reduce_mult_sum(prob, ranx, axis=[-1, -2])
        sy = k_reduce_mult_sum(prob, rany, axis=[-1, -2])
        # uv are in normalized coords [-1, 1], uv order.
        uvd = layers.concatenate([sx, sy, disp])
        uvd = layers.Reshape((3, num_kp))(uvd)
        uvd = layers.Permute((2, 1), name='uvd')(uvd)  # [batch, num_kp, 3]

    uv_pix_raw, uv_pix, uvdw, uvdw_pos = convert_uvd_raw(
        uvd, offsets, hom, mparams)

    xyzw = project(to_world, uvdw_pos, True)  # [batch, 4, num_kp]

    model = keras.Model(inputs={
        'img_L': img_l,
        'img_R': img_r,
        'offsets': offsets,
        'hom': hom,
        'to_world_L': to_world
    },
                        outputs={
                            'uvd': uvd,
                            'uvdw': uvdw,
                            'uvdw_pos': uvdw_pos,
                            'uv_pix': uv_pix,
                            'uv_pix_raw': uv_pix_raw,
                            'xyzw': xyzw,
                            'prob': prob,
                            'disp': disp_map
                        },
                        name='keypose')

    model.summary()
    return model
示例#21
0
def get_smart_model(input,
                    leaky_relu_slope,
                    dropout_rate,
                    regularization_rate,
                    input_shape = (48, 48, 1),
                    n_classes = 8,
                    logits = False):

    regularization = l2(regularization_rate)

    x = layers.SeparableConv2D(48,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same',
                               input_shape = input_shape)(input)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)
    x = layers.SeparableConv2D(48,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.SpatialDropout2D(dropout_rate)(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)

    x = layers.SeparableConv2D(48,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)
    x = layers.SeparableConv2D(48,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.SpatialDropout2D(dropout_rate)(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)

    x = layers.SeparableConv2D(96,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)
    x = layers.SeparableConv2D(96,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.SpatialDropout2D(dropout_rate)(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)

    x = layers.SeparableConv2D(96,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)
    x = layers.SeparableConv2D(96,
                               (3, 3),
                               kernel_regularizer = regularization,
                               padding = 'same')(x)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPooling2D((2, 2))(x)
    x = layers.SpatialDropout2D(dropout_rate)(x)
    x = layers.LeakyReLU(leaky_relu_slope)(x)

    x = layers.Conv2D(n_classes, (1, 1), padding = 'same')(x)
    output = layers.GlobalAveragePooling2D()(x)

    if not logits:
        output = layers.Softmax()(output)

    model = Model(input, output)
    return model
示例#22
0
def Convolutional(input_shape=(51, 51, 1),
                  conv_layers_dimensions=(16, 32, 64, 128),
                  dense_layers_dimensions=(32, 32),
                  steps_per_pooling=1,
                  dropout=(),
                  dense_top=True,
                  number_of_outputs=3,
                  output_activation=None,
                  output_kernel_size=3,
                  loss=nd_mean_absolute_error,
                  convolution_block="convolutional",
                  pooling_block="pooling",
                  dense_block="dense",
                  **kwargs):
    """Creates and compiles a convolutional neural network.
    A convolutional network with a dense top.
    Parameters
    ----------
    input_shape : tuple of ints
        Size of the images to be analyzed.
    conv_layers_dimensions : tuple of ints
        Number of convolutions in each convolutional layer.
    dense_layers_dimensions : tuple of ints
        Number of units in each dense layer.
    dropout : tuple of float
        Adds a dropout between the convolutional layers
    number_of_outputs : int
        Number of units in the output layer.
    output_activation : str or keras activation
        The activation function of the output.
    loss : str or keras loss function
        The loss function of the network.
    layer_function : Callable[int] -> keras layer
        Function that returns a convolutional layer with convolutions
        determined by the input argument. Can be use to futher customize the network.
    Returns
    -------
    keras.models.Model
        Deep learning network
    """

    # Update layer functions
    dense_block = as_block(dense_block)
    convolution_block = as_block(convolution_block)
    pooling_block = as_block(pooling_block)

    ### INITIALIZE DEEP LEARNING NETWORK

    if isinstance(input_shape, list):
        inputs = [layers.Input(shape for shape in input_shape)]
        inputs = layers.Concatenate(axis=-1)(inputs)
    else:
        network_input = layers.Input(input_shape)
        inputs = network_input

    layer = inputs

    ### CONVOLUTIONAL BASIS
    for conv_layer_dimension in conv_layers_dimensions:

        for _ in range(steps_per_pooling):
            layer = convolution_block(conv_layer_dimension)(layer)

        if dropout:
            layer = layers.SpatialDropout2D(dropout[0])(layer)
            dropout = dropout[1:]

        # add pooling layer
        layer = pooling_block(conv_layer_dimension)(layer)

    # DENSE TOP

    if dense_top:
        layer = layers.Flatten()(layer)
        for dense_layer_dimension in dense_layers_dimensions:
            layer = dense_block(dense_layer_dimension)(layer)
        output_layer = layers.Dense(number_of_outputs,
                                    activation=output_activation)(layer)
    else:

        output_layer = layers.Conv2D(
            number_of_outputs,
            kernel_size=output_kernel_size,
            activation=output_activation,
            padding="same",
            name="output",
        )(layer)

    model = models.Model(inputs, output_layer)

    return KerasModel(model, loss=loss, **kwargs)
示例#23
0
validation_gen = validation_image_generator.flow_from_directory(
    directory=str(tmp_validation_data_dir),
    batch_size=BATCH_SIZE,
    shuffle=True,
    target_size=(IMG_HEIGHT, IMG_WIDTH),
    classes=list(validation_class_names))
epochs = 30
model = models.Sequential()
model.add(
    layers.Conv2D(32,
                  3,
                  padding='same',
                  activation='relu',
                  input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)))
model.add(layers.MaxPooling2D())
model.add(layers.SpatialDropout2D(0.1))
model.add(layers.Conv2D(32, 3, padding='same', activation='relu'))
model.add(layers.MaxPooling2D())
model.add(layers.SpatialDropout2D(0.1))
model.add(layers.Conv2D(64, 3, padding='same', activation='relu'))
model.add(layers.MaxPooling2D())
model.add(layers.SpatialDropout2D(0.1))
model.add(layers.UpSampling2D())
model.add(layers.Conv2D(128, 3, padding='same', activation='relu'))
model.add(layers.MaxPooling2D())
model.add(layers.SpatialDropout2D(0.1))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(43))
# ?, activation='softmax'
model.compile(optimizer='adam',
示例#24
0
def create_model(model_layers, config, compile=True):

    model = models.Sequential()

    for item in model_layers:
        if (item["type"] == "EMBEDDING"):
            model.add(
                layers.Embedding(input_dim=item["input_dim"],
                                 output_dim=item["output_dim"],
                                 input_length=item["input_length"]))
        elif (item["type"] == "MAX_POOLING"):
            model.add(
                layers.MaxPooling1D(pool_size=item["max_pooling_size"],
                                    strides=item["max_pooling_strides"]))
        elif (item["type"] == "CNN_1D"):
            model.add(
                layers.Conv1D(item["num_filters"],
                              item["kernel_size"],
                              activation=item["activation"]))
        elif (item["type"] == "LSTM"):
            if (item["recurrent_dropout"] > 0):
                model.add(
                    layers.LSTM(units=item["unit"],
                                recurrent_dropout=item["recurrent_dropout"]))
            else:
                model.add(layers.LSTM(units=item["units"]))
        elif (item["type"] == "BiLSTM"):
            if (item["recurrent_dropout"] > 0
                    and item["recurrent_dropout"] < 1):
                model.add(
                    layers.Bidirectional(
                        layers.LSTM(
                            units=item["units"],
                            recurrent_dropout=item["recurrent_dropout"])))
            else:
                model.add(
                    layers.Bidirectional(layers.LSTM(units=item["units"])))
        elif (item["type"] == "GRU"):
            if (item["recurrent_dropout"] > 0
                    and item["recurrent_dropout"] < 1):
                model.add(
                    layers.GRU(units=item["units"],
                               recurrent_dropout=item["recurrent_dropout"]))
            else:
                model.add(layers.GRU(units=item["units"]))
        elif (item["type"] == "BiGRU"):
            if (item["recurrent_dropout"] > 0):
                model.add(
                    layers.Bidirectional(
                        layers.GRU(
                            units=item["units"],
                            recurrent_dropout=item["recurrent_dropout"])))
            else:
                model.add(layers.Bidirectional(
                    layers.GRU(units=item["units"])))
        elif (item["type"] == "GLOBAL"):
            model.add(layers.GlobalMaxPooling1D())
        elif (item["type"] == "FLATTEN"):
            model.add(layers.Flatten())
        elif (item["type"] == "DENSE"):
            model.add(
                layers.Dense(item["neurons"], activation=item["activation"]))
        elif (item["type"] == "DROPOUT"):
            if (item["dropout"] > 0 and item["dropout"] < 1):
                model.add(layers.Dropout(rate=item["dropout"]))
        elif (item["type"] == "SPATIAL_DROPOUT_2D"):
            if (item["dropout"] > 0 and item["dropout"] < 1):
                model.add(layers.SpatialDropout2D(rate=item["dropout"]))
        else:
            continue

    # optimalizáló és veszteség függvények beállítása
    if compile == True:
        model.compile(optimizer=config["optimizer"],
                      loss=config["loss"],
                      metrics=config["metrics"])

    # modell összegzése
    if (config["verbose"] >= 1):
        model.summary()

    return model
示例#25
0
def gated_resnet(x,
                 a=None,
                 h=None,
                 conv2d=down_shifted_conv2d,
                 nonlinearity=concat_elu,
                 kernel_size=(2, 3),
                 dropout_rate=0.1,
                 **kwargs):
    """Build a single Gated Masked Conv2D module.

    Args:
    - x: a 4-Tensor with shape [batch_dim, height, width, channels]
    - a: a 4-Tensor with shape [batch_dim, height, width, channels]
        that represents features from earlier hierarchies
    - h: a 2-Tensor with shape [batch_dim, height, width, channels]
        that conditions image generation

    - conv2d: The type of convolution operation to use in this module,
        must be Keras compatible.
    - nonlinearity: The type of nonlinearity to use in this module,
        must be Keras compatible.
    - kernel_size: An integer or tuple/list of 2 integers, specifying
        the height and width of the 2D convolution window. Can be a single
        integer to specify the same value for all spatial dimensions.

    - dropout_rate: Float between 0 and 1. Fraction of the input
        units to drop.

    Returns:
    - out_x: a 4-Tensor with shape [batch_dim, height, width, channels]
    """
    filters = int(x.shape[-1])

    c = nonlinearity(x)
    c = conv2d(c, filters, kernel_size, **kwargs)

    if a is not None:
        a = nonlinearity(a)
        c = layers.add([
            c,
            layers.Conv2D(filters,
                          1,
                          padding='valid',
                          data_format='channels_last',
                          **kwargs)(a)
        ])

    c = nonlinearity(c)
    c = layers.SpatialDropout2D(dropout_rate, data_format='channels_last')(c)
    c = conv2d(c, filters * 2, kernel_size, **kwargs)

    if h is not None:
        h = nonlinearity(h)
        c = layers.add([
            c,
            layers.Conv2D(filters * 2,
                          1,
                          padding='valid',
                          data_format='channels_last',
                          **kwargs)(h)
        ])

    def split_backend(z):
        return tf.split(z, 2, axis=3)

    c_a, c_b = layers.Lambda(split_backend)(c)
    return layers.add(
        [x, layers.multiply([c_a, layers.Activation(tf.math.sigmoid)(c_b)])])
示例#26
0
def bottleneck(input_tensor,
               in_filters,
               out_filters,
               stage,
               block,
               drop_rate=0.1,
               dilation_rate=(1, 1),
               projection_ratio=4):
    """ENet bottleneck block
    :param input_tensor: input tensor
    :param in_filters: integer, the input filters of conv layers at shortcut path
    :param out_filters: integer, the out filters of conv layers at shortcut path
    :param stage: integer, current stage label, used for generating layer names
    :param block: integer, current block label, used for generating layer names
    :param drop_rate: spatial dropout rate
    :param dilation_rate: shortcut conv dilation rate
    :param projection_ratio: integer, the projection ratio of conv layers at shortcut path
    :return: bottleneck block tensor
    """

    if backend.image_data_format() == 'channels_last':
        channel_axis = -1
    else:
        channel_axis = 1

    name_base = 'stage' + str(stage) + '_' + 'block' + str(block)
    reduced_depth = in_filters // projection_ratio

    shortcut = layers.Conv2D(
        filters=reduced_depth,
        kernel_size=(1, 1),
        strides=(1, 1),
        padding='valid',
        use_bias=False,
        kernel_initializer='he_normal',
        kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
        name=name_base + '_conv_reduce')(input_tensor)
    shortcut = layers.BatchNormalization(axis=channel_axis,
                                         name=name_base +
                                         '_bn_reduce')(shortcut)
    shortcut = layers.PReLU(alpha_initializer=PRELU_ALPHA)(shortcut)

    shortcut = layers.Conv2D(
        filters=reduced_depth,
        kernel_size=(3, 3),
        strides=(1, 1),
        padding='same',
        dilation_rate=dilation_rate,
        use_bias=False,
        kernel_initializer='he_normal',
        kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
        name=name_base + '_conv')(shortcut)
    shortcut = layers.BatchNormalization(axis=channel_axis,
                                         name=name_base + '_bn')(shortcut)
    shortcut = layers.PReLU(alpha_initializer=PRELU_ALPHA)(shortcut)

    shortcut = layers.Conv2D(
        filters=out_filters,
        kernel_size=(1, 1),
        strides=(1, 1),
        padding='same',
        use_bias=False,
        kernel_initializer='he_normal',
        kernel_regularizer=regularizers.l2(L2_WEIGHT_DECAY),
        name=name_base + '_conv_expansion')(shortcut)
    shortcut = layers.BatchNormalization(axis=channel_axis,
                                         name=name_base +
                                         '_bn_expansion')(shortcut)
    shortcut = layers.SpatialDropout2D(rate=drop_rate)(shortcut)

    x = layers.add([input_tensor, shortcut])
    x = layers.PReLU(alpha_initializer=PRELU_ALPHA)(x)

    return x
示例#27
0
                  strides=(1, 1),
                  activation='elu',
                  padding='same')(x)
x = layers.Conv2D(filters=32,
                  kernel_size=(3, 3),
                  strides=(1, 1),
                  activation='elu',
                  padding='same')(x)
x = layers.Conv2D(filters=32,
                  kernel_size=(3, 3),
                  strides=(1, 1),
                  activation='elu',
                  padding='same')(x)

x = layers.BatchNormalization()(x)
x = layers.SpatialDropout2D(0.1)(x)
x = layers.MaxPooling2D(pool_size=(2, 2))(x)

x = layers.Conv2D(filters=64,
                  kernel_size=(3, 3),
                  strides=(1, 1),
                  activation='elu',
                  padding='same')(x)
x = layers.Conv2D(filters=64,
                  kernel_size=(3, 3),
                  strides=(1, 1),
                  activation='elu',
                  padding='same')(x)
x = layers.Conv2D(filters=64,
                  kernel_size=(3, 3),
                  strides=(1, 1),
示例#28
0
def advanced_CNN_model():
    (x_train, y_train), (x_test,
                         y_test) = keras.datasets.fashion_mnist.load_data()
    x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) / 255
    y_train = y_train.reshape(y_train.shape[0], 1)
    x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) / 255
    y_test = y_test.reshape(y_test.shape[0], 1)

    # our model, input again, still in an image shape
    inputs = keras.Input(shape=(
        28,
        28,
        1,
    ), name='img')
    # run pairs of conv layers, all 3s3 kernels
    x = layers.Conv2D(filters=8,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(inputs)
    x = layers.Conv2D(filters=8,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(x)
    # batch normalisation, before the non-linearity
    x = layers.BatchNormalization()(x)
    # spatial dropout, this will drop whole kernels, i.e. 20% of our 3x3 filters will be dropped out rather
    # than dropping out 20% of the invidual pixels
    x = layers.SpatialDropout2D(0.2)(x)
    # max pooling, 2x2, which will downsample the image
    x = layers.MaxPool2D(pool_size=(2, 2))(x)
    # rinse and repeat with 2D convs, batch norm, dropout and max pool
    x = layers.Conv2D(filters=16,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(x)
    x = layers.Conv2D(filters=16,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(x)
    x = layers.BatchNormalization()(x)
    x = layers.SpatialDropout2D(0.2)(x)
    x = layers.MaxPool2D(pool_size=(2, 2))(x)
    # final conv2d, batch norm and spatial dropout
    x = layers.Conv2D(filters=32,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(x)
    x = layers.Conv2D(filters=32,
                      kernel_size=(3, 3),
                      padding='same',
                      activation='relu')(x)
    x = layers.BatchNormalization()(x)
    x = layers.SpatialDropout2D(0.2)(x)
    # flatten layer
    x = layers.Flatten()(x)
    # we'll use a couple of dense layers here, mainly so that we can show what another dropout layer looks like
    # in the middle
    x = layers.Dense(256, activation='relu')(x)
    x = layers.Dropout(0.5)(x)
    x = layers.Dense(64, activation='relu')(x)
    # the output
    outputs = layers.Dense(10, activation=None)(x)

    # build the model, and print a summary
    model_cnn = keras.Model(inputs=inputs,
                            outputs=outputs,
                            name='fashion_mnist_cnn_model')
    model_cnn.summary()

    # keras.utils.plot_model(model_cnn, show_shapes=True)

    logdir = os.path.join("logs",
                          datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
    tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir,
                                                          histogram_freq=1)
    # tb_sup = TensorboardSupervisor(logdir)

    model_cnn.compile(
        loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        optimizer=keras.optimizers.RMSprop(),
        metrics=['accuracy'])
    history = model_cnn.fit(x_train,
                            y_train,
                            batch_size=64,
                            epochs=5,
                            validation_split=0.2,
                            callbacks=[tensorboard_callback])

    eval_model(model_cnn, x_test, y_test)
示例#29
0
def UNet(input_shape=(None, None, 1),
         conv_layers_dimensions=(16, 32, 64, 128),
         base_conv_layers_dimensions=(128, 128),
         output_conv_layers_dimensions=(16, 16),
         dropout=(),
         steps_per_pooling=1,
         number_of_outputs=1,
         output_kernel_size=3,
         output_activation=None,
         loss=nd_mean_absolute_error,
         encoder_convolution_block="convolutional",
         base_convolution_block="convolutional",
         decoder_convolution_block="convolutional",
         output_convolution_block="convolutional",
         pooling_block="pooling",
         upsampling_block="deconvolutional",
         **kwargs):
    """Creates and compiles a U-Net.
    Parameters
    ----------
    input_shape : tuple of ints
        Size of the images to be analyzed.
    conv_layers_dimensions : tuple of ints
        Number of convolutions in each convolutional layer during down-
        and upsampling.
    base_conv_layers_dimensions : tuple of ints
        Number of convolutions in each convolutional layer at the base
        of the unet, where the image is the most downsampled.
    output_conv_layers_dimensions : tuple of ints
        Number of convolutions in each convolutional layer after the
        upsampling.
    steps_per_pooling : int
        Number of convolutional layers between each pooling and upsampling
        step.
    number_of_outputs : int
        Number of convolutions in output layer.
    output_activation : str or keras activation
        The activation function of the output.
    loss : str or keras loss function
        The loss function of the network.
    layer_function : Callable[int] -> keras layer
        Function that returns a convolutional layer with convolutions
        determined by the input argument. Can be use to futher customize the network.
    Returns
    -------
    keras.models.Model
        Deep learning network.
    """

    # Update layer functions

    encoder_convolution_block = as_block(encoder_convolution_block)
    base_convolution_block = as_block(base_convolution_block)
    output_convolution_block = as_block(output_convolution_block)
    decoder_convolution_block = as_block(decoder_convolution_block)
    pooling_block = as_block(pooling_block)
    upsampling_block = as_block(upsampling_block)

    unet_input = layers.Input(input_shape)

    concat_layers = []

    layer = unet_input

    # Downsampling path
    for conv_layer_dimension in conv_layers_dimensions:
        for _ in range(steps_per_pooling):
            layer = encoder_convolution_block(conv_layer_dimension)(layer)
        concat_layers.append(layer)

        if dropout:
            layer = layers.SpatialDropout2D(dropout[0])(layer)
            dropout = dropout[1:]

        layer = pooling_block(conv_layer_dimension)(layer)

    # Bottleneck path
    for conv_layer_dimension in base_conv_layers_dimensions:
        layer = base_convolution_block(conv_layer_dimension)(layer)

    # Upsampling path
    for conv_layer_dimension, concat_layer in zip(
            reversed(conv_layers_dimensions), reversed(concat_layers)):

        layer = upsampling_block(conv_layer_dimension)(layer)
        layer = layers.Concatenate(axis=-1)([layer, concat_layer])

        for _ in range(steps_per_pooling):
            layer = decoder_convolution_block(conv_layer_dimension)(layer)

    # Output step
    for conv_layer_dimension in output_conv_layers_dimensions:
        layer = output_convolution_block(conv_layer_dimension)(layer)

    output_layer = layers.Conv2D(
        number_of_outputs,
        kernel_size=output_kernel_size,
        activation=output_activation,
        padding="same",
    )(layer)

    model = models.Model(unet_input, output_layer)

    return KerasModel(model, loss=loss, **kwargs)
示例#30
0
def create_cnn_model_2(learning_rate=0.00002,
                       nr_classes=1108,
                       input_shape=(6, 224, 224)):
    """
    CNN model based on latest archidecture in prototype
    """

    model = keras.Sequential([
        keras.Input(shape=input_shape),
        layers.Permute((3, 2, 1)),
        layers.Conv2D(
            64,
            kernel_size=(1, 1),
            activation="relu",
            padding="same",
            input_shape=(6, 224, 224),
            data_format="channels_first",
        ),
        layers.Conv2D(
            64,
            kernel_size=(4, 4),
            activation="relu",
            padding="same",
            data_format="channels_first",
        ),
        layers.MaxPooling2D(pool_size=(2, 2), data_format="channels_first"),
        layers.Conv2D(
            128,
            kernel_size=(3, 3),
            activation="relu",
            padding="same",
            data_format="channels_first",
        ),
        layers.Conv2D(
            128,
            kernel_size=(3, 3),
            activation="relu",
            padding="same",
            data_format="channels_first",
        ),
        layers.AveragePooling2D(pool_size=(2, 2),
                                data_format="channels_first"),
        layers.Conv2D(
            256,
            kernel_size=(3, 3),
            activation="relu",
            padding="same",
            data_format="channels_first",
        ),
        # layers.Conv2D(256, kernel_size=(3, 3), activation="relu", padding="same"),
        layers.AveragePooling2D(pool_size=(3, 3),
                                data_format="channels_first"),
        layers.SpatialDropout2D(0.5, data_format="channels_first"),
        layers.Flatten(),
        layers.Dense(
            512,
            activation="relu",
            kernel_initializer="he_uniform",
            kernel_regularizer="l2",
        ),
        layers.Dropout(0.5),
        layers.Dense(
            256,
            activation="relu",
            kernel_initializer="he_uniform",
            kernel_regularizer="l2",
        ),
        layers.Dropout(0.5),
        layers.Dense(128, activation="relu"),
        layers.Dropout(0.5),
        layers.Dense(nr_classes, activation="softmax"),
    ])
    model.compile(
        loss="sparse_categorical_crossentropy",
        optimizer=Adam(learning_rate),
        metrics=["accuracy"],
    )
    model.summary()

    return model