如何访问地图函数内的张量形状

How to access tensor shape inside map function

我需要访问图像形状来执行增强管道,尽管通过 image.shape[0] and image.shape[1] 访问时我无法执行增强,因为它输出我的张量具有形状 None.

相关问题:

如果有人能提供帮助,我们将不胜感激。

parsed_dataset = tf.data.TFRecordDataset(filenames=train_records_paths).map(parsing_fn) # Returns [image,label]
augmented_dataset = parsed_dataset.map(augment_pipeline) 
augmented_dataset = augmented_dataset.unbatch()

映射函数

""" 
    Returns:
      5 Versions of the original image: 4 corner crops + a central crop and the respective labels.
"""
def augment_pipeline(original_image,label):
  central_crop = lambda image: tf.image.central_crop(image,0.5)
  corner_crops = lambda image: tf.image.extract_patches(images=tf.expand_dims(image,0), # Transform image in a batch of single sample
                                                sizes=[1, int(0.5 * image.shape[0]), int(0.5 * image.shape[1]), 1], # 50% of the image's height and width
                                                rates=[1, 1, 1, 1],
                                                strides=[1, int(0.5 * image.shape[0]), int(0.5 * image.shape[1]), 1],
                                                padding="SAME")
  reshaped_patches = tf.reshape(corner_crops(original_image), [-1,int(0.5*original_image.shape[0]),int(0.5*original_image.shape[1]),3])
  images = tf.concat([reshaped_patches,tf.expand_dims(central_crop(original_image),axis=0)],axis=0)
  label = tf.reshape(label,[1,1])
  labels = tf.tile(label,[5,1])
  return images,labels

经过进一步研究,我能够按照建议 here and tf.shape(image)[0] 使用 py_func 进行管理。

代码:

""" 
    Returns:
      5 Versions of the original image: 4 corner crops + a central crop and the respective labels.
"""
def augment_pipeline(original_image,label):
  height  = int(tf.shape(original_image)[0].numpy() * 0.5)  # 50% of the image's height and width
  width = int(tf.shape(original_image)[1].numpy() * 0.5)
  central_crop = lambda image: tf.image.central_crop(image,0.5)
  corner_crops = lambda image: tf.image.extract_patches(images=tf.expand_dims(image,0), # Transform image in a batch of single sample
                                                sizes=[1, height, width, 1],
                                                rates=[1, 1, 1, 1],
                                                strides=[1, height, width, 1],
                                                padding="SAME")

                                              .
                                              .
                                              .

然后我们使用 py_func 允许在 map 函数中访问 numpy 值:

parsed_dataset = tf.data.TFRecordDataset(filenames=train_records_paths).map(parsing_fn) # Returns [image,label]
augmented_dataset = parsed_dataset.map(lambda image,label: tf.py_function(func=augment_pipeline,
                                                                          inp=[image,label],
                                                                          Tout=[tf.float32,tf.int64])) 
augmented_dataset = augmented_dataset.unbatch()

每个数据集对象都是可迭代的。现在 Dataset 对象可以是批处理形式或非批处理形式。我会告诉你如何在这两种情况下获得它们的元素形状。

案例 1. 数据集对象为非批处理形式。

方法一.使用iter消费其元素

it = iter(dataset)
element = next(it)
image,label = element
## element is a tuple

方法2.使用take

element = dataset.take(1)
image,label = element
# element is a tuple

情况2.当数据集被批处理时。现在我假设数据集包含 (image,label) tuples

方法一、使用iter

it = iter(dataset)
batch = next(it)
images,labels = batch
## batch is a tuple check it using type(batch)

方法2.使用take

batch = dataset.take(1)
## Note here each element of the dataset is a batch and each batch contains some number of 
## (image,label) tuples
batch = next(iter(batch))
images,labels = batch
## batch is again a tuple