diff --git a/README.md b/README.md index 41ac810..0af1b19 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,6 @@ Set `train = False` to visualize how adjusting the initial noise affects image g Set `cifar = True` to train on CIFAR. This sets the network to train on 32x32 images instead of 64x64, and reads from the CIFAR binaries rather than from JPEGS. +To download the CIFAR dataset, just run `wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz` and `tar -xzvf cifar-10-python.tar.gz` Utils and network structure from [DCGAN-Tensorflow](https://github.com/carpedm20/DCGAN-tensorflow). diff --git a/main.py b/main.py index a2d9065..ea85e03 100644 --- a/main.py +++ b/main.py @@ -8,25 +8,25 @@ from scipy.misc import imsave as ims from random import randint -cifar = False +cifar = True def discriminator(image, reuse=False): - if reuse: - tf.get_variable_scope().reuse_variables() if cifar: - h0 = lrelu(conv2d(image, 3, df_dim, name='d_h0_conv')) #16x16x64 - h1 = lrelu(d_bn1(conv2d(h0, df_dim, df_dim*2, name='d_h1_conv'))) #8x8x128 - h2 = lrelu(d_bn2(conv2d(h1, df_dim*2, df_dim*4, name='d_h2_conv'))) #4x4x256 - h4 = dense(tf.reshape(h2, [batchsize, -1]), 4*4*df_dim*4, 1, scope='d_h3_lin') - return tf.nn.sigmoid(h4), h4 + with tf.variable_scope(tf.get_variable_scope(), reuse=reuse) as scope: + h0 = lrelu(conv2d(image, 3, df_dim, name='d_h0_conv')) #16x16x64 + h1 = lrelu(d_bn1(conv2d(h0, df_dim, df_dim*2, name='d_h1_conv'))) #8x8x128 + h2 = lrelu(d_bn2(conv2d(h1, df_dim*2, df_dim*4, name='d_h2_conv'))) #4x4x256 + h4 = dense(tf.reshape(h2, [batchsize, -1]), 4*4*df_dim*4, 1, scope='d_h3_lin') + return tf.nn.sigmoid(h4), h4 else: - h0 = lrelu(conv2d(image, 3, df_dim, name='d_h0_conv')) - h1 = lrelu(d_bn1(conv2d(h0, 64, df_dim*2, name='d_h1_conv'))) - h2 = lrelu(d_bn2(conv2d(h1, 128, df_dim*4, name='d_h2_conv'))) - h3 = lrelu(d_bn3(conv2d(h2, 256, df_dim*8, name='d_h3_conv'))) - h4 = dense(tf.reshape(h3, [batchsize, -1]), 4*4*512, 1, scope='d_h3_lin') - return tf.nn.sigmoid(h4), h4 + with tf.variable_scope(tf.get_variable_scope(), reuse=reuse) as scope: + h0 = lrelu(conv2d(image, 3, df_dim, name='d_h0_conv')) + h1 = lrelu(d_bn1(conv2d(h0, 64, df_dim*2, name='d_h1_conv'))) + h2 = lrelu(d_bn2(conv2d(h1, 128, df_dim*4, name='d_h2_conv'))) + h3 = lrelu(d_bn3(conv2d(h2, 256, df_dim*8, name='d_h3_conv'))) + h4 = dense(tf.reshape(h3, [batchsize, -1]), 4*4*512, 1, scope='d_h3_lin') + return tf.nn.sigmoid(h4), h4 def generator(z): if cifar: @@ -82,10 +82,10 @@ def generator(z): D_fake_prob, D_fake_logit = discriminator(G, reuse=True) - d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(D_logit, tf.ones_like(D_logit))) - d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(D_fake_logit, tf.zeros_like(D_fake_logit))) + d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_logit, labels=tf.ones_like(D_logit))) + d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_fake_logit, labels=tf.zeros_like(D_fake_logit))) - gloss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(D_fake_logit, tf.ones_like(D_fake_logit))) + gloss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_fake_logit, labels=tf.ones_like(D_fake_logit))) dloss = d_loss_real + d_loss_fake t_vars = tf.trainable_variables() @@ -102,7 +102,7 @@ def generator(z): d_optim = tf.train.AdamOptimizer(learningrate, beta1=beta1).minimize(dloss, var_list=d_vars) g_optim = tf.train.AdamOptimizer(learningrate, beta1=beta1).minimize(gloss, var_list=g_vars) - tf.initialize_all_variables().run() + tf.global_variables_initializer().run() saver = tf.train.Saver(max_to_keep=10) @@ -111,10 +111,11 @@ def generator(z): display_z = np.random.uniform(-1, 1, [batchsize, z_dim]).astype(np.float32) - realfiles = data[0:64] - realim = [get_image(batch_file, [64,64,3], is_crop=False) for batch_file in realfiles] - real_img = np.array(realim).astype(np.float32) - ims("results/imagenet/real.jpg",merge(real_img,[8,8])) + if not cifar: + realfiles = data[0:64] + realim = [get_image(batch_file, [64,64,3], is_crop=False) for batch_file in realfiles] + real_img = np.array(realim).astype(np.float32) + ims("results/imagenet/real.jpg",merge(real_img,[8,8])) train = True if train: diff --git a/ops.py b/ops.py index b324472..1676858 100644 --- a/ops.py +++ b/ops.py @@ -15,7 +15,7 @@ def __call__(self, x, train=True): shape = x.get_shape().as_list() if train: - with tf.variable_scope(self.name) as scope: + with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE) as scope: self.beta = tf.get_variable("beta", [shape[-1]], initializer=tf.constant_initializer(0.)) self.gamma = tf.get_variable("gamma", [shape[-1]],