def METHOD_NAME(
        batch: List[Tuple[Image.Image, Union[Image.Image, np.ndarray]]],
        memory_format: torch.memory_format = torch.contiguous_format) -> Tuple[torch.Tensor, torch.Tensor]:
    """Constructs a length 2 tuple of torch.Tensors from datasets that yield samples of type
    :class:`PIL.Image.Image`.
    This function can be used as the ``collate_fn`` argument of a :class:`torch.utils.data.DataLoader`.
    Args:
        batch (List[Tuple[Image.Image, Union[Image.Image, np.ndarray]]]): List of (image, target) tuples
            that will be aggregated and converted into a single (:class:`~torch.Tensor`, :class:`~torch.Tensor`)
            tuple.
        memory_format (torch.memory_format): The memory format for the input and target tensors.
    Returns:
        (torch.Tensor, torch.Tensor): Tuple of (image tensor, target tensor)
            The image tensor will be four-dimensional (NCHW or NHWC, depending on the ``memory_format``).
    """
    imgs = [sample[0] for sample in batch]
    w, h = imgs[0].size
    image_tensor = torch.zeros((len(imgs), 3, h, w), dtype=torch.uint8).contiguous(memory_format=memory_format)
    # Convert targets to torch tensor
    targets = [sample[1] for sample in batch]
    if isinstance(targets[0], Image.Image):
        target_dims = (len(targets), targets[0].size[1], targets[0].size[0])
    else:
        target_dims = (len(targets),)
    target_tensor = torch.zeros(target_dims, dtype=torch.int64).contiguous(memory_format=memory_format)
    for i, img in enumerate(imgs):
        nump_array = np.asarray(img, dtype=np.uint8)
        if nump_array.ndim < 3:
            nump_array = np.expand_dims(nump_array, axis=-1)
        nump_array = np.rollaxis(nump_array, 2).copy()
        if nump_array.shape[0] != 3:
            assert nump_array.shape[0] == 1, 'unexpected shape'
            nump_array = np.resize(nump_array, (3, h, w))
        assert image_tensor.shape[1:] == nump_array.shape, 'shape mismatch'
        image_tensor[i] += torch.from_numpy(nump_array)
        target_tensor[i] += torch.from_numpy(np.array(targets[i], dtype=np.int64))
    return image_tensor, target_tensor