Skip to content

Commit a4b63d5

Browse files
[part 1] Update all bundles to Huggingface and fix missing dependencies (#740)
Part of #739 ### Description This PR is used to: 1. update a part of bundle versions to enable huggingface hosting. 2. update premerge CI test scripts to resolve dependency conflicts (for example, `requirements-dev.txt` has conflicts with some bundles' requirements) 3. fix missing dependencies issues on some bundles ### Status **Ready** ### Please ensure all the checkboxes: <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Codeformat tests passed locally by running `./runtests.sh --codeformat`. - [ ] In-line docstrings updated. - [ ] Update `version` and `changelog` in `metadata.json` if changing an existing bundle. - [ ] Please ensure the naming rules in config files meet our requirements (please refer to: `CONTRIBUTING.md`). - [ ] Ensure versions of packages such as `monai`, `pytorch` and `numpy` are correct in `metadata.json`. - [ ] Descriptions should be consistent with the content, such as `eval_metrics` of the provided weights and TorchScript modules. - [ ] Files larger than 25MB are excluded and replaced by providing download links in `large_file.yml`. - [ ] Avoid using path that contains personal information within config files (such as use `/home/your_name/` for `"bundle_root"`). --------- Signed-off-by: Yiheng Wang <vennw@nvidia.com>
1 parent 18b0e58 commit a4b63d5

23 files changed

Lines changed: 127 additions & 90 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,5 @@ temp/
131131
*.zip
132132
models/*/models/*
133133
models/*/output/*
134+
models/*/eval/*
134135
hf_models/*/eval/*

ci/bundle_custom_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
# This dict is used for our CI tests to install required dependencies that cannot be installed by `pip install` directly.
5959
# If a bundle has this kind of dependencies, please add the bundle name (key), and the path of the install script (value)
6060
# into the dict.
61-
install_dependency_dict = {"maisi_ct_generative": "ci/install_scripts/install_maisi_ct_generative_dependency.sh"}
61+
install_dependency_dict = {}
6262

6363
# This list is used for our CI tests to determine whether a bundle supports TensorRT export. Related
6464
# test will be employed for bundles in the dict.

ci/download_latest_bundle.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,14 @@
1414
import os
1515

1616
from monai.bundle import download
17-
from utils import get_checksum, get_hash_func, get_latest_version, get_version_checksum
17+
from utils import get_latest_version
1818

1919

2020
def download_latest_bundle(bundle_name: str, models_path: str, download_path: str):
2121
model_info_path = os.path.join(models_path, "model_info.json")
2222
version = get_latest_version(bundle_name=bundle_name, model_info_path=model_info_path)
23-
checksum = get_version_checksum(bundle_name=bundle_name, version=version, model_info_path=model_info_path)
2423
download(name=bundle_name, source="monaihosting", version=version, bundle_dir=download_path)
2524

26-
# verify checksum
27-
hash_func = get_hash_func(hash_type="sha1")
28-
bundle_zip_name = f"{bundle_name}_v{version}.zip"
29-
downloaded_checksum = get_checksum(dst_path=os.path.join(download_path, bundle_zip_name), hash_func=hash_func)
30-
if checksum != downloaded_checksum:
31-
raise ValueError(f"Checksum of {bundle_zip_name} is not correct.")
32-
print(f"Checksum of downloaded bundle {bundle_name} is correct.")
33-
3425

3526
if __name__ == "__main__":
3627
parser = argparse.ArgumentParser(description="")

ci/get_bundle_requirements.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def increment_version(version):
5353
return result_version
5454

5555

56-
def get_requirements(bundle, models_path):
56+
def get_requirements(bundle, models_path, requirements_file):
5757
"""
5858
This function is used to produce a requirements txt file, and print a string
5959
which shows the filename. The printed string can be used in shell scripts.
@@ -86,14 +86,12 @@ def get_requirements(bundle, models_path):
8686
else:
8787
if "pytorch_version" in metadata.keys():
8888
# remove torch from libs
89-
libs = [lib for lib in libs if "torch" not in lib]
89+
libs = [lib for lib in libs if "torch" not in lib or lib == "pytorch-ignite"]
9090

9191
if len(libs) > 0:
92-
requirements_file_name = f"requirements_{bundle}.txt"
93-
with open(requirements_file_name, "w") as f:
92+
with open(requirements_file, "w") as f:
9493
for line in libs:
9594
f.write(f"{line}\n")
96-
print(requirements_file_name)
9795

9896

9997
def get_install_script(bundle):
@@ -109,11 +107,13 @@ def get_install_script(bundle):
109107
parser.add_argument("-b", "--b", type=str, help="bundle name.")
110108
parser.add_argument("-p", "--p", type=str, default="models", help="models path.")
111109
parser.add_argument("--get_script", type=bool, default=False, help="whether to get the install script.")
110+
parser.add_argument("--requirements_file", type=str, help="output filename for requirements.")
112111
args = parser.parse_args()
113112
bundle = args.b
114113
models_path = args.p
115114
get_script = args.get_script
116115
if get_script is True:
117116
get_install_script(bundle)
118117
else:
119-
get_requirements(bundle, models_path)
118+
requirements_file = args.requirements_file
119+
get_requirements(bundle, models_path, requirements_file)

ci/run_premerge_cpu.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ verify_bundle() {
4949
done
5050
echo 'Run verify bundle...'
5151
pip install -r requirements.txt
52+
# install extra dependencies for get changed bundle
53+
pip install jsonschema gdown pyyaml
5254
head_ref=$(git rev-parse HEAD)
5355
git fetch origin dev $head_ref
5456
# achieve all changed files in 'models'
@@ -66,18 +68,19 @@ verify_bundle() {
6668
if is_excluded "$bundle"; then
6769
echo "skip '$bundle' cpu premerge tests."
6870
else
69-
pip install -r requirements-dev.txt
7071
# get required libraries according to the bundle's metadata file
71-
requirements=$(python $(pwd)/ci/get_bundle_requirements.py --b "$bundle")
72+
requirements_file="requirements_$bundle.txt"
73+
python $(pwd)/ci/get_bundle_requirements.py --b "$bundle" --requirements_file "$requirements_file"
7274
# check if ALLOW_MONAI_RC is set to 1, if so, append --pre to the pip install command
7375
if [ $ALLOW_MONAI_RC = true ]; then
7476
include_pre_release="--pre"
7577
else
7678
include_pre_release=""
7779
fi
78-
if [ ! -z "$requirements" ]; then
80+
# Check if the requirements file exists and is not empty
81+
if [ -s "$requirements_file" ]; then
7982
echo "install required libraries for bundle: $bundle"
80-
pip install $include_pre_release -r "$requirements"
83+
pip install $include_pre_release -r "$requirements_file"
8184
fi
8285
# verify bundle
8386
python $(pwd)/ci/verify_bundle.py -b "$bundle" -m "min" # min tests on cpu

ci/run_premerge_gpu.sh

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ fi
3131

3232
init_venv() {
3333
if [ ! -d "model_zoo_venv" ]; then # Check if the venv directory does not exist
34-
echo "initializing pip environment: $1"
34+
echo "initializing pip environment"
3535
python -m venv model_zoo_venv
3636
source model_zoo_venv/bin/activate
3737
pip install --upgrade pip wheel
38-
pip install -r $1
38+
pip install jsonschema gdown pyyaml parameterized fire
3939
export PYTHONPATH=$PWD
4040
else
4141
echo "Virtual environment model_zoo_venv already exists. Activating..."
4242
source model_zoo_venv/bin/activate
43+
pip install --upgrade pip wheel
44+
pip install jsonschema gdown pyyaml parameterized fire
45+
export PYTHONPATH=$PWD
4346
fi
4447
}
4548

@@ -54,9 +57,8 @@ remove_venv() {
5457
}
5558

5659
set_local_env() {
57-
echo "set local pip environment: $1"
60+
echo "set local pip environment"
5861
pip install --upgrade pip wheel
59-
pip install -r $1
6062
export PYTHONPATH=$PWD
6163
}
6264

@@ -75,24 +77,20 @@ verify_bundle() {
7577
python $(pwd)/ci/prepare_schema.py --l "$bundle_list"
7678
for bundle in $bundle_list;
7779
do
78-
# Check if the bundle is "maisi_ct_generative", if so, set local environment (venv cannot work with xformers)
79-
if [ "$bundle" == "maisi_ct_generative" ]; then
80-
echo "Special handling for maisi_ct_generative bundle"
81-
set_local_env requirements-dev.txt
82-
else
83-
init_venv requirements-dev.txt
84-
fi
8580
# get required libraries according to the bundle's metadata file
86-
requirements=$(python $(pwd)/ci/get_bundle_requirements.py --b "$bundle")
81+
requirements_file="requirements_$bundle.txt"
82+
python $(pwd)/ci/get_bundle_requirements.py --b "$bundle" --requirements_file "$requirements_file"
8783
# check if ALLOW_MONAI_RC is set to 1, if so, append --pre to the pip install command
8884
if [ $ALLOW_MONAI_RC = true ]; then
8985
include_pre_release="--pre"
9086
else
9187
include_pre_release=""
9288
fi
93-
if [ ! -z "$requirements" ]; then
89+
init_venv
90+
# Check if the requirements file exists and is not empty
91+
if [ -s "$requirements_file" ]; then
9492
echo "install required libraries for bundle: $bundle"
95-
pip install $include_pre_release -r "$requirements"
93+
pip install $include_pre_release -r "$requirements_file"
9694
fi
9795
# get extra install script if exists
9896
extra_script=$(python $(pwd)/ci/get_bundle_requirements.py --b "$bundle" --get_script True)

ci/run_regular_tests_cpu.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,23 @@ verify_release_bundle() {
3434
echo 'Run verify bundle...'
3535
# get all bundles
3636
download_path="download"
37-
pip install -r requirements-dev.txt
3837
pip install git+https://github.com/Project-MONAI/MONAI.git@dev # project-monai/model-zoo issue #505
38+
pip install jsonschema gdown
3939
# download bundle from releases
4040
python $(pwd)/ci/download_latest_bundle.py --b "$bundle" --models_path $(pwd)/models --p "$download_path"
4141
# get required libraries according to the bundle's metadata file
42-
requirements=$(python $(pwd)/ci/get_bundle_requirements.py --b "$bundle" --p "$download_path")
42+
requirements_file="requirements_$bundle.txt"
43+
python $(pwd)/ci/get_bundle_requirements.py --b "$bundle" --requirements_file "$requirements_file"
4344
# check if ALLOW_MONAI_RC is set to 1, if so, append --pre to the pip install command
4445
if [ $ALLOW_MONAI_RC = true ]; then
4546
include_pre_release="--pre"
4647
else
4748
include_pre_release=""
4849
fi
49-
if [ ! -z "$requirements" ]; then
50+
# Check if the requirements file exists and is not empty
51+
if [ -s "$requirements_file" ]; then
5052
echo "install required libraries for bundle: $bundle"
51-
pip install $include_pre_release -r "$requirements"
53+
pip install $include_pre_release -r "$requirements_file"
5254
fi
5355
# verify bundle
5456
python $(pwd)/ci/verify_bundle.py -b "$bundle" -p "$download_path" -m "regular" # regular tests on cpu

ci/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import shutil
1818
from typing import List
1919

20-
from huggingface_hub import HfApi
2120
from monai.apps.utils import download_url
2221
from monai.bundle.config_parser import ConfigParser
2322
from monai.utils import look_up_option, optional_import
2423

24+
huggingface_hub, _ = optional_import("huggingface_hub")
2525
Github, _ = optional_import("github", name="Github")
2626

2727
SUPPORTED_HASH_TYPES = {"md5": hashlib.md5, "sha1": hashlib.sha1, "sha256": hashlib.sha256, "sha512": hashlib.sha512}
@@ -178,7 +178,7 @@ def get_existing_bundle_list(model_info):
178178

179179

180180
def create_bundle_to_huggingface(bundle_name: str, org_name: str):
181-
api = HfApi()
181+
api = huggingface_hub.HfApi()
182182
try:
183183
_ = api.create_repo(repo_id=f"{org_name}/{bundle_name}", repo_type="model", private=False)
184184
except Exception as e:
@@ -189,7 +189,7 @@ def create_bundle_to_huggingface(bundle_name: str, org_name: str):
189189

190190

191191
def upload_version_to_huggingface(bundle_name: str, version: str, root_path: str, org_name: str):
192-
api = HfApi()
192+
api = huggingface_hub.HfApi()
193193

194194
try:
195195
# if no file is changed, will skip uploading automatically

models/brain_image_synthesis_latent_diffusion_model/configs/metadata.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
22
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20240725.json",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"changelog": {
5+
"1.0.2": "fix missing dependencies",
56
"1.0.1": "update to huggingface hosting",
67
"1.0.0": "Initial release"
78
},
89
"monai_version": "1.4.0",
910
"pytorch_version": "2.5.1",
1011
"numpy_version": "1.26.4",
1112
"required_packages_version": {
12-
"nibabel": "5.3.2"
13+
"nibabel": "5.3.2",
14+
"einops": "0.7.0"
1315
},
1416
"task": "Brain image synthesis",
1517
"description": "A generative model for creating high-resolution 3D brain MRI based on UK Biobank",

models/brats_mri_axial_slices_generative_diffusion/configs/metadata.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20240725.json",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"changelog": {
5+
"1.1.3": "update to huggingface hosting and fix missing dependencies",
56
"1.1.2": "update issue for IgniteInfo",
67
"1.1.1": "enable tensorrt",
78
"1.1.0": "update to use monai 1.4, model ckpt not changed, rm GenerativeAI repo",
@@ -21,7 +22,10 @@
2122
"numpy_version": "1.24.4",
2223
"required_packages_version": {
2324
"nibabel": "5.2.1",
24-
"lpips": "0.1.4"
25+
"lpips": "0.1.4",
26+
"tensorboard": "2.17.0",
27+
"einops": "0.7.0",
28+
"pytorch-ignite": "0.4.11"
2529
},
2630
"supported_apps": {},
2731
"name": "BraTS MRI axial slices latent diffusion generation",

0 commit comments

Comments
 (0)