image_loader
allennlp.data.image_loader
OnePath¶
OnePath = Union[str, PathLike]
ManyPaths¶
ManyPaths = Sequence[OnePath]
ImagesWithSize¶
ImagesWithSize = Tuple[FloatTensor, IntTensor]
ImageLoader¶
class ImageLoader(Registrable):
| def __init__(
| self,
| *, size_divisibility: int = 0,
| *, pad_value: float = 0.0,
| *, device: Union[str, torch.device] = "cpu"
| ) -> None
An ImageLoader
is a callable that takes as input one or more filenames, and outputs two
tensors: one representing the images themselves, and one that just holds the sizes
of each image.
The first tensor is the images and is of shape (batch_size, color_channels, height, width)
.
The second tensor is the sizes and is of shape (batch_size, 2)
, where
the last dimension contains the height and width, respectively.
If only a single image is passed (as a Path
or str
, instead of a list) then
the batch dimension will be removed.
Subclasses only need to implement the load()
method, which should load a single image
from a path.
Parameters¶
-
size_divisibility :
int
, optional (default =0
)
If set to a positive number, padding will be added so that the height and width dimensions are divisible bysize_divisibility
. Certain models may require this. -
pad_value :
float
, optional (default =0.0
)
The value to use for padding. -
device :
Union[str, torch.device]
, optional (default ="cpu"
)
A torch device identifier to put the image and size tensors on.
default_implementation¶
class ImageLoader(Registrable):
| ...
| default_implementation = "torch"
__call__¶
class ImageLoader(Registrable):
| ...
| def __call__(
| self,
| filename_or_filenames: Union[OnePath, ManyPaths]
| ) -> ImagesWithSize
load¶
class ImageLoader(Registrable):
| ...
| def load(self, filename: OnePath) -> FloatTensor
TorchImageLoader¶
@ImageLoader.register("torch")
class TorchImageLoader(ImageLoader):
| def __init__(
| self,
| *, image_backend: str = None,
| *, resize: bool = True,
| *, normalize: bool = True,
| *, min_size: int = 800,
| *, max_size: int = 1333,
| *, pixel_mean: Tuple[float, float, float] = (0.485, 0.456, 0.406),
| *, pixel_std: Tuple[float, float, float] = (0.229, 0.224, 0.225),
| *, size_divisibility: int = 32,
| **kwargs,
| *, ,
| ) -> None
This is just a wrapper around the default image loader from torchvision.
Parameters¶
- image_backend :
Optional[str]
, optional (default =None
)
Set the image backend. Can be one of"PIL"
or"accimage"
. - resize :
bool
, optional (default =True
)
IfTrue
(the default), images will be resized when necessary according to the values ofmin_size
andmax_size
. - normalize :
bool
, optional (default =True
)
IfTrue
(the default), images will be normalized according to the values ofpixel_mean
andpixel_std
. - min_size :
int
, optional (default =800
)
Ifresize
isTrue
, images smaller than this will be resized up tomin_size
. - max_size :
int
, optional (default =1333
)
Ifresize
isTrue
, images larger than this will be resized down tomax_size
. - pixel_mean :
Tuple[float, float, float]
, optional (default =(0.485, 0.456, 0.406)
)
Mean values for image normalization. The defaults are reasonable for most models fromtorchvision
. - pixel_std :
Tuple[float, float, float]
, optional (default =(0.229, 0.224, 0.225)
)
Standard deviation for image normalization. The defaults are reasonable for most models fromtorchvision
. - size_divisibility :
int
, optional (default =32
)
Same parameter as with theImageLoader
base class, but the default here is different.
load¶
class TorchImageLoader(ImageLoader):
| ...
| def load(self, filename: OnePath) -> FloatTensor