Adding AdditiveGaussianNoise to a single image - AssertionError Expected boolean as argument for 'return_batch'
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This error usually appears when imgaug receives a positional argument where it expects a boolean flag named return_batch. For a single image, the fix is to call the augmenter with a named image argument or use the single-image helper method instead of calling the augmenter like a plain function.
Why the error happens
In imgaug, augment() is a keyword-oriented API. The library expects calls such as augment(image=...) or augment(images=...). If you call the augmenter as aug(resized_img), the array is treated as the first positional parameter, which in that API slot corresponds to return_batch.
That is why the error message says it expected a boolean. The library is not complaining about the image content yet. It is complaining that the function was called with the wrong argument style.
The correct way to augment one image
Here is a working example with one image. It uses image= for a single input and also converts the resized result into a dtype that matches a pixel-scale noise setting.
You can also use images=[image] if you want batch-style behavior and then index the first result. The key point is that the input must be passed by name.
Single image versus batch input
There are two valid patterns:
- '
aug(image=image)for one image' - '
aug(images=[image])for a batch-like call'
The batch form is useful when your code path already expects a list or array of images. The single-image form is more direct when you are just testing or building a small preprocessing step.
Using the correct call signature also makes the code easier to read. Anyone reviewing it can immediately tell whether the augmenter is being applied to one image or a batch.
Watch the dtype and value range
There is a second issue that often follows the API fix. skimage.transform.resize commonly returns floating-point data, often in the zero-to-one range, while the noise scale in many imgaug examples is expressed in pixel units based on zero to 255.
If you keep the resized image as floating-point zero-to-one data but use scale=(0, 0.2 * 255), the noise is far larger than you probably intend. Either preserve pixel range and cast to uint8, or keep the floating-point representation and scale the noise to match that range.
That mismatch does not cause the return_batch assertion, but it does cause bad augmentations once the call starts working.
Common Pitfalls
- Calling
aug(image_array)positionally instead of usingimage=orimages=. - Fixing the API call but leaving the image in zero-to-one float format while using a zero-to-255 noise scale.
- Forgetting that
resizemay change dtype and range unless you manage them deliberately. - Using batch mode accidentally and then being confused by the extra leading dimension.
- Debugging the augmenter parameters first when the initial error is only about argument passing.
Summary
- The
return_batchassertion usually meansimgaugreceived a positional image argument in the wrong API slot. - Pass a single image with
image=imageor a batch withimages=[image]. - The library is expecting named arguments, not a raw array as the first positional input.
- After fixing the call, check the image dtype and value range so the noise scale still makes sense.
- For single-image augmentation, the named single-image call is the clearest option.
Related reading
- Affine transformation algorithm
- AlexNet architecture for black and white image identification
- Algorithm challenge Generate color scheme from an image
- Algorithm for calculating inverse color
- Adding custom jars to pyspark in jupyter notebook
- Adding days to a date in Python
- Adding an anonymous Task to ListTask does not execute it after calling .WaitAll C
- Adding information to an exception?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.