Пример #1
0
def model_SCNN_custom_v2_ps3(input_shape, classes):
    kernel_size = (3, 3)
    pool_size = (3, 3)

    model = Sequential()

    model.add(
        SConv2D(filters=32,
                kernel_size=kernel_size,
                padding='SAME',
                activation=tf.nn.relu,
                input_shape=input_shape))
    model.add(
        SConv2D(filters=32,
                kernel_size=kernel_size,
                strides=(2, 2),
                padding='SAME',
                activation=tf.nn.relu))
    model.add(Dropout(rate=0.25))
    model.add(
        SConv2D(filters=64,
                kernel_size=kernel_size,
                padding='SAME',
                activation=tf.nn.relu))
    model.add(SMaxPool2D(pool_size=pool_size, padding='SAME'))
    model.add(Dropout(rate=0.25))
    model.add(Flatten())
    model.add(Dense(units=128, activation=tf.nn.relu))
    model.add(Dropout(rate=0.5))
    model.add(Dense(units=classes, activation=tf.nn.softmax))

    return model
Пример #2
0
def resnet_layer(inputs,
                 num_filters=16,
                 filter_size=1.0,
                 kernel_size=3,
                 strides=1,
                 activation='relu',
                 batch_normalization=True,
                 conv_first=True,
                 mode='baseline'):
    '''2D Convolution-Batch Normalization-Activation stack builder

    # Arguments
        inputs (tensor): input tensor from input image or previous layer
        num_filters (int): Conv2D number of filters
        filter_size (float): filter size factor (convolutional layers)
        kernel_size (int): Conv2D square kernel dimensions
        strides (int): Conv2D square stride dimensions
        activation (string): activation name
        batch_normalization (bool): whether to include batch normalization
        conv_first (bool): conv-bn-activation (True) or
            bn-activation-conv (False)

    # Returns
        x (tensor): tensor as input to the next layer
    '''

    num_filters = int(filter_size * num_filters)

    if mode == 'baseline':
        conv = Conv2D(num_filters,
                      kernel_size=kernel_size,
                      strides=strides,
                      padding='same',
                      kernel_initializer='he_normal',
                      kernel_regularizer=l2(1e-4))
    elif mode == 'S-ResNet':
        conv = SConv2D(filters=num_filters,
                       kernel_size=kernel_size,
                       strides=strides,
                       padding='SAME')
    elif mode == 'H-ResNet':
        conv = HConv2D(filters=num_filters,
                       kernel_size=kernel_size,
                       strides=strides,
                       padding='SAME')

    x = inputs
    if conv_first:
        x = conv(x)
        if batch_normalization:
            x = BatchNormalization()(x)
        if activation is not None:
            x = Activation(activation)(x)
    else:
        if batch_normalization:
            x = BatchNormalization()(x)
        if activation is not None:
            x = Activation(activation)(x)
        x = conv(x)
    return x
Пример #3
0
def model_CNN_SConv2D(input_shape, classes, kernel_size, pool_size):
    model = Sequential()

    model.add(
        SConv2D(filters=32,
                kernel_size=kernel_size,
                padding='SAME',
                activation=tf.nn.relu,
                input_shape=input_shape))
    model.add(MaxPool2D(pool_size=pool_size, padding='SAME'))
    model.add(Dropout(rate=0.25))
    model.add(
        SConv2D(filters=64,
                kernel_size=kernel_size,
                padding='SAME',
                activation=tf.nn.relu))
    model.add(MaxPool2D(pool_size=pool_size, padding='SAME'))
    model.add(Dropout(rate=0.25))
    model.add(Flatten())
    model.add(Dense(units=128, activation=tf.nn.relu))
    model.add(Dropout(rate=0.5))
    model.add(Dense(units=classes, activation=tf.nn.softmax))

    return model
Пример #4
0
    def build_generator(self):
        model = Sequential()

        # https://github.com/eriklindernoren/Keras-GAN/blob/master/acgan/acgan.py
        # https://github.com/keras-team/keras/blob/master/examples/mnist_acgan.py

        model.add(
            Dense(units=128 * 8 * 8,
                  activation='relu',
                  input_dim=self.latent_dim))
        model.add(Reshape(target_shape=(8, 8, 128)))

        if self.mode == 'baseline':
            # model.add(UpSampling2D())
            # model.add(Conv2D(filters=128, kernel_size=3, padding='same'))
            model.add(
                Conv2DTranspose(filters=128,
                                kernel_size=3,
                                strides=2,
                                padding='same'))
        elif self.mode == 'S-ACGAN':
            # model.add(SSampling2D())
            # model.add(SConv2D(filters=128, kernel_size=3, padding='SAME'))
            model.add(
                SConv2DTranspose(filters=128,
                                 kernel_size=3,
                                 strides=2,
                                 padding='SAME'))
        elif self.mode == 'H-ACGAN':
            # model.add(HSampling2D())
            # model.add(HConv2D(filters=128, kernel_size=3, padding='SAME'))
            model.add(
                HConv2DTranspose(filters=128,
                                 kernel_size=3,
                                 strides=2,
                                 padding='SAME'))

        model.add(Activation('relu'))
        model.add(BatchNormalization())

        if self.mode == 'baseline':
            # model.add(UpSampling2D())
            # model.add(Conv2D(filters=64, kernel_size=3, padding='same'))
            model.add(
                Conv2DTranspose(filters=64,
                                kernel_size=3,
                                strides=2,
                                padding='same'))
        elif self.mode == 'S-ACGAN':
            # model.add(SSampling2D())
            # model.add(SConv2D(filters=64, kernel_size=3, padding='SAME'))
            model.add(
                SConv2DTranspose(filters=64,
                                 kernel_size=3,
                                 strides=2,
                                 padding='SAME'))
        elif self.mode == 'H-ACGAN':
            # model.add(HSampling2D())
            # model.add(HConv2D(filters=64, kernel_size=3, padding='SAME'))
            model.add(
                HConv2DTranspose(filters=64,
                                 kernel_size=3,
                                 strides=2,
                                 padding='SAME'))

        model.add(Activation('relu'))
        model.add(BatchNormalization())

        if self.mode == 'baseline':
            model.add(
                Conv2D(filters=self.channels, kernel_size=3, padding='same'))
        elif self.mode == 'S-ACGAN':
            model.add(
                SConv2D(filters=self.channels, kernel_size=3, padding='SAME'))
        elif self.mode == 'H-ACGAN':
            model.add(
                HConv2D(filters=self.channels, kernel_size=3, padding='SAME'))

        model.add(Activation('tanh'))

        noise = Input(shape=(self.latent_dim, ))
        label = Input(shape=(1, ), dtype='int32')
        label_embedding = Flatten()(Embedding(self.classes,
                                              self.latent_dim)(label))
        model_input = multiply([noise, label_embedding])
        img = model(model_input)

        return (Model([noise, label], img), model)
Пример #5
0
    def build_discriminator(self):
        model = Sequential()

        # https://github.com/keras-team/keras/blob/master/examples/mnist_acgan.py

        if self.mode == 'baseline':
            model.add(
                Conv2D(filters=32,
                       kernel_size=3,
                       strides=2,
                       padding='same',
                       input_shape=self.input_shape))
        elif self.mode == 'S-ACGAN':
            model.add(
                SConv2D(filters=32,
                        kernel_size=3,
                        strides=2,
                        padding='SAME',
                        input_shape=self.input_shape))
        elif self.mode == 'H-ACGAN':
            model.add(
                HConv2D(filters=32,
                        kernel_size=3,
                        strides=2,
                        padding='SAME',
                        input_shape=self.input_shape))

        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(rate=0.3))

        if self.mode == 'baseline':
            model.add(Conv2D(filters=64, kernel_size=3, padding='same'))
        elif self.mode == 'S-ACGAN':
            model.add(SConv2D(filters=64, kernel_size=3, padding='SAME'))
        elif self.mode == 'H-ACGAN':
            model.add(HConv2D(filters=64, kernel_size=3, padding='SAME'))

        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(rate=0.3))

        if self.mode == 'baseline':
            model.add(
                Conv2D(filters=128, kernel_size=3, strides=2, padding='same'))
        elif self.mode == 'S-ACGAN':
            model.add(
                SConv2D(filters=128, kernel_size=3, strides=2, padding='SAME'))
        elif self.mode == 'H-ACGAN':
            model.add(
                HConv2D(filters=128, kernel_size=3, strides=2, padding='SAME'))

        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(rate=0.3))

        if self.mode == 'baseline':
            model.add(Conv2D(filters=256, kernel_size=3, padding='same'))
        elif self.mode == 'S-ACGAN':
            model.add(SConv2D(filters=256, kernel_size=3, padding='SAME'))
        elif self.mode == 'H-ACGAN':
            model.add(HConv2D(filters=256, kernel_size=3, padding='SAME'))

        model.add(LeakyReLU(alpha=0.2))
        model.add(Dropout(rate=0.3))
        model.add(Flatten())

        img = Input(shape=self.input_shape)
        features = model(img)
        validity = Dense(units=1, activation='sigmoid')(features)
        label = Dense(units=self.classes, activation='softmax')(features)

        return (Model(img, [validity, label]), model)