Tensorflow数据集:使用不同形状的数据进行批处理

问题描述 投票:0回答:1
import tensorflow_datasets as tfds
import tensorflow as tf

def input_fn():

    dataset_builder = tfds.builder("oxford_flowers102")
    dataset_builder.download_and_prepare()

    ds = dataset_builder.as_dataset(split=tfds.Split.TRAIN)
    ds = ds.repeat()
    ds = ds.batch(32)
    return ds

将导致

InvalidArgumentError: Cannot batch tensors with different shapes in component 1. 
First element had shape [500,666,3] and element 1 had shape [752,500,3]. 
[Op:IteratorGetNextSync]

这可以通过使用调整大小/填充功能来解决,该功能返回与herehere相同形状的图像>

ds = ds.map(resize_or_pad_function)
ds = ds.batch(...)

但是,由于我想保留图像的原始大小和外观,因此我不想调整图像大小或填充图像。它用于训练可以接受各种图像大小的卷积神经网络。

如果我需要一批形状为(32, None, None, 3)的张量,而每个图像的高度和宽度都不同,该怎么办?

作为tfds导入tensorflow_datasets作为tf def input_fn()导入张量流:dataset_builder = tfds.builder(“ oxford_flowers102”)dataset_builder.download_and_prepare()ds = ...

tensorflow tensorflow-datasets
1个回答
1
投票
© www.soinside.com 2019 - 2024. All rights reserved.