Generate anomaly segmentation images. Args: image_paths: List[str] List of paths to images. segmentations: [List[np.ndarray]] Generated anomaly segmentations. anomaly_scores: [List[float]] Anomaly scores for each image. mask_paths: [List[str]] List of paths to gr
(
savefolder,
image_paths,
segmentations,
anomaly_scores=None,
mask_paths=None,
image_transform=lambda x: x,
mask_transform=lambda x: x,
save_depth=4,
)
| 13 | |
| 14 | |
| 15 | def plot_segmentation_images( |
| 16 | savefolder, |
| 17 | image_paths, |
| 18 | segmentations, |
| 19 | anomaly_scores=None, |
| 20 | mask_paths=None, |
| 21 | image_transform=lambda x: x, |
| 22 | mask_transform=lambda x: x, |
| 23 | save_depth=4, |
| 24 | ): |
| 25 | """Generate anomaly segmentation images. |
| 26 | |
| 27 | Args: |
| 28 | image_paths: List[str] List of paths to images. |
| 29 | segmentations: [List[np.ndarray]] Generated anomaly segmentations. |
| 30 | anomaly_scores: [List[float]] Anomaly scores for each image. |
| 31 | mask_paths: [List[str]] List of paths to ground truth masks. |
| 32 | image_transform: [function or lambda] Optional transformation of images. |
| 33 | mask_transform: [function or lambda] Optional transformation of masks. |
| 34 | save_depth: [int] Number of path-strings to use for image savenames. |
| 35 | """ |
| 36 | if mask_paths is None: |
| 37 | mask_paths = ["-1" for _ in range(len(image_paths))] |
| 38 | masks_provided = mask_paths[0] != "-1" |
| 39 | if anomaly_scores is None: |
| 40 | anomaly_scores = ["-1" for _ in range(len(image_paths))] |
| 41 | |
| 42 | os.makedirs(savefolder, exist_ok=True) |
| 43 | |
| 44 | for image_path, mask_path, anomaly_score, segmentation in tqdm.tqdm( |
| 45 | zip(image_paths, mask_paths, anomaly_scores, segmentations), |
| 46 | total=len(image_paths), |
| 47 | desc="Generating Segmentation Images...", |
| 48 | leave=False, |
| 49 | ): |
| 50 | image = PIL.Image.open(image_path).convert("RGB") |
| 51 | image = image_transform(image) |
| 52 | if not isinstance(image, np.ndarray): |
| 53 | image = image.numpy() |
| 54 | |
| 55 | if masks_provided: |
| 56 | if mask_path is not None: |
| 57 | mask = PIL.Image.open(mask_path).convert("RGB") |
| 58 | mask = mask_transform(mask) |
| 59 | if not isinstance(mask, np.ndarray): |
| 60 | mask = mask.numpy() |
| 61 | else: |
| 62 | mask = np.zeros_like(image) |
| 63 | |
| 64 | savename = image_path.split("/") |
| 65 | savename = "_".join(savename[-save_depth:]) |
| 66 | savename = os.path.join(savefolder, savename) |
| 67 | f, axes = plt.subplots(1, 2 + int(masks_provided)) |
| 68 | axes[0].imshow(image.transpose(1, 2, 0)) |
| 69 | axes[1].imshow(mask.transpose(1, 2, 0)) |
| 70 | axes[2].imshow(segmentation) |
| 71 | f.set_size_inches(3 * (2 + int(masks_provided)), 3) |
| 72 | f.tight_layout() |
nothing calls this directly
no test coverage detected