Ejemplo n.º 1
0
def dense_net(input_tensor, blocks):
    bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1
    x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(input_tensor)
    x = layers.Conv2D(64, 7, strides=2, use_bias=False)(x)
    x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5)(x)
    x = layers.Activation('relu')(x)
    x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = layers.MaxPooling2D(3, strides=2)(x)

    x = dense_block(x, blocks[0])
    x = transition_block(x, 0.5)
    x = dense_block(x, blocks[1])

    o1 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5)(x)
    o1 = layers.GlobalMaxPooling2D()(o1)

    x = transition_block(x, 0.5)
    x = dense_block(x, blocks[2])
    x = transition_block(x, 0.5)
    x = dense_block(x, blocks[3])

    o2 = None
    if len(blocks) >= 5:
        o2 = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5)(x)
        o2 = layers.GlobalMaxPooling2D()(o2)

        x = transition_block(x, 0.5)
        x = dense_block(x, blocks[4])

    x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5)(x)
    x = layers.GlobalMaxPooling2D()(x)
    return x, o1, o2
def raw_model(lr=0.001, shape=(64, 64, 3)):
    inputs = layers.Input(shape=shape)
    base = applications.DenseNet121(input_tensor=inputs, weights='imagenet', include_top=False)
    for i, layer in enumerate(base.layers):
        print(i, layer.name)

    # for layer in base.layers[:-7]:
    #    layer.trainable = False
    # for layer in base.layers[-7:]:
    #    layer.trainable = True
    output = layers.GlobalMaxPooling2D()(base.output)
    output = layers.Dropout(0.5)(output)
    output = layers.Dense(230, activation='softmax')(output)
    model = Model(inputs=inputs, outputs=output)
    opti = optimizers.Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(optimizer=opti, loss=losses.mean_squared_error, metrics=[metrics.categorical_accuracy])

    for i, layer in enumerate(model.layers):
        print(i, layer.name)

    output = layers.GlobalMaxPooling2D()(base.output)
    output = layers.Dense(1024, activation='sigmoid')(output)
    middle_layer_model = Model(inputs=model.input, outputs=output)

    return model, middle_layer_model
Ejemplo n.º 3
0
def cbam_block(tensor, ratio=8, activation='relu'):
    from keras import layers
    import keras.backend as K
    channel = tensor.shape[-1]
	
    avg_pool = layers.GlobalAveragePooling2D()(tensor)    
    avg_pool = layers.Reshape((1,1,channel))(avg_pool)
    avg_pool = layers.Dense(channel//ratio, activation= activation)(avg_pool)
    avg_pool = layers.Dense(channel, kernel_initializer='he_normal')(avg_pool)
  
    max_pool = layers.GlobalMaxPooling2D()(tensor)
    max_pool = layers.Reshape((1,1,channel))(max_pool)
    max_pool = layers.Dense(channel//ratio, activation= activation)(max_pool)
    max_pool = layers.Dense(channel, kernel_initializer='he_normal')(max_pool)
  
    channel_att = layers.add([avg_pool,max_pool])
    channel_att = layers.core.Activation('sigmoid')(channel_att)
    channel_att= layers.multiply([tensor, channel_att])
  
    #spatial attention
    avg_pool = layers.Lambda(lambda x: K.mean(x, axis=3, keepdims=True))(channel_att)
    max_pool = layers.Lambda(lambda x: K.max(x, axis=3, keepdims=True))(channel_att)
    concat = layers.concatenate([avg_pool, max_pool], axis= -1)
    spatial_att = layers.Conv2D(filters = 1,
            kernel_size=7,
            strides=1,
            padding='same',
            activation='sigmoid')(concat)	
      
    return layers.multiply([channel_att, spatial_att])
Ejemplo n.º 4
0
def Xception(input_shape=None,
             include_top=True,
             pooling='avg',
             n_class=14,
             pretrain_weights='imagenet'):
    """Instantiates the Xception architecture.
    
    Note that the default input image size for this model is 299x299.

    Args:
        input_shape: list or tuple, (height, width, channel)
        include_top: whether to include the fully-connected
            layer at the top of the network.
        pretrain_weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        n_class: int, num of class
        pooling: string, option of ['max', 'avg']
    Returns:

    Raise:

    Examples:

    """
    inputs = layers.Input(shape=input_shape)
    x = conv_bn_relu(inputs, 32, (3, 3), strides=(2, 2), name='block1_conv1')
    x = conv_bn_relu(x, 64, (3, 3), name='block1_conv2')

    x = xception_block1(x, [128, 128], (3, 3), activation=None, name='block2')
    x = xception_block1(x, [256, 256], (3, 3), name='block3')
    x = xception_block1(x, [728, 728], (3, 3), name='block4')

    for i in range(8):
        prefix = 'block' + str(i + 5)
        x = xception_block2(x, 728, (3, 3), name=prefix)
    # bottle neck
    x = xception_block1(x, [728, 1024], (3, 3), name='block13')
    x = sepconv_bn_relu(x, 1536, (3, 3), name='block14_conv1')
    x = sepconv_bn_relu(x, 2048, (3, 3), name='block14_conv2')
    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(n_class, activation='sigmoid', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)
    # Create model
    model = Model(inputs, x, name='xception')

    # Load weights
    if pretrain_weights == 'imagenet':
        load_imagenet_weights(model, include_top)
    elif os.path.exists(pretrain_weights):
        print("pretain from imagenet----", pretrain_weights)
        model.load_weights(pretrain_weights)
    else:
        print("weight path does not exist!")
    return model
Ejemplo n.º 5
0
 def init_model(self):
     conv_base = resnet50.ResNet50(weights='imagenet',
                                   include_top=False,
                                   input_shape=(input_size, input_size, 3))
     for layer in conv_base.layers[:]:
         if 'BatchNormalization' in str(layer):
             layer.trainable = True
         else:
             layer.trainable = False
     main_input = conv_base.input
     embedding = conv_base.output
     x = layers.GlobalMaxPooling2D()(embedding)
     x = layers.Dropout(0.5)(x)
     x = layers.Dense(4096,
                      activation='relu',
                      kernel_regularizer=regularizers.l2(0.01))(x)
     x = layers.BatchNormalization()(x)
     x = layers.Dropout(0.5)(x)
     x = layers.Dense(5004, activation='softmax')(x)
     model = models.Model(inputs=[main_input], outputs=[x])
     model.compile(loss='categorical_crossentropy',
                   optimizer='adam',
                   metrics=['acc'])
     self.model = model
     return model
Ejemplo n.º 6
0
def channel_attention(input_feature, ratio=8):
    channel = input_feature._keras_shape[-1]

    shared_layer_one = KL.Dense(int(channel / ratio),
                                activation='relu',
                                kernel_initializer='he_normal',
                                use_bias=True,
                                bias_initializer='zeros')
    shared_layer_two = KL.Dense(channel,
                                kernel_initializer='he_normal',
                                use_bias=True,
                                bias_initializer='zeros')

    avg_pool = KL.GlobalAveragePooling2D()(input_feature)
    avg_pool = KL.Reshape((1, 1, channel))(avg_pool)
    assert avg_pool._keras_shape[1:] == (1, 1, channel)
    avg_pool = shared_layer_one(avg_pool)
    assert avg_pool._keras_shape[1:] == (1, 1, channel // ratio)
    avg_pool = shared_layer_two(avg_pool)
    assert avg_pool._keras_shape[1:] == (1, 1, channel)

    max_pool = KL.GlobalMaxPooling2D()(input_feature)
    max_pool = KL.Reshape((1, 1, channel))(max_pool)
    assert max_pool._keras_shape[1:] == (1, 1, channel)
    max_pool = shared_layer_one(max_pool)
    assert max_pool._keras_shape[1:] == (1, 1, channel // ratio)
    max_pool = shared_layer_two(max_pool)
    assert max_pool._keras_shape[1:] == (1, 1, channel)

    cbam_feature = KL.Add()([avg_pool, max_pool])
    cbam_feature = KL.Activation('sigmoid')(cbam_feature)

    return KL.multiply([input_feature, cbam_feature])
Ejemplo n.º 7
0
def ResNet10(input_shape=None, include_top=True, pooling=None, classes=None):
    """
    Resnet10 backbone, attention: there is no identity block in resnet10
    :param img_input: the input tensor
    :param include_top:
    :param pooling: if include_tp=False, then we choose the pooling layer
    :param classes:
    :return: backbone model
    """

    img_input = layers.Input(shape=input_shape)
    if backend.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    x = layers.ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='valid',
                      kernel_initializer='he_normal',
                      name='conv1')(x)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = conv_block2(x, 3, [64, 64], stage=2, block='a', strides=(1, 1))

    x = conv_block2(x, 3, [128, 128], stage=3, block='a')

    x = conv_block2(x, 3, [256, 256], stage=4, block='a')

    x = conv_block2(x, 3, [512, 512], stage=5, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=3, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=4, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=5, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=6, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=7, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=8, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=9, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=10, block='a')
    # x = conv_block2(x, 3, [64, 64], stage=11, block='a')

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D()(x)
        else:
            print('The output shape of `ResNet10(include_top=False)` '
                  'has been changed ')

    inputs = img_input
    model = models.Model(inputs, x, name='resnet10')

    return model
def _channel_attention(input_feature, ratio=8):
    channel_dim = K.int_shape(input_feature)[-1]

    shared_layer_one = layers.Dense(channel_dim // ratio, activation='relu')
    shared_layer_two = layers.Dense(channel_dim)

    avg_pool = layers.GlobalAveragePooling2D()(input_feature)
    avg_pool = layers.Reshape((1, 1, channel_dim))(avg_pool)
    assert K.int_shape(avg_pool)[1:] == (1, 1, channel_dim)
    avg_pool = shared_layer_one(avg_pool)
    assert K.int_shape(avg_pool)[1:] == (1, 1, channel_dim // ratio)
    avg_pool = shared_layer_two(avg_pool)
    assert K.int_shape(avg_pool)[1:] == (1, 1, channel_dim)

    max_pool = layers.GlobalMaxPooling2D()(input_feature)
    max_pool = layers.Reshape((1, 1, channel_dim))(max_pool)
    assert K.int_shape(max_pool)[1:] == (1, 1, channel_dim)
    max_pool = shared_layer_one(max_pool)
    assert K.int_shape(max_pool)[1:] == (1, 1, channel_dim // ratio)
    max_pool = shared_layer_two(max_pool)
    assert K.int_shape(max_pool)[1:] == (1, 1, channel_dim)

    cbam_feature = layers.Add()([avg_pool, max_pool])
    cbam_feature = layers.Activation('sigmoid')(cbam_feature)

    return layers.multiply([input_feature, cbam_feature])
Ejemplo n.º 9
0
def vgg_graph(input_image, architecture, include_top=True, pooling=None, classes=1000):
    assert architecture in VGG_STRUCTURES
    assert pooling in ["avg", "max", None]

    x = input_image
    stages = VGG_STRUCTURES["VGG11"]
    stage_checkpoints = []
    for bl, stage in enumerate(stages):
        *k, f = stage["filters"]
        layers = stage["layers"]
        for cv, l in enumerate(range(layers)):
            cv_name = "block{}_pool{}".format(bl, cv)
            x = KL.Conv2D(f, k, activation='relu', padding='same', name=cv_name)(x)
        mp_name = "block{}_pool".format(bl)
        x = KL.MaxPooling2D((2, 2), strides=(2, 2), name=mp_name)(x)
        stage_checkpoints.append(x)

    if include_top:
        # Classification block
        x = KL.Flatten(name='flatten')(x)
        x = KL.Dense(4096, activation='relu', name='fc1')(x)
        x = KL.Dense(4096, activation='relu', name='fc2')(x)
        x = KL.Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = KL.GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = KL.GlobalMaxPooling2D()(x)
    stage_checkpoints.append(x)
    return stage_checkpoints
Ejemplo n.º 10
0
def ResNet34(include_top=True,
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000,
             **kwargs):
    if input_tensor is None:
        img_input = layers.Input(shape=input_shape)
    else:
        img_input = input_tensor
    bn_axis = 3
    endpoints = {}
    x = layers.Conv2D(64, (7, 7),
                      strides=(2, 2),
                      padding='same',
                      kernel_initializer='he_normal',
                      name='conv1')(img_input)
    x = layers.BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = layers.Activation('relu')(x)
    endpoints['stage_0/tensor'] = x
    x = layers.ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x)
    x = layers.MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
    endpoints['stage_1/tensor'] = x

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
    endpoints['stage_2/tensor'] = x

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
    endpoints['stage_3/tensor'] = x

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

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

    inputs = img_input
    # Create model.
    model = Model(inputs, x, name='resnet34')

    return model, endpoints
Ejemplo n.º 11
0
def create_model_multi_input(input_shapes,
                             yshape,
                             global_decay=5e-6) -> models.Model:
    """Create a multi input model for keras."""
    segments = []
    inputs = []
    for xshape in input_shapes:
        ix = layers.Input(shape=xshape)
        inputs.append(ix)
        x = layers.Conv2D(
            filters=32,
            kernel_size=4,
            activation="relu",
            strides=1,
            kernel_regularizer=regularizers.l2(global_decay),
        )(ix)
        x = layers.Conv2D(
            filters=48,
            kernel_size=3,
            activation="relu",
            strides=1,
            kernel_regularizer=regularizers.l2(global_decay),
        )(x)
        # x = layers.Conv2D(
        #     filters=48, kernel_size=2, activation="relu", strides=1,
        #     kernel_regularizer=regularizers.l2(global_decay),
        # )(x)
        x = layers.Conv2D(
            filters=64,
            kernel_size=2,
            activation="relu",
            strides=2,
            kernel_regularizer=regularizers.l2(global_decay),
        )(x)
        # x = layers.MaxPooling2D(pool_size=2, strides=2)(x)

        # x = layers.GlobalAveragePooling2D()(x)
        x = layers.GlobalMaxPooling2D()(x)
        segments.append(x)

    if len(segments) > 1:
        x = layers.concatenate(segments)
    else:
        x = segments[0]

    x = layers.Dense(
        units=64,
        activation="relu",
        # kernel_initializer="uniform",
        kernel_regularizer=regularizers.l2(global_decay))(x)
    x = layers.Dense(
        units=32,
        activation="relu",
        # kernel_initializer="uniform",
        kernel_regularizer=regularizers.l2(global_decay))(x)

    x = layers.Dense(units=yshape, activation="softmax")(x)

    model = models.Model(inputs=inputs, outputs=x)
    return model
Ejemplo n.º 12
0
def YAMNet(include_top=True,
           weights=None,
           input_tensor=None,
           input_shape=None,
           pooling='avg',
           classes=521,
           classifier_activation="sigmoid"):
    """Define the core YAMNet mode in Keras."""

    if input_tensor is None:
        input_shape = input_shape if input_shape is not None else (
            params.PATCH_FRAMES, params.PATCH_BANDS)

        input_tensor = layers.Input(shape=input_shape)

    net = layers.Reshape(input_shape + (1, ))(input_tensor)

    for (i, (layer_fun, kernel, stride,
             filters)) in enumerate(_YAMNET_LAYER_DEFS):
        net = layer_fun('layer{}'.format(i + 1), kernel, stride, filters)(net)

    if include_top:
        if weights is not None and classes != params.NUM_CLASSES:
            model_temp = Model(inputs=input_tensor, outputs=net)

            if weights is not None:
                model_temp.load_weights(weights)

            net = model_temp.output

        net = layers.GlobalAveragePooling2D()(net)
        logits = layers.Dense(units=classes, use_bias=True)(net)
        predictions = layers.Activation(
            name=params.EXAMPLE_PREDICTIONS_LAYER_NAME,
            activation=classifier_activation)(logits)

    else:
        if weights is not None:
            model_temp = Model(inputs=input_tensor, outputs=net)

            if weights is not None:
                model_temp.load_weights(weights)

            net = model_temp.output

        if pooling == 'avg':
            predictions = layers.GlobalAveragePooling2D()(net)
        elif pooling == 'max':
            predictions = layers.GlobalMaxPooling2D()(net)
        else:
            predictions = net

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

    if weights is not None and classes == params.NUM_CLASSES:
        model.load_weights(weights)

    return model
def create_inline_CNNs( embedding_dimension ):
    cnn_input_layer = layers.Input(shape=(64,embedding_dimension,1))
    
    tower_1_c = layers.Conv2D(256, kernel_size=(1,embedding_dimension), strides=(1,1), padding='valid', activation='relu' )(cnn_input_layer)
    tower_1_p = layers.GlobalMaxPooling2D()(tower_1_c)
    
    tower_2_c = layers.Conv2D(128, kernel_size=(2,embedding_dimension), strides=(1,1), padding='valid', activation='relu' )(cnn_input_layer)
    tower_2_p = layers.GlobalMaxPooling2D()(tower_2_c)
    
    tower_3_c = layers.Conv2D(64, kernel_size=(3,embedding_dimension), strides=(1,1), padding='valid', activation='relu' )(cnn_input_layer)
    tower_3_p = layers.GlobalMaxPooling2D()(tower_3_c)
    
    tower_4_c = layers.Conv2D(32, kernel_size=(4,embedding_dimension), strides=(1,1), padding='valid', activation='relu' )(cnn_input_layer)
    tower_4_p = layers.GlobalMaxPooling2D()(tower_4_c)
        
    merged = layers.concatenate([tower_1_p, tower_2_p, tower_3_p, tower_4_p])
    cnn_model = Model(cnn_input_layer, merged)
    cnn_model.summary()
    return cnn_model
Ejemplo n.º 14
0
def main():
    now = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
    model_name = 'pretrain_NASNet_' + now + '.h5'
    batch_size = 32
    num_epochs = 30
    lr = .0001

    num_train_samples = len(os.listdir('./data/train/cancer')) + len(os.listdir('./data/train/healthy'))
    num_valid_samples = len(os.listdir('./data/validation/cancer')) + len(os.listdir('./data/validation/healthy'))

    # Build our model
    input_tensor = Input(shape=(96, 96, 3))
    NASNet = NASNetMobile(include_top=False, input_shape=(96, 96, 3))
    x = NASNet(input_tensor)
    x1 = layers.GlobalMaxPooling2D()(x)
    x2 = layers.GlobalAveragePooling2D()(x)
    x3 = layers.Flatten()(x)
    z = layers.Concatenate(axis=-1)([x1, x2, x3])
    z = layers.Dropout(.5)(z)
    output_tensor = layers.Dense(1, activation='sigmoid')(z)

    model = Model(input_tensor, output_tensor)
    model.summary()

    # Get things ready to train: tweak learning rate, etc.
    model.compile(optimizer=Adam(lr), loss='binary_crossentropy', metrics=['acc'])

    train_generator = train_gen(batch_size)
    validation_generator = valid_gen(batch_size)

    steps_per_epoch = num_train_samples / batch_size
    validation_steps = num_valid_samples / batch_size

    # Basic callbacks
    checkpoint = callbacks.ModelCheckpoint(filepath='./models/' + model_name,
                                           monitor='val_loss',
                                           save_best_only=True)
    early_stop = callbacks.EarlyStopping(monitor='val_acc',
                                         patience=4)
    csv_logger = callbacks.CSVLogger('./logs/' + model_name.split('.')[0] + '.csv')

    callback_list = [checkpoint, early_stop, csv_logger]

    # Training begins
    history = model.fit_generator(train_generator,
                                  steps_per_epoch=steps_per_epoch,
                                  epochs=num_epochs,
                                  verbose=1,
                                  callbacks=callback_list,
                                  validation_data=validation_generator,
                                  validation_steps=validation_steps)

    model.save('./models/' + model_name)

    make_plots(history, model_name)
Ejemplo n.º 15
0
def pointnet2_cls_ssg(num_class, num_points, num_dim = 3):
    '''
    input:  BxNx3
    output: Bxnum_class
    '''
    input = keras.Input((num_points,num_dim)) # (batch, num_points, num_dim)
    inp = input
    
    if num_dim > 3:
        l0_xyz = crop(2, 0, 3)(input)
        l0_points = crop(2, 3, num_dim)(input)
        use_feature = True
    else : 
        l0_xyz = input
        l0_points = input # useless
        # for the first stage, there is no high level feature, only coordinate
        use_feature = False
    
    l1_xyz, l1_points, _ = pointnet_sa_module(l0_xyz, l0_points,
                                    n_centroid=512, radius=0.2,
                                    n_samples=32, mlp=[64,64,128],
                                    bn=True, relu6=False, use_xyz=True,
                                    use_feature=use_feature, random_sample=False)
    
    l2_xyz, l2_points, _ = pointnet_sa_module(l1_xyz, l1_points,
                                    n_centroid=128, radius=0.4,
                                    n_samples=64, mlp=[128,128,256],
                                    bn=True, relu6=False, use_xyz=True,
                                    use_feature=True, random_sample=False)
    '''
    l3_xyz, l3_points, _ = pointnet_sa_module(l2_xyz, l2_points,
                                    n_centroid=32, radius=0.6,
                                    n_samples=32, mlp=[256,512,1024],
                                    bn=True, relu6=False, use_xyz=True,
                                    use_feature=True) 
    x = layers.GlobalMaxPooling1D()(l3_points)                                
    # at this stage, no sampling or grouping, use PointNet layer directly 
    # as Keras don't support None as input or output
    # the original implementation doesn't work here
    '''
    # try this instead
    x = l2_points
    x = layers.Reshape((-1,1,256))(x)
    x = mlp_layers(x, [256, 512, 1024])
    x = layers.GlobalMaxPooling2D()(x)

    # fullly connected layers
    # x = layers.Flatten()(x) # (Batch, :)
    x = fully_connected(x, 512, bn=True, relu6=False, activation=True)
    x = layers.Dropout(0.5)(x)
    x = fully_connected(x, 256, bn=True, relu6 = False, activation=True)
    x = layers.Dropout(0.5)(x)
    x = fully_connected(x, num_class, bn=False, activation=False) # no BN nor ReLU here
    x = layers.Softmax()(x)
    return keras.models.Model(inputs=inp, outputs=x)
Ejemplo n.º 16
0
def define_cnn_architecture_cloud(fog_output,
                                  alpha,
                                  depth_multiplier,
                                  classes,
                                  include_top,
                                  pooling,
                                  dropout=1e-3):
    cloud = _depthwise_conv_block(fog_output,
                                  512,
                                  alpha,
                                  depth_multiplier,
                                  block_id=9)
    cloud = _depthwise_conv_block(cloud,
                                  512,
                                  alpha,
                                  depth_multiplier,
                                  block_id=10)
    cloud = _depthwise_conv_block(cloud,
                                  512,
                                  alpha,
                                  depth_multiplier,
                                  block_id=11)

    cloud = _depthwise_conv_block(cloud,
                                  1024,
                                  alpha,
                                  depth_multiplier,
                                  strides=(2, 2),
                                  block_id=12)
    cloud = _depthwise_conv_block(cloud,
                                  1024,
                                  alpha,
                                  depth_multiplier,
                                  block_id=13)

    if include_top:
        if K.image_data_format() == 'channels_first':
            shape = (int(1024 * alpha), 1, 1)
        else:
            shape = (1, 1, int(1024 * alpha))

        cloud = layers.GlobalAveragePooling2D()(cloud)
        cloud = layers.Reshape(shape, name='reshape_1')(cloud)
        cloud = layers.Dropout(dropout, name='dropout')(cloud)
        cloud = layers.Conv2D(classes, (1, 1),
                              padding='same',
                              name='conv_preds')(cloud)
        cloud = layers.Reshape((classes, ), name='reshape_2')(cloud)
        cloud_output = layers.Activation('softmax', name='output')(cloud)
    else:
        if pooling == 'avg':
            cloud_output = layers.GlobalAveragePooling2D()(cloud)
        elif pooling == 'max':
            cloud_output = layers.GlobalMaxPooling2D()(cloud)
    return cloud_output
Ejemplo n.º 17
0
 def create_branches(a, b):
     def sym(l, x, y):
         a = l(x)
         b = l(y)
         return a, b
     a, b = sym(layers.Conv2D(params["n_filter"], (params["h_filter"], 100)), a, b)
     a, b = sym(layers.GlobalMaxPooling2D(), a, b)
     for dim in params["d_encode"]:
         a, b = sym(layers.Dense(dim), a, b)
         a, b = sym(layers.Dropout(params["p_dropout"]),a , b)
     return a, b
Ejemplo n.º 18
0
def DenseNet(img_input,blocks,
             include_top=True,
             input_shape=None,
             pooling=None,
             classes=1000
             ):
    # img_input = layers.Input(shape=input_shape)

    x = layers.Reshape((input_shape[0], input_shape[1], -1))(img_input)

    x = dense_block(x, blocks[0], name='conv2')
    x = transition_block(x, 0.5, name='pool2')
    x = SEN_self_att(x,r=4,name='SEN2')

    x = dense_block(x, blocks[1], name='conv3')
    x = transition_block(x, 0.5, name='pool3')
    x = SEN_self_att(x, r=4, name='SEN3')

    x = dense_block(x, blocks[2], name='conv4')
    x = transition_block(x, 0.5, name='pool4')
    x = SEN_self_att(x, r=4, name='SEN4')

    x = dense_block(x, blocks[3], name='conv5')
    x = transition_block(x, 0.5, name='pool5')
    x = SEN_self_att(x, r=4, name='SEN5')

    if include_top:
        # x = layers.Reshape([1,K.int_shape(x)[-2],-1])(x)
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        # x = layers.Flatten()(x)
        x = layers.Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='max_pool')(x)

    # Ensure that the model takes into account
    # any potential predecessors
    #
    #  of `input_tensor`.
    inputs = img_input

    # Create model.
    if blocks == [6, 12, 24, 16]:
        model = models.Model(inputs, x, name='densenet121')
    elif blocks == [6, 12, 32, 32]:
        model = models.Model(inputs, x, name='densenet169')
    elif blocks == [6, 12, 48, 32]:
        model = models.Model(inputs, x, name='densenet201')
    else:
        model = models.Model(inputs, x, name='densenet')

    return model
Ejemplo n.º 19
0
def DenseNet(input_shape,
             blocks,
             include_top=True,
             regularization='dropout',
             dropout_prob=0.2,
             dropout_noise_shape=None,
             pooling=None,
             classes=10,
             reduction=0.5):
    assert regularization in [None, 'dropout']
    assert pooling in [None, 'avg', 'max']

    inp = layers.Input(shape=input_shape)

    bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1

    x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(inp)
    x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = layers.BatchNormalization(
        axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
    x = layers.Activation('relu', name='conv1/relu')(x)
    x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = layers.MaxPooling2D(3, strides=2, name='pool1')(x)

    i = 2

    for block in blocks:
        x = dense_block(x, block, name='conv{}'.format(i))

        if regularization is 'dropout':
            name = 'drop{}'.format(i)
            x = layers.Dropout(rate=dropout_prob, noise_shape=dropout_noise_shape, name=name)(x)

        if i <= len(blocks):
            x = transition_block(x, reduction, name='pool{}'.format(i))
        i = i + 1

    x = layers.BatchNormalization(
        axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
    x = layers.Activation('relu', name='relu')(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(classes, activation='softmax', name='fc{}'.format(classes))(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='max_pool')(x)

    model = models.Model(inp, x, name='densenet')

    return model
Ejemplo n.º 20
0
def mk_conv_32(*, channels):
    i = kr.Input((32, 32, 4), name='x0')

    cc_stack = [
        # kr.BatchNormalization(),
        kr.GaussianNoise(0.025),
        kr.Conv2D(10, 1, name='cconv_0'),
        kr.LeakyReLU(alpha=0.4),
        kr.Conv2D(channels, 1, name='cconv_1'),
        kr.LeakyReLU(alpha=0.4),
    ]

    h = apply_layers(i, cc_stack)

    conv_stack_0 = [
        kr.GaussianNoise(0.025),
        kr.Conv2D(64, 3, activation='relu'),
        kr.Conv2D(128, 3, activation='relu'),
        kr.Conv2D(256, 3, activation='relu'),
        kr.Conv2D(256, 3, strides=2, activation='relu'),
    ]

    h = apply_layers(h, conv_stack_0)
    ga0 = kr.GlobalAveragePooling2D()(h)
    gm0 = kr.GlobalMaxPooling2D()(h)

    conv_stack_1 = [
        kr.Conv2D(196, 3, activation='relu'),
        kr.Conv2D(196, 3, strides=2, activation='relu'),
        kr.Conv2D(196, 3, activation='relu'),
        kr.Flatten(),
    ]

    h = apply_layers(h, conv_stack_1)
    cat = kr.Concatenate()([h, gm0, ga0])

    head = [
        kr.Dropout(0.5),
        kr.Dense(512, activation='elu'),
        kr.Dropout(0.5),
        kr.Dense(256, activation='elu'),
        kr.Dropout(0.5),
        kr.Dense(Y_TRAIN.shape[1], activation='sigmoid', name='labs'),
    ]

    y = apply_layers(cat, head)

    m = krm.Model(inputs=[i], outputs=[y], name='conv_32')
    m.compile(loss=f2_crossentropy,
              optimizer='adam',
              metrics=['binary_accuracy'])

    return m
Ejemplo n.º 21
0
def train_classifier(train_gt, train_img_dir, fast_train=False):    
    names = os.listdir(train_img_dir)
    df = pd.DataFrame({'filename':names})
    df['class'] = [*map(lambda name: train_gt[name], df['filename'])]
    batch_size=10
    train_generator = ImageDataGenerator(
        rescale=1./255,
        rotation_range=10,
        zoom_range=0.2,
        horizontal_flip=True
    ).flow_from_dataframe(
        directory=train_img_dir,
        dataframe=df,
        target_size=(224, 224),
        batch_size=batch_size,
        class_mode='sparse',
        x_col='filename',
        y_col='class',
    )

    num_classes = 50
    xception = Xception()
    for layer in xception.layers: # first we optimize new layers only
        layer.trainable = False

    activation = xception.get_layer('block14_sepconv2_act').output
    pool = L.GlobalMaxPooling2D()(activation)
    dropout = L.Dropout(0.5)(pool)
    dense = L.Dense(200, activation='relu')(dropout)
    dense = L.Dense(num_classes, activation='softmax')(dense)
    model = keras.models.Model(inputs=xception.inputs, outputs=dense)
    model.compile(
        optimizer=Adam(lr=0.001), 
        loss='sparse_categorical_crossentropy', 
        metrics=['sparse_categorical_accuracy']
    )
    # localy this was done till convergence
    model.fit_generator(train_generator, steps_per_epoch= 10, verbose=1)
    
    # now optimize whole model
    for layer in model.layers:
        layer.trainable = True
    model.compile(
        optimizer=Adam(lr=0.0001), 
        loss='sparse_categorical_crossentropy', 
        metrics=['sparse_categorical_accuracy']
    )
    
    # localy this was done till convergence
    model.fit_generator(train_generator, steps_per_epoch= 10, verbose=1)
Ejemplo n.º 22
0
def create_discriminator():
    discriminator = keras.Sequential(
        [
            keras.Input(shape=(28, 28, 1)),
            layers.Conv2D(64, (3, 3), strides=(2, 2), padding="same"),
            layers.LeakyReLU(alpha=0.2),
            layers.Conv2D(128, (3, 3), strides=(2, 2), padding="same"),
            layers.LeakyReLU(alpha=0.2),
            layers.GlobalMaxPooling2D(),
            layers.Dense(1),
        ],
        name="discriminator",
    )
    discriminator.summary()
    return discriminator
Ejemplo n.º 23
0
def build_model():

    densenet = DenseNet121(weights='DenseNet-BC-121-32-no-top.h5',
                           include_top=False,
                           input_shape=(224, 224, 3))

    model = Sequential()
    model.add(densenet)
    model.add(layers.GlobalMaxPooling2D())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(5, activation='sigmoid'))

    model.compile(loss='binary_crossentropy',
                  optimizer=Adam(lr=0.0001),
                  metrics=['accuracy'])

    return model
Ejemplo n.º 24
0
def get_model(shape, class_num, kernel_heights=[2, 3, 4, 5]):
    row_num, col_num = shape
    inputs = layers.Input(shape=(row_num, col_num))
    reshape = layers.Reshape(target_shape=(row_num, col_num, 1))(inputs)
    convs = []
    for kernel_height in kernel_heights:
        x = layers.Conv2D(filters=256, kernel_size=(kernel_height, col_num), activation='relu')(reshape)
        x = layers.GlobalMaxPooling2D()(x)
        convs.append(x)
    my_concat = layers.Lambda(lambda x: K.concatenate(x, axis=1))
    concats = my_concat(convs)
    outputs = layers.Dropout(0.5)(concats)
    outputs = layers.Dense(class_num, activation='softmax')(outputs)
    model = models.Model(input=inputs, outputs=outputs)
    adam = Adam(lr=1e-3)
    model.compile(adam, 'categorical_crossentropy', metrics=['accuracy', acc_top3])
    model.summary()
    return model
Ejemplo n.º 25
0
def get_model(name: str):
    if name == 'dense':
        return keras.Sequential([
            layers.InputLayer(input_shape=(85,)),
            layers.Dense(70, name='hidden1', activation='relu'),
            layers.Dense(60, name='hidden2', activation='relu'),
            layers.Dense(30, name='hidden3', activation='relu'),
            layers.Dense(10, name='output', activation='softmax')
        ], 'poker_predictor')

    if name == 'conv':
        return keras.Sequential([
            layers.InputLayer(input_shape=(5, 17, 1)),
            layers.Conv2D(10, (5, 5), name='conv'),
            layers.GlobalMaxPooling2D(),
            layers.Dense(20, name='hidden1', activation='relu'),
            layers.Dense(20, name='hidden2', activation='relu'),
            layers.Dense(10, name='output', activation='softmax')
        ], 'poker_predictor')
Ejemplo n.º 26
0
def MnasNet(input_shape=None, alpha=1.0, depth_multiplier=1, pooling=None, nb_classes=10):
    img_input = layers.Input(shape=input_shape)
    
    first_block_filters = _make_divisible(32 * alpha, 8)
    x = _conv_block(img_input, strides=2, filters=first_block_filters)

    x = _sep_conv_block(x, filters=16, alpha=alpha, 
                        pointwise_conv_filters=16, depth_multiplier=depth_multiplier)
    
    x = _inverted_res_block(x, kernel=3, expansion=3, stride=2, alpha=alpha, filters=24, block_id=1)
    x = _inverted_res_block(x, kernel=3, expansion=3, stride=1, alpha=alpha, filters=24, block_id=2)
    x = _inverted_res_block(x, kernel=3, expansion=3, stride=1, alpha=alpha, filters=24, block_id=3)
    
    x = _inverted_res_block(x, kernel=5, expansion=3, stride=2, alpha=alpha, filters=40, block_id=4)
    x = _inverted_res_block(x, kernel=5, expansion=3, stride=1, alpha=alpha, filters=40, block_id=5)
    x = _inverted_res_block(x, kernel=5, expansion=3, stride=1, alpha=alpha, filters=40, block_id=6)
    
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=2, alpha=alpha, filters=80, block_id=7)
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=1, alpha=alpha, filters=80, block_id=8)
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=1, alpha=alpha, filters=80, block_id=9)

    x = _inverted_res_block(x, kernel=3, expansion=6, stride=1, alpha=alpha, filters=96, block_id=10)
    x = _inverted_res_block(x, kernel=3, expansion=6, stride=1, alpha=alpha, filters=96, block_id=11)
    
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=2, alpha=alpha, filters=192, block_id=12)
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=1, alpha=alpha, filters=192, block_id=13)
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=1, alpha=alpha, filters=192, block_id=14)
    x = _inverted_res_block(x, kernel=5, expansion=6, stride=1, alpha=alpha, filters=192, block_id=15)
    
    x = _inverted_res_block(x, kernel=3, expansion=6, stride=1, alpha=alpha, filters=320, block_id=16)
    
    if pooling == 'avg':
        x = layers.GlobalAveragePooling2D()(x)
    else:
        x = layers.GlobalMaxPooling2D()(x)
        
    x = layers.Dense(nb_classes, activation='softmax', use_bias=True, name='proba')(x)
    
    inputs = img_input
    
    model = models.Model(inputs, x, name='mnasnet')
    return model
Ejemplo n.º 27
0
def channel_attention(input_feature, ratio=8):
    print('input feature shape', input_feature._keras_shape)
    channel = input_feature._keras_shape[1]

    shared_layer_one = layers.Dense(channel // ratio,
                                    activation='relu',
                                    kernel_initializer='he_normal',
                                    use_bias=True,
                                    bias_initializer='zeros')

    shared_layer_two = layers.Dense(channel,
                                    kernel_initializer='he_normal',
                                    use_bias=True,
                                    bias_initializer='zeros')

    avg_pool = layers.GlobalAveragePooling2D()(input_feature)
    print('before reshpae avg pool', avg_pool._keras_shape)
    avg_pool = layers.Reshape((channel, 1, 1))(avg_pool)
    print('after reshpae avg pool', avg_pool._keras_shape)

    avg_pool = shared_layer_one(avg_pool)
    print('after shared layer 01 avg pool', avg_pool._keras_shape)

    avg_pool = shared_layer_two(avg_pool)
    print('after shared layer 02 avg pool', avg_pool._keras_shape)

    max_pool = layers.GlobalMaxPooling2D()(input_feature)
    print('before reshpae max pool', max_pool._keras_shape)

    max_pool = layers.Reshape((channel, 1, 1))(max_pool)
    print('after reshpae max pool', max_pool._keras_shape)

    max_pool = shared_layer_one(max_pool)
    print('after shared layer 01 max pool', max_pool._keras_shape)

    max_pool = shared_layer_two(max_pool)
    print('before shared layer 02 max pool', max_pool._keras_shape)

    cbam_feature = layers.Add()([avg_pool, max_pool])
    cbam_feature = layers.Activation('sigmoid')(cbam_feature)

    return layers.multiply([input_feature, cbam_feature])
Ejemplo n.º 28
0
 def __init__(self):
     super(Channel_attention, self).__init__()
     self.reduction_ratio = 0.125
     self.channel_axis = 1 if K.image_data_format(
     ) == "channels_first" else 3
     self.maxpool = KL.GlobalMaxPooling2D()
     self.avgpool = KL.GlobalAvgPool2D()
     self.sigmoid = KL.Activation('sigmoid')
     self.channel = 128
     self.Dense_One = KL.Dense(units=int(self.channel *
                                         self.reduction_ratio),
                               activation='relu',
                               kernel_initializer='he_normal',
                               use_bias=True,
                               bias_initializer='zeros')
     self.Dense_Two = KL.Dense(units=int(self.channel),
                               activation='relu',
                               kernel_initializer='he_normal',
                               use_bias=True,
                               bias_initializer='zeros')
def build_densenet(blocks,
                   include_top=True,
                   input_shape=None,
                   pooling=None,
                   num_classes=1000):

    img_input = layers.Input(shape=input_shape)

    bn_axis = 3 if backend.image_data_format() == 'channels_last' else 1

    x = layers.ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
    x = layers.Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = layers.BatchNormalization(axis=bn_axis,
                                  epsilon=1.001e-5,
                                  name='conv1/bn')(x)
    x = layers.Activation('relu', name='conv1/relu')(x)
    x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = layers.MaxPooling2D(3, strides=2, name='pool1')(x)

    for i, block in blocks:
        conv_name = 'conv{}'.format(i + 1)
        pool_name = 'pool{}'.format(i + 1)
        x = dense_block(x, block, name=conv_name)
        x = transition_block(x, 0.5, name=pool_name)

    x = layers.BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)
    x = layers.Activation('relu', name='relu')(x)

    if include_top:
        x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        x = layers.Dense(num_classes, activation='softmax', name='fc')(x)
    else:
        if pooling == 'avg':
            x = layers.GlobalAveragePooling2D(name='avg_pool')(x)
        elif pooling == 'max':
            x = layers.GlobalMaxPooling2D(name='max_pool')(x)

    model = models.Model(img_input, x, name='densenet')

    return model
def build_model_Inception_Resnet():

    inception_resnet = inception_resnet_v2.InceptionResNetV2(
    weights='imagenet',
    include_top=False,
    input_shape=(224,224,3)
    )

    model = Sequential()
    model.add(inception_resnet)
    model.add(layers.GlobalMaxPooling2D())
    model.add(layers.Dropout(0.5))
    
    model.add(layers.Dense(2, activation='sigmoid'))
    
    model.compile(
        loss='categorical_crossentropy',
        optimizer=Adam(lr=0.0005),
        metrics=['accuracy']
    )
    
    return model