MCPcopy Create free account
hub / github.com/THUYimingLi/BackdoorBox / any2tensor

Function any2tensor

core/utils/any2tensor.py:36–50  ·  view source on GitHub ↗

Convert strpath, PIL.Image.Image, numpy.ndarray, torch.Tensor image(s) to a torch.Tensor. Args: imgs (list[strpath] | list[PIL.Image.Image] | list[numpy.ndarray] | list[torch.Tensor] | strpath | PIL.Image.Image | numpy.ndarray | torch.Tensor): The input images. Returns: tor

(imgs)

Source from the content-addressed store, hash-verified

34
35
36def any2tensor(imgs):
37 """Convert strpath, PIL.Image.Image, numpy.ndarray, torch.Tensor image(s) to a torch.Tensor.
38
39 Args:
40 imgs (list[strpath] | list[PIL.Image.Image] | list[numpy.ndarray] | list[torch.Tensor] | strpath | PIL.Image.Image | numpy.ndarray | torch.Tensor): The input images.
41
42 Returns:
43 torch.Tensor: The converted image(s).
44 """
45 if isinstance(imgs, list):
46 return torch.stack([_any2tensor(img) for img in imgs], dim=0)
47 elif isinstance(imgs, (str, PIL.Image.Image, numpy.ndarray, torch.Tensor)):
48 return _any2tensor(imgs)
49 else:
50 raise TypeError('imgs is an unsupported type, imgs should be list[strpath] | list[PIL.Image.Image] | list[numpy.ndarray] | list[torch.Tensor] | strpath | PIL.Image.Image | numpy.ndarray | torch.Tensor. But got {}'.format(type(imgs)))

Callers 2

testFunction · 0.90
testFunction · 0.90

Calls 1

_any2tensorFunction · 0.85

Tested by 2

testFunction · 0.72
testFunction · 0.72