| 307 | |
| 308 | |
| 309 | def nested_tensor_from_tensor_list(tensor_list: List[Tensor]): |
| 310 | # TODO make this more general |
| 311 | if tensor_list[0].ndim == 3: |
| 312 | # TODO make it support different-sized images |
| 313 | max_size = _max_by_axis([list(img.shape) for img in tensor_list]) |
| 314 | # min_size = tuple(min(s) for s in zip(*[img.shape for img in tensor_list])) |
| 315 | batch_shape = [len(tensor_list)] + max_size |
| 316 | b, c, h, w = batch_shape |
| 317 | dtype = tensor_list[0].dtype |
| 318 | device = tensor_list[0].device |
| 319 | tensor = torch.zeros(batch_shape, dtype=dtype, device=device) |
| 320 | mask = torch.ones((b, h, w), dtype=torch.bool, device=device) |
| 321 | for img, pad_img, m in zip(tensor_list, tensor, mask): |
| 322 | pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img) |
| 323 | m[: img.shape[1], :img.shape[2]] = False |
| 324 | else: |
| 325 | raise ValueError('not supported') |
| 326 | return NestedTensor(tensor, mask) |
| 327 | |
| 328 | |
| 329 | class NestedTensor(object): |