Beispiel #1
0
              data_format='channels_last')(_inputBN)
C1 = LeakyReLU()(C1)

C2 = ConvSN2D(25, (7, 7), padding='valid', data_format='channels_last')(C1)
C2 = LeakyReLU()(C2)

C3 = ConvSN2D(40, (9, 9), padding='valid', data_format='channels_last')(C2)
C3 = LeakyReLU()(C3)

C4 = ConvSN2D(50, (11, 11), padding='valid', data_format='channels_last')(C3)
C4 = LeakyReLU()(C4)

Average_score = GlobalAveragePooling2D(name='Average_score')(
    C4)  #(batch_size, channels)

D1 = DenseSN(50)(Average_score)
D1 = LeakyReLU()(D1)

D2 = DenseSN(10)(D1)
D2 = LeakyReLU()(D2)

Score = DenseSN(1)(D2)

Discriminator = Model(outputs=Score, inputs=_input)

Discriminator.trainable = True
Discriminator.compile(loss='mse', optimizer='adam')

#### Combine the two networks to become MetricGAN
Discriminator.trainable = False
Beispiel #2
0
def BuildDiscriminator(summary=True,
                       spectral_normalization=True,
                       batch_normalization=False,
                       bn_momentum=0.9,
                       bn_epsilon=0.00002,
                       resnet=True,
                       name='Discriminator',
                       plot=False):
    if resnet:
        model_input = Input(shape=(32, 32, 3))
        resblock_1 = ResBlock(input_shape=(32, 32, 3),
                              channels=128,
                              sampling='down',
                              batch_normalization=True,
                              spectral_normalization=spectral_normalization,
                              name='Discriminator_resblock_Down_1')
        h = resblock_1(model_input)
        resblock_2 = ResBlock(input_shape=(16, 16, 128),
                              channels=128,
                              sampling='down',
                              batch_normalization=True,
                              spectral_normalization=spectral_normalization,
                              name='Discriminator_resblock_Down_2')
        h = resblock_2(h)
        resblock_3 = ResBlock(input_shape=(8, 8, 128),
                              channels=128,
                              sampling=None,
                              batch_normalization=True,
                              spectral_normalization=spectral_normalization,
                              trainable_sortcut=False,
                              name='Discriminator_resblock_1')
        h = resblock_3(h)
        resblock_4 = ResBlock(input_shape=(8, 8, 128),
                              channels=128,
                              sampling=None,
                              batch_normalization=True,
                              spectral_normalization=spectral_normalization,
                              trainable_sortcut=False,
                              name='Discriminator_resblock_2')
        h = resblock_4(h)
        h = Activation('relu')(h)
        h = GlobalSumPooling2D()(h)
        model_output = DenseSN(1, kernel_initializer='glorot_uniform')(h)

        model = Model(model_input, model_output, name=name)

    else:
        if spectral_normalization:
            model = Sequential(name=name)
            model.add(
                ConvSN2D(64,
                         kernel_size=3,
                         strides=1,
                         kernel_initializer='glorot_uniform',
                         padding='same',
                         input_shape=(32, 32, 3)))
            model.add(LeakyReLU(0.1))
            model.add(
                ConvSN2D(64,
                         kernel_size=4,
                         strides=2,
                         kernel_initializer='glorot_uniform',
                         padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                ConvSN2D(128,
                         kernel_size=3,
                         strides=1,
                         kernel_initializer='glorot_uniform',
                         padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                ConvSN2D(128,
                         kernel_size=4,
                         strides=2,
                         kernel_initializer='glorot_uniform',
                         padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                ConvSN2D(256,
                         kernel_size=3,
                         strides=1,
                         kernel_initializer='glorot_uniform',
                         padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                ConvSN2D(256,
                         kernel_size=4,
                         strides=2,
                         kernel_initializer='glorot_uniform',
                         padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                ConvSN2D(512,
                         kernel_size=3,
                         strides=1,
                         kernel_initializer='glorot_uniform',
                         padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(GlobalSumPooling2D())
            model.add(DenseSN(1, kernel_initializer='glorot_uniform'))
        else:
            model = Sequential(name=name)
            model.add(
                Conv2D(64,
                       kernel_size=3,
                       strides=1,
                       kernel_initializer='glorot_uniform',
                       padding='same',
                       input_shape=(32, 32, 3)))
            model.add(LeakyReLU(0.1))
            model.add(
                Conv2D(64,
                       kernel_size=4,
                       strides=2,
                       kernel_initializer='glorot_uniform',
                       padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                Conv2D(128,
                       kernel_size=3,
                       strides=1,
                       kernel_initializer='glorot_uniform',
                       padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                Conv2D(128,
                       kernel_size=4,
                       strides=2,
                       kernel_initializer='glorot_uniform',
                       padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                Conv2D(256,
                       kernel_size=3,
                       strides=1,
                       kernel_initializer='glorot_uniform',
                       padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                Conv2D(256,
                       kernel_size=4,
                       strides=2,
                       kernel_initializer='glorot_uniform',
                       padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(
                Conv2D(512,
                       kernel_size=3,
                       strides=1,
                       kernel_initializer='glorot_uniform',
                       padding='same'))
            model.add(LeakyReLU(0.1))
            model.add(GlobalSumPooling2D())
            model.add(Dense(1, kernel_initializer='glorot_uniform'))
    if plot:
        plot_model(model, name + '.png', show_layer_names=True)

    if summary:
        print('Discriminator')
        print('Spectral Normalization: {}'.format(spectral_normalization))
        model.summary()
    return model
Beispiel #3
0
def discriminator_aux(input_shape,
                      pc_code_dim=0,
                      bn_momentum=0.9,
                      bn_epsilon=0.00002):

    inputs = Input(shape=input_shape, name='image_input')
    channels = 64
    x = ResBlock(input_shape=input_shape,
                 channels=channels,
                 sampling='down',
                 batch_normalization=False,
                 name='dis_resblock_0')(inputs)

    d = input_shape[0] // 2
    shape = (d, d, channels)
    name = 'dis_resblock_1'
    x = ResBlock(input_shape=shape,
                 channels=(channels * 2),
                 sampling='down',
                 batch_normalization=False,
                 name=name)(x)
    channels *= 2

    d = input_shape[0] // 4
    shape = (d, d, channels)
    name = 'dis_resblock_2'
    x = ResBlock(input_shape=shape,
                 channels=(channels * 2),
                 sampling='down',
                 batch_normalization=False,
                 name=name)(x)
    channels *= 2

    d = input_shape[0] // 8
    shape = (d, d, channels)
    name = 'dis_resblock_3'
    x = ResBlock(input_shape=shape,
                 channels=(channels * 2),
                 sampling='down',
                 batch_normalization=False,
                 name=name)(x)
    channels *= 2
    aux_layer = Flatten(name='aux_layer')(x)

    d = input_shape[0] // 16
    shape = (d, d, channels)
    name = 'dis_resblock_4'
    x = ResBlock(input_shape=shape,
                 channels=(channels * 2),
                 sampling='down',
                 batch_normalization=False,
                 name=name)(x)
    channels *= 2

    d = input_shape[0] // 32
    shape = (d, d, channels)
    name = 'dis_resblock_5'
    x = ResBlock(input_shape=shape,
                 channels=channels,
                 sampling=None,
                 batch_normalization=False,
                 name=name)(x)

    x = Activation('relu')(x)
    x = GlobalSumPooling2D()(x)
    preal = DenseSN(1,
                    activation='sigmoid',
                    kernel_initializer='glorot_uniform',
                    name='real_fake')(x)

    aux_layer = DenseSN(256, activation='relu')(aux_layer)
    reco_pc_code = DenseSN(1024, activation='relu')(aux_layer)
    reco_pc_code = DenseSN(1024, activation='relu')(reco_pc_code)
    reco_pc_code = DenseSN(1024, activation='relu')(reco_pc_code)
    reco_pc_code = DenseSN(pc_code_dim,
                           activation='linear',
                           name='reco_pc_code')(reco_pc_code)

    reco_elev_code = DenseSN(128, activation='relu')(aux_layer)
    reco_elev_code = DenseSN(128, activation='relu')(reco_elev_code)
    reco_elev_code = DenseSN(128, activation='relu')(reco_elev_code)
    reco_elev_code = DenseSN(1,
                             activation='sigmoid',
                             kernel_initializer='glorot_uniform',
                             name='reco_elev_code')(reco_elev_code)

    reco_azim_code = DenseSN(128, activation='relu')(aux_layer)
    reco_azim_code = DenseSN(128, activation='relu')(reco_azim_code)
    reco_azim_code = DenseSN(128, activation='relu')(reco_azim_code)
    reco_azim_code = DenseSN(1,
                             activation='sigmoid',
                             kernel_initializer='glorot_uniform',
                             name='reco_azim_code')(reco_azim_code)

    outputs = [preal, reco_pc_code, reco_elev_code, reco_azim_code]

    return Model(inputs, outputs, name='discriminator')
Beispiel #4
0
def word_cnn(dataset, use_glove=False, sn=True):
    filters = 250
    kernel_size = 3
    hidden_dims = 250

    max_len = config.word_max_len[dataset]
    num_classes = config.num_classes[dataset]
    loss = config.loss[dataset]
    activation = config.activation[dataset]
    embedding_dims = config.wordCNN_embedding_dims[dataset]
    num_words = config.num_words[dataset]

    print('Build word_cnn model...')
    model = Sequential()

    if use_glove:
        file_path = r'./glove.6B.{}d.txt'.format(str(embedding_dims))
        pdb.set_trace()
        get_embedding_index(file_path)
        get_embedding_matrix(dataset, num_words, embedding_dims)
        model.add(
            Embedding(  # Layer 0, Start
                input_dim=num_words +
                1,  # Size to dictionary, has to be input + 1
                output_dim=embedding_dims,  # Dimensions to generate
                weights=[embedding_matrix],  # Initialize word weights
                input_length=max_len,
                name="embedding_layer",
                trainable=False))
    else:
        # embedding_dims => 50
        model.add(Embedding(num_words, embedding_dims, input_length=max_len))
    model.add(Dropout(0.2))

    if sn:
        ##  with SN  ##
        model.add(Reshape((-1, 400, 50)))
        model.add(
            ConvSN2D(filters, (1, kernel_size),
                     strides=1,
                     padding='valid',
                     activation='relu'))
        model.add(Reshape((398, 250)))
        model.add(GlobalMaxPooling1D())
        model.add(DenseSN(hidden_dims))
        model.add(Dropout(0.2))
        model.add(Activation('relu'))
        model.add(DenseSN(num_classes))
        model.add(Activation(activation))
    else:

        ##  without SN  ##
        model.add(
            Conv1D(filters,
                   kernel_size,
                   padding='valid',
                   activation='relu',
                   strides=1))
        model.add(GlobalMaxPooling1D())
        model.add(Dense(hidden_dims))
        model.add(Dropout(0.2))
        model.add(Activation('relu'))
        model.add(Dense(num_classes))
        model.add(Activation(activation))

    model.compile(loss=loss, optimizer='adam', metrics=['accuracy'])

    return model
Beispiel #5
0
def discriminator(input_shape,
                  pc_code_dim=0,
                  spectral_normalization=True,
                  batch_normalization=False,
                  bn_momentum=0.9,
                  bn_epsilon=0.00002):

    inputs = Input(shape=input_shape, name='image_input')
    channels = 64
    # conv2.1
    x = ResBlockBottleneck(input_shape=input_shape,
                           channels=channels,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_01')(inputs)

    d = input_shape[0]
    shape = (d, d, channels)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_02')(x)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels,
                           sampling='down',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_03')(x)
    # conv2.2
    d = input_shape[0] // 2
    shape = (d, d, channels)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_10')(x)
    shape = (d, d, channels * 2)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_11')(x)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_12')(x)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='down',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_13')(x)
    channels *= 2

    # conv2.3

    d = input_shape[0] // 4
    shape = (d, d, channels)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_20')(x)
    shape = (d, d, channels * 2)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_21')(x)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_22')(x)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_23')(x)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_24')(x)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='down',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_25')(x)
    channels *= 2
    d = input_shape[0] // 8
    shape = (d, d, channels)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_30')(x)
    shape = (d, d, channels * 2)
    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='None',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_31')(x)

    x = ResBlockBottleneck(input_shape=shape,
                           channels=channels * 2,
                           sampling='down',
                           batch_normalization=batch_normalization,
                           spectral_normalization=spectral_normalization,
                           name='dis_resblock_32')(x)
    channels *= 2

    # d = input_shape[0] // 2
    # shape = (d, d, channels)
    # name = 'dis_resblock_1'
    # x = ResBlockBottleneck(input_shape=shape,
    #              channels=(channels * 2),
    #              sampling='down',
    #              batch_normalization=batch_normalization,
    #              spectral_normalization=spectral_normalization,
    #              name=name)(x)
    # channels *= 2
    #
    # d = input_shape[0] // 4
    # shape = (d, d, channels)
    # name = 'dis_resblock_2'
    # x = ResBlockBottleneck(input_shape=shape,
    #              channels=(channels * 2),
    #              sampling='down',
    #              batch_normalization=batch_normalization,
    #              spectral_normalization=spectral_normalization,
    #              name=name)(x)
    # channels *= 2
    #
    # d = input_shape[0] // 8
    # shape = (d, d, channels)
    # name = 'dis_resblock_3'
    # x = ResBlockBottleneck(input_shape=shape,
    #              channels=(channels * 2),
    #              sampling='down',
    #              batch_normalization=batch_normalization,
    #              spectral_normalization=spectral_normalization,
    #              name=name)(x)
    # channels *= 2
    aux_layer_pc = Flatten(name='aux_layer_pc')(x)

    # d = input_shape[0] // 16
    # shape = (d, d, channels)
    # name = 'dis_resblock_4'
    # x = ResBlockBottleneck(input_shape=shape,
    #              channels=(channels * 2),
    #              sampling='down',
    #              batch_normalization=batch_normalization,
    #              spectral_normalization=spectral_normalization,
    #              name=name)(x)
    # channels *= 2
    #
    # d = input_shape[0] // 32
    # shape = (d, d, channels)
    # name = 'dis_resblock_5'
    # x = ResBlockBottleneck(input_shape=shape,
    #              channels=channels,
    #              sampling=None,
    #              batch_normalization=batch_normalization,
    #              spectral_normalization=spectral_normalization,
    #              name=name)(x)

    x = Activation('relu')(x)
    x = GlobalSumPooling2D()(x)
    preal = DenseSN(1,
                    activation='sigmoid',
                    kernel_initializer='glorot_uniform',
                    name='real_fake')(x)

    aux_layer_pc = DenseSN(256, activation='relu')(aux_layer_pc)
    reco_pc_code = DenseSN(1024, activation='relu')(aux_layer_pc)
    reco_pc_code = DenseSN(1024, activation='relu')(reco_pc_code)
    reco_pc_code = DenseSN(1024, activation='relu')(reco_pc_code)
    reco_pc_code = DenseSN(pc_code_dim,
                           activation='linear',
                           name='reco_pc_code')(reco_pc_code)

    reco_azim_code = DenseSN(1,
                             activation='sigmoid',
                             kernel_initializer='glorot_uniform',
                             name='reco_azim_code')(x)

    reco_elev_code = DenseSN(1,
                             activation='sigmoid',
                             kernel_initializer='glorot_uniform',
                             name='reco_elev_code')(x)

    outputs = [preal, reco_pc_code, reco_elev_code, reco_azim_code]

    return Model(inputs, outputs, name='discriminator')