示例#1
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")
示例#2
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)
示例#3
0
def model_generator(latent_dim, input_dim, hidden_dim=256):
    return Sequential([
        Dense(hidden_dim, name="generator_h1", input_dim=latent_dim),
        LeakyReLU(0.2),
        Dense(hidden_dim, name="generator_h2"),
        LeakyReLU(0.2),
        Dense(hidden_dim, name="generator_h3"),
        LeakyReLU(0.2),
        Dense(input_dim, name="generator_x_flat")],
        name="generator")
示例#4
0
def model_discriminator(input_dim, hidden_dim=256):
    return Sequential([
        Dense(hidden_dim, name="discriminator_h1", input_dim=input_dim),
        LeakyReLU(0.2),
        Dense(hidden_dim, name="discriminator_h2"),
        LeakyReLU(0.2),
        Dense(hidden_dim, name="discriminator_h3"),
        LeakyReLU(0.2),
        Dense(1, name="discriminator_y"),
        Activation('sigmoid')],
        name="discriminator")
def model_discriminator(input_shape, hidden_dim=1024, reg=lambda: l1l2(1e-5, 1e-5), output_activation="sigmoid"):
    return Sequential([
        Flatten(name="discriminator_flatten", input_shape=input_shape),
        Dense(hidden_dim, name="discriminator_h1", W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(hidden_dim / 2, name="discriminator_h2", W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(hidden_dim / 4, name="discriminator_h3", W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(1, name="discriminator_y", W_regularizer=reg()),
        Activation(output_activation)],
        name="discriminator")
def model_generator(latent_dim, input_shape, hidden_dim=1024, reg=lambda: l1l2(1e-5, 1e-5)):
    return Sequential([
        Dense(hidden_dim / 4, name="generator_h1", input_dim=latent_dim, W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(hidden_dim / 2, name="generator_h2", W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(hidden_dim, name="generator_h3", W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(np.prod(input_shape), name="generator_x_flat", W_regularizer=reg()),
        Activation('sigmoid'),
        Reshape(input_shape, name="generator_x")],
        name="generator")
示例#7
0
def model_discriminator(input_shape,
                        hidden_dim=1024,
                        reg=lambda: l1l2(1e-5, 1e-5)):
    return Sequential([
        Flatten(input_shape=input_shape),
        Dense(hidden_dim, W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(int(hidden_dim / 2), W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(int(hidden_dim / 4), W_regularizer=reg()),
        LeakyReLU(0.2),
        Dense(1, W_regularizer=reg()),
        Activation('sigmoid')
    ],
                      name="discriminator")
示例#8
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
示例#9
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
示例#10
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])
示例#11
0
def get_discriminator():
    """
    Returns the discriminator model
    """
    reg = lambda: l1l2(1e-5, 1e-5)
    model = Sequential()
    model.add(
        Flatten(name="discriminator_flatten",
                input_shape=(IMAGE_DIM, IMAGE_DIM)))
    model.add(Dense(1024, W_regularizer=reg()))
    model.add(LeakyReLU(0.2))
    model.add(Dense(512, W_regularizer=reg()))
    model.add(LeakyReLU(0.2))
    model.add(Dense(256, W_regularizer=reg()))
    model.add(Dense(1, W_regularizer=reg()))
    model.add(Activation('sigmoid'))
    return model
示例#12
0
def get_generator():
    """
    Returns the generator model
    """
    reg = lambda: l1l2(1e-5, 1e-5)
    model = Sequential()
    model.add(Dense(256, input_dim=IMAGE_DIM, W_regularizer=reg()))
    model.add(LeakyReLU(0.2))
    model.add(Dense(512, W_regularizer=reg()))
    model.add(LeakyReLU(0.2))
    model.add(Dense(1024, W_regularizer=reg()))
    model.add(LeakyReLU(0.2))

    # I think this has to be done to make the discriminator work
    model.add(Dense(np.prod((IMAGE_DIM, IMAGE_DIM)), W_regularizer=reg()))
    model.add(Activation('sigmoid'))
    model.add(Reshape((IMAGE_DIM, IMAGE_DIM)))
    return model
示例#13
0
def model_discriminator(latent_dim,
                        output_dim=1,
                        units=256,
                        reg=lambda: l1l2(1e-7, 1e-7)):
    z = Input((latent_dim, ))
    h = z
    mode = 1
    h = Dense(units, name="discriminator_h1", W_regularizer=reg())(h)
    # h = BatchNormalization(mode=mode)(h)
    h = LeakyReLU(0.2)(h)
    h = Dense(units / 2, name="discriminator_h2", W_regularizer=reg())(h)
    # h = BatchNormalization(mode=mode)(h)
    h = LeakyReLU(0.2)(h)
    h = Dense(units / 2, name="discriminator_h3", W_regularizer=reg())(h)
    # h = BatchNormalization(mode=mode)(h)
    h = LeakyReLU(0.2)(h)
    y = Dense(output_dim,
              name="discriminator_y",
              activation="sigmoid",
              W_regularizer=reg())(h)
    return Model(z, y)
示例#14
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)
示例#15
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)