Created
September 7, 2019 11:11
-
-
Save jasonsalas/4561d50ff2257e768c1876226806b3ec to your computer and use it in GitHub Desktop.
Model definition for shallow DCGAN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' SHALLOW MODEL ''' | |
img_shape = (32, 32, 3) | |
z_dim = 100 | |
init = initializers.RandomNormal(mean=0.0, stddev=0.02) | |
opt = Adam(lr=0.0002, beta_1=0.5) | |
def build_discriminator(in_shape=img_shape): | |
model = Sequential() | |
model.add(Conv2D(64, (5,5), input_shape=in_shape, kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Conv2D(128, (5,5), strides=2, padding='same', kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Conv2D(256, (5,5), strides=2, padding='same', kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Conv2D(512, (5,5), strides=2, padding='same', kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Flatten()) | |
model.add(Dropout(0.4)) | |
model.add(Dense(1, activation='sigmoid')) | |
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy']) | |
return model | |
def build_generator(latent_dim): | |
model = Sequential() | |
model.add(Dense(512*4*4, input_dim=latent_dim, kernel_initializer=init)) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Reshape((4, 4, 512))) | |
model.add(Conv2DTranspose(256, (5,5), strides=2, padding='same', kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Conv2DTranspose(128, (5,5), strides=2, padding='same', kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Conv2DTranspose(64, (5,5), strides=2, padding='same', kernel_initializer=init)) | |
model.add(BatchNormalization()) | |
model.add(LeakyReLU(alpha=0.2)) | |
model.add(Conv2D(3, (3,3), activation='tanh', padding='same', kernel_initializer=init)) | |
return model | |
def build_gan(generator, discriminator): | |
discriminator.trainable = False | |
model = Sequential() | |
model.add(generator) | |
model.add(discriminator) | |
model.compile(loss='binary_crossentropy', optimizer=opt) | |
return model |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment