【问题标题】:Save tensor as JPEG image with tf.map_fn - Python/TensorFlow使用 tf.map_fn 将张量保存为 JPEG 图像 - Python/TensorFlow
【发布时间】:2018-03-16 12:14:27
【问题描述】:

我有一个函数可以返回一个名为 layer 的变量 - 图像格式:

<tf.Tensor 'Conv2D_1:0' shape=(?, 16, 16, 1) dtype=float32>

我需要将这些图像保存为 .jpeg。

到目前为止,我已经考虑过这样做:

# Reshape into tf.image.encode_jpeg format
images = tf.image.convert_image_dtype(layer, tf.uint8)

train_batch_size = 300

并且在 session = tf.Session()

images_encode = tf.map_fn(lambda x: tf.image.encode_jpeg(x), images, dtype=tf.uint8) # There was no error in this line, is it right?

我现在的疑问是如何配置它来保存它们?

我试过了:

# That means it will only scroll through my 300 images
# And it's these 300 images that I want to save
x_batch, y_true_batch = next_batch_size(train_batch_size)

feed_dict_train = {x: x_batch, y_true: y_true_batch}

result = session.run(images_encode, feed_dict=feed_dict_train)

format_str = ('%s.jpeg')
fr = format_str % datetime.now()
f = open(fr, "wb+")
f.write(result.eval())
f.close()

但我收到以下错误:

InvalidArgumentError (see above for traceback): TensorArray dtype is uint8 but Op is trying to write dtype string.
    [[Node: map_5/while/TensorArrayWrite/TensorArrayWriteV3 = TensorArrayWriteV3[T=DT_STRING, _class=["loc:@map_5/TensorArray_1"], _device="/job:localhost/replica:0/task:0/cpu:0"](map_5/while/TensorArrayWrite/TensorArrayWriteV3/Enter, map_5/while/Identity, map_5/while/EncodeJpeg, map_5/while/Identity_1)]]

我的占位符是:

# Placeholder variable for the input images
x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')

# Reshape 'x'
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])

# Placeholder variable for the true labels associated with the images
y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')

【问题讨论】:

  • 如果您仍然需要帮助或者答案是否解决了您的问题,请告诉我。
  • @Patwie,抱歉耽搁了,这几天一直很忙!

标签: python python-2.7 image-processing tensorflow tensor


【解决方案1】:

你差不多完成了。类型必须是tf.string: 这给出了一个嘈杂的图像:

import tensorflow as tf
import numpy as np

noise = np.random.randn(3, 128, 128, 1).astype(np.float32) * 255
# your data
layer = tf.convert_to_tensor(noise)

images = tf.image.convert_image_dtype(layer, tf.uint8)
images_encode = tf.map_fn(lambda x: tf.image.encode_jpeg(x),
                          images, dtype=tf.string)


def write_jpg(buf, fn):
    with open(fn, 'wb') as f:
        f.write(encoded_jpegs[0])

with tf.Session() as sess:
    encoded_jpegs = sess.run(images_encode)

    for k, jpg in enumerate(encoded_jpegs):
        with open("test%03i.jpg" % k, 'wb') as f:
            f.write(jpg)

【讨论】:

  • 这很有帮助。但这仍然不是我需要的,我需要单独保存 300 张图像中的每一张,例如:test1.jpg, test2.jpg, ..., test300.jpg。我需要它们的原始形式,没有噪音。因为这个想法是稍后生成这些图像的直方图。
  • 您可以随时连接这些图像或使用 tf.py_func
  • 我添加了循环来写入所有图像,它们没有噪音。我只是以噪音为例。 layer 可以是任何图片!
  • 我收到以下错误:InvalidArgumentError: You must feed the value for placeholder 'x' tensor with dtype float
  • 然后使用sess.run(images_encode, {x: numpy_array})
猜你喜欢
  • 2018-03-13
  • 1970-01-01
  • 2019-01-12
  • 2021-02-19
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多