【问题标题】:Convert tf.Tensor to numpy array and than save it as image in without eager_execution将 tf.Tensor 转换为 numpy 数组,然后将其保存为图像而不使用 eager_execution
【发布时间】:2021-06-05 10:33:14
【问题描述】:

我的 OC 对于苹果 M1 来说是很大的,因此我的 tensorflow 版本是 2.4,它是从苹果官方 github repo(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 获取图像?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

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

    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 才能使该函数正常工作,我不确定。

    【讨论】:

    • 这是什么?请在此处包含重要信息。我什至无法打开链接(域被阻止)
    • 这个信息很大,如果下面的链接也不适合你,我会把它包括在这里。 controlc.com/9bbd7a57
    • 我不使用急切执行。
    • 你为什么不呢?
    • 2021-03-09 21:48:19.587997: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] 未启用 MLIR 优化通道(已注册 2)2021-03-09 21 :48:19.639121: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] 无法获取 CPU 频率:0 Hz
    【解决方案2】:

    要从 tensorflow 张量创建一个 numpy 数组,您可以使用 `make_ndarray' :https://www.tensorflow.org/api_docs/python/tf/make_ndarray

    make_ndarray 将原始张量作为参数,因此您必须先将张量转换为原始张量

    proto_tensor = tf.make_tensor_proto(a) # convert tensor a to a proto tensor

    (https://www.geeksforgeeks.org/tensorflow-how-to-create-a-tensorproto/)

    Convert a tensor to numpy array in Tensorflow?

    张量必须是形状(img_height, img_width, 3)3 如果要生成 RGB 图像(3 通道),请参阅以下代码以使用 PIL 将 numpy aaray 转换为图像

    要从 numpy 数组生成图像,您可以使用 PIL(Python 图像库):How do I convert a numpy array to (and display) an image?

    from PIL import Image
    import numpy as np
    img_w, img_h = 200, 200
    data = np.zeros((img_h, img_w, 3), dtype=np.uint8)  <- zero np_array depth 3 for RGB
    data[100, 100] = [255, 0, 0]    <- fille array with 255,0,0 in RGB
    img = Image.fromarray(data, 'RGB')    <- array to image (all black then)
    img.save('test.png')
    img.show()
    

    来源:https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-109.php

    【讨论】:

    • 是的,我无法将 Tensor 类转换为 numpy 数组,这是主要问题。我尝试使用 make_ndarray,但没有成功。 pastebin.com/fhMRBN7y
    • 说如果 pastebin 对你也不起作用
    • make_ndarray 将原始张量作为参数,因此您必须先将张量转换为原始张量,我在上面进行了更改...
    【解决方案3】:

    这是你照顾的老式方式吗?

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(sr)
    

    【讨论】:

    • @Ymka 抱歉,缺少初始化。现在怎么说?
    • 你可能想import tensorflow.compat.v1 as tf 来代替完整的TF1。
    猜你喜欢
    • 1970-01-01
    • 2020-12-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多