Load an image, preprocess it, and prepare it for use in a deep learning model. Parameters: image_path (str): The file path to the image. device (torch.device): The PyTorch device (CPU or GPU) on which the image should be loaded. Returns: torch.Tensor: A PyTorch
(image_path, device)
| 8 | from matplotlib import cm |
| 9 | |
| 10 | def load_image(image_path, device): |
| 11 | """ |
| 12 | Load an image, preprocess it, and prepare it for use in a deep learning model. |
| 13 | |
| 14 | Parameters: |
| 15 | image_path (str): The file path to the image. |
| 16 | device (torch.device): The PyTorch device (CPU or GPU) on which the image should be loaded. |
| 17 | |
| 18 | Returns: |
| 19 | torch.Tensor: A PyTorch tensor representing the preprocessed image. |
| 20 | |
| 21 | Example: |
| 22 | >>> image = load_image('example.jpg', device='cuda') |
| 23 | """ |
| 24 | # Load the image |
| 25 | image = read_image(image_path) # You should have a read_image function to load the image |
| 26 | |
| 27 | # Preprocess the image |
| 28 | image = image[:3].unsqueeze_(0).float() / 127.5 - 1. # Normalize pixel values to the range [-1, 1] |
| 29 | image = F.interpolate(image, (512, 512)) # Resize the image to a specified size |
| 30 | |
| 31 | # Move the preprocessed image to the specified PyTorch device |
| 32 | image = image.to(device) |
| 33 | |
| 34 | return image |
| 35 | |
| 36 | def load_mask(mask_path, device, size=(128, 128), mode='nearest'): |
| 37 | """ |
no outgoing calls
no test coverage detected