Ejemplo n.º 1
0
def u_net(input_dim,
          target_dim,
          pool,
          strides=1,
          skips=True,
          skip_type='concat',
          batch_norm=False,
          dropout=0.5,
          dropout_allLayers=False,
          layer_activation='relu',
          out_activation='softmax',
          filterdims=[],
          filterdims_out=3,
          latentdims=[64, 3],
          prefilterdims=[],
          separable_convs=False,
          l2_pen=0,
          per_image_standardization=False):
    """U-net model

    Args:
        input_dim :      dimensions of the input data (x,x)
        target_dim:      dimensions of the target data (x,x)
        strides:         stride of the first convolution per conv block in encoder; 
                         & stride of the last convolution per conv block in decoder;
        pool:            maxpool dimension
        skips:           True or false; use skip connections or not
        skip_type:       'sum' or 'concat'
        batch_norm:      True or False; apply batch normalization
        dropout:         dropout on the fully connected layers
        dropout_allLayers: use dropout for each layer block or only the latent dimension
        layer_activation: type of activation between conv and batch_norm (e.g. 'relu', 'LeakyReLU')
        out_activation:  type of activation at the output (e.g. 'sigmoid', or None)
        filterdims:      filter dimensionality matrix 
                         (e.g. for 3 layers: [[Nfeat1,dim1],[Nfeat2,dim2],[Nfeat3,dim3]])
        filterdims_out:  kernel dimensions of the filter at the output layer
        latent_dims:     latent filter dimensions
        prefilterdims:   optional set of filters before the encoder
        separable_convs: use depthwise separable convolutions rather than regular convs [Xception: Deep Learning with Depthwise Separable Convolutions] 
        l2_pen:          l2 penalty on the convolutional weights
        per_image_standardization: normalize input images to zero mean unity variance
        
    Returns:
        keras model
    """

    inputs = Input(shape=input_dim)

    input_layer = inputs

    # make input dimensions compatible with the network; i.e. add a channel dim if neccesary
    if len(np.shape(input_layer)) < 4:
        input_layer = Lambda(lambda x: K.expand_dims(x))(input_layer)

    if per_image_standardization == True:
        input_layer = Lambda(
            standardization,
            output_shape=standardization_output_shape)(input_layer)

    if filterdims == []:
        filterdims = [[16, 3], [32, 5], [64, 5]]

    if len(target_dim) < 3:
        n_classes = 1
    else:
        n_classes = target_dim[-1]

    last_layer = input_layer
    """
    =============================================================================
        PREFILTER LAYER
    =============================================================================
    """
    for i in range(0, np.size(prefilterdims, 0)):
        if separable_convs:
            conv_pre = SeparableConv2D(
                prefilterdims[i][0],
                prefilterdims[i][1],
                activation=None,
                padding='same',
                kernel_initializer='he_normal',
                kernel_regularizer=regularizers.l2(l2_pen),
                name='preconv{}'.format(i))
        else:
            conv_pre = Conv2D(prefilterdims[i][0],
                              prefilterdims[i][1],
                              activation=None,
                              padding='same',
                              kernel_initializer='he_normal',
                              kernel_regularizer=regularizers.l2(l2_pen),
                              name='preconv{}'.format(i))

        conv = last_layer

        if batch_norm == True:
            conv = BatchNormalization()(conv)

        conv = conv_pre(conv)

        print("conv shape :", conv.shape)

        if layer_activation == 'LeakyReLU':
            conv = LeakyReLU(alpha=0.1)(conv)
        else:
            conv = Activation(layer_activation)(conv)

        if dropout_allLayers and dropout > 0:
            conv = Dropout(dropout)(conv)
            print("dropout layer")

        last_layer = conv
    """
    =============================================================================
        ENCODER
    =============================================================================
    """
    convs = []
    convs_a = []
    convs_b = []
    pools = []
    for i in range(0, np.size(filterdims, 0)):
        if separable_convs:
            conv_a = SeparableConv2D(
                filterdims[i][0],
                filterdims[i][1],
                strides=strides,
                activation=None,
                padding='same',
                kernel_initializer='he_normal',
                kernel_regularizer=regularizers.l2(l2_pen),
                name='conv{}a'.format(i))
            conv_b = SeparableConv2D(
                filterdims[i][0],
                filterdims[i][1],
                activation=None,
                padding='same',
                kernel_initializer='he_normal',
                kernel_regularizer=regularizers.l2(l2_pen),
                name='conv{}b'.format(i))
        else:
            conv_a = Conv2D(filterdims[i][0],
                            filterdims[i][1],
                            strides=strides,
                            activation=None,
                            padding='same',
                            kernel_initializer='he_normal',
                            kernel_regularizer=regularizers.l2(l2_pen),
                            name='conv{}a'.format(i))
            conv_b = Conv2D(filterdims[i][0],
                            filterdims[i][1],
                            activation=None,
                            padding='same',
                            kernel_initializer='he_normal',
                            kernel_regularizer=regularizers.l2(l2_pen),
                            name='conv{}b'.format(i))

        conv = last_layer
        if batch_norm == True:
            conv = BatchNormalization()(conv)

        conv = conv_a(conv)

        print("conv shape :", conv.shape)

        if layer_activation == 'LeakyReLU':
            conv = LeakyReLU(alpha=0.1)(conv)
        else:
            conv = Activation(layer_activation)(conv)

        if batch_norm == True:
            conv = BatchNormalization()(conv)

        conv = conv_b(conv)
        print("conv shape :", conv.shape)

        if layer_activation == 'LeakyReLU':
            conv = LeakyReLU(alpha=0.1)(conv)
        else:
            conv = Activation(layer_activation)(conv)

        convs.append(conv)
        pools.append(
            MaxPooling2D(pool_size=pool[i], name='maxpool{}'.format(i))(conv))
        print("pool shape :", pools[i].shape)

        last_layer = pools[-1]

        if dropout_allLayers and dropout > 0:
            last_layer = Dropout(dropout)(last_layer)
            print("dropout layer")
    """
    =============================================================================
        LATENT LAYER
    =============================================================================
    """
    if len(latentdims) == 2:
        if separable_convs:
            conv_latent = SeparableConv2D(
                latentdims[0],
                latentdims[1],
                activation=None,
                padding='same',
                kernel_initializer='he_normal',
                kernel_regularizer=regularizers.l2(l2_pen),
                name='Conv1latent_space')(last_layer)
        else:
            conv_latent = Conv2D(latentdims[0],
                                 latentdims[1],
                                 activation=None,
                                 padding='same',
                                 kernel_initializer='he_normal',
                                 kernel_regularizer=regularizers.l2(l2_pen),
                                 name='Conv1latent_space')(last_layer)

            print("conv shape :", conv_latent.shape)

        if layer_activation == 'LeakyReLU':
            conv_latent = LeakyReLU(alpha=0.1)(conv_latent)
        else:
            conv_latent = Activation(layer_activation)(conv_latent)

        if dropout > 0:
            conv_latent = Dropout(dropout)(conv_latent)
            print("dropout layer")

        if separable_convs:
            conv_latent = SeparableConv2D(
                latentdims[0],
                latentdims[1],
                activation=None,
                padding='same',
                kernel_initializer='he_normal',
                kernel_regularizer=regularizers.l2(l2_pen),
                name='Conv2latent_space')(conv_latent)
        else:
            conv_latent = Conv2D(latentdims[0],
                                 latentdims[1],
                                 activation=None,
                                 padding='same',
                                 kernel_initializer='he_normal',
                                 kernel_regularizer=regularizers.l2(l2_pen),
                                 name='Conv2latent_space')(conv_latent)

        print("conv shape :", conv_latent.shape)

        if layer_activation == 'LeakyReLU':
            conv_latent = LeakyReLU(alpha=0.1)(conv_latent)
        else:
            conv_latent = Activation(layer_activation)(conv_latent)

    else:
        conv_latent = last_layer
        print("skipping latent layer..")

        if dropout > 0:
            conv_latent = Dropout(dropout)(conv_latent)
            print("dropout layer")

    last_layer = conv_latent
    """
    =============================================================================
        DECODER
    =============================================================================
    """
    filterdims = filterdims[::-1]
    for i in range(0, np.size(filterdims, 0)):
        # 'learned' upsampling (nearest neighbor interpolation with 2x2, followed by conv of 2x2 )
        #        up = Conv2DTranspose(filterdims[i][0], 2, activation = None, padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(last_layer))
        up = UpSampling2D(name='upsample{}'.format(i),
                          size=pool[-i - 1])(last_layer)
        print("up shape   :", up.shape)

        if skips == True:
            # Skip connections
            if skip_type == 'concat':
                merged = concatenate([convs[-1 - i], up], 3)
            else:
                merged = Add(name='Skip-connection{}'.format(i))(
                    [convs[-1 - i], up])
        else:
            merged = up

        print("merge shape:", merged.shape)

        if batch_norm == True:
            conv = BatchNormalization()(conv)

        shape_in = merged.shape.as_list()

        layer = Conv2DTranspose(filterdims[i][0],
                                filterdims[i][1],
                                activation=None,
                                padding='same',
                                kernel_initializer='he_normal',
                                kernel_regularizer=regularizers.l2(l2_pen),
                                name='deconv{}a'.format(i))
        conv = layer(merged)

        shape_out = layer.compute_output_shape(shape_in)
        conv.set_shape(shape_out)

        print("conv shape :", conv.shape)

        if layer_activation == 'LeakyReLU':
            conv = LeakyReLU(alpha=0.1)(conv)
        else:
            conv = Activation(layer_activation)(conv)

        if batch_norm == True:
            conv = BatchNormalization()(conv)

        if i < np.size(filterdims, 0) - 1:
            shape_in = merged.shape.as_list()

            layer = Conv2DTranspose(filterdims[i + 1][0],
                                    filterdims[i + 1][1],
                                    strides=strides,
                                    activation=None,
                                    padding='same',
                                    kernel_initializer='he_normal',
                                    kernel_regularizer=regularizers.l2(l2_pen),
                                    name='deconv{}b'.format(i))
            conv = layer(conv)

            shape_out = layer.compute_output_shape(shape_in)
            conv.set_shape(shape_out)

            if layer_activation == 'LeakyReLU':
                conv = LeakyReLU(alpha=0.1)(conv)
            else:
                conv = Activation(layer_activation)(conv)

            print("conv shape :", conv.shape)
            last_layer = conv

            if dropout_allLayers and dropout > 0:
                last_layer = Dropout(dropout)(last_layer)
                print("dropout layer")

        else:  # last layer:
            conv = Conv2DTranspose(filterdims[i][0],
                                   filterdims[i][1],
                                   activation=None,
                                   strides=strides,
                                   padding='same',
                                   kernel_initializer='he_normal',
                                   kernel_regularizer=regularizers.l2(l2_pen),
                                   name='deconv{}b'.format(i))(conv)
            if layer_activation == 'LeakyReLU':
                conv = LeakyReLU(alpha=0.1)(conv)
            else:
                conv = Activation(layer_activation)(conv)

            conv = Conv2DTranspose(filterdims[i][0],
                                   filterdims[i][1],
                                   activation=None,
                                   padding='same',
                                   kernel_initializer='he_normal',
                                   kernel_regularizer=regularizers.l2(l2_pen),
                                   name='deconv{}c'.format(i))(conv)
            if layer_activation == 'LeakyReLU':
                conv = LeakyReLU(alpha=0.1)(conv)
            else:
                conv = Activation(layer_activation)(conv)

            final_layer = Conv2D(n_classes,
                                 filterdims_out,
                                 activation=out_activation,
                                 padding='same',
                                 kernel_initializer='he_normal',
                                 kernel_regularizer=regularizers.l2(l2_pen),
                                 name='Final_conv')(conv)
            print("conv shape :", conv.shape)

            final_layer = Reshape(target_dim)(final_layer)

    model = Model(inputs=inputs, outputs=final_layer)

    return model
Ejemplo n.º 2
0
def create_classifier(source_layers, num_priors, normalizations, num_classes=21, classifier_times=3):
    mbox_conf = []
    mbox_loc = []
    for i, layer in enumerate(source_layers):
        # source_layers
        # name='block3b_add/add_1:0  shape=(batch, 38, 38, 40)
        # name='block5c_add/add_1:0 shape=(batch, 19, 19, 112)
        # name='block7a_project_bn/cond_1/Identity:0' shape=(batch, 10, 10, 320)
        # name='activation_1/Relu:0' shape=(batch, 5, 5, 256)
        # name='activation_3/Relu:0' shape=(batch, 3, 3, 256)
        # name='activation_5/Relu:0' shape=(batch, 1, 1, 256)
        x = layer
        # name = x.name.split('/')[0] # name만 추출 (ex: block3b_add)
        name = x.name.split(':')[0] # name만 추출 (ex: block3b_add)

        # <<< reduce norm
        if normalizations is not None and normalizations[i] > 0:
           x = Normalize(normalizations[i], name=name + '_norm')(x)
           #print('norm_feature : '+x.name)

        # x = activation_5/Relu:0, shape=(Batch, 1, 1, 256)
        # print("start_multibox_head.py")
        # print("num_priors[i]",num_priors[i]) #6 (첫 번째 38,38일 경우)
        # print("num_classes",num_classes) #21
        # print("num_priors[i] * num_classes",num_priors[i] * num_classes) # 126

        ## original ----
        # x1 = Conv2D(num_priors[i] * num_classes, 3, padding='same', kernel_regularizer=l2(5e-4) ,name= name + '_mbox_conf')(x)
        # x1 = SeparableConv2D(num_priors[i] * num_classes, 3, padding='same', use_bias=False, kernel_regularizer=l2(5e-4), name= name + '_mbox_conf')(x)
        x1 = SeparableConv2D(num_priors[i] * num_classes, 3, padding='same',
                             depthwise_initializer=initializers.VarianceScaling(),
                             pointwise_initializer=initializers.VarianceScaling(),
                             name= name + '_mbox_conf_1')(x)

        for cls_times in range(classifier_times-1):
            #print('cls_times', cls_times)
            x1 = SeparableConv2D(num_priors[i] * num_classes, 3, padding='same',
                                 depthwise_initializer=initializers.VarianceScaling(),
                                 pointwise_initializer=initializers.VarianceScaling(),
                                 name= name + '_mbox_conf_'+str(cls_times+2))(x1)

        x1 = Flatten(name=name + '_mbox_conf_flat')(x1)


        # x1 = activation_b5_mbox_conf_flat/Reshape:0 , shape=(Batch , 84)
        mbox_conf.append(x1)

        # x2 = Conv2D(num_priors[i] * 4, 3, padding='same', kernel_regularizer=l2(5e-4) ,name= name + '_mbox_loc')(x)
        # x2 = SeparableConv2D(num_priors[i] * 4, 3, padding='same', use_bias=False, kernel_regularizer=l2(5e-4),name= name + '_mbox_loc')(x)
        x2 = SeparableConv2D(num_priors[i] * 4, 3, padding='same',
                             depthwise_initializer=initializers.VarianceScaling(),
                             pointwise_initializer=initializers.VarianceScaling(),
                             name= name + '_mbox_loc_1')(x)

        for loc_times in range(classifier_times - 1):
            x2 = SeparableConv2D(num_priors[i] * 4, 3, padding='same',
                                 depthwise_initializer=initializers.VarianceScaling(),
                                 pointwise_initializer=initializers.VarianceScaling(),
                                 name= name + '_mbox_loc_'+str(loc_times+2))(x2)

        x2 = Flatten(name=name + '_mbox_loc_flat')(x2)
        # x2 = activation_b5_mbox_loc_flat/Reshape:0 , shape=(Batch , 16)
        mbox_loc.append(x2)

    # mbox_loc/concat:0 , shape=(Batch, 34928)
    mbox_loc = Concatenate(axis=1, name='mbox_loc')(mbox_loc)
    # mbox_loc_final/Reshape:0, shape=(Batch, 8732, 4)
    mbox_loc = Reshape((-1, 4), name='mbox_loc_final')(mbox_loc)

    # mobx_conf/concat:0, shape=(Batch, 183372)
    mbox_conf = Concatenate(axis=1, name='mbox_conf')(mbox_conf)
    # mbox_conf_logits/Reshape:0, shape=(None, 8732, 21)
    mbox_conf = Reshape((-1, num_classes), name='mbox_conf_logits')(mbox_conf)
    # mbox_conf = Activation('softmax', name='mbox_conf_final')(mbox_conf)

    # predictions/concat:0, shape=(Batch, 8732, 25)
    predictions = Concatenate(axis=2, name='predictions', dtype=tf.float32)([mbox_loc, mbox_conf])

    return predictions
Ejemplo n.º 3
0
print(y_test.shape)

callbacks = [
    EarlyStopping(monitor='val_accuracy',
                  min_delta=1e-2,
                  patience=10,
                  verbose=1)
]

# Create a sequential keras model
model = Sequential()
model.add(
    SeparableConv2D(filters=8,
                    kernel_size=3,
                    padding="same",
                    activation="relu",
                    input_shape=(50, 50, 3)))
model.add(MaxPooling2D(pool_size=2))
model.add(
    SeparableConv2D(filters=16,
                    kernel_size=3,
                    padding="same",
                    activation="relu"))
model.add(MaxPooling2D(pool_size=2))
model.add(
    SeparableConv2D(filters=32,
                    kernel_size=3,
                    padding="same",
                    activation="relu"))
model.add(MaxPooling2D(pool_size=2))
Ejemplo n.º 4
0
def create_modified_pixor_pp_v1(
        input_shape=(800, 700, 35), kernel_regularizer=None,
        downsample_factor=4):
    '''
        This architecture key differences:
            1- Using separable convolutions in some deep layers
            2- Uses large kernel sizes on the first blocks, and larger on deeper blocks
            3- Uses 1x1 conolution in the output layer
            4- Using strided convolution instead of maxpooling to downsize the spatial dimensions
            
        # Params: 3,969,031
    '''
    K.clear_session()

    KERNEL_REG = kernel_regularizer
    KERNEL_SIZE = {
        "Block1": 7,
        "Block2": 5,
        "Block3": 3,
        "Block4": 3,
        "Header": 3,
        "Out": 1
    }
    PADDING = 'same'
    FILTERS = 256
    MAXPOOL_SIZE = 2
    MAXPOOL_STRIDES = None
    DECONV_STRIDES = 2
    CLASS_CHANNELS = 1
    REGRESS_CHANNELS = 6

    inp = Input(shape=input_shape)
    x = inp

    with tf.name_scope("Backbone"):
        with tf.name_scope("Block1"):
            x = Conv2D(filters=FILTERS // 16,
                       kernel_size=KERNEL_SIZE['Block1'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = Conv2D(filters=FILTERS // 8,
                       kernel_size=KERNEL_SIZE['Block1'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = Conv2D(filters=FILTERS // 8,
                       kernel_size=KERNEL_SIZE['Block1'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            block1_out = x
            block1_out = AveragePooling2D(pool_size=MAXPOOL_SIZE)(block1_out)
            block1_out = AveragePooling2D(pool_size=MAXPOOL_SIZE)(block1_out)
#             print("Block 1 output shape: " + str(block1_out.shape))

        with tf.name_scope("Block2"):
            x = Conv2D(filters=FILTERS // 8,
                       kernel_size=KERNEL_SIZE['Block2'],
                       padding=PADDING,
                       strides=2,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = Conv2D(filters=FILTERS // 4,
                       kernel_size=KERNEL_SIZE['Block2'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = Conv2D(filters=FILTERS // 4,
                       kernel_size=KERNEL_SIZE['Block2'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            block2_out = x
            block2_out = AveragePooling2D(pool_size=MAXPOOL_SIZE)(block2_out)
#             print("Block 2 output shape: " + str(block2_out.shape))

        with tf.name_scope("Block3"):
            x = Conv2D(filters=FILTERS // 4,
                       kernel_size=KERNEL_SIZE['Block3'],
                       padding=PADDING,
                       strides=2,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 2,
                                kernel_size=KERNEL_SIZE['Block3'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 2,
                                kernel_size=KERNEL_SIZE['Block3'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = Conv2D(filters=FILTERS // 2,
                       kernel_size=KERNEL_SIZE['Block3'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            block3_out = x
#             print("Block 3 output shape: " + str(block3_out.shape))

        with tf.name_scope("Block4"):
            x = Conv2D(filters=FILTERS // 2,
                       kernel_size=KERNEL_SIZE['Block4'],
                       padding=PADDING,
                       strides=2,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 1,
                                kernel_size=KERNEL_SIZE['Block4'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 1,
                                kernel_size=KERNEL_SIZE['Block4'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 1,
                                kernel_size=KERNEL_SIZE['Block4'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 1,
                                kernel_size=KERNEL_SIZE['Block4'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = SeparableConv2D(filters=FILTERS // 1,
                                kernel_size=KERNEL_SIZE['Block4'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            x = Conv2D(filters=FILTERS // 1,
                       kernel_size=KERNEL_SIZE['Block4'],
                       padding=PADDING,
                       kernel_regularizer=KERNEL_REG)(x)
            x = BatchNormalization()(x)
            x = ReLU()(x)
            block4_out = x
            block4_out = tf.image.resize(
                block4_out,
                size=(input_shape[0] // downsample_factor,
                      input_shape[1] // downsample_factor))


#             print("Block 4 output shape: " + str(block4_out.shape))

    concat = Concatenate(axis=-1)(
        [block1_out, block2_out, block3_out, block4_out])

    with tf.name_scope("Header"):
        x = concat
        x = Conv2D(filters=FILTERS,
                   kernel_size=KERNEL_SIZE['Header'],
                   padding=PADDING,
                   kernel_regularizer=KERNEL_REG)(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)
        x = Conv2D(filters=FILTERS,
                   kernel_size=KERNEL_SIZE['Header'],
                   padding=PADDING,
                   kernel_regularizer=KERNEL_REG)(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)
        x = SeparableConv2D(filters=FILTERS,
                            kernel_size=KERNEL_SIZE['Header'],
                            padding=PADDING,
                            kernel_regularizer=KERNEL_REG)(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)
        x = SeparableConv2D(filters=FILTERS,
                            kernel_size=KERNEL_SIZE['Header'],
                            padding=PADDING,
                            kernel_regularizer=KERNEL_REG)(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)
        x = Conv2D(filters=FILTERS,
                   kernel_size=KERNEL_SIZE['Header'],
                   padding=PADDING,
                   kernel_regularizer=KERNEL_REG)(x)
        x = BatchNormalization()(x)
        x = ReLU()(x)

    with tf.name_scope("Out"):
        objectness_map = Conv2D(CLASS_CHANNELS,
                                kernel_size=KERNEL_SIZE['Out'],
                                padding=PADDING,
                                kernel_regularizer=KERNEL_REG,
                                activation='sigmoid',
                                name='objectness_map')(x)
        geometric_map = Conv2D(REGRESS_CHANNELS,
                               kernel_size=KERNEL_SIZE['Out'],
                               padding=PADDING,
                               kernel_regularizer=KERNEL_REG,
                               name='geometric_map')(x)

    output_map = Concatenate(
        axis=-1, name='output_map')([objectness_map, geometric_map])
    model = Model(inp, output_map)
    return model
Ejemplo n.º 5
0
def create_feature_extactor(config: Dict):
    """
    Create the feature extractor based on pretrained existing keras models.
    :param config: dict holding the model and data config
    :return: feature extractor model
    """
    input_shape = (config["data"]["image_target_size"][0],
                   config["data"]["image_target_size"][1], 3)

    feature_extractor_type = config["model"]["feature_extractor"]["type"]

    weights = "imagenet"
    feature_extractor = Sequential(name='feature_extractor')
    if feature_extractor_type == "mobilenetv2":
        feature_extractor.add(
            MobileNetV2(include_top=False,
                        input_shape=input_shape,
                        weights=None,
                        pooling='avg'))
    elif feature_extractor_type == "efficientnetb0":
        feature_extractor.add(
            EfficientNetB0(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb1":
        feature_extractor.add(
            EfficientNetB1(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb2":
        feature_extractor.add(
            EfficientNetB2(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb3":
        feature_extractor.add(
            EfficientNetB3(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb4":
        feature_extractor.add(
            EfficientNetB4(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb5":
        feature_extractor.add(
            EfficientNetB5(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb6":
        feature_extractor.add(
            EfficientNetB6(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "efficientnetb7":
        feature_extractor.add(
            EfficientNetB7(include_top=False,
                           input_shape=input_shape,
                           weights=weights,
                           pooling='avg'))
    elif feature_extractor_type == "resnet50":
        feature_extractor.add(
            ResNet50(include_top=False,
                     input_shape=input_shape,
                     weights=weights,
                     pooling='avg'))
    elif feature_extractor_type == "simple_cnn":
        feature_extractor.add(tf.keras.layers.Input(shape=input_shape))
        feature_extractor.add(
            SeparableConv2D(64,
                            kernel_size=3,
                            activation='relu',
                            input_shape=input_shape))
        for i in range(3):
            feature_extractor.add(
                SeparableConv2D(32, kernel_size=3, activation='relu'))
            feature_extractor.add(
                SeparableConv2D(32, kernel_size=3, activation='relu'))
            feature_extractor.add(MaxPool2D(pool_size=(2, 2)))
        feature_extractor.add(
            SeparableConv2D(32, kernel_size=3, activation='relu'))
        feature_extractor.add(
            SeparableConv2D(32, kernel_size=3, activation='relu'))
    elif feature_extractor_type == "fsconv":
        feature_extractor.add(tf.keras.layers.Input(shape=input_shape))
        feature_extractor.add(Conv2D(32, kernel_size=3, activation='relu'))
        feature_extractor.add(MaxPool2D(strides=(2, 2)))
        feature_extractor.add(Conv2D(124, kernel_size=3, activation='relu'))
        feature_extractor.add(MaxPool2D(strides=(2, 2)))
        feature_extractor.add(Conv2D(512, kernel_size=3, activation='relu'))
        feature_extractor.add(MaxPool2D(strides=(2, 2)))
    elif feature_extractor_type == "mnist_cnn":
        input_shape = (config["data"]["image_target_size"][0],
                       config["data"]["image_target_size"][1], 1)
        # feature_extractor.add(tf.keras.layers.Input(shape=input_shape))
        # feature_extractor.add(Conv2D(8, kernel_size=3, activation='relu', input_shape=input_shape))
        # feature_extractor.add(MaxPool2D(strides=(2, 2)))
        # feature_extractor.add(Conv2D(16, kernel_size=3, activation='relu', input_shape=input_shape))
        # feature_extractor.add(MaxPool2D(strides=(2, 2)))
        feature_extractor.add(Flatten(input_shape=(28, 28)))
        feature_extractor.add(tf.keras.layers.Dense(128, activation='relu'))
        feature_extractor.add(tf.keras.layers.Dense(64, activation='relu'))
    else:
        raise Exception("Choose valid model architecture!")

    if config["model"]["feature_extractor"]["global_max_pooling"]:
        feature_extractor.add(GlobalMaxPool2D())
    if config["model"]["feature_extractor"]["num_output_features"] > 0:
        activation = config["model"]["feature_extractor"]["output_activation"]
        feature_extractor.add(
            Dense(config["model"]["feature_extractor"]["num_output_features"],
                  activation=activation))
    # feature_extractor.build(input_shape=input_shape)
    return feature_extractor
def light_model_v2(input_shape = (512, 512, 4), classes = 8):
   """Construct the encoder-decoder model architecture."""
   input = Input(input_shape)

   # First Encoder stage 1.
   enc = Conv2D(128, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', strides = (2, 2))(input)
   enc = BatchNormalization()(enc)

   # First Encoder stage 2.
   enc = Conv2D(64, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', strides = (2, 2))(enc)
   enc = BatchNormalization()(enc)

   # First Encoder stage 3.
   enc = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', strides = (2, 2))(enc)
   enc = BatchNormalization()(enc)

   # First Encoder stage 4.
   enc = Conv2D(16, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', strides = (2, 2))(enc)
   enc = BatchNormalization()(enc)

   # Second Encoder stage 1.
   enc2 = SeparableConv2D(128, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal', strides = (2, 2))(input)
   enc2 = BatchNormalization()(enc2)

   # Second Encoder stage 2.
   enc2 = SeparableConv2D(64, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal', strides = (2, 2))(enc2)
   enc2 = BatchNormalization()(enc2)

   # Second Encoder stage 3.
   enc2 = SeparableConv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal', strides = (2, 2))(enc2)
   enc2 = BatchNormalization()(enc2)

   # Second Encoder stage 4.
   enc2 = SeparableConv2D(16, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal', strides = (2, 2))(enc2)
   enc2 = BatchNormalization()(enc2)

   # Concatenate encoders into a single output.
   encoder_output = Concatenate()([enc, enc2])

   # Decoder stage 1.
   dec_branch_1 = Conv2D(32, kernel_size = (5, 5), activation = 'relu', padding = 'same',
                         kernel_initializer = 'he_normal')(encoder_output)
   dec_branch_2 = SeparableConv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                  kernel_initializer = 'he_normal')(encoder_output)
   dec = Add()([dec_branch_1, dec_branch_2])
   dec = UpSampling2D(size = (2, 2))(dec)

   # Decoder stage 2.
   dec_branch_1 = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                         kernel_initializer = 'he_normal')(dec)
   dec_branch_2 = SeparableConv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                  kernel_initializer = 'he_normal')(dec)
   dec = Add()([dec_branch_1, dec_branch_2])
   dec = BatchNormalization()(dec)
   dec = UpSampling2D(size = (2, 2))(dec)

   # Decoder Stage 3.
   dec_branch_1 = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                         kernel_initializer = 'he_normal')(dec)
   dec_branch_2 = SeparableConv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                  kernel_initializer = 'he_normal')(dec)
   dec = Add()([dec_branch_1, dec_branch_2])
   dec = BatchNormalization()(dec)
   dec = UpSampling2D(size = (2, 2))(dec)

   # Decoder Stage 4.
   dec_branch_1 = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                         kernel_initializer = 'he_normal')(dec)
   dec_branch_2 = SeparableConv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                  kernel_initializer = 'he_normal')(dec)
   dec = Add()([dec_branch_1, dec_branch_2])
   dec = BatchNormalization()(dec)
   decoder_output = UpSampling2D(size = (2, 2))(dec)

   # Mini-encoder-decoder to learn higher-level features.
   # MIni-encoder-decoder encoding branch 1.
   mini_model_branch1 = DepthwiseConv2D(kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                        kernel_initializer = 'he_normal')(input)
   mini_model_branch1 = Conv2D(128, kernel_size = (1, 1), activation = 'relu', padding = 'same',
                               kernel_initializer = 'he_normal')(mini_model_branch1)
   mini_model_branch1 = BatchNormalization()(mini_model_branch1)
   mini_model_branch1 = MaxPooling2D(pool_size = (2, 2))(mini_model_branch1)

   mini_model_branch1 = DepthwiseConv2D(kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                        kernel_initializer = 'he_normal')(mini_model_branch1)
   mini_model_branch1 = Conv2D(64, kernel_size = (1, 1), activation = 'relu', padding = 'same',
                               kernel_initializer = 'he_normal')(mini_model_branch1)
   mini_model_branch1 = BatchNormalization()(mini_model_branch1)
   mini_model_branch1 = MaxPooling2D(pool_size = (2, 2))(mini_model_branch1)

   mini_model_branch1 = DepthwiseConv2D(kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                        kernel_initializer = 'he_normal')(mini_model_branch1)
   mini_model_branch1 = Conv2D(16, kernel_size = (1, 1), activation = 'relu', padding = 'same',
                               kernel_initializer = 'he_normal')(mini_model_branch1)
   mini_model_branch1 = BatchNormalization()(mini_model_branch1)
   mini_model_branch1 = MaxPooling2D(pool_size = (2, 2))(mini_model_branch1)

   # Mini encoder-decoder encoding branch 2.
   mini_model_branch2 = Conv2D(128, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                               kernel_initializer = 'he_normal')(input)
   mini_model_branch2 = BatchNormalization()(mini_model_branch2)
   mini_model_branch2 = AveragePooling2D(pool_size = (2, 2))(mini_model_branch2)

   mini_model_branch2 = Conv2D(64, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                               kernel_initializer = 'he_normal')(mini_model_branch2)
   mini_model_branch2 = BatchNormalization()(mini_model_branch2)
   mini_model_branch2 = AveragePooling2D(pool_size = (2, 2))(mini_model_branch2)

   mini_model_branch2 = Conv2D(16, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                               kernel_initializer = 'he_normal')(mini_model_branch2)
   mini_model_branch2 = BatchNormalization()(mini_model_branch2)
   mini_model_branch2 = AveragePooling2D(pool_size = (2, 2))(mini_model_branch2)

   # Concatenate encoding branches.
   mini_model_encoding = Concatenate()([mini_model_branch1, mini_model_branch2])

   # Mini encoder-decoder decoder segment.
   mini_model_decoding = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                kernel_initializer = 'he_normal')(mini_model_encoding)
   mini_model_decoding = BatchNormalization()(mini_model_decoding)
   mini_model_decoding = UpSampling2D(size = (2, 2))(mini_model_decoding)

   mini_model_decoding = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                kernel_initializer = 'he_normal')(mini_model_decoding)
   mini_model_decoding = BatchNormalization()(mini_model_decoding)
   mini_model_decoding = UpSampling2D(size = (2, 2))(mini_model_decoding)

   mini_model_decoding = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                                kernel_initializer = 'he_normal')(mini_model_decoding)
   mini_model_decoding = BatchNormalization()(mini_model_decoding)
   mini_model_decoding = UpSampling2D(size = (2, 2))(mini_model_decoding)

   # Concatenate primary model and mini-model.
   decoder_output = Add()([mini_model_decoding, decoder_output])

   # Model output stage.
   output = Conv2D(classes, kernel_size = (3, 3), activation = 'sigmoid', padding = 'same',
                   kernel_initializer = 'he_normal')(decoder_output)

   return Model(input, output)
Ejemplo n.º 7
0
def build_encoder_decoder():
    # Encoder
    input_tensor = Input(shape=(320, 320, 4))
    x = Conv2D(64, (3, 3), padding='same', activation='relu',
               name='conv1_1')(input_tensor)
    x = Conv2D(64, (3, 3), padding='same', activation='relu',
               name='conv1_2')(x)
    orig_1 = x
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Conv2D(128, (3, 3), padding='same', activation='relu',
               name='conv2_1')(x)
    x = Conv2D(128, (3, 3), padding='same', activation='relu',
               name='conv2_2')(x)
    orig_2 = x
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Conv2D(256, (3, 3), padding='same', activation='relu',
               name='conv3_1')(x)
    x = Conv2D(256, (3, 3), padding='same', activation='relu',
               name='conv3_2')(x)
    x = Conv2D(256, (3, 3), padding='same', activation='relu',
               name='conv3_3')(x)
    orig_3 = x
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)
    inputs_size = x.get_shape()[1:3]

    conv_4_1x1 = SeparableConv2D(512, (1, 1),
                                 activation='relu',
                                 padding='same',
                                 name='conv4_1x1')(x)
    conv_4_3x3_1 = SeparableConv2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   dilation_rate=ATROUS_RATES[0],
                                   name='conv4_3x3_1')(x)
    conv_4_3x3_2 = SeparableConv2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   dilation_rate=ATROUS_RATES[1],
                                   name='conv4_3x3_2')(x)
    conv_4_3x3_3 = SeparableConv2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   dilation_rate=ATROUS_RATES[2],
                                   name='conv4_3x3_3')(x)
    # Image average pooling
    image_level_features = Lambda(
        lambda x: tf.reduce_mean(x, [1, 2], keepdims=True),
        name='global_average_pooling')(x)
    image_level_features = Conv2D(
        512, (1, 1),
        activation='relu',
        padding='same',
        name='image_level_features_conv_1x1')(image_level_features)
    image_level_features = Lambda(lambda x: tf.image.resize(x, inputs_size),
                                  name='upsample_1')(image_level_features)
    # Concat
    x = Concatenate(axis=3)([
        conv_4_1x1, conv_4_3x3_1, conv_4_3x3_2, conv_4_3x3_3,
        image_level_features
    ])
    x = Conv2D(512, (1, 1),
               activation='relu',
               padding='same',
               name='conv_1x1_1_concat')(x)
    x = Conv2D(512, (1, 1),
               activation='relu',
               padding='same',
               name='conv_1x1_2_concat')(x)
    orig_4 = x
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)

    x = Conv2D(512, (3, 3), activation='relu', padding='same',
               name='conv5_1')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same',
               name='conv5_2')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same',
               name='conv5_3')(x)
    orig_5 = x
    x = MaxPooling2D((2, 2), strides=(2, 2))(x)

    # Decoder
    #
    x = UpSampling2D(size=(2, 2))(x)
    the_shape = K.int_shape(orig_5)
    shape = (1, the_shape[1], the_shape[2], the_shape[3])
    origReshaped = Reshape(shape)(orig_5)
    xReshaped = Reshape(shape)(x)
    together = Concatenate(axis=1)([origReshaped, xReshaped])
    x = Unpooling()(together)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='deconv5_1',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='deconv5_2',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='deconv5_3',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)

    x = UpSampling2D(size=(2, 2))(x)
    the_shape = K.int_shape(orig_4)
    shape = (1, the_shape[1], the_shape[2], the_shape[3])
    origReshaped = Reshape(shape)(orig_4)
    xReshaped = Reshape(shape)(x)
    together = Concatenate(axis=1)([origReshaped, xReshaped])
    x = Unpooling()(together)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='deconv4_1',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='deconv4_2',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='deconv4_3',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)

    x = UpSampling2D(size=(2, 2))(x)
    the_shape = K.int_shape(orig_3)
    shape = (1, the_shape[1], the_shape[2], the_shape[3])
    origReshaped = Reshape(shape)(orig_3)
    xReshaped = Reshape(shape)(x)
    together = Concatenate(axis=1)([origReshaped, xReshaped])
    x = Unpooling()(together)
    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='deconv3_1',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='deconv3_2',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='deconv3_3',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)

    x = UpSampling2D(size=(2, 2))(x)
    the_shape = K.int_shape(orig_2)
    shape = (1, the_shape[1], the_shape[2], the_shape[3])
    origReshaped = Reshape(shape)(orig_2)
    xReshaped = Reshape(shape)(x)
    together = Concatenate(axis=1)([origReshaped, xReshaped])
    x = Unpooling()(together)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='deconv2_1',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='deconv2_2',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)

    x = UpSampling2D(size=(2, 2))(x)
    the_shape = K.int_shape(orig_1)
    shape = (1, the_shape[1], the_shape[2], the_shape[3])
    origReshaped = Reshape(shape)(orig_1)
    xReshaped = Reshape(shape)(x)
    together = Concatenate(axis=1)([origReshaped, xReshaped])
    x = Unpooling()(together)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='deconv1_1',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='deconv1_2',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)
    x = BatchNormalization()(x)

    x = Conv2D(1, (3, 3),
               activation='sigmoid',
               padding='same',
               name='pred',
               kernel_initializer='he_normal',
               bias_initializer='zeros')(x)

    model = Model(inputs=input_tensor, outputs=x)
    return model
Ejemplo n.º 8
0
    def Xception_notop(img_size):

        # IMG_SIZE = 299
        # CLASS_NUM = 1000
        # NUM_CHANNEL = 3
        input_shape = (img_size, img_size, 3)

        inputs = InputLayer(shape=input_shape)

        ## Entry Flow
        # Block1
        x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False)(inputs)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = Conv2D(64, (3, 3), use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)

        shortcut1 = Conv2D(128, (1, 1),
                           strides=(2, 2),
                           padding="same",
                           use_bias=False)(x)
        shortcut1 = BatchNormalization()(shortcut1)

        # Block2
        x = SeparableConv2D(128, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = SeparableConv2D(128, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = MaxPool2D((3, 3), strides=(2, 2), padding="same")(x)
        # merge1
        x = Add()([x, shortcut1])

        shortcut2 = Conv2D(256, (1, 1),
                           strides=(2, 2),
                           padding="same",
                           use_bias=False)(x)
        shortcut2 = BatchNormalization()(shortcut2)

        # Block3
        x = Activation("relu")(x)
        x = SeparableConv2D(256, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = SeparableConv2D(256, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = MaxPool2D((3, 3), strides=(2, 2), padding="same")(x)
        # merge2
        x = Add()([x, shortcut2])

        shortcut3 = Conv2D(728, (1, 1),
                           strides=(2, 2),
                           padding="same",
                           use_bias=False)(x)
        shortcut3 = BatchNormalization()(shortcut3)

        # Block4
        x = Activation("relu")(x)
        x = SeparableConv2D(728, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = SeparableConv2D(728, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = MaxPool2D((3, 3), strides=(2, 2), padding="same")(x)
        # merge3
        x = Add()([x, shortcut3])
        # At the end the entry flow, we get 19x19x728 feature maps

        ## Middle Flow
        for i in range(8):
            # Block5-12
            shortcut4 = x
            x = Activation("relu")(x)
            x = SeparableConv2D(728, (3, 3), padding="same", use_bias=False)(x)
            x = BatchNormalization()(x)
            x = Activation("relu")(x)
            x = SeparableConv2D(728, (3, 3), padding="same", use_bias=False)(x)
            x = BatchNormalization()(x)
            x = Activation("relu")(x)
            x = SeparableConv2D(728, (3, 3), padding="same", use_bias=False)(x)
            x = BatchNormalization()(x)
            # merge4-11
            x = Add()([x, shortcut4])
        # At the end the middle flow, we get 19x19x728 feature maps

        ## Exit Flow
        shortcut5 = Conv2D(1024, (1, 1),
                           strides=(2, 2),
                           padding="same",
                           use_bias=False)(x)

        # Block13
        x = Activation("relu")(x)
        x = SeparableConv2D(728, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = SeparableConv2D(1024, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = MaxPool2D((3, 3), strides=(2, 2), padding="same")(x)
        # merge5
        x = Add()([x, shortcut5])

        # Block14
        x = SeparableConv2D(1536, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        x = SeparableConv2D(2048, (3, 3), padding="same", use_bias=False)(x)
        x = BatchNormalization()(x)
        x = Activation("relu")(x)
        #x = GlobalAveragePooling2D()(x)
        # The feature map size after the Global AVG pooling is 2048
        #x = Dense(CLASS_NUM, activation="softmax")(x)

        model = Model(inputs, x, name="xception")

        weights_path = get_file("xception_weights.h5",
                                WEIGHTS_PATH,
                                cache_subdir="models")

        model.load_weights(weights_path)

        return model
Ejemplo n.º 9
0
def tiny_XCEPTION(input_shape, num_classes, l2_regularization=0.01):
    regularization = l2(l2_regularization)

    # base
    img_input = Input(input_shape)
    x = Conv2D(5, (3, 3),
               strides=(1, 1),
               kernel_regularizer=regularization,
               use_bias=False)(img_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(5, (3, 3),
               strides=(1, 1),
               kernel_regularizer=regularization,
               use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # module 1
    residual = Conv2D(8, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(8, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(8, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
    x = layers.add([x, residual])

    # module 2
    residual = Conv2D(16, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(16, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(16, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
    x = layers.add([x, residual])

    # module 3
    residual = Conv2D(32, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(32, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(32, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
    x = layers.add([x, residual])

    # module 4
    residual = Conv2D(64, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(64, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = SeparableConv2D(64, (3, 3),
                        padding='same',
                        kernel_regularizer=regularization,
                        use_bias=False)(x)
    x = BatchNormalization()(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
    x = layers.add([x, residual])

    x = Conv2D(
        num_classes,
        (3, 3),
        # kernel_regularizer=regularization,
        padding='same')(x)
    x = GlobalAveragePooling2D()(x)
    output = Activation('softmax', name='predictions')(x)

    model = Model(img_input, output)
    return model
Ejemplo n.º 10
0
def xception_conv(n_classes=5,
                  img_h=config_img_height,
                  img_w=config_img_width,
                  n_channels=3):
    """
    Keras implementation of Xception architecture created by Francois Chollet (https://arxiv.org/abs/1610.02357)
    Args:
        n_classes: number of classes - used in softmax layer
        img_h: input image height
        img_w: input image width
        n_channels: number of channels in input image (3 for rgb)
    Returns:
        Keras Model() object
    """

    input_shape = (img_h, img_w, n_channels)
    x_input = Input(input_shape)

    # Two small, initial convolutional layers
    x = Conv2D(32, (7, 7), strides=(2, 2), use_bias=False)(x_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(64, (3, 3), use_bias=False)(x_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # Residual layer 1
    skip_layer = Conv2D(64, (1, 1),
                        strides=(2, 2),
                        padding='same',
                        use_bias=False)(x)
    skip_layer = BatchNormalization()(skip_layer)

    # Initial separable convolutional layers
    x = SeparableConv2D(64, (3, 3), padding='same', use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = SeparableConv2D(64, (3, 3), padding='same', use_bias=False)(x)
    x = BatchNormalization()(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    # Add back residual layer
    x = Add()([x, skip_layer])

    # Second set of separable convolutional layers
    x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False)(x)
    x = BatchNormalization()(x)

    # Several consecutive separable convolutional layers with resnet skip layers
    x = separable_resnet_stack(x, n_filters=128)
    x = separable_resnet_stack(x, n_filters=128)
    x = separable_resnet_stack(x, n_filters=128)
    x = Activation('relu')(x)

    # Final separable convolutional layers
    x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = SeparableConv2D(512, (3, 3), padding='same', use_bias=False)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # Dimension Reduction
    x = GlobalAvgPool2D(name='global_avg_pooling')(x)
    x = Dense(n_classes, activation='softmax')(x)

    model = Model(inputs=x_input, outputs=x, name='resnet_28_layer')
    return model
Ejemplo n.º 11
0
def bifpn(features, out_channels, ids, training=True):
    # The first Bifpn layer
    if ids == 0:
        _, _, c3, c4, c5 = features
        p3 = Conv2D(out_channels, 1, 1, padding='same')(c3)
        p3 = BatchNormalization(momentum=MOMENTUM,
                                epsilon=EPSILON)(p3, training=training)

        p4 = Conv2D(out_channels, 1, 1, padding='same')(c4)
        p4 = BatchNormalization(momentum=MOMENTUM,
                                epsilon=EPSILON)(p4, training=training)

        p5 = Conv2D(out_channels, 1, 1, padding='same')(c5)
        p5 = BatchNormalization(momentum=MOMENTUM,
                                epsilon=EPSILON)(p5, training=training)

        p6 = Conv2D(out_channels, 1, 1, padding='same')(c5)
        p6 = BatchNormalization(momentum=MOMENTUM,
                                epsilon=EPSILON)(p6, training=training)
        p6 = MaxPool2D(3, 2, padding='same')(p6)

        p7 = MaxPool2D(3, 2, padding='same')(p6)
        p7_up = UpSampling2D(2)(p7)

        p6_middle = WeightAdd()([p6, p7_up])
        p6_middle = Swish()(p6_middle)
        p6_middle = SeparableConv2D(out_channels, 3, 1,
                                    padding='same')(p6_middle)
        p6_middle = BatchNormalization(momentum=MOMENTUM,
                                       epsilon=EPSILON)(p6_middle,
                                                        training=training)
        p6_up = UpSampling2D(2)(p6_middle)

        p5_middle = WeightAdd()([p5, p6_up])
        p5_middle = Swish()(p5_middle)
        p5_middle = SeparableConv2D(out_channels, 3, padding='same')(p5_middle)
        p5_middle = BatchNormalization(momentum=MOMENTUM,
                                       epsilon=EPSILON)(p5_middle,
                                                        training=training)
        p5_up = UpSampling2D(2)(p5_middle)

        p4_middle = WeightAdd()([p4, p5_up])
        p4_middle = Swish()(p4_middle)
        p4_middle = SeparableConv2D(out_channels, 3, padding='same')(p4_middle)
        p4_middle = BatchNormalization(momentum=MOMENTUM,
                                       epsilon=EPSILON)(p4_middle,
                                                        training=training)
        p4_up = UpSampling2D(2)(p4_middle)

        p3_out = WeightAdd()([p3, p4_up])
        p3_out = Swish()(p3_out)
        p3_out = SeparableConv2D(out_channels, 3, padding='same')(p3_out)
        p3_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p3_out, training=training)
        p3_down = MaxPool2D(3, strides=2, padding='same')(p3_out)

        # path aggregation
        p4_out = WeightAdd()([p4, p4_middle, p3_down])
        p4_out = Swish()(p4_out)
        p4_out = SeparableConv2D(out_channels,
                                 kernel_size=3,
                                 strides=1,
                                 padding='same')(p4_out)
        p4_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p4_out, training=training)
        p4_down = MaxPool2D(pool_size=3, strides=2, padding='same')(p4_out)

        p5_out = WeightAdd()([p5, p5_middle, p4_down])
        p5_out = Swish()(p5_out)
        p5_out = SeparableConv2D(out_channels, 3, 1, padding='same')(p5_out)
        p5_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p5_out, training=training)
        p5_down = MaxPool2D(3, strides=2, padding='same')(p5_out)

        p6_out = WeightAdd()([p6, p6_middle, p5_down])
        p6_out = Swish()(p6_out)
        p6_out = SeparableConv2D(out_channels,
                                 kernel_size=3,
                                 strides=1,
                                 padding='same')(p6_out)
        p6_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p6_out, training=training)
        p6_down = MaxPool2D(pool_size=3, strides=2, padding='same')(p6_out)

        p7_out = WeightAdd()([p7, p6_down])
        p7_out = Swish()(p7_out)
        p7_out = SeparableConv2D(out_channels,
                                 kernel_size=3,
                                 strides=1,
                                 padding='same')(p7_out)
        p7_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p7_out, training=training)

    # Not the first Bifpn layer
    else:
        p3, p4, p5, p6, p7 = features

        p7_up = UpSampling2D(2)(p7)

        p6_middle = WeightAdd()([p6, p7_up])
        p6_middle = Swish()(p6_middle)
        p6_middle = SeparableConv2D(out_channels, 3, 1,
                                    padding='same')(p6_middle)
        p6_middle = BatchNormalization(momentum=MOMENTUM,
                                       epsilon=EPSILON)(p6_middle,
                                                        training=training)
        p6_up = UpSampling2D(2)(p6_middle)

        p5_middle = WeightAdd()([p5, p6_up])
        p5_middle = Swish()(p5_middle)
        p5_middle = SeparableConv2D(out_channels, 3, padding='same')(p5_middle)
        p5_middle = BatchNormalization(momentum=MOMENTUM,
                                       epsilon=EPSILON)(p5_middle,
                                                        training=training)
        p5_up = UpSampling2D(2)(p5_middle)

        p4_middle = WeightAdd()([p4, p5_up])
        p4_middle = Swish()(p4_middle)
        p4_middle = SeparableConv2D(out_channels, 3, padding='same')(p4_middle)
        p4_middle = BatchNormalization(momentum=MOMENTUM,
                                       epsilon=EPSILON)(p4_middle,
                                                        training=training)
        p4_up = UpSampling2D(2)(p4_middle)

        p3_out = WeightAdd()([p3, p4_up])
        p3_out = Swish()(p3_out)
        p3_out = SeparableConv2D(out_channels, 3, padding='same')(p3_out)
        p3_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p3_out, training=training)
        p3_down = MaxPool2D(3, strides=2, padding='same')(p3_out)

        # path aggregation
        p4_out = WeightAdd()([p4, p4_middle, p3_down])
        p4_out = Swish()(p4_out)
        p4_out = SeparableConv2D(out_channels,
                                 kernel_size=3,
                                 strides=1,
                                 padding='same')(p4_out)
        p4_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p4_out, training=training)
        p4_down = MaxPool2D(pool_size=3, strides=2, padding='same')(p4_out)

        p5_out = WeightAdd()([p5, p5_middle, p4_down])
        p5_out = Swish()(p5_out)
        p5_out = SeparableConv2D(out_channels, 3, 1, padding='same')(p5_out)
        p5_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p5_out, training=training)
        p5_down = MaxPool2D(3, strides=2, padding='same')(p5_out)

        p6_out = WeightAdd()([p6, p6_middle, p5_down])
        p6_out = Swish()(p6_out)
        p6_out = SeparableConv2D(out_channels,
                                 kernel_size=3,
                                 strides=1,
                                 padding='same')(p6_out)
        p6_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p6_out, training=training)
        p6_down = MaxPool2D(pool_size=3, strides=2, padding='same')(p6_out)

        p7_out = WeightAdd()([p7, p6_down])
        p7_out = Swish()(p7_out)
        p7_out = SeparableConv2D(out_channels,
                                 kernel_size=3,
                                 strides=1,
                                 padding='same')(p7_out)
        p7_out = BatchNormalization(momentum=MOMENTUM,
                                    epsilon=EPSILON)(p7_out, training=training)

    return p3_out, p4_out, p5_out, p6_out, p7_out
Ejemplo n.º 12
0
def entry_flow(inputs) :
    
    # channel-1 with kernel size of 3
    
    x = Conv2D(32, 3, strides = 2, padding='same')(inputs)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Conv2D(64,3,padding='same')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    previous_block_activation_x = x

    for size in [128, 256, 728] :

        x = Activation('relu')(x)
        x = SeparableConv2D(size, 3, padding='same')(x)
        x = BatchNormalization()(x)

        x = Activation('relu')(x)
        x = SeparableConv2D(size, 3, padding='same')(x)
        x = BatchNormalization()(x)

        x = MaxPooling2D(3, strides=2, padding='same')(x)

        residual_x = Conv2D(size, 1, strides=2, padding='same')(previous_block_activation_x)

        x = tensorflow.keras.layers.Add()([x, residual_x])
        previous_block_activation_x = x

    # channel-2 with kernel size of 7

    y = Conv2D(32, 7, strides = 2, padding='same')(inputs)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)

    y = Conv2D(64,7,padding='same')(y)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)

    previous_block_activation_y = y

    for size in [128, 256, 728] :

        y = Activation('relu')(y)
        y = SeparableConv2D(size, 3, padding='same')(y)
        y = BatchNormalization()(y)

        y = Activation('relu')(y)
        y = SeparableConv2D(size, 3, padding='same')(y)
        y = BatchNormalization()(y)

        y = MaxPooling2D(3, strides=2, padding='same')(y)

        residual_y = Conv2D(size, 1, strides=2, padding='same')(previous_block_activation_y)

        y = tensorflow.keras.layers.Add()([y, residual_y])
        previous_block_activation_y = y

    return x, y
Ejemplo n.º 13
0
def separable_conv_block(inputs, filters, kernel_size, strides):
    channel_axis = 1 if K.image_data_format() == "channels_first" else -1
    Z = SeparableConv2D(filters, kernel_size, strides=strides, padding="same", use_bias=False)(inputs)
    Z = BatchNormalization(axis=channel_axis)(Z)
    A = PReLU(shared_axes=[1, 2])(Z)
    return A
Ejemplo n.º 14
0
def aspp_module(x, planes, kernel_size, padding, dilation):
    x = ZeroPadding2D(padding)(x)
    x = SeparableConv2D(planes, kernel_size, 1, dilation_rate=dilation)(x)
    x = BatchNormalization()(x)
    x = ReLU()(x)
    return x
Ejemplo n.º 15
0
def stem_split_3k(x,
                  filters,
                  mode="conc",
                  kernel_size_1=(3, 3),
                  kernel_size_2=(5, 5),
                  kernel_size_3=(7, 7),
                  dilation_1=(1, 1),
                  dilation_2=(1, 1),
                  dilation_3=(1, 1),
                  padding="same",
                  strides=1):

    if mode == "conc":
        res_filters = filters
        skip_filters = np.uint64(filters * 3)
    elif mode == "add":
        res_filters = filters
        skip_filters = filters

    x1 = SeparableConv2D(res_filters,
                         kernel_size=kernel_size_1,
                         dilation_rate=dilation_1,
                         padding=padding,
                         strides=1,
                         depthwise_initializer=he_normal(seed=5),
                         pointwise_initializer=he_normal(seed=5),
                         bias_initializer='zeros')(x)
    x1 = BatchNormalization()(x1)
    # x1 = Activation("relu")(x1)
    x1 = LeakyReLU(alpha=0.1)(x1)

    res1 = SeparableConv2D(res_filters,
                           kernel_size=kernel_size_1,
                           dilation_rate=dilation_1,
                           padding=padding,
                           strides=1,
                           depthwise_initializer=he_normal(seed=5),
                           pointwise_initializer=he_normal(seed=5),
                           bias_initializer='zeros')(x1)

    x2 = SeparableConv2D(res_filters,
                         kernel_size=kernel_size_2,
                         dilation_rate=dilation_2,
                         padding=padding,
                         strides=1,
                         depthwise_initializer=he_normal(seed=5),
                         pointwise_initializer=he_normal(seed=5),
                         bias_initializer='zeros')(x)
    x2 = BatchNormalization()(x2)
    # x2 = Activation("relu")(x2)
    x2 = LeakyReLU(alpha=0.1)(x2)

    res2 = SeparableConv2D(res_filters,
                           kernel_size=kernel_size_2,
                           dilation_rate=dilation_2,
                           padding=padding,
                           strides=1,
                           depthwise_initializer=he_normal(seed=5),
                           pointwise_initializer=he_normal(seed=5),
                           bias_initializer='zeros')(x2)

    x3 = SeparableConv2D(res_filters,
                         kernel_size=kernel_size_3,
                         dilation_rate=dilation_3,
                         padding=padding,
                         strides=1,
                         depthwise_initializer=he_normal(seed=5),
                         pointwise_initializer=he_normal(seed=5),
                         bias_initializer='zeros')(x)
    x3 = BatchNormalization()(x3)
    # x3 = Activation("relu")(x3)
    x3 = LeakyReLU(alpha=0.1)(x3)

    res3 = SeparableConv2D(res_filters,
                           kernel_size=kernel_size_3,
                           dilation_rate=dilation_3,
                           padding=padding,
                           strides=1,
                           depthwise_initializer=he_normal(seed=5),
                           pointwise_initializer=he_normal(seed=5),
                           bias_initializer='zeros')(x3)

    shortcut = Conv2D(skip_filters,
                      kernel_size=(1, 1),
                      padding=padding,
                      strides=1,
                      kernel_initializer=he_normal(seed=5),
                      bias_initializer='zeros')(x)

    shortcut = BatchNormalization()(shortcut)

    if mode == "conc":
        res = Concatenate()([res1, res2, res3])
        output = Add()([shortcut, res])
    elif mode == "add":
        output = Add()([shortcut, res1, res2, res3])

    return output
Ejemplo n.º 16
0
def Xception(include_top=True, weights='imagenet',
             input_tensor=None, input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the Xception architecture.

    Optionally loads weights pre-trained
    on ImageNet. This model is available for TensorFlow only,
    and can only be used with inputs following the TensorFlow
    data format `(width, height, channels)`.
    You should set `image_data_format='channels_last'` in your Keras config
    located at ~/.keras/keras.json.

    Note that the default input image size for this model is 299x299.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(299, 299, 3)`.
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 71.
            E.g. `(150, 150, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
    """
    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    if K.backend() != 'tensorflow':
        raise RuntimeError('The Xception model is only available with '
                           'the TensorFlow backend.')
    if K.image_data_format() != 'channels_last':
        warnings.warn('The Xception model is only available for the '
                      'input data format "channels_last" '
                      '(width, height, channels). '
                      'However your settings specify the default '
                      'data format "channels_first" (channels, width, height). '
                      'You should set `image_data_format="channels_last"` in your Keras '
                      'config located at ~/.keras/keras.json. '
                      'The model being returned right now will expect inputs '
                      'to follow the "channels_last" data format.')
        K.set_image_data_format('channels_last')
        old_data_format = 'channels_first'
    else:
        old_data_format = None

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=299,
                                      min_size=71,
                                      data_format=K.image_data_format(),
                                      require_flatten=False,
                                      weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1', padding="same")(img_input)
    x = BatchNormalization(name='block1_conv1_bn')(x)
    x = Activation('relu', name='block1_conv1_act')(x)
    x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2', padding='same')(x)
    x = BatchNormalization(name='block1_conv2_bn')(x)
    x = Activation('relu', name='block1_conv2_act')(x)

    residual = Conv2D(128, (1, 1), strides=(2, 2),
                      padding='same', use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv1')(x)
    x = BatchNormalization(name='block2_sepconv1_bn')(x)
    x = Activation('relu', name='block2_sepconv2_act')(x)
    x = SeparableConv2D(128, (3, 3), padding='same', use_bias=False, name='block2_sepconv2')(x)
    x = BatchNormalization(name='block2_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block2_pool')(x)
    x = layers.add([x, residual])

    residual = Conv2D(256, (1, 1), strides=(2, 2),
                      padding='same', use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block3_sepconv1_act')(x)
    x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv1')(x)
    x = BatchNormalization(name='block3_sepconv1_bn')(x)
    x = Activation('relu', name='block3_sepconv2_act')(x)
    x = SeparableConv2D(256, (3, 3), padding='same', use_bias=False, name='block3_sepconv2')(x)
    x = BatchNormalization(name='block3_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block3_pool')(x)
    x = layers.add([x, residual])

    residual = Conv2D(728, (1, 1), strides=(2, 2),
                      padding='same', use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block4_sepconv1_act')(x)
    x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv1')(x)
    x = BatchNormalization(name='block4_sepconv1_bn')(x)
    x = Activation('relu', name='block4_sepconv2_act')(x)
    x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block4_sepconv2')(x)
    x = BatchNormalization(name='block4_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block4_pool')(x)
    x = layers.add([x, residual])

    for i in range(8):
        residual = x
        prefix = 'block' + str(i + 5)

        x = Activation('relu', name=prefix + '_sepconv1_act')(x)
        x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv1')(x)
        x = BatchNormalization(name=prefix + '_sepconv1_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv2_act')(x)
        x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv2')(x)
        x = BatchNormalization(name=prefix + '_sepconv2_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv3_act')(x)
        x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name=prefix + '_sepconv3')(x)
        x = BatchNormalization(name=prefix + '_sepconv3_bn')(x)

        x = layers.add([x, residual])

    residual = Conv2D(1024, (1, 1), strides=(2, 2),
                      padding='same', use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block13_sepconv1_act')(x)
    x = SeparableConv2D(728, (3, 3), padding='same', use_bias=False, name='block13_sepconv1')(x)
    x = BatchNormalization(name='block13_sepconv1_bn')(x)
    x = Activation('relu', name='block13_sepconv2_act')(x)
    x = SeparableConv2D(1024, (3, 3), padding='same', use_bias=False, name='block13_sepconv2')(x)
    x = BatchNormalization(name='block13_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='block13_pool')(x)
    x = layers.add([x, residual])

    x = SeparableConv2D(1536, (3, 3), padding='same', use_bias=False, name='block14_sepconv1')(x)
    x = BatchNormalization(name='block14_sepconv1_bn')(x)
    x = Activation('relu', name='block14_sepconv1_act')(x)

    x = SeparableConv2D(2048, (3, 3), padding='same', use_bias=False, name='block14_sepconv2')(x)
    x = BatchNormalization(name='block14_sepconv2_bn')(x)
    x = Activation('relu', name='block14_sepconv2_act')(x)

    if include_top:
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='xception')

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file('xception_weights_tf_dim_ordering_tf_kernels.h5',
                                    TF_WEIGHTS_PATH,
                                    cache_subdir='models',
                                    file_hash='0a58e3b7378bc2990ea3b43d5981f1f6')
        else:
            weights_path = get_file('xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                                    TF_WEIGHTS_PATH_NO_TOP,
                                    cache_subdir='models',
                                    file_hash='b0042744bf5b25fce3cb969f33bebb97')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    if old_data_format:
        K.set_image_data_format(old_data_format)
    return model
def EEGNet(nb_classes,
           Chans=8,
           Samples=384,
           dropoutRate=0.5,
           kernLength=64,
           F1=8,
           D=2,
           F2=16,
           norm_rate=0.25,
           dropoutType='Dropout'):
    #    Inputs:
    #
    #      nb_classes      : int, number of classes to classify
    #      Chans, Samples  : number of channels and time points in the EEG data
    #      dropoutRate     : dropout fraction
    #      kernLength      : length of temporal convolution in first layer. We found
    #                        that setting this to be half the sampling rate worked
    #                        well in practice. For the SMR dataset in particular
    #                        since the data was high-passed at 4Hz we used a kernel
    #                        length of 32.
    #      F1, F2          : number of temporal filters (F1) and number of pointwise
    #                        filters (F2) to learn. Default: F1 = 8, F2 = F1 * D.
    #      D               : number of spatial filters to learn within each temporal
    #                        convolution. Default: D = 2
    #      dropoutType     : Either SpatialDropout2D or Dropout, passed as a string.
    if dropoutType == 'SpatialDropout2D':
        dropoutType = SpatialDropout2D
    elif dropoutType == 'Dropout':
        dropoutType = Dropout
    else:
        raise ValueError('dropoutType must be one of SpatialDropout2D '
                         'or Dropout, passed as a string.')

    input1 = Input(shape=(Chans, Samples, 1))
    ##################################################################
    block1 = Conv2D(F1, (1, kernLength),
                    padding='same',
                    input_shape=(Chans, Samples, 1),
                    use_bias=False)(input1)
    #    block1       = BatchNormalization(axis = 3)(block1)
    block1 = DepthwiseConv2D((Chans, 1),
                             use_bias=False,
                             depth_multiplier=D,
                             depthwise_constraint=max_norm(1.))(block1)
    #    block1       = BatchNormalization(axis = 3)(block1)
    block1 = Activation('elu')(block1)
    block1 = AveragePooling2D((1, 4))(block1)
    block1 = dropoutType(dropoutRate)(block1)

    block2 = SeparableConv2D(F2, (1, 16), use_bias=False,
                             padding='same')(block1)
    #    block2       = BatchNormalization(axis = 3)(block2)
    block2 = Activation('elu')(block2)
    block2 = AveragePooling2D((1, 8))(block2)
    block2 = dropoutType(dropoutRate)(block2)

    flatten = Flatten(name='flatten')(block2)

    dense = Dense(nb_classes,
                  name='dense',
                  kernel_constraint=max_norm(norm_rate))(flatten)
    softmax = Activation('softmax', name='softmax')(dense)

    return Model(inputs=input1, outputs=softmax)
Ejemplo n.º 18
0
def SeparableConvBlock(num_channels, kernel_size, strides, name, freeze_bn=False):
    f1 = SeparableConv2D(num_channels, kernel_size=kernel_size, strides=strides, padding='same',
                                use_bias=True, name=name+'/conv')
    f2 = BatchNormalization(momentum=MOMENTUM, epsilon=EPSILON, name=name+'/bn')
    # f2 = BatchNormalization(freeze=freeze_bn, name=f'{name}/bn')
    return reduce(lambda f, g: lambda *args, **kwargs: g(f(*args, **kwargs)), (f1, f2))
Ejemplo n.º 19
0
def light_model(input_shape = (512, 512, 4), classes = 8):
   """Construct the encoder-decoder model architecture."""
   input = Input(input_shape)

   # First Encoder stage 1.
   enc = Conv2D(256, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal')(input)
   enc = BatchNormalization()(enc)
   enc = MaxPooling2D(pool_size = (2, 2), padding = 'same')(enc)
   enc = Dropout(0.1)(enc)

   # First Encoder stage 2.
   enc = Conv2D(128, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', kernel_regularizer = l2(0.01))(enc)
   enc = BatchNormalization()(enc)
   enc = MaxPooling2D(pool_size = (2, 2), padding = 'same')(enc)
   enc = Dropout(0.1)(enc)

   # First Encoder stage 3.
   enc = Conv2D(64, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', kernel_regularizer = l2(0.01))(enc)
   enc = BatchNormalization()(enc)
   enc = MaxPooling2D(pool_size = (2, 2), padding = 'same')(enc)
   enc = Dropout(0.1)(enc)

   # First Encoder stage 4.
   enc = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal', kernel_regularizer = l2(0.01))(enc)
   enc = BatchNormalization()(enc)
   enc = MaxPooling2D(pool_size = (2, 2), padding = 'same')(enc)
   enc = Dropout(0.1)(enc)

   # Second Encoder stage 1.
   enc2 = SeparableConv2D(256, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal')(input)
   enc2 = BatchNormalization()(enc2)
   enc2 = AveragePooling2D(pool_size = (2, 2), padding = 'same')(enc2)
   enc2 = Dropout(0.1)(enc2)

   # Second Encoder stage 2.
   enc2 = SeparableConv2D(128, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal')(enc2)
   enc2 = BatchNormalization()(enc2)
   enc2 = AveragePooling2D(pool_size = (2, 2), padding = 'same')(enc2)
   enc2 = Dropout(0.1)(enc2)

   # Second Encoder stage 3.
   enc2 = SeparableConv2D(64, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal')(enc2)
   enc2 = BatchNormalization()(enc2)
   enc2 = AveragePooling2D(pool_size = (2, 2), padding = 'same')(enc2)
   enc2 = Dropout(0.1)(enc2)

   # Second Encoder stage 4.
   enc2 = SeparableConv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                          kernel_initializer = 'he_normal')(enc2)
   enc2 = BatchNormalization()(enc2)
   enc2 = AveragePooling2D(pool_size = (2, 2), padding = 'same')(enc2)
   enc2 = Dropout(0.1)(enc2)

   # Concatenate encoders into a single output.
   encoder_output = Concatenate()([enc, enc2])

   # Decoder stage 1.
   dec = Conv2D(32, kernel_size = (5, 5), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal')(encoder_output)
   dec = BatchNormalization()(dec)
   dec = UpSampling2D(size = (2, 2))(dec)
   dec = Dropout(0.1)(dec)

   # Decoder stage 2.
   dec = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal')(dec)
   dec = BatchNormalization()(dec)
   dec = UpSampling2D(size = (2, 2))(dec)
   dec = Dropout(0.1)(dec)

   # Decoder Stage 3.
   dec = Conv2D(32, kernel_size = (3, 3), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal')(dec)
   dec = BatchNormalization()(dec)
   dec = UpSampling2D(size = (2, 2))(dec)
   dec = Dropout(0.1)(dec)

   # Decoder Stage 4.
   dec = Conv2D(32, kernel_size = (1, 1), activation = 'relu', padding = 'same',
                kernel_initializer = 'he_normal')(dec)
   dec = BatchNormalization()(dec)
   dec = UpSampling2D(size = (2, 2))(dec)
   decoder_output = Dropout(0.1)(dec)

   # Model output stage.
   output = Conv2D(classes, kernel_size = (3, 3), activation = 'sigmoid', padding = 'same',
                   kernel_initializer = 'he_normal')(decoder_output)

   return Model(input, output)
def CNN_A(dropout=0.5):
    K.set_image_data_format("channels_first")

    clear_session()

    channels = 17
    timesteps = 87  # upsampled 58 fps

    inputs = Input(shape=(channels, timesteps))

    input_permute = Permute((1, 2), input_shape=(channels, timesteps))(inputs)
    input_reshape = Reshape((1, channels, timesteps))(input_permute)

    conv2d_1 = Conv2D(
        32,
        (1, channels),
        activation="linear",
        input_shape=(channels, timesteps),
        padding="same",
    )(input_reshape)
    conv2d_1_bn = BatchNormalization()(conv2d_1)

    conv2d_2DW = DepthwiseConv2D(
        (channels, 1),
        use_bias=False,
        activation="linear",
        depth_multiplier=2,
        padding="valid",
        kernel_constraint=max_norm(1.0),
    )(conv2d_1_bn)
    conv2d_2DW_bn = BatchNormalization()(conv2d_2DW)
    conv2d_2DW_bn_act = Activation("elu")(conv2d_2DW_bn)

    conv2d_2DW_bn_act_avpool = AveragePooling2D((1, 4))(conv2d_2DW_bn_act)
    conv2d_2DW_bn_act_avpool_dp = Dropout(
        rate=dropout)(conv2d_2DW_bn_act_avpool)

    conv2d_3Sep = SeparableConv2D(32, (1, channels - 1),
                                  activation="linear",
                                  padding="same")(conv2d_2DW_bn_act_avpool_dp)
    conv2d_3Sep_bn = BatchNormalization()(conv2d_3Sep)
    conv2d_3Sep_bn_act = Activation("elu")(conv2d_3Sep_bn)

    conv2d_3Sep_bn_act_avgpool = AveragePooling2D((1, 8))(conv2d_3Sep_bn_act)
    conv2d_3Sep_bn_act_avgpool_dp = Dropout(
        rate=dropout)(conv2d_3Sep_bn_act_avgpool)

    flatten_1 = Flatten()(conv2d_3Sep_bn_act_avgpool_dp)
    dense_1 = Dense(64,
                    activation="elu",
                    kernel_constraint=max_norm(0.25),
                    name="embedding")(flatten_1)

    predictions = Dense(1,
                        activation="sigmoid",
                        kernel_constraint=max_norm(0.25),
                        name="predictions")(dense_1)

    model = Model(inputs=inputs, outputs=predictions)

    return model
Ejemplo n.º 21
0
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
# y_train = keras.utils.to_categorical(y_train, num_classes)
# y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()

model.add(
    SeparableConv2D(16,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    padding='same',
                    kernel_initializer='he_normal'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(
    SeparableConv2D(8,
                    kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape,
                    padding='same',
                    kernel_initializer='he_normal'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(
    SeparableConv2D(8,
def CNN_A3(dropout=0.25):
    K.set_image_data_format("channels_first")

    clear_session()

    channels = 17
    timesteps = 87  # upsampled 58 fps

    inputs = Input(shape=(channels, timesteps))

    input_permute = Permute((1, 2), input_shape=(channels, timesteps))(inputs)
    x = Reshape((1, channels, timesteps))(input_permute)

    x = Conv2D(
        32,
        (1, channels),
        activation="linear",
        input_shape=(channels, timesteps),
        padding="same",
    )(x)
    x = BatchNormalization()(x)

    x = Conv2D(
        32,
        (1, channels),
        activation="linear",
        input_shape=(channels, timesteps),
        padding="same",
    )(x)
    x = BatchNormalization()(x)

    x = Conv2D(
        32,
        (channels, 1),
        activation="linear",
        input_shape=(channels, timesteps),
        padding="same",
    )(x)
    x = BatchNormalization()(x)

    x = DepthwiseConv2D(
        (channels, 1),
        use_bias=False,
        activation="linear",
        depth_multiplier=2,
        padding="valid",
        kernel_constraint=max_norm(1.0),
    )(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = AveragePooling2D((1, 2))(x)
    x = Dropout(rate=dropout)(x)

    x = SeparableConv2D(32, (1, channels - 1),
                        activation="linear",
                        padding="same")(x)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = AveragePooling2D((1, 2))(x)
    x = Dropout(rate=dropout)(x)

    x = Flatten()(x)

    x = Dense(256, activation="relu", name="d1")(x)

    x = Dense(128, activation="relu", name="d2")(x)

    x = Dense(64, activation="relu", name="embedding")(x)

    predictions = Dense(1, activation="sigmoid", name="predictions")(x)

    return Model(inputs=inputs, outputs=predictions)
Ejemplo n.º 23
0
def multibox_head_separable(source_layers,
                            num_priors,
                            normalizations=None,
                            softmax=True):

    num_classes = 2
    class_activation = 'softmax' if softmax else 'sigmoid'

    mbox_conf = []
    mbox_loc = []
    mbox_quad = []
    mbox_rbox = []
    for i in range(len(source_layers)):
        x = source_layers[i]
        name = x.name.split('/')[0]

        # normalize
        if normalizations is not None and normalizations[i] > 0:
            name = name + '_norm'
            x = Normalize(normalizations[i], name=name)(x)

        # confidence
        name1 = name + '_mbox_conf'
        x1 = SeparableConv2D(num_priors[i] * num_classes, (3, 5),
                             padding='same',
                             name=name1)(x)
        x1 = Flatten(name=name1 + '_flat')(x1)
        mbox_conf.append(x1)

        # location, Delta(x,y,w,h)
        name2 = name + '_mbox_loc'
        x2 = SeparableConv2D(num_priors[i] * 4, (3, 5),
                             padding='same',
                             name=name2)(x)
        x2 = Flatten(name=name2 + '_flat')(x2)
        mbox_loc.append(x2)

        # quadrilateral, Delta(x1,y1,x2,y2,x3,y3,x4,y4)
        name3 = name + '_mbox_quad'
        x3 = SeparableConv2D(num_priors[i] * 8, (3, 5),
                             padding='same',
                             name=name3)(x)
        x3 = Flatten(name=name3 + '_flat')(x3)
        mbox_quad.append(x3)

        # rotated rectangle, Delta(x1,y1,x2,y2,h)
        name4 = name + '_mbox_rbox'
        x4 = SeparableConv2D(num_priors[i] * 5, (3, 5),
                             padding='same',
                             name=name4)(x)
        x4 = Flatten(name=name4 + '_flat')(x4)
        mbox_rbox.append(x4)

    mbox_conf = concatenate(mbox_conf, axis=1, name='mbox_conf')
    mbox_conf = Reshape((-1, num_classes), name='mbox_conf_logits')(mbox_conf)
    mbox_conf = Activation(class_activation, name='mbox_conf_final')(mbox_conf)

    mbox_loc = concatenate(mbox_loc, axis=1, name='mbox_loc')
    mbox_loc = Reshape((-1, 4), name='mbox_loc_final')(mbox_loc)

    mbox_quad = concatenate(mbox_quad, axis=1, name='mbox_quad')
    mbox_quad = Reshape((-1, 8), name='mbox_quad_final')(mbox_quad)

    mbox_rbox = concatenate(mbox_rbox, axis=1, name='mbox_rbox')
    mbox_rbox = Reshape((-1, 5), name='mbox_rbox_final')(mbox_rbox)

    predictions = concatenate([mbox_loc, mbox_quad, mbox_rbox, mbox_conf],
                              axis=2,
                              name='predictions')

    return predictions
Ejemplo n.º 24
0
def identity_block(X, f, filters, stage, block):
    """
    Implementation of the identity block as defined in Figure 3

    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    stage -- integer, used to name the layers, depending on their position in the network
    block -- string/character, used to name the layers, depending on their position in the network

    Returns:
    X -- output of the identity block, tensor of shape (n_H, n_W, n_C)
    """

    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    # Retrieve Filters
    F1, F2, F3 = filters

    # Save the input value. You'll need this later to add back to the main path.
    X_shortcut = X

    # First component of main path
    X = SeparableConv2D(filters=F1,
                        kernel_size=(1, 1),
                        strides=(1, 1),
                        padding='valid',
                        name=conv_name_base + '2a',
                        kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = ReLU(max_value=6)(X)

    ### START CODE HERE ###

    # Second component of main path (≈3 lines)
    X = SeparableConv2D(filters=F2,
                        kernel_size=(f, f),
                        strides=(1, 1),
                        padding='same',
                        name=conv_name_base + '2b',
                        kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = ReLU(max_value=6)(X)

    # Third component of main path (≈2 lines)
    X = SeparableConv2D(filters=F3,
                        kernel_size=(1, 1),
                        strides=(1, 1),
                        padding='valid',
                        name=conv_name_base + '2c',
                        kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
    X = add([X, X_shortcut])
    X = ReLU(max_value=6)(X)

    ### END CODE HERE ###

    return X
Ejemplo n.º 25
0
def EEGNet(nb_classes,
           Chans=64,
           Samples=128,
           dropoutRate=0.5,
           kernLength=64,
           F1=8,
           D=2,
           F2=16,
           norm_rate=0.25,
           dropoutType='Dropout'):
    """ Keras Implementation of EEGNet
    http://iopscience.iop.org/article/10.1088/1741-2552/aace8c/meta

    Note that this implements the newest version of EEGNet and NOT the earlier
    version (version v1 and v2 on arxiv). We strongly recommend using this
    architecture as it performs much better and has nicer properties than
    our earlier version. For example:
        
        1. Depthwise Convolutions to learn spatial filters within a 
        temporal convolution. The use of the depth_multiplier option maps 
        exactly to the number of spatial filters learned within a temporal
        filter. This matches the setup of algorithms like FBCSP which learn 
        spatial filters within each filter in a filter-bank. This also limits 
        the number of free parameters to fit when compared to a fully-connected
        convolution. 
        
        2. Separable Convolutions to learn how to optimally combine spatial
        filters across temporal bands. Separable Convolutions are Depthwise
        Convolutions followed by (1x1) Pointwise Convolutions. 
        
    
    While the original paper used Dropout, we found that SpatialDropout2D 
    sometimes produced slightly better results for classification of ERP 
    signals. However, SpatialDropout2D significantly reduced performance 
    on the Oscillatory dataset (SMR, BCI-IV Dataset 2A). We recommend using
    the default Dropout in most cases.
        
    Assumes the input signal is sampled at 128Hz. If you want to use this model
    for any other sampling rate you will need to modify the lengths of temporal
    kernels and average pooling size in blocks 1 and 2 as needed (double the 
    kernel lengths for double the sampling rate, etc). Note that we haven't 
    tested the model performance with this rule so this may not work well. 
    
    The model with default parameters gives the EEGNet-8,2 model as discussed
    in the paper. This model should do pretty well in general, although it is
	advised to do some model searching to get optimal performance on your
	particular dataset.

    We set F2 = F1 * D (number of input filters = number of output filters) for
    the SeparableConv2D layer. We haven't extensively tested other values of this
    parameter (say, F2 < F1 * D for compressed learning, and F2 > F1 * D for
    overcomplete). We believe the main parameters to focus on are F1 and D. 

    Inputs:
        
      nb_classes      : int, number of classes to classify
      Chans, Samples  : number of channels and time points in the EEG data
      dropoutRate     : dropout fraction
      kernLength      : length of temporal convolution in first layer. We found
                        that setting this to be half the sampling rate worked
                        well in practice. For the SMR dataset in particular
                        since the data was high-passed at 4Hz we used a kernel
                        length of 32.     
      F1, F2          : number of temporal filters (F1) and number of pointwise
                        filters (F2) to learn. Default: F1 = 8, F2 = F1 * D. 
      D               : number of spatial filters to learn within each temporal
                        convolution. Default: D = 2
      dropoutType     : Either SpatialDropout2D or Dropout, passed as a string.

    """

    if dropoutType == 'SpatialDropout2D':
        dropoutType = SpatialDropout2D
    elif dropoutType == 'Dropout':
        dropoutType = Dropout
    else:
        raise ValueError('dropoutType must be one of SpatialDropout2D '
                         'or Dropout, passed as a string.')

    input1 = Input(shape=(1, Chans, Samples))

    ##################################################################
    block1 = Conv2D(F1, (1, kernLength),
                    padding='same',
                    input_shape=(1, Chans, Samples),
                    use_bias=False)(input1)
    block1 = BatchNormalization(axis=1)(block1)
    block1 = DepthwiseConv2D((Chans, 1),
                             use_bias=False,
                             depth_multiplier=D,
                             depthwise_constraint=max_norm(1.))(block1)
    block1 = BatchNormalization(axis=1)(block1)
    block1 = Activation('elu')(block1)
    block1 = AveragePooling2D((1, 4))(block1)
    block1 = dropoutType(dropoutRate)(block1)

    block2 = SeparableConv2D(F2, (1, 16), use_bias=False,
                             padding='same')(block1)
    block2 = BatchNormalization(axis=1)(block2)
    block2 = Activation('elu')(block2)
    block2 = AveragePooling2D((1, 8))(block2)
    block2 = dropoutType(dropoutRate)(block2)

    flatten = Flatten(name='flatten')(block2)

    dense = Dense(nb_classes,
                  name='dense',
                  kernel_constraint=max_norm(norm_rate))(flatten)
    softmax = Activation('softmax', name='softmax')(dense)

    return Model(inputs=input1, outputs=softmax)
Ejemplo n.º 26
0
def CreateModelCNNV2(num_classes=1, drop=0.25, isBN=True, ad_batch_size=1):
    model = Sequential()
    model.add(
        SeparableConv2D(filters=14,
                        kernel_size=(7, 7),
                        padding='same',
                        strides=(2, 2),
                        input_shape=(ad_batch_size, 7, 1)))
    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))

    model.add(
        SeparableConv2D(filters=28,
                        kernel_size=(3, 3),
                        padding='same',
                        strides=(2, 2)))
    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))

    model.add(
        SeparableConv2D(filters=56,
                        kernel_size=(3, 3),
                        padding='same',
                        strides=(2, 2)))
    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))

    model.add(
        SeparableConv2D(filters=56,
                        kernel_size=(3, 3),
                        padding='same',
                        strides=(1, 1)))
    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))

    # -----------------
    model.add(Flatten())
    # -----------
    model.add(Dense(56))
    if isBN:
        model.add(BatchNormalization())
    model.add(Activation('tanh'))
    if drop > 0:
        model.add(Dropout(drop))

    model.add(Dense(28))
    if isBN:
        model.add(BatchNormalization())
    model.add(Activation('tanh'))
    if drop > 0:
        model.add(Dropout(drop))

    model.add(Dense(28))
    if isBN:
        model.add(BatchNormalization())
    model.add(Activation('tanh'))
    if drop > 0:
        model.add(Dropout(drop))

    model.add(Dense(num_classes))
    if isBN:
        model.add(BatchNormalization())

    model.summary()
    sModelName = 'smartcar_ad_CNNV2_drop_0%d_adSize_%d' % (int(
        drop * 100), ad_batch_size)
    if not isBN:
        sModelName += '_nobn'
    return sModelName, model
Ejemplo n.º 27
0
def get_test_model_exhaustive():
    """Returns a exhaustive test model."""
    input_shapes = [(2, 3, 4, 5, 6), (2, 3, 4, 5, 6), (7, 8, 9, 10),
                    (7, 8, 9, 10), (11, 12, 13), (11, 12, 13), (14, 15),
                    (14, 15), (16, ),
                    (16, ), (2, ), (1, ), (2, ), (1, ), (1, 3), (1, 4),
                    (1, 1, 3), (1, 1, 4), (1, 1, 1, 3), (1, 1, 1, 4),
                    (1, 1, 1, 1, 3), (1, 1, 1, 1, 4), (26, 28, 3), (4, 4, 3),
                    (4, 4, 3), (4, ), (2, 3), (1, ), (1, ), (1, ), (2, 3),
                    (9, 16, 1), (1, 9, 16)]

    inputs = [Input(shape=s) for s in input_shapes]

    outputs = []

    outputs.append(Conv1D(1, 3, padding='valid')(inputs[6]))
    outputs.append(Conv1D(2, 1, padding='same')(inputs[6]))
    outputs.append(Conv1D(3, 4, padding='causal', dilation_rate=2)(inputs[6]))
    outputs.append(ZeroPadding1D(2)(inputs[6]))
    outputs.append(Cropping1D((2, 3))(inputs[6]))
    outputs.append(MaxPooling1D(2)(inputs[6]))
    outputs.append(MaxPooling1D(2, strides=2, padding='same')(inputs[6]))
    outputs.append(AveragePooling1D(2)(inputs[6]))
    outputs.append(AveragePooling1D(2, strides=2, padding='same')(inputs[6]))
    outputs.append(GlobalMaxPooling1D()(inputs[6]))
    outputs.append(GlobalMaxPooling1D(data_format="channels_first")(inputs[6]))
    outputs.append(GlobalAveragePooling1D()(inputs[6]))
    outputs.append(
        GlobalAveragePooling1D(data_format="channels_first")(inputs[6]))

    outputs.append(Conv2D(4, (3, 3))(inputs[4]))
    outputs.append(Conv2D(4, (3, 3), use_bias=False)(inputs[4]))
    outputs.append(
        Conv2D(4, (2, 4), strides=(2, 3), padding='same')(inputs[4]))
    outputs.append(
        Conv2D(4, (2, 4), padding='same', dilation_rate=(2, 3))(inputs[4]))

    outputs.append(SeparableConv2D(3, (3, 3))(inputs[4]))
    outputs.append(DepthwiseConv2D((3, 3))(inputs[4]))
    outputs.append(DepthwiseConv2D((1, 2))(inputs[4]))

    outputs.append(MaxPooling2D((2, 2))(inputs[4]))
    # todo: check if TensorFlow >= 2.1 supports this
    #outputs.append(MaxPooling2D((2, 2), data_format="channels_first")(inputs[4])) # Default MaxPoolingOp only supports NHWC on device type CPU
    outputs.append(
        MaxPooling2D((1, 3), strides=(2, 3), padding='same')(inputs[4]))
    outputs.append(AveragePooling2D((2, 2))(inputs[4]))
    # todo: check if TensorFlow >= 2.1 supports this
    #outputs.append(AveragePooling2D((2, 2), data_format="channels_first")(inputs[4])) # Default AvgPoolingOp only supports NHWC on device type CPU
    outputs.append(
        AveragePooling2D((1, 3), strides=(2, 3), padding='same')(inputs[4]))

    outputs.append(GlobalAveragePooling2D()(inputs[4]))
    outputs.append(
        GlobalAveragePooling2D(data_format="channels_first")(inputs[4]))
    outputs.append(GlobalMaxPooling2D()(inputs[4]))
    outputs.append(GlobalMaxPooling2D(data_format="channels_first")(inputs[4]))

    outputs.append(Permute((3, 4, 1, 5, 2))(inputs[0]))
    outputs.append(Permute((1, 5, 3, 2, 4))(inputs[0]))
    outputs.append(Permute((3, 4, 1, 2))(inputs[2]))
    outputs.append(Permute((2, 1, 3))(inputs[4]))
    outputs.append(Permute((2, 1))(inputs[6]))
    outputs.append(Permute((1, ))(inputs[8]))

    outputs.append(Permute((3, 1, 2))(inputs[31]))
    outputs.append(Permute((3, 1, 2))(inputs[32]))
    outputs.append(BatchNormalization()(Permute((3, 1, 2))(inputs[31])))
    outputs.append(BatchNormalization()(Permute((3, 1, 2))(inputs[32])))

    outputs.append(BatchNormalization()(inputs[0]))
    outputs.append(BatchNormalization(axis=1)(inputs[0]))
    outputs.append(BatchNormalization(axis=2)(inputs[0]))
    outputs.append(BatchNormalization(axis=3)(inputs[0]))
    outputs.append(BatchNormalization(axis=4)(inputs[0]))
    outputs.append(BatchNormalization(axis=5)(inputs[0]))
    outputs.append(BatchNormalization()(inputs[2]))
    outputs.append(BatchNormalization(axis=1)(inputs[2]))
    outputs.append(BatchNormalization(axis=2)(inputs[2]))
    outputs.append(BatchNormalization(axis=3)(inputs[2]))
    outputs.append(BatchNormalization(axis=4)(inputs[2]))
    outputs.append(BatchNormalization()(inputs[4]))
    # todo: check if TensorFlow >= 2.1 supports this
    #outputs.append(BatchNormalization(axis=1)(inputs[4])) # tensorflow.python.framework.errors_impl.InternalError:  The CPU implementation of FusedBatchNorm only supports NHWC tensor format for now.
    outputs.append(BatchNormalization(axis=2)(inputs[4]))
    outputs.append(BatchNormalization(axis=3)(inputs[4]))
    outputs.append(BatchNormalization()(inputs[6]))
    outputs.append(BatchNormalization(axis=1)(inputs[6]))
    outputs.append(BatchNormalization(axis=2)(inputs[6]))
    outputs.append(BatchNormalization()(inputs[8]))
    outputs.append(BatchNormalization(axis=1)(inputs[8]))
    outputs.append(BatchNormalization()(inputs[27]))
    outputs.append(BatchNormalization(axis=1)(inputs[27]))
    outputs.append(BatchNormalization()(inputs[14]))
    outputs.append(BatchNormalization(axis=1)(inputs[14]))
    outputs.append(BatchNormalization(axis=2)(inputs[14]))
    outputs.append(BatchNormalization()(inputs[16]))
    # todo: check if TensorFlow >= 2.1 supports this
    #outputs.append(BatchNormalization(axis=1)(inputs[16])) # tensorflow.python.framework.errors_impl.InternalError:  The CPU implementation of FusedBatchNorm only supports NHWC tensor format for now.
    outputs.append(BatchNormalization(axis=2)(inputs[16]))
    outputs.append(BatchNormalization(axis=3)(inputs[16]))
    outputs.append(BatchNormalization()(inputs[18]))
    outputs.append(BatchNormalization(axis=1)(inputs[18]))
    outputs.append(BatchNormalization(axis=2)(inputs[18]))
    outputs.append(BatchNormalization(axis=3)(inputs[18]))
    outputs.append(BatchNormalization(axis=4)(inputs[18]))
    outputs.append(BatchNormalization()(inputs[20]))
    outputs.append(BatchNormalization(axis=1)(inputs[20]))
    outputs.append(BatchNormalization(axis=2)(inputs[20]))
    outputs.append(BatchNormalization(axis=3)(inputs[20]))
    outputs.append(BatchNormalization(axis=4)(inputs[20]))
    outputs.append(BatchNormalization(axis=5)(inputs[20]))

    outputs.append(Dropout(0.5)(inputs[4]))

    outputs.append(ZeroPadding2D(2)(inputs[4]))
    outputs.append(ZeroPadding2D((2, 3))(inputs[4]))
    outputs.append(ZeroPadding2D(((1, 2), (3, 4)))(inputs[4]))
    outputs.append(Cropping2D(2)(inputs[4]))
    outputs.append(Cropping2D((2, 3))(inputs[4]))
    outputs.append(Cropping2D(((1, 2), (3, 4)))(inputs[4]))

    outputs.append(Dense(3, use_bias=True)(inputs[13]))
    outputs.append(Dense(3, use_bias=True)(inputs[14]))
    outputs.append(Dense(4, use_bias=False)(inputs[16]))
    outputs.append(Dense(4, use_bias=False, activation='tanh')(inputs[18]))
    outputs.append(Dense(4, use_bias=False)(inputs[20]))

    outputs.append(Reshape(((2 * 3 * 4 * 5 * 6), ))(inputs[0]))
    outputs.append(Reshape((2, 3 * 4 * 5 * 6))(inputs[0]))
    outputs.append(Reshape((2, 3, 4 * 5 * 6))(inputs[0]))
    outputs.append(Reshape((2, 3, 4, 5 * 6))(inputs[0]))
    outputs.append(Reshape((2, 3, 4, 5, 6))(inputs[0]))

    outputs.append(Reshape((16, ))(inputs[8]))
    outputs.append(Reshape((2, 8))(inputs[8]))
    outputs.append(Reshape((2, 2, 4))(inputs[8]))
    outputs.append(Reshape((2, 2, 2, 2))(inputs[8]))
    outputs.append(Reshape((2, 2, 1, 2, 2))(inputs[8]))

    outputs.append(
        UpSampling2D(size=(1, 2), interpolation='nearest')(inputs[4]))
    outputs.append(
        UpSampling2D(size=(5, 3), interpolation='nearest')(inputs[4]))
    outputs.append(
        UpSampling2D(size=(1, 2), interpolation='bilinear')(inputs[4]))
    outputs.append(
        UpSampling2D(size=(5, 3), interpolation='bilinear')(inputs[4]))

    for axis in [-5, -4, -3, -2, -1, 1, 2, 3, 4, 5]:
        outputs.append(Concatenate(axis=axis)([inputs[0], inputs[1]]))
    for axis in [-4, -3, -2, -1, 1, 2, 3, 4]:
        outputs.append(Concatenate(axis=axis)([inputs[2], inputs[3]]))
    for axis in [-3, -2, -1, 1, 2, 3]:
        outputs.append(Concatenate(axis=axis)([inputs[4], inputs[5]]))
    for axis in [-2, -1, 1, 2]:
        outputs.append(Concatenate(axis=axis)([inputs[6], inputs[7]]))
    for axis in [-1, 1]:
        outputs.append(Concatenate(axis=axis)([inputs[8], inputs[9]]))
    for axis in [-1, 2]:
        outputs.append(Concatenate(axis=axis)([inputs[14], inputs[15]]))
    for axis in [-1, 3]:
        outputs.append(Concatenate(axis=axis)([inputs[16], inputs[17]]))
    for axis in [-1, 4]:
        outputs.append(Concatenate(axis=axis)([inputs[18], inputs[19]]))
    for axis in [-1, 5]:
        outputs.append(Concatenate(axis=axis)([inputs[20], inputs[21]]))

    outputs.append(UpSampling1D(size=2)(inputs[6]))
    # outputs.append(UpSampling1D(size=2)(inputs[8])) # ValueError: Input 0 of layer up_sampling1d_1 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 16]

    outputs.append(Multiply()([inputs[10], inputs[11]]))
    outputs.append(Multiply()([inputs[11], inputs[10]]))
    outputs.append(Multiply()([inputs[11], inputs[13]]))
    outputs.append(Multiply()([inputs[10], inputs[11], inputs[12]]))
    outputs.append(Multiply()([inputs[11], inputs[12], inputs[13]]))

    shared_conv = Conv2D(1, (1, 1),
                         padding='valid',
                         name='shared_conv',
                         activation='relu')

    up_scale_2 = UpSampling2D((2, 2))
    x1 = shared_conv(up_scale_2(inputs[23]))  # (1, 8, 8)
    x2 = shared_conv(up_scale_2(inputs[24]))  # (1, 8, 8)
    x3 = Conv2D(1, (1, 1),
                padding='valid')(up_scale_2(inputs[24]))  # (1, 8, 8)
    x = Concatenate()([x1, x2, x3])  # (3, 8, 8)
    outputs.append(x)

    x = Conv2D(3, (1, 1), padding='same', use_bias=False)(x)  # (3, 8, 8)
    outputs.append(x)
    x = Dropout(0.5)(x)
    outputs.append(x)
    x = Concatenate()([MaxPooling2D((2, 2))(x),
                       AveragePooling2D((2, 2))(x)])  # (6, 4, 4)
    outputs.append(x)

    x = Flatten()(x)  # (1, 1, 96)
    x = Dense(4, use_bias=False)(x)
    outputs.append(x)
    x = Dense(3)(x)  # (1, 1, 3)
    outputs.append(x)

    outputs.append(Add()([inputs[26], inputs[30], inputs[30]]))
    outputs.append(Subtract()([inputs[26], inputs[30]]))
    outputs.append(Multiply()([inputs[26], inputs[30], inputs[30]]))
    outputs.append(Average()([inputs[26], inputs[30], inputs[30]]))
    outputs.append(Maximum()([inputs[26], inputs[30], inputs[30]]))
    outputs.append(Concatenate()([inputs[26], inputs[30], inputs[30]]))

    intermediate_input_shape = (3, )
    intermediate_in = Input(intermediate_input_shape)
    intermediate_x = intermediate_in
    intermediate_x = Dense(8)(intermediate_x)
    intermediate_x = Dense(5, name='duplicate_layer_name')(intermediate_x)
    intermediate_model = Model(inputs=[intermediate_in],
                               outputs=[intermediate_x],
                               name='intermediate_model')
    intermediate_model.compile(loss='mse', optimizer='nadam')

    x = intermediate_model(x)  # (1, 1, 5)

    intermediate_model_2 = Sequential()
    intermediate_model_2.add(Dense(7, input_shape=(5, )))
    intermediate_model_2.add(Dense(5, name='duplicate_layer_name'))
    intermediate_model_2.compile(optimizer='rmsprop',
                                 loss='categorical_crossentropy')

    x = intermediate_model_2(x)  # (1, 1, 5)

    x = Dense(3)(x)  # (1, 1, 3)

    shared_activation = Activation('tanh')

    outputs = outputs + [
        Activation('tanh')(inputs[25]),
        Activation('hard_sigmoid')(inputs[25]),
        Activation('selu')(inputs[25]),
        Activation('sigmoid')(inputs[25]),
        Activation('softplus')(inputs[25]),
        Activation('softmax')(inputs[25]),
        Activation('softmax')(inputs[25]),
        Activation('relu')(inputs[25]),
        LeakyReLU()(inputs[25]),
        ELU()(inputs[25]),
        PReLU()(inputs[24]),
        PReLU()(inputs[25]),
        PReLU()(inputs[26]),
        shared_activation(inputs[25]),
        Activation('linear')(inputs[26]),
        Activation('linear')(inputs[23]),
        x,
        shared_activation(x),
    ]

    model = Model(inputs=inputs, outputs=outputs, name='test_model_exhaustive')
    model.compile(loss='mse', optimizer='nadam')

    # fit to dummy data
    training_data_size = 2
    data_in = generate_input_data(training_data_size, input_shapes)
    initial_data_out = model.predict(data_in)
    data_out = generate_output_data(training_data_size, initial_data_out)
    model.fit(data_in, data_out, epochs=10)
    return model
Ejemplo n.º 28
0
def smallpureCNNModelV2(num_classes=1, drop=0.25, isBN=True, ad_batch_size=1):
    model = Sequential()
    model.add(
        Conv2D(filters=64,
               kernel_size=(1, 7),
               padding='same',
               strides=(1, 1),
               input_shape=(ad_batch_size, 7, 1)))

    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))

    model.add(
        SeparableConv2D(filters=64,
                        kernel_size=(1, 7),
                        padding='same',
                        strides=(1, 1),
                        input_shape=(ad_batch_size, 7, 1)))

    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))
    # model.add(Dropout(drop))

    model.add(
        SeparableConv2D(filters=64,
                        kernel_size=(1, 7),
                        padding='same',
                        strides=(1, 1),
                        input_shape=(ad_batch_size, 7, 1)))

    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))

    model.add(
        SeparableConv2D(filters=512,
                        kernel_size=(1, 7),
                        padding='same',
                        strides=(1, 1),
                        input_shape=(ad_batch_size, 7, 1)))

    if isBN:
        model.add(BatchNormalization())
    model.add(ReLU(max_value=8))
    # model.add(Dropout(drop))

    model.add(GlobalAveragePooling2D())

    model.add(Flatten())

    model.add(Dense(num_classes))
    if isBN:
        model.add(BatchNormalization())

    model.summary()
    sModelName = 'smartcar_ad_pureCNN_drop_0%d_adSize_%d' % (int(
        drop * 100), ad_batch_size)
    if not isBN:
        sModelName += '_nobn'
    return sModelName, model
Ejemplo n.º 29
0
def EEGNet_SSVEP(nb_classes = 12, Chans = 8, Samples = 256, 
             dropoutRate = 0.5, kernLength = 256, F1 = 96, 
             D = 1, F2 = 96, dropoutType = 'Dropout'):
    """ SSVEP Variant of EEGNet, as used in [1]. 

    Inputs:
        
      nb_classes      : int, number of classes to classify
      Chans, Samples  : number of channels and time points in the EEG data
      dropoutRate     : dropout fraction
      kernLength      : length of temporal convolution in first layer
      F1, F2          : number of temporal filters (F1) and number of pointwise
                        filters (F2) to learn. 
      D               : number of spatial filters to learn within each temporal
                        convolution.
      dropoutType     : Either SpatialDropout2D or Dropout, passed as a string.
      
      
    [1]. Waytowich, N. et. al. (2018). Compact Convolutional Neural Networks
    for Classification of Asynchronous Steady-State Visual Evoked Potentials.
    Journal of Neural Engineering vol. 15(6). 
    http://iopscience.iop.org/article/10.1088/1741-2552/aae5d8

    """
    
    if dropoutType == 'SpatialDropout2D':
        dropoutType = SpatialDropout2D
    elif dropoutType == 'Dropout':
        dropoutType = Dropout
    else:
        raise ValueError('dropoutType must be one of SpatialDropout2D '
                         'or Dropout, passed as a string.')
    
    input1   = Input(shape = (1, Chans, Samples))

    ##################################################################
    block1       = Conv2D(F1, (1, kernLength), padding = 'same',
                                   input_shape = (1, Chans, Samples),
                                   use_bias = False)(input1)
    block1       = BatchNormalization(axis = 1)(block1)
    block1       = DepthwiseConv2D((Chans, 1), use_bias = False, 
                                   depth_multiplier = D,
                                   depthwise_constraint = max_norm(1.))(block1)
    block1       = BatchNormalization(axis = 1)(block1)
    block1       = Activation('elu')(block1)
    block1       = AveragePooling2D((1, 4))(block1)
    block1       = dropoutType(dropoutRate)(block1)
    
    block2       = SeparableConv2D(F2, (1, 16),
                                   use_bias = False, padding = 'same')(block1)
    block2       = BatchNormalization(axis = 1)(block2)
    block2       = Activation('elu')(block2)
    block2       = AveragePooling2D((1, 8))(block2)
    block2       = dropoutType(dropoutRate)(block2)
        
    flatten      = Flatten(name = 'flatten')(block2)
    
    dense        = Dense(nb_classes, name = 'dense')(flatten)
    softmax      = Activation('softmax', name = 'softmax')(dense)
    
    return Model(inputs=input1, outputs=softmax)
Ejemplo n.º 30
0
np.random.seed(seed)

generator = ImageDataGenerator(rotation_range=30,
                               width_shift_range=0.1,
                               height_shift_range=0.1,
                               zoom_range=0.3,
                               vertical_flip=True,
                               horizontal_flip=True)
train_data = generator.flow(X_train, y_train, batch_size=batch_size, seed=seed)

model = Sequential()
model.add(
    SeparableConv2D(16,
                    kernel_size=(3, 3),
                    input_shape=(X_train.shape[1], X_train.shape[2],
                                 X_train.shape[3]),
                    padding="same",
                    activation="relu",
                    kernel_initializer="he_uniform"))
model.add(
    SeparableConv2D(32,
                    kernel_size=(3, 3),
                    padding="same",
                    activation="relu",
                    kernel_initializer="he_uniform"))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(BatchNormalization())
model.add(
    SeparableConv2D(64,
                    kernel_size=(1, 1),