(tensor_list: List[Tensor])
| 47 | |
| 48 | |
| 49 | def nested_tensor_from_tensor_list(tensor_list: List[Tensor]): |
| 50 | # TODO make this more general |
| 51 | if tensor_list[0].ndim == 3: |
| 52 | if torchvision._is_tracing(): |
| 53 | # nested_tensor_from_tensor_list() does not export well to ONNX |
| 54 | # call _onnx_nested_tensor_from_tensor_list() instead |
| 55 | return _onnx_nested_tensor_from_tensor_list(tensor_list) |
| 56 | |
| 57 | # TODO make it support different-sized images |
| 58 | max_size = _max_by_axis([list(img.shape) for img in tensor_list]) |
| 59 | # min_size = tuple(min(s) for s in zip(*[img.shape for img in tensor_list])) |
| 60 | batch_shape = [len(tensor_list)] + max_size |
| 61 | b, c, h, w = batch_shape |
| 62 | dtype = tensor_list[0].dtype |
| 63 | device = tensor_list[0].device |
| 64 | tensor = torch.zeros(batch_shape, dtype=dtype, device=device) |
| 65 | mask = torch.ones((b, h, w), dtype=torch.bool, device=device) |
| 66 | for img, pad_img, m in zip(tensor_list, tensor, mask): |
| 67 | pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img) |
| 68 | m[: img.shape[1], : img.shape[2]] = False |
| 69 | else: |
| 70 | raise ValueError("not supported") |
| 71 | return NestedTensor(tensor, mask) |
| 72 | |
| 73 | |
| 74 | # _onnx_nested_tensor_from_tensor_list() is an implementation of |
nothing calls this directly
no test coverage detected