def augment_batches(self, batches, hooks=None, background=False):
if isinstance(batches, (Batch, UnnormalizedBatch)):<EOL><INDENT>batches = [batches]<EOL><DEDENT>ia.do_assert(<EOL>(ia.is_iterable(batches)<EOL>and not ia.is_np_array(batches)<EOL>and not ia.is_string(batches))<EOL>or ia.is_generator(batches),<EOL>("<STR_LIT>"<EOL>+ "<STR_LIT>") % (type(batches),))<EOL>if background:<EOL><INDENT>ia.do_assert(<EOL>hooks is None,<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>def _normalize_batch(idx, batch):<EOL><INDENT>if isinstance(batch, Batch):<EOL><INDENT>batch_copy = batch.deepcopy()<EOL>batch_copy.data = (idx, batch_copy.data)<EOL>batch_normalized = batch_copy<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch, UnnormalizedBatch):<EOL><INDENT>batch_copy = batch.to_normalized_batch()<EOL>batch_copy.data = (idx, batch_copy.data)<EOL>batch_normalized = batch_copy<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif ia.is_np_array(batch):<EOL><INDENT>ia.do_assert(<EOL>batch.ndim in (<NUM_LIT:3>, <NUM_LIT:4>),<EOL>("<STR_LIT>"<EOL>+ "<STR_LIT>") % (batch.shape,))<EOL>batch_normalized = Batch(images=batch, data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch, list):<EOL><INDENT>if len(batch) == <NUM_LIT:0>:<EOL><INDENT>batch_normalized = Batch(data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif ia.is_np_array(batch[<NUM_LIT:0>]):<EOL><INDENT>batch_normalized = Batch(images=batch, data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], ia.HeatmapsOnImage):<EOL><INDENT>batch_normalized = Batch(heatmaps=batch, data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], ia.SegmentationMapOnImage):<EOL><INDENT>batch_normalized = Batch(segmentation_maps=batch,<EOL>data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], ia.KeypointsOnImage):<EOL><INDENT>batch_normalized = Batch(keypoints=batch, data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], ia.BoundingBoxesOnImage):<EOL><INDENT>batch_normalized = Batch(bounding_boxes=batch, data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], ia.PolygonsOnImage):<EOL><INDENT>batch_normalized = Batch(polygons=batch, data=(idx,))<EOL>batch_orig_dt = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>raise Exception(<EOL>("<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>") % (type(batch[<NUM_LIT:0>]),))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise Exception(<EOL>("<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>") % (type(batch),))<EOL><DEDENT>if batch_orig_dt not in ["<STR_LIT>",<EOL>"<STR_LIT>"]:<EOL><INDENT>ia.warn_deprecated(<EOL>("<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>"<EOL>+ "<STR_LIT>") % (<EOL>batch_orig_dt,))<EOL><DEDENT>return batch_normalized, batch_orig_dt<EOL><DEDENT>def _unnormalize_batch(batch_aug, batch_orig, batch_orig_dt):<EOL><INDENT>if batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug<EOL>batch_unnormalized.data = batch_unnormalized.data[<NUM_LIT:1>]<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_aug.data = batch_aug.data[<NUM_LIT:1>]<EOL>batch_unnormalized =batch_orig.fill_from_augmented_normalized_batch(batch_aug)<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug.images_aug<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = []<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug.images_aug<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug.heatmaps_aug<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug.segmentation_maps_aug<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug.keypoints_aug<EOL><DEDENT>elif batch_orig_dt == "<STR_LIT>":<EOL><INDENT>batch_unnormalized = batch_aug.bounding_boxes_aug<EOL><DEDENT>else:  <EOL><INDENT>ia.do_assert(batch_orig_dt == "<STR_LIT>")<EOL>batch_unnormalized = batch_aug.polygons_aug<EOL><DEDENT>return batch_unnormalized<EOL><DEDENT>if not background:<EOL><INDENT>for idx, batch in enumerate(batches):<EOL><INDENT>batch_normalized, batch_orig_dt = _normalize_batch(idx, batch)<EOL>batch_normalized = self.augment_batch(<EOL>batch_normalized, hooks=hooks)<EOL>batch_unnormalized = _unnormalize_batch(<EOL>batch_normalized, batch, batch_orig_dt)<EOL>yield batch_unnormalized<EOL><DEDENT><DEDENT>else:<EOL><INDENT>import imgaug.multicore as multicore<EOL>id_to_batch_orig = dict()<EOL>def load_batches():<EOL><INDENT>for idx, batch in enumerate(batches):<EOL><INDENT>batch_normalized, batch_orig_dt = _normalize_batch(<EOL>idx, batch)<EOL>id_to_batch_orig[idx] = (batch, batch_orig_dt)<EOL>yield batch_normalized<EOL><DEDENT><DEDENT>with multicore.Pool(self) as pool:<EOL><INDENT>for batch_aug in pool.imap_batches(load_batches()):<EOL><INDENT>idx = batch_aug.data[<NUM_LIT:0>]<EOL>assert idx in id_to_batch_orig<EOL>batch_orig, batch_orig_dt = id_to_batch_orig[idx]<EOL>batch_unnormalized = _unnormalize_batch(<EOL>batch_aug, batch_orig, batch_orig_dt)<EOL>del id_to_batch_orig[idx]<EOL>yield batch_unnormalized<EOL><DEDENT><DEDENT><DEDENT>
Augment multiple batches.

In contrast to other augment functions, this function _yields_ batches
instead of just returning a full list. This is more suited for most
training loops. It also supports augmentation on multiple cpu cores,
activated via the `background` flag.

Parameters
----------
batches : imgaug.augmentables.batches.Batch \
          or imgaug.augmentables.batches.UnnormalizedBatch \
          or iterable of imgaug.augmentables.batches.Batch \
          or iterable of imgaug.augmentables.batches.UnnormalizedBatch
    A single batch or a list of batches to augment.

hooks : None or imgaug.HooksImages, optional
    HooksImages object to dynamically interfere with the augmentation
    process.

background : bool, optional
    Whether to augment the batches in background processes.
    If true, hooks can currently not be used as that would require
    pickling functions.
    Note that multicore augmentation distributes the batches onto
    different CPU cores. It does not split the data within batches.
    It is therefore not sensible to use ``background=True`` for a
    single batch.
    Note also that multicore augmentation needs some time to start. It
    is therefore not recommended to use it for very few batches.

Yields
-------
imgaug.augmentables.batches.Batch \
          or imgaug.augmentables.batches.UnnormalizedBatch \
          or iterable of imgaug.augmentables.batches.Batch \
          or iterable of imgaug.augmentables.batches.UnnormalizedBatch
    Augmented batches.