1

I want to use TensorFlow to create a GAN. I have managed to create these parts of code. My images are of shape [299, 299, 3] because I took some images and resized them using TensorFlow and saves them so that all my images are of the same shape.

Now I am trying to generate more such images.

The error now is

Traceback (most recent call last):
File "D:/Development_Avector/PycharmProjects/TensorFlow/gan.py", line 41, in <module>
DgL = discriminator(G_sample, reuse=True)
File "D:/Development_Avector/PycharmProjects/TensorFlow/gan.py", line 31, in discriminator
Z1 = tf.layers.conv2d(x, kernel_size=5,filters=64, strides=2, padding='SAME')
File "D:\Development_Avecto\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\layers\convolutional.py", line 551, in conv2d
return layer.apply(inputs)
File "D:\Development_Avecto\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\layers\base.py", line 492, in apply
return self.__call__(inputs, *args, **kwargs)
File "D:\Development_Avecto\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\layers\base.py", line 428, in __call__
self._assert_input_compatibility(inputs)
File "D:\Development_Avecto\Anaconda\envs\tensorflow\lib\site-packages\tensorflow\python\layers\base.py", line 540, in _assert_input_compat
ibility
str(x.get_shape().as_list()))
ValueError: Input 0 of layer conv2d_5 is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 6]

This is the code.

# The part that discriminates
X = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name='X')


# The part that generates
Z = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name='Z')



def generator(z,reuse=False):
    with tf.variable_scope('generator',reuse=reuse):
        #z = tf.reshape(z, shape=[-1, 299, 299, 64])
        Z1 = tf.layers.conv2d(z,kernel_size=5,filters=64, strides=2, padding='SAME')
        A1 = tf.nn.relu(Z1)
        Z2 = tf.layers.conv2d(A1,kernel_size=5, filters=64, strides=1, padding='SAME')
        A2 = tf.nn.relu(Z2)
        P2 = tf.contrib.layers.flatten(A2)
        Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn=None)
        return Z3


def discriminator(x,reuse=False):
    with tf.variable_scope('discriminator',reuse=reuse):
        #x = tf.reshape(x, shape=[-1, 299, 299, 3])
        Z1 = tf.layers.conv2d(x, kernel_size=5,filters=64, strides=2, padding='SAME')
        A1 = tf.nn.relu(Z1)
        Z2 = tf.layers.conv2d(A1, kernel_size=5,filters=64, strides=1, padding='SAME')
        A2 = tf.nn.relu(Z2)
        P2 = tf.contrib.layers.flatten(A2)
        Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn=None)
        return Z3

G_sample = generator(Z)
DxL = discriminator(X)
DgL = discriminator(G_sample, reuse=True)
print (DxL)
D_Disc_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = DxL, labels = tf.ones_like(DxL)))
D_Disc_loss1 = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = DgL, labels = tf.ones_like(DgL)))
D_MainLoss = D_Disc_loss + D_Disc_loss1
G_Generate_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = DgL, labels = tf.ones_like(DgL)))

D_loss = tf.summary.scalar("Discriminator Loss", D_MainLoss)
G_loss = tf.summary.scalar("Generator Loss", G_Generate_loss)
merge = tf.summary.merge_all()

variables = tf.trainable_variables()
dvariables = [var for var in variables if var.name.startswith("discriminator")]
print (dvariables)
gvariables = [var for var in variables if var.name.startswith("generator")]
print (gvariables)

D_optimizer = tf.train.AdamOptimizer().minimize(D_Disc_loss, var_list=dvariables)
G_optimizer = tf.train.AdamOptimizer().minimize(G_Generate_loss, var_list=gvariables)

The part that trains

def train():
filenames = tf.train.string_input_producer(
    tf.train.match_filenames_once("D:/Development_Avecto/TensorFlow/resizedimages/*.png"))
reader = tf.WholeFileReader()
_, input = reader.read(filenames)
input = tf.Print(input,[input,tf.shape(input),"Input shape"])
input = tf.image.decode_png(input, channels=3)
input.set_shape([299, 299, 3])

batch = tf.train.batch([input],
                       batch_size=2)

init = (tf.global_variables_initializer(), tf.local_variables_initializer())

with tf.Session() as sess:
    sess.run(init)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    train_writer = tf.summary.FileWriter('D:/Development_Avecto/TensorFlow/logs/1/train', sess.graph)


    for it in range(50):
        _, X_batch =  sess.run([input,batch])
        summary,_, DiscriminatorLoss = sess.run([merge,D_optimizer, D_Disc_loss], feed_dict={X: X_batch})
        summary,_, GeneratorLoss = sess.run([merge,G_optimizer, G_Generate_loss])

        train_writer.add_summary(summary, it)
        train_writer.flush()

train_writer.close()
coord.request_stop()
coord.join(threads)
sess.close()

There is a problem with the shape of X and Z. The first commented line in the functions 'discriminator' and 'generator'. How should the shape be ? Can anyone explain ?

Update : I have refactored the question.

Green Falcon
  • 14,058
  • 9
  • 57
  • 98

2 Answers2

0
#x = tf.reshape(x, shape=[-1, 299, 299, 3])

change the above line to the following code snippet.

dim = x.get_shape().as_list()
x = tf.reshape(x, shape = [-1, *dim[1:]])

The same behavior applies for z and Z1.

Green Falcon
  • 14,058
  • 9
  • 57
  • 98
0

I want to post the working code here as an answer. I had to read and understand many concepts to make the code work. The is working but my training process isn't thoroughly vetted but that is a different subject.

I had to revise my understanding of these subjects.

  1. Convolutions

  2. Deconvolutions(What are deconvolutional layers?)

  3. Basic TensorFlow API and shapes

This is the main part that tripped me. So I am posting that alone.

X = tf.placeholder(tf.float32, shape=[None, 299, 299, 3], name='X')


Z = tf.placeholder(dtype=tf.float32,
                              shape=(None, 100),
                              name='Z')

filters = [2, 2, 3, 32]
weights = tf.Variable(tf.truncated_normal(filters, stddev=0.03))

This is just basic code but it helped me answer my original question.

def generator(z,reuse=False):
    with tf.variable_scope('generator',reuse=reuse):
        linear = tf.layers.dense(z, 1024 * 4 * 4)
        conv = tf.reshape(linear, (-1, 4, 4, 1024))
        out = tf.layers.conv2d_transpose(conv, 3,kernel_size=4,strides=[2,2], padding='SAME')
        out = tf.nn.relu(out)

        tf.nn.tanh(out)
        return out


def discriminator(x,reuse=False):
    with tf.variable_scope('discriminator',reuse=reuse):
        out = tf.nn.conv2d(x, weights, [1, 1, 1, 1], padding='SAME')
        out = tf.nn.relu(out)
        out = tf.nn.max_pool(out, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],padding='SAME')
        out = tf.layers.dense(out, units=128, activation=tf.nn.relu)
        out = tf.layers.dense(out, units=1, activation=tf.nn.sigmoid)
        return out