1
votes

I want to feed a tf.data Dataset to a Keras model, but I get the following error:

AttributeError: 'DatasetV1Adapter' object has no attribute 'ndim'

This dataset will be used to solve a segmentation problem, so both input and output will be images (3D tensors)

The dataset is created with this code:

dataset = tf.data.Dataset.list_files(TRAIN_PATH + "*.png",shuffle=False)

def process_path(file_path):
  img = tf.io.read_file(file_path)
  img = tf.image.decode_png(img, channels=3)
  train_image_path=tf.strings.regex_replace(file_path,"image","mask")
  mask = tf.io.read_file(train_image_path)
  mask = tf.image.decode_png(mask, channels=1)
  mask = tf.squeeze(mask)
  mask = tf.one_hot(tf.cast(mask, tf.int32), Num_Classes, axis = -1)
  return img,mask

dataset = dataset.map(process_path)
dataset = dataset.batch(32,drop_remainder=True)

Taking an item from the dataset shows that I get a tuple containing an input tensor and an output tensor, whose dimensions are correct:

Input: (batch-size, image height, image width, 3 channels)

Output: (batch-size, image height, image width, 4 channels)

When fitting the model I get the error:

model.fit(dataset, epochs = 50)
1
Which tensorflow version are you using? There is no tf.image.decode_png attribute in TF2.2. Try using tf.io.decode_png or tf.io.decode_image - Dwij Mehta
When you pass a dataset to fit, Keras expects it to return tuples of batches, either (inputs, targets) or (inputs, targets, sample_weights). Your dataset returns dicts of batches. - jdehesa
@jdehesa, even replacing the dictionary with a tuple leads to the same error. I've replaced "return {"input":img,"output":mask}", with return img, mask, with no success. - MarcoM
@DwijMehta, tf.image.decode_png seems to work fine: I get the image. TF version is 2.0.0 - MarcoM
@jdehesa: I edited the question putting the version without the dictionary output - MarcoM

1 Answers

1
votes

I've solved the provem moving to Keras 2.4.3 and Tensorflow 2.2

Everything was right but apparently the previous release of Keras did not manage this tf.data correctly.

Here's a tutorial I've found very useful on this.