Пример #1
0
def model_discriminator(input_shape=(1, 92, 92), dropout_rate=0.5):
    d_input = dim_ordering_input(input_shape, name="input_x")
    nch = 128
    # nch = 128
    H = Convolution2D(int(nch / 2),
                      5,
                      5,
                      subsample=(2, 2),
                      border_mode='same',
                      activation='relu')(d_input)
    H = LeakyReLU(0.2)(H)
    H = Dropout(dropout_rate)(H)
    H = Convolution2D(nch,
                      5,
                      5,
                      subsample=(2, 2),
                      border_mode='same',
                      activation='relu')(H)
    H = LeakyReLU(0.2)(H)
    H = Dropout(dropout_rate)(H)
    H = Flatten()(H)
    H = Dense(int(nch / 2))(H)
    H = LeakyReLU(0.2)(H)
    H = Dropout(dropout_rate)(H)
    d_V = Dense(1, activation='sigmoid')(H)
    return Model(d_input, d_V)
Пример #2
0
def model_encoder(latent_dim,
                  input_shape,
                  units=512,
                  reg=lambda: l1l2(l1=1e-7, l2=1e-7),
                  dropout=0.5):
    k = 5
    x = Input(input_shape)
    h = Convolution2D(units / 4, k, k, border_mode='same',
                      W_regularizer=reg())(x)
    # h = SpatialDropout2D(dropout)(h)
    h = MaxPooling2D(pool_size=(2, 2))(h)
    h = LeakyReLU(0.2)(h)
    h = Convolution2D(units / 2, k, k, border_mode='same',
                      W_regularizer=reg())(h)
    # h = SpatialDropout2D(dropout)(h)
    h = MaxPooling2D(pool_size=(2, 2))(h)
    h = LeakyReLU(0.2)(h)
    h = Convolution2D(units / 2, k, k, border_mode='same',
                      W_regularizer=reg())(h)
    # h = SpatialDropout2D(dropout)(h)
    h = MaxPooling2D(pool_size=(2, 2))(h)
    h = LeakyReLU(0.2)(h)
    h = Convolution2D(units, k, k, border_mode='same', W_regularizer=reg())(h)
    # h = SpatialDropout2D(dropout)(h)
    h = LeakyReLU(0.2)(h)
    h = Flatten()(h)
    mu = Dense(latent_dim, name="encoder_mu", W_regularizer=reg())(h)
    log_sigma_sq = Dense(latent_dim,
                         name="encoder_log_sigma_sq",
                         W_regularizer=reg())(h)
    z = Lambda(
        lambda
        (_mu, _lss): _mu + K.random_normal(K.shape(_mu)) * K.exp(_lss / 2),
        output_shape=lambda (_mu, _lss): _mu)([mu, log_sigma_sq])
    return Model(x, z, name="encoder")
Пример #3
0
def get_discriminator_cifar():
    nch = 256
    h = 5
    reg = lambda: l1l2(l1=1e-7, l2=1e-7)

    c1 = Convolution2D(int(nch / 4),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg(),
                       input_shape=(32, 32, 3))
    c2 = Convolution2D(int(nch / 2),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg())
    c3 = Convolution2D(nch, h, h, border_mode='same', W_regularizer=reg())
    c4 = Convolution2D(1, h, h, border_mode='same', W_regularizer=reg())

    model = Sequential()
    model.add(c1)
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(c2)
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(c3)
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(c4)
    model.add(AveragePooling2D(pool_size=(4, 4), border_mode='valid'))
    model.add(Flatten())
    model.add(Activation('sigmoid'))
    return model
Пример #4
0
def model_generator(latent_dim,
                    units=512,
                    dropout=0.5,
                    reg=lambda: l1l2(l1=1e-7, l2=1e-7)):
    model = Sequential(name="decoder")
    h = 5
    model.add(Dense(units * 4 * 4, input_dim=latent_dim, W_regularizer=reg()))
    model.add(Reshape(dim_ordering_shape((units, 4, 4))))
    # model.add(SpatialDropout2D(dropout))
    model.add(LeakyReLU(0.2))
    model.add(
        Convolution2D(units / 2, h, h, border_mode='same',
                      W_regularizer=reg()))
    # model.add(SpatialDropout2D(dropout))
    model.add(LeakyReLU(0.2))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(
        Convolution2D(units / 2, h, h, border_mode='same',
                      W_regularizer=reg()))
    # model.add(SpatialDropout2D(dropout))
    model.add(LeakyReLU(0.2))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(
        Convolution2D(units / 4, h, h, border_mode='same',
                      W_regularizer=reg()))
    # model.add(SpatialDropout2D(dropout))
    model.add(LeakyReLU(0.2))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg()))
    model.add(Activation('sigmoid'))
    return model
Пример #5
0
def model_generator():
    model = Sequential()
    nch = 256
    reg = lambda: l1l2(l1=1e-7, l2=1e-7)
    h = 5
    model.add(Dense(nch * 4 * 4, input_dim=100, W_regularizer=reg()))
    model.add(BatchNormalization(mode=0))
    model.add(Reshape(dim_ordering_shape((nch, 4, 4))))
    model.add(
        Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()))
    model.add(BatchNormalization(mode=0, axis=1))
    model.add(LeakyReLU(0.2))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(
        Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()))
    model.add(BatchNormalization(mode=0, axis=1))
    model.add(LeakyReLU(0.2))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(
        Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg()))
    model.add(BatchNormalization(mode=0, axis=1))
    model.add(LeakyReLU(0.2))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg()))
    model.add(Activation('sigmoid'))
    return model
Пример #6
0
def model_generator():
    nch = 256
    reg = lambda: l1l2(l1=1e-7, l2=1e-7)
    h = 5

    input_z = Input(shape=(latent_dim, ))
    input_label = Input(shape=(1, ))

    input_label_embedding = Flatten()(
        Embedding(nb_labels,
                  latent_dim,
                  embeddings_initializer='glorot_normal')(input_label))

    H = layers.multiply([input_z, input_label_embedding])
    H = Dense(nch * 4 * 4, W_regularizer=reg())(H)
    H = BatchNormalization(mode=0)(H)
    H = Reshape(dim_ordering_shape((nch, 4, 4)))(H)
    H = Convolution2D(int(nch / 2),
                      h,
                      h,
                      border_mode='same',
                      W_regularizer=reg())(H)
    H = BatchNormalization(mode=0, axis=1)(H)
    H = LeakyReLU(0.2)(H)
    H = (UpSampling2D(size=(2, 2)))(H)
    H = (Convolution2D(int(nch / 2),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg()))(H)
    H = (BatchNormalization(mode=0, axis=1))(H)
    H = (LeakyReLU(0.2))(H)
    H = (UpSampling2D(size=(2, 2)))(H)
    H = (Convolution2D(int(nch / 2),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg()))(H)
    H = (BatchNormalization(mode=0, axis=1))(H)
    H = (LeakyReLU(0.2))(H)
    H = (UpSampling2D(size=(2, 2)))(H)
    H = (Convolution2D(int(nch / 4),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg()))(H)
    H = (BatchNormalization(mode=0, axis=1))(H)
    H = (LeakyReLU(0.2))(H)
    H = (UpSampling2D(size=(2, 2)))(H)
    H = (Convolution2D(3, h, h, border_mode='same', W_regularizer=reg()))(H)
    H = (Activation('sigmoid'))(H)
    return Model(inputs=[input_z, input_label], outputs=H)
Пример #7
0
def model_discriminator():
    nch = 512
    h = 5
    reg = lambda: l1l2(l1=1e-7, l2=1e-7)

    input_d = Input(shape=(64, 64, 3))
    c1 = Convolution2D(int(nch / 4),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg(),
                       input_shape=(64, 64, 3))
    c2 = Convolution2D(int(nch / 4),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg())
    c3 = Convolution2D(int(nch / 2),
                       h,
                       h,
                       border_mode='same',
                       W_regularizer=reg())
    c4 = Convolution2D(int(nch), h, h, border_mode='same', W_regularizer=reg())

    H = c1(input_d)
    H = MaxPooling2D(pool_size=(2, 2))(H)
    H = LeakyReLU(0.2)(H)
    H = c2(H)
    H = MaxPooling2D(pool_size=(2, 2))(H)
    H = LeakyReLU(0.2)(H)
    H = c3(H)
    H = MaxPooling2D(pool_size=(2, 2))(H)
    H = LeakyReLU(0.2)(H)
    H = c4(H)
    H = AveragePooling2D(pool_size=(4, 4), border_mode='valid')(H)
    H = Flatten()(H)
    H = Dense(256)(H)
    H = LeakyReLU(0.2)(H)
    H = Dense(64)(H)
    H = LeakyReLU(0.2)(H)

    fake = Dense(1)(H)
    fake = Activation('sigmoid')(fake)

    category = Dense(nb_labels)(H)
    category = Activation('softmax')(category)
    return Model(inputs=[input_d], outputs=[fake, category])
Пример #8
0
def model_generator():
    nch = 256
    g_input = Input(shape=[100])
    H = Dense(nch * 14 * 14)(g_input)
    H = BatchNormalization(mode=2)(H)
    H = Activation('relu')(H)
    H = dim_ordering_reshape(nch, 14)(H)
    H = UpSampling2D(size=(2, 2))(H)
    H = Convolution2D(int(nch / 2), 3, 3, border_mode='same')(H)
    H = BatchNormalization(mode=2, axis=1)(H)
    H = Activation('relu')(H)
    H = Convolution2D(int(nch / 4), 3, 3, border_mode='same')(H)
    H = BatchNormalization(mode=2, axis=1)(H)
    H = Activation('relu')(H)
    H = Convolution2D(1, 1, 1, border_mode='same')(H)
    g_V = Activation('sigmoid')(H)
    return Model(g_input, g_V)
Пример #9
0
def model_discriminator():
    nch = 256
    h = 5
    reg = lambda: l1l2(l1=1e-7, l2=1e-7)

    model = Sequential()
    model.add(Convolution2D(int(nch / 4), h, h, border_mode='same', W_regularizer=reg(),
                       input_shape=dim_ordering_shape((3, 32, 32))))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(Convolution2D(int(nch / 2), h, h, border_mode='same', W_regularizer=reg()))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(Convolution2D(nch, h, h, border_mode='same', W_regularizer=reg()))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(Convolution2D(1, h, h, border_mode='same', W_regularizer=reg()))
    model.add(AveragePooling2D(pool_size=(4, 4), border_mode='valid'))
    model.add(Flatten())
    model.add(Activation('sigmoid'))
    return model
Пример #10
0
def model_discriminator_cifar():
    nch = 256
    h = 5
    reg = lambda: l1l2(l1=1e-7, l2=1e-7)
    '''
    c1 = Convolution2D(int(nch / 4), h, h, border_mode='same', W_regularizer=reg(),
                       input_shape=dim_ordering_shape((3, 32, 32)))
    '''

    # M: I've modified the input shape to (8, 256, 256) representing
    # M: the 8 bit grayscale images with 256x256 resolution
    # M: (Or 32x32 for debugging)
    input_shape = dim_ordering_shape((3, 32, 32))
    c1 = Convolution2D(int(nch / 4), h, h, border_mode='same', W_regularizer=reg(),
                       input_shape=dim_ordering_shape((32, 32, 3)))
    print "c1..."
    c2 = Convolution2D(int(nch / 2), h, h, border_mode='same', W_regularizer=reg())
    print "c2..."
    c3 = Convolution2D(nch, h, h, border_mode='same', W_regularizer=reg())
    print "c3..."
    c4 = Convolution2D(1, h, h, border_mode='same', W_regularizer=reg())
    print "c4..."

    model = Sequential()
    model.add(c1)
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(c2)
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(c3)
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(LeakyReLU(0.2))
    model.add(c4)
    model.add(AveragePooling2D(pool_size=(4, 4), border_mode='valid'))
    model.add(Flatten())
    model.add(Activation('sigmoid'))
    return model