Python 如何在Tensorflow中解码jpeg后获得图像的形状?

Python 如何在Tensorflow中解码jpeg后获得图像的形状?,python,image,tensorflow,Python,Image,Tensorflow,我有一张图片,我已经将它输入到tf.image.decode_jpeg: img=tf.io.read\u文件(文件路径) img=tf.image.decode\u jpeg(img,通道=3) 我试图得到它的高度和宽度img.shape[0]和img.shape[1],但都返回None。实际上,img.shape返回(无,无,3) 我在一个映射到tf.data.Dataset的函数中使用这个函数。如何获得图像的真实形状 更新: img = tf.io.read_file("sample.j

我有一张图片,我已经将它输入到tf.image.decode_jpeg:

img=tf.io.read\u文件(文件路径)
img=tf.image.decode\u jpeg(img,通道=3)
我试图得到它的高度和宽度
img.shape[0]
img.shape[1]
,但都返回
None
。实际上,
img.shape
返回
(无,无,3)

我在一个映射到
tf.data.Dataset
的函数中使用这个函数。如何获得图像的真实形状

更新:

img = tf.io.read_file("sample.jpg")
img = tf.image.decode_jpeg(img, channels=3)
height = img.shape[0]
width = img.shape[1]

print("Height:",height)
print("Width:",width) 

目前,我已经找到了一个解决方案,其中包括使用
tf.py_函数
包装代码,以便急切地执行它,因为数据集创建了一个内部图。如果有人能用纯图形的方式来实现这一点,我将不胜感激。这将提高性能。

因为您已经找到了一种解决方案,可以通过将代码包装在
tf.py_函数上来获得图像的形状。为社区利益提供解决方案

但是,由于TensorFlow 2中默认启用了“急切执行”,因此您可以像下面提到的那样直接获得形状,而无需将其环绕在
tf.py_函数

Tensorflow 1.x:

img = tf.io.read_file("sample.jpg")
img = tf.image.decode_jpeg(img, channels=3)

with tf.Session() as sess:
  array = img.eval(session=sess)
  height = array.shape[0]
  width = array.shape[1]
  print("Height:",height)
  print("Width:",width) 
身高:320

宽度:320

Tensorflow 2:

img = tf.io.read_file("sample.jpg")
img = tf.image.decode_jpeg(img, channels=3)
height = img.shape[0]
width = img.shape[1]

print("Height:",height)
print("Width:",width) 
身高:320

宽度:320


您好@Diego Palacios,您能分享您试图加载的确切图像吗?您好@TF_支持,我正在使用ILSVRC的图像,但我不再处理此问题。我刚刚使用了带有“tf.py_函数”的解决方案。大家好,Diego Palacios。