Skip to content

Commit 3e815e9

Browse files
committed
update new url
Signed-off-by: Can-Zhao <[email protected]>
1 parent d6da454 commit 3e815e9

File tree

4 files changed

+195
-138
lines changed

4 files changed

+195
-138
lines changed

generation/maisi/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 🚨🚨🚨 THIS FOLDER IS DEPRECATED 🚨🚨🚨
2+
# 👉 Please switch to: [https://github.com/NVIDIA-Medtech/NV-Generate-CTMR/tree/main](https://github.com/NVIDIA-Medtech/NV-Generate-CTMR/tree/main)
3+
14
# Medical AI for Synthetic Imaging (MAISI)
25
This example demonstrates the applications of training and validating NVIDIA MAISI, a 3D Latent Diffusion Model (LDM) capable of generating large CT images accompanied by corresponding segmentation masks. It supports variable volume size and voxel spacing and allows for the precise control of organ/tumor size.
36

generation/maisi/maisi_inference_tutorial.ipynb

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
{
5555
"cell_type": "code",
56-
"execution_count": 2,
56+
"execution_count": null,
5757
"id": "67e2019e-1556-41a6-95e8-5d1a65f8b3a1",
5858
"metadata": {
5959
"scrolled": true
@@ -112,6 +112,7 @@
112112
"from scripts.utils import define_instance\n",
113113
"from scripts.utils_plot import find_label_center_loc, get_xyz_plot, show_image\n",
114114
"from scripts.diff_model_setting import setup_logging\n",
115+
"from scripts.download_model_data import download_model_data\n",
115116
"\n",
116117
"print_config()\n",
117118
"\n",
@@ -170,7 +171,7 @@
170171
},
171172
{
172173
"cell_type": "code",
173-
"execution_count": 4,
174+
"execution_count": null,
174175
"id": "e3c12dcc",
175176
"metadata": {},
176177
"outputs": [
@@ -204,70 +205,7 @@
204205
" os.makedirs(directory, exist_ok=True)\n",
205206
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
206207
"\n",
207-
"# TODO: remove the `files` after the files are uploaded to the NGC\n",
208-
"files = [\n",
209-
" {\n",
210-
" \"path\": \"models/autoencoder_epoch273.pt\",\n",
211-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai/tutorials\"\n",
212-
" \"/model_zoo/model_maisi_autoencoder_epoch273_alternative.pt\",\n",
213-
" },\n",
214-
" {\n",
215-
" \"path\": \"models/mask_generation_autoencoder.pt\",\n",
216-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai\" \"/tutorials/mask_generation_autoencoder.pt\",\n",
217-
" },\n",
218-
" {\n",
219-
" \"path\": \"models/mask_generation_diffusion_unet.pt\",\n",
220-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai\"\n",
221-
" \"/tutorials/model_zoo/model_maisi_mask_generation_diffusion_unet_v2.pt\",\n",
222-
" },\n",
223-
" {\n",
224-
" \"path\": \"configs/all_anatomy_size_condtions.json\",\n",
225-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai/tutorials/all_anatomy_size_condtions.json\",\n",
226-
" },\n",
227-
" {\n",
228-
" \"path\": \"datasets/all_masks_flexible_size_and_spacing_4000.zip\",\n",
229-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai\"\n",
230-
" \"/tutorials/all_masks_flexible_size_and_spacing_4000.zip\",\n",
231-
" },\n",
232-
"]\n",
233-
"\n",
234-
"if maisi_version == \"maisi3d-ddpm\":\n",
235-
" files += [\n",
236-
" {\n",
237-
" \"path\": \"models/diff_unet_3d_ddpm.pt\",\n",
238-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai/tutorials/model_zoo\"\n",
239-
" \"/model_maisi_input_unet3d_data-all_steps1000size512ddpm_random_current_inputx_v1_alternative.pt\",\n",
240-
" },\n",
241-
" {\n",
242-
" \"path\": \"models/controlnet_3d_ddpm.pt\",\n",
243-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai/tutorials/model_zoo\"\n",
244-
" \"/model_maisi_controlnet-20datasets-e20wl100fold0bc_noi_dia_fsize_current_alternative.pt\",\n",
245-
" },\n",
246-
" {\n",
247-
" \"path\": \"configs/candidate_masks_flexible_size_and_spacing_3000.json\",\n",
248-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai\"\n",
249-
" \"/tutorials/candidate_masks_flexible_size_and_spacing_3000.json\",\n",
250-
" },\n",
251-
" ]\n",
252-
"elif maisi_version == \"maisi3d-rflow\":\n",
253-
" files += [\n",
254-
" {\n",
255-
" \"path\": \"models/diff_unet_3d_rflow.pt\",\n",
256-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai/tutorials/\"\n",
257-
" \"diff_unet_ckpt_rflow_epoch19350.pt\",\n",
258-
" },\n",
259-
" {\n",
260-
" \"path\": \"models/controlnet_3d_rflow.pt\",\n",
261-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai/tutorials/controlnet_rflow_epoch60.pt\",\n",
262-
" },\n",
263-
" {\n",
264-
" \"path\": \"configs/candidate_masks_flexible_size_and_spacing_4000.json\",\n",
265-
" \"url\": \"https://developer.download.nvidia.com/assets/Clara/monai\"\n",
266-
" \"/tutorials/candidate_masks_flexible_size_and_spacing_4000.json\",\n",
267-
" },\n",
268-
" ]\n",
269-
"else:\n",
270-
" raise ValueError(f\"maisi_version has to be chosen from ['maisi3d-ddpm', 'maisi3d-rflow'], yet got {maisi_version}.\")\n",
208+
"download_model_data(maisi_version,root_dir)\n",
271209
"\n",
272210
"for file in files:\n",
273211
" file[\"path\"] = file[\"path\"] if \"datasets/\" not in file[\"path\"] else os.path.join(root_dir, file[\"path\"])\n",
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import os, subprocess, shutil
2+
import argparse
3+
from tqdm.auto import tqdm
4+
from monai.apps import download_url
5+
from pathlib import Path
6+
from huggingface_hub import snapshot_download
7+
from typing import List, Dict, Optional
8+
9+
def fetch_to_hf_path_cmd(
10+
items: List[Dict[str, str]],
11+
root_dir: str = "./", # staging dir for CLI output
12+
revision: str = "main",
13+
overwrite: bool = False,
14+
token: Optional[str] = None, # or rely on env HUGGINGFACE_HUB_TOKEN
15+
) -> list[str]:
16+
"""
17+
items: list of {"repo_id": "...", "filename": "path/in/repo.ext", "path": "local/target.ext"}
18+
Returns list of saved local paths (in the same order as items).
19+
"""
20+
saved = []
21+
root = Path(root_dir)
22+
root.mkdir(parents=True, exist_ok=True)
23+
24+
# Env for subprocess; keep Rust fast-path off to avoid notebook progress quirks
25+
env = os.environ.copy()
26+
if token:
27+
env["HUGGINGFACE_HUB_TOKEN"] = token
28+
env.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "0") # safer in Jupyter
29+
env.setdefault("HF_HUB_DISABLE_PROGRESS_BARS", "0") # show CLI progress in terminal
30+
31+
for it in items:
32+
repo_id = it["repo_id"]
33+
repo_file = it["filename"]
34+
dst = Path(it["path"])
35+
dst.parent.mkdir(parents=True, exist_ok=True)
36+
37+
if dst.exists() and not overwrite:
38+
saved.append(str(dst))
39+
continue
40+
41+
# Build command (no shell=True; no quoting issues)
42+
cmd = [
43+
"huggingface-cli", "download",
44+
repo_id,
45+
"--include", repo_file,
46+
"--revision", revision,
47+
"--local-dir", str(root),
48+
]
49+
# Run
50+
subprocess.run(cmd, check=True, env=env)
51+
52+
# Source path where CLI placed the file
53+
src = root / repo_file
54+
if not src.exists():
55+
raise FileNotFoundError(
56+
f"Expected downloaded file missing: {src}\n"
57+
f"Tip: authenticate (`huggingface-cli login` or pass token=...),"
58+
f" and avoid shared-IP 429s."
59+
)
60+
61+
# Move to desired target
62+
if dst.exists() and overwrite:
63+
dst.unlink()
64+
if src.resolve() != dst.resolve():
65+
dst.parent.mkdir(parents=True, exist_ok=True)
66+
shutil.move(str(src), str(dst))
67+
saved.append(str(dst))
68+
69+
return saved
70+
71+
72+
73+
def download_model_data(generate_version,root_dir, model_only=False):
74+
# TODO: remove the `files` after the files are uploaded to the NGC
75+
if generate_version == "ddpm-ct" or generate_version == "rflow-ct":
76+
files = [
77+
{
78+
"path": "models/autoencoder_v1.pt",
79+
"repo_id": "nvidia/NV-Generate-CT",
80+
"filename":"models/autoencoder_v1.pt",
81+
},
82+
{
83+
"path": "models/mask_generation_autoencoder.pt",
84+
"repo_id": "nvidia/NV-Generate-CT",
85+
"filename": "models/mask_generation_autoencoder.pt",
86+
},
87+
{
88+
"path": "models/mask_generation_diffusion_unet.pt",
89+
"repo_id": "nvidia/NV-Generate-CT",
90+
"filename": "models/mask_generation_diffusion_unet.pt",
91+
}]
92+
if not model_only:
93+
files += [
94+
{
95+
"path": "datasets/all_anatomy_size_conditions.json",
96+
"repo_id": "nvidia/NV-Generate-CT",
97+
"filename": "datasets/all_anatomy_size_conditions.json",
98+
},
99+
{
100+
"path": "datasets/all_masks_flexible_size_and_spacing_4000.zip",
101+
"repo_id": "nvidia/NV-Generate-CT",
102+
"filename": "datasets/all_masks_flexible_size_and_spacing_4000.zip",
103+
},
104+
]
105+
elif generate_version == "rflow-mr":
106+
files = [
107+
{
108+
"path": "models/autoencoder_v2.pt",
109+
"repo_id": "nvidia/NV-Generate-MR",
110+
"filename": "models/autoencoder_v2.pt",
111+
},
112+
{
113+
"path": "models/diff_unet_3d_rflow-mr.pt",
114+
"repo_id": "nvidia/NV-Generate-MR",
115+
"filename": "models/diff_unet_3d_rflow-mr.pt",
116+
}
117+
]
118+
else:
119+
raise ValueError(f"generate_version has to be chosen from ['ddpm-ct', 'rflow-ct', 'rflow-mr'], yet got {generate_version}.")
120+
if generate_version == "ddpm-ct":
121+
files += [
122+
{
123+
"path": "models/diff_unet_3d_ddpm-ct.pt",
124+
"repo_id": "nvidia/NV-Generate-CT",
125+
"filename": "models/diff_unet_3d_ddpm-ct.pt",
126+
},
127+
{
128+
"path": "models/controlnet_3d_ddpm-ct.pt",
129+
"repo_id": "nvidia/NV-Generate-CT",
130+
"filename": "models/controlnet_3d_ddpm-ct.pt",
131+
}]
132+
if not model_only:
133+
files += [
134+
{
135+
"path": "datasets/candidate_masks_flexible_size_and_spacing_3000.json",
136+
"repo_id": "nvidia/NV-Generate-CT",
137+
"filename": "datasets/candidate_masks_flexible_size_and_spacing_3000.json",
138+
},
139+
]
140+
elif generate_version == "rflow-ct":
141+
files += [
142+
{
143+
"path": "models/diff_unet_3d_rflow-ct.pt",
144+
"repo_id": "nvidia/NV-Generate-CT",
145+
"filename": "models/diff_unet_3d_rflow-ct.pt",
146+
},
147+
{
148+
"path": "models/controlnet_3d_rflow-ct.pt",
149+
"repo_id": "nvidia/NV-Generate-CT",
150+
"filename": "models/controlnet_3d_rflow-ct.pt",
151+
}]
152+
if not model_only:
153+
files += [
154+
{
155+
"path": "datasets/candidate_masks_flexible_size_and_spacing_4000.json",
156+
"repo_id": "nvidia/NV-Generate-CT",
157+
"filename": "datasets/candidate_masks_flexible_size_and_spacing_4000.json",
158+
},
159+
]
160+
161+
for file in files:
162+
file["path"] = file["path"] if "datasets/" not in file["path"] else os.path.join(root_dir, file["path"])
163+
if "repo_id" in file.keys():
164+
path = fetch_to_hf_path_cmd([file],root_dir=root_dir, revision="main")
165+
print("saved to:", path)
166+
else:
167+
download_url(url=file["url"], filepath=file["path"])
168+
return
169+
170+
171+
if __name__ == "__main__":
172+
parser = argparse.ArgumentParser(description="Model downloading")
173+
parser.add_argument(
174+
"--version",
175+
type=str,
176+
default="rflow-ct",
177+
)
178+
parser.add_argument(
179+
"--root_dir",
180+
type=str,
181+
default="./",
182+
)
183+
parser.add_argument("--model_only", dest="model_only", action="store_true", help="Download model only, not any dataset")
184+
185+
args = parser.parse_args()
186+
download_model_data(args.version, args.root_dir, args.model_only)

generation/maisi/scripts/inference.py

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from scripts.sample import LDMSampler, check_input
2828
from scripts.utils import define_instance
2929
from scripts.utils_plot import find_label_center_loc, get_xyz_plot, show_image
30+
from scripts.download_model_data import download_model_data
3031

3132

3233
def main():
@@ -88,78 +89,7 @@ def main():
8889
root_dir = tempfile.mkdtemp() if directory is None else directory
8990
print(root_dir)
9091

91-
# TODO: remove the `files` after the files are uploaded to the NGC
92-
files = [
93-
{
94-
"path": "models/autoencoder_epoch273.pt",
95-
"url": "https://developer.download.nvidia.com/assets/Clara/monai/tutorials"
96-
"/model_zoo/model_maisi_autoencoder_epoch273_alternative.pt",
97-
},
98-
{
99-
"path": "models/mask_generation_autoencoder.pt",
100-
"url": "https://developer.download.nvidia.com/assets/Clara/monai"
101-
"/tutorials/mask_generation_autoencoder.pt",
102-
},
103-
{
104-
"path": "models/mask_generation_diffusion_unet.pt",
105-
"url": "https://developer.download.nvidia.com/assets/Clara/monai"
106-
"/tutorials/model_zoo/model_maisi_mask_generation_diffusion_unet_v2.pt",
107-
},
108-
{
109-
"path": "configs/all_anatomy_size_condtions.json",
110-
"url": "https://developer.download.nvidia.com/assets/Clara/monai/tutorials/all_anatomy_size_condtions.json",
111-
},
112-
{
113-
"path": "datasets/all_masks_flexible_size_and_spacing_4000.zip",
114-
"url": "https://developer.download.nvidia.com/assets/Clara/monai"
115-
"/tutorials/all_masks_flexible_size_and_spacing_4000.zip",
116-
},
117-
]
118-
119-
if maisi_version == "maisi3d-ddpm":
120-
files += [
121-
{
122-
"path": "models/diff_unet_3d_ddpm.pt",
123-
"url": "https://developer.download.nvidia.com/assets/Clara/monai/tutorials/model_zoo"
124-
"/model_maisi_input_unet3d_data-all_steps1000size512ddpm_random_current_inputx_v1_alternative.pt",
125-
},
126-
{
127-
"path": "models/controlnet_3d_ddpm.pt",
128-
"url": "https://developer.download.nvidia.com/assets/Clara/monai/tutorials/model_zoo"
129-
"/model_maisi_controlnet-20datasets-e20wl100fold0bc_noi_dia_fsize_current_alternative.pt",
130-
},
131-
{
132-
"path": "configs/candidate_masks_flexible_size_and_spacing_3000.json",
133-
"url": "https://developer.download.nvidia.com/assets/Clara/monai"
134-
"/tutorials/candidate_masks_flexible_size_and_spacing_3000.json",
135-
},
136-
]
137-
elif maisi_version == "maisi3d-rflow":
138-
files += [
139-
{
140-
"path": "models/diff_unet_3d_rflow.pt",
141-
"url": "https://developer.download.nvidia.com/assets/Clara/monai/tutorials/"
142-
"diff_unet_ckpt_rflow_epoch19350.pt",
143-
},
144-
{
145-
"path": "models/controlnet_3d_rflow.pt",
146-
"url": "https://developer.download.nvidia.com/assets/Clara/monai/tutorials/"
147-
"controlnet_rflow_epoch60.pt",
148-
},
149-
{
150-
"path": "configs/candidate_masks_flexible_size_and_spacing_4000.json",
151-
"url": "https://developer.download.nvidia.com/assets/Clara/monai"
152-
"/tutorials/candidate_masks_flexible_size_and_spacing_4000.json",
153-
},
154-
]
155-
else:
156-
raise ValueError(
157-
f"maisi_version has to be chosen from ['maisi3d-ddpm', 'maisi3d-rflow'], yet got {maisi_version}."
158-
)
159-
160-
for file in files:
161-
file["path"] = file["path"] if "datasets/" not in file["path"] else os.path.join(root_dir, file["path"])
162-
download_url(url=file["url"], filepath=file["path"])
92+
download_model_data(maisi_version,root_dir)
16393

16494
# ## Read in environment setting, including data directory, model directory, and output directory
16595
# The information for data directory, model directory, and output directory are saved in ./configs/environment.json

0 commit comments

Comments
 (0)