MCPcopy Create free account
hub / github.com/WeChatCV/WeVisionOne / nested_tensor_from_tensor_list

Function nested_tensor_from_tensor_list

WeVisionOne/utils/misc.py:49–71  ·  view source on GitHub ↗
(tensor_list: List[Tensor])

Source from the content-addressed store, hash-verified

47
48
49def 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

Callers

nothing calls this directly

Calls 3

_max_by_axisFunction · 0.85
NestedTensorClass · 0.85

Tested by

no test coverage detected