Load an image mask, resize it, and prepare it for use in PyTorch [0, 255] -> [0, 1], returned shape (1,1,H,W) Parameters: mask_path (str): The file path to the image mask. device (torch.device): The PyTorch device (CPU or GPU) on which the mask should be loaded. size
(mask_path, device, size=(128, 128), mode='nearest')
| 34 | return image |
| 35 | |
| 36 | def load_mask(mask_path, device, size=(128, 128), mode='nearest'): |
| 37 | """ |
| 38 | Load an image mask, resize it, and prepare it for use in PyTorch [0, 255] -> [0, 1], returned shape (1,1,H,W) |
| 39 | Parameters: |
| 40 | mask_path (str): The file path to the image mask. |
| 41 | device (torch.device): The PyTorch device (CPU or GPU) on which the mask should be loaded. |
| 42 | size (tuple, optional): The target size to which the mask should be resized. Default is (64, 64). |
| 43 | mode (str, optional): The interpolation mode for resizing. Options include 'nearest', 'bilinear', 'bicubic', and more. |
| 44 | Default is 'nearest'. |
| 45 | |
| 46 | Returns: |
| 47 | torch.Tensor(1,1,H,W): A PyTorch tensor with shape (1, 1, H, W) representing the resized image mask. |
| 48 | |
| 49 | Example: |
| 50 | >>> mask = load_mask('mask.png', device='cuda', size=(128, 128), mode='nearest') |
| 51 | """ |
| 52 | mask = read_image(mask_path) |
| 53 | mask = F.interpolate(mask.unsqueeze(0), size=size, mode=mode) |
| 54 | mask = (mask / 255.).to(torch.uint8).to(device) |
| 55 | return mask |
| 56 | |
| 57 | def show_cam_on_image(img: np.ndarray, |
| 58 | mask: np.ndarray, |
no outgoing calls
no test coverage detected