Bug? Properly using images and masks with tensorflow datasets - cv2.error in function 'warpAffine' when applying Rotate, CLAHE, OpticalDistortion..
See original GitHub issueHi , I am having difficulties to run a segmentation task, where I use images and masks. Things that I tried:
def transform(image, mask):
augmentations = A.Compose(
[
A.HorizontalFlip(p = 0.5), # Apply horizontal flip to 50% of images
A.OneOf(
[
A.RandomContrast(),
A.RandomGamma(),
A.RandomBrightness(),
],
p = 0.5
),
A.Rotate(p=0.3),
A.CLAHE (p=0.5),
]
)
return augmentations(image=image)['image'], augmentations(mask=mask)['mask']
def prepare(ds, augment=True):
ds = ds.map(resize_and_rescale, num_parallel_calls=opt.AUTOTUNE)
# Batch dataset
ds = ds.batch(opt.BATCH_SIZE)
if augment:
ds = ds.map(transform, num_parallel_calls=opt.AUTOTUNE)
return ds.prefetch(buffer_size=opt.AUTOTUNE)
train_ds = prepare(train_ds)
This results in : TypeError: image must be numpy array type
and I can’t find a way to overcome this.
Another approach that I used:
def aug_fn_img(image):
data = {"image": image}
aug_data = transform(**data)
aug_img = aug_data["image"]
return aug_img
def aug_fn_mask(mask):
data = {'mask': mask}
aug_data = transform(**data)
aug_mask = aug_data["mask"]
return aug_mask
def process_aug(image, mask):
aug_img = tf.numpy_function(func=aug_fn_img, inp=[image],
Tout=[tf.float32])
aug_mask = tf.numpy_function(func=aug_fn_mask, inp=[mask],
Tout=[tf.float32])
return aug_img, aug_mask
and then call if augment: ds = ds.map(process_aug, num_parallel_calls=opt.AUTOTUNE)
it gives me : ValueError: as_list() is not defined on an unknown TensorShape.
How can I deal with images and masks with tensorflow?
Issue Analytics
- State:
- Created a year ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Bug with masks · Issue #290 · albumentations-team ... - GitHub
Bug The masks do not follow the rotation of the images To Reproduce Steps to reproduce the behavior: 1.Define the following transformation: ...
Read more >OpenCV warpAffine error during image augmentation using ...
But I got some error from OpenCV while transforming the images. I ran the code below on Kaggle's notebook. The dataset is called...
Read more >Augmentation With Albumentations - Kaggle
Doing data augmentation for image segmentation is not as easy as for classification, because you have two images: the actual image and the...
Read more >COVID-19: Face Mask Detector with OpenCV, Keras ...
We will use the dataset to build a COVID-19 face mask detector with computer ... rotate and resize the mask, and then apply...
Read more >Image, Video and Real-Time Webcam Object Detection ...
In this tutorial, we will explore Mask R-CNN to understand how instance segmentation works, then implement object detection and instance ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think that the main problem is that you are trying to augment images after you batch them (Albumentations process one image/mask at a time). The proper way would be:
Also, you may find a bit better way to resize your images. Maybe you can try something from Albumentations like: LongestMaxSize + PadIfNeeded. But this all is your choice.
Thanks @Andredance ! It works fine now!