title: Tensorflow Image Translator (Pix2Pix)
body: <p>I need some help with this TensorFlow project. I would like to reprogram something like Pix2Pix. I encountered some problems, especially when reading in images and defining the TensorFlow graph. But first I created a folder ("Source/"), in which there are many pictures of different sizes. The CNN is now to be trained to turn a".jpg" image into a".JPG" image. The "jpg" and "JPG" have the same height, width and depth(RGB). However, the "JPG" differs in its structure from the "jpg" (as it does in Pix2Pix). I think my problems arise during training and I have a "wrong" TensorFlow graph (one that should be improved). Here is my current code:</p>
  
  <pre><code>import os
  import tensorflow as tf
  import tensorflow.contrib.layers as lays
  
  from random import shuffle
  
  learnrate = 0.1
  batch_size = 32
  
  Source = "Source/"
  files = []
  for file in os.listdir(Source):
      if file.endswith(".jpg"):
          files.append(Source + file)
  shuffle(files)
  
  filenames = tf.constant(files)
  
  safe_name = "test"
  
  data = tf.data.Dataset.from_tensor_slices((filenames,filenames))
  
  data = data.map(parse)
  data = data.batch(batch_size)
  
  def autoencoder(inputs):
      # encoder
      net = lays.conv2d(inputs, 3, [5, 5], stride=1, padding='SAME')
      net = lays.conv2d(net, 6, [5, 5], stride=2, padding='SAME')
      net = lays.conv2d(net, 12, [5, 5], stride=4, padding='SAME')
      # decoder
      net = lays.conv2d_transpose(net, 12, [5, 5], stride=4, padding='SAME')
      net = lays.conv2d_transpose(net, 6, [5, 5], stride=2, padding='SAME')
      net = lays.conv2d_transpose(net, 3, [5, 5], stride=1, padding='SAME', activation_fn=tf.nn.tanh)
      return net
  
  print(files)
  
  ae_inputs = tf.placeholder(tf.float32, (None, 480, 270, 3))
  ae_outputs = autoencoder(ae_inputs)
  
  loss = tf.reduce_mean(tf.square(ae_outputs - ae_inputs))
  train_op = tf.train.AdamOptimizer(learning_rate=learnrate).minimize(loss)
  
  init = tf.global_variables_initializer()
  
  with tf.Session() as sess:
  
      saver = tf.train.Saver()
  
      for x in range(0, 1000):
          iterator = data.make_one_shot_iterator()
          images, labels = iterator.get_next()
          sess.run(init)
          sess.run([images, labels])
  
      file_path = os.path.dirname(os.path.abspath(__file__)) + "/data/" + str(safe_name) + ".ckpt"
      saver.save(sess, file_path)
      print("Safe File! - Training done!")
  
  def parse(filename,filenames):
      image_string = tf.read_file(filename)
      image_decoded = tf.image.decode_jpeg(image_string, channels=3)
      image = tf.cast(image_decoded, tf.float32)
      label_string = tf.read_file(filename.replace(".jpg", ".JPG"))
      label_decoded = tf.image.decode_jpeg(label_string, channels=3)
      label = tf.cast(label_decoded, tf.float32)
      return image, label
  </code></pre>
  
  <p>What can I do to improve my results? And what did I actually do wrong when defining the graph/the rest of the code? Is there perhaps a well understandable example where someone has programmed Pix2Pix in TensorFlow and in which you can also use your own images?</p>