Reputation: 25
I am trying to augment (random crop) images while loading them using a tensorflow Dataset. I am getting this error when I call the method tf.image.random_crop in the mapped function:
ValueError: Dimensions must be equal, but are 4 and 3 for '{{node random_crop/GreaterEqual}} = GreaterEqual[T=DT_INT32](random_crop/Shape, random_crop/size)' with input shapes: [4], [3].
In order to reproduce the error, just place some png images in the directory:
./img/class0/
Then run this code:
import os
import tensorflow as tf
train_set_raw = tf.keras.preprocessing.image_dataset_from_directory('./img',label_mode=None,validation_split=None,batch_size=32)
def augment(tensor):
tensor = tf.cast(x=tensor, dtype=tf.float32)
tensor = tf.divide(x=tensor, y=tf.constant(255.))
tensor = tf.image.random_crop(value=tensor, size=(256, 256, 3))
return tensor
train_set_raw = train_set_raw.map(augment).batch(32)
If I specify the batch size explicitly,
tensor = tf.image.random_crop(value=tensor, size=(32,256, 256, 3))
the error can be sorted. However, if you try to fit a model with a dataset created with a fixed batch size, you will get an error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [Need value.shape >= size, got ] [1 256 256 3] [32 256 256 3]
[[{{node random_crop/Assert/Assert}}]]
Upvotes: 1
Views: 1509
Reputation: 36684
Try using a batch size of 1:
tensor = tf.image.random_crop(value=tensor, size=(1,256, 256, 3))
But I don't think you should mix high-level data loaders with a lower level tf.data.Dataset
. Try using only the latter.
import tensorflow as tf
image_dir = r'C:\Users\user\Pictures'
files = tf.data.Dataset.list_files(image_dir + '\\*jpg')
def load(filepath):
image = tf.io.read_file(filepath)
image = tf.image.decode_image(image)
return image
ds = files.map(load)
def augment(tensor):
tensor = tf.cast(x=tensor, dtype=tf.float32)
tensor = tf.divide(x=tensor, y=tf.constant(255.))
tensor = tf.image.random_crop(value=tensor, size=(100, 100, 3))
random_target = tf.random.uniform((1,), dtype=tf.int32, maxval=2)
return tensor, random_target
train_set_raw = ds.map(augment).batch(32)
model = tf.keras.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(8, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy', optimizer='adam')
history = model.fit(train_set_raw)
Upvotes: 1