gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:01:23 25 4
gpt4 key购买 nike

我有一个张量,如下所示:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com