Tensorflow图像转换器(Pix2Pix)

时间:2018-08-27 09:05:35

标签: python image tensorflow

我需要有关此TensorFlow项目的帮助。我想重新编程Pix2Pix之类的东西。我遇到了一些问题,尤其是在读取图像并定义TensorFlow图时。但是首先我创建了一个文件夹(“ Source /”),其中有许多不同大小的图片。现在将训练CNN将“ .jpg”图像转换为“ .JPG”图像。 “ jpg”和“ JPG”具有相同的高度,宽度和深度(RGB)。但是,“ JPG”的结构与“ jpg”不同(就像在Pix2Pix中一样)。我认为我的问题是在训练期间出现的,并且我有一个“错误的” TensorFlow图(应该加以改进)。这是我当前的代码:

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

我该怎么做才能改善结果?在定义图形/其余代码时,我实际上错了什么?也许有人可以在TensorFlow中对Pix2Pix进行编程,并且您还可以使用自己的图像吗?

0 个答案:

没有答案