import torch


def threshold_resize_torch(preds, shape, threshold=0.5):
    """
    Thresholds and resizes predictions as a tensor.

    Args:
        preds (torch tensor): Predictions.
        shape (tuple [2]): Shape to resize to
        threshold (float, optional): Threshold. Defaults to 0.5.

    Returns:
        np array: Resized predictions.
    """
    preds = preds.unsqueeze(0).unsqueeze(0)
    preds = torch.nn.functional.interpolate(
        preds, (shape[1], shape[0]), mode='bilinear', align_corners=False
    )
    return (preds > threshold).cpu().numpy()[0, 0]