6

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). When i use code bellow, i get 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?

3 Answers 3

0

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. You may have to multiply the result by 255 and convert to np.uint8 for that function to work, I'm not sure.

Sign up to request clarification or add additional context in comments.

7 Comments

What is this? Please include important information here. I can't even open the link (domain blocked)
this information is big, i'll include it here if link bellow also doesn't work for you. controlc.com/9bbd7a57
i don't use eagerly execution.
why are you not?
2021-03-09 21:48:19.587997: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2) 2021-03-09 21:48:19.639121: W tensorflow/core/platform/profile_utils/cpu_utils.cc:126] Failed to get CPU frequency: 0 Hz
|
0

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

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?

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

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?

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

3 Comments

Yup, i can't convert Tensor class to numpy array, that's the main problem. I tried to use make_ndarray, but it hasn't worked. pastebin.com/fhMRBN7y
say if pastebin also doesn't work for you
make_ndarray takes proto tensor as argument so you have to convert the tensor into a proto tensor first, i changed this above...
0

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)

2 Comments

@Ymka Sorry, initialization was missing. What does it say now?
You might want to import tensorflow.compat.v1 as tf instead to go full TF1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.