aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.43k stars 2.44k forks source link

AssertionError: AssertionFailed on augment_batches #284

Open vojavocni opened 5 years ago

vojavocni commented 5 years ago

Say I have a list of 10 images X and corresponding 10 masks y. I do the following:

b = ia.Batch(X, segmentation_maps=S)
g = g = seq.augment_batches([b])

next(g)

I get the following error:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-41-e734f8aca5ac> in <module>()
----> 1 next(g)

~/anaconda3/envs/linda/lib/python3.6/site-packages/imgaug/augmenters/meta.py in augment_batches(self, batches, hooks, background)
    266         for i, batch in enumerate(batches):
    267             if isinstance(batch, ia.Batch):
--> 268                 batch_copy = batch.deepcopy()
    269                 batch_copy.data = (i, batch_copy.data)
    270                 batches_normalized.append(batch_copy)

~/anaconda3/envs/linda/lib/python3.6/site-packages/imgaug/imgaug.py in deepcopy(self)
   6851             images=_copy_images(self.images_unaug),
   6852             heatmaps=_copy_augmentable_objects(self.heatmaps_unaug, HeatmapsOnImage),
-> 6853             segmentation_maps=_copy_augmentable_objects(self.segmentation_maps_unaug, SegmentationMapOnImage),
   6854             keypoints=_copy_augmentable_objects(self.keypoints_unaug, KeypointsOnImage),
   6855             bounding_boxes=_copy_augmentable_objects(self.bounding_boxes_unaug, BoundingBoxesOnImage),

~/anaconda3/envs/linda/lib/python3.6/site-packages/imgaug/imgaug.py in _copy_augmentable_objects(augmentables, clazz)
   6844             else:
   6845                 do_assert(is_iterable(augmentables))
-> 6846                 do_assert(all([isinstance(augmentable, clazz) for augmentable in augmentables]))
   6847                 augmentables_copy = [augmentable.deepcopy() for augmentable in augmentables]
   6848             return augmentables_copy

~/anaconda3/envs/linda/lib/python3.6/site-packages/imgaug/imgaug.py in do_assert(condition, message)
   1821     """
   1822     if not condition:
-> 1823         raise AssertionError(str(message))
   1824 
   1825 

AssertionError: Assertion failed.

Not sure what happens here, I think I followed everything I found in the documentation. X is a list of images of size 256x256x3, and y is 256x256x1. Could be a bug. I will also dig a bit more through the code if I have time.

vojavocni commented 5 years ago

UPDATE: It seems the masks are not automatically cast to SegmentationMapOnImage. This means I have to do it manually prior to creating ia.Batch. Is this really necessary? I would find it very convenient to do it under the hood, after all we are explicetely defining them to be segmentation mapsas soon as we insert them in a parameter called segmentation_maps, right?

aleju commented 5 years ago

Yes, imgaug currently only allows images to be arrays and everything else has to be objects. Batch and augment_batches() are the only places where that could be changed. I guess the main reason so far to not do that was simply lack of time to implement it. There is some minor risk though that people call the class/method via positional arguments, then mess up the order of these arguments and get completely wrong results that are hard to debug. Not sure how many people use positional arguments in such cases.

aleju commented 5 years ago

The latest version in master should now support this since PR #290, e.g. via

batch = ia.UnnormalizedBatch(images=<array>, segmentation_maps=<(N, H, W) array>)
batch_aug = seq.augment_batch(batch)

or

images_aug, segmaps_aug = seq.augment(images=<array>, segmentation_maps<(N, H, W) array>)