image_list.py 875 Bytes
Newer Older
1
2
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import torch
eellison's avatar
eellison committed
3
4
from torch.jit.annotations import List, Tuple
from torch import Tensor
5
6
7
8
9
10
11
12
13
14
15


class ImageList(object):
    """
    Structure that holds a list of images (of possibly
    varying sizes) as a single tensor.
    This works by padding the images to the same size,
    and storing in a field the original sizes of each image
    """

    def __init__(self, tensors, image_sizes):
16
        # type: (Tensor, List[Tuple[int, int]]) -> None
17
18
19
20
21
22
23
24
        """
        Arguments:
            tensors (tensor)
            image_sizes (list[tuple[int, int]])
        """
        self.tensors = tensors
        self.image_sizes = image_sizes

eellison's avatar
eellison committed
25
    def to(self, device):
26
        # type: (Device) -> ImageList # noqa
eellison's avatar
eellison committed
27
        cast_tensor = self.tensors.to(device)
28
        return ImageList(cast_tensor, self.image_sizes)