I am loading a dataset of images using tf.keras.utils.image_dataset_from_directory but I am not sure if the image augmentations are being applied so I want to be able to view some of the images after the augmentation takes place and later for the evaluation. But I am getting an error.
Code:
pixels = 224
IMAGE_SIZE = (pixels, pixels)
BATCH_SIZE = 32
def build_dataset(subset):
return tf.keras.utils.image_dataset_from_directory(
path,
validation_split=.20,
subset=subset,
label_mode="categorical",
seed=123,
image_size=IMAGE_SIZE,
batch_size=1)
def process_image(image, label, img_size):
# cast and normalize image
image = tf.image.convert_image_dtype(image, tf.float32)
# apply simple augmentations
image = tf.image.random_flip_left_right(image)
image = tf.image.resize(image,[img_size, img_size])
return image, label
transforms = A.Compose([
A.Rotate(limit=15),
A.HorizontalFlip(p=0.5),
])
def is_gray_image(image):
return len(image.shape) == 2 or image.shape[-1] == 1
def aug_fn(image, img_size):
data = {"image":image}
aug_data = transforms(**data)
aug_img = aug_data["image"]
aug_img = tf.cast(aug_img/255.0, tf.float32)
aug_img = tf.image.resize(aug_img, size=[img_size, img_size])
return aug_img
def process_data(image, label, img_size):
aug_img = tf.numpy_function(func=aug_fn, inp=[image, img_size], Tout=tf.float32)
return aug_img, label
def set_shapes(img, label, img_shape=(pixels, pixels, 3)):
img.set_shape(img_shape)
label = tf.expand_dims(label, axis=-1)
# label.set_shape([1,])
return img, label
def view_image(ds):
image, label = next(iter(ds)) # extract 1 batch from the dataset
image = image.numpy()
label = label.numpy()
fig = plt.figure(figsize=(22, 22))
for i in range(20):
ax = fig.add_subplot(4, 5, i + 1, xticks=[], yticks=[])
ax.imshow(image[i].astype(dtype=np.uint8))
ax.set_title(f"Label: {label[i]}")
plt.show()
train_data_gen = build_dataset("training")
AUTOTUNE = tf.data.AUTOTUNE
class_names = tuple(train_data_gen.class_names)
# Augmentation
train_data_gen = train_data_gen.map(partial(process_data, img_size = pixels), num_parallel_calls=AUTOTUNE)
# Resize
train_data_gen = train_data_gen.map(set_shapes, num_parallel_calls=AUTOTUNE).repeat().prefetch(AUTOTUNE)
train_size = train_data_gen.cardinality().numpy()
val_data_gen = build_dataset("validation")
# Augmentation
val_data_gen = val_data_gen.map(partial(process_data, img_size = pixels), num_parallel_calls=AUTOTUNE)
# Resize
val_data_gen = val_data_gen.map(set_shapes, num_parallel_calls=AUTOTUNE).repeat().prefetch(AUTOTUNE)
valid_size = val_data_gen.cardinality().numpy()
view_image(train_data_gen)
Output:
Found 28412 files belonging to 164 classes. Using 22730 files for training. Found 28412 files belonging to 164 classes. Using 5682 files for validation.
Error Output:
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
<ipython-input-11-2872f0627e3e> in <module>
----> 1 view_image(train_data_gen)
4 frames
/usr/local/lib/python3.8/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
7162 def raise_from_not_ok_status(e, name):
7163 e.message += (" name: " + name if name is not None else "")
-> 7164 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7165
7166
UnknownError: error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/imgwarp.cpp:2595: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'warpAffine'
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/script_ops.py", line 270, in __call__
ret = func(*args)
File "/usr/local/lib/python3.8/dist-packages/tensorflow/python/autograph/impl/api.py", line 642, in wrapper
return func(*args, **kwargs)
File "<ipython-input-10-f127acf4fdd3>", line 38, in aug_fn
aug_data = transforms(**data)
File "/usr/local/lib/python3.8/dist-packages/albumentations/core/composition.py", line 205, in __call__
data = t(force_apply=force_apply, **data)
File "/usr/local/lib/python3.8/dist-packages/albumentations/core/transforms_interface.py", line 98, in __call__
return self.apply_with_params(params, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/albumentations/core/transforms_interface.py", line 111, in apply_with_params
res[key] = target_function(arg, **dict(params, **target_dependencies))
File "/usr/local/lib/python3.8/dist-packages/albumentations/augmentations/geometric/rotate.py", line 104, in apply
img_out = F.rotate(img, angle, interpolation, self.border_mode, self.value)
File "/usr/local/lib/python3.8/dist-packages/albumentations/augmentations/functional.py", line 172, in wrapped_function
result = func(img, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/albumentations/augmentations/geometric/functional.py", line 124, in rotate
return warp_fn(img)
File "/usr/local/lib/python3.8/dist-packages/albumentations/augmentations/functional.py", line 297, in __process_fn
img = process_fn(img, **kwargs)
cv2.error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/imgwarp.cpp:2595: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'warpAffine'
[[{{node PyFunc}}]] [Op:IteratorGetNext]