我有一个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)

关于python - 如何将张量图像另存为PNG?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55714528/

10-12 00:13
查看更多