gpt4 book ai didi

python - 将 tf.Tensor 转换为 numpy 数组,然后将其另存为图像而不需要eager_execution

转载 作者:行者123 更新时间:2023-12-03 17:22:31 31 4
gpt4 key购买 nike

我的 OC 是苹果 M1 的大 sur,因此我的 tensorflow 版本是 2.4,已从官方苹果 github 存储库(https://github.com/apple/tensorflow_macos)安装。当我使用下面的代码时,我得到 tensor( )

import tensorflow as tf
import tensorflow_hub as hub
from PIL import Image
import numpy as np

from tensorflow.python.compiler.mlcompute import mlcompute
from tensorflow.python.framework.ops import disable_eager_execution
disable_eager_execution()
mlcompute.set_mlc_device(device_name='gpu') # Available options are 'cpu', 'gpu', and 'any'.
tf.config.run_functions_eagerly(False)
print(tf.executing_eagerly())

image = np.asarray(Image.open('/Users/alex26/Downloads/face.jpg'))
image = tf.cast(image, tf.float32)
image = tf.expand_dims(image, 0)

model = hub.load("https://tfhub.dev/captain-pool/esrgan-tf2/1")
sr = model(image) #<tf.Tensor 'StatefulPartitionedCall:0' shape=(1, 2880, 4320, 3)dtype=float32>
如何从 sr Tensor 获取图像?

最佳答案

如果你急切地执行它会起作用:

import tensorflow as tf
import numpy as np
import tensorflow_hub as hub

model = hub.load("https://tfhub.dev/captain-pool/esrgan-tf2/1")

x = np.random.rand(1, 224, 224, 3).astype(np.float32)

image = model(x)
那么你可以使用 tf.keras.preprocessing.image.save_img 保存生成的图像。您可能需要将结果乘以 255并转换为 np.uint8要使该功能正常工作,我不确定。

关于python - 将 tf.Tensor 转换为 numpy 数组,然后将其另存为图像而不需要eager_execution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66514664/

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