image_list.py 742 Bytes
Newer Older
1
import torch
eellison's avatar
eellison committed
2
from torch import Tensor
3
from typing import List, Tuple
4
5
6
7
8
9
10
11
12
13


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
    """

14
    def __init__(self, tensors: Tensor, image_sizes: List[Tuple[int, int]]):
15
        """
16
        Args:
17
18
19
20
21
22
            tensors (tensor)
            image_sizes (list[tuple[int, int]])
        """
        self.tensors = tensors
        self.image_sizes = image_sizes

23
    def to(self, device: torch.device) -> 'ImageList':
eellison's avatar
eellison committed
24
        cast_tensor = self.tensors.to(device)
25
        return ImageList(cast_tensor, self.image_sizes)