Home > database >  convert "tensorflow.python.framework.ops.EagerTensor" to tensorflow.Tensor or torch.Tensor
convert "tensorflow.python.framework.ops.EagerTensor" to tensorflow.Tensor or torch.Tensor

Time:11-17

This my function that SHOULD convert an img or jpeg file to a tensor, so that I can then feed it to my AI but it returns a "tensorflow.python.framework.ops.EagerTensor" and I can't figure out how to convert it to a native f or torch tensor.

def imgprocessing(path):
    test_img = image.load_img(path, target_size=(28, 28))
    test_img_array = image.img_to_array(test_img)
    test_img_array = test_img_array / 255.0 # normalize
    test_img_array = tf.image.rgb_to_grayscale(test_img_array) # will return shape (28, 28, 1)
    test_img_array = tf.squeeze(test_img_array, axis = -1) # shape is (28, 28)
    t = tf.expand_dims(test_img_array, axis = 0) # shape: (1, 28, 28)
    t = tf.convert_to_tensor(t, dtype=tf.float32)
    return t

Does anybody know how to convert this or just how to turn a Image to a Tensor with dimensions of 1,28,28? Help would really be appreciated

CodePudding user response:

Q: I can't figure out how to convert it to a native f or torch tensor.

Error: AttributeError: 'Tensor' object has no attribute 'numpy'

You can do it by this step but you may not convert from array to tf.constant within the definition ( tensorflow.python.framework.ops.EagerTensor ). You cannot convert to NumPy when using TF1 alternateuse the "skimage.transform" and "Numpy" for TF1, it is also Dtype compatibility when using float64. The problem is at " image = tf.image.resize(image, [32,32], method='nearest') " it is image cannot convert to tf.constant().

image = plt.imread( file )
image = tf.keras.utils.img_to_array( image )
image = tf.image.resize(image, [32,32], method='nearest')
image = tf.image.rgb_to_grayscale( image )

Sample ( Between process ): You cannot access extending in the function "tf.image.resize" and " tf.image.rgb_to_grayscale ", which are supposed to use for the work process. { image.numpy() | image }

import tensorflow as tf
from skimage.transform import resize
  
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@tf.function
def f(  ):
    image = plt.imread( "F:\\datasets\\downloads\\dark\\train\\01.jpg" )
    image = tf.keras.utils.img_to_array( image )
    image = tf.image.resize(image, [32,32], method='nearest')
    image = tf.image.rgb_to_grayscale( image )
    return image
    
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Tasks
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
print( f(c, d) )

Output: tf.constant()

 ...
 [[ 23.122398]
 [ 19.688301]
 [ 21.9161  ]
 ...
 [ 15.7597  ]
 [ 44.8233  ]
 [ 42.111702]]], shape=(32, 32, 1), dtype=float32)

Sample ( Load Image ): This way you had image as Numpy, I always using when using TF1 but TF2 you can use tf.constant()

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@tf.function
def f(  ):
    image = plt.imread( "F:\\datasets\\downloads\\dark\\train\\01.jpg" )
    image = resize(image, (32, 32))
    image = np.reshape( image, (1, 32, 32, 3) )
    return image

Output: Image to Numpy in function call.

  ...
  [[0.27418377 0.30133097 0.30310639]
   [0.10582442 0.12432269 0.12456823]
   [0.07306318 0.08882116 0.09093407]
   ...
   [0.14883224 0.09423414 0.07170916]
   [0.19801652 0.11498221 0.07868552]
   [0.25829258 0.16194494 0.11493717]]]], shape=(1, 32, 32, 3), dtype=float64)

CodePudding user response:

Al you need is this

def imgprocessing(path):
    test_img_array = tf.image.resize(tf.image.decode_image(tf.io.read_file(path)),(128,128))
    test_img_array = test_img_array / 255 # normalize
    test_img_array = tf.image.rgb_to_grayscale(test_img_array) # will return shape (28, 28, 1)
    test_img_array = tf.squeeze(test_img_array, axis = -1) # shape is (28, 28)
    t = tf.expand_dims(test_img_array, axis = 0) # shape: (1, 28, 28)
    return t
imgprocessing(path)

Output:

<tf.Tensor: shape=(1, 128, 128), dtype=float32, numpy=
array([[[0.26624316, 0.26624316, 0.2666902 , ..., 0.3209216 ,...
  • Related