Ejemplo n.º 1
0
def head(endpoints, embedding_dim, is_training):
    endpoints['emb1'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_1_pointwise'])
    endpoints['fc_layer1_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb1'])
    endpoints['bn_1_1'] = BN()(endpoints['fc_layer1_1'])
    endpoints['feature1'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(endpoints['bn_1_1'])

    endpoints['emb2'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_3_pointwise'])
    endpoints['fc_layer2_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb2'])
    endpoints['bn_2_1'] = BN()(endpoints['fc_layer2_1'])
    endpoints['feature2'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(endpoints['bn_2_1'])

    endpoints['emb3'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_5_pointwise'])
    endpoints['fc_layer3_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb3'])
    endpoints['bn_3_1'] = BN()(endpoints['fc_layer3_1'])
    endpoints['feature3'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(endpoints['bn_3_1'])

    endpoints['emb4'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_11_pointwise'])
    endpoints['fc_layer4_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb4'])
    endpoints['bn_4_1'] = BN()(endpoints['fc_layer4_1'])
    endpoints['feature4'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(endpoints['bn_4_1'])

    endpoints['emb5'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_13_pointwise'])
    endpoints['fc_layer5_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb5'])
    endpoints['bn_5_1'] = BN()(endpoints['fc_layer5_1'])
    endpoints['feature5'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(endpoints['bn_5_1'])

    endpoints['fusion_layer'] = FusionLayer()([endpoints['feature1'], endpoints['feature2'], endpoints['feature3'],
                                               endpoints['feature4'],endpoints['feature5']])

    endpoints['emb'] = endpoints['emb_raw'] = endpoints['fusion_layer']
    return endpoints
Ejemplo n.º 2
0
def Conv2D_Acti_BN(input_tensor, activation, *args):
    output_tensor = Conv2D(*args,
                           activation=activation,
                           padding='same',
                           kernel_initializer='he_normal')(input_tensor)
    output_tensor = BN()(output_tensor)
    return output_tensor
Ejemplo n.º 3
0
def Conv2D_BN_Leaky(input_tensor, *args):
    output_tensor = Conv2D(*args,
                           padding='same',
                           kernel_initializer='he_normal')(input_tensor)
    output_tensor = BN()(output_tensor)
    output_tensor = LeakyReLU(alpha=0.1)(output_tensor)
    return output_tensor
Ejemplo n.º 4
0
def model_wj(ts_in:Input):
    
    ts_z = ts_in
    
    '''-----Embadding'''
    ### 256->128
    ts_z = Conv2D( filters=16, kernel_size=3, strides=2, padding='same', use_bias=False )(ts_z)
    ts_z = BN()(ts_z)
    ts_z = GELU()(ts_z)
    ts_z = Conv2D( filters=32, kernel_size=3, strides=1, padding='same', use_bias=False )(ts_z)
    
    '''----Body'''   
    def BRC(x,filters,down=False):
        s = 2 if down else 1
        Cin = x.shape[3]
        
        if   Cin==filters and down==False:
            P = x        
        else:       
            P = Conv2D( filters=filters, kernel_size=1, strides=s, padding='same', use_bias=False )(x)            
            P = BN()(P)
        
        if down :
            x = AveragePooling2D()(x)
        x = BN()(x)
        x = GELU()(x)
        x = Conv2D( filters=filters, kernel_size=5, strides=1, padding='same', use_bias=False )(x)
#         x = BN()(x)
#         x = GELU()(x)
#         x = Conv2D( filters=filters, kernel_size=3, padding='same', use_bias=False )(x)
        x = P+x
        return x
        
    ### 128->64
    ts_z = BRC(ts_z,filters=64,down=True)
    
    ### 64->32
    ts_z = BRC(ts_z,filters=128,down=True)
    
    ### 32->16
    ts_z = BRC(ts_z,filters=256,down=True)
    
    '''----Exit'''
#     ts_z = BRC(ts_z,filters=128)
    
    ### 16->8
    ts_z = BRC(ts_z,filters=512,down=True)
    
    '''----GAP'''
    ts_feature = GAP2D()(ts_z)
    ts_z = ts_feature
        
    '''----FC'''
    ts_z = Dense( units=26, activation=None, use_bias=False)(ts_z)
    ts_out = Activation('linear', dtype=tf.float32)(ts_z)
        
    model = keras.Model(inputs=ts_in,outputs=ts_out)
        
    return model
Ejemplo n.º 5
0
    def BRC(x,filters,down=False):
        s = 2 if down else 1
        Cin = x.shape[3]
        
        if   Cin==filters and down==False:
            P = x        
        else:       
            P = Conv2D( filters=filters, kernel_size=1, strides=s, padding='same', use_bias=False )(x)            
            P = BN()(P)
        
        if down :
            x = AveragePooling2D()(x)
        x = BN()(x)
        x = GELU()(x)
        x = Conv2D( filters=filters, kernel_size=5, strides=1, padding='same', use_bias=False )(x)
#         x = BN()(x)
#         x = GELU()(x)
#         x = Conv2D( filters=filters, kernel_size=3, padding='same', use_bias=False )(x)
        x = P+x
        return x
Ejemplo n.º 6
0
    def build(self, classes=CLASSES):
        INPUT_SHAPE = (self.height, self.width, self.dept)
        channel_dimension = -1

        if k == "channels_first":
            INPUT_SHAPE = (self.dept, self.height, self.width)
            channel_dimension = 1
        if classes > 2:
            FINAL_ACTIVATION = "softmax"
        model = Sequential()
        # first layer
        model.add(Conv2D(32, kernel_size=KERNEL_SIZE, padding=PADDING, activation=ACTIVATION, input_shape=INPUT_SHAPE))
        model.add(BN(axis=channel_dimension))

        # second layer
        model.add(Conv2D(32, kernel_size=KERNEL_SIZE, padding=PADDING, activation=ACTIVATION))
        model.add(BN(axis=channel_dimension))
        model.add(MaxPool2D(pool_size=POOL_SIZE, padding=PADDING))
        model.add(Dropout(rate=DROPOUT1))

        # third layer
        model.add(Conv2D(64, kernel_size=KERNEL_SIZE, padding=PADDING, activation=ACTIVATION))
        model.add(BN(axis=channel_dimension))

        # fourth layer
        model.add(Conv2D(64, kernel_size=KERNEL_SIZE, padding=PADDING, activation=ACTIVATION))
        model.add(BN(axis=channel_dimension))
        model.add(MaxPool2D(pool_size=POOL_SIZE))
        model.add(Dropout(rate=DROPOUT1))

        # last layer
        model.add(Flatten())
        model.add(Dense(units=500, activation=ACTIVATION))
        model.add(BN(axis=channel_dimension))
        model.add(Dropout(rate=DROPOUT2))
        model.add(Dense(units=classes, activation=FINAL_ACTIVATION))
        
        return model
Ejemplo n.º 7
0
for i in range(K - 1):
    fltr = fltr.dot(fltr)
fltr.sort_indices()

# Model definition

#GCN input
area_in = Input(shape=(F, ))  #area feature
fltr_in = Input((N, ), sparse=True)  #adjacent matrix

bn0 = BN(
    axis=-1,
    momentum=0.99,
    epsilon=0.001,
    center=True,
    scale=True,
    beta_initializer='zeros',
    gamma_initializer='ones',
    moving_mean_initializer='zeros',
    moving_variance_initializer='ones',
    renorm_momentum=0.99,
)(area_in)  #batch norm on the area data

if not include_batch_norm_layers:
    bn0 = area_in

if not include_batch_norm_layers:
    g1 = GraphConv(
        embedding_vecor_length1,  #first graph conv layer
        activation='relu',
        kernel_regularizer=l2(l2_reg),
        use_bias=True)([bn0, fltr_in])
Ejemplo n.º 8
0
def head(endpoints, embedding_dim, backbone_model, is_training):
    if backbone_model == 'mobilenet_v1_1_224':
        endpoints['emb1'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_1_pointwise'])
        endpoints['fc_layer1_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb1'])
        endpoints['bn_1_1'] = BN()(endpoints['fc_layer1_1'])
        endpoints['feature1'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_1_1'])

        endpoints['emb2'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_3_pointwise'])
        endpoints['fc_layer2_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb2'])
        endpoints['bn_2_1'] = BN()(endpoints['fc_layer2_1'])
        endpoints['feature2'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_2_1'])

        endpoints['emb3'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_5_pointwise'])
        endpoints['fc_layer3_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb3'])
        endpoints['bn_3_1'] = BN()(endpoints['fc_layer3_1'])
        endpoints['feature3'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_3_1'])

        endpoints['emb4'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_11_pointwise'])
        endpoints['fc_layer4_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb4'])
        endpoints['bn_4_1'] = BN()(endpoints['fc_layer4_1'])
        endpoints['feature4'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_4_1'])

        endpoints['emb5'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['Conv2d_13_pointwise'])
        endpoints['fc_layer5_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb5'])
        endpoints['bn_5_1'] = BN()(endpoints['fc_layer5_1'])
        endpoints['feature5'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_5_1'])

        endpoints['fusion_layer'] = FusionLayer_mob()([endpoints['feature1'], endpoints['feature2'],
                                                       endpoints['feature3'], endpoints['feature4'],
                                                       endpoints['feature5']])

        endpoints['emb'] = endpoints['emb_raw'] = endpoints['fusion_layer']
        return endpoints

    elif backbone_model == 'resnet_v1_50':
        endpoints['emb1'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['resnet_v1_50/block1'])
        endpoints['fc_layer1_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb1'])
        endpoints['bn_1_1'] = BN()(endpoints['fc_layer1_1'])
        endpoints['feature1'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_1_1'])

        endpoints['emb2'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['resnet_v1_50/block2'])
        endpoints['fc_layer2_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb2'])
        endpoints['bn_2_1'] = BN()(endpoints['fc_layer2_1'])
        endpoints['feature2'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_2_1'])

        endpoints['emb3'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['resnet_v1_50/block3'])
        endpoints['fc_layer3_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb3'])
        endpoints['bn_3_1'] = BN()(endpoints['fc_layer3_1'])
        endpoints['feature3'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_3_1'])

        endpoints['emb4'] = GlobalAveragePooling2D(data_format='channels_last')(endpoints['resnet_v1_50/block4'])
        endpoints['fc_layer4_1'] = Dense(1024, activation='relu', kernel_initializer='Orthogonal')(endpoints['emb4'])
        endpoints['bn_4_1'] = BN()(endpoints['fc_layer4_1'])
        endpoints['feature4'] = Dense(embedding_dim, activation=None, kernel_initializer='Orthogonal')(
            endpoints['bn_4_1'])

        endpoints['fusion_layer'] = FusionLayer_res()(
            [endpoints['feature1'], endpoints['feature2'], endpoints['feature3'],
             endpoints['feature4']])

        endpoints['emb'] = endpoints['emb_raw'] = endpoints['fusion_layer']
        return endpoints

    else:
        print('no such model, failure to build merged head layer')
        exit(1)
Ejemplo n.º 9
0
x = keras.layers.maximum([c1, c2])
x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same')(x)
x = Dropout(.2)(x)

c1 = Conv2D(filters=32,
            kernel_size=1,
            strides=(1, 1),
            padding='same',
            activation=None)(x)
c2 = Conv2D(filters=32,
            kernel_size=1,
            strides=(1, 1),
            padding='same',
            activation=None)(x)
x = keras.layers.maximum([c1, c2])
x = BN(axis=3, scale=False)(x)
x = Dropout(.2)(x)

c1 = Conv2D(filters=48,
            kernel_size=3,
            strides=(1, 1),
            padding='same',
            activation=None)(x)
c2 = Conv2D(filters=48,
            kernel_size=3,
            strides=(1, 1),
            padding='same',
            activation=None)(x)
x = keras.layers.maximum([c2, c1])
x = Dropout(.2)(x)
x = MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='same')(x)