Feed training/inference data with DataFeeder¶
Fluid provides the DataFeeder
class, which converts data types such as numpy array into a LoDTensor
type to feed the training/inference network.
To create a DataFeeder
object:
import paddle.fluid as fluid
image = fluid.layers.data(name='image', shape=[-1, 3, 224, 224], dtype='float32')
label = fluid.layers.data(name='label', shape=[-1, 1], dtype='int64')
place = fluid.CUDAPlace(0) if fluid.core.is_compiled_with_cuda() else fluid.CPUPlace()
feeder = fluid.DataFeeder(feed_list=[image, label], place=place)
The feed_list
parameter is a list of variables created by fluid.layers.data()
. The place
parameter indicates that data such as numpy array passed in from the Python side should be converted to GPU or CPU LoDTensor
. After creating the DataFeeder
object, the user can call the feed(iterable)
method to convert iterable
data given by user into LoDTensor
.
iterable
should be a object of Python List or a Tuple type, and each element in iterable
is a Python List of length N or Tuple type object, where N is the number of feed_list
variables passed in when the DataFeeder
object is created.
The concrete format of iterable
is:
iterable = [
(image_1, label_1),
(image_2, label_2),
...
(image_n, label_n)
]
image_i
and label_i
are both numpy array data. If the dimension of the input data is [1], such as label_i
, you can feed Python int, float, and other types of data. The data types and dimensions of image_i
and label_i
are not necessarily the same as dtype
and shape
specified at fluid.layers.data()
. DataFeeder
internally performs the conversion of data types and dimensions. If the lod_level
of the variable in feed_list
is not zero, in Fluid, the 0th dimension of each row in the dimensionally converted iterable
will be returned as LoD
.
Read api_fluid_DataFeeder for specific usage.