def fcc_wgan_generator(bnmode=0): global usegbn, conv_init d_input = Input(shape=(100,)) L = Reshape(target_shape=dim_ordering_shape((100, 1, 1)))(d_input) L = Dense(64)(L) L = Activation('relu')(L) L = Dense(512)(L) L = Activation('relu')(L) L = Dense(128 * 3 * 3)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Reshape(dim_ordering_shape((128, 3, 3)))(L) L = Conv2DTranspose(filters=64, kernel_size=3, strides=2, use_bias=False, kernel_initializer=conv_init)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', use_bias=False, kernel_initializer=conv_init)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=1, kernel_size=3, strides=2, padding='same', use_bias=False, kernel_initializer=conv_init)(L) d_output = Activation('tanh')(L) return Model(d_input, d_output)
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, kernel_regularizer=reg())) model.add(BatchNormalization()) model.add(Reshape(dim_ordering_shape((nch, 4, 4)))) model.add( Conv2D(int(nch / 2), (h, h), padding="same", kernel_regularizer=reg())) model.add(BatchNormalization()) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add( Conv2D(int(nch / 2), (h, h), padding="same", kernel_regularizer=reg())) model.add(BatchNormalization()) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add( Conv2D(int(nch / 4), (h, h), padding="same", kernel_regularizer=reg())) model.add(BatchNormalization()) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Conv2D(3, (h, h), padding="same", kernel_regularizer=reg())) model.add(Activation("sigmoid")) return model
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
def fccgan_generator(bnmode=0): global usegbn, conv_init #LID improvement dense layers d_input = Input(shape=(100,)) L = Dense(64)(d_input) L = Activation('relu')(L) L = Dense(512)(L) L = Activation('relu')(L) L = Dense(256 * 4 * 4)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Reshape(dim_ordering_shape((256, 4, 4)))(L) L = Conv2DTranspose(filters=128, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) L = Cropping2D(cropping=1)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=64, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) L = Cropping2D(cropping=1)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=3, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) L = Cropping2D(cropping=1)(L) d_output = Activation('tanh')(L) return Model(d_input, d_output)
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
def fcc_wgan_discriminator(bnmode=0): global usedbn, conv_init d_input = Input(shape=dim_ordering_shape((1, 28, 28))) L = Conv2D(filters=32, kernel_size=3, strides=2, padding='same', use_bias=False, kernel_initializer=conv_init)(d_input) if(usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = LeakyReLU(alpha=0.2)(L) L = Conv2D(filters=64, kernel_size=3, strides=2, padding='same', use_bias=False, kernel_initializer=conv_init)(L) if (usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = LeakyReLU(alpha=0.2)(L) L = Conv2D(filters=128, kernel_size=3, strides=2, use_bias=False, kernel_initializer=conv_init)(L) if (usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = LeakyReLU(alpha=0.2)(L) L = Flatten()(L) L = Dense(512)(L) L = LeakyReLU(alpha=0.2)(L) L = Dense(64)(L) L = LeakyReLU(alpha=0.2)(L) L = Dense(16)(L) L = LeakyReLU(alpha=0.2)(L) d_output = Dense(1)(L) return Model(d_input, d_output)
def model_discriminator(): nch = 256 h = 5 reg = lambda: l1l2(l1=1e-7, l2=1e-7) c1 = Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg(), input_shape=dim_ordering_shape((3, 32, 32))) c2 = Convolution2D(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
def dcgan_discriminator(bnmode=0): global usedbn, conv_init d_input = Input(shape=dim_ordering_shape((3, 32, 32))) L = ZeroPadding2D()(d_input) L = Conv2D(filters=64, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) if(usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = LeakyReLU(alpha=0.2)(L) L = ZeroPadding2D()(L) L = Conv2D(filters=128, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) if (usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = LeakyReLU(alpha=0.2)(L) L = ZeroPadding2D()(L) L = Conv2D(filters=256, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) if (usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = LeakyReLU(alpha=0.2)(L) L = Conv2D(filters=1, kernel_size=4, strides=1, use_bias=False, kernel_initializer=conv_init)(L) L = Flatten()(L) d_output = Activation('sigmoid')(L) return Model(d_input, d_output)
def dcgan_generator(bnmode=0): global usegbn, conv_init d_input = Input(shape=(100,)) L = Reshape(target_shape=dim_ordering_shape((100, 1, 1)))(d_input) L = Conv2DTranspose(filters=256, kernel_size=4, strides=1, use_bias=False, kernel_initializer=conv_init)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=128, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) L = Cropping2D(cropping=1)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=64, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) L = Cropping2D(cropping=1)(L) if (usegbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = Activation("relu")(L) L = Conv2DTranspose(filters=3, kernel_size=4, strides=2, use_bias=False, kernel_initializer=conv_init)(L) L = Cropping2D(cropping=1)(L) d_output = Activation('tanh')(L) return Model(d_input, d_output)
def fccgan_discriminator_pooling(bnmode=0): global usedbn, conv_init d_input = Input(shape=dim_ordering_shape((3, 32, 32))) L = Conv2D(filters=64, kernel_size=4, strides=1, padding='same', use_bias=False, kernel_initializer=conv_init)(d_input) if(usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = AveragePooling2D(pool_size=(2, 2))(L) L = LeakyReLU(alpha=0.2)(L) L = Conv2D(filters=128, kernel_size=4, strides=1, padding='same', use_bias=False, kernel_initializer=conv_init)(L) if (usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = AveragePooling2D(pool_size=(2, 2))(L) L = LeakyReLU(alpha=0.2)(L) L = Conv2D(filters=256, kernel_size=4, strides=1, padding='same', use_bias=False, kernel_initializer=conv_init)(L) if (usedbn): L = BatchNormalization(axis=get_bn_axis(), epsilon=1.01e-5)(L, training=bnmode) L = AveragePooling2D(pool_size=(2, 2))(L) L = LeakyReLU(alpha=0.2)(L) L = Flatten()(L) L = Dense(512)(L) L = LeakyReLU(alpha=0.2)(L) L = Dense(64)(L) L = LeakyReLU(alpha=0.2)(L) L = Dense(16)(L) L = LeakyReLU(alpha=0.2)(L) L = Dense(1)(L) d_output = Activation('sigmoid')(L) return Model(d_input, d_output)
def model_discriminator(): nch = 256 h = 5 reg = lambda: L1L2(l1=1e-7, l2=1e-7) c1 = Conv2D(int(nch / 4), (h, h), padding="same", kernel_regularizer=reg(), input_shape=dim_ordering_shape((3, 32, 32))) c2 = Conv2D(int(nch / 2), (h, h), padding="same", kernel_regularizer=reg()) c3 = Conv2D(nch, (h, h), padding="same", kernel_regularizer=reg()) c4 = Conv2D(1, (h, h), padding="same", kernel_regularizer=reg()) def m(dropout): model = Sequential() model.add(c1) model.add(SpatialDropout2D(dropout)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(LeakyReLU(0.2)) model.add(c2) model.add(SpatialDropout2D(dropout)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(LeakyReLU(0.2)) model.add(c3) model.add(SpatialDropout2D(dropout)) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(LeakyReLU(0.2)) model.add(c4) model.add(AveragePooling2D(pool_size=(4, 4), padding="valid")) model.add(Flatten()) model.add(Activation("sigmoid")) return model return m
def model_discriminator(): 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))) 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
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
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
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(int(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(int(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(int(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
def run_wgan_exp(exp_dir, netG, netD): K.set_image_data_format('channels_last') netG.summary() netD.summary() nz = 100 #noise dimension ngf = 64 ndf = 64 n_extra_layers = 0 Diters = 5 imageSize = 32 #image height and width nc = 3 #image channels batchSize = 64 lrD = 0.00005 lrG = 0.00005 #define weight clipping function clamp_lower, clamp_upper = -0.01, 0.01 #wgan weight clip maximum and minimum value clamp_updates = [ K.update(v, K.clip(v, clamp_lower, clamp_upper)) for v in netD.trainable_weights ] netD_clamp = K.function([], [], clamp_updates) #define wgan loss discriminator netD_real_input = Input(shape=dim_ordering_shape((nc, imageSize, imageSize))) noisev = Input(shape=(nz, )) loss_real = K.mean(netD(netD_real_input)) loss_fake = K.mean(netD(netG(noisev))) loss = loss_fake - loss_real training_updates = RMSprop(lr=lrD).get_updates(netD.trainable_weights, [], loss) netD_train = K.function([netD_real_input, noisev], [loss_real, loss_fake], training_updates) #define wgan loss for generator loss = -loss_fake training_updates = RMSprop(lr=lrG).get_updates(netG.trainable_weights, [], loss) netG_train = K.function([noisev], [loss], training_updates) #get real data and noise train_X, test_X = cifar10_data() start = 0 niter = 100 gen_iterations = 0 loss = [] for epoch in range(start, start + niter): i = 0 np.random.shuffle(train_X) batches = train_X.shape[0] // batchSize while i < batches: _Diters = Diters j = 0 while j < _Diters and i < batches: j += 1 i += 1 netD_clamp([]) real_data = train_X[i * batchSize:(i + 1) * batchSize] noise = np.random.normal(size=(batchSize, nz)) errD_real, errD_fake = netD_train([real_data, noise]) noise = np.random.normal(size=(batchSize, nz)) errG, = netG_train([noise]) gen_iterations += 1 # Save generator model after every epoch print('Discriminator loss: ', 100 * (errD_fake - errD_real), 'Generator loss: ', 100 * errG) loss.extend([errD_fake, errD_real, errG]) # rem = epoch % 100 # if rem <= 10: generate_images(generator_sampler(nz, netG), './wgan-v2-images/' + exp_dir + '/epoch-{:03d}.png', epoch) netG.save_weights('./wgan-v2-model-weights/' + exp_dir + '/gen_weight_epoch_' + str(epoch) + '.h5') #save loss history df = pd.DataFrame(loss) df.to_csv('./wgan-v2-images/' + exp_dir + '/history.csv') #save final weights netG.save_weights('./wgan-v2-model-weights/' + exp_dir + '/generator.h5') netD.save_weights('./wgan-v2-model-weights/' + exp_dir + '/discriminator.h5')
def example_aae(path, adversarial_optimizer): # z \in R^100 latent_dim = 256 units = 512 # x \in R^{28x28} input_shape = dim_ordering_shape((3, 32, 32)) # generator (z -> x) generator = model_generator(latent_dim, units=units) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape, units=units) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (z -> y) discriminator = model_discriminator(latent_dim, units=units) # build AAE x = encoder.inputs[0] z = encoder(x) xpred = generator(z) zreal = normal_latent_sampling((latent_dim, ))(x) yreal = discriminator(zreal) yfake = discriminator(z) aae = Model(x, fix_names([xpred, yfake, yreal], ["xpred", "yfake", "yreal"])) # print summary of models generator.summary() encoder.summary() discriminator.summary() autoencoder.summary() # build adversarial model generative_params = generator.trainable_weights + encoder.trainable_weights model = AdversarialModel( base_model=aae, player_params=[generative_params, discriminator.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile( adversarial_optimizer=adversarial_optimizer, player_optimizers=[Adam(3e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss={ "yfake": "binary_crossentropy", "yreal": "binary_crossentropy", "xpred": "mean_squared_error" }, player_compile_kwargs=[{ "loss_weights": { "yfake": 1e-1, "yreal": 1e-1, "xpred": 1e2 } }] * 2) # load mnist data xtrain, xtest = cifar10_data() # callback for image grid of generated samples def generator_sampler(): zsamples = np.random.normal(size=(10 * 10, latent_dim)) return dim_ordering_unfix(generator.predict(zsamples)).transpose( (0, 2, 3, 1)).reshape((10, 10, 32, 32, 3)) generator_cb = ImageGridCallback( os.path.join(path, "generated-epoch-{:03d}.png"), generator_sampler) # callback for image grid of autoencoded samples def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = dim_ordering_unfix(autoencoder.predict(xrep)).reshape( (10, 9, 3, 32, 32)) xsamples = dim_ordering_unfix(xsamples).reshape((10, 1, 3, 32, 32)) samples = np.concatenate((xsamples, xgen), axis=1) samples = samples.transpose((0, 1, 3, 4, 2)) return samples autoencoder_cb = ImageGridCallback(os.path.join( path, "autoencoded-epoch-{:03d}.png"), autoencoder_sampler, cmap=None) # train network # generator, discriminator; pred, yfake, yreal n = xtrain.shape[0] y = [ xtrain, np.ones((n, 1)), np.zeros((n, 1)), xtrain, np.zeros((n, 1)), np.ones((n, 1)) ] ntest = xtest.shape[0] ytest = [ xtest, np.ones((ntest, 1)), np.zeros((ntest, 1)), xtest, np.zeros((ntest, 1)), np.ones((ntest, 1)) ] history = fit(model, x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb, autoencoder_cb], nb_epoch=100, batch_size=32) # save history df = pd.DataFrame(history.history) df.to_csv(os.path.join(path, "history.csv")) # save model encoder.save(os.path.join(path, "encoder.h5")) generator.save(os.path.join(path, "generator.h5")) discriminator.save(os.path.join(path, "discriminator.h5"))
def example_aae(path, adversarial_optimizer): # z \in R^100 latent_dim = 256 units = 512 # x \in R^{28x28} input_shape = dim_ordering_shape((3, 32, 32)) # generator (z -> x) generator = model_generator(latent_dim, units=units) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape, units=units) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (z -> y) discriminator = model_discriminator(latent_dim, units=units) # build AAE x = encoder.inputs[0] z = encoder(x) xpred = generator(z) zreal = normal_latent_sampling((latent_dim,))(x) yreal = discriminator(zreal) yfake = discriminator(z) aae = Model(x, fix_names([xpred, yfake, yreal], ["xpred", "yfake", "yreal"])) # print summary of models generator.summary() encoder.summary() discriminator.summary() autoencoder.summary() # build adversarial model generative_params = generator.trainable_weights + encoder.trainable_weights model = AdversarialModel(base_model=aae, player_params=[generative_params, discriminator.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile(adversarial_optimizer=adversarial_optimizer, player_optimizers=[Adam(3e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss={"yfake": "binary_crossentropy", "yreal": "binary_crossentropy", "xpred": "mean_squared_error"}, compile_kwargs={"loss_weights": {"yfake": 1e-1, "yreal": 1e-1, "xpred": 1e2}}) # load mnist data xtrain, xtest = cifar10_data() # callback for image grid of generated samples def generator_sampler(): zsamples = np.random.normal(size=(10 * 10, latent_dim)) return dim_ordering_unfix(generator.predict(zsamples)).transpose((0, 2, 3, 1)).reshape((10, 10, 32, 32, 3)) generator_cb = ImageGridCallback(os.path.join(path, "generated-epoch-{:03d}.png"), generator_sampler) # callback for image grid of autoencoded samples def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = dim_ordering_unfix(autoencoder.predict(xrep)).reshape((10, 9, 3, 32, 32)) xsamples = dim_ordering_unfix(xsamples).reshape((10, 1, 3, 32, 32)) samples = np.concatenate((xsamples, xgen), axis=1) samples = samples.transpose((0, 1, 3, 4, 2)) return samples autoencoder_cb = ImageGridCallback(os.path.join(path, "autoencoded-epoch-{:03d}.png"), autoencoder_sampler, cmap=None) # train network # generator, discriminator; pred, yfake, yreal n = xtrain.shape[0] y = [xtrain, np.ones((n, 1)), np.zeros((n, 1)), xtrain, np.zeros((n, 1)), np.ones((n, 1))] ntest = xtest.shape[0] ytest = [xtest, np.ones((ntest, 1)), np.zeros((ntest, 1)), xtest, np.zeros((ntest, 1)), np.ones((ntest, 1))] history = fit(model, x=xtrain, y=y, validation_data=(xtest, ytest), callbacks=[generator_cb, autoencoder_cb], nb_epoch=100, batch_size=32) # save history df = pd.DataFrame(history.history) df.to_csv(os.path.join(path, "history.csv")) # save model encoder.save(os.path.join(path, "encoder.h5")) generator.save(os.path.join(path, "generator.h5")) discriminator.save(os.path.join(path, "discriminator.h5"))
def example_faae(path, adversarial_optimizer): latent_dim = 256 units = 512 input_shape = dim_ordering_shape((3, 32, 32)) # generator (z -> x) generator = model_generator(latent_dim, units=units) # encoder (x ->z) encoder = model_encoder(latent_dim, input_shape, units=units) # autoencoder (x -> x') autoencoder = Model(encoder.inputs, generator(encoder(encoder.inputs))) # discriminator (z -> y) discriminator = model_discriminator() # build FAAE zreal = discriminator.inputs[0] x = generator.inputs[0] z = generator(x) xpred = encoder(z) yreal = discriminator(zreal) yfake = discriminator(z) aae = Model([zreal, x], fix_names([xpred, yfake, yreal], ["xpred", "yfake", "yreal"])) # print summary of models generator.summary() encoder.summary() discriminator.summary() #encoder.load_weights(os.path.join(path, "encoder.h5")) #generator.load_weights(os.path.join(path, "generator.h5")) #discriminator.load_weights(os.path.join(path, "discriminator.h5")) # build adversarial model generative_params = generator.trainable_weights + encoder.trainable_weights model = AdversarialModel( base_model=aae, player_params=[generative_params, discriminator.trainable_weights], player_names=["generator", "discriminator"]) model.adversarial_compile( adversarial_optimizer=adversarial_optimizer, player_optimizers=[Adam(3e-4, decay=1e-4), Adam(1e-3, decay=1e-4)], loss={ "yfake": "binary_crossentropy", "yreal": "binary_crossentropy", "xpred": "mean_squared_error" }, player_compile_kwargs=[{ "loss_weights": { "yfake": 1, "yreal": 1, "xpred": 8 } }] * 2) xtrain, xtest = cifar10_data() def generator_sampler(): zsamples = np.random.randn(10 * 10, latent_dim) return dim_ordering_unfix(generator.predict(zsamples)).transpose( (0, 2, 3, 1)).reshape((10, 10, 32, 32, 3)) generator_cb = ImageGridCallback( os.path.join(path, "generated-epoch-{:03d}.png"), generator_sampler) def autoencoder_sampler(): xsamples = n_choice(xtest, 10) xrep = np.repeat(xsamples, 9, axis=0) xgen = dim_ordering_unfix(autoencoder.predict(xrep)).reshape( (10, 9, 3, 32, 32)) xsamples = dim_ordering_unfix(xsamples).reshape((10, 1, 3, 32, 32)) samples = np.concatenate((xsamples, xgen), axis=1) samples = samples.transpose((0, 1, 3, 4, 2)) return samples autoencoder_cb = ImageGridCallback(os.path.join( path, "autoencoded-epoch-{:03d}.png"), autoencoder_sampler, cmap=None) train_datagen = gen_sample(128, 256, False) test_datagen = gen_sample(32, 256, True) history = model.fit_generator(train_datagen, epochs=200, steps_per_epoch=1000, validation_data=test_datagen, validation_steps=100, callbacks=[generator_cb, autoencoder_cb]) # save history df = pd.DataFrame(history.history) df.to_csv(os.path.join(path, "history.csv")) # save model encoder.save(os.path.join(path, "encoder.h5")) generator.save(os.path.join(path, "generator.h5")) discriminator.save(os.path.join(path, "discriminator.h5"))