|
| 1 | +import pathlib |
| 2 | +from typing import Any, Optional |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +import tifffile |
| 7 | +import torch |
| 8 | + |
| 9 | +from .image_padding_specs import compute_patch_mapping |
| 10 | +from .save_utils import save_image_locally, save_image_mlflow |
| 11 | + |
| 12 | + |
| 13 | +class SaveWholeSlices: |
| 14 | + """ |
| 15 | + Saves chosen images, and all voxels from those images, to a 3D tiff format either locally or in MLflow. |
| 16 | + """ |
| 17 | + |
| 18 | + def __init__( |
| 19 | + self, |
| 20 | + image_dataset: torch.utils.data.Dataset, |
| 21 | + image_dataset_idxs: list[int], |
| 22 | + image_specs: dict[str, Any], |
| 23 | + stride: tuple[int], |
| 24 | + crop_shape: tuple[int], |
| 25 | + pad_mode="reflect", |
| 26 | + image_postprocessor: Any = lambda x: x, |
| 27 | + local_save_path: Optional[pathlib.Path] = None, |
| 28 | + ): |
| 29 | + |
| 30 | + self.image_dataset = image_dataset |
| 31 | + self.image_dataset_idxs = image_dataset_idxs |
| 32 | + self.image_specs = image_specs |
| 33 | + self.stride = stride |
| 34 | + self.crop_shape = crop_shape |
| 35 | + self.pad_mode = pad_mode |
| 36 | + self.image_postprocessor = image_postprocessor |
| 37 | + self.local_save_path = local_save_path |
| 38 | + |
| 39 | + self.unique_image_dataset_idxs = [] |
| 40 | + self.reduce_dataset_idxs(image_dataset=image_dataset) |
| 41 | + |
| 42 | + self.pad_width, self.original_crop_coords = None, None |
| 43 | + self.epoch = None |
| 44 | + |
| 45 | + def reduce_dataset_idxs(self, image_dataset: torch.utils.data.Dataset): |
| 46 | + """ |
| 47 | + For reducing the dataset to only unique indices. |
| 48 | + We don't want to save redundant images. |
| 49 | + Dataset indices reflect crop samples, and not whole image samples prior to this function. |
| 50 | + """ |
| 51 | + self.unique_image_dataset_idxs = [] |
| 52 | + |
| 53 | + for sample_idx in self.image_dataset_idxs: |
| 54 | + if ( |
| 55 | + image_dataset[sample_idx]["metadata"]["Metadata_ID"] |
| 56 | + not in self.unique_image_dataset_idxs |
| 57 | + ): |
| 58 | + self.unique_image_dataset_idxs.append(sample_idx) |
| 59 | + |
| 60 | + def predict_target( |
| 61 | + self, padded_image: torch.Tensor, model: torch.nn.Module |
| 62 | + ) -> torch.Tensor: |
| 63 | + """ |
| 64 | + padded_image: |
| 65 | + Expects image of shape: (Z, H, W) |
| 66 | + Z -> Number of Z slices |
| 67 | + H -> Image Height |
| 68 | + W -> Image Width |
| 69 | + """ |
| 70 | + |
| 71 | + output = torch.zeros( |
| 72 | + *padded_image.shape, |
| 73 | + dtype=torch.float32, |
| 74 | + device=padded_image.device, |
| 75 | + ) |
| 76 | + weight = torch.zeros_like(output) |
| 77 | + |
| 78 | + spatial_ranges = [ |
| 79 | + range(0, s - c, st) |
| 80 | + for s, c, st in zip(padded_image.shape, self.crop_shape, self.stride) |
| 81 | + ] |
| 82 | + |
| 83 | + for idx in torch.cartesian_prod( |
| 84 | + *[torch.tensor(list(r)) for r in spatial_ranges] |
| 85 | + ): |
| 86 | + start = idx.tolist() |
| 87 | + end = [s + c for s, c in zip(start, self.crop_shape)] |
| 88 | + |
| 89 | + slices = tuple(slice(s, e) for s, e in zip(start, end)) |
| 90 | + crop = padded_image[slices].unsqueeze(0) # add batch dim |
| 91 | + |
| 92 | + with torch.no_grad(): |
| 93 | + generated_prediction = self.image_postprocessor( |
| 94 | + generated_prediction=model(crop) |
| 95 | + ).squeeze(0) |
| 96 | + |
| 97 | + output[slices] += generated_prediction |
| 98 | + weight[slices] += 1.0 |
| 99 | + |
| 100 | + output /= weight |
| 101 | + |
| 102 | + return output[self.original_crop_coords] |
| 103 | + |
| 104 | + def pad_image(self, input_image: torch.Tensor) -> torch.Tensor: |
| 105 | + """ |
| 106 | + input_image: |
| 107 | + Expects image of shape: (Z, H, W) |
| 108 | + Z -> Number of Z slices |
| 109 | + H -> Image Height |
| 110 | + W -> Image Width |
| 111 | + """ |
| 112 | + |
| 113 | + padded_image = np.pad( |
| 114 | + input_image.detach().cpu().numpy(), |
| 115 | + pad_width=self.pad_width, |
| 116 | + mode=self.pad_mode, |
| 117 | + ) |
| 118 | + |
| 119 | + padded_image = torch.from_numpy(padded_image).to( |
| 120 | + dtype=torch.float32, device=input_image.device |
| 121 | + ) |
| 122 | + |
| 123 | + return padded_image |
| 124 | + |
| 125 | + def save_image( |
| 126 | + self, |
| 127 | + image_path: pathlib.Path, |
| 128 | + image_type: str, |
| 129 | + image: torch.Tensor, |
| 130 | + ) -> bool: |
| 131 | + """ |
| 132 | + - Determines if the image is completely black or not. |
| 133 | + - Saves images in the correct format to the hardcoded path. |
| 134 | + """ |
| 135 | + |
| 136 | + if not ((image > 0.0) & (image < 1.0)).any(): |
| 137 | + if image_type == "input": |
| 138 | + raise ValueError("Pixels should be between 0 and 1 in the input image") |
| 139 | + |
| 140 | + if image_type == "target": |
| 141 | + image = (image != 0).float() |
| 142 | + |
| 143 | + image = (image * 255).byte().cpu().numpy() |
| 144 | + |
| 145 | + # Black images will not be saved |
| 146 | + if np.max(image) == 0: |
| 147 | + return False |
| 148 | + |
| 149 | + image_suffix = ".tiff" if ".tif" in image_path.suffix else image_path.suffix |
| 150 | + |
| 151 | + filename = f"3D_{image_type}_{image_path.stem}{image_suffix}" |
| 152 | + |
| 153 | + fov_well_name = image_path.parent.name |
| 154 | + patient_name = image_path.parents[2].name |
| 155 | + |
| 156 | + save_image_path_folder = f"{patient_name}/{fov_well_name}" |
| 157 | + save_image_path_folder = ( |
| 158 | + f"whole_images/epoch_{self.epoch:02}/{save_image_path_folder}" |
| 159 | + if self.epoch is not None |
| 160 | + else save_image_path_folder |
| 161 | + ) |
| 162 | + |
| 163 | + if self.local_save_path is None: |
| 164 | + save_image_mlflow( |
| 165 | + image=image, |
| 166 | + save_image_path_folder=save_image_path_folder, |
| 167 | + image_filename=filename, |
| 168 | + ) |
| 169 | + else: |
| 170 | + save_image_path_folder = self.local_save_path / save_image_path_folder |
| 171 | + save_image_locally( |
| 172 | + image=image, |
| 173 | + save_image_path_folder=save_image_path_folder, |
| 174 | + image_filename=filename, |
| 175 | + ) |
| 176 | + |
| 177 | + return True |
| 178 | + |
| 179 | + def __call__( |
| 180 | + self, |
| 181 | + model: torch.nn.Module, |
| 182 | + epoch: Optional[int] = None, |
| 183 | + ) -> None: |
| 184 | + |
| 185 | + self.epoch = epoch |
| 186 | + for sample_idx in self.unique_image_dataset_idxs: |
| 187 | + |
| 188 | + self.image_specs["image_shape"][0] = tifffile.imread( |
| 189 | + self.image_dataset[sample_idx]["input_path"] |
| 190 | + ).shape[0] |
| 191 | + |
| 192 | + # For computing image padding and original crop coordinates |
| 193 | + # Only the z-padding and the z-crop coordinates need to be computed |
| 194 | + # each time, because the number of z-slices isn't consistent across |
| 195 | + # 3D images. |
| 196 | + self.pad_width, self.original_crop_coords = compute_patch_mapping( |
| 197 | + image_specs=self.image_specs, |
| 198 | + crop_shape=self.crop_shape, |
| 199 | + stride=self.stride, |
| 200 | + pad_slices=True, |
| 201 | + ) |
| 202 | + |
| 203 | + sample_image = self.save_image( |
| 204 | + image_path=self.image_dataset[sample_idx]["target_path"], |
| 205 | + image_type="target", |
| 206 | + image=self.image_dataset[sample_idx]["target"], |
| 207 | + ) |
| 208 | + |
| 209 | + # Only save these images if the segmentation mask isn't black |
| 210 | + # We expect the model to generate black segmentation crops, |
| 211 | + # which will present regardless of weather or not the whole segmented image |
| 212 | + # is black or not. |
| 213 | + if sample_image: |
| 214 | + padded_image = self.pad_image( |
| 215 | + input_image=self.image_dataset[sample_idx]["input"] |
| 216 | + ) |
| 217 | + |
| 218 | + generated_prediction = self.predict_target( |
| 219 | + padded_image=padded_image, model=model |
| 220 | + ) |
| 221 | + |
| 222 | + self.save_image( |
| 223 | + image_path=self.image_dataset[sample_idx]["input_path"], |
| 224 | + image_type="input", |
| 225 | + image=self.image_dataset[sample_idx]["input"], |
| 226 | + ) |
| 227 | + |
| 228 | + self.save_image( |
| 229 | + image_path=self.image_dataset[sample_idx]["target_path"], |
| 230 | + image_type="generated-prediction", |
| 231 | + image=generated_prediction, |
| 232 | + ) |
0 commit comments