Example #1
0
def DCCNN(band, imx, ncla1):
    input1 = Input(shape=(imx, imx, band))

    # define network
    conv01 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv02 = Conv2D(128,
                    kernel_size=(3, 3),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv03 = Conv2D(128,
                    kernel_size=(5, 5),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    bn1 = BatchNormalization(axis=-1,
                             momentum=0.9,
                             epsilon=0.001,
                             center=True,
                             scale=True,
                             beta_initializer='zeros',
                             gamma_initializer='ones',
                             moving_mean_initializer='zeros',
                             moving_variance_initializer='ones')
    bn2 = BatchNormalization(axis=-1,
                             momentum=0.9,
                             epsilon=0.001,
                             center=True,
                             scale=True,
                             beta_initializer='zeros',
                             gamma_initializer='ones',
                             moving_mean_initializer='zeros',
                             moving_variance_initializer='ones')
    conv0 = Conv2D(128,
                   kernel_size=(1, 1),
                   padding='same',
                   kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv11 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv12 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv21 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv22 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv31 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv32 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv33 = Conv2D(128,
                    kernel_size=(1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    fc1 = Dense(ncla1,
                activation='softmax',
                name='output1',
                kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    # begin
    x1 = conv01(input1)
    x2 = conv02(input1)
    x3 = conv03(input1)
    x1 = MaxPooling2D(pool_size=(5, 5))(x1)
    x2 = MaxPooling2D(pool_size=(3, 3))(x2)
    x1 = concatenate([x1, x2, x3], axis=-1)

    x1 = Activation('relu')(x1)
    x1 = bn1(x1)
    x1 = conv0(x1)

    x11 = Activation('relu')(x1)
    x11 = bn2(x11)
    x11 = conv11(x11)
    x11 = Activation('relu')(x11)
    x11 = conv12(x11)
    x1 = Add()([x1, x11])

    x11 = Activation('relu')(x1)
    x11 = conv21(x11)
    x11 = Activation('relu')(x11)
    x11 = conv22(x11)
    x1 = Add()([x1, x11])

    x1 = Activation('relu')(x1)
    x1 = conv31(x1)
    x1 = Activation('relu')(x1)
    x1 = Dropout(0.5)(x1)
    x1 = conv32(x1)
    x1 = Activation('relu')(x1)
    x1 = Dropout(0.5)(x1)
    x1 = conv33(x1)

    x1 = Flatten()(x1)
    pre1 = fc1(x1)

    model1 = Model(inputs=input1, outputs=pre1)
    return model1
X_train, X_valid, y_train, y_valid = train_test_split(veri,
                                                      labels,
                                                      test_size=0.1)

X_train = np.array(X_train).reshape(691, 8, 1)
X_valid = np.array(X_valid).reshape(77, 8, 1)

#Modelin Oluşturulması
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Conv1D, Dropout, Flatten, MaxPooling1D

model = Sequential()

model = Sequential()
model.add(Conv1D(512, 1, input_shape=(nb_features, 1)))
model.add(Activation("relu"))
model.add(MaxPooling1D(2))
model.add(Conv1D(256, 1))
model.add(Activation("relu"))
model.add(MaxPooling1D(2))
#verilerlin %25 atıyoruz
model.add(Dropout(0.25))
#verileri düzleştirme
model.add(Flatten())
#Yapay Sinir Ağı
model.add(Dense(2048, activation="relu"))
model.add(Dense(1024, activation="relu"))
#En son sonoflandırma yapalım. Sınıflandırma için softmax kullanılır
model.add(Dense(nb_classes, activation="softmax"))
model.summary()
Example #3
0
def EEGNet_SSVEP(nb_classes = 12, Chans = 8, Samples = 256, 
             dropoutRate = 0.5, kernLength = 256, F1 = 96, 
             D = 1, F2 = 96, dropoutType = 'Dropout'):
    """ SSVEP Variant of EEGNet, as used in [1]. 

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

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

    ##################################################################
    block1       = Conv2D(F1, (1, kernLength), padding = 'same',
                                   input_shape = (Chans, Samples, 1),
                                   use_bias = False)(input1)
    block1       = BatchNormalization()(block1)
    block1       = DepthwiseConv2D((Chans, 1), use_bias = False, 
                                   depth_multiplier = D,
                                   depthwise_constraint = max_norm(1.))(block1)
    block1       = BatchNormalization()(block1)
    block1       = Activation('elu')(block1)
    block1       = AveragePooling2D((1, 4))(block1)
    block1       = dropoutType(dropoutRate)(block1)
    
    block2       = SeparableConv2D(F2, (1, 16),
                                   use_bias = False, padding = 'same')(block1)
    block2       = BatchNormalization()(block2)
    block2       = Activation('elu')(block2)
    block2       = AveragePooling2D((1, 8))(block2)
    block2       = dropoutType(dropoutRate)(block2)
        
    flatten      = Flatten(name = 'flatten')(block2)
    
    dense        = Dense(nb_classes, name = 'dense')(flatten)
    softmax      = Activation('softmax', name = 'softmax')(dense)
    
    return Model(inputs=input1, outputs=softmax)
Example #4
0
    def __init__(self, input_size=512):
        '''
        输入部分, 训练时有除了input_image以外, 还有
        overly_small_text_region_training_mask : 标记尺寸过小的文字, 避免干扰训练
        text_region_boundary_training_mask: 标记文本框与score map的边界
        target_score_map: grand truth score
        '''
        input_image = Input(shape=(None, None, 3), name='input_image')
        overly_small_text_region_training_mask = Input(
            shape=(None, None, 1),
            name='overly_small_text_region_training_mask')
        text_region_boundary_training_mask = Input(
            shape=(None, None, 1), name='text_region_boundary_training_mask')
        target_score_map = Input(shape=(None, None, 1),
                                 name='target_score_map')

        resnet = MobileNetV2(input_tensor=input_image)
        x = resnet.get_layer('out_relu').output

        x = Lambda(resize_bilinear, name='resize_1')(x)
        x = concatenate([x, resnet.get_layer('block_13_expand_relu').output],
                        axis=3)
        x = Conv2D(128, (1, 1),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)
        x = Conv2D(128, (3, 3),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)

        x = Lambda(resize_bilinear, name='resize_2')(x)
        x = concatenate([x, resnet.get_layer('block_6_expand_relu').output],
                        axis=3)
        x = Conv2D(64, (1, 1),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)
        x = Conv2D(64, (3, 3),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)

        x = Lambda(resize_bilinear, name='resize_3')(x)
        x = concatenate([x, resnet.get_layer('block_3_expand_relu').output],
                        axis=3)
        x = Conv2D(32, (1, 1),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)
        x = Conv2D(32, (3, 3),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)

        x = Conv2D(32, (3, 3),
                   padding='same',
                   kernel_regularizer=regularizers.l2(1e-5))(x)
        x = BatchNormalization(momentum=0.997, epsilon=1e-5, scale=True)(x)
        x = Activation('relu')(x)

        pred_score_map = Conv2D(1, (1, 1),
                                activation=tf.nn.sigmoid,
                                name='pred_score_map')(x)
        rbox_geo_map = Conv2D(4, (1, 1),
                              activation=tf.nn.sigmoid,
                              name='rbox_geo_map')(x)
        rbox_geo_map = Lambda(lambda x: x * input_size)(rbox_geo_map)
        angle_map = Conv2D(1, (1, 1),
                           activation=tf.nn.sigmoid,
                           name='rbox_angle_map')(x)
        angle_map = Lambda(lambda x: (x - 0.5) * np.pi / 2)(angle_map)
        pred_geo_map = concatenate([rbox_geo_map, angle_map],
                                   axis=3,
                                   name='pred_geo_map')

        model = Model(inputs=[
            input_image, overly_small_text_region_training_mask,
            text_region_boundary_training_mask, target_score_map
        ],
                      outputs=[pred_score_map, pred_geo_map])

        self.model = model
        self.input_image = input_image
        self.overly_small_text_region_training_mask = overly_small_text_region_training_mask
        self.text_region_boundary_training_mask = text_region_boundary_training_mask
        self.target_score_map = target_score_map
        self.pred_score_map = pred_score_map
        self.pred_geo_map = pred_geo_map
Example #5
0
def resnet_v1(input_shape, depth, num_classes=10):
    """ResNet Version 1 Model builder [a]

    Stacks of 2 x (3 x 3) Conv2D-BN-ReLU
    Last ReLU is after the shortcut connection.
    At the beginning of each stage, the feature map size is halved (downsampled)
    by a convolutional layer with strides=2, while the number of filters is
    doubled. Within each stage, the layers have the same number filters and the
    same number of filters.
    Features maps sizes:
    stage 0: 32x32, 16
    stage 1: 16x16, 32
    stage 2:  8x8,  64
    The Number of parameters is approx the same as Table 6 of [a]:
    ResNet20 0.27M
    ResNet32 0.46M
    ResNet44 0.66M
    ResNet56 0.85M
    ResNet110 1.7M

    Args:
        input_shape (tensor): shape of input image tensor
        depth (int): number of core convolutional layers
        num_classes (int): number of classes (CIFAR10 has 10)

    Returns:
        model (Model): Keras model instance
    """
    if (depth - 2) % 6 != 0:
        raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])')
    # Start model definition.
    num_filters = 16
    num_res_blocks = int((depth - 2) / 6)

    inputs = Input(shape=input_shape)
    x = resnet_layer(inputs=inputs)
    # Instantiate the stack of residual units
    for stack in range(3):
        for res_block in range(num_res_blocks):
            strides = 1
            if stack > 0 and res_block == 0:  # first layer but not first stack
                strides = 2  # downsample
            y = resnet_layer(inputs=x,
                             num_filters=num_filters,
                             strides=strides)
            y = resnet_layer(inputs=y,
                             num_filters=num_filters,
                             activation=None)
            if stack > 0 and res_block == 0:  # first layer but not first stack
                # linear projection residual shortcut connection to match
                # changed dims
                x = resnet_layer(inputs=x,
                                 num_filters=num_filters,
                                 kernel_size=1,
                                 strides=strides,
                                 activation=None,
                                 batch_normalization=False)
            x = keras.layers.add([x, y])
            x = Activation('relu')(x)
        num_filters *= 2

    # Add classifier on top.
    # v1 does not use BN after last shortcut connection-ReLU
    x = AveragePooling2D(pool_size=x.shape[1:3])(x)
    y = Flatten()(x)
    outputs = Dense(num_classes,
                    activation='softmax',
                    kernel_initializer='he_normal')(y)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model
Example #6
0
def HRNetResidual(input_shape=(128, 128, 3), num_keypoints=20):
    """Instantiates HRNET Residual model

    # Arguments
        input_shape: List of three elements e.g. ''(H, W, 3)''
        num_keypoints: Int.

    # Returns
        Tensorflow-Keras model.

    # References
       -[High-Resolution Representations for Labeling Pixels
            and Regions](https://arxiv.org/pdf/1904.04514.pdf)
    """

    # stem
    inputs = Input(shape=input_shape, name='image')
    x1 = stem(inputs, 64)
    x1 = Conv2D(64 * 4, 1, padding='same', use_bias=False)(x1)
    x1 = BatchNormalization()(x1)
    for block in range(4):
        x1 = bottleneck(x1)

    # stage I
    x1 = Conv2D(32, 3, padding='same', use_bias=False)(x1)
    x1 = BatchNormalization()(x1)
    x1 = Activation('relu')(x1)
    x2 = transition_block(x1, 2)

    # stage II
    for block in range(4):
        x1 = residual_block(x1, 32)
        x2 = residual_block(x2, 64)
    x1, x2 = fuse([x1, x2])
    x3 = transition_block(x2, 2)

    # stage III
    for module in range(4):
        for block in range(4):
            x1 = residual_block(x1, 32)
            x2 = residual_block(x2, 64)
            x3 = residual_block(x3, 128)
        x1, x2, x3 = fuse([x1, x2, x3])
    x4 = transition_block(x3, 2)

    # stage IV
    for module in range(3):
        for block in range(4):
            x1 = residual_block(x1, 32)
            x2 = residual_block(x2, 64)
            x3 = residual_block(x3, 128)
            x4 = residual_block(x4, 256)
        x1, x2, x3, x4 = fuse([x1, x2, x3, x4])

    # head
    x2 = UpSampling2D(size=(2, 2))(x2)
    x3 = UpSampling2D(size=(4, 4))(x3)
    x4 = UpSampling2D(size=(8, 8))(x4)
    x = Concatenate()([x1, x2, x3, x4])
    x = Conv2D(480, 1)(x)
    x = BatchNormalization(epsilon=1.001e-5)(x)
    x = Activation('relu')(x)
    x = Conv2D(num_keypoints, 1)(x)

    # extra
    x = BatchNormalization(epsilon=1.001e-5)(x)
    x = Activation('relu')(x)
    x = UpSampling2D(size=(4, 4), interpolation='bilinear')(x)
    x = Permute([3, 1, 2])(x)
    x = Reshape([num_keypoints, input_shape[0] * input_shape[1]])(x)
    x = Activation('softmax')(x)
    x = Reshape([num_keypoints, input_shape[0], input_shape[1]])(x)
    outputs = ExpectedValue2D(name='keypoints')(x)
    model = Model(inputs, outputs, name='hrnet-residual')
    return model
def DilatedNet(img_height,
               img_width,
               nclasses,
               use_ctx_module=False,
               bn=False):
    print('. . . . .Building DilatedNet. . . . .')

    def bilinear_upsample(image_tensor):
        upsampled = tf.image.resize_bilinear(image_tensor,
                                             size=(img_height, img_width))
        return upsampled

    def conv_block(conv_layers,
                   tensor,
                   nfilters,
                   size=3,
                   name='',
                   padding='same',
                   dilation_rate=1,
                   pool=False):
        if dilation_rate == 1:
            conv_type = 'conv'
        else:
            conv_type = 'dilated_conv'
        for i in range(conv_layers):
            tensor = Conv2D(nfilters,
                            size,
                            padding=padding,
                            use_bias=False,
                            dilation_rate=dilation_rate,
                            name=f'block{name}_{conv_type}{i+1}')(tensor)
            if bn:
                tensor = BatchNormalization(
                    name=f'block{name}_bn{i+1}')(tensor)
            tensor = Activation('relu', name=f'block{name}_relu{i+1}')(tensor)
        if pool:
            tensor = MaxPooling2D(2, name=f'block{name}_pool')(tensor)
        return tensor

    nfilters = 64
    img_input = Input(shape=(img_height, img_width, 3))
    x = conv_block(conv_layers=2,
                   tensor=img_input,
                   nfilters=nfilters * 1,
                   size=3,
                   pool=True,
                   name=1)
    x = conv_block(conv_layers=2,
                   tensor=x,
                   nfilters=nfilters * 2,
                   size=3,
                   pool=True,
                   name=2)
    x = conv_block(conv_layers=3,
                   tensor=x,
                   nfilters=nfilters * 4,
                   size=3,
                   pool=True,
                   name=3)
    x = conv_block(conv_layers=3,
                   tensor=x,
                   nfilters=nfilters * 8,
                   size=3,
                   name=4)
    x = conv_block(conv_layers=3,
                   tensor=x,
                   nfilters=nfilters * 8,
                   size=3,
                   dilation_rate=2,
                   name=5)
    x = conv_block(conv_layers=1,
                   tensor=x,
                   nfilters=nfilters * 64,
                   size=7,
                   dilation_rate=4,
                   name='_FCN1')
    x = Dropout(0.5)(x)
    x = conv_block(conv_layers=1,
                   tensor=x,
                   nfilters=nfilters * 64,
                   size=1,
                   name='_FCN2')
    x = Dropout(0.5)(x)
    x = Conv2D(filters=nclasses,
               kernel_size=1,
               padding='same',
               name=f'frontend_output')(x)
    if use_ctx_module:
        x = conv_block(conv_layers=2,
                       tensor=x,
                       nfilters=nclasses * 2,
                       size=3,
                       name='_ctx1')
        x = conv_block(conv_layers=1,
                       tensor=x,
                       nfilters=nclasses * 4,
                       size=3,
                       name='_ctx2',
                       dilation_rate=2)
        x = conv_block(conv_layers=1,
                       tensor=x,
                       nfilters=nclasses * 8,
                       size=3,
                       name='_ctx3',
                       dilation_rate=4)
        x = conv_block(conv_layers=1,
                       tensor=x,
                       nfilters=nclasses * 16,
                       size=3,
                       name='_ctx4',
                       dilation_rate=8)
        x = conv_block(conv_layers=1,
                       tensor=x,
                       nfilters=nclasses * 32,
                       size=3,
                       name='_ctx5',
                       dilation_rate=16)
        x = conv_block(conv_layers=1,
                       tensor=x,
                       nfilters=nclasses * 32,
                       size=3,
                       name='_ctx7')
        x = Conv2D(filters=nclasses,
                   kernel_size=1,
                   padding='same',
                   name=f'ctx_output')(x)
    x = Lambda(bilinear_upsample, name='bilinear_upsample')(x)
    x = Reshape((img_height * img_width, nclasses))(x)
    x = Activation('softmax', name='final_softmax')(x)

    model = Model(inputs=img_input, outputs=x, name='DilatedNet')
    print('. . . . .Building network successful. . . . .')
    return model
Example #8
0
def resnet_v1_eembc(input_shape=[32, 32, 3],
                    num_classes=10,
                    num_filters=[16, 32, 64],
                    kernel_sizes=[3, 1],
                    strides=[1, 2],
                    l1p=1e-4,
                    l2p=0):

    # Input layer, change kernel size to 7x7 and strides to 2 for an official resnet
    inputs = Input(shape=input_shape)
    x = Conv2D(num_filters[0],
               kernel_size=kernel_sizes[0],
               strides=strides[0],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(inputs)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    # First stack
    # Weight layers
    y = Conv2D(num_filters[0],
               kernel_size=kernel_sizes[0],
               strides=strides[0],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = Conv2D(num_filters[0],
               kernel_size=kernel_sizes[0],
               strides=strides[0],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
    y = BatchNormalization()(y)

    # Overall residual, connect weight layer and identity paths
    x = Add()([x, y])
    x = Activation('relu')(x)

    # Second stack
    # Weight layers
    y = Conv2D(num_filters[1],
               kernel_size=kernel_sizes[0],
               strides=strides[1],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = Conv2D(num_filters[1],
               kernel_size=kernel_sizes[0],
               strides=strides[0],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
    y = BatchNormalization()(y)

    # Adjust for change in dimension due to stride in identity
    x = Conv2D(num_filters[1],
               kernel_size=kernel_sizes[1],
               strides=strides[1],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)

    # Overall residual, connect weight layer and identity paths
    x = Add()([x, y])
    x = Activation('relu')(x)

    # Third stack
    # Weight layers
    y = Conv2D(num_filters[2],
               kernel_size=kernel_sizes[0],
               strides=strides[1],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)
    y = BatchNormalization()(y)
    y = Activation('relu')(y)
    y = Conv2D(num_filters[2],
               kernel_size=kernel_sizes[0],
               strides=strides[0],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(y)
    y = BatchNormalization()(y)

    # Adjust for change in dimension due to stride in identity
    x = Conv2D(num_filters[2],
               kernel_size=kernel_sizes[1],
               strides=strides[1],
               padding='same',
               kernel_initializer='he_normal',
               kernel_regularizer=l1_l2(l1=l1p, l2=l2p))(x)

    # Overall residual, connect weight layer and identity paths
    x = Add()([x, y])
    x = Activation('relu')(x)

    # Final classification layer.
    pool_size = int(np.amin(x.shape[1:3]))
    x = AveragePooling2D(pool_size=pool_size)(x)
    y = Flatten()(x)
    outputs = Dense(num_classes,
                    activation='softmax',
                    kernel_initializer='he_normal')(y)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model
def fully_convolutional_resnet50(
    input_shape,
    num_classes=1000,
    pretrained_resnet=True,
    use_bias=True,
):
    # init input layer
    img_input = Input(shape=input_shape)

    # define basic model pipeline
    x = ZeroPadding2D(padding=((3, 3), (3, 3)), name="conv1_pad")(img_input)
    x = Conv2D(64, 7, strides=2, use_bias=use_bias, name="conv1_conv")(x)
    x = BatchNormalization(axis=3, epsilon=1.001e-5, name="conv1_bn")(x)
    x = Activation("relu", name="conv1_relu")(x)

    x = ZeroPadding2D(padding=((1, 1), (1, 1)), name="pool1_pad")(x)
    x = MaxPooling2D(3, strides=2, name="pool1_pool")(x)

    # the sequence of stacked residual blocks
    x = stack1(x, 64, 3, stride1=1, name="conv2")
    x = stack1(x, 128, 4, name="conv3")
    x = stack1(x, 256, 6, name="conv4")
    x = stack1(x, 512, 3, name="conv5")

    # add avg pooling layer after feature extraction layers
    x = AveragePooling2D(pool_size=7)(x)

    # add final convolutional layer
    conv_layer_final = Conv2D(
        filters=num_classes,
        kernel_size=1,
        use_bias=use_bias,
        name="last_conv",
    )(x)

    # configure fully convolutional ResNet50 model
    model = training.Model(img_input, x)

    # load model weights
    if pretrained_resnet:
        model_name = "resnet50"
        # configure full file name
        file_name = model_name + "_weights_tf_dim_ordering_tf_kernels_notop.h5"
        # get the file hash from TF WEIGHTS_HASHES
        file_hash = WEIGHTS_HASHES[model_name][1]
        weights_path = data_utils.get_file(
            file_name,
            BASE_WEIGHTS_PATH + file_name,
            cache_subdir="models",
            file_hash=file_hash,
        )

        model.load_weights(weights_path)

    # form final model
    model = training.Model(inputs=model.input, outputs=[conv_layer_final])

    if pretrained_resnet:
        # get model with the dense layer for further FC weights extraction
        resnet50_extractor = ResNet50(
            include_top=True,
            weights="imagenet",
            classes=num_classes,
        )
        # set ResNet50 FC-layer weights to final convolutional layer
        set_conv_weights(model=model, feature_extractor=resnet50_extractor)
    return model
Example #10
0
def build_model(img_shape: Tuple[int, int, int], num_classes: int,
                optimizer: tf.keras.optimizers.Optimizer, learning_rate: float,
                filter_block1: int, kernel_size_block1: int,
                filter_block2: int, kernel_size_block2: int,
                filter_block3: int, kernel_size_block3: int,
                dense_layer_size: int,
                kernel_initializer: tf.keras.initializers.Initializer,
                activation_cls: tf.keras.layers.Activation,
                dropout_rate: float, use_batch_normalization: bool,
                use_dense: bool, use_global_pooling: bool) -> Model:
    input_img = Input(shape=img_shape)

    x = Conv2D(filters=filter_block1,
               kernel_size=kernel_size_block1,
               padding="same",
               kernel_initializer=kernel_initializer,
               name="heatmap1")(input_img)
    if use_batch_normalization:
        x = BatchNormalization()(x)
    x = activation_cls(x)
    x = Conv2D(filters=filter_block1,
               kernel_size=kernel_size_block1,
               padding="same",
               kernel_initializer=kernel_initializer)(x)
    if use_batch_normalization:
        x = BatchNormalization()(x)
    if dropout_rate:
        x = Dropout(rate=dropout_rate)(x)
    x = activation_cls(x)
    x = MaxPool2D()(x)

    x = Conv2D(filters=filter_block2,
               kernel_size=kernel_size_block2,
               padding="same",
               kernel_initializer=kernel_initializer)(x)
    if use_batch_normalization:
        x = BatchNormalization()(x)
    x = activation_cls(x)
    x = Conv2D(filters=filter_block2,
               kernel_size=kernel_size_block2,
               padding="same",
               kernel_initializer=kernel_initializer)(x)
    if use_batch_normalization:
        x = BatchNormalization()(x)
    if dropout_rate:
        x = Dropout(rate=dropout_rate)(x)
    x = activation_cls(x)
    x = MaxPool2D()(x)

    x = Conv2D(filters=filter_block3,
               kernel_size=kernel_size_block3,
               padding="same",
               kernel_initializer=kernel_initializer)(x)
    if use_batch_normalization:
        x = BatchNormalization()(x)
    x = activation_cls(x)
    x = Conv2D(filters=filter_block3,
               kernel_size=kernel_size_block3,
               padding="same",
               kernel_initializer=kernel_initializer)(x)
    if use_batch_normalization:
        x = BatchNormalization()(x)
    if dropout_rate:
        x = Dropout(rate=dropout_rate)(x)
    x = activation_cls(x)
    x = MaxPool2D()(x)

    if use_global_pooling:
        x = GlobalAveragePooling2D()(x)
    else:
        x = Flatten()(x)
    if use_dense:
        x = Dense(units=dense_layer_size,
                  kernel_initializer=kernel_initializer)(x)
        if use_batch_normalization:
            x = BatchNormalization()(x)
        x = activation_cls(x)
    x = Dense(units=num_classes, kernel_initializer=kernel_initializer)(x)
    y_pred = Activation("softmax")(x)

    model = Model(inputs=[input_img], outputs=[y_pred])

    opt = optimizer(learning_rate=learning_rate)

    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    return model
Example #11
0
def _bn_relu(input):
    """Helper to build a BN -> relu block
    """
    norm = BatchNormalization(axis=CHANNEL_AXIS)(input)
    return Activation("relu")(norm)
Example #12
0
def DeepSTN(
    H=32,
    W=32,
    channel=2,  #H-map_height W-map_width channel-map_channel
    c=3,
    p=1,
    t=1,  #c-closeness p-period t-trend
    pre_F=64,
    conv_F=64,
    R_N=2,  #pre_F-prepare_conv_featrue conv_F-resnet_conv_featrue R_N-resnet_number
    is_plus=True,  #use ResPlus or mornal convolution
    is_plus_efficient=False,  #use the efficient version of ResPlus
    plus=8,
    rate=2,  #rate-pooling_rate
    is_pt=True,  #use PoI and Time or not
    P_N=9,
    T_F=31,
    PT_F=9,
    T=24,  #P_N-poi_number T_F-time_feature PT_F-poi_time_feature T-T_times/day 
    drop=0,
    is_summary=True,  #show detail
    lr=0.0002,
    kernel1=1,  #kernel1 decides whether early-fusion uses conv_unit0 or conv_unit1, 1 recommended
    isPT_F=1
):  #isPT_F decides whether PT_model uses one more Conv after multiplying PoI and Time, 1 recommended

    all_channel = channel * (c + p + t)

    cut0 = int(0)
    cut1 = int(cut0 + channel * c)
    cut2 = int(cut1 + channel * p)
    cut3 = int(cut2 + channel * t)

    cpt_input = Input(shape=(H, W, all_channel))

    c_input = Lambda(cpt_slice, arguments={'h1': cut0, 'h2': cut1})(cpt_input)
    p_input = Lambda(cpt_slice, arguments={'h1': cut1, 'h2': cut2})(cpt_input)
    t_input = Lambda(cpt_slice, arguments={'h1': cut2, 'h2': cut3})(cpt_input)
    c_out1 = Conv2D(filters=pre_F, kernel_size=(1, 1), padding="same")(c_input)
    p_out1 = Conv2D(filters=pre_F, kernel_size=(1, 1), padding="same")(p_input)
    t_out1 = Conv2D(filters=pre_F, kernel_size=(1, 1), padding="same")(t_input)
    # print(t_out1.shape)
    if is_pt:
        poi_in = Input(shape=(P_N, H, W))
        # T_times/day + 7days/week
        time_in = Input(shape=(T + 7, H, W))

        PT_model = PT_trans('PT_trans', P_N, PT_F, T, T_F, H, W, isPT_F)

        poi_time = PT_model([poi_in, time_in])

        cpt_con1 = Concatenate(axis=-1)([c_out1, p_out1, t_out1, poi_time])
        # print("="*10)
        # print(c_out1.shape,p_out1.shape,t_out1.shape,poi_time.shape)
        # print(cpt_con1.shape)
        if kernel1:
            cpt = conv_unit1(pre_F * 3 + PT_F * isPT_F + P_N * (not isPT_F),
                             conv_F, drop, H, W)(cpt_con1)
        else:
            cpt = conv_unit0(pre_F * 3 + PT_F * isPT_F + P_N * (not isPT_F),
                             conv_F, drop, H, W)(cpt_con1)

    else:
        cpt_con1 = Concatenate(axis=1)([c_out1, p_out1, t_out1])
        if kernel1:
            cpt = conv_unit1(pre_F * 3, conv_F, drop, H, W)(cpt_con1)
        else:
            cpt = conv_unit0(pre_F * 3, conv_F, drop, H, W)(cpt_con1)

    if is_plus:
        if is_plus_efficient:
            for i in range(R_N):
                cpt = Res_plus_E('Res_plus_' + str(i + 1), conv_F, plus, rate,
                                 drop, H, W)(cpt)
        else:
            for i in range(R_N):
                cpt = Res_plus('Res_plus_' + str(i + 1), conv_F, plus, rate,
                               drop, H, W)(cpt)

    else:
        for i in range(R_N):
            cpt = Res_normal('Res_normal_' + str(i + 1), conv_F, drop, H,
                             W)(cpt)

    cpt_conv2 = Activation('relu')(cpt)
    cpt_out2 = BatchNormalization()(cpt_conv2)
    cpt_conv1 = Dropout(drop)(cpt_out2)
    cpt_conv1 = Conv2D(filters=channel, kernel_size=(1, 1),
                       padding="same")(cpt_conv1)
    cpt_out1 = Activation('tanh')(cpt_conv1)

    if is_pt:
        DeepSTN_model = Model(inputs=[cpt_input, poi_in, time_in],
                              outputs=cpt_out1)
    else:
        DeepSTN_model = Model(inputs=cpt_input, outputs=cpt_out1)

    DeepSTN_model.compile(loss='mse', optimizer='Adam', metrics=['mae'])

    DeepSTN_model.summary()

    return DeepSTN_model
def create_network(n_commands,
                   n_value1,
                   n_durations,
                   embed_size=100,
                   rnn_units=256,
                   use_attention=False):
    """ create the structure of the neural network """

    commands_in = Input(shape=(None, ), name="commands_channels_in")
    value1_in = Input(shape=(None, ), name="values1_in")
    durations_in = Input(shape=(None, ), name="durations_in")

    x1 = Embedding(n_commands, embed_size)(commands_in)
    x2 = Embedding(n_value1, embed_size)(value1_in)
    x3 = Embedding(n_durations, embed_size)(durations_in)

    x = Concatenate()([x1, x2, x3])

    x = LSTM(rnn_units, return_sequences=True)(x)
    #x = Dropout(0.2)(x)

    if use_attention:

        x = LSTM(rnn_units, return_sequences=True)(x)
        #x = Dropout(0.2)(x)

        e = Dense(1, activation='tanh')(x)
        e = Reshape([-1])(e)
        alpha = Activation('softmax')(e)

        alpha_repeated = Permute([2, 1])(RepeatVector(rnn_units)(alpha))

        c = Multiply()([x, alpha_repeated])
        c = Lambda(lambda xin: K.sum(xin, axis=1),
                   output_shape=(rnn_units, ))(c)

    else:
        c = LSTM(rnn_units)(x)
        #c = Dropout(0.2)(c)

    commands_out = Dense(n_commands,
                         activation='softmax',
                         name='commands_channels_out')(c)
    value1_out = Dense(n_value1, activation='softmax', name='values1_out')(c)
    durations_out = Dense(n_durations,
                          activation='softmax',
                          name='durations_out')(c)

    model = Model([commands_in, value1_in, durations_in],
                  [commands_out, value1_out, durations_out])

    if use_attention:
        att_model = Model([commands_in, value1_in, durations_in], alpha)
    else:
        att_model = None

    opti = RMSprop(lr=0.005)
    model.compile(loss=[
        'categorical_crossentropy', 'categorical_crossentropy',
        'categorical_crossentropy'
    ],
                  optimizer=opti)

    return model, att_model
Example #14
0
def resnet99(band, ncla1):
    input1 = Input(shape=(9, 9, band))

    # define network
    conv0x = Conv2D(32,
                    kernel_size=(3, 3),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv0 = Conv2D(32,
                   kernel_size=(3, 3),
                   padding='valid',
                   kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    bn11 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv11 = Conv2D(64,
                    kernel_size=(3, 3),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv12 = Conv2D(64,
                    kernel_size=(3, 3),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    bn21 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv21 = Conv2D(64,
                    kernel_size=(3, 3),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv22 = Conv2D(64,
                    kernel_size=(3, 3),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    fc1 = Dense(ncla1,
                activation='softmax',
                name='output1',
                kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    # x1
    x1 = conv0(input1)
    x1x = conv0x(input1)
    #    x1 = MaxPooling2D(pool_size=(2,2))(x1)
    #    x1x = MaxPooling2D(pool_size=(2,2))(x1x)
    x1 = concatenate([x1, x1x], axis=-1)
    x11 = bn11(x1)
    x11 = Activation('relu')(x11)
    x11 = conv11(x11)
    x11 = Activation('relu')(x11)
    x11 = conv12(x11)
    x1 = Add()([x1, x11])

    #    x11 = bn21(x1)
    #    x11 = Activation('relu')(x11)
    #    x11 = conv21(x11)
    #    x11 = Activation('relu')(x11)
    #    x11 = conv22(x11)
    #    x1 = Add()([x1,x11])

    x1 = Flatten()(x1)
    pre1 = fc1(x1)

    model1 = Model(inputs=input1, outputs=pre1)
    return model1
valid_set = test_datagen.flow_from_directory('images/validation',
                                            target_size = (48,48),
                                            batch_size = 32,
                                            color_mode='grayscale',
                                            class_mode = 'categorical')

# number of possible label values
n = 7
#print(dir(trainig_set))

# Initialising the CNN
model = Sequential()

# 1 - Convolution
model.add(Conv2D(32,(3,3), padding='same', input_shape=(48, 48,1)))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

# 2nd Convolution layer
model.add(Conv2D(64,(5,5), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

# 3rd Convolution layer
model.add(Conv2D(128,(3,3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
save_dir = os.path.join(os.getcwd(), "..", "saved_models")
save_dir = os.path.abspath(save_dir)
model_name = "dropout_rate_025.h5"
print(model_name)

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train / 255
x_test = x_test / 255
y_train = keras.utils.to_categorical(y_train, classes)
y_test = keras.utils.to_categorical(y_test, classes)

model = Sequential()
model.add(Conv2D(32, (3, 3), padding="SAME", input_shape=x_train.shape[1:]))
model.add(Activation("relu"))
model.add(Conv2D(32, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(dropout_rate))

model.add(Conv2D(64, (3, 3), padding="SAME"))
model.add(Activation("relu"))
model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(dropout_rate))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation("relu"))
Example #17
0
def HRNetDense(input_shape=(128, 128, 3), num_keypoints=20, growth_rate=4):
    # stem
    inputs = Input(shape=input_shape)
    x1 = stem(inputs, 64)
    x1 = Conv2D(64 * 4, 1, padding='same', use_bias=False)(x1)
    x1 = BatchNormalization()(x1)
    for block in range(4):
        x1 = bottleneck(x1)

    # stage I
    x1 = Conv2D(32, 3, padding='same', use_bias=False)(x1)
    x1 = BatchNormalization()(x1)
    x1 = Activation('relu')(x1)
    x2 = transition_block(x1, 2)
    print('stage 1', x1.shape, x2.shape)

    # stage II
    x1 = dense_block(x1, 4, growth_rate)
    x2 = dense_block(x2, 4, growth_rate)
    x1, x2 = fuse([x1, x2])
    x3 = transition_block(x2, 0.5)
    print('stage 2', x1.shape, x2.shape, x3.shape)

    # stage III
    x1 = dense_block(x1, 4, growth_rate)
    x2 = dense_block(x2, 4, growth_rate)
    x3 = dense_block(x3, 4, growth_rate)
    x1, x2, x3 = fuse([x1, x2, x3])
    x4 = transition_block(x3, 0.5)
    print('stage 3', x1.shape, x2.shape, x3.shape, x4.shape)

    # stage IV
    x1 = dense_block(x1, 3, growth_rate)
    x2 = dense_block(x2, 3, growth_rate)
    x3 = dense_block(x3, 3, growth_rate)
    x4 = dense_block(x4, 3, growth_rate)
    x1, x2, x3, x4 = fuse([x1, x2, x3, x4])
    print('stage 4', x1.shape, x2.shape, x3.shape, x4.shape)

    x2 = UpSampling2D(size=(2, 2))(x2)
    x3 = UpSampling2D(size=(4, 4))(x3)
    x4 = UpSampling2D(size=(8, 8))(x4)
    x = Concatenate()([x1, x2, x3, x4])

    # head
    x = Conv2D(480, 1)(x)
    x = BatchNormalization(epsilon=1.001e-5)(x)
    x = Activation('relu')(x)
    x = Conv2D(num_keypoints, 1)(x)

    # extra
    x = BatchNormalization(epsilon=1.001e-5)(x)
    x = Activation('relu')(x)
    x = UpSampling2D(size=(4, 4), interpolation='bilinear')(x)
    x = Permute([3, 1, 2])(x)
    x = Reshape([num_keypoints, input_shape[0] * input_shape[1]])(x)
    x = Activation('softmax')(x)
    x = Reshape([num_keypoints, input_shape[0], input_shape[1]])(x)
    outputs = ExpectedValue2D(name='expected_uv')(x)
    model = Model(inputs, outputs, name='hrnet-dense')
    return model
def Make_SingleNode_Model(
    input_shape,
    n_contr=2,  #number of feature to contract in each site
    activation=None,  #activcation function tu use
    use_batch_norm=False,  #use (or not) batch normalization
    bond_dim=10,  #boind dimension of the layers
    verbose=False,  #verbosity, print the inital configuration
    use_reg=True  #use (or not) kernel regularization
):

    KER_REG_L2 = 1.0e-4  #layer parameter regualrization
    n_layers = int(math.log(input_shape[0],
                            n_contr))  #number of layers to create

    if verbose:
        print("input_shape", input_shape)
        print("n_contr", n_contr)
        print("n_layers", n_layers)
    tn_model = Sequential()

    #if the batch normalization is required the architecture has to be created step by step
    if use_batch_norm:
        #first layer, input shape is required and parameter
        tn_model.add(
            TTN_SingleNode(
                bond_dim=bond_dim,  #bond dimension of the weights
                n_contraction=
                n_contr,  #number of feature to contract to each weight tensor
                input_shape=input_shape,
                kernel_regularizer=regularizers.l2(KER_REG_L2)
                if use_reg else None  #use regularization if specified
            ))
        #add batch normalization after the layer but before the activation
        tn_model.add(
            BatchNormalization(epsilon=1e-06, momentum=0.9, weights=None))
        #if required use activation function
        if activation is not None:
            tn_model.add(Activation(activation))

        #intermediate layers, same as before but withoput specifing input shape
        for _ in range(n_layers - 2):
            tn_model.add(
                TTN_SingleNode(bond_dim=bond_dim,
                               n_contraction=n_contr,
                               kernel_regularizer=regularizers.l2(KER_REG_L2)
                               if use_reg else None))
            tn_model.add(
                BatchNormalization(epsilon=1e-06, momentum=0.9, weights=None))
            if activation is not None:
                tn_model.add(Activation(activation))

        #last layer with bond dimension 1
        tn_model.add(
            TTN_SingleNode(
                bond_dim=1,  #bond dimension must be one to obtain a single value
                use_bias=True,
                n_contraction=n_contr,
                input_shape=input_shape,
                kernel_regularizer=regularizers.l2(KER_REG_L2)
                if use_reg else None))
        tn_model.add(
            BatchNormalization(epsilon=1e-06, momentum=0.9, weights=None))
        #activation must be used to interpret results as a probability
        tn_model.add(Activation('sigmoid'))

    #without batch normalization the activation can be directly included inside the model creation
    else:
        #first layer
        tn_model.add(
            TTN_SingleNode(
                bond_dim=bond_dim,  #bond dimension
                activation=activation,  #activation function
                input_shape=input_shape,  #input shape
                n_contraction=n_contr,  #contraction per site
                kernel_regularizer=regularizers.l2(KER_REG_L2)
                if use_reg else None  #regularization
            ))
        #intermediate layers, same as previous layer but without input shape required
        for _ in range(n_layers - 2):
            tn_model.add(
                TTN_SingleNode(bond_dim=bond_dim,
                               activation=activation,
                               n_contraction=n_contr,
                               kernel_regularizer=regularizers.l2(KER_REG_L2)
                               if use_reg else None))
        #last layer, activation imposed to sigmoid to interpret results as probabilities
        tn_model.add(
            TTN_SingleNode(
                bond_dim=1,  #bond dim = 1 to get a single value
                activation=
                'sigmoid',  #sigmoid activation to obtain a probability
                n_contraction=n_contr,
                kernel_regularizer=regularizers.l2(KER_REG_L2)
                if use_reg else None))
    return tn_model
Example #19
0
def transition_block(x, alpha):
    filters = int(K.int_shape(x)[-1] * alpha)
    x = Conv2D(filters, 1, strides=2, use_bias=False)(x)
    x = BatchNormalization(epsilon=1.001e-5)(x)
    x = Activation('relu')(x)
    return x
Example #20
0
def build_model(image_size,
                n_classes,
                mode='training',
                l2_regularization=0.0,
                min_scale=0.1,
                max_scale=0.9,
                scales=None,
                aspect_ratios_global=[0.5, 1.0, 2.0],
                aspect_ratios_per_layer=None,
                two_boxes_for_ar1=True,
                steps=None,
                offsets=None,
                clip_boxes=False,
                variances=[1.0, 1.0, 1.0, 1.0],
                coords='centroids',
                normalize_coords=False,
                subtract_mean=None,
                divide_by_stddev=None,
                swap_channels=False,
                confidence_thresh=0.01,
                iou_threshold=0.45,
                top_k=200,
                nms_max_output_size=400,
                return_predictor_sizes=False):
    '''
    Build a Keras model with SSD architecture, see references.

    The model consists of convolutional feature layers and a number of convolutional
    predictor layers that take their input from different feature layers.
    The model is fully convolutional.

    The implementation found here is a smaller version of the original architecture
    used in the paper (where the base network consists of a modified VGG-16 extended
    by a few convolutional feature layers), but of course it could easily be changed to
    an arbitrarily large SSD architecture by following the general design pattern used here.
    This implementation has 7 convolutional layers and 4 convolutional predictor
    layers that take their input from layers 4, 5, 6, and 7, respectively.

    Most of the arguments that this function takes are only needed for the anchor
    box layers. In case you're training the network, the parameters passed here must
    be the same as the ones used to set up `SSDBoxEncoder`. In case you're loading
    trained weights, the parameters passed here must be the same as the ones used
    to produce the trained weights.

    Some of these arguments are explained in more detail in the documentation of the
    `SSDBoxEncoder` class.

    Note: Requires Keras v2.0 or later. Training currently works only with the
    TensorFlow backend (v1.0 or later).

    Arguments:
        image_size (tuple): The input image size in the format `(height, width, channels)`.
        n_classes (int): The number of positive classes, e.g. 20 for Pascal VOC, 80 for MS COCO.
        mode (str, optional): One of 'training', 'inference' and 'inference_fast'. In 'training' mode,
            the model outputs the raw prediction tensor, while in 'inference' and 'inference_fast' modes,
            the raw predictions are decoded into absolute coordinates and filtered via confidence thresholding,
            non-maximum suppression, and top-k filtering. The difference between latter two modes is that
            'inference' follows the exact procedure of the original Caffe implementation, while
            'inference_fast' uses a faster prediction decoding procedure.
        l2_regularization (float, optional): The L2-regularization rate. Applies to all convolutional layers.
        min_scale (float, optional): The smallest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images.
        max_scale (float, optional): The largest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images. All scaling factors between the smallest and the
            largest will be linearly interpolated. Note that the second to last of the linearly interpolated
            scaling factors will actually be the scaling factor for the last predictor layer, while the last
            scaling factor is used for the second box for aspect ratio 1 in the last predictor layer
            if `two_boxes_for_ar1` is `True`.
        scales (list, optional): A list of floats containing scaling factors per convolutional predictor layer.
            This list must be one element longer than the number of predictor layers. The first `k` elements are the
            scaling factors for the `k` predictor layers, while the last element is used for the second box
            for aspect ratio 1 in the last predictor layer if `two_boxes_for_ar1` is `True`. This additional
            last scaling factor must be passed either way, even if it is not being used. If a list is passed,
            this argument overrides `min_scale` and `max_scale`. All scaling factors must be greater than zero.
        aspect_ratios_global (list, optional): The list of aspect ratios for which anchor boxes are to be
            generated. This list is valid for all predictor layers. The original implementation uses more aspect ratios
            for some predictor layers and fewer for others. If you want to do that, too, then use the next argument instead.
        aspect_ratios_per_layer (list, optional): A list containing one aspect ratio list for each predictor layer.
            This allows you to set the aspect ratios for each predictor layer individually. If a list is passed,
            it overrides `aspect_ratios_global`.
        two_boxes_for_ar1 (bool, optional): Only relevant for aspect ratio lists that contain 1. Will be ignored otherwise.
            If `True`, two anchor boxes will be generated for aspect ratio 1. The first will be generated
            using the scaling factor for the respective layer, the second one will be generated using
            geometric mean of said scaling factor and next bigger scaling factor.
        steps (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be
            either ints/floats or tuples of two ints/floats. These numbers represent for each predictor layer how many
            pixels apart the anchor box center points should be vertically and horizontally along the spatial grid over
            the image. If the list contains ints/floats, then that value will be used for both spatial dimensions.
            If the list contains tuples of two ints/floats, then they represent `(step_height, step_width)`.
            If no steps are provided, then they will be computed such that the anchor box center points will form an
            equidistant grid within the image dimensions.
        offsets (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be
            either floats or tuples of two floats. These numbers represent for each predictor layer how many
            pixels from the top and left boarders of the image the top-most and left-most anchor box center points should be
            as a fraction of `steps`. The last bit is important: The offsets are not absolute pixel values, but fractions
            of the step size specified in the `steps` argument. If the list contains floats, then that value will
            be used for both spatial dimensions. If the list contains tuples of two floats, then they represent
            `(vertical_offset, horizontal_offset)`. If no offsets are provided, then they will default to 0.5 of the step size,
            which is also the recommended setting.
        clip_boxes (bool, optional): If `True`, clips the anchor box coordinates to stay within image boundaries.
        variances (list, optional): A list of 4 floats >0. The anchor box offset for each coordinate will be divided by
            its respective variance value.
        coords (str, optional): The box coordinate format to be used internally by the model (i.e. this is not the input format
            of the ground truth labels). Can be either 'centroids' for the format `(cx, cy, w, h)` (box center coordinates, width,
            and height), 'minmax' for the format `(xmin, xmax, ymin, ymax)`, or 'corners' for the format `(xmin, ymin, xmax, ymax)`.
        normalize_coords (bool, optional): Set to `True` if the model is supposed to use relative instead of absolute coordinates,
            i.e. if the model predicts box coordinates within [0,1] instead of absolute coordinates.
        subtract_mean (array-like, optional): `None` or an array-like object of integers or floating point values
            of any shape that is broadcast-compatible with the image shape. The elements of this array will be
            subtracted from the image pixel intensity values. For example, pass a list of three integers
            to perform per-channel mean normalization for color images.
        divide_by_stddev (array-like, optional): `None` or an array-like object of non-zero integers or
            floating point values of any shape that is broadcast-compatible with the image shape. The image pixel
            intensity values will be divided by the elements of this array. For example, pass a list
            of three integers to perform per-channel standard deviation normalization for color images.
        swap_channels (list, optional): Either `False` or a list of integers representing the desired order in which the input
            image channels should be swapped.
        confidence_thresh (float, optional): A float in [0,1), the minimum classification confidence in a specific
            positive class in order to be considered for the non-maximum suppression stage for the respective class.
            A lower value will result in a larger part of the selection process being done by the non-maximum suppression
            stage, while a larger value will result in a larger part of the selection process happening in the confidence
            thresholding stage.
        iou_threshold (float, optional): A float in [0,1]. All boxes that have a Jaccard similarity of greater than `iou_threshold`
            with a locally maximal box will be removed from the set of predictions for a given class, where 'maximal' refers
            to the box's confidence score.
        top_k (int, optional): The number of highest scoring predictions to be kept for each batch item after the
            non-maximum suppression stage.
        nms_max_output_size (int, optional): The maximal number of predictions that will be left over after the NMS stage.
        return_predictor_sizes (bool, optional): If `True`, this function not only returns the model, but also
            a list containing the spatial dimensions of the predictor layers. This isn't strictly necessary since
            you can always get their sizes easily via the Keras API, but it's convenient and less error-prone
            to get them this way. They are only relevant for training anyway (SSDBoxEncoder needs to know the
            spatial dimensions of the predictor layers), for inference you don't need them.

    Returns:
        model: The Keras SSD model.
        predictor_sizes (optional): A Numpy array containing the `(height, width)` portion
            of the output tensor shape for each convolutional predictor layer. During
            training, the generator function needs this in order to transform
            the ground truth labels into tensors of identical structure as the
            output tensors of the model, which is in turn needed for the cost
            function.

    References:
        https://arxiv.org/abs/1512.02325v5
    '''

    n_predictor_layers = 4 # The number of predictor conv layers in the network
    n_classes += 1 # Account for the background class.
    l2_reg = l2_regularization # Make the internal name shorter.
    img_height, img_width, img_channels = image_size[0], image_size[1], image_size[2]

    ############################################################################
    # Get a few exceptions out of the way.
    ############################################################################

    if aspect_ratios_global is None and aspect_ratios_per_layer is None:
        raise ValueError("`aspect_ratios_global` and `aspect_ratios_per_layer` cannot both be None. At least one needs to be specified.")
    if aspect_ratios_per_layer:
        if len(aspect_ratios_per_layer) != n_predictor_layers:
            raise ValueError("It must be either aspect_ratios_per_layer is None or len(aspect_ratios_per_layer) == {}, but len(aspect_ratios_per_layer) == {}.".format(n_predictor_layers, len(aspect_ratios_per_layer)))

    if (min_scale is None or max_scale is None) and scales is None:
        raise ValueError("Either `min_scale` and `max_scale` or `scales` need to be specified.")
    if scales:
        if len(scales) != n_predictor_layers+1:
            raise ValueError("It must be either scales is None or len(scales) == {}, but len(scales) == {}.".format(n_predictor_layers+1, len(scales)))
    else: # If no explicit list of scaling factors was passed, compute the list of scaling factors from `min_scale` and `max_scale`
        scales = np.linspace(min_scale, max_scale, n_predictor_layers+1)

    if len(variances) != 4: # We need one variance value for each of the four box coordinates
        raise ValueError("4 variance values must be pased, but {} values were received.".format(len(variances)))
    variances = np.array(variances)
    if np.any(variances <= 0):
        raise ValueError("All variances must be >0, but the variances given are {}".format(variances))

    if (not (steps is None)) and (len(steps) != n_predictor_layers):
        raise ValueError("You must provide at least one step value per predictor layer.")

    if (not (offsets is None)) and (len(offsets) != n_predictor_layers):
        raise ValueError("You must provide at least one offset value per predictor layer.")

    ############################################################################
    # Compute the anchor box parameters.
    ############################################################################

    # Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers.
    if aspect_ratios_per_layer:
        aspect_ratios = aspect_ratios_per_layer
    else:
        aspect_ratios = [aspect_ratios_global] * n_predictor_layers

    # Compute the number of boxes to be predicted per cell for each predictor layer.
    # We need this so that we know how many channels the predictor layers need to have.
    if aspect_ratios_per_layer:
        n_boxes = []
        for ar in aspect_ratios_per_layer:
            if (1 in ar) & two_boxes_for_ar1:
                n_boxes.append(len(ar) + 1) # +1 for the second box for aspect ratio 1
            else:
                n_boxes.append(len(ar))
    else: # If only a global aspect ratio list was passed, then the number of boxes is the same for each predictor layer
        if (1 in aspect_ratios_global) & two_boxes_for_ar1:
            n_boxes = len(aspect_ratios_global) + 1
        else:
            n_boxes = len(aspect_ratios_global)
        n_boxes = [n_boxes] * n_predictor_layers

    if steps is None:
        steps = [None] * n_predictor_layers
    if offsets is None:
        offsets = [None] * n_predictor_layers

    ############################################################################
    # Define functions for the Lambda layers below.
    ############################################################################

    def identity_layer(tensor):
        return tensor

    def input_mean_normalization(tensor):
        return tensor - np.array(subtract_mean)

    def input_stddev_normalization(tensor):
        return tensor / np.array(divide_by_stddev)

    #def input_channel_swap(tensor):
    #    if len(swap_channels) == 3:
    #        return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]]], axis=-1)
    #    elif len(swap_channels) == 4:
    #        return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]], tensor[...,swap_channels[3]]], axis=-1)

    ############################################################################
    # Build the network.
    ############################################################################

    x = Input(shape=(img_height, img_width, img_channels))

    # The following identity layer is only needed so that the subsequent lambda layers can be optional.
    x1 = Lambda(identity_layer, output_shape=(img_height, img_width, img_channels), name='identity_layer')(x)
    if not (subtract_mean is None):
        x1 = Lambda(input_mean_normalization, output_shape=(img_height, img_width, img_channels), name='input_mean_normalization')(x1)
    if not (divide_by_stddev is None):
        x1 = Lambda(input_stddev_normalization, output_shape=(img_height, img_width, img_channels), name='input_stddev_normalization')(x1)
    #if swap_channels: #REMOVED FOR TFLITE
    #    x1 = Lambda(input_channel_swap, output_shape=(img_height, img_width, img_channels), name='input_channel_swap')(x1)

    conv1 = Conv2D(32, (5, 5), strides=(2, 2), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv1')(x1)
    conv1 = BatchNormalization(axis=3, momentum=0.99, name='bn1')(conv1) # Tensorflow uses filter format [filter_height, filter_width, in_channels, out_channels], hence axis = 3
    conv1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same', name='pool1')(conv1)


    conv2 = QuantConv2D(48, (3, 3), strides=(1, 1), padding="same", kernel_initializer='glorot_normal', use_bias=False, name='conv2', **kwargs)(conv1)
    conv2 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool2')(conv2)
    conv2 = BatchNormalization(axis=3, momentum=0.99, name='bn2')(conv2)
    

    conv3 = QuantConv2D(64, (3, 3), strides=(1, 1), padding="same", kernel_initializer='glorot_normal', use_bias=False, name='conv3', **kwargs)(conv2)
    #conv3 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(conv3)
    conv3 = BatchNormalization(axis=3, momentum=0.99, name='bn3')(conv3)
    
    #Conv1,2,3 are for feature extraction and downsampling
    #Conv4,5,6,7 are the pre-detection feature maps
    
    conv4 = QuantConv2D(64, (3, 3), strides=(1, 1), padding="same", kernel_initializer='glorot_normal', use_bias=False, name='conv4', **kwargs)(conv3)
    conv4 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool4')(conv4)
    conv4 = BatchNormalization(axis=3, momentum=0.99, name='bn4')(conv4)
    

    conv5 = QuantConv2D(48, (3, 3), strides=(1, 1), padding="same", kernel_initializer='glorot_normal', use_bias=False, name='conv5', **kwargs)(conv4)
    conv5 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(conv5)
    conv5 = BatchNormalization(axis=3, momentum=0.99, name='bn5')(conv5)
    

    conv6 = QuantConv2D(48, (3, 3), strides=(1, 1), padding="same", kernel_initializer='glorot_normal', use_bias=False, name='conv6', **kwargs)(conv5)
    conv6 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool6')(conv6)
    conv6 = BatchNormalization(axis=3, momentum=0.99, name='bn6')(conv6)
    

    conv7 = QuantConv2D(32, (3, 3), strides=(1, 1), padding="same", kernel_initializer='glorot_normal', use_bias=False, name='conv7', **kwargs)(conv6)
    conv7 = BatchNormalization(axis=3, momentum=0.99, name='bn7')(conv7)

    # The next part is to add the convolutional predictor layers on top of the base network
    # that we defined above. Note that I use the term "base network" differently than the paper does.
    # To me, the base network is everything that is not convolutional predictor layers or anchor
    # box layers. In this case we'll have four predictor layers, but of course you could
    # easily rewrite this into an arbitrarily deep base network and add an arbitrary number of
    # predictor layers on top of the base network by simply following the pattern shown here.

    # Build the convolutional predictor layers on top of conv layers 4, 5, 6, and 7.
    # We build two predictor layers on top of each of these layers: One for class prediction (classification), one for box coordinate prediction (localization)
    # We precidt `n_classes` confidence values for each box, hence the `classes` predictors have depth `n_boxes * n_classes`
    # We predict 4 box coordinates for each box, hence the `boxes` predictors have depth `n_boxes * 4`
    # Output shape of `classes`: `(batch, height, width, n_boxes * n_classes)`
    classes4 = Conv2D(n_boxes[0] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes4')(conv4)
    classes5 = Conv2D(n_boxes[1] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes5')(conv5)
    classes6 = Conv2D(n_boxes[2] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes6')(conv6)
    classes7 = Conv2D(n_boxes[3] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes7')(conv7)
    # Output shape of `boxes`: `(batch, height, width, n_boxes * 4)`
    boxes4 = Conv2D(n_boxes[0] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes4')(conv4)
    boxes5 = Conv2D(n_boxes[1] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes5')(conv5)
    boxes6 = Conv2D(n_boxes[2] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes6')(conv6)
    boxes7 = Conv2D(n_boxes[3] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes7')(conv7)

    # Generate the anchor boxes
    # Output shape of `anchors`: `(batch, height, width, n_boxes, 8)`
    anchors4 = AnchorBoxes(img_height, img_width, this_scale=scales[0], next_scale=scales[1], aspect_ratios=aspect_ratios[0],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[0], this_offsets=offsets[0],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors4')(boxes4)
    anchors5 = AnchorBoxes(img_height, img_width, this_scale=scales[1], next_scale=scales[2], aspect_ratios=aspect_ratios[1],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[1], this_offsets=offsets[1],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors5')(boxes5)
    anchors6 = AnchorBoxes(img_height, img_width, this_scale=scales[2], next_scale=scales[3], aspect_ratios=aspect_ratios[2],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[2], this_offsets=offsets[2],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors6')(boxes6)
    anchors7 = AnchorBoxes(img_height, img_width, this_scale=scales[3], next_scale=scales[4], aspect_ratios=aspect_ratios[3],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[3], this_offsets=offsets[3],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors7')(boxes7)

    # Reshape the class predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, n_classes)`
    # We want the classes isolated in the last axis to perform softmax on them
    
    #Shape inference is different for keras.layers and tf.keras.layers (-1), check documentation
    #So I had to manually input the intended shape of the reshape
    
    #cba = classes, boxes, anchors SHAPE: shape will be reused later anyways
    cba_4 = classes4.shape[1]*classes4.shape[2]*n_boxes[0]
    cba_5 = classes5.shape[1]*classes5.shape[2]*n_boxes[1]
    cba_6 = classes6.shape[1]*classes6.shape[2]*n_boxes[2]
    cba_7 = classes7.shape[1]*classes7.shape[2]*n_boxes[3]
    
    classes4_reshaped = Reshape((cba_4, n_classes), name='classes4_reshape')(classes4)
    classes5_reshaped = Reshape((cba_5, n_classes), name='classes5_reshape')(classes5)
    classes6_reshaped = Reshape((cba_6, n_classes), name='classes6_reshape')(classes6)
    classes7_reshaped = Reshape((cba_7, n_classes), name='classes7_reshape')(classes7)
    # Reshape the box coordinate predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, 4)`
    
    #shape for classes_reshaped is SAME with boxes EXCEPT for n_classes and anchors so NO NEED TO RECOMPUTE
    
    # We want the four box coordinates isolated in the last axis to compute the smooth L1 loss
    boxes4_reshaped = Reshape((cba_4, 4), name='boxes4_reshape')(boxes4)
    boxes5_reshaped = Reshape((cba_5, 4), name='boxes5_reshape')(boxes5)
    boxes6_reshaped = Reshape((cba_6, 4), name='boxes6_reshape')(boxes6)
    boxes7_reshaped = Reshape((cba_7, 4), name='boxes7_reshape')(boxes7)
    # Reshape the anchor box tensors, yielding 3D tensors of shape `(batch, height * width * n_boxes, 8)`
    anchors4_reshaped = Reshape((cba_4, 8), name='anchors4_reshape')(anchors4)
    anchors5_reshaped = Reshape((cba_5, 8), name='anchors5_reshape')(anchors5)
    anchors6_reshaped = Reshape((cba_6, 8), name='anchors6_reshape')(anchors6)
    anchors7_reshaped = Reshape((cba_7, 8), name='anchors7_reshape')(anchors7)

    # Concatenate the predictions from the different layers and the assosciated anchor box tensors
    # Axis 0 (batch) and axis 2 (n_classes or 4, respectively) are identical for all layer predictions,
    # so we want to concatenate along axis 1
    # Output shape of `classes_concat`: (batch, n_boxes_total, n_classes)
    classes_concat = Concatenate(axis=1, name='classes_concat')([classes4_reshaped,
                                                                 classes5_reshaped,
                                                                 classes6_reshaped,
                                                                 classes7_reshaped])

    # Output shape of `boxes_concat`: (batch, n_boxes_total, 4)
    boxes_concat = Concatenate(axis=1, name='boxes_concat')([boxes4_reshaped,
                                                             boxes5_reshaped,
                                                             boxes6_reshaped,
                                                             boxes7_reshaped])

    # Output shape of `anchors_concat`: (batch, n_boxes_total, 8)
    anchors_concat = Concatenate(axis=1, name='anchors_concat')([anchors4_reshaped,
                                                                 anchors5_reshaped,
                                                                 anchors6_reshaped,
                                                                 anchors7_reshaped])

    # The box coordinate predictions will go into the loss function just the way they are,
    # but for the class predictions, we'll apply a softmax activation layer first
    classes_softmax = Activation('softmax', name='classes_softmax')(classes_concat)

    # Concatenate the class and box coordinate predictions and the anchors to one large predictions tensor
    # Output shape of `predictions`: (batch, n_boxes_total, n_classes + 4 + 8)
    predictions = Concatenate(axis=2, name='predictions')([classes_softmax, boxes_concat, anchors_concat])

    if mode == 'training':
        model = Model(inputs=x, outputs=predictions)
    elif mode == 'inference':
        decoded_predictions = DecodeDetections(confidence_thresh=confidence_thresh,
                                               iou_threshold=iou_threshold,
                                               top_k=top_k,
                                               nms_max_output_size=nms_max_output_size,
                                               coords=coords,
                                               normalize_coords=normalize_coords,
                                               img_height=img_height,
                                               img_width=img_width,
                                               name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    elif mode == 'inference_fast':
        decoded_predictions = DecodeDetectionsFast(confidence_thresh=confidence_thresh,
                                                   iou_threshold=iou_threshold,
                                                   top_k=top_k,
                                                   nms_max_output_size=nms_max_output_size,
                                                   coords=coords,
                                                   normalize_coords=normalize_coords,
                                                   img_height=img_height,
                                                   img_width=img_width,
                                                   name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    else:
        raise ValueError("`mode` must be one of 'training', 'inference' or 'inference_fast', but received '{}'.".format(mode))

    if return_predictor_sizes:
        # The spatial dimensions are the same for the `classes` and `boxes` predictor layers.
        predictor_sizes = np.array([classes4._keras_shape[1:3],
                                    classes5._keras_shape[1:3],
                                    classes6._keras_shape[1:3],
                                    classes7._keras_shape[1:3]])
        return model, predictor_sizes
    else:
        return model
Example #21
0
y = raw_wine.target
print(X.shape)
print(set(y))

y_hot = to_categorical(y)

X_tn, X_te, y_tn, y_te = train_test_split(X, y_hot, random_state=0)

n_feat = X_tn.shape[1]
n_class = len(set(y))
epo = 30

model = Sequential()
model.add(Dense(20, input_dim=n_feat))  # output_dim, input_dim
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(n_class))  # output_dim
model.add(Activation('softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])  # 이진형 : binary_crossentropy

hist = model.fit(X_tn, y_tn, epochs=epo, batch_size=5)

print(model.evaluate(X_tn, y_tn)[1])
print(model.evaluate(X_te, y_te)[1])

epoch = np.arange(1, epo + 1)
Example #22
0
layer_sizes = [32, 64, 128]
conv_layers = [1, 2, 3]

for conv_layer in conv_layers:
    for layer_size in layer_sizes:
        for dense_layer in dense_layers:
            NAME = "{}--conv-{}-nodes-{}-dense-{}".format(
                conv_layer, layer_size, dense_layer, int(time.time()))
            tensorboard = TensorBoard(log_dir='logs\{}'.format(
                NAME))  # 'tensorboard --logdir=logs'  shows tensorboard
            print(NAME)

            model = Sequential()

            model.add(Conv2D(layer_size, (3, 3), input_shape=X.shape[1:]))
            model.add(Activation("relu"))
            model.add(MaxPooling2D(pool_size=(2, 2)))

            for l in range(conv_layer - 1):
                model.add(Conv2D(layer_size, (3, 3)))
                model.add(Activation("relu"))
                model.add(MaxPooling2D(pool_size=(2, 2)))

            model.add(Flatten(
            ))  # this converts our 3D feature maps to 1D feature vectors

            for l in range(dense_layer):
                model.add(Dense(layer_size))
                model.add(Activation("relu"))

            model.add(Dense(1))
Example #23
0
ENV_NAME = 'CartPole-v0'

# Get the environment and extract the number of actions.
env = gym.make(ENV_NAME)
np.random.seed(123)
env.seed(123)

nb_actions = env.action_space.n
obs_dim = env.observation_space.shape[0]

# Option 1 : Simple model
model = Sequential()
model.add(Flatten(input_shape=(1, ) + env.observation_space.shape))
model.add(Dense(nb_actions))
model.add(Activation('softmax'))

# Option 2: deep network
# model = Sequential()
# model.add(Flatten(input_shape=(1,) + env.observation_space.shape))
# model.add(Dense(16))
# model.add(Activation('relu'))
# model.add(Dense(16))
# model.add(Activation('relu'))
# model.add(Dense(16))
# model.add(Activation('relu'))
# model.add(Dense(nb_actions))
# model.add(Activation('softmax'))

print(model.summary())
#splitting data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.15) #0.15 
#means allocating 15% of the data to be testing data

#TRAIN AND EVALUATE LINEAR REGRESSION MODEL

LinReg_model = LinearRegression()
LinReg_model.fit(x_train, y_train)
accuracy_LinReg = LinReg_model.score(x_train, y_train)
print (accuracy_LinReg)

# USING ARTIFICIAL NEURAL NETWORK

ANN_model = keras.Sequential()
ANN_model.add(Dense(50, input_dim = 7))
ANN_model.add(Activation('relu'))

ANN_model.add(Dense(150))
ANN_model.add(Activation('relu'))
ANN_model.add(Dropout(0.5))

ANN_model.add(Dense(150))
ANN_model.add(Activation('relu'))
ANN_model.add(Dropout(0.5))

ANN_model.add(Dense(50))
ANN_model.add(Activation('linear'))
ANN_model.add(Dense(1))

ANN_model.compile(loss = 'mse', optimizer = 'adam')
ANN_model.summary()
Example #25
0
import pickle
import numpy as np

DATADIR = "/content/drive/My Drive/Google20/"

pickle_in = open(DATADIR + "datasetX.pickle", "rb")
X = pickle.load(pickle_in)
X = X / 255.0

pickle_in = open(DATADIR + "datasety.pickle", "rb")
y = pickle.load(pickle_in)
y = np.array(y)

model = Sequential()
model.add(Conv2D(32, (5, 5), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(5))
model.add(Activation('sigmoid'))

tensorboard = TensorBoard(log_dir=DATADIR +
                          "logs/{}".format("Music Tron 3000"))

opt = Adam()
def mini_XCEPTION(input_shape, num_classes, l2_regularization=0.01):
    regularization = l2(l2_regularization)

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

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

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

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

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

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

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

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

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

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

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

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

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

    x = Conv2D(
        num_classes,
        (3, 3),
        # kernel_regularizer=regularization,
        padding='same')(x)
    x = GlobalAveragePooling2D()(x)
    output = Dense(30)(x)

    model = Model(img_input, output)
    return model
Example #27
0
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)

x_train = x_train / 255.0
x_test = x_test / 255.0

y_train = to_categorical(y_train, NUM_CLASSES)
y_test = to_categorical(y_test, NUM_CLASSES)

print("x_train.shape = {}, y_train.shape = {}".format(x_train.shape, y_train.shape))
print("x_test.shape = {}, y_test.shape = {}".format(x_test.shape, y_test.shape))

inputs = Input(shape=(28, 28, 1), name='input')

x = Conv2D(24, kernel_size=(6, 6), strides=1)(inputs)
x = BatchNormalization(scale=False, beta_initializer=Constant(0.01))(x)
x = Activation('relu')(x)
x = Dropout(rate=0.25)(x)

x = Conv2D(48, kernel_size=(5, 5), strides=2)(x)
x = BatchNormalization(scale=False, beta_initializer=Constant(0.01))(x)
x = Activation('relu')(x)
x = Dropout(rate=0.25)(x)

x = Conv2D(64, kernel_size=(4, 4), strides=2)(x)
x = BatchNormalization(scale=False, beta_initializer=Constant(0.01))(x)
x = Activation('relu')(x)
x = Dropout(rate=0.25)(x)

x = Flatten()(x)
x = Dense(200)(x)
x = BatchNormalization(scale=False, beta_initializer=Constant(0.01))(x)
Example #28
0
print("starting")

import tensorflow as tf

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation

model1 = Sequential([
    Dense(256, input_shape=(24, )),
    Activation('relu'),
    Dense(128),
    Activation('relu'),
    Dense(128),
    Activation('relu'),
    Dense(64),
    Activation('relu'),
    Dense(2),
    Activation('softmax')
])

print(model1.summary())
Example #29
0
def DeepConvNet(nb_classes, Chans = 64, Samples = 256,
                dropoutRate = 0.5):
    """ Keras implementation of the Deep Convolutional Network as described in
    Schirrmeister et. al. (2017), Human Brain Mapping.
    
    This implementation assumes the input is a 2-second EEG signal sampled at 
    128Hz, as opposed to signals sampled at 250Hz as described in the original
    paper. We also perform temporal convolutions of length (1, 5) as opposed
    to (1, 10) due to this sampling rate difference. 
    
    Note that we use the max_norm constraint on all convolutional layers, as 
    well as the classification layer. We also change the defaults for the
    BatchNormalization layer. We used this based on a personal communication 
    with the original authors.
    
                      ours        original paper
    pool_size        1, 2        1, 3
    strides          1, 2        1, 3
    conv filters     1, 5        1, 10
    
    Note that this implementation has not been verified by the original 
    authors. 
    
    """

    # start the model
    input_main   = Input((Chans, Samples, 1))
    block1       = Conv2D(25, (1, 5), 
                                 input_shape=(Chans, Samples, 1),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(input_main)
    block1       = Conv2D(25, (Chans, 1),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block1)
    block1       = BatchNormalization(epsilon=1e-05, momentum=0.1)(block1)
    block1       = Activation('elu')(block1)
    block1       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block1)
    block1       = Dropout(dropoutRate)(block1)
  
    block2       = Conv2D(50, (1, 5),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block1)
    block2       = BatchNormalization(epsilon=1e-05, momentum=0.1)(block2)
    block2       = Activation('elu')(block2)
    block2       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block2)
    block2       = Dropout(dropoutRate)(block2)
    
    block3       = Conv2D(100, (1, 5),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block2)
    block3       = BatchNormalization(epsilon=1e-05, momentum=0.1)(block3)
    block3       = Activation('elu')(block3)
    block3       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block3)
    block3       = Dropout(dropoutRate)(block3)
    
    block4       = Conv2D(200, (1, 5),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block3)
    block4       = BatchNormalization(epsilon=1e-05, momentum=0.1)(block4)
    block4       = Activation('elu')(block4)
    block4       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block4)
    block4       = Dropout(dropoutRate)(block4)
    
    flatten      = Flatten()(block4)
    
    dense        = Dense(nb_classes, kernel_constraint = max_norm(0.5))(flatten)
    softmax      = Activation('softmax')(dense)
    
    return Model(inputs=input_main, outputs=softmax)
Example #30
0
def DBMA(band, imx, ncla1):
    input1 = Input(shape=(imx, imx, band, 1))

    ## spectral branch
    conv11 = Conv3D(24,
                    kernel_size=(1, 1, 7),
                    strides=(1, 1, 2),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    bn12 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv12 = Conv3D(24,
                    kernel_size=(1, 1, 7),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    bn13 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv13 = Conv3D(24,
                    kernel_size=(1, 1, 7),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    bn14 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv14 = Conv3D(24,
                    kernel_size=(1, 1, 7),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    bn15 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv15 = Conv3D(60,
                    kernel_size=(1, 1, 4),
                    strides=(1, 1, 1),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    fc11 = Dense(30,
                 activation=None,
                 kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    fc12 = Dense(60,
                 activation=None,
                 kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    ## spatial branch
    conv21 = Conv3D(24,
                    kernel_size=(1, 1, band),
                    strides=(1, 1, 1),
                    padding='valid',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    bn22 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv22 = Conv3D(12,
                    kernel_size=(3, 3, 1),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    bn23 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv23 = Conv3D(12,
                    kernel_size=(3, 3, 1),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    bn24 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv24 = Conv3D(12,
                    kernel_size=(3, 3, 1),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    bn25 = BatchNormalization(axis=-1,
                              momentum=0.9,
                              epsilon=0.001,
                              center=True,
                              scale=True,
                              beta_initializer='zeros',
                              gamma_initializer='ones',
                              moving_mean_initializer='zeros',
                              moving_variance_initializer='ones')
    conv25 = Conv3D(24,
                    kernel_size=(3, 3, 1),
                    strides=(1, 1, 1),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))
    conv26 = Conv3D(1,
                    activation=None,
                    kernel_size=(3, 3, 2),
                    strides=(1, 1, 2),
                    padding='same',
                    kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    fc = Dense(ncla1,
               activation='softmax',
               name='output1',
               kernel_initializer=RandomNormal(mean=0.0, stddev=0.01))

    # spectral
    x1 = conv11(input1)

    x11 = bn12(x1)
    x11 = Activation('relu')(x11)
    x11 = conv12(x11)

    x12 = concatenate([x1, x11], axis=-1)
    x12 = bn13(x12)
    x12 = Activation('relu')(x12)
    x12 = conv13(x12)

    x13 = concatenate([x1, x11, x12], axis=-1)
    x13 = bn14(x13)
    x13 = Activation('relu')(x13)
    x13 = conv14(x13)

    x14 = concatenate([x1, x11, x12, x13], axis=-1)
    x14 = bn15(x14)
    x14 = Activation('relu')(x14)
    x14 = conv15(x14)

    x1_max = MaxPooling3D(pool_size=(7, 7, 1))(x14)
    x1_avg = AveragePooling3D(pool_size=(7, 7, 1))(x14)

    x1_max = fc11(x1_max)
    x1_max = fc12(x1_max)

    x1_avg = fc11(x1_avg)
    x1_avg = fc12(x1_avg)

    x1 = Add()([x1_max, x1_avg])
    x1 = Activation('sigmoid')(x1)
    x1 = multiply([x1, x14])
    x1 = GlobalAveragePooling3D()(x1)

    # spatial
    x2 = conv21(input1)
    x21 = bn22(x2)
    x21 = Activation('relu')(x21)
    x21 = conv22(x21)

    x22 = concatenate([x2, x21], axis=-1)
    x22 = bn23(x22)
    x22 = Activation('relu')(x22)
    x22 = conv23(x22)

    x23 = concatenate([x2, x21, x22], axis=-1)
    x23 = bn24(x23)
    x23 = Activation('relu')(x23)
    x23 = conv24(x23)

    x24 = concatenate([x2, x21, x22, x23], axis=-1)
    x24 = Reshape(target_shape=(7, 7, 60, 1))(x24)

    x2_max = MaxPooling3D(pool_size=(1, 1, 60))(x24)
    x2_avg = AveragePooling3D(pool_size=(1, 1, 60))(x24)

    x2_max = Reshape(target_shape=(7, 7, 1))(x2_max)
    x2_avg = Reshape(target_shape=(7, 7, 1))(x2_avg)

    x25 = concatenate([x2_max, x2_avg], axis=-1)
    x25 = Reshape(target_shape=(7, 7, 2, 1))(x25)
    x25 = conv26(x25)
    x25 = Activation('sigmoid')(x25)

    x2 = multiply([x24, x25])
    x2 = Reshape(target_shape=(7, 7, 1, 60))(x2)
    x2 = GlobalAveragePooling3D()(x2)

    x = concatenate([x1, x2], axis=-1)
    pre = fc(x)

    model = Model(inputs=input1, outputs=pre)
    return model