![](/img/trans.png)
[英]tf.image.decode_jpeg - contents must be scalar, got shape [1]
[英]How to get the shape of an image after decode_jpeg in Tensorflow?
我有一個圖像,我已將其輸入 tf.image.decode_jpeg:
img = tf.io.read_file(file_path)
img = tf.image.decode_jpeg(img, channels=3)
我試圖獲得它的高度和寬度img.shape[0]
和img.shape[1]
,但都返回None
。 實際上, img.shape
返回(None, None, 3)
。
我在映射到tf.data.Dataset
的函數中使用它。 如何獲得圖像的真實形狀?
更新:
目前,我找到了一個解決方案,即用tf.py_function
包裝代碼以tf.py_function
地執行它,因為數據集創建了一個內部圖。 如果有人有另一種解決方案以純圖形方式進行處理,我將不勝感激,這將提高性能。
由於您已經找到了通過將代碼圍繞tf.py_function
來獲取圖像形狀的解決tf.py_function
。 在這里提供解決方案以造福社區。
但是,由於在 TensorFlow 2 中默認啟用了tf.py_function
,因此您可以直接獲得如下所述的形狀,而無需將其包裹在tf.py_function
周圍。
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
張量流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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.