MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / det_collate

Function det_collate

PATH/core/solvers/solver_multitask_dev.py:1332–1433  ·  view source on GitHub ↗

r""" Function that takes in a batch of data and puts the elements within the batch into a tensor with an additional outer dimension - batch size. The exact output type can be a :class:`torch.Tensor`, a `Sequence` of :class:`torch.Tensor`, a Collection of :class:`torch

(batch)

Source from the content-addressed store, hash-verified

1330
1331
1332def det_collate(batch): # altered collate_fn to support 'Instance' object within batch
1333 r"""
1334 Function that takes in a batch of data and puts the elements within the batch
1335 into a tensor with an additional outer dimension - batch size. The exact output type can be
1336 a :class:`torch.Tensor`, a `Sequence` of :class:`torch.Tensor`, a
1337 Collection of :class:`torch.Tensor`, or left unchanged, depending on the input type.
1338 This is used as the default function for collation when
1339 `batch_size` or `batch_sampler` is defined in :class:`~torch.utils.data.DataLoader`.
1340
1341 Here is the general input type (based on the type of the element within the batch) to output type mapping:
1342 * :class:`torch.Tensor` -> :class:`torch.Tensor` (with an added outer dimension batch size)
1343 * NumPy Arrays -> :class:`torch.Tensor`
1344 * `float` -> :class:`torch.Tensor`
1345 * `int` -> :class:`torch.Tensor`
1346 * `str` -> `str` (unchanged)
1347 * `bytes` -> `bytes` (unchanged)
1348 * `Mapping[K, V_i]` -> `Mapping[K, det_collate([V_1, V_2, ...])]`
1349 * `NamedTuple[V1_i, V2_i, ...]` -> `NamedTuple[det_collate([V1_1, V1_2, ...]), det_collate([V2_1, V2_2, ...]), ...]`
1350 * `Sequence[V1_i, V2_i, ...]` -> `Sequence[det_collate([V1_1, V1_2, ...]), det_collate([V2_1, V2_2, ...]), ...]`
1351
1352 Args:
1353 batch: a single batch to be collated
1354
1355 Examples:
1356 >>> # Example with a batch of `int`s:
1357 >>> det_collate([0, 1, 2, 3])
1358 tensor([0, 1, 2, 3])
1359 >>> # Example with a batch of `str`s:
1360 >>> det_collate(['a', 'b', 'c'])
1361 ['a', 'b', 'c']
1362 >>> # Example with `Map` inside the batch:
1363 >>> det_collate([{'A': 0, 'B': 1}, {'A': 100, 'B': 100}])
1364 {'A': tensor([ 0, 100]), 'B': tensor([ 1, 100])}
1365 >>> # Example with `NamedTuple` inside the batch:
1366 >>> Point = namedtuple('Point', ['x', 'y'])
1367 >>> det_collate([Point(0, 0), Point(1, 1)])
1368 Point(x=tensor([0, 1]), y=tensor([0, 1]))
1369 >>> # Example with `Tuple` inside the batch:
1370 >>> det_collate([(0, 1), (2, 3)])
1371 [tensor([0, 2]), tensor([1, 3])]
1372 >>> # Example with `List` inside the batch:
1373 >>> det_collate([[0, 1], [2, 3]])
1374 [tensor([0, 2]), tensor([1, 3])]
1375 """
1376 elem = batch[0]
1377 elem_type = type(elem)
1378 if isinstance(elem, torch.Tensor):
1379 if elem.ndim == 3:
1380 return nested_tensor_from_tensor_list(batch)
1381 else:
1382 out = None
1383 if torch.utils.data.get_worker_info() is not None:
1384 # If we're in a background process, concatenate directly into a
1385 # shared memory tensor to avoid an extra copy
1386 numel = sum(x.numel() for x in batch)
1387 storage = elem.storage()._new_shared(numel)
1388 out = elem.new(storage).resize_(len(batch), *list(elem.size()))
1389 return torch.stack(batch, 0, out=out)

Callers

nothing calls this directly

Calls 3

sizeMethod · 0.80
stackMethod · 0.80

Tested by

no test coverage detected