【问题标题】:Save tensor as JPEG image - Python/TensorFlow将张量保存为 JPEG 图像 - Python/TensorFlow
【发布时间】:2018-03-13 14:35:15
【问题描述】:

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

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

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

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

# Reshape into tf.image.encode_jpeg format
images = tf.reshape(tf.cast(layer, tf.uint8), [16, 16, 1])

# Encode
images_encode = tf.image.encode_jpeg(images)

# Create a files name
fname = tf.constant('datetime.now() + ".jpeg"')

# Write files
fwrite = tf.write_file(fname, images_encode)

train_batch_size = 300

并且在 session = tf.Session()

# That means it will only scroll through my 300 images...
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(fwrite, feed_dict=feed_dict_train)

但我收到以下错误:

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 76800 values, but the requested shape has 256
    [[Node: Reshape_7 = Reshape[T=DT_UINT8, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"](Cast_7, Reshape_7/shape)]]

我的占位符是:

# 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')

知道如何解决这个问题,或者我可以应用一些其他方法来保存图像吗?

【问题讨论】:

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


    【解决方案1】:

    您使用的批量大小为 76800 / 256 = 300

    reshape 函数正在尝试将整个批次重塑为 (16,16,1) 张量:这是不可能的。

    如果要保存单张图片,有两种选择:

    1. 提取批处理的第一个元素tf.reshape(tf.cast(layer[0], tf.uint8), [16, 16, 1])
    2. 将批量大小设置为 1

    如果您想保存整批图像,则必须遍历该批图像(使用 tf.map_fn)并对图像进行单独编码(因为 tf.image.encode_jpeg 适用于单个图像)。然后,从 python 中提取每个编码的图像并将其保存到磁盘。

    【讨论】:

    • 谢谢!我测试了保存单个图像,它可以工作,但它全是黑色的,有什么办法修复它吗?
    • 不要使用tf.cast,而是使用tf.image.convert_image_dtype (tensorflow.org/api_docs/python/tf/image/convert_image_dtype)。 tf.cast 粗暴地将值从一种类型转换为另一种类型(您通常使用 0,1 范围内的浮动图像),您需要将它们放大到 0.255。您可以使用 convert_image_dtype 或手动进行缩放,然后使用 tf.cast
    • 谢谢!这很有帮助。
    猜你喜欢
    • 2018-03-16
    • 1970-01-01
    • 2019-01-12
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多