简体   繁体   English

如何使用tf.image.decode_jpeg返回的图像张量和高度进行检索?

[英]How to retrieve with and height of an image tensor returned by tf.image.decode_jpeg?

I try to setup an images pipeline that builds an images dataset for Tensorflow that crops the images. 我尝试设置一个图像管道,该管道为裁剪图像的Tensorflow构建图像数据集。 I followed this tutorial but I want to crop the file to a square and not resize it without preserving the aspect ratio. 我遵循了本教程,但是我想将文件裁剪为正方形,并且在不保留宽高比的情况下不要调整其大小。 I can't figure out how to get their dimensions. 我不知道如何获得它们的尺寸。

#
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
#

import glob


AUTOTUNE = tf.data.experimental.AUTOTUNE
IMAGE_SIZE = 192


def preprocess_image(path):
    img_raw = tf.io.read_file(path)
    img_tensor = tf.image.decode_jpeg(img_raw, channels=3)
    print("img_tensor")
    print(img_tensor)
    height = img_tensor.shape[0]
    print("height")
    print(height)
    return img_tensor


files_path = glob.glob('./images/*.jpeg')
image_count = len(files_path)
path_ds = tf.data.Dataset.from_tensor_slices(files_path)
path_ds.map(preprocess_image, num_parallel_calls=AUTOTUNE)

The tensor shape returned by tf.image.decode_jpeg is : tf.image.decode_jpeg返回的张量形状为:

Tensor("DecodeJpeg:0", shape=(None, None, 3), dtype=uint8)

How can I access the size of the jpg image ? 如何获取jpg图片的大小?

When I access it this way it works : 当我以这种方式访问​​它时:

#
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
#

image = tf.io.read_file('./images/4c34476047bcbbfd10b1fd3342605659.jpeg/')
image = tf.image.decode_jpeg(image, channels=3)

print("image.shape")
print(image.shape)


It prints : 它打印:

image.shape
(700, 498, 3)

You are facing this issue because the dataset is loaded lazily (only evaluated as it's needed). 由于数据集延迟加载(仅根据需要进行评估),因此您面临此问题。

Inherently, tf can only 'know' the size of the image if it either reads the file or we, as the developer, tell it. 本质上,如果tf读取了文件或者我们(作为开发人员)告诉了文件,则tf只能“知道”图像的大小。 That may seem like an obvious point but it is worth bearing in mind. 这似乎是显而易见的一点,但值得牢记。

So, given that a tf Dataset object can represent arbitrarily large sequences of data (in fact, it's perfectly reasonable to represent infinite datasets this way), by design it doesn't read the files up front. 因此,考虑到tf Dataset对象可以表示任意大的数据序列(实际上,以这种方式表示无限的数据集是完全合理的),因此从设计上讲,它不会预先读取文件。 Rather it reads them each time our downstream code needs a new example or batch. 而是每当我们的下游代码需要新的示例或批处理时,它就会读取它们。

I'm afraid it's really on us to either know the size of the images or to code against all possible sizes up front. 恐怕要知道图像的大小或预先针对所有可能的大小进行编码是我们的责任。

PS The reason you can get the second method to work is that it is eagerly evaluating the (single) tensor example. PS可以使用第二种方法的原因是,它急切地评估了(单个)张量示例。

PPS You perhaps already know that you can "evaluate" the shape of any tensor at execution time with tf.shape() (and use the results of this in your dataset pre-processing pipeline) but you can't inspect it up front PPS您可能已经知道,您可以在执行时使用tf.shape() “评估”任何张量的形状(并在数据集预处理管道中使用此结果),但是您无法预先对其进行检查

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

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