对《Python深度学习》的GAN源码,根据自己的理解做了些许修改,发现性能提高很多。
import keras
from keras.constraints import min_max_norm
from keras import layers, initializers
import numpy as np
import os
from keras.preprocessing import image
from keras import backend as K
def wasserstein(y_true, y_pred):
return K.mean(y_true * y_pred)
batch_size = 64
latent_dim = 32
height = 32
width = 32
s = 32
channels = 3
Citers = 5
start_dim = 2
c = 0.01
lr_G = 5e-5
lr_D = 5e-5
f = 512
reshape_shape = (start_dim, start_dim, f)
generator_input = keras.Input(shape=(latent_dim,))
x = layers.Dense(f * start_dim * start_dim, use_bias=False)(generator_input)
x = layers.BatchNormalization(axis=-1)(x)
x = layers.LeakyReLU()(x)
x = layers.Reshape(reshape_shape)(x)
for i in range(3):
nb_filters = int(f / (2 ** (i + 1)))
x = layers.Deconv2D(nb_filters, (3, 3),