Ejemplo n.º 1
0
def EEGNet_fusion(nb_classes, Chans=64, Samples=128,
                  dropoutRate=0.5, norm_rate=0.25, dropoutType='Dropout', cpu=False):
    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.')
    if cpu:
        input_shape = (Samples, Chans, 1)
        conv_filters = (64, 1)
        conv_filters2 = (96, 1)
        conv_filters3 = (128, 1)

        depth_filters = (1, Chans)
        pool_size = (4, 1)
        pool_size2 = (8, 1)
        separable_filters = (8, 1)
        separable_filters2 = (16, 1)
        separable_filters3 = (32, 1)

        axis = -1
    else:
        input_shape = (1, Chans, Samples)
        conv_filters = (1, 64)
        conv_filters2 = (1, 96)
        conv_filters3 = (1, 128)

        depth_filters = (Chans, 1)
        pool_size = (1, 4)
        pool_size2 = (1, 8)
        separable_filters = (1, 8)
        separable_filters2 = (1, 16)
        separable_filters3 = (1, 32)

        axis = 1

    F1 = 8
    F1_2 = 16
    F1_3 = 32
    F2 = 16
    F2_2 = 32
    F2_3 = 64
    D = 2
    D2 = 2
    D3 = 2

    input1 = Input(shape=input_shape)
    block1 = Conv2D(F1, conv_filters, padding='same',
                    input_shape=input_shape,
                    use_bias=False)(input1)
    block1 = BatchNormalization(axis=axis)(block1)
    block1 = DepthwiseConv2D(depth_filters, use_bias=False,
                             depth_multiplier=D,
                             depthwise_constraint=max_norm(1.))(block1)
    block1 = BatchNormalization(axis=axis)(block1)
    block1 = Activation('elu')(block1)
    block1 = AveragePooling2D(pool_size)(block1)
    block1 = dropoutType(dropoutRate)(block1)

    block2 = SeparableConv2D(F2, separable_filters,
                             use_bias=False, padding='same')(block1)  # 8
    block2 = BatchNormalization(axis=axis)(block2)
    block2 = Activation('elu')(block2)
    block2 = AveragePooling2D(pool_size2)(block2)
    block2 = dropoutType(dropoutRate)(block2)
    block2 = Flatten()(block2)  # 13

    # 8 - 13

    input2 = Input(shape=input_shape)
    block3 = Conv2D(F1_2, conv_filters2, padding='same',
                    input_shape=input_shape,
                    use_bias=False)(input2)
    block3 = BatchNormalization(axis=axis)(block3)
    block3 = DepthwiseConv2D(depth_filters, use_bias=False,
                             depth_multiplier=D2,
                             depthwise_constraint=max_norm(1.))(block3)
    block3 = BatchNormalization(axis=axis)(block3)
    block3 = Activation('elu')(block3)
    block3 = AveragePooling2D(pool_size)(block3)
    block3 = dropoutType(dropoutRate)(block3)

    block4 = SeparableConv2D(F2_2, separable_filters2,
                             use_bias=False, padding='same')(block3)  # 22
    block4 = BatchNormalization(axis=axis)(block4)
    block4 = Activation('elu')(block4)
    block4 = AveragePooling2D(pool_size2)(block4)
    block4 = dropoutType(dropoutRate)(block4)
    block4 = Flatten()(block4)  # 27
    # 22 - 27

    input3 = Input(shape=input_shape)
    block5 = Conv2D(F1_3, conv_filters3, padding='same',
                    input_shape=input_shape,
                    use_bias=False)(input3)
    block5 = BatchNormalization(axis=axis)(block5)
    block5 = DepthwiseConv2D(depth_filters, use_bias=False,
                             depth_multiplier=D3,
                             depthwise_constraint=max_norm(1.))(block5)
    block5 = BatchNormalization(axis=axis)(block5)
    block5 = Activation('elu')(block5)
    block5 = AveragePooling2D(pool_size)(block5)
    block5 = dropoutType(dropoutRate)(block5)

    block6 = SeparableConv2D(F2_3, separable_filters3,
                             use_bias=False, padding='same')(block5)  # 36
    block6 = BatchNormalization(axis=axis)(block6)
    block6 = Activation('elu')(block6)
    block6 = AveragePooling2D(pool_size2)(block6)
    block6 = dropoutType(dropoutRate)(block6)
    block6 = Flatten()(block6)  # 41

    # 36 - 41

    merge_one = concatenate([block2, block4])
    merge_two = concatenate([merge_one, block6])

    flatten = Flatten()(merge_two)

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

    softmax = Activation('softmax', name='softmax')(dense)

    return Model(inputs=[input1, input2, input3], outputs=softmax)
Ejemplo n.º 2
0
    def build(width, height, depth, classes):
        # init the input shape to be the channel last and the channels
        # dimension itself
        input_shape = height, width, depth
        chan_dim = -1

        if K.image_data_format() == 'channel_first':
            input_shape = depth, height, width
            chan_dim = 1

        # define model input
        inputs = Input(shape=input_shape)
        x = MiniGoogleNet.conv_module(x=inputs,
                                      k=96,
                                      kx=3,
                                      ky=3,
                                      stride=(1, 1),
                                      chan_dim=chan_dim)

        # two inception modules followed by downsample module
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=32,
                                           numx3=32,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=32,
                                           numx3=48,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.downsample_module(x=x, k=80, chan_dim=chan_dim)

        # 4 inception then downsample
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=112,
                                           numx3=48,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=96,
                                           numx3=64,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=80,
                                           numx3=80,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=48,
                                           numx3=96,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.downsample_module(x=x, k=96, chan_dim=chan_dim)

        # two inception modules followed by global pool and dropout
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=176,
                                           numx3=160,
                                           chan_dim=chan_dim)
        x = MiniGoogleNet.inception_module(x=x,
                                           numx1=176,
                                           numx3=160,
                                           chan_dim=chan_dim)
        x = AveragePooling2D(pool_size=(7, 7))(x)
        x = Dropout(0.5)(x)

        # softmax classifier
        x = Flatten()(x)
        x = Dense(classes)(x)
        x = Activation("softmax")(x)

        # create the model
        model = Model(inputs, x, name="googlenet")
        return model
Ejemplo n.º 3
0
def ASPP(tensor):
    '''atrous spatial pyramid pooling'''
    dims = K.int_shape(tensor)

    y_pool = AveragePooling2D(pool_size=(dims[1], dims[2]),
                              name='average_pooling')(tensor)
    y_pool = Conv2D(filters=256,
                    kernel_size=1,
                    padding='same',
                    kernel_initializer='he_normal',
                    name='pool_1x1conv2d',
                    use_bias=False)(y_pool)
    y_pool = BatchNormalization(name=f'bn_1')(y_pool)
    y_pool = Activation('relu', name=f'relu_1')(y_pool)

    y_pool = Upsample(tensor=y_pool, size=[dims[1], dims[2]])

    y_1 = Conv2D(filters=256,
                 kernel_size=1,
                 dilation_rate=1,
                 padding='same',
                 kernel_initializer='he_normal',
                 name='ASPP_conv2d_d1',
                 use_bias=False)(tensor)
    y_1 = BatchNormalization(name=f'bn_2')(y_1)
    y_1 = Activation('relu', name=f'relu_2')(y_1)

    y_6 = Conv2D(filters=256,
                 kernel_size=3,
                 dilation_rate=6,
                 padding='same',
                 kernel_initializer='he_normal',
                 name='ASPP_conv2d_d6',
                 use_bias=False)(tensor)
    y_6 = BatchNormalization(name=f'bn_3')(y_6)
    y_6 = Activation('relu', name=f'relu_3')(y_6)

    y_12 = Conv2D(filters=256,
                  kernel_size=3,
                  dilation_rate=12,
                  padding='same',
                  kernel_initializer='he_normal',
                  name='ASPP_conv2d_d12',
                  use_bias=False)(tensor)
    y_12 = BatchNormalization(name=f'bn_4')(y_12)
    y_12 = Activation('relu', name=f'relu_4')(y_12)

    y_18 = Conv2D(filters=256,
                  kernel_size=3,
                  dilation_rate=18,
                  padding='same',
                  kernel_initializer='he_normal',
                  name='ASPP_conv2d_d18',
                  use_bias=False)(tensor)
    y_18 = BatchNormalization(name=f'bn_5')(y_18)
    y_18 = Activation('relu', name=f'relu_5')(y_18)

    y = concatenate([y_pool, y_1, y_6, y_12, y_18], name='ASPP_concat')

    y = Conv2D(filters=256,
               kernel_size=1,
               dilation_rate=1,
               padding='same',
               kernel_initializer='he_normal',
               name='ASPP_conv2d_final',
               use_bias=False)(y)
    y = BatchNormalization(name=f'bn_final')(y)
    y = Activation('relu', name=f'relu_final')(y)
    return y
Ejemplo n.º 4
0
# initialize the training data augmentation object
trainAug = ImageDataGenerator(
	  rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
 	  fill_mode="nearest")
      
      
baseModel = VGG16(weights="imagenet", include_top=False,
	input_tensor=Input(shape=(224, 224, 3)))

# construct the head of the model that will be placed on top of the
# the base model
headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(3, activation="softmax")(headModel)

# place the head FC model on top of the base model (this will become
# the actual model we will train)
model = Model(inputs=baseModel.input, outputs=headModel)

# loop over all layers in the base model and freeze them so they will
# *not* be updated during the first training process
for layer in baseModel.layers:
	layer.trainable = False

# compile our model
Ejemplo n.º 5
0
def EEGNet(nb_classes,
           Chans=64,
           Samples=128,
           dropoutRate=0.5,
           kernLength=64,
           F1=8,
           D=2,
           F2=16,
           norm_rate=0.25,
           EnK=True,
           dropoutType='Dropout'):
    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.')
    # with tf.Graph().as_default() as g:
    input1 = Input(shape=(1, Chans, Samples))

    block1 = Conv2D(F1, (1, kernLength),
                    padding='same',
                    input_shape=(1, Chans, Samples),
                    use_bias=False,
                    data_format='channels_first')(input1)

    block1 = EnKLayer(filter=F1,
                      EnK=EnK,
                      InputData_shape=(1, Chans, Samples),
                      output_dimension=(1, 1, kernLength),
                      conv2DOutput=block1)(input1)

    block1 = BatchNormalization(axis=1)(block1)
    block1 = DepthwiseConv2D((Chans, 1),
                             use_bias=False,
                             depth_multiplier=D,
                             data_format='channels_first',
                             depthwise_constraint=max_norm(1.))(block1)
    block1 = BatchNormalization(axis=1)(block1)  # changed by avinash
    block1 = Activation('elu')(block1)

    block1 = AveragePooling2D(
        (1, 4),
        data_format='channels_first',
    )(block1)
    block1 = dropoutType(dropoutRate)(block1)

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

    flatten = Flatten(name='flatten', data_format='channels_first')(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.º 6
0
def ResNet(input_shape,
           num_classes=1000,
           block_fn=_residual_block_basic,
           repetitions=(2, 2, 2, 2),
           use_bias=False,
           kernel_initializer='he_normal',
           kernel_regularizer=regulizers.l2(1e-4)):
    """
    Build a ResNet model for classification.
    :param input_shape:             Input shape (e.g. (224, 224, 3))
    :param num_classes:             Number of classes to predict
    :param block_fn:                Block layer method to be used.
    :param repetitions:             List of repetitions for each macro-blocks the network should contain.
    :param use_bias:                Flag to use bias or not in Conv layer.
    :param kernel_initializer:      Kernel initialisation method name.
    :param kernel_regularizer:      Kernel regularizer.
    :return:                        ResNet model.
    """

    # Input and 1st layers:
    inputs = Input(shape=input_shape)
    conv = _res_conv(filters=64,
                     kernel_size=7,
                     strides=2,
                     use_relu=True,
                     use_bias=use_bias,
                     kernel_initializer=kernel_initializer,
                     kernel_regularizer=kernel_regularizer)(inputs)
    maxpool = MaxPooling2D(pool_size=3, strides=2, padding='same')(conv)

    # Chain of residual blocks:
    filters = 64
    strides = 2
    res_block = maxpool
    for i, repet in enumerate(repetitions):
        # We do not further reduce the input size for the 1st block (max-pool applied just before):
        block_strides = strides if i != 0 else 1
        macroblock_name = "block_{}".format(i)
        res_block = _residual_macroblock(
            block_fn=block_fn,
            repetitions=repet,
            name=macroblock_name,
            filters=filters,
            strides_1st_block=block_strides,
            use_bias=use_bias,
            kernel_initializer=kernel_initializer,
            kernel_regularizer=kernel_regularizer)(res_block)
        filters = min(filters * 2, 1024)  # we limit to 1024 filters max

    # Final layers for prediction:
    res_spatial_dim = tf.keras.backend.int_shape(res_block)[1:3]
    avg_pool = AveragePooling2D(pool_size=res_spatial_dim,
                                strides=1)(res_block)
    flatten = Flatten()(avg_pool)
    predictions = Dense(units=num_classes,
                        kernel_initializer=kernel_initializer,
                        activation='softmax')(flatten)

    # Model:
    model = Model(inputs=inputs, outputs=predictions)
    return model
Ejemplo n.º 7
0
def resnet18(input_shape=(224,224,3), classes=1000):

    inputs = Input(shape=input_shape)

    # conv1
    layer1 = resnet_layer(inputs=inputs,num_filters=64, strides=(2,2), kernel_size=(7,7),activation='relu')
    conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(layer1)
    pool1_helper = PoolHelper()(conv1_zero_pad)
    pool_1 = MaxPooling2D(pool_size=(3,3), strides=(2,2), padding='valid', name='pool1/3x3_s2')(pool1_helper)

    # 변수, layer_a_b_c 에서    a : conv number , b : block number , c : layer number 이다.
    # 변수, res_a_b 에서        a : conv number , b : block number ( = res number) 이다.
    # 변수, acti_a_b 에서       a : conv number , b : number between blocks 이다.
    # 이 사실을 기억하고, 아래의 변수들을 해석한다.

    # conv2_x
    layer_2_1_1 = resnet_layer(inputs=pool_1, num_filters=64, strides=(1,1), kernel_size=(3,3), activation='relu')
    layer_2_1_2 = resnet_layer(inputs=layer_2_1_1, num_filters=64, strides=(1,1), kernel_size=(3,3), activation=None)
    res_2_1 = add([pool_1, layer_2_1_2]) # block 1
    acti_2_1 = Activation("relu")(res_2_1)
    layer_2_2_1 = resnet_layer(inputs=acti_2_1, num_filters=64, strides=(1,1), kernel_size=(3,3), activation='relu')
    layer_2_2_2 = resnet_layer(inputs=layer_2_2_1, num_filters=64, strides=(1,1), kernel_size=(3,3), activation=None)
    res_2_2 = add([acti_2_1, layer_2_2_2]) # block 2 // 그리고 이런 규칙은 아래에서도 적용된다.
    acti_2_output = Activation('relu')(res_2_2)

    # 변수 identi_3_input 은 conv3_x 묶음에 처음으로 들어올 input이다. 이 값은 변환 없이 자기 자신을 유지한 채로 간다.

    # conv3_x
    identi_3_input = Conv2D(128, (1, 1), strides=(2, 2), padding='valid')(acti_2_output) # 차원을 맞춰주기 위해 convolution 연산 추가. (그대로 가는것)
    #identi_3_input = BatchNormalization()(identi_3_input) # 모든 conv.뒤에는 bn을 적용.
    layer_3_1_1 = resnet_layer(inputs=acti_2_output, num_filters=128, strides=(2,2), kernel_size=(3,3),activation='relu') #이 아래 두층은 rensnet층
    layer_3_1_2 = resnet_layer(inputs=layer_3_1_1, num_filters=128, strides=(1,1), kernel_size=(3,3), activation=None)
    res_3_1 = add([identi_3_input, layer_3_1_2])
    acti_3_1 = Activation("relu")(res_3_1)
    layer_3_2_1 = resnet_layer(inputs=acti_3_1,num_filters=128, strides=(1,1), kernel_size=(3,3), activation='relu') #이 아래 두층은 rensnet층
    layer_3_2_2 = resnet_layer(inputs=layer_3_2_1, num_filters=128, strides=(1,1), kernel_size=(3,3), activation=None)
    res_3_2 = add([acti_3_1, layer_3_2_2])
    acti_3_output = Activation('relu')(res_3_2)

    # conv4_x
    identi_4_input = Conv2D(256, (1, 1), strides=(2, 2), padding='valid')(acti_3_output) # 차원을 맞춰주기 위해 convolution 연산 추가. (그대로 가는것)
    #identi_4_input = BatchNormalization()(identi_4_input) # 모든 conv.뒤에는 bn을 적용.
    layer_4_1_1 = resnet_layer(inputs=acti_3_output, num_filters=256, strides=(2,2), kernel_size=(3,3),activation='relu') #이 아래 두층은 rensnet층
    layer_4_1_2 = resnet_layer(inputs=layer_4_1_1, num_filters=256, strides=(1,1), kernel_size=(3,3), activation=None)
    res_4_1 = add([identi_4_input, layer_4_1_2])
    acti_4_1 = Activation("relu")(res_4_1)
    layer_4_2_1 = resnet_layer(inputs=acti_4_1,num_filters=256, strides=(1,1), kernel_size=(3,3), activation='relu') #이 아래 두층은 rensnet층
    layer_4_2_2 = resnet_layer(inputs=layer_4_2_1, num_filters=256, strides=(1,1), kernel_size=(3,3), activation=None)
    res_4_2 = add([acti_4_1, layer_4_2_2])
    acti_4_output = Activation('relu')(res_4_2)

    # conv5_x
    identi_5_input = Conv2D(512, (1, 1), strides=(2, 2), padding='valid')(acti_4_output) # 차원을 맞춰주기 위해 convolution 연산 추가. (그대로 가는것)
    #identi_5_input = BatchNormalization()(identi_5_input) # 모든 conv.뒤에는 bn을 적용.
    layer_5_1_1 = resnet_layer(inputs=acti_4_output, num_filters=512, strides=(2,2), kernel_size=(3,3),activation='relu') #이 아래 두층은 rensnet층
    layer_5_1_2 = resnet_layer(inputs=layer_5_1_1, num_filters=512, strides=(1,1), kernel_size=(3,3), activation=None)
    res_5_1 = add([identi_5_input, layer_5_1_2])
    acti_5_1 = Activation("relu")(res_5_1)
    layer_5_2_1 = resnet_layer(inputs=acti_5_1,num_filters=512, strides=(1,1), kernel_size=(3,3), activation='relu') #이 아래 두층은 rensnet층
    layer_5_2_2 = resnet_layer(inputs=layer_5_2_1, num_filters=512, strides=(1,1), kernel_size=(3,3), activation=None)
    res_5_2 = add([acti_5_1, layer_5_2_2])
    acti_5_output = Activation('relu')(res_5_2)

    # 마지막 1층
    y=AveragePooling2D(pool_size=(7, 7), strides=(1, 1), name='loss1/ave_pool')(acti_5_output)
    y = Flatten()(y)
    outputs = Dense(classes, activation='softmax', kernel_initializer='he_normal')(y)

    model = Model(inputs=inputs, outputs=outputs)
    return model
Ejemplo n.º 8
0
# construct training image generator for data augmentation
dataAug = ImageDataGenerator(rotation_range=20,
                             zoom_range=0.15,
                             width_shift_range=0.2,
                             height_shift_range=0.2,
                             shear_range=0.15,
                             fill_mode="nearest")

# load the MobileNetV2 network leaving off head FC layer sets
modelBase = MobileNetV2(weights="imagenet",
                        include_top=False,
                        input_tensor=Input(shape=(224, 224, 3)))

# construct head of model
modelHead = modelBase.output
modelHead = AveragePooling2D(pool_size=(7, 7))(modelHead)
modelHead = Flatten(name="flatten")(modelHead)
modelHead = Dense(128, activation="relu")(modelHead)
modelHead = Dropout(0.5)(modelHead)
modelHead = Dense(16, activation="softmax")(modelHead)

# place head FC model on top of base model
model = Model(inputs=modelBase.input, outputs=modelHead)

# loop over all layers in the base model and freeze them
for layer in modelBase.layers:
    layer.trainable = False

# compile model
opt = Adam(lr=INIT_LR)
model.compile(loss="categorical_crossentropy",
Ejemplo n.º 9
0
def ResNet(input_shape, nb_classes=1000, block_func=_residual_block_basic, repetitions=(2, 2, 2, 2),
           use_bias=False, use_dropout=False, kernel_initialiser='he_normal', kernel_regulariser=regulisers.l2(1e-5)):
    """ This builds the ResNet Model for the Classification task.
    Parameters:
        - input_shape, is the input data shape such as (224, 224, 3).
        - nb_classes, is the number of classes to be predicted.
        - block_func, is the Block Layer method to be used.
        - repetitions, is the List of repetitions for each macro-blocks the network should build/contain.
        - use_bias, is a Flag to use or not the bias in the convolution layer.
        - use_dropout, is a Flag to use or not the dropout in the Fully-connected layer.
        - kernel_initialiser, is the kernel initialisation method name.
        - kernel_regulariser, is the kernel regulariser.
    Returns:
        - returns the ResNet model.
    """
    # ResNet's Input layer:
    inputs = Input(shape=input_shape)

    conv = _res_conv(filters=64,
                     kernel_size=7,
                     strides=2,
                     use_relu=True,
                     use_bias=use_bias,
                     kernel_initialiser=kernel_initialiser,
                     kernel_regulariser=kernel_regulariser
                     )(inputs)

    maxpool = MaxPooling2D(pool_size=3,
                           strides=2,
                           padding='same')(conv)

    # Resnet's chain of Residual Blocks (Repetition Layers):
    filters = 64
    strides = 2
    res_block = maxpool

    for i, repet in enumerate(repetitions):
        # NOTE: no further input size reduction for the 1st block, as max-pooling was applied prior.
        block_strides = strides if i != 0 else 1
        macroblock_name = "block_{}".format(i)

        res_block = _residual_macroblock(block_func=block_func,
                                         filters=filters,
                                         repetitions=repet,
                                         strides_1stBlock=block_strides,
                                         use_bias=use_bias,
                                         kernel_initialiser=kernel_initialiser,
                                         kernel_regulariser=kernel_regulariser,
                                         name=macroblock_name
                                         )(res_block)

        # Limit the number of filters to 1024 as the maximum:
        filters = min(filters * 2, 1024)

        # Resnet's Final/Output Layer:
    res_spatial_dimen = tf.keras.backend.int_shape(res_block)[1:3]

    avg_pool = AveragePooling2D(pool_size=res_spatial_dimen,
                                strides=1
                                )(res_block)

    flatten = Flatten()(avg_pool)

    # # Adding a fully connected layer having 1024 neurons
    # FC_layer = Dense(units=1024,
    #                  activation='relu',
    #                  use_bias=True,
    #                  kernel_initializer='glorot_uniform')(flatten)

    # Adding the 1st hidden fully connected layer having 256 neurons:
    FC_layer_1 = Dense(units=256,
                       activation='relu',
                       use_bias=True,
                       kernel_initializer='glorot_uniform')(flatten)

    # Adding dropout, if True:
    if use_dropout:
        FC_layer_1 = Dropout(rate=0.3)(FC_layer_1)

    # Adding the 2nd hidden fully connected layer having 256 neurons:
    FC_layer_2 = Dense(units=128,
                       activation='relu',
                       use_bias=True,
                       kernel_initializer='glorot_uniform')(FC_layer_1)

    # Adding dropout, if True:
    if use_dropout:
        FC_layer_2 = Dropout(rate=0.2)(FC_layer_2)



    predictions = Dense(units=nb_classes,
                        activation='softmax',
                        kernel_initializer=kernel_initialiser
                        )(FC_layer_2)

    # Model (Keras API):
    model = Model(inputs=inputs,
                  outputs=predictions)

    # Return the model configurations:
    return model
Ejemplo n.º 10
0
def main(args):
    config_path = os.path.expanduser(args.config_path)
    weights_path = os.path.expanduser(args.weights_path)
    assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format(
        config_path)
    assert weights_path.endswith(
        '.weights'), '{} is not a .weights file'.format(weights_path)

    output_path = os.path.expanduser(args.output_path)
    #assert output_path.endswith(
        #'.h5'), 'output path {} is not a .h5 file'.format(output_path)
    output_root = os.path.splitext(output_path)[0]

    # Load weights and config.
    print('Loading weights.')
    weights_file = open(weights_path, 'rb')
    major, minor, revision = np.ndarray(
        shape=(3, ), dtype='int32', buffer=weights_file.read(12))
    if (major*10+minor)>=2 and major<1000 and minor<1000:
        seen = np.ndarray(shape=(1,), dtype='int64', buffer=weights_file.read(8))
    else:
        seen = np.ndarray(shape=(1,), dtype='int32', buffer=weights_file.read(4))
    print('Weights Header: ', major, minor, revision, seen)

    print('Parsing Darknet config.')
    unique_config_file = unique_config_sections(config_path)
    cfg_parser = configparser.ConfigParser()
    cfg_parser.read_file(unique_config_file)

    weight_decay = float(cfg_parser['net_0']['decay']) if 'net_0' in cfg_parser.sections() else 5e-4

    # Parase model input width, height
    width = int(cfg_parser['net_0']['width']) if 'net_0' in cfg_parser.sections() else None
    height = int(cfg_parser['net_0']['height']) if 'net_0' in cfg_parser.sections() else None

    print('Creating Keras model.')
    if width and height and args.fixed_input_shape:
        input_layer = Input(shape=(height, width, 3), name='image_input')
    else:
        input_layer = Input(shape=(None, None, 3), name='image_input')
    prev_layer = input_layer
    all_layers = []

    count = 0
    out_index = []
    anchors = None
    for section in cfg_parser.sections():
        print('Parsing section {}'.format(section))
        if section.startswith('convolutional'):
            filters = int(cfg_parser[section]['filters'])
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            pad = int(cfg_parser[section]['pad'])
            activation = cfg_parser[section]['activation']
            batch_normalize = 'batch_normalize' in cfg_parser[section]

            padding = 'same' if pad == 1 and stride == 1 else 'valid'

            # support DepthwiseConv2D with "groups"
            # option in conv section
            if 'groups' in cfg_parser[section]:
                groups = int(cfg_parser[section]['groups'])
                # Now only support DepthwiseConv2D with "depth_multiplier=1",
                # which means conv groups should be same as filters
                assert groups == filters, 'Only support groups is same as filters.'
                depthwise = True
                depth_multiplier = 1
            else:
                depthwise = False

            # Setting weights.
            # Darknet serializes convolutional weights as:
            # [bias/beta, [gamma, mean, variance], conv_weights]
            prev_layer_shape = K.int_shape(prev_layer)

            if depthwise:
                # DepthwiseConv2D weights shape in TF:
                # (kernel_size, kernel_size, in_channels, depth_multiplier).
                weights_shape = (size, size, prev_layer_shape[-1], depth_multiplier)
                darknet_w_shape = (depth_multiplier, weights_shape[2], size, size)
                weights_size = np.product(weights_shape)
                print('depthwiseconv2d', 'bn'
                      if batch_normalize else '  ', activation, weights_shape)
            else:
                weights_shape = (size, size, prev_layer_shape[-1], filters)
                darknet_w_shape = (filters, weights_shape[2], size, size)
                weights_size = np.product(weights_shape)
                print('conv2d', 'bn'
                      if batch_normalize else '  ', activation, weights_shape)

            conv_bias = np.ndarray(
                shape=(filters, ),
                dtype='float32',
                buffer=weights_file.read(filters * 4))
            count += filters

            if batch_normalize:
                bn_weights = np.ndarray(
                    shape=(3, filters),
                    dtype='float32',
                    buffer=weights_file.read(filters * 12))
                count += 3 * filters

                bn_weight_list = [
                    bn_weights[0],  # scale gamma
                    conv_bias,  # shift beta
                    bn_weights[1],  # running mean
                    bn_weights[2]  # running var
                ]

            conv_weights = np.ndarray(
                shape=darknet_w_shape,
                dtype='float32',
                buffer=weights_file.read(weights_size * 4))
            count += weights_size

            # DarkNet conv_weights are serialized Caffe-style:
            # (out_dim, in_dim, height, width)
            # We would like to set these to Tensorflow order:
            # (height, width, in_dim, out_dim)
            conv_weights = np.transpose(conv_weights, [2, 3, 1, 0])
            conv_weights = [conv_weights] if batch_normalize else [
                conv_weights, conv_bias
            ]

            # Handle activation.
            act_fn = None
            if activation == 'leaky':
                pass  # Add advanced activation later.
            elif activation == 'relu':
                pass  # Add advanced activation later.
            elif activation == 'mish':
                pass  # Add advanced activation later.
            elif activation == 'logistic':
                pass  # Add advanced activation later.
            elif activation != 'linear':
                raise ValueError(
                    'Unknown activation function `{}` in section {}'.format(
                        activation, section))

            # Create Conv2D layer
            if stride>1:
                # Darknet uses left and top padding instead of 'same' mode
                prev_layer = ZeroPadding2D(((1,0),(1,0)))(prev_layer)

            if depthwise:
                conv_layer = (DepthwiseConv2D(
                    (size, size),
                    strides=(stride, stride),
                    depth_multiplier=depth_multiplier,
                    kernel_regularizer=l2(weight_decay),
                    use_bias=not batch_normalize,
                    weights=conv_weights,
                    activation=act_fn,
                    padding=padding))(prev_layer)
            else:
                conv_layer = (Conv2D(
                    filters, (size, size),
                    strides=(stride, stride),
                    kernel_regularizer=l2(weight_decay),
                    use_bias=not batch_normalize,
                    weights=conv_weights,
                    activation=act_fn,
                    padding=padding))(prev_layer)

            if batch_normalize:
                conv_layer = (BatchNormalization(
                    weights=bn_weight_list))(conv_layer)
            prev_layer = conv_layer

            if activation == 'linear':
                all_layers.append(prev_layer)
            elif activation == 'mish':
                act_layer = Activation(mish)(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)
            elif activation == 'leaky':
                act_layer = LeakyReLU(alpha=0.1)(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)
            elif activation == 'relu':
                act_layer = ReLU()(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)
            elif activation == 'logistic':
                act_layer = Activation('sigmoid')(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)

        elif section.startswith('route'):
            ids = [int(i) for i in cfg_parser[section]['layers'].split(',')]
            layers = [all_layers[i] for i in ids]

            if ('groups' in cfg_parser[section]):
                # support route with groups, which is for splitting input tensor into group
                # Reference comment (from AlexeyAB):
                #
                # https://github.com/lutzroeder/netron/issues/531
                #
                assert 'group_id' in cfg_parser[section], 'route with groups should have group_id.'
                assert len(layers) == 1, 'route with groups should have 1 input layer.'

                groups = int(cfg_parser[section]['groups'])
                group_id = int(cfg_parser[section]['group_id'])
                route_layer = layers[0]  # group route only have 1 input layer
                print('Split {} to {} groups and pick id {}'.format(route_layer, groups, group_id))

                all_layers.append(
                    Lambda(
                        # tf.split implementation for groups route
                        lambda x: tf.split(x, num_or_size_splits=groups, axis=-1)[group_id],
                        name='group_route_'+str(len(all_layers)))(route_layer))
                prev_layer = all_layers[-1]
            else:
                if len(layers) > 1:
                    print('Concatenating route layers:', layers)
                    concatenate_layer = Concatenate()(layers)
                    all_layers.append(concatenate_layer)
                    prev_layer = concatenate_layer
                else:
                    skip_layer = layers[0]  # only one layer to route
                    all_layers.append(skip_layer)
                    prev_layer = skip_layer

        elif section.startswith('maxpool'):
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            all_layers.append(
                MaxPooling2D(
                    pool_size=(size, size),
                    strides=(stride, stride),
                    padding='same')(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('avgpool'):
            all_layers.append(
                AveragePooling2D()(prev_layer))
            prev_layer = all_layers[-1]

        # support GAP and Reshape layer for se block
        # Reference:
        #
        # https://github.com/gitE0Z9/keras-YOLOv3-model-set/commit/67ed826f2ce00f28296fcf88c03c86f1d86b8b9e
        #
        elif section.startswith('gap'):
            all_layers.append(
                GlobalAveragePooling2D()(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('reshape'):
            all_layers.append(
                Reshape((1, 1, K.int_shape(prev_layer)[-1]))(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('shortcut'):
            index = int(cfg_parser[section]['from'])
            activation = cfg_parser[section]['activation']
            assert activation == 'linear', 'Only linear activation supported.'
            all_layers.append(Add()([all_layers[index], prev_layer]))
            prev_layer = all_layers[-1]

        elif section.startswith('sam'):
            # support SAM (Modified Spatial Attention Module in YOLOv4) layer
            # Reference comment:
            #
            # https://github.com/AlexeyAB/darknet/issues/3708
            #
            index = int(cfg_parser[section]['from'])
            all_layers.append(Multiply()([all_layers[index], prev_layer]))
            prev_layer = all_layers[-1]

        elif section.startswith('dropout'):
            rate = float(cfg_parser[section]['probability'])
            assert rate >= 0 and rate <= 1, 'Dropout rate should be between 0 and 1, got {}.'.format(rate)
            all_layers.append(Dropout(rate=rate)(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('upsample'):
            stride = int(cfg_parser[section]['stride'])
            assert stride%2 == 0, 'upsample stride should be multiples of 2'
            all_layers.append(UpSampling2D(stride)(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('reorg'):
            block_size = int(cfg_parser[section]['stride'])
            assert block_size == 2, 'Only reorg with stride 2 supported.'
            all_layers.append(
                Lambda(
                    #space_to_depth_x2,
                    #output_shape=space_to_depth_x2_output_shape,
                    lambda x: tf.nn.space_to_depth(x, block_size=2),
                    name='space_to_depth_x2')(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('region'):
            # YOLOv2 anchors parse, here we convert origin
            # grid-reference value to pixel size value
            anchors_line = cfg_parser[section]['anchors']
            anchors_list = list(map(float, anchors_line.split(',')))
            anchors_line = [str(anchor * 32) for anchor in anchors_list]
            anchors = ', '.join(anchors_line)

        elif section.startswith('yolo'):
            out_index.append(len(all_layers)-1)
            all_layers.append(None)
            prev_layer = all_layers[-1]
            # YOLOv3/v4 anchors parse
            anchors = cfg_parser[section]['anchors']

        elif (section.startswith('net') or section.startswith('cost') or
              section.startswith('softmax')):
            pass

        else:
            raise ValueError(
                'Unsupported section header type: {}'.format(section))

    # Create and save model.
    if len(out_index)==0: out_index.append(len(all_layers)-1)

    if args.yolo4_reorder:
        # reverse the output tensor index for YOLOv4 cfg & weights,
        # since it use a different yolo outout order
        out_index.reverse()

    model = Model(inputs=input_layer, outputs=[all_layers[i] for i in out_index])
    print(model.summary())
    if args.weights_only:
        model.save_weights('{}'.format(output_path))
        print('Saved Keras weights to {}'.format(output_path))
    else:
        model.save('{}'.format(output_path))
        print('Saved Keras model to {}'.format(output_path))

    # Check to see if all weights have been read.
    remaining_weights = len(weights_file.read()) / 4
    weights_file.close()
    print('Read {} of {} from Darknet weights.'.format(count, count +
                                                       remaining_weights))
    if remaining_weights > 0:
        print('Warning: {} unused weights'.format(remaining_weights))

    if anchors:
        with open('{}_anchors.txt'.format(output_root), 'w') as f:
            print(anchors, file=f)
        print('Saved anchors to {}_anchors.txt'.format(output_root))

    if args.plot_model:
        plot(model, to_file='{}.png'.format(output_root), show_shapes=True)
        print('Saved model plot to {}.png'.format(output_root))
def build_fn(model_struct):
    if model_struct == 'LeNet':
        model = Sequential([
            Conv2D(filters=6,
                   kernel_size=(3, 3),
                   activation='relu',
                   input_shape=(224, 224, 1)),  # input shape 1 or 3?
            AveragePooling2D(),
            Conv2D(filters=16, kernel_size=(3, 3), activation='relu'),
            AveragePooling2D(),
            Flatten(),
            Dense(
                units=60, activation='relu'
            ),  # TODO change Units because we only have 2 classes to predict
            Dense(
                units=42, activation='relu'
            ),  # TODO change Units because we only have 2 classes to predict
            Dense(
                units=1, activation='sigmoid'
            )  # TODO change Units because we only have 2 classes to predict
        ])
        return model.get_config()
    elif model_struct == 'AlexNet':
        model = Sequential([
            Conv2D(filters=96,
                   input_shape=(224, 224, 1),
                   kernel_size=(11, 11),
                   activation='relu',
                   strides=(4, 4),
                   padding='valid'),  # Should input shape be 1 or 3?
            MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'),
            Conv2D(filters=256,
                   kernel_size=(11, 11),
                   activation='relu',
                   strides=(1, 1),
                   padding='valid'),
            MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'),
            Conv2D(filters=384,
                   kernel_size=(3, 3),
                   activation='relu',
                   strides=(1, 1),
                   padding='valid'),
            Conv2D(filters=384,
                   kernel_size=(3, 3),
                   activation='relu',
                   strides=(1, 1),
                   padding='valid'),
            Conv2D(filters=256,
                   kernel_size=(3, 3),
                   activation='relu',
                   strides=(1, 1),
                   padding='valid'),
            MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'),
            Flatten(),
            Dense(4096, input_shape=(224 * 224, ), activation='relu'),
            Dropout(0.4),
            Dense(
                4096, activation='relu'
            ),  # TODO change Units because we only have 2 classes to predict
            Dropout(0.4),
            Dense(
                1000, activation='relu'
            ),  # TODO change Units because we only have 2 classes to predict
            Dropout(0.4),
            Dense(
                1, activation='sigmoid'
            )  # TODO change Units because we only have 2 classes to predict
        ])
        return model.get_config()
    elif model_struct == 'Random':
        model = Sequential()
        model.add(random_conv(224, True))

        while True:
            rand_val = random.random()

            try:
                if not any(isinstance(x, Flatten) for x in
                           model.layers):  # if there's not a Flatten layer yet
                    input_size = min(model.layers[-1].output_shape[1],
                                     model.layers[-1].output_shape[2])

                    if input_size < 2 or len(model.layers) > 6:
                        model.add(Flatten())
                        continue

                    if rand_val < 0.3:
                        model.add(random_conv(input_size, False))
                    elif rand_val < 0.6:
                        model.add(random_pool(input_size, 'max'))
                    elif rand_val < 0.9:
                        model.add(random_pool(input_size, 'average'))
                    else:
                        model.add(Flatten())
                else:  # if there is a Flatten layer already
                    if rand_val < 0.35:
                        model.add(random_dropout())
                    elif rand_val < 0.7:
                        model.add(random_dense(False))
                    else:
                        break
            except tensorflow.errors.ResourceExhaustedError:
                print("Initial Architecture went OOM, retrying...")

        model.add(random_dense(True))
        return model.get_config()

    print("model was not LeNet, AlexNet, or Random")
            'weights_1000/siamese_neural_congas_1_Mixed7a_Branch_1_Conv2d_1d_3x3_Conv2D_weights'
        ).transpose(1, 2, 3, 0)),
    bias_initializer=Constant(
        np.load(
            'weights_1000/siamese_neural_congas_1_Mixed7a_Branch_1_Conv2d_1d_3x3_Conv2D_bias'
        )))(relu3_5)
relu3_6 = ReLU(max_value=6.)(conv3_6)

maxpool3_1 = MaxPool2D(pool_size=[3, 3], strides=[2, 2],
                       padding='same')(concat2_1)

concat3_1 = Concatenate(axis=3)([relu3_2, relu3_6, maxpool3_1])

# Block_04
avgpool4_1 = AveragePooling2D(pool_size=[4, 4],
                              strides=[1, 1],
                              padding='valid')(concat3_1)
conv4_1 = Conv2D(
    filters=40,
    kernel_size=[1, 1],
    strides=[1, 1],
    padding="same",
    dilation_rate=[1, 1],
    kernel_initializer=Constant(
        np.load(
            'weights_1000/siamese_neural_congas_1_feature_compression_Conv2d_0a_Conv2D_weights'
        ).transpose(1, 2, 3, 0)),
    bias_initializer=Constant(
        np.load(
            'weights_1000/siamese_neural_congas_1_feature_compression_Conv2d_0a_Conv2D_bias'
        )))(avgpool4_1)
    def call(self, inputs, training=None, mask=None, end_at="logits", add_inbetween_endpoints=False):

        end_points = {"4a" : None, "5a" : None, "6e" : None, "7c" : None, "fc" : None}

        # 299 x 299 x 3
        x = self.Conv2d_1a_3x3(inputs)
        # 149 x 149 x 32
        x = self.Conv2d_2a_3x3(x)
        # 147 x 147 x 32
        x = self.Conv2d_2b_3x3(x)
        # 147 x 147 x 64
        x = MaxPooling2D((3, 3), strides=(2, 2))(x)
        # 73 x 73 x 64
        x = self.Conv2d_3b_1x1(x)
        # 73 x 73 x 80
        x = self.Conv2d_4a_3x3(x)

        if add_inbetween_endpoints or end_at=="4a":
            end_points["4a"] = x
            if end_at == "4a":
                return end_points

        # 71 x 71 x 192
        x = MaxPooling2D((3, 3), strides=(2, 2))(x)
        # 35 x 35 x 192
        x = self.Mixed_5b(x)
        # 35 x 35 x 256
        x = self.Mixed_5c(x)
        # 35 x 35 x 288
        x = self.Mixed_5d(x)

        if add_inbetween_endpoints or end_at=="5d":
            end_points["5d"] = x
            if end_at == "5d":
                return end_points

        # 35 x 35 x 288
        x = self.Mixed_6a(x)
        # 17 x 17 x 768
        x = self.Mixed_6b(x)
        # 17 x 17 x 768
        x = self.Mixed_6c(x)
        # 17 x 17 x 768
        x = self.Mixed_6d(x)
        # 17 x 17 x 768
        x = self.Mixed_6e(x)

        if add_inbetween_endpoints or end_at=="6e":
            end_points["6e"] = x
            if end_at == "6e":
                return end_points

        # 17 x 17 x 768
        x = self.Mixed_7a(x)
        # 8 x 8 x 1280
        x = self.Mixed_7b(x)
        # 8 x 8 x 2048
        x = self.Mixed_7c(x)

        if add_inbetween_endpoints or end_at=="7c":
            end_points["7c"] = x
            if end_at == "7c":
                return end_points

        # 8 x 8 x 2048
        kernel_size = x.shape[1]
        x = AveragePooling2D(kernel_size, padding='same')(x)
        # 1 x 1 x 2048
        x = layers.Dropout(0.2)(x)
        # 1 x 1 x 2048
        x = tf.keras.layers.Flatten()(x)

        if add_inbetween_endpoints or end_at=="features":
            end_points["features"] = x
            if end_at == "features":
                return end_points

        # 2048
        x = self.fc(x)

        end_points["logits"] = x

        return end_points
Ejemplo n.º 14
0
    def get_xception_abbreviated_clsresidual(self,
                                             input_shape=(1, 1024, 26),
                                             gpu_device="/gpu:1"):
        r"""Returns the TensorFlow 2.2 implementation of Xception (Abbreviated w/ CLS Residual).
            Inspired by Chollet 2017 : http://arxiv.org/abs/1610.02357

        Args:
            input_shape (tuple, optional): Shape of the input tensor. Defaults to (1, 1024, 26).
            gpu_device (str, optional): If GPU devices are available, defines which one to utilize. Defaults to "/gpu:0".
            verbose (bool, optional): Log details to console. Defaults to True.

        Returns:
           tf.keras.Model: returns a TensorFlow 2.20 model (compiled, untrained)
        """

        # Input validation
        self.__require_params(input_shape=input_shape)

        if (not gpu_device) or self.__GPU_count == 0:
            gpu_device = "/cpu:0"

        # Model hyperparameters and metadata
        model_name = 'Binary Classification Xception (Abbreviated w/ CLS Residual)'
        opt = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
        loss = CategoricalCrossentropy(from_logits=True)
        metrics = ['accuracy']

        # Construct model & compile
        with tf.device(gpu_device):

            # input image size
            input_img = layers.Input(shape=input_shape, dtype=tf.float32)

            # pull the last channel layer for residual connection layer
            inp_seq = input_img[:, :, :, -1]
            inp_seq = tf.squeeze(inp_seq, axis=1)

            # Block 1
            x = Conv2D(64, (1, 3), strides=(1, 3), use_bias=False)(input_img)
            x = BatchNormalization()(x)
            x = Activation('relu')(x)
            x = Conv2D(128, (1, 3), use_bias=False)(x)
            x = BatchNormalization()(x)
            x = Activation('relu')(x)

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

            # Block 2
            x = SeparableConv2D(256, (1, 3), padding='same', use_bias=False)(x)
            x = BatchNormalization()(x)
            x = Activation('relu')(x)
            x = SeparableConv2D(512, (1, 3), padding='same', use_bias=False)(x)
            x = BatchNormalization()(x)

            # Block 2 Pool
            x = AveragePooling2D((1, 3), strides=(1, 2), padding='same')(x)
            x = layers.add([x, residual])

            # Fully Connected Layer
            x = GlobalAveragePooling2D()(x)

            # add the skip level residual back to the last CLS token
            x = layers.concatenate([x, inp_seq])

            x = layers.Dense(2, dtype=tf.float32, name='dense_2_final')(x)

            model = models.Model(input_img, x, name=model_name)

        model.compile(loss=loss, optimizer=opt, metrics=metrics)

        # Print verbose output to console
        if verbose:
            self.__verbose_print(model, model_name, input_shape, opt, loss,
                                 metrics)

        return model
Ejemplo n.º 15
0
def ResNet(input_shape, classes):
    _input = Input(shape=input_shape)
    res = ZeroPadding2D([3, 3])(_input)

    # stage 1
    res = Conv2D(filters=64,
                 kernel_size=(7, 7),
                 strides=(2, 2),
                 name="res_stage_1_conv",
                 kernel_initializer=glorot_uniform(seed=0))(res)
    res = BatchNormalization(axis=3, name="res_stage_1_BN")(res)
    res = Activation('relu')(res)
    res = MaxPooling2D((3, 3), strides=(2, 2))(res)

    # stage 2
    res = conv_block(input_tensor=res,
                     kernel_size=3,
                     filters=[64, 64, 256],
                     stride=1,
                     stage="2a")

    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[64, 64, 256],
                   stage="2b")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[64, 64, 256],
                   stage="2c")
    # stage 3
    res = conv_block(input_tensor=res,
                     kernel_size=3,
                     filters=[128, 128, 512],
                     stride=2,
                     stage="3a")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[128, 128, 512],
                   stage="3b")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[128, 128, 512],
                   stage="3c")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[128, 128, 512],
                   stage="3d")

    # stage 4
    res = conv_block(input_tensor=res,
                     kernel_size=3,
                     filters=[256, 256, 1024],
                     stride=2,
                     stage="4a")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[256, 256, 1024],
                   stage="4b")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[256, 256, 1024],
                   stage="4c")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[256, 256, 1024],
                   stage="4d")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[256, 256, 1024],
                   stage="4e")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[256, 256, 1024],
                   stage="4f")

    # stage 5
    res = conv_block(input_tensor=res,
                     kernel_size=3,
                     filters=[512, 512, 2048],
                     stride=2,
                     stage="5a")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[512, 512, 2048],
                   stage="5b")
    res = id_block(input_tensor=res,
                   kernel_size=3,
                   filters=[512, 512, 2048],
                   stage="5c")
    res = AveragePooling2D(pool_size=(2, 2), padding='same')(res)

    res = Flatten()(res)
    res = Dense(classes,
                activation='softmax',
                name='output_fc_layer',
                kernel_initializer=glorot_uniform(seed=0))(res)

    model = Model(inputs=_input, outputs=res, name='ResNet')

    return model
Ejemplo n.º 16
0
# This section creates a simple CNN using Keras, and trains it
# with backpropagation. There are no spikes involved at this point.

input_shape = x_train.shape[1:]
input_layer = Input(input_shape)

layer = Conv2D(filters=16,
               kernel_size=(5, 5),
               strides=(2, 2),
               activation='relu',
               use_bias=False)(input_layer)
layer = Conv2D(filters=32,
               kernel_size=(3, 3),
               activation='relu',
               use_bias=False)(layer)
layer = AveragePooling2D()(layer)
layer = Conv2D(filters=8,
               kernel_size=(3, 3),
               padding='same',
               activation='relu',
               use_bias=False)(layer)
layer = Flatten()(layer)
layer = Dropout(0.01)(layer)
layer = Dense(units=10,
              activation='softmax',
              use_bias=False)(layer)

model = Model(input_layer, layer)

model.summary()
Ejemplo n.º 17
0
def InceptionV3(include_top=True,
                weights='imagenet',
                input_tensor=None,
                input_shape=None,
                pooling=None,
                classes=1000):
    """Instantiates the Inception v3 architecture.
    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format='channels_last'` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.
    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)` (with `channels_last` data format)
            or `(3, 299, 299)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 139.
            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.
    """
    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')

    # Determine proper input shape
    input_shape = _obtain_input_shape(
        input_shape,
        default_size=299,
        min_size=139,
        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

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = 3

    x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='same')
    x = conv2d_bn(x, 32, 3, 3, padding='same')
    x = conv2d_bn(x, 64, 3, 3)
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = conv2d_bn(x, 80, 1, 1, padding='same')
    x = conv2d_bn(x, 192, 3, 3, padding='same')
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    # mixed 0, 1, 2: 35 x 35 x 256
    branch1x1 = conv2d_bn(x, 64, 1, 1)

    branch5x5 = conv2d_bn(x, 48, 1, 1)
    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

    branch3x3dbl = conv2d_bn(x, 64, 1, 1)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 32, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch5x5, branch3x3dbl, branch_pool],
        axis=channel_axis,
        name='mixed0')

    # mixed 1: 35 x 35 x 256
    branch1x1 = conv2d_bn(x, 64, 1, 1)

    branch5x5 = conv2d_bn(x, 48, 1, 1)
    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

    branch3x3dbl = conv2d_bn(x, 64, 1, 1)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch5x5, branch3x3dbl, branch_pool],
        axis=channel_axis,
        name='mixed1')

    # mixed 2: 35 x 35 x 256
    branch1x1 = conv2d_bn(x, 64, 1, 1)

    branch5x5 = conv2d_bn(x, 48, 1, 1)
    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

    branch3x3dbl = conv2d_bn(x, 64, 1, 1)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch5x5, branch3x3dbl, branch_pool],
        axis=channel_axis,
        name='mixed2')

    # mixed 3: 17 x 17 x 768
    branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='same')

    branch3x3dbl = conv2d_bn(x, 64, 1, 1)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
    branch3x3dbl = conv2d_bn(
        branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='same')

    branch_pool = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
    x = layers.concatenate(
        [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3')

    # mixed 4: 17 x 17 x 768
    branch1x1 = conv2d_bn(x, 192, 1, 1)

    branch7x7 = conv2d_bn(x, 128, 1, 1)
    branch7x7 = conv2d_bn(branch7x7, 128, 1, 7)
    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

    branch7x7dbl = conv2d_bn(x, 128, 1, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch7x7, branch7x7dbl, branch_pool],
        axis=channel_axis,
        name='mixed4')

    # mixed 5, 6: 17 x 17 x 768
    for i in range(2):
        branch1x1 = conv2d_bn(x, 192, 1, 1)

        branch7x7 = conv2d_bn(x, 160, 1, 1)
        branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)
        branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

        branch7x7dbl = conv2d_bn(x, 160, 1, 1)
        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)
        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
        branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

        branch_pool = AveragePooling2D(
            (3, 3), strides=(1, 1), padding='same')(x)
        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
        x = layers.concatenate(
            [branch1x1, branch7x7, branch7x7dbl, branch_pool],
            axis=channel_axis,
            name='mixed' + str(5 + i))

    # mixed 7: 17 x 17 x 768
    branch1x1 = conv2d_bn(x, 192, 1, 1)

    branch7x7 = conv2d_bn(x, 192, 1, 1)
    branch7x7 = conv2d_bn(branch7x7, 192, 1, 7)
    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

    branch7x7dbl = conv2d_bn(x, 192, 1, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch7x7, branch7x7dbl, branch_pool],
        axis=channel_axis,
        name='mixed7')

    # mixed 8: 8 x 8 x 1280
    branch3x3 = conv2d_bn(x, 192, 1, 1)
    branch3x3 = conv2d_bn(branch3x3, 320, 3, 3,
                          strides=(2, 2), padding='same')

    branch7x7x3 = conv2d_bn(x, 192, 1, 1)
    branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7)
    branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1)
    branch7x7x3 = conv2d_bn(
        branch7x7x3, 192, 3, 3, strides=(2, 2), padding='same')

    branch_pool = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
    x = layers.concatenate(
        [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8')

    # mixed 9: 8 x 8 x 2048
    for i in range(2):
        branch1x1 = conv2d_bn(x, 320, 1, 1)

        branch3x3 = conv2d_bn(x, 384, 1, 1)
        branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)
        branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)
        branch3x3 = layers.concatenate(
            [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i))

        branch3x3dbl = conv2d_bn(x, 448, 1, 1)
        branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)
        branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)
        branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)
        branch3x3dbl = layers.concatenate(
            [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis)

        branch_pool = AveragePooling2D(
            (3, 3), strides=(1, 1), padding='same')(x)
        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
        x = layers.concatenate(
            [branch1x1, branch3x3, branch3x3dbl, branch_pool],
            axis=channel_axis,
            name='mixed' + str(9 + i))

    if include_top:
        # Classification block
        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='inception_v3')

    # load weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
        if include_top:
            weights_path = get_file(
                'inception_v3_weights_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH,
                cache_subdir='models',
                file_hash='9a0d58056eeedaa3f26cb7ebd46da564')
        else:
            weights_path = get_file(
                'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                file_hash='bcbd6486424b2319ff4ef7d526e38f63')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model
Ejemplo n.º 18
0
# perform one-hot encoding on the labels
lb = LabelBinarizer()
labels = lb.fit_transform(labels)
labels = to_categorical(labels)

(trainX, testX, trainY, testY) = train_test_split(data, labels,
	test_size=0.20, stratify=labels, random_state=42)

# initialize the training data augmentation object
trainAug = ImageDataGenerator(rotation_range=15,fill_mode="nearest")

baseModel = DenseNet121(weights="imagenet", include_top=False,input_tensor=Input(shape=(224, 224, 3)))

headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(3, 3))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(64, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation="softmax")(headModel)


model = Model(inputs=baseModel.input, outputs=headModel)

for layer in baseModel.layers:
	layer.trainable = False

opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="binary_crossentropy", optimizer=opt,
	metrics=["accuracy"])
Ejemplo n.º 19
0
    def __init__(self):
        super(PhraseDiscriminatorModel,
              self).__init__(name='phrase_discriminator')
        self.x1_1_1 = Conv2D(filters=16,
                             kernel_size=[1, 4],
                             strides=[1, 2],
                             activation='relu',
                             padding='same')
        self.x1_1_2 = Conv2D(filters=16,
                             kernel_size=[4, 1],
                             strides=[2, 1],
                             activation='relu',
                             padding='same')

        self.x1_2_1 = Conv2D(filters=16,
                             kernel_size=[4, 1],
                             strides=[2, 1],
                             activation='relu',
                             padding='same')
        self.x1_2_2 = Conv2D(filters=16,
                             kernel_size=[1, 4],
                             strides=[1, 2],
                             activation='relu',
                             padding='same')

        self.x2 = Conv2D(filters=32,
                         kernel_size=[3, 3],
                         strides=[2, 2],
                         activation='relu',
                         padding='same')

        self.x3 = Conv2D(filters=64,
                         kernel_size=[3, 3],
                         strides=[2, 2],
                         activation='relu',
                         padding='same')
        self.x4 = Conv2D(filters=128,
                         kernel_size=[3, 3],
                         strides=[2, 2],
                         activation='relu',
                         padding='same')

        self.chord_x1 = Conv2D(filters=16,
                               kernel_size=[3, 3],
                               strides=[2, 1],
                               activation='relu',
                               padding='same')
        self.chord_x2 = Conv2D(filters=32,
                               kernel_size=[3, 3],
                               strides=[2, 1],
                               activation='relu',
                               padding='same')
        self.chord_x3 = Conv2D(filters=64,
                               kernel_size=[3, 3],
                               strides=[2, 1],
                               activation='relu',
                               padding='same')
        self.chord_x4 = Conv2D(filters=128,
                               kernel_size=[3, 3],
                               strides=[2, 2],
                               activation='relu',
                               padding='same')

        self.on_off_x1 = Conv2D(filters=16,
                                kernel_size=[3, 1],
                                strides=[2, 1],
                                activation='relu',
                                padding='same')
        self.on_off_x2 = Conv2D(filters=32,
                                kernel_size=[3, 1],
                                strides=[2, 1],
                                activation='relu',
                                padding='same')
        self.on_off_x3 = Conv2D(filters=64,
                                kernel_size=[3, 1],
                                strides=[2, 1],
                                activation='relu',
                                padding='same')
        self.on_off_x4 = Conv2D(filters=128,
                                kernel_size=[3, 1],
                                strides=[2, 1],
                                activation='relu',
                                padding='same')

        self.x_avg = AveragePooling2D([24, 6])
        self.chord_avg = AveragePooling2D([24, 6])
        self.on_off_avg = AveragePooling2D([24, 1])

        self.feature1 = Conv2D(filters=128,
                               kernel_size=[1, 1],
                               strides=[1, 1],
                               activation='relu',
                               padding='same')
        self.feature2 = Conv2D(filters=1,
                               kernel_size=[1, 1],
                               strides=[1, 1],
                               activation='sigmoid',
                               padding='same')

        self.flatten = tf.keras.layers.Flatten()
Ejemplo n.º 20
0
def get_cifar_backbone(embeded_dim, model_type="mlp", norm_type="bn", acti_type="relu"):
    """ For `cifar10` and `cifar100` dataset
    Args:
        embeded_dim (int): 10 or 100
    Returns:
        encoder: (32, 32, 3) -> (1, 1, 10) or (1, 1, 100)
        decoder: (1, 1, 10) or (1, 1, 100) -> (32, 32, 3)
    """
    input_shape = (32, 32, 3)
    init = "he_normal"
    if model_type == "mlp":
        return get_mlp_backbones(input_shape, embeded_dim, norm_type, acti_type, kernel_initializer=init)
    elif model_type == "allconv":
        acti = acti_type
        encoder_input = Input(shape=input_shape)
        encoder_layers = []
        encoder_layers.extend(n_conv_norm(1, norm_type, acti, filters=32, kernel_size=5, strides=2,
                                          padding="same", kernel_initializer=init))  # 16
        encoder_layers.extend(n_conv_norm(1, norm_type, acti, filters=64, kernel_size=5, strides=2,
                                          padding="same", kernel_initializer=init))  # 8
        encoder_layers.extend(n_conv_norm(1, norm_type, acti, filters=128, kernel_size=3, strides=2,
                                          padding="valid", kernel_initializer=init))  # 3
        encoder_layers.append(Conv2D(embeded_dim, 3, activation=None, kernel_initializer=init))  # 1
        encoder_output = encoder_input
        for layer in encoder_layers:
            encoder_output = layer(encoder_output)

        decoder_input = Input(shape=(1, 1, embeded_dim))
        decoder_layers = []
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti, filters=128, kernel_size=3,
                                            kernel_initializer=init))  # 3
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti, filters=64, kernel_size=3, output_padding=1,
                                            strides=2, padding="valid", kernel_initializer=init))  # 8
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti, filters=32, kernel_size=5,
                                            strides=2, padding="same", kernel_initializer=init))  # 16
        decoder_layers.append(Conv2DTranspose(input_shape[-1], 5, strides=2, padding="same",
                                              activation="sigmoid", kernel_initializer=init))  # 32
        decoder_output = decoder_input
        for layer in decoder_layers:
            decoder_output = layer(decoder_output)
        encoder = Model(inputs=encoder_input, outputs=encoder_output, name="allconvbn_encoder")
        decoder = Model(inputs=decoder_input, outputs=decoder_output, name="allconvbn_decoder")
        return encoder, decoder
    elif model_type == "conv":
        # 与原文cifar10_2保持一致的encoder结构
        acti = acti_type

        encoder_input = Input(shape=input_shape)
        encoder_layers = []
        encoder_layers.extend(n_conv_norm(3, norm_type, acti,
                                          filters=64, kernel_size=3,
                                          padding="valid", kernel_initializer=init))  # 26,26,64
        encoder_layers.append(MaxPool2D(2, 2, padding="valid"))  # 13
        encoder_layers.extend(n_conv_norm(3, norm_type, acti,
                                          filters=128, kernel_size=3,
                                          padding="valid", kernel_initializer=init))  # 7,7,128
        encoder_layers.append(MaxPool2D(2, 2, padding="valid"))  # 3,3,128
        encoder_layers.extend(n_conv_norm(1, norm_type, acti,
                                          filters=embeded_dim, kernel_size=1,
                                          padding="valid", kernel_initializer=init))  # 3,3,10
        encoder_layers.append(AveragePooling2D(3))
        encoder_output = encoder_input
        for layer in encoder_layers:
            encoder_output = layer(encoder_output)

        decoder_input = Input(shape=(1, 1, embeded_dim))
        decoder_layers = []
        decoder_layers.append(UpSampling2D(3))  # 3
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti,
                                            filters=128, kernel_size=1,
                                            padding="valid", kernel_initializer=init))  # 3
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti,
                                            filters=128, kernel_size=3, strides=2,
                                            padding="valid", kernel_initializer=init))  # 7
        decoder_layers.extend(n_deconv_norm(2, norm_type, acti,
                                            filters=128, kernel_size=3,
                                            padding="valid", kernel_initializer=init))  # 11
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti,
                                            filters=64, kernel_size=3,
                                            padding="valid", kernel_initializer=init))  # 13
        decoder_layers.append(UpSampling2D(2))
        decoder_layers.extend(n_deconv_norm(2, norm_type, acti,
                                            filters=64, kernel_size=3,
                                            padding="valid", kernel_initializer=init))  # 30
        decoder_layers.append(Conv2DTranspose(3, kernel_size=3, kernel_initializer=init))  # 32, (no bn)
        decoder_output = decoder_input
        for layer in decoder_layers:
            decoder_output = layer(decoder_output)

        encoder = Model(inputs=encoder_input, outputs=encoder_output, name="conv_encoder")
        decoder = Model(inputs=decoder_input, outputs=decoder_output, name="conv_encoder")
        return encoder, decoder
Ejemplo n.º 21
0
def resnet(input_shape=(224, 224, 3), num_filters=64, classes=1000, num_blocks=[0, 0, 0, 0], version=None):
    # num_blocks 는 list로 받는 인자. conv3_x ~ conv5_x의 block의 수를 앞에서부터 차례로 넣어주면 된다.
    # num_filters 는 초기의 filters를 의미한다.

    if len(num_blocks) != 4:
        raise NameError("Please input the number of blocks from conv2_x to conv5_x in the 'num_blocks' variable.")

    if version == "v1":  # resnet 18, 34에 해당. (블록이 같음)
        block = block_v1
    elif version == "v2":  # resnet 50, 101, 152에 해당. (블록이 같음)
        block = block_v2
    else:
        raise NameError("Please input the string 'v1' or 'v2' in the 'version' variable. ")

    inputs = Input(shape=input_shape)

    # conv1
    layer1 = resnet_layer(inputs=inputs, num_filters=num_filters, strides=(2, 2), kernel_size=(7, 7), activation='relu')
    conv1_zero_pad = ZeroPadding2D(padding=(1, 1))(layer1)
    pool1_helper = PoolHelper()(conv1_zero_pad)
    cum_block = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid', name='pool1/3x3_s2')(pool1_helper)

    # cum_block 변수에 하나씩 residual block을 누적시켜보자.

    for stack in range(4):
        if stack == 0:
            # 우리가 누적시킬 변수, cum_block을 정의하고, 이 변수에 누적시켜가면서 모형을 출력한다.

            # 이 아래 for문은 conv2_x
            # 들어가기 전에, block_v2라면 input과 output의 filter size를 동일하게 해주는 작업을 한다. 덧셈이 가능해야 하므로, 반드시!
            if version == 'v2':
                cum_block = Conv2D(4 * num_filters, kernel_size=(1, 1), strides=(1, 1), padding='same',
                                   kernel_initializer='he_normal')(cum_block)
                # cum_block = BatchNormalization()(cum_block) # 모든 conv.뒤에는 bn을 적용.

            for res_block in range(num_blocks[stack]):
                cum_block = block(input_shape=cum_block, num_filters=num_filters, identi=cum_block, half=False)
        else:

            # 이 아래는 conv3_x ~ conv5_x 까지.
            num_filters *= 2

            if version == 'v2':
                x = Conv2D(4 * num_filters, kernel_size=(1, 1), strides=(2, 2), padding='valid',
                           kernel_initializer='he_normal')(cum_block)  # block_v2라면, input에 해당하는 x를 덧셈이 가능하게 맞춰준다.
                # x = BatchNormalization()(x) # 모든 conv.뒤에는 bn을 적용.
            else:
                x = Conv2D(num_filters, (1, 1), strides=(2, 2), padding='valid')(
                    cum_block)  # identi_input 에 해당 // rxc가 각각 반토막남.
                # 즉, 위 x는 결국 이어질 conv 층의 input임.

            for res_block in range(num_blocks[stack]):
                if res_block == 0:
                    cum_block = block(input_shape=cum_block, num_filters=num_filters, identi=x, half=True)  # block 갱신
                else:
                    cum_block = block(input_shape=cum_block, num_filters=num_filters, identi=cum_block,
                                      half=False)  # block 갱신

    # 마지막 1층
    y = AveragePooling2D(pool_size=(7, 7), strides=(1, 1), name='loss1/ave_pool')(cum_block)
    y = Flatten()(y)
    outputs = Dense(classes, activation='softmax', kernel_initializer='he_normal')(y)

    model = Model(inputs=inputs, outputs=outputs)
    return model
Ejemplo n.º 22
0
def get_mnist_backbone(embeded_dim, model_type="mlp", norm_type="bn", acti_type="relu"):
    """  For `mnist` and `fashion-mnist` dataset
    Args:
        embeded_dim (int): 10
    Returns:
        encoder: (28, 28, 1) -> (1, 1, 10)
        decoder: (1, 1, 10) -> (28, 28, 1)
    """
    input_shape = (28, 28, 1)
    init = "he_normal"
    if model_type == "mlp":
        return get_mlp_backbones(input_shape, embeded_dim, norm_type, acti_type, kernel_initializer=init)
    elif model_type == "conv":
        # 与原论文略有不同
        acti = acti_type

        encoder_input = Input(shape=input_shape)
        encoder_layers = []
        encoder_layers.extend(n_conv_norm(3, norm_type, acti,
                                          filters=64, kernel_size=3,
                                          padding="valid", kernel_initializer=init))  # 22,22,64
        encoder_layers.append(MaxPool2D(2, 2, padding="valid"))  # 11
        encoder_layers.extend(n_conv_norm(4, norm_type, acti,
                                          filters=128, kernel_size=3,
                                          padding="valid", kernel_initializer=init))  # 3,3,128
        # encoder_layers.append(MaxPool2D(2, 2, padding="valid"))
        encoder_layers.extend(n_conv_norm(1, norm_type, acti,
                                          filters=embeded_dim, kernel_size=1,
                                          padding="valid", kernel_initializer=init))  # 3,3,10
        # encoder_layers.append(AveragePooling2D(2))  # 1, 1, 10
        encoder_layers.append(AveragePooling2D(3))  # 1, 1, 10
        encoder_output = encoder_input
        for layer in encoder_layers:
            encoder_output = layer(encoder_output)

        decoder_input = Input(shape=(1, 1, embeded_dim))
        decoder_layers = []
        decoder_layers.append(UpSampling2D(3))  # 3
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti,
                                            filters=128, kernel_size=1,
                                            padding="valid", kernel_initializer=init))  # 3
        decoder_layers.extend(n_deconv_norm(3, norm_type, acti,
                                            filters=128, kernel_size=3,
                                            padding="valid", kernel_initializer=init))  # 9
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti,
                                            filters=64, kernel_size=3,
                                            padding="valid", kernel_initializer=init))  # 11
        decoder_layers.append(UpSampling2D(2))
        decoder_layers.extend(n_deconv_norm(2, norm_type, acti,
                                            filters=64, kernel_size=3,
                                            padding="valid", kernel_initializer=init))  # 26
        decoder_layers.append(Conv2DTranspose(1, 3, kernel_initializer=init))  # 28, (no bn)
        decoder_output = decoder_input
        for layer in decoder_layers:
            decoder_output = layer(decoder_output)

        encoder = Model(inputs=encoder_input, outputs=encoder_output, name="conv_encoder")
        decoder = Model(inputs=decoder_input, outputs=decoder_output, name="conv_encoder")
        return encoder, decoder
    elif model_type == "allconv":
        acti = acti_type
        encoder_input = Input(shape=input_shape)
        encoder_layers = []
        encoder_layers.extend(n_conv_norm(1, norm_type, acti, filters=32, kernel_size=5, strides=2,
                                          padding="same", kernel_initializer=init))  # 14
        encoder_layers.extend(n_conv_norm(1, norm_type, acti, filters=64, kernel_size=5, strides=2,
                                          padding="same", kernel_initializer=init))  # 7
        encoder_layers.extend(n_conv_norm(1, norm_type, acti, filters=128, kernel_size=3, strides=2,
                                          padding="valid", kernel_initializer=init))  # 3
        encoder_layers.append(Conv2D(embeded_dim, 3, activation="relu", kernel_initializer=init))  # 1
        encoder_output = encoder_input
        for layer in encoder_layers:
            encoder_output = layer(encoder_output)

        decoder_input = Input(shape=(1, 1, embeded_dim))
        decoder_layers = []
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti, filters=128, kernel_size=3,
                                            kernel_initializer=init))  # 3
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti, filters=64, kernel_size=3,
                                            strides=2, padding="valid", kernel_initializer=init))  # 7
        decoder_layers.extend(n_deconv_norm(1, norm_type, acti, filters=32, kernel_size=5,
                                            strides=2, padding="same", kernel_initializer=init))  # 14
        decoder_layers.append(Conv2DTranspose(input_shape[-1], 5, strides=2, padding="same",
                                              activation="relu", kernel_initializer=init))  # 28
        decoder_output = decoder_input
        for layer in decoder_layers:
            decoder_output = layer(decoder_output)
        encoder = Model(inputs=encoder_input, outputs=encoder_output, name="allconvbn_encoder")
        decoder = Model(inputs=decoder_input, outputs=decoder_output, name="allconvbn_decoder")
        return encoder, decoder
    else:
        print('Not defined model: ', model_type)
        exit(0)
Ejemplo n.º 23
0
def build():
    """Instantiates ResNet32 model """
    # Parameters for Resnet32 on Cifar-100
    num_blocks = 5
    classes = 100

    training = False
    input_shape = (32, 32, 3)
    img_input = layers.Input(shape=input_shape)
    x = img_input
    bn_axis = 1
    if tf.keras.backend.image_data_format() == "channels_last":
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D(padding=(1, 1))(x)
    x = Conv2D(
        16,
        (3, 3),
        strides=(1, 1),
        padding="valid",
        kernel_initializer="he_normal",
        kernel_regularizer=l2(L2_WEIGHT_DECAY),
        bias_regularizer=l2(L2_WEIGHT_DECAY),
    )(x)
    x = BatchNormalization(
        axis=bn_axis, momentum=BATCH_NORM_DECAY, epsilon=BATCH_NORM_EPSILON, fused=True
    )(x, training=training)
    x = Activation("approx_activation")(x)

    x = resnet_block(
        x,
        size=num_blocks,
        kernel_size=3,
        filters=[16, 16],
        stage=2,
        conv_strides=(1, 1),
        training=training,
    )

    x = resnet_block(
        x,
        size=num_blocks,
        kernel_size=3,
        filters=[32, 32],
        stage=3,
        conv_strides=(2, 2),
        training=training,
    )

    x = resnet_block(
        x,
        size=num_blocks,
        kernel_size=3,
        filters=[64, 64],
        stage=4,
        conv_strides=(2, 2),
        training=training,
    )

    x = AveragePooling2D(pool_size=(8, 8), strides=(1, 1), padding="VALID")(x)
    x = Lambda(lambda w: tf.keras.backend.squeeze(w, 1))(x)
    x = Lambda(lambda w: tf.keras.backend.squeeze(w, 1))(x)
    x = Dense(
        classes,
        activation="softmax",
        kernel_initializer="he_normal",
        kernel_regularizer=l2(L2_WEIGHT_DECAY),
        bias_regularizer=l2(L2_WEIGHT_DECAY),
    )(x)

    inputs = img_input
    # Create model.
    model = tf.keras.models.Model(inputs, x)

    return model
def InceptionResNetV2(include_top=True,
                      weights='imagenet',
                      input_tensor=None,
                      input_shape=None,
                      pooling=None,
                      classes=1000):
    """Instantiates the Inception-ResNet v2 architecture.

    Optionally loads weights pre-trained on ImageNet.
    Note that when using TensorFlow, for best performance you should
    set `"image_data_format": "channels_last"` in your Keras config
    at `~/.keras/keras.json`.

    The model and the weights are compatible with both TensorFlow and Theano
    backends (but not CNTK). The data format convention used by the model is
    the one specified in your Keras config file.

    Note that the default input image size for this model is 299x299, instead
    of 224x224 as in the VGG16 and ResNet models. For preprocessing use:
    ```
    image = ...
    image /= 127.5
    image -= 1.0

    # 'RGB'->'BGR'
    x = x[..., ::-1]

    # substract mean
    mean = [103.939, 116.779, 123.68]
    x[..., 0] -= mean[0]
    x[..., 1] -= mean[1]
    x[..., 2] -= mean[2]
    ```
    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or `'imagenet'` (pre-training on ImageNet).
        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)` (with `'channels_last'` data format)
            or `(3, 299, 299)` (with `'channels_first'` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 139.
            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 an unsupported backend.
    """
    if K.backend() in {'cntk'}:
        raise RuntimeError(K.backend() +
                           ' backend is currently unsupported for this model.')

    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

    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')

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=299,
                                      min_size=139,
                                      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

    # Stem block: 35 x 35 x 192
    x = conv2d_bn(img_input, 32, 3, strides=2, padding='same')
    x = conv2d_bn(x, 32, 3, padding='same')
    x = conv2d_bn(x, 64, 3)
    x = MaxPooling2D(3, strides=2, padding="same")(x)
    x = conv2d_bn(x, 80, 1, padding='same')
    x = conv2d_bn(x, 192, 3, padding='same')
    x = MaxPooling2D(3, strides=2, padding='same')(x)

    # Mixed 5b (Inception-A block): 35 x 35 x 320
    branch_0 = conv2d_bn(x, 96, 1)
    branch_1 = conv2d_bn(x, 48, 1)
    branch_1 = conv2d_bn(branch_1, 64, 5)
    branch_2 = conv2d_bn(x, 64, 1)
    branch_2 = conv2d_bn(branch_2, 96, 3)
    branch_2 = conv2d_bn(branch_2, 96, 3)
    branch_pool = AveragePooling2D(3, strides=1, padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 64, 1)
    branches = [branch_0, branch_1, branch_2, branch_pool]
    channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
    x = Concatenate(axis=channel_axis, name='mixed_5b')(branches)

    # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320
    for block_idx in range(1, 11):
        x = inception_resnet_block(x,
                                   scale=0.17,
                                   block_type='block35',
                                   block_idx=block_idx)

    # Mixed 6a (Reduction-A block): 17 x 17 x 1088
    branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='same')
    branch_1 = conv2d_bn(x, 256, 1)
    branch_1 = conv2d_bn(branch_1, 256, 3)
    branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='same')
    branch_pool = MaxPooling2D(3, strides=2, padding='same')(x)
    branches = [branch_0, branch_1, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_6a')(branches)

    # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088
    for block_idx in range(1, 21):
        x = inception_resnet_block(x,
                                   scale=0.1,
                                   block_type='block17',
                                   block_idx=block_idx)

    # Mixed 7a (Reduction-B block): 8 x 8 x 2080
    branch_0 = conv2d_bn(x, 256, 1)
    branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='same')
    branch_1 = conv2d_bn(x, 256, 1)
    branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='same')
    branch_2 = conv2d_bn(x, 256, 1)
    branch_2 = conv2d_bn(branch_2, 288, 3)
    branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='same')
    branch_pool = MaxPooling2D(3, strides=2, padding='same')(x)
    branches = [branch_0, branch_1, branch_2, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_7a')(branches)

    # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080
    for block_idx in range(1, 10):
        x = inception_resnet_block(x,
                                   scale=0.2,
                                   block_type='block8',
                                   block_idx=block_idx)
    x = inception_resnet_block(x,
                               scale=1.,
                               activation=None,
                               block_type='block8',
                               block_idx=10)

    # Final convolution block: 8 x 8 x 1536
    x = conv2d_bn(x, 1536, 1, name='conv_7b')

    if include_top:
        # Classification block
        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='inception_resnet_v2')

    # Load weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
        if include_top:
            weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5'
            weights_path = get_file(
                weights_filename,
                BASE_WEIGHT_URL + weights_filename,
                cache_subdir='models',
                md5_hash='e693bd0210a403b3192acc6073ad2e96')
        else:
            weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5'
            weights_path = get_file(
                weights_filename,
                BASE_WEIGHT_URL + weights_filename,
                cache_subdir='models',
                md5_hash='d19885ff4a710c122648d3b5c3b684e4')
        model.load_weights(weights_path)

    return model
Ejemplo n.º 25
0
def minigooglenet_functional(width, height, depth, classes):
    def conv_module(x, K, kX, kY, stride, chanDim, padding="same"):
        # define a CONV => BN => RELU pattern
        x = Conv2D(K, (kX, kY), strides=stride, padding=padding)(x)
        x = BatchNormalization(axis=chanDim)(x)
        x = Activation("relu")(x)

        # return the block
        return x

    def inception_module(x, numK1x1, numK3x3, chanDim):
        # define two CONV modules, then concatenate across the
        # channel dimension
        conv_1x1 = conv_module(x, numK1x1, 1, 1, (1, 1), chanDim)
        conv_3x3 = conv_module(x, numK3x3, 3, 3, (1, 1), chanDim)
        x = concatenate([conv_1x1, conv_3x3], axis=chanDim)

        # return the block
        return x

    def downsample_module(x, K, chanDim):
        # define the CONV module and POOL, then concatenate
        # across the channel dimensions
        conv_3x3 = conv_module(x, K, 3, 3, (2, 2), chanDim, padding="valid")
        pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
        x = concatenate([conv_3x3, pool], axis=chanDim)

        # return the block
        return x

    # initialize the input shape to be "channels last" and the
    # channels dimension itself
    inputShape = (height, width, depth)
    chanDim = -1

    # define the model input and first CONV module
    inputs = Input(shape=inputShape)
    x = conv_module(inputs, 96, 3, 3, (1, 1), chanDim)

    # two Inception modules followed by a downsample module
    x = inception_module(x, 32, 32, chanDim)
    x = inception_module(x, 32, 48, chanDim)
    x = downsample_module(x, 80, chanDim)

    # four Inception modules followed by a downsample module
    x = inception_module(x, 112, 48, chanDim)
    x = inception_module(x, 96, 64, chanDim)
    x = inception_module(x, 80, 80, chanDim)
    x = inception_module(x, 48, 96, chanDim)
    x = downsample_module(x, 96, chanDim)

    # two Inception modules followed by global POOL and dropout
    x = inception_module(x, 176, 160, chanDim)
    x = inception_module(x, 176, 160, chanDim)
    x = AveragePooling2D((7, 7))(x)
    x = Dropout(0.5)(x)

    # softmax classifier
    x = Flatten()(x)
    x = Dense(classes)(x)
    x = Activation("softmax")(x)

    # create the model
    model = Model(inputs, x, name="minigooglenet")

    # return the constructed network architecture
    return model
Ejemplo n.º 26
0
def BisenetV2(include_top=True,
              input_tensor=None,
              input_shape=(224, 224, 3),
              weights=None
              ):
    if K.backend() != 'tensorflow':
        raise RuntimeError('Only tensorflow supported for now')
    name = "bisenetv2"
    input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=28, require_flatten=include_top,
                                      data_format=K.image_data_format())
    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

    img_input1 = Conv2D(16, kernel_size=(3, 3), strides=2, padding="same", use_bias=False, name="stem_block/conv_block_1")(img_input)
    img_input1 = BatchNormalization(axis=-1, name="stem_block/conv_block_1/bn_1")(img_input1)
    img_input1 = Activation(activation="relu", name="stem_block/conv_block_1/activate_1")(img_input1)

    branch_left_output = Conv2D(int(16/2), kernel_size=(1, 1), strides=1, padding="same", use_bias=False, name="stem_block/downsample_branch_left/1x1_conv_block")(img_input1)
    branch_left_output = BatchNormalization(axis=-1, name="stem_block/downsample_branch_left/1x1_conv_block/bn_1")(branch_left_output)
    branch_left_output = Activation(activation="relu", name="stem_block/downsample_branch_left/1x1_conv_block/activate_1")(branch_left_output)


    branch_left_output = Conv2D(16, kernel_size=(3, 3), strides=2, padding="same", use_bias=False,
                                name="stem_block/downsample_branch_left/3x3_conv_block")(branch_left_output)
    branch_left_output = BatchNormalization(axis=-1, name="stem_block/downsample_branch_left/3x3_conv_block/bn_1")(branch_left_output)
    branch_left_output = Activation(activation="relu", name="stem_block/downsample_branch_left/3x3_conv_block/activate_1")(branch_left_output)


    branch_right_output = MaxPool2D(pool_size=(3, 3), strides=2, padding='same', name="stem_block/downsample_branch_right/maxpooling_block")(img_input1)
    stem_result = Concatenate(axis=-1, name="stem_block/concate_features")([branch_left_output, branch_right_output])
    stem_result = Conv2D(16, kernel_size=(3, 3), strides=1, padding="same", use_bias=False, name="stem_block/final_conv_block")(stem_result)
    stem_result = BatchNormalization(axis=-1, name="stem_block/final_conv_block/bn_1")(stem_result)
    stem_result = Activation(activation="relu", name="stem_block/final_conv_block/activate_1")(stem_result)

    # k_reduce_mean = Lambda(lambda x: tf.reduce_mean(x, axis=[1, 2], keepdims=True, name='global_avg_pooling'))
    # embedding_result=k_reduce_mean(stem_result)
    # embedding_result = K.mean(stem_result, axis=[1, 2], keepdims=True)
    embedding_result = KerasReduceMean(axis=(1, 2), keep_dim=True, name="global_avg_pooling")(stem_result)

    embedding_result = BatchNormalization(axis=-1, name="context_embedding_block/bn")(embedding_result)
    output_channels = stem_result.get_shape().as_list()[-1]
    embedding_result = Conv2D(output_channels, kernel_size=(1, 1), strides=1, padding="same", use_bias=False,
                              name="context_embedding_block/conv_block_1")(embedding_result)
    embedding_result = BatchNormalization(axis=-1, name="context_embedding_block/conv_block_1/bn_1")(embedding_result)
    embedding_result = Activation(activation="relu", name="context_embedding_block/conv_block_1/activate_1")(embedding_result)
    embedding_result = Add(name="context_embedding_block/fused_features")([embedding_result, stem_result])
    embedding_result = Conv2D(output_channels, kernel_size=(3, 3), strides=1, padding="same", use_bias=False, name="context_embedding_block/final_conv_block")(embedding_result)


    output_channels = embedding_result.get_shape().as_list()[-1]
    gather_expansion_result = Conv2D(output_channels, kernel_size=(3, 3), strides=1, padding="same", use_bias=False,
                                     name="ge_block_with_stride_1/stride_equal_one_module/3x3_conv_block")(embedding_result)
    gather_expansion_result = BatchNormalization(axis=-1, name="ge_block_with_stride_1/stride_equal_one_module/3x3_conv_block/bn_1")(gather_expansion_result)
    gather_expansion_result = Activation(activation="relu", name="ge_block_with_stride_1/stride_equal_one_module/3x3_conv_block/activate_1")(gather_expansion_result)

    gather_expansion_result = DepthwiseConv2D(kernel_size=3, strides=1, depth_multiplier=6, padding='same',
                                              name="ge_block_with_stride_1/stride_equal_one_module/depthwise_conv_block")(gather_expansion_result)
    gather_expansion_result = BatchNormalization(axis=-1, name="ge_block_with_stride_1/stride_equal_one_module/dw_bn")(gather_expansion_result)

    gather_expansion_result = Conv2D(output_channels, kernel_size=(1, 1), strides=1, padding="same", use_bias=False,
                                     name="ge_block_with_stride_1/stride_equal_one_module/1x1_conv_block")(gather_expansion_result)
    gather_expansion_result = Add(name="ge_block_with_stride_1/stride_equal_one_module/fused_features")([embedding_result, gather_expansion_result])
    gather_expansion_result = Activation(activation="relu", name="ge_block_with_stride_1/stride_equal_one_module/ge_output")(gather_expansion_result)

    gather_expansion_proj_result = DepthwiseConv2D(kernel_size=3, depth_multiplier=1, strides=2, padding="same",
                                                   name="ge_block_with_stride_2/stride_equal_two_module/input_project_dw_conv_block")(gather_expansion_result)
    gather_expansion_proj_result = BatchNormalization(axis=-1, name="ge_block_with_stride_2/stride_equal_two_module/input_project_bn")(gather_expansion_proj_result)
    gather_expansion_proj_result = Conv2D(128, kernel_size=(1, 1), strides=1, padding="same", use_bias=False, activation=None)(gather_expansion_proj_result)
    input_tensor_channels = gather_expansion_result.get_shape().as_list()[-1]
    gather_expansion_stride2_result = Conv2D(input_tensor_channels, kernel_size=(3, 3), strides=1, padding="same",
                                             use_bias=False, name="ge_block_with_stride_2/stride_equal_two_module/3x3_conv_block")(gather_expansion_result)
    gather_expansion_stride2_result = BatchNormalization(axis=-1, name="ge_block_with_stride_2/stride_equal_two_module/3x3_conv_block/bn_1")(gather_expansion_stride2_result)
    gather_expansion_stride2_result = Activation(activation="relu", name="ge_block_with_stride_2/stride_equal_two_module/3x3_conv_block/activate_1")(gather_expansion_stride2_result)

    gather_expansion_stride2_result = DepthwiseConv2D(kernel_size=3, depth_multiplier=6, strides=2, padding="same",
                                                      name="ge_block_with_stride_2/stride_equal_two_module/depthwise_conv_block_1")(gather_expansion_stride2_result)
    gather_expansion_stride2_result = BatchNormalization(axis=-1, name="ge_block_with_stride_2/stride_equal_two_module/dw_bn_1")(gather_expansion_stride2_result)
    gather_expansion_stride2_result = DepthwiseConv2D(kernel_size=3, depth_multiplier=1, strides=1, padding="same",
                                                      name="ge_block_with_stride_2/stride_equal_two_module/depthwise_conv_block_2")(gather_expansion_stride2_result)
    gather_expansion_stride2_result = BatchNormalization(axis=-1, name="ge_block_with_stride_2/stride_equal_two_module/dw_bn_2")(gather_expansion_stride2_result)
    gather_expansion_stride2_result = Conv2D(128, kernel_size=(1, 1), strides=1, padding="same",
                                             use_bias=False, activation=None, name="ge_block_with_stride_2/stride_equal_two_module/1x1_conv_block")(gather_expansion_stride2_result)
    gather_expansion_total_result = Add(name="ge_block_with_stride_2/stride_equal_two_module/fused_features")([gather_expansion_proj_result, gather_expansion_stride2_result])
    gather_expansion_total_result = Activation(activation="relu", name="ge_block_with_stride_2/stride_equal_two_module/ge_output")(gather_expansion_total_result)


    gather_expansion_proj2_result = DepthwiseConv2D(kernel_size=3, depth_multiplier=1, strides=2, padding="same",
                                                   name="ge_block_with_stride_2_repeat/stride_equal_two_module/input_project_dw_conv_block")(gather_expansion_total_result)
    gather_expansion_proj2_result = BatchNormalization(axis=-1, name="ge_block_with_stride_2_repeat/stride_equal_two_module/input_project_bn")(gather_expansion_proj2_result)
    gather_expansion_proj2_result = Conv2D(128, kernel_size=(1, 1), strides=1, padding="same", use_bias=False, activation=None)(gather_expansion_proj2_result)
    input_tensor_channels = gather_expansion_total_result.get_shape().as_list()[-1]
    gather_expansion_stride2_result_repeat = Conv2D(input_tensor_channels, kernel_size=(3, 3), strides=1,  padding="same",
                                             use_bias=False, name="ge_block_with_stride_2_repeat/stride_equal_two_module/3x3_conv_block")(gather_expansion_total_result)
    gather_expansion_stride2_result_repeat = BatchNormalization(axis=-1, name="ge_block_with_stride_2_repeat/stride_equal_two_module/3x3_conv_block/bn_1")(gather_expansion_stride2_result_repeat)
    gather_expansion_stride2_result_repeat = Activation(activation="relu", name="ge_block_with_stride_2_repeat/stride_equal_two_module/3x3_conv_block/activate_1")(gather_expansion_stride2_result_repeat)

    gather_expansion_stride2_result_repeat = DepthwiseConv2D(kernel_size=3, depth_multiplier=6, strides=2, padding="same",
                                                      name="ge_block_with_stride_2_repeat/stride_equal_two_module/depthwise_conv_block_1")(gather_expansion_stride2_result_repeat)
    gather_expansion_stride2_result_repeat = BatchNormalization(axis=-1, name="ge_block_with_stride_2_repeat/stride_equal_two_module/dw_bn_1")(gather_expansion_stride2_result_repeat)
    gather_expansion_stride2_result_repeat = DepthwiseConv2D(kernel_size=3, depth_multiplier=1, strides=1, padding="same",
                                                      name="ge_block_with_stride_2_repeat/stride_equal_two_module/depthwise_conv_block_2")(gather_expansion_stride2_result_repeat)
    gather_expansion_stride2_result_repeat = BatchNormalization(axis=-1, name="ge_block_with_stride_2_repeat/stride_equal_two_module/dw_bn_2")(gather_expansion_stride2_result_repeat)
    gather_expansion_stride2_result_repeat = Conv2D(128, kernel_size=(1, 1), strides=1, padding="same",
                                             use_bias=False, activation=None, name="ge_block_with_stride_2_repeat/stride_equal_two_module/1x1_conv_block")(gather_expansion_stride2_result_repeat)
    gather_expansion_total_result_repeat = Add(name="ge_block_with_stride_2_repeat/stride_equal_two_module/fused_features")([gather_expansion_proj2_result, gather_expansion_stride2_result_repeat])
    gather_expansion_total_result_repeat = Activation(activation="relu", name="ge_block_with_stride_2_repeat/stride_equal_two_module/ge_output")(gather_expansion_total_result_repeat)

    detail_input_tensor = stem_result
    semantic_input_tensor = gather_expansion_total_result_repeat
    output_channels = stem_result.get_shape().as_list()[-1]
    detail_branch_remain = DepthwiseConv2D(kernel_size=3, strides=1, padding="same", depth_multiplier=1,
                                           name="guided_aggregation_block/detail_branch/3x3_dw_conv_block")(detail_input_tensor)
    detail_branch_remain = BatchNormalization(axis=-1, name="guided_aggregation_block/detail_branch/bn_1")(detail_branch_remain)
    detail_branch_remain = Conv2D(output_channels, kernel_size=(1, 1), padding="same", strides=1, use_bias=False,
                                  name="guided_aggregation_block/detail_branch/1x1_conv_block")(detail_branch_remain)

    detail_branch_downsample = Conv2D(output_channels, kernel_size=(3, 3), strides=2, use_bias=False, activation=None,
                                      padding="same", name="guided_aggregation_block/detail_branch/3x3_conv_block")(detail_input_tensor)

    detail_branch_downsample = AveragePooling2D(pool_size=(3, 3), strides=2, padding="same", name="guided_aggregation_block/detail_branch/avg_pooling_block")(detail_branch_downsample)

    semantic_branch_remain = DepthwiseConv2D(kernel_size=3, strides=1, padding="same", depth_multiplier=1,
                                             name="guided_aggregation_block/semantic_branch/3x3_dw_conv_block")(semantic_input_tensor)
    semantic_branch_remain = BatchNormalization(axis=-1, name="guided_aggregation_block/semantic_branch/bn_1")(semantic_branch_remain)
    semantic_branch_remain = Conv2D(output_channels, kernel_size=(1, 1), strides=1, use_bias=False, activation=None, padding="same",
                                    name="guided_aggregation_block/semantic_branch/1x1_conv_block")(semantic_branch_remain)
    # semantic_branch_remain = sigmoid(semantic_branch_remain)
    # keras_sigmoid = Lambda(lambda x: tf.nn.sigmoid(x, name="guided_aggregation_block/semantic_branch/semantic_remain_sigmoid"))
    # semantic_branch_remain = keras_sigmoid(semantic_branch_remain)
    semantic_branch_remain = Activation("sigmoid", name="guided_aggregation_block/semantic_branch/semantic_remain_sigmoid")(semantic_branch_remain)

    semantic_branch_upsample = Conv2D(output_channels, kernel_size=(3, 3), strides=1, padding="same", use_bias=False,
                                      activation=None, name="guided_aggregation_block/semantic_branch/3x3_conv_block")(semantic_input_tensor)
    # semantic_branch_upsample = resize_images(semantic_branch_upsample, 4, 4, data_format="channels_last", interpolation='bilinear')

    # upsample_bilinear0 = Lambda(lambda x: tf.image.resize_bilinear(x, size=stem_result.get_shape().as_list()[1:3],
    #                                                               name="guided_aggregation_block/semantic_branch/semantic_upsample_features"))
    # semantic_branch_upsample = upsample_bilinear0(semantic_branch_upsample)
    semantic_branch_upsample = BilinearUpSampling2D((4, 4), name="guided_aggregation_block/semantic_branch/semantic_upsample_features")(semantic_branch_upsample)
    semantic_branch_upsample = Activation("sigmoid", name="guided_aggregation_block/semantic_branch/semantic_branch_upsample_sigmoid")(semantic_branch_upsample)
    # keras_sigmoid_1 = Lambda(lambda x: tf.nn.sigmoid(x, name="guided_aggregation_block/semantic_branch/semantic_branch_upsample_sigmoid"))
    # semantic_branch_upsample = keras_sigmoid_1(semantic_branch_upsample)
    # semantic_branch_upsample = sigmoid(semantic_branch_upsample)

    guided_features_remain = Multiply(name="guided_aggregation_block/aggregation_features/guided_detail_features")([detail_branch_remain, semantic_branch_upsample])
    guided_features_downsample = Multiply(name="guided_aggregation_block/aggregation_features/guided_semantic_features")([detail_branch_downsample, semantic_branch_remain])

    # upsample_bilinear1 = Lambda(lambda x: tf.image.resize_bilinear(x, size=stem_result.get_shape().as_list()[1:3],
    #                                        name="guided_aggregation_block/aggregation_features/guided_upsample_features"))
    #
    # guided_features_upsample = upsample_bilinear1(guided_features_downsample)
    guided_features_upsample = BilinearUpSampling2D((4, 4), name="guided_aggregation_block/aggregation_features/guided_upsample_features")(guided_features_downsample)
    # guided_features_upsample = resize_images(guided_features_downsample, 4, 4, data_format="channels_last", interpolation='bilinear')

    guided_features = Add(name="guided_aggregation_block/aggregation_features/fused_features")([guided_features_remain, guided_features_upsample])
    guided_features = Conv2D(output_channels, kernel_size=(3, 3), strides=1, use_bias=False, padding="same",
                             name="guided_aggregation_block/aggregation_features/aggregation_feature_output")(guided_features)
    guided_features = BatchNormalization(axis=-1, name="guided_aggregation_block/aggregation_features/aggregation_feature_output/bn_1")(guided_features)
    guided_features = Activation(activation="relu", name="guided_aggregation_block/aggregation_features/aggregation_feature_output/activate_1")(guided_features)

    # input_tensor_size = [int(tmp * 4)for tmp in guided_features.get_shape().as_list()[1:3]]
    result = Conv2D(8, kernel_size=(3, 3), strides=1, use_bias=False, padding="same", name="seg_head_block/3x3_conv_block")(guided_features)
    result = BatchNormalization(axis=-1, name="seg_head_block/bn_1")(result)
    result = Activation("relu", name="seg_head_block/activate_1")(result)

    # upsample_bilinear2 = Lambda(lambda x: tf.image.resize_bilinear(x, size=input_tensor_size, name="seg_head_block/segmentation_head_logits"))
    # result = upsample_bilinear2(result)
    result = BilinearUpSampling2D((4, 4), name="seg_head_block/segmentation_head_upsample")(result)
    # result = resize_images(result, 4, 4, data_format="channels_last", interpolation='bilinear')

    result = Conv2D(1, kernel_size=(1, 1), strides=1, use_bias=False, padding="same",
                    name="seg_head_block/1x1_conv_block")(result)
    if input_tensor:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    model = Model(inputs, result, name=name)

    if weights:
        model.load_weights(weights, by_name=True)

    return model
                          zoom_range=0.15,
                          width_shift_range=0.2,
                          height_shift_range=0.2,
                          shear_range=0.15,
                          horizontal_flip=True,
                          vertical_flip=True,
                          fill_mode='nearest')

#building neural network using mobileNetV2 and 5 layers
baseModel = MobileNetV2(weights='imagenet',
                        include_top=False,
                        input_tensor=Input(shape=(224, 224, 3)))
baseModel.summary()

headModel = baseModel.output
headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
headModel = Flatten(name='Flatten')(headModel)
headModel = Dense(64, activation='relu')(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(2, activation='softmax')(headModel)
model = Model(inputs=baseModel.input, outputs=headModel)
model.summary()

for layer in baseModel.layers:
    layer.trainable = False
learning_rate = 0.001
Epochs = 20
Batch_size = 32
print(len(train_X) // Batch_size)
optimizer = Adam(lr=learning_rate, decay=learning_rate / Epochs)
model.compile(loss='binary_crossentropy',
Ejemplo n.º 28
0
def buildSE_ResNet152Model(img_height, img_width, img_channl, num_classes,
                           num_GPU):
    inputs = Input(shape=(img_height, img_width, img_channl))

    x = ZeroPadding2D((3, 3))(inputs)
    x = Conv2d_BN(x,
                  nb_filter=64,
                  kernel_size=(7, 7),
                  strides=(2, 2),
                  padding='valid')
    x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)

    x = Conv_Block_3(x,
                     nb_filter=[64, 64, 256],
                     kernel_size=(3, 3),
                     strides=(1, 1),
                     with_conv_shortcut=True)
    x = Conv_Block_3(x, nb_filter=[64, 64, 256], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[64, 64, 256], kernel_size=(3, 3))

    x = Conv_Block_3(x,
                     nb_filter=[128, 128, 512],
                     kernel_size=(3, 3),
                     strides=(2, 2),
                     with_conv_shortcut=True)
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[128, 128, 512], kernel_size=(3, 3))

    x = Conv_Block_3(x,
                     nb_filter=[256, 256, 1024],
                     kernel_size=(3, 3),
                     strides=(2, 2),
                     with_conv_shortcut=True)
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))

    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))

    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))

    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[256, 256, 1024], kernel_size=(3, 3))

    x = Conv_Block_3(x,
                     nb_filter=[512, 512, 2048],
                     kernel_size=(3, 3),
                     strides=(2, 2),
                     with_conv_shortcut=True)
    x = Conv_Block_3(x, nb_filter=[512, 512, 2048], kernel_size=(3, 3))
    x = Conv_Block_3(x, nb_filter=[512, 512, 2048], kernel_size=(3, 3))
    x = AveragePooling2D(pool_size=(7, 7))(x)
    x = Flatten()(x)
    outputs = Dense(num_classes, activation='sigmoid')(x)
    model = Model(inputs=inputs, outputs=outputs)
    model.summary()
    if (num_GPU > 1):
        model = multi_gpu_model(model, gpus=num_GPU)
    model.compile(
        loss='binary_crossentropy',
        optimizer='adam',  #0.001
        metrics=['categorical_accuracy', 'binary_accuracy', f1_m])
    return model
Ejemplo n.º 29
0
def Deeplabv3(weights=None, input_tensor=None, infer = False, input_shape=(512, 512, 3), classes=2, backbone='mobilenetv2', OS=16, alpha=1.):
    """ Instantiates the Deeplabv3+ architecture
    Optionally loads weights pre-trained
    on PASCAL VOC. This model is available for TensorFlow only,
    and can only be used with inputs following the TensorFlow
    data format `(width, height, channels)`.
    # Arguments
        weights: one of 'pascal_voc' (pre-trained on pascal voc)
            or None (random initialization)
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images
        classes: number of desired classes. If classes != 21,
            last layer is initialized randomly
        backbone: backbone to use. one of {'xception','mobilenetv2'}
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - If `alpha` < 1.0, proportionally decreases the number
                    of filters in each layer.
                - If `alpha` > 1.0, proportionally increases the number
                    of filters in each layer.
                - If `alpha` = 1, default number of filters from the paper
                    are used at each layer.
            Used only for mobilenetv2 backbone
    # Returns
        A Keras model instance.
    # Raises
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
        ValueError: in case of invalid argument for `weights` or `backbone`
    """
        
    if not (weights in {'pascal_voc', None}):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `pascal_voc` '
                         '(pre-trained on PASCAL VOC)')

    # if K.backend() != 'tensorflow':
    #     raise RuntimeError('The Deeplabv3+ model is only available with '
    #                        'the TensorFlow backend.')

    if not (backbone in {'xception', 'mobilenetv2'}):
        raise ValueError('The `backbone` argument should be either '
                         '`xception`  or `mobilenetv2` ')

    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
    
    
    
    # batches_input = Lambda(lambda x: x/127.5 - 1)(img_input)

    print(img_input)
 
    OS = 8
    first_block_filters = _make_divisible(32 * alpha, 8)
    x = Conv2D(first_block_filters,
               kernel_size=3,
               strides=(2, 2), padding='same',
               use_bias=False, name='Conv')(img_input)
    x = BatchNormalization(
        epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)
    
    x = Lambda(lambda x: relu(x, max_value=6.))(x)

    x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1,
                            expansion=1, block_id=0, skip_connection=False)

    x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2,
                            expansion=6, block_id=1, skip_connection=False)
    x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1,
                            expansion=6, block_id=2, skip_connection=True)

    x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2,
                            expansion=6, block_id=3, skip_connection=False)
    x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1,
                            expansion=6, block_id=4, skip_connection=True)
    x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1,
                            expansion=6, block_id=5, skip_connection=True)

    # stride in block 6 changed from 2 -> 1, so we need to use rate = 2
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1,  # 1!
                            expansion=6, block_id=6, skip_connection=False)
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2,
                            expansion=6, block_id=7, skip_connection=True)
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2,
                            expansion=6, block_id=8, skip_connection=True)
    x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2,
                            expansion=6, block_id=9, skip_connection=True)

    x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2,
                            expansion=6, block_id=10, skip_connection=False)
    x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2,
                            expansion=6, block_id=11, skip_connection=True)
    x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2,
                            expansion=6, block_id=12, skip_connection=True)

    x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=2,  # 1!
                            expansion=6, block_id=13, skip_connection=False)
    x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4,
                            expansion=6, block_id=14, skip_connection=True)
    x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4,
                            expansion=6, block_id=15, skip_connection=True)

    x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, rate=4,
                            expansion=6, block_id=16, skip_connection=False)

    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling

    # Image Feature branch
    #out_shape = int(np.ceil(input_shape[0] / OS))
    b4 = AveragePooling2D(pool_size=(int(np.ceil(input_shape[0] / OS)), int(np.ceil(input_shape[1] / OS))))(x)
        
    b4 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation('relu')(b4)
    
    b4 = Lambda(lambda x: tf.image.resize(x,size=(int(np.ceil(input_shape[0]/OS)), int(np.ceil(input_shape[1]/OS))), method='bilinear'))(b4)

    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation('relu', name='aspp0_activation')(b0)

    # there are only 2 branches in mobilenetV2. not sure why

    x = Concatenate()([b4, b0])
        
    x = Conv2D(256, (1, 1), padding='same',
               use_bias=False, name='concat_projection')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation('relu')(x)
    x = Dropout(0.1)(x)

    # DeepLab v.3+ decoder

    # you can use it with arbitary number of classes
    if classes == 21:
        last_layer_name = 'logits_semantic'
    else:
        last_layer_name = 'custom_logits_semantic'
    
    
    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)
    x = Lambda(lambda x: tf.image.resize(x,size=(input_shape[0],input_shape[1]), method='bilinear'))(x)
    

    if infer:
        x = Activation('softmax')(x)
    else:
        x = Reshape((input_shape[0]*input_shape[1], classes)) (x)
        x = Activation('softmax')(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

    model = Model(inputs, x, name='deeplabv3p')

    # load weights

    if weights == 'pascal_voc':
        if backbone == 'xception':
            weights_path = tf.keras.utils.get_file('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',
                                    WEIGHTS_PATH_X,
                                    cache_subdir='models')
        else:
            weights_path = tf.keras.utils.get_file('deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',
                                    WEIGHTS_PATH_MOBILE,
                                    cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    return model
Ejemplo n.º 30
0
def ResNet50(input_shape, classes):
    # making a tensor of shape input_shape
    X_input = Input(input_shape)
    # zero padding the tensor
    X = ZeroPadding2D((3, 3))(X_input)

    # stage 1
    X = Conv2D(filters=64,
               kernel_size=(7, 7),
               strides=(2, 2),
               name='conv1',
               kernel_initializer='he_normal')(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2), name='max_pool')(X)

    # stage 2
    X = convBlock(inputTensor=X,
                  kernel_size=3,
                  filters=[64, 64, 256],
                  stage=2,
                  block='a',
                  strides=(1, 1))
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[64, 64, 256],
                      stage=2,
                      block='b')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[64, 64, 256],
                      stage=2,
                      block='c')

    # stage 3
    X = convBlock(inputTensor=X,
                  kernel_size=3,
                  filters=[128, 128, 512],
                  stage=3,
                  block='a',
                  strides=(2, 2))
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[128, 128, 512],
                      stage=3,
                      block='b')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[128, 128, 512],
                      stage=3,
                      block='c')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[128, 128, 512],
                      stage=3,
                      block='d')

    # stage 4
    X = convBlock(inputTensor=X,
                  kernel_size=3,
                  filters=[256, 256, 1024],
                  stage=4,
                  block='a',
                  strides=(2, 2))
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[256, 256, 1024],
                      stage=4,
                      block='b')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[256, 256, 1024],
                      stage=4,
                      block='c')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[256, 256, 1024],
                      stage=4,
                      block='d')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[256, 256, 1024],
                      stage=4,
                      block='e')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[256, 256, 1024],
                      stage=4,
                      block='f')

    # stage 5
    X = convBlock(inputTensor=X,
                  kernel_size=3,
                  filters=[512, 512, 2048],
                  stage=5,
                  block='a',
                  strides=(2, 2))
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[512, 512, 2048],
                      stage=5,
                      block='b')
    X = identityBlock(inputTensor=X,
                      kernel_size=3,
                      filters=[512, 512, 2048],
                      stage=5,
                      block='c')

    # pooling (average)
    X = AveragePooling2D((7, 7), name='avg_pool')(X)

    # flatten the output layer
    X = Flatten()(X)
    # add a dense layer
    X = Dense(128,
              activation='relu',
              name=f'fc_128',
              kernel_initializer='he_normal')(X)
    # add a dropout layer to address overfitting
    X = Dropout(0.5)(X)
    # creating a fully connected layer for predicting classes
    X = Dense(classes,
              activation='softmax',
              name=f'fc_{classes}',
              kernel_initializer='he_normal')(X)

    # finally creating a model
    model = Model(inputs=X_input, outputs=X, name='ResNet50')

    # return the model
    return model