def collate(batch, samples_per_gpu=<NUM_LIT:1>):
if not isinstance(batch, collections.Sequence):<EOL><INDENT>raise TypeError("<STR_LIT>".format(batch.dtype))<EOL><DEDENT>if isinstance(batch[<NUM_LIT:0>], DataContainer):<EOL><INDENT>assert len(batch) % samples_per_gpu == <NUM_LIT:0><EOL>stacked = []<EOL>if batch[<NUM_LIT:0>].cpu_only:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(batch), samples_per_gpu):<EOL><INDENT>stacked.append(<EOL>[sample.data for sample in batch[i:i + samples_per_gpu]])<EOL><DEDENT>return DataContainer(<EOL>stacked, batch[<NUM_LIT:0>].stack, batch[<NUM_LIT:0>].padding_value, cpu_only=True)<EOL><DEDENT>elif batch[<NUM_LIT:0>].stack:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(batch), samples_per_gpu):<EOL><INDENT>assert isinstance(batch[i].data, torch.Tensor)<EOL>assert batch[i].dim() == <NUM_LIT:3><EOL>c, h, w = batch[i].size()<EOL>for sample in batch[i:i + samples_per_gpu]:<EOL><INDENT>assert c == sample.size(<NUM_LIT:0>)<EOL>h = max(h, sample.size(<NUM_LIT:1>))<EOL>w = max(w, sample.size(<NUM_LIT:2>))<EOL><DEDENT>padded_samples = [<EOL>F.pad(<EOL>sample.data,<EOL>(<NUM_LIT:0>, w - sample.size(<NUM_LIT:2>), <NUM_LIT:0>, h - sample.size(<NUM_LIT:1>)),<EOL>value=sample.padding_value)<EOL>for sample in batch[i:i + samples_per_gpu]<EOL>]<EOL>stacked.append(default_collate(padded_samples))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(batch), samples_per_gpu):<EOL><INDENT>stacked.append(<EOL>[sample.data for sample in batch[i:i + samples_per_gpu]])<EOL><DEDENT><DEDENT>return DataContainer(stacked, batch[<NUM_LIT:0>].stack, batch[<NUM_LIT:0>].padding_value)<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], collections.Sequence):<EOL><INDENT>transposed = zip(*batch)<EOL>return [collate(samples, samples_per_gpu) for samples in transposed]<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], collections.Mapping):<EOL><INDENT>return {<EOL>key: collate([d[key] for d in batch], samples_per_gpu)<EOL>for key in batch[<NUM_LIT:0>]<EOL>}<EOL><DEDENT>else:<EOL><INDENT>return default_collate(batch)<EOL><DEDENT>
Puts each data field into a tensor/DataContainer with outer dimension
    batch size.

    Extend default_collate to add support for
    :type:`~mmcv.parallel.DataContainer`. There are 3 cases.

    1. cpu_only = True, e.g., meta data
    2. cpu_only = False, stack = True, e.g., images tensors
    3. cpu_only = False, stack = False, e.g., gt bboxes