[英]convert "tensorflow.python.framework.ops.EagerTensor" to tensorflow.Tensor or torch.Tensor?
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.这是我的 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
Does anybody know how to convert this or just how to turn a Image to a Tensor with dimensions of 1,28,28?有谁知道如何转换它或如何将图像转换为尺寸为 1、28、28 的张量? Help would really be appreciated
真的很感激帮助
Q: I can't figure out how to convert it to a native f or torch tensor .问:我不知道如何将它转换为原生的 f或torch 张量。
Error: AttributeError: 'Tensor' object has no attribute 'numpy'错误:AttributeError: 'Tensor' object 没有属性 '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 ).您可以通过这一步完成,但您不能在定义中从数组转换为 tf.constant ( 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.
使用 TF1 时不能转换为 NumPy 替代使用 TF1 的“ skimage.transform ”和“Numpy”,使用 float64 时也是 Dtype 兼容性。 The problem is at " image = tf.image.resize (image, [32,32], method='nearest') " it is image cannot convert to tf.constant().
问题出在“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 )
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.
示例(进程之间):您无法访问 function 中的扩展“tf.image.resize”和“tf.image.rgb_to_grayscale”,它们应该用于工作进程。 { image.numpy() |
{ 图片.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()
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()
示例(加载图像):这样你的图像为 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: Image to Numpy in function call.
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.