File size: 8,325 Bytes
599a397 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import argparse
import json
import logging
import os
from pathlib import Path
import monai
import nibabel as nib
import numpy as np
import torch
import torch.distributed as dist
from monai.transforms import Compose
from monai.utils import set_determinism
from .diff_model_setting import initialize_distributed, load_config, setup_logging
from .utils import define_instance
# Set the random seed for reproducibility
set_determinism(seed=0)
def create_transforms(dim: tuple = None) -> Compose:
"""
Create a set of MONAI transforms for preprocessing.
Args:
dim (tuple, optional): New dimensions for resizing. Defaults to None.
Returns:
Compose: Composed MONAI transforms.
"""
if dim:
return Compose(
[
monai.transforms.LoadImaged(keys="image"),
monai.transforms.EnsureChannelFirstd(keys="image"),
monai.transforms.Orientationd(keys="image", axcodes="RAS"),
monai.transforms.EnsureTyped(keys="image", dtype=torch.float32),
monai.transforms.ScaleIntensityRanged(
keys="image", a_min=-1000, a_max=1000, b_min=0, b_max=1, clip=True
),
monai.transforms.Resized(keys="image", spatial_size=dim, mode="trilinear"),
]
)
else:
return Compose(
[
monai.transforms.LoadImaged(keys="image"),
monai.transforms.EnsureChannelFirstd(keys="image"),
monai.transforms.Orientationd(keys="image", axcodes="RAS"),
]
)
def round_number(number: int, base_number: int = 128) -> int:
"""
Round the number to the nearest multiple of the base number, with a minimum value of the base number.
Args:
number (int): Number to be rounded.
base_number (int): Number to be common divisor.
Returns:
int: Rounded number.
"""
new_number = max(round(float(number) / float(base_number)), 1.0) * float(base_number)
return int(new_number)
def load_filenames(data_list_path: str) -> list:
"""
Load filenames from the JSON data list.
Args:
data_list_path (str): Path to the JSON data list file.
Returns:
list: List of filenames.
"""
with open(data_list_path, "r") as file:
json_data = json.load(file)
filenames_raw = json_data["training"]
return [_item["image"] for _item in filenames_raw]
def process_file(
filepath: str,
args: argparse.Namespace,
autoencoder: torch.nn.Module,
device: torch.device,
plain_transforms: Compose,
new_transforms: Compose,
logger: logging.Logger,
) -> None:
"""
Process a single file to create training data.
Args:
filepath (str): Path to the file to be processed.
args (argparse.Namespace): Configuration arguments.
autoencoder (torch.nn.Module): Autoencoder model.
device (torch.device): Device to process the file on.
plain_transforms (Compose): Plain transforms.
new_transforms (Compose): New transforms.
logger (logging.Logger): Logger for logging information.
"""
out_filename_base = filepath.replace(".gz", "").replace(".nii", "")
out_filename_base = os.path.join(args.embedding_base_dir, out_filename_base)
out_filename = out_filename_base + "_emb.nii.gz"
if os.path.isfile(out_filename):
return
test_data = {"image": os.path.join(args.data_base_dir, filepath)}
transformed_data = plain_transforms(test_data)
nda = transformed_data["image"]
dim = [int(nda.meta["dim"][_i]) for _i in range(1, 4)]
spacing = [float(nda.meta["pixdim"][_i]) for _i in range(1, 4)]
logger.info(f"old dim: {dim}, old spacing: {spacing}")
new_data = new_transforms(test_data)
nda_image = new_data["image"]
new_affine = nda_image.meta["affine"].numpy()
nda_image = nda_image.numpy().squeeze()
logger.info(f"new dim: {nda_image.shape}, new affine: {new_affine}")
try:
out_path = Path(out_filename)
out_path.parent.mkdir(parents=True, exist_ok=True)
logger.info(f"out_filename: {out_filename}")
with torch.amp.autocast("cuda"):
pt_nda = torch.from_numpy(nda_image).float().to(device).unsqueeze(0).unsqueeze(0)
z = autoencoder.encode_stage_2_inputs(pt_nda)
logger.info(f"z: {z.size()}, {z.dtype}")
out_nda = z.squeeze().cpu().detach().numpy().transpose(1, 2, 3, 0)
out_img = nib.Nifti1Image(np.float32(out_nda), affine=new_affine)
nib.save(out_img, out_filename)
except Exception as e:
logger.error(f"Error processing {filepath}: {e}")
@torch.inference_mode()
def diff_model_create_training_data(
env_config_path: str, model_config_path: str, model_def_path: str, num_gpus: int
) -> None:
"""
Create training data for the diffusion model.
Args:
env_config_path (str): Path to the environment configuration file.
model_config_path (str): Path to the model configuration file.
model_def_path (str): Path to the model definition file.
"""
args = load_config(env_config_path, model_config_path, model_def_path)
local_rank, world_size, device = initialize_distributed(num_gpus=num_gpus)
logger = setup_logging("creating training data")
logger.info(f"Using device {device}")
autoencoder = define_instance(args, "autoencoder_def").to(device)
try:
checkpoint_autoencoder = torch.load(args.trained_autoencoder_path, weights_only=True)
autoencoder.load_state_dict(checkpoint_autoencoder)
except Exception:
logger.error("The trained_autoencoder_path does not exist!")
Path(args.embedding_base_dir).mkdir(parents=True, exist_ok=True)
filenames_raw = load_filenames(args.json_data_list)
logger.info(f"filenames_raw: {filenames_raw}")
plain_transforms = create_transforms(dim=None)
for _iter in range(len(filenames_raw)):
if _iter % world_size != local_rank:
continue
filepath = filenames_raw[_iter]
new_dim = tuple(
round_number(
int(plain_transforms({"image": os.path.join(args.data_base_dir, filepath)})["image"].meta["dim"][_i])
)
for _i in range(1, 4)
)
new_transforms = create_transforms(new_dim)
process_file(filepath, args, autoencoder, device, plain_transforms, new_transforms, logger)
if dist.is_initialized():
dist.destroy_process_group()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Diffusion Model Training Data Creation")
parser.add_argument(
"--env_config",
type=str,
default="./configs/environment_maisi_diff_model_train.json",
help="Path to environment configuration file",
)
parser.add_argument(
"--model_config",
type=str,
default="./configs/config_maisi_diff_model_train.json",
help="Path to model training/inference configuration",
)
parser.add_argument(
"--model_def", type=str, default="./configs/config_maisi.json", help="Path to model definition file"
)
parser.add_argument("--num_gpus", type=int, default=1, help="Number of GPUs to use for distributed training")
args = parser.parse_args()
diff_model_create_training_data(args.env_config, args.model_config, args.model_def, args.num_gpus)
|