繁体   English   中英

如何将图像张量保存为 PNG?

[英]How can I save a tensor of images as PNGs?

我有一个看起来像的tensor

Tensor("Identity:0", shape=(10000, 32, 32, 3), dtype=float32)

我想出了如何迭代它:

for adv_x in tf.unstack(adv):
    asnumpy = tf.Session().run(tf.unstack(adv_x))
    print(asnumpy)

这将返回:

...
Tensor("unstack:9997", shape=(32, 32, 3), dtype=float32)
Tensor("unstack:9998", shape=(32, 32, 3), dtype=float32)
Tensor("unstack:9999", shape=(32, 32, 3), dtype=float32)

如何获取每个值并保存到 png?

这是使用python3.7tensorflow1.13

我有一个错误:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value conv2d_1/kernel
         [[{{node conv2d_1/kernel/read}}]]

将其转换为 numpy,然后使用scipy.misc.imsave存储它

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    asnumpy = sess.run(tf.unstack(adv)) # `asnumpy` stores images as numpy arrays

要存储它:

import scipy.misc

for i, image in enumerate(asnumpy):
    scipy.misc.imsave('image' + str(i) + '.png', image)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM