How to use tf.data.Dataset.padded_batch with a nested shape?

2024/9/8 9:27:15

I am building a dataset with two tensors of shape [batch,width,heigh,3] and [batch,class] for each element. For simplicity lets say class = 5.

What shape do you feed to dataset.padded_batch(1000,shape) such that image is padded along the width/height/3 axis?

I have tried the following:

tf.TensorShape([[None,None,None,3],[None,5]])
[tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])]
[[None,None,None,3],[None,5]]
([None,None,None,3],[None,5])
(tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5])‌​)

Each raising TypeError

The docs state:

padded_shapes: A nested structure of tf.TensorShape or tf.int64 vectortensor-like objects representing the shape to which the respectivecomponent of each input element should be padded prior to batching.Any unknown dimensions (e.g. tf.Dimension(None) in a tf.TensorShape or-1 in a tensor-like object) will be padded to the maximum size of that dimension in each batch.

The relevant code:

dataset = tf.data.Dataset.from_generator(generator,tf.float32)
shapes = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))
batch = dataset.padded_batch(1,shapes)
Answer

Thanks to mrry for finding the solution. Turns out that the type in from_generator has to match the number of tensors in the entries.

new code:

dataset = tf.data.Dataset.from_generator(generator,(tf.float32,tf.float32))
shapes = (tf.TensorShape([None,None,None,3]),tf.TensorShape([None,5]))
batch = dataset.padded_batch(1,shapes)
https://en.xdnf.cn/q/72939.html

Related Q&A

Python, thread and gobject

I am writing a program by a framework using pygtk. The main program doing the following things:Create a watchdog thread to monitor some resource Create a client to receive data from socket call gobjec…

How to type annotate overrided methods in a subclass?

Say I already have a method with type annotations:class Shape:def area(self) -> float:raise NotImplementedErrorWhich I will then subclass multiple times:class Circle:def area(self) -> float:retur…

Import Error: No module named pytz after using easy_install

Today is my first day at Python and have been going through problems. One that I was working on was, "Write a short program which extracts the current date and time from the operating system and p…

Python catch exception pandas.errors.ParserError: Error tokenizing data. C error

I am facing problem with my malfunction csv input file whole reading and which i can deal with by adding "error_bad_lines=False" in my read_csv func to remove those.But i need to report these…

Nested tags in BeautifulSoup - Python

Ive looked at many examples on websites and on stackoverflow but I couldnt find a universal solution to my question. Im dealing with a really messy website and Id like to scrape some data. The markup l…

How do I check if a string is a negative number before passing it through int()?

Im trying to write something that checks if a string is a number or a negative. If its a number (positive or negative) it will passed through int(). Unfortunately isdigit() wont recognize it as a numbe…

openpyxl chage font size of title y_axis.title

I am currently struggling with changing the font of y axis title & the charts title itself.I have tried to create a font setting & applying it to the titles - with no luck what so ever. new_cha…

Combination of all possible cases of a string

I am trying to create a program to generate all possible capitalization cases of a string in python. For example, given abcedfghij, I want a program to generate: Abcdefghij ABcdef.. . . aBcdef.. . ABCD…

How to change download directory location path in Selenium using Chrome?

Im using Selenium in Python and Im trying to change the download path. But either this: prefs = {"download.default_directory": "C:\\Users\\personal\\Downloads\\exports"} options.add…

Keras, TensorFlow : TypeError: Cannot interpret feed_dict key as Tensor

I am trying to use keras fune-tuning to develop image classify applications. I deployed that application to a web server and the image classification is succeeded.However, when the application is used …