简体   繁体   English

将 tf.Tensor 转换为 numpy 数组,然后在不使用 eager_execution 的情况下将其保存为图像

[英]Convert tf.Tensor to numpy array and than save it as image in without eager_execution

My OC is big sur for apple M1, therefore my tensorflow version is 2.4 which has been installed from official apple github repo( https://github.com/apple/tensorflow_macos ).我的 OC 对于苹果 M1 来说是很大的,因此我的 tensorflow 版本是 2.4,它是从苹果官方 github repo 安装的( https://github.com When i use code bellow, i get tensor(<tf.Tensor 'StatefulPartitionedCall:0' shape=(1, 2880, 4320, 3) dtype=float32>)当我使用下面的代码时,我得到 tensor(<tf.Tensor 'StatefulPartitionedCall:0' shape=(1, 2880, 4320, 3) dtype=float32>)

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>

How to get image from sr Tensor?如何从 sr Tensor 获取图像?

To create an numpy array from a tensorflow tensor you can use `make_ndarray': https://www.tensorflow.org/api_docs/python/tf/make_ndarray To create an numpy array from a tensorflow tensor you can use `make_ndarray': https://www.tensorflow.org/api_docs/python/tf/make_ndarray

make_ndarray takes proto tensor as argument so you have to convert the tensor into a proto tensor first 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/ ) https://www.geeksforgeeks.org/tensorflow-how-to-create-a-tensorproto/

Convert a tensor to numpy array in Tensorflow? 将张量转换为 Tensorflow 中的 numpy 数组?

the tensor has to be if shape (img_height, img_width, 3) , the 3 if you want to generate an RGB image (3 channels), see the following code to convert an numpy aaray to an image using PIL张量必须是 if shape (img_height, img_width, 3) 3如果要生成 RGB 图像(3 个通道),则为 3,请参阅以下代码以使用PIL将 numpy aaray 转换为图像

To generate an image from the numpy array then you can use PIL (Python Imaging Library): How do I convert a numpy array to (and display) an image?要从 numpy 数组生成图像,您可以使用PIL (Python 图像库): 如何将 numpy 数组转换为(并显示)图像?

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()

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

If you execute eagerly it works:如果您急切地执行它,它会起作用:

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)

Then you can use tf.keras.preprocessing.image.save_img to save the resulting image.然后您可以使用tf.keras.preprocessing.image.save_img来保存生成的图像。 You may have to multiply the result by 255 and convert to np.uint8 for that function to work, I'm not sure.您可能必须将结果乘以255并转换为np.uint8才能使 function 工作,我不确定。

Is this the old fashioned way that you are looking after?这是你正在照顾的老式方式吗?

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

暂无
暂无

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

相关问题 将 tf.tensor 转换为 numpy - convert tf.tensor to numpy Tensorflow 2.0 使用 tf.compat.v1.disable_eager_execution() 时无法将张量转换为 numpy 数组 - Tensorflow 2.0 Cannot convert tensor to numpy array when using tf.compat.v1.disable_eager_execution() 如何将tf.Tensor从最大池化层转换为numpy数组? - How to convert tf.Tensor from max pooling layer into numpy array? How to implement a numpy equation in the call of a tensorflow layer for a tensorflow model(无法将符号 tf.Tensor 转换为 numpy 数组) - How to implement a numpy equation in the call of a tensorflow layer for a tensorflow model (Cannot convert a symbolic tf.Tensor to a numpy array) 如何在 TF2.x 中临时关闭/打开 eager_execution? - How to temporarily turn off/on eager_execution in TF2.x? Graph 执行中不允许使用 `tf.Tensor` 作为 Python `bool`。 使用 Eager execution 或使用 @tf.function 装饰此函数 - using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function 在 Graph 执行中不允许迭代 `tf.Tensor`。 使用 Eager 执行或使用 @tf.function 装饰此 function。 Function 到数据集 - Iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function. Function to dataset 在 Graph 执行中不允许迭代 `tf.Tensor`。 使用 Eager 执行或使用 @tf.function 装饰此 function - iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function 将张量转换为 numpy 没有急切模式 - Convert to numpy a tensor without eager mode TensorFlow:将 tf.Dataset 转换为 tf.Tensor - TensorFlow: convert tf.Dataset to tf.Tensor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM