model/ds/nested_tensor.py
def nested_tensor_from_tensor_list(tensor_list: List[Tensor]):
    """
    convert a batch of images with different shape into unified image
    Args:
        tensor_list: the almost case is a batch of images

    Returns:
        NestedTensor: [tensor, mask], mask is designed for representing original tensor in resized tensor

    """
    # TODO make this more general
    if tensor_list[0].ndim == 3:
        if torchvision._is_tracing():
            # nested_tensor_from_tensor_list() does not export well to ONNX
            # call _onnx_nested_tensor_from_tensor_list() instead
            return _onnx_nested_tensor_from_tensor_list(tensor_list)

        # TODO make it support different-sized images
        # get max size of image [c, h, w]
        max_size = _max_by_axis([list(img.shape) for img in tensor_list])
        # min_size = tuple(min(s) for s in zip(*[img.shape for img in tensor_list]))
        # size: [b, max_c, max_h, max_w]
        batch_shape = [len(tensor_list)] + max_size
        b, c, h, w = batch_shape
        dtype = tensor_list[0].dtype
        device = tensor_list[0].device
        # after croping image size
        tensor = torch.zeros(batch_shape, dtype=dtype, device=device)
        mask = torch.ones((b, h, w), dtype=torch.bool, device=device)
        for img, pad_img, m in zip(tensor_list, tensor, mask):
            # if original image size less than cropped size, transformer should focus on original region
            # so we need to create mask for every batch
            pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)
            m[: img.shape[1], :img.shape[2]] = False
    else:
        raise ValueError('not supported')
    return NestedTensor(tensor, mask)

    convert a batch of images with different shape into unified image
    Args:
        tensor_list: the almost case is a batch of images

    Returns:
        NestedTensor: [tensor, mask], mask is designed for representing original tensor in resized tensor

    
convert a batch of images with different shape into unified image
convert a batch of images with different shape into unified image
def nested_tensor_from_tensor_list(tensor_list: List[Tensor]):
    if tensor_list[0].ndim == 3:
        if torchvision._is_tracing():
            return _onnx_nested_tensor_from_tensor_list(tensor_list)
        max_size = _max_by_axis([list(img.shape) for img in tensor_list])
        batch_shape = [len(tensor_list)] + max_size
        b, c, h, w = batch_shape
        dtype = tensor_list[0].dtype
        device = tensor_list[0].device
        tensor = torch.zeros(batch_shape, dtype=dtype, device=device)
        mask = torch.ones((b, h, w), dtype=torch.bool, device=device)
        for img, pad_img, m in zip(tensor_list, tensor, mask):
            pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img)
            m[: img.shape[1], :img.shape[2]] = False
    else:
        raise ValueError('not supported')
    return NestedTensor(tensor, mask)
def nested_tensor_from_tensor_list ( tensor_list : List [ Tensor ] ) : if tensor_list [ 0 ] . ndim == 3 : if torchvision . _is_tracing ( ) : return _onnx_nested_tensor_from_tensor_list ( tensor_list ) max_size = _max_by_axis ( [ list ( img . shape ) for img in tensor_list ] ) batch_shape = [ len ( tensor_list ) ] + max_size b , c , h , w = batch_shape dtype = tensor_list [ 0 ] . dtype device = tensor_list [ 0 ] . device tensor = torch . zeros ( batch_shape , dtype = dtype , device = device ) mask = torch . ones ( ( b , h , w ) , dtype = torch . bool , device = device ) for img , pad_img , m in zip ( tensor_list , tensor , mask ) : pad_img [ : img . shape [ 0 ] , : img . shape [ 1 ] , : img . shape [ 2 ] ] . copy_ ( img ) m [ : img . shape [ 1 ] , : img . shape [ 2 ] ] = False else : raise ValueError ( 'not supported' ) return NestedTensor ( tensor , mask )
convert a batch of images with different shape into unified image
convert a batch of images with different shape into unified image
"""
    convert a batch of images with different shape into unified image
    Args:
        tensor_list: the almost case is a batch of images

    Returns:
        NestedTensor: [tensor, mask], mask is designed for representing original tensor in resized tensor

    """ # TODO make this more general
 # nested_tensor_from_tensor_list() does not export well to ONNX
 # call _onnx_nested_tensor_from_tensor_list() instead
 # TODO make it support different-sized images
 # get max size of image [c, h, w]
 # min_size = tuple(min(s) for s in zip(*[img.shape for img in tensor_list]))
 # size: [b, max_c, max_h, max_w]
 # after croping image size
 # if original image size less than cropped size, transformer should focus on original region
 # so we need to create mask for every batch

[tensor, mask], mask is designed for representing original tensor in resized tensor
[ tensor mask ] mask is designed for representing original tensor in resized tensor
NestedTensor
tensor_list
List[Tensor]
the almost case is a batch of images
the almost case is a batch of images