繁体   English   中英

将“tensorflow.python.framework.ops.EagerTensor”转换为 tensorflow.Tensor 或 torch.Tensor?

[英]convert "tensorflow.python.framework.ops.EagerTensor" to tensorflow.Tensor or torch.Tensor?

这是我的 function,它应该将 img 或 jpeg 文件转换为张量,以便我可以将其提供给我的 AI,但它返回“tensorflow.python.framework.ops.EagerTensor”,我不知道如何转换它是一个原生的 f 或 torch 张量。

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

有谁知道如何转换它或如何将图像转换为尺寸为 1、28、28 的张量? 真的很感激帮助

问:我不知道如何将它转换为原生的 ftorch 张量

错误:AttributeError: 'Tensor' object 没有属性 'numpy'

您可以通过这一步完成,但您不能在定义中从数组转换为 tf.constant ( tensorflow.python.framework.ops.EagerTensor )。 使用 TF1 时不能转换为 NumPy 替代使用 TF1 的“ skimage.transform ”和“Numpy”,使用 float64 时也是 Dtype 兼容性。 问题出在“image = tf.image.resize (image, [32,32], method='nearest')”,图像无法转换为 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 )

示例(进程之间):您无法访问 function 中的扩展“tf.image.resize”和“tf.image.rgb_to_grayscale”,它们应该用于工作进程。 { 图片.numpy() | 图片 }

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)

示例(加载图像):这样你的图像为 Numpy,我在使用 TF1 时总是使用但 TF2 你可以使用 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:在 function 通话中将图像发送到 Numpy。

  ...
  [[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)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM