Skip to content

Commit

Permalink
Merge pull request #334 from HenriquesLab/bmz_model_export_update
Browse files Browse the repository at this point in the history
Bmz model export update
  • Loading branch information
mariana-gferreira authored Oct 18, 2024
2 parents 56fa940 + 735ab59 commit 2debffb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Colab_notebooks/Latest_Notebook_versions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fnet (2D),1.14.1
fnet (3D),1.13.1
U-Net (2D),2.2.1
U-Net (3D),2.2.1
U-Net (2D) multilabel,2.1.3
U-Net (2D) multilabel,2.1.4
Kaibu,1.13.2
MaskRCNN,1.14.1
Noise2Void (2D),1.16.2
Expand Down
172 changes: 69 additions & 103 deletions Colab_notebooks/U-Net_2D_Multilabel_ZeroCostDL4Mic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
"outputs": [],
"source": [
"from __future__ import print_function\n",
"Notebook_version = '2.1.3'\n",
"Notebook_version = '2.1.4'\n",
"Network = 'U-Net (2D) multilabel'\n",
"\n",
"from builtins import any as b_any\n",
Expand Down Expand Up @@ -324,6 +324,7 @@
"from bioimageio.spec import save_bioimageio_package\n",
"from bioimageio.spec._internal.io import FileDescr\n",
"import bioimageio.core as bioimageio_core\n",
"from bioimageio.core.weight_converter.keras._tensorflow import convert_weights_to_tensorflow_saved_model_bundle\n",
"from zipfile import ZipFile\n",
"import requests\n",
"from bioimageio.spec.pretty_validation_errors import (\n",
Expand Down Expand Up @@ -790,56 +791,6 @@
" mask[mask <= threshold] = 0\n",
" return mask\n",
"\n",
"# BMZ model export functions\n",
"def make_author(author_input_info: str):\n",
" \"\"\"\n",
" Create an Author object from a string input.\n",
"\n",
" Args:\n",
" author_input_info: A string containing the author's name and affiliation.\n",
"\n",
" Returns:\n",
" An Author object\n",
" \"\"\"\n",
" if author_input_info.strip() == '':\n",
" return None\n",
"\n",
" auth_order = ['name', 'affiliation', 'email', 'orcid', 'github_user']\n",
" auth_dict = {}\n",
"\n",
" auth_info_split = author_input_info.split(',')\n",
"\n",
" for i in range(len(auth_info_split)):\n",
" if auth_info_split[i].strip() == 'None' or auth_info_split[i].strip() == '':\n",
" continue\n",
" else:\n",
" auth_dict[auth_order[i]] = auth_info_split[i].strip()\n",
"\n",
" return bioimageio_spec.Author(**auth_dict)\n",
"\n",
"def make_maintainer(maintainer_input_info: str):\n",
" \"\"\"\n",
" Create an Author object from a string input.\n",
"\n",
" Args:\n",
" author_input_info: A string containing the author's name and affiliation.\n",
"\n",
" Returns:\n",
" An Author object\n",
" \"\"\"\n",
" maint_order = [ 'github_user', 'name', 'affiliation', 'email', 'orcid']\n",
" maint_dict = {}\n",
"\n",
" maint_info_split = maintainer_input_info.split(',')\n",
"\n",
" for i in range(len(maint_info_split)):\n",
" if maint_info_split[i].strip() == 'None' or maint_info_split[i].strip() == '':\n",
" continue\n",
" else:\n",
" maint_dict[maint_order[i]] = maint_info_split[i].strip()\n",
"\n",
" return bioimageio_spec.Maintainer(**maint_dict)\n",
"\n",
"\n",
"# -------------- Other definitions -----------\n",
"W = '\\033[0m' # white (normal)\n",
Expand Down Expand Up @@ -1331,23 +1282,25 @@
"outputs": [],
"source": [
"# ------------- Initial user input ------------\n",
"#@markdown ###Path to training images:\n",
"#@markdown ### Path to training images:\n",
"Training_source = '' #@param {type:\"string\"}\n",
"Training_target = '' #@param {type:\"string\"}\n",
"\n",
"model_name = '' #@param {type:\"string\"}\n",
"model_path = '' #@param {type:\"string\"}\n",
"\n",
"labels = 3 #@param {type:\"number\"}\n",
"# @markdown\n",
"\n",
"#@markdown ###Training parameters:\n",
"#@markdown ### Training parameters:\n",
"#@markdown Number of epochs\n",
"number_of_epochs = 10#@param {type:\"number\"}\n",
"# @markdown\n",
"\n",
"#@markdown ###Advanced parameters:\n",
"#@markdown ### Advanced parameters:\n",
"Use_Default_Advanced_Parameters = True #@param {type:\"boolean\"}\n",
"\n",
"#@markdown ###If not, please input:\n",
"#@markdown ### If not, please input:\n",
"batch_size = 5#@param {type:\"integer\"}\n",
"number_of_steps = 0#@param {type:\"number\"}\n",
"pooling_steps = 3 #@param [1,2,3,4]{type:\"raw\"}\n",
Expand Down Expand Up @@ -2176,53 +2129,62 @@
"source": [
"# ------------- User input ------------\n",
"# information about the model\n",
"#@markdown ##Insert the information to document your model:\n",
"#@markdown ### Insert the information to document your model:\n",
"Trained_model_name = \"\" #@param {type:\"string\"}\n",
"Trained_model_description = \"\" #@param {type:\"string\"}\n",
"# @markdown \n",
"\n",
"#@markdown ### Author(s) names, comma separated:\n",
"author_name = \"Author 1 name, Author 2 name, Author 3 name\" #@param {type:\"string\"}\n",
"\n",
"#@markdown ###Author(s) - insert information separated by commas:\n",
"Trained_model_author_1 = \"Author 1 name, *Author 1 affiliation, *Author 1 email, *Author 1 ORCID, *Author 1 Github User\" #@param {type:\"string\"}\n",
"Trained_model_author_2 = \"Author 2 name, *Author 2 affiliation, *Author 2 email, *Author 2 ORCID, *Author 2 Github User\" #@param {type:\"string\"}\n",
"# @markdown\n",
"\n",
"# @markdown ###Model Packager:\n",
"packager_same_as_author = True #@param {type:\"boolean\"}\n",
"#@markdown - If not, please, provide the following information:\n",
"Trained_model_packager = \"Packager name, *Packager affiliation, *Packager email, *Packager ORCID, *Packager Github User\" #@param {type:\"string\"}\n",
"# @markdown ### Model Packager position in the list of authors:\n",
"# @markdown E.g.: 1 for Author 1, 2 for Author 2, etc.\n",
"Trained_model_packager = 1 #@param {type:\"number\"}\n",
"# @markdown\n",
"\n",
"# @markdown ###Model Maintainer:\n",
"maintainer_same_as_author = True #@param {type:\"boolean\"}\n",
"#@markdown - If not, please, provide the following information:\n",
"Trained_model_maintainer = \"Maintainer Github User, *Maintainer name, *Maintainer affiliation, *Maintainer email, *Maintainer ORCID\" #@param {type:\"string\"}\n",
"# @markdown ### Model Maintainer:\n",
"Trained_model_maintainer = \"Maintainer Github User\" #@param {type:\"string\"}\n",
"# @markdown #### If maintainer name is in author list provide the corresponding number:\n",
"# @markdown E.g.: 1 for Author 1, 2 for Author 2, etc.\n",
"maintainer_author_number = 1 #@param {type:\"number\"}\n",
"\n",
"# @markdown ###License:\n",
"Trained_model_license = 'CC-BY-4.0' #@param {type:\"string\"}\n",
"# @markdown\n",
"\n",
"# @markdown ### License:\n",
"Trained_model_license = 'CC-BY-4.0' #@param [\"BSD-Protection\", \"CC-BY-SA-2.0\", \"Unicode-DFS-2016\", \"CC-BY-ND-2.5\", \"CC-BY-NC-ND-3.0\", \"BSD-3-Clause-LBNL\", \"NCGL-UK-2.0\", \"GPL-1.0+\", \"OSL-1.0\", \"MIT\", \"MPL-2.0\", \"CC-BY-NC-1.0\", \"CC-BY-NC-ND-2.5\", \"CC-BY-SA-2.0-UK\", \"BSD-4-Clause-Shortened\", \"CC-BY-4.0\"]\n",
"Trained_model_references = [\"Falk et al. Nature Methods 2019\", \"Ronneberger et al. arXiv in 2015\", \"Lucas von Chamier et al. biorXiv 2020\"]\n",
"Trained_model_DOI = [\"https://doi.org/10.1038/s41592-018-0261-2\",\"https://doi.org/10.1007/978-3-319-24574-4_28\", \"https://doi.org/10.1101/2020.03.20.000133\"]\n",
"# @markdown\n",
"\n",
"# Training data\n",
"# ---------------------------------------\n",
"#@markdown ##Include information about training data (optional):\n",
"#@markdown ### Include information about training data (optional):\n",
"include_training_data = False #@param {type: \"boolean\"}\n",
"#@markdown ### - If it is published in the BioImage Model Zoo, please, provide the ID\n",
"#@markdown #### If it is published in the BioImage Model Zoo, please, provide the ID\n",
"data_from_bioimage_model_zoo = False #@param {type: \"boolean\"}\n",
"training_data_ID = ''#@param {type:\"string\"}\n",
"#@markdown ### - If not, please provide the URL to the data and a short description to be added to the README.md file\n",
"#@markdown #### If not, please provide the URL to the data and a short description to be added to the README.md file\n",
"training_data_source = ''#@param {type:\"string\"}\n",
"training_data_description = ''#@param {type:\"string\"}\n",
"# @markdown\n",
"\n",
"# Add input image information\n",
"# ---------------------------------------\n",
"#@markdown ##Indicate the minimum x/y size of the image (in pixels) and step size (in pixels) to be used for block/tiling:\n",
"#@markdown ### Minimum x/y size of the image and step size (in pixels) for block/tiling:\n",
"# information about the example image\n",
"min_size = 64 #@param {type:\"number\"}\n",
"step_size = 16 #@param {type:\"number\"}\n",
"#@markdown ##Do you want to choose the example image?\n",
"# @markdown\n",
"\n",
"#@markdown ### Do you want to choose the example image?\n",
"default_example_image = True #@param {type:\"boolean\"}\n",
"#@markdown ###If not, please input:\n",
"#@markdown #### If not, please input:\n",
"fileID = \"\" #@param {type:\"string\"}\n",
"if default_example_image:\n",
" fileID = os.path.join(Source_QC_folder, os.listdir(Source_QC_folder)[0])\n",
"# @markdown\n",
"\n",
"# Load the model and process the example image\n",
"# ---------------------------------------\n",
Expand All @@ -2244,31 +2206,18 @@
" training_data = None\n",
"\n",
"# create the author/maintainer/packager spec input\n",
"author_1_spec = make_author(Trained_model_author_1)\n",
"authors = [author_1_spec]\n",
"authors = [] \n",
"for i in range(len(author_name.split(','))):\n",
" authors.append(bioimageio_spec.Author(name= author_name.split(',')[i].strip()))\n",
"\n",
"# check if author 2 was filled\n",
"if 'Author 2 name' not in Trained_model_author_2:\n",
" author_2_spec = make_author(Trained_model_author_2)\n",
" authors.append(author_2_spec)\n",
"assert len(author_name.split(',')) >= Trained_model_packager, \"Author list has less authors than the packager number inserted.\"\n",
"packager = [authors[Trained_model_packager-1]]\n",
"\n",
"if packager_same_as_author:\n",
" packager_spec = author_1_spec\n",
"else:\n",
" packager_spec = make_author(Trained_model_packager)\n",
"\n",
"if maintainer_same_as_author:\n",
" if author_1_spec.github_user != None:\n",
" maintainer_from_author = [str(author_1_spec.github_user), str(author_1_spec.name), str(author_1_spec.affiliation), str(author_1_spec.email), str(author_1_spec.orcid)]\n",
" maintainer_str = ', '.join(maintainer_from_author)\n",
" maintainer_spec = make_maintainer(maintainer_str)\n",
" else:\n",
" print('Please, provide the author GitHub username in the author information')\n",
"else:\n",
" maintainer_spec = make_maintainer(Trained_model_maintainer)\n",
"if len(author_name.split(',')) < maintainer_author_number:\n",
" print(\"Author list has less authors than the maintainer number inserted, no maintainer name will be added.\")\n",
"maintainer = [bioimageio_spec.Maintainer(github_user=Trained_model_maintainer, name=author_name.split(',')[maintainer_author_number-1].strip())]\n",
"\n",
"\n",
"# I would recommend using CCBY-4 as licence\n",
"# I would recommend using CCBY-4 as license\n",
"license = Trained_model_license\n",
"\n",
"# where to save the model\n",
Expand Down Expand Up @@ -2401,8 +2350,8 @@
"plt.imsave(cover_path, cover, cmap='gray')\n",
"\n",
"# make weights description\n",
"unet_tf_weights = bioimageio_spec.KerasHdf5WeightsDescr(source=weight_path, tensorflow_version=tf.__version__)\n",
"unet_weights = bioimageio_spec.WeightsDescr(keras_hdf5=unet_tf_weights)\n",
"unet_keras_weights = bioimageio_spec.KerasHdf5WeightsDescr(source=weight_path, tensorflow_version=tf.__version__)\n",
"unet_weights = bioimageio_spec.WeightsDescr(keras_hdf5=unet_keras_weights)\n",
"\n",
"# create model description for export\n",
"model_description = bioimageio_spec.ModelDescr(name=Trained_model_name,\n",
Expand All @@ -2413,17 +2362,28 @@
" cite=citation_spec,\n",
" license=license,\n",
"\n",
" maintainers=[maintainer_spec],\n",
" maintainers=maintainer,\n",
" tags=['zerocostdl4mic', 'deepimagej', 'segmentation', 'unet'],\n",
" documentation= documentation_path,\n",
" inputs=[input_tensor],\n",
" outputs=[output_tensor],\n",
" packaged_by=[packager_spec],\n",
" packaged_by=packager,\n",
" weights=unet_weights,\n",
" training_data=training_data,\n",
"\n",
" )\n",
"\n",
"# convert the keras weights to tensorflow and add them to the model\n",
"tf_weight_path = os.path.join(full_QC_model_path, \"tf_weights\")\n",
"# we need to make sure that the tf weight folder does not exist\n",
"if os.path.exists(tf_weight_path):\n",
" rmtree(Path(tf_weight_path))\n",
"convert_weights_to_tensorflow_saved_model_bundle(model_description, Path(tf_weight_path + \".zip\"))\n",
"\n",
"unet_tf_saved_model_bundle = bioimageio_spec.TensorflowSavedModelBundleWeightsDescr(source = Path(tf_weight_path + \".zip\"), tensorflow_version=tf.__version__)\n",
"\n",
"model_description.weights.tensorflow_saved_model_bundle = unet_tf_saved_model_bundle\n",
"\n",
"\n",
"# test model\n",
"summary = bioimageio_core.test_model(model_description, weight_format=\"keras_hdf5\")\n",
Expand Down Expand Up @@ -2588,6 +2548,11 @@
"# **7. Version log**\n",
"\n",
"---\n",
"<font size = 4>**v2.1.4**: \n",
"\n",
"* Bug fixes and formatting\n",
"* Updated Bioimage.IO model export to latest version (core-0.6.10, spec-0.5.3.3)\n",
"\n",
"<font size = 4>**v2.1.3**: \n",
"\n",
"* Updated Bioimage.IO model export to latest version (core-0.6.9, spec-0.5.3.2)\n",
Expand Down Expand Up @@ -2626,7 +2591,8 @@
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "bioimageio-core",
"language": "python",
"name": "python3"
},
"language_info": {
Expand All @@ -2639,7 +2605,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 2debffb

Please sign in to comment.