Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2d diffusion model bundle #408

Merged
merged 81 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
c83d75a
add bundle for 2d ldm
Can-Zhao May 2, 2023
3745ad7
add bundle for 2d ldm
Can-Zhao May 2, 2023
93e1001
update readme and config
Can-Zhao May 4, 2023
9b1aec6
update readme
Can-Zhao May 4, 2023
4d7c3b1
update readme
Can-Zhao May 4, 2023
a9dcf89
Merge branch 'Project-MONAI:dev' into ldm2d
Can-Zhao May 6, 2023
95843bd
update readme
Can-Zhao May 6, 2023
4d0e495
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 6, 2023
2cecca4
change sampler to inferer
Can-Zhao May 6, 2023
8d4b037
change network_def
Can-Zhao May 6, 2023
975dc34
change hyperparameter
Can-Zhao May 6, 2023
18c5df5
update configs
Can-Zhao May 7, 2023
06aa8b3
update readme
Can-Zhao May 7, 2023
53cf59a
update readme
Can-Zhao May 7, 2023
414df64
update readme
Can-Zhao May 7, 2023
3a64b74
update readme
Can-Zhao May 7, 2023
10519fd
update large file
Can-Zhao May 7, 2023
8aab29c
update dataset path
Can-Zhao May 7, 2023
b7718b3
update readme
Can-Zhao May 7, 2023
58e859d
Merge branch 'dev' into ldm2d
Can-Zhao May 7, 2023
7dde3e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 7, 2023
193fa19
update readme
Can-Zhao May 8, 2023
8c94d90
reformat
Can-Zhao May 8, 2023
a6627a0
update readme
Can-Zhao May 8, 2023
4ffb51b
update meta
Can-Zhao May 8, 2023
4893112
update readme
Can-Zhao May 8, 2023
41b7544
update readme
Can-Zhao May 8, 2023
105ed8c
update readme
Can-Zhao May 9, 2023
fc68040
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 9, 2023
04aba98
Merge branch 'dev' into ldm2d
Can-Zhao May 9, 2023
67e3d99
update readme, update perceptual loss
Can-Zhao May 12, 2023
4aa72c5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 12, 2023
917e140
update readme, update perceptual loss
Can-Zhao May 12, 2023
1ae4239
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 12, 2023
3bcd001
seprate data download out, update readme
Can-Zhao May 13, 2023
d0f3129
update readme
Can-Zhao May 13, 2023
8b2916e
reformat
Can-Zhao May 13, 2023
095ee80
update readme
Can-Zhao May 15, 2023
8d10d27
add vae inference
Can-Zhao May 15, 2023
1860159
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2023
9d0b0f5
update latgefile
Can-Zhao May 15, 2023
dc605d1
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 15, 2023
575517d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 15, 2023
8fb6c22
update readme and config for pre-trained issue
Can-Zhao May 15, 2023
b39d611
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 15, 2023
14ff18a
update readme
Can-Zhao May 15, 2023
657f67c
Merge branch 'dev' into ldm2d
Can-Zhao May 15, 2023
c61b6b6
update readme
Can-Zhao May 15, 2023
4b87941
typo
Can-Zhao May 15, 2023
a573503
update readme
Can-Zhao May 15, 2023
b9c9faf
reformat
Can-Zhao May 15, 2023
5be47fe
Merge branch 'Project-MONAI:dev' into ldm2d
Can-Zhao May 15, 2023
489404a
update readme
Can-Zhao May 15, 2023
71df4eb
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 15, 2023
137260c
update pretrain in config
Can-Zhao May 15, 2023
edf48c5
update readme on dependency
Can-Zhao May 15, 2023
d4b49bc
Merge branch 'dev' into ldm2d
Can-Zhao May 15, 2023
0877601
Merge branch 'dev' into ldm2d
Can-Zhao May 15, 2023
1079dc9
change sampler to inferer to meet requirement in inference.json
Can-Zhao May 15, 2023
bb26c89
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 15, 2023
9c79a74
change ci/bundle_custom_data.py
Can-Zhao May 15, 2023
38ba237
change ci/bundle_custom_data.py
Can-Zhao May 15, 2023
8465a45
change ci verify_bundle.py
Can-Zhao May 15, 2023
b3b77e0
reformat
Can-Zhao May 15, 2023
5c763d2
change monai generative model dependency to 0.2.1
Can-Zhao May 15, 2023
f210254
reformat
Can-Zhao May 15, 2023
bdc9cd9
update meta
Can-Zhao May 15, 2023
95d8a47
update meta and trained model weights for ldm3d
Can-Zhao May 16, 2023
f26eaf6
update verify_bundle.py
Can-Zhao May 16, 2023
3a0d324
add install file for 2d ldm
Can-Zhao May 16, 2023
e80743d
add install file for 2d ldm
Can-Zhao May 16, 2023
ccb3152
add ci verify model for 2d ldm
Can-Zhao May 16, 2023
d3f5a41
add import on monai.utils.first
yiheng-wang-nv May 16, 2023
6ceef5c
update reference
Can-Zhao May 16, 2023
36b1e5b
Merge branch 'ldm2d' of github.com:Can-Zhao/model-zoo into ldm2d
Can-Zhao May 16, 2023
d0233c6
update data section
Can-Zhao May 16, 2023
423911e
update loss fig
Can-Zhao May 16, 2023
26449de
update loss fig
Can-Zhao May 16, 2023
fb30347
update loss fig
Can-Zhao May 16, 2023
8791481
update loss fig version
Can-Zhao May 16, 2023
7585c3e
update readme time step data type
Can-Zhao May 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ci/bundle_custom_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"lung_nodule_ct_detection",
"pathology_nuclei_segmentation_classification",
"brats_mri_generative_diffusion",
"brats_mri_axial_slices_generative_diffusion",
]

# This list is used for our CI tests to determine whether a bundle contains the preferred files.
Expand All @@ -34,13 +35,15 @@
"breast_density_classification",
"mednist_reg",
"brats_mri_generative_diffusion",
"brats_mri_axial_slices_generative_diffusion",
]

# This dict is used for our CI tests to install required dependencies that cannot be installed by `pip install` directly.
# If a bundle has this kind of dependencies, please add the bundle name (key), and the path of the install script (value)
# into the dict.
install_dependency_dict = {
"brats_mri_generative_diffusion": "ci/install_scripts/install_brats_mri_generative_diffusion_dependency.sh"
"brats_mri_generative_diffusion": "ci/install_scripts/install_brats_mri_generative_diffusion_dependency.sh",
"brats_mri_axial_slices_generative_diffusion": "ci/install_scripts/install_brats_mri_axial_slices_generative_diffusion.sh",
}

# This list is used for our CI tests to determine whether a bundle supports TensorRT export. Related
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.

# install cython to avoid error:
# https://github.com/Project-MONAI/model-zoo/actions/runs/4980530305/jobs/8913560199#step:2:550
pip install git+https://github.com/Project-MONAI/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@

# install cython to avoid error:
# https://github.com/Project-MONAI/model-zoo/actions/runs/4980530305/jobs/8913560199#step:2:550
git clone https://github.com/Project-MONAI/GenerativeModels.git
cd GenerativeModels/
git checkout f969c24f88d013dc0045fb7b2885a01fb219992b
python setup.py install
cd ..
pip install git+https://github.com/Project-MONAI/[email protected]
4 changes: 4 additions & 0 deletions ci/verify_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ def _get_weights_names(bundle: str):
# TODO: this function is temporarily used. It should be replaced by detailed config tests.
if bundle == "brats_mri_generative_diffusion":
return "model_autoencoder.pt", "model_autoencoder.ts"
if bundle == "brats_mri_axial_slices_generative_diffusion":
return "model_autoencoder.pt", None
return "model.pt", "model.ts"


def _get_net_id(bundle: str):
# TODO: this function is temporarily used. It should be replaced by detailed config tests.
if bundle == "brats_mri_generative_diffusion":
return "autoencoder_def"
if bundle == "brats_mri_axial_slices_generative_diffusion":
return "autoencoder_def"
return "network_def"


Expand Down
201 changes: 201 additions & 0 deletions models/brats_mri_axial_slices_generative_diffusion/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"imports": [
"$import torch",
"$from datetime import datetime",
"$from pathlib import Path",
"$from PIL import Image",
"$from scripts.utils import visualize_2d_image"
],
"bundle_root": ".",
"model_dir": "$@bundle_root + '/models'",
"output_dir": "$@bundle_root + '/output'",
"create_output_dir": "$Path(@output_dir).mkdir(exist_ok=True)",
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
"output_postfix": "$datetime.now().strftime('sample_%Y%m%d_%H%M%S')",
"channel": 0,
"spatial_dims": 2,
"image_channels": 1,
"latent_channels": 1,
"latent_shape": [
"@latent_channels",
64,
64
],
"autoencoder_def": {
"_target_": "generative.networks.nets.AutoencoderKL",
"spatial_dims": "@spatial_dims",
"in_channels": "@image_channels",
"out_channels": "@image_channels",
"latent_channels": "@latent_channels",
"num_channels": [
64,
128,
256
],
"num_res_blocks": 2,
"norm_num_groups": 32,
"norm_eps": 1e-06,
"attention_levels": [
false,
false,
false
],
"with_encoder_nonlocal_attn": true,
"with_decoder_nonlocal_attn": true
},
"network_def": {
"_target_": "generative.networks.nets.DiffusionModelUNet",
"spatial_dims": "@spatial_dims",
"in_channels": "@latent_channels",
"out_channels": "@latent_channels",
"num_channels": [
32,
64,
128,
256
],
"attention_levels": [
false,
true,
true,
true
],
"num_head_channels": [
0,
32,
32,
32
],
"num_res_blocks": 2
},
"load_autoencoder_path": "$@bundle_root + '/models/model_autoencoder.pt'",
"load_autoencoder": "$@autoencoder_def.load_state_dict(torch.load(@load_autoencoder_path))",
"autoencoder": "$@autoencoder_def.to(@device)",
"load_diffusion_path": "$@model_dir + '/model.pt'",
"load_diffusion": "$@network_def.load_state_dict(torch.load(@load_diffusion_path))",
"diffusion": "$@network_def.to(@device)",
"noise_scheduler": {
"_target_": "generative.networks.schedulers.DDIMScheduler",
"_requires_": [
"@load_diffusion",
"@load_autoencoder"
],
"num_train_timesteps": 1000,
"beta_start": 0.0015,
"beta_end": 0.0195,
"beta_schedule": "scaled_linear",
"clip_sample": false
},
"noise": "$torch.randn([1]+@latent_shape).to(@device)",
"set_timesteps": "$@noise_scheduler.set_timesteps(num_inference_steps=50)",
"inferer": {
"_target_": "scripts.ldm_sampler.LDMSampler",
"_requires_": "@set_timesteps"
},
"sample": "[email protected]_fn(@noise, @autoencoder, @diffusion, @noise_scheduler)",
"saver": {
"_target_": "SaveImage",
"_requires_": "@create_output_dir",
"output_dir": "@output_dir",
"output_postfix": "@output_postfix"
},
"generated_image": "$@sample",
"generated_image_np": "$@generated_image[0,0].cpu().numpy().transpose(1, 0)[::-1, ::-1]",
"img_pil": "$Image.fromarray(visualize_2d_image(@generated_image_np), 'RGB')",
"run": [
"$@img_pil.save(@output_dir+'/synimg_'+@output_postfix+'.png')"
]
}
Loading