Home > Net >  AttributeError: 'BatchDataset' object has no attribute 'reshape' when loading a
AttributeError: 'BatchDataset' object has no attribute 'reshape' when loading a

Time:07-17

I am trying to load a custom dataset to Tensorflow like this:

# Load the dataset
train_images = tf.keras.utils.image_dataset_from_directory(
  './data/',
  seed=123,
  image_size=(250, 250),
  batch_size=batch_size)

test_images = tf.keras.utils.image_dataset_from_directory(
  './data/',
  seed=123,
  image_size=(250, 250),
  batch_size=batch_size)

This is my dataset structure:

data
    train
        a.jpg
        b.jpg
        c.jpg
    test
        1.jpg
        2.jpg
        3.jpg

But I'm getting the following error:

Found 13233 files belonging to 2 classes.
Found 13233 files belonging to 2 classes.
<BatchDataset element_spec=(TensorSpec(shape=(None, 250, 250, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None,), dtype=tf.int32, name=None))>
<BatchDataset element_spec=(TensorSpec(shape=(None, 250, 250, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None,), dtype=tf.int32, name=None))>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-4381d053064c> in <module>()
    144 
    145 # Add noise for condition input
--> 146 train_images = train_images.reshape(train_images.shape[0], raw_size, raw_size, channels)
    147 train_inputs = add_gaussian_noise(train_images, stdev=0.2, data_range=(0, 255)).astype('float32')
    148 train_inputs = normalise(train_inputs, (-1, 1), (0, 255))

AttributeError: 'BatchDataset' object has no attribute 'reshape'

This is due to the fact that I am trying to process my dataset like this:

# Add noise for condition input
train_images = train_images.reshape(train_images.shape[0], raw_size, raw_size, channels)
train_inputs = add_gaussian_noise(train_images, stdev=0.2, data_range=(0, 255)).astype('float32')
train_inputs = normalise(train_inputs, (-1, 1), (0, 255))
train_images = normalise(train_images, (-1, 1), (0, 255))
train_labels = train_images.astype('float32')

train_dataset = tf.data.Dataset.from_tensor_slices((train_inputs, train_labels)).shuffle(buffer_size).batch(batch_size)

# Test set
test_images = test_images.reshape(test_images.shape[0], raw_size, raw_size, channels)
test_inputs = add_gaussian_noise(test_images, stdev=0.2, data_range=(0, 255)).astype('float32')
test_inputs = normalise(test_inputs, (-1, 1), (0, 255))
test_images = normalise(test_images, (-1, 1), (0, 255))
test_labels = test_images.astype('float32')

Where am I going wrong? I get that BatchDataset does not have a function .reshape() but how can I go about fixing this?

CodePudding user response:

You have to use tf.reshape(), Dataset doesn't contain a reshape() method.

train_images = tf.reshape(train_images, newShape)

Where newShape is a tuple containing your parameters.

  • Related