From e1c4788fac0bb21d3b245e349dfcf0ffa3e8d114 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Thu, 30 May 2024 23:55:19 +0000 Subject: [PATCH 01/16] Add docs config --- .readthedocs.yaml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .readthedocs.yaml diff --git a/.readthedocs.yaml b/.readthedocs.yaml new file mode 100644 index 00000000..81346fa5 --- /dev/null +++ b/.readthedocs.yaml @@ -0,0 +1,22 @@ +# Read the Docs configuration file for Sphinx projects +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +# Required +version: 2 + +# Set the OS, Python version and other tools you might need +build: + os: ubuntu-22.04 + tools: + python: "3.12" + +# Build documentation in the "docs/" directory with Sphinx +sphinx: + configuration: docs/conf.py + +# Optional but recommended, declare the Python requirements required +# to build your documentation +# See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html +python: + install: + - requirements: docs/requirements.txt \ No newline at end of file From fcd44edda01b331e535cd0686fe14bc1ac2f494b Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 00:09:52 +0000 Subject: [PATCH 02/16] Fix mock --- docs/conf.py | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/conf.py b/docs/conf.py index 0e4d19d8..9d99277b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -105,6 +105,7 @@ def get_version(): "efficientnet-pytorch", "segmentation_models_pytorch.encoders", "segmentation_models_pytorch.utils", + "huggingface_hub", # 'segmentation_models_pytorch.base', ] From 9adce21198db924aaaf29db03240a54ed1502a3a Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 00:13:31 +0000 Subject: [PATCH 03/16] Add huggingface_hub to reqs for docs --- docs/requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/requirements.txt b/docs/requirements.txt index 4d7d4875..0de39c80 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,4 @@ faculty-sphinx-theme==0.2.2 six==1.15.0 -autodocsumm \ No newline at end of file +autodocsumm +huggingface_hub \ No newline at end of file From 887c8d66d3e0534307ca73b6de4419c53f522dc2 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 00:13:47 +0000 Subject: [PATCH 04/16] Remove from mocks --- docs/conf.py | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 9d99277b..0e4d19d8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -105,7 +105,6 @@ def get_version(): "efficientnet-pytorch", "segmentation_models_pytorch.encoders", "segmentation_models_pytorch.utils", - "huggingface_hub", # 'segmentation_models_pytorch.base', ] From fc7080127dd75a15760613ca62d91c0f2d5f0674 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 00:16:51 +0000 Subject: [PATCH 05/16] Fix --- docs/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/requirements.txt b/docs/requirements.txt index 0de39c80..5100586b 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,4 @@ +sphinx<7 faculty-sphinx-theme==0.2.2 six==1.15.0 autodocsumm From 959ecbba10fca7a975077b51dc9809bb37cbf4c1 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 00:22:30 +0000 Subject: [PATCH 06/16] Change theme --- docs/conf.py | 2 +- docs/requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 0e4d19d8..f21c0c8c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -73,7 +73,7 @@ def get_version(): # import karma_sphinx_theme # html_theme = "karma_sphinx_theme" -html_theme = "faculty_sphinx_theme" +html_theme = "sphinx_book_theme" # import catalyst_sphinx_theme # html_theme = "catalyst_sphinx_theme" diff --git a/docs/requirements.txt b/docs/requirements.txt index 5100586b..7e5b1eff 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,5 +1,5 @@ sphinx<7 -faculty-sphinx-theme==0.2.2 +sphinx-book-theme==1.1.2 six==1.15.0 autodocsumm huggingface_hub \ No newline at end of file From 92089f115c496fdc47f2c824988f80433bc5d220 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 08:09:56 +0000 Subject: [PATCH 07/16] Fix --- docs/conf.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index f21c0c8c..6395a5c8 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -67,8 +67,8 @@ def get_version(): # a list of builtin themes. # -html_theme = "sphinx_rtd_theme" -html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] +# html_theme = "sphinx_rtd_theme" +# html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # import karma_sphinx_theme # html_theme = "karma_sphinx_theme" From 052a505b6066d0c105cf3d5fcb72b218519460a6 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 08:11:21 +0000 Subject: [PATCH 08/16] Fix --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 6395a5c8..c7dde9e5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,7 +16,7 @@ import sys import datetime -import sphinx_rtd_theme +# import sphinx_rtd_theme sys.path.append("..") From 2c137bd85ad0638682d732e2d8c75d300a86e49c Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 10:37:12 +0000 Subject: [PATCH 09/16] Update emoji --- docs/encoders.rst | 2 +- docs/encoders_timm.rst | 2 +- docs/insights.rst | 2 +- docs/install.rst | 2 +- docs/metrics.rst | 2 +- docs/models.rst | 2 +- docs/quickstart.rst | 4 ++-- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/encoders.rst b/docs/encoders.rst index d64607b8..652745b7 100644 --- a/docs/encoders.rst +++ b/docs/encoders.rst @@ -1,4 +1,4 @@ -πŸ” Available Encoders +πŸ” Available Encoders ===================== ResNet diff --git a/docs/encoders_timm.rst b/docs/encoders_timm.rst index 3441fdca..26a18a64 100644 --- a/docs/encoders_timm.rst +++ b/docs/encoders_timm.rst @@ -1,4 +1,4 @@ -πŸͺ Timm Encoders +🎯 Timm Encoders ~~~~~~~~~~~~~~~~ Pytorch Image Models (a.k.a. timm) has a lot of pretrained models and interface which allows using these models as encoders in smp, diff --git a/docs/insights.rst b/docs/insights.rst index 6489dfd1..ad5355b9 100644 --- a/docs/insights.rst +++ b/docs/insights.rst @@ -1,4 +1,4 @@ -πŸ”§ Insights +πŸ’‘ Insights =========== 1. Models architecture diff --git a/docs/install.rst b/docs/install.rst index 583a2527..e43c0f38 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -1,4 +1,4 @@ -πŸ›  Installation +βš™οΈ Installation =============== PyPI version: diff --git a/docs/metrics.rst b/docs/metrics.rst index db5ec581..fc06c131 100644 --- a/docs/metrics.rst +++ b/docs/metrics.rst @@ -1,4 +1,4 @@ -πŸ“ˆ Metrics +πŸ“ Metrics ========== Functional metrics diff --git a/docs/models.rst b/docs/models.rst index 47de61ee..190f38b6 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -1,4 +1,4 @@ -πŸ“¦ Segmentation Models +πŸ•ΈοΈ Segmentation Models ============================== Unet diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 60f4f287..0eee6615 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -1,4 +1,4 @@ -⏳ Quick Start +πŸš€ Quick Start ============== **1. Create segmentation model** @@ -17,7 +17,7 @@ Segmentation model is just a PyTorch nn.Module, which can be created as easy as: ) - see table with available model architectures -- see table with avaliable encoders and its corresponding weights +- see table with available encoders and its corresponding weights **2. Configure data preprocessing** From 2851474872a2004672300b2959184cd6d25dc3dd Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 10:52:03 +0000 Subject: [PATCH 10/16] Table of content --- docs/models.rst | 56 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/docs/models.rst b/docs/models.rst index 190f38b6..0a43cf03 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -1,40 +1,78 @@ πŸ•ΈοΈ Segmentation Models ============================== + +Table of contents: +------------------ +- [Unet](#unet) +- [Unet++](#unetplusplus) +- [MAnet](#manet) +- [Linknet](#linknet) +- [FPN](#fpn) +- [PSPNet](#pspnet) +- [PAN](#pan) +- [DeepLabV3](#deeplabv3) +- [DeepLabV3+](#deeplabv3plus) + + +.. _unet: + Unet ~~~~ .. autoclass:: segmentation_models_pytorch.Unet + +.. _unetplusplus: + Unet++ ~~~~~~ .. autoclass:: segmentation_models_pytorch.UnetPlusPlus -MAnet -~~~~~~ -.. autoclass:: segmentation_models_pytorch.MAnet -Linknet -~~~~~~~ -.. autoclass:: segmentation_models_pytorch.Linknet +.. _fpn: FPN ~~~ .. autoclass:: segmentation_models_pytorch.FPN + +.. _pspnet: + PSPNet ~~~~~~ .. autoclass:: segmentation_models_pytorch.PSPNet -PAN -~~~ -.. autoclass:: segmentation_models_pytorch.PAN + +.. _deeplabv3: DeepLabV3 ~~~~~~~~~ .. autoclass:: segmentation_models_pytorch.DeepLabV3 + +.. _deeplabv3plus: + DeepLabV3+ ~~~~~~~~~~ .. autoclass:: segmentation_models_pytorch.DeepLabV3Plus +.. _linknet: + +Linknet +~~~~~~~ +.. autoclass:: segmentation_models_pytorch.Linknet + + +.. _manet: + +MAnet +~~~~~~ +.. autoclass:: segmentation_models_pytorch.MAnet + + +.. _pan: + +PAN +~~~ +.. autoclass:: segmentation_models_pytorch.PAN From 316e2e8dc7cb78e83b4779906502a049db35f74f Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 10:52:17 +0000 Subject: [PATCH 11/16] Links in doc --- docs/quickstart.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 0eee6615..5063eb0f 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -16,8 +16,9 @@ Segmentation model is just a PyTorch nn.Module, which can be created as easy as: classes=3, # model output channels (number of classes in your dataset) ) -- see table with available model architectures -- see table with available encoders and its corresponding weights +- Check the page with available :doc:`model architectures `. +- Check the table with :doc:`available ported encoders and its corresponding weights `. +- `Pytorch Image Models (timm) `_ encoders are also supported, check it :doc:`here`. **2. Configure data preprocessing** From a159be0acb6d905575303610f52b30ef18f20da5 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 11:08:16 +0000 Subject: [PATCH 12/16] Update content --- docs/models.rst | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/docs/models.rst b/docs/models.rst index 0a43cf03..003908a0 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -2,18 +2,8 @@ ============================== -Table of contents: ------------------- -- [Unet](#unet) -- [Unet++](#unetplusplus) -- [MAnet](#manet) -- [Linknet](#linknet) -- [FPN](#fpn) -- [PSPNet](#pspnet) -- [PAN](#pan) -- [DeepLabV3](#deeplabv3) -- [DeepLabV3+](#deeplabv3plus) - +.. contents:: + :local: .. _unet: From eccb16cdf0caa335d34204bde0e353c2aeb71c25 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 11:08:40 +0000 Subject: [PATCH 13/16] Update examples --- docs/quickstart.rst | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 5063eb0f..76a762d8 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -34,4 +34,18 @@ All encoders have pretrained weights. Preparing your data the same way as during **3. Congratulations!** πŸŽ‰ -You are done! Now you can train your model with your favorite framework! +You are done! Now you can train your model with your favorite framework, or as simple as: + +.. code-block:: python + + for images, gt_masks in dataloader: + + predicted_mask = model(image) + loss = loss_fn(prediction, mask) + + loss.backward() + optimizer.step() + +Check the following examples: +- Finetuning notebook on Oxford Pet dataset with `PyTorch Lightning `_ +- Finetuning script for cloth segmentation with `PyTorch Lightning `_ \ No newline at end of file From 2eaf008de815f17604b6505d0c5987442d72908a Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 11:17:33 +0000 Subject: [PATCH 14/16] Update --- docs/quickstart.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 76a762d8..99bb6f99 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -41,11 +41,13 @@ You are done! Now you can train your model with your favorite framework, or as s for images, gt_masks in dataloader: predicted_mask = model(image) - loss = loss_fn(prediction, mask) + loss = loss_fn(predicted_mask, gt_masks) loss.backward() optimizer.step() Check the following examples: -- Finetuning notebook on Oxford Pet dataset with `PyTorch Lightning `_ -- Finetuning script for cloth segmentation with `PyTorch Lightning `_ \ No newline at end of file + +- Finetuning notebook on Oxford Pet dataset with `PyTorch Lightning `_ .. image:: https://colab.research.google.com/assets/colab-badge.svg + :target: https://colab.research.google.com/github/qubvel/segmentation_models.pytorch/blob/master/examples/binary_segmentation_intro.ipynb +- Finetuning script for cloth segmentation with `PyTorch Lightning `_ From 349a6ca2996d499cfbf752b273c93e97174c7ab0 Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 11:22:12 +0000 Subject: [PATCH 15/16] Update --- docs/quickstart.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 99bb6f99..0c27ab4a 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -48,6 +48,9 @@ You are done! Now you can train your model with your favorite framework, or as s Check the following examples: -- Finetuning notebook on Oxford Pet dataset with `PyTorch Lightning `_ .. image:: https://colab.research.google.com/assets/colab-badge.svg +.. |colab-badge| image:: https://colab.research.google.com/assets/colab-badge.svg :target: https://colab.research.google.com/github/qubvel/segmentation_models.pytorch/blob/master/examples/binary_segmentation_intro.ipynb + :alt: Open In Colab + +- Finetuning notebook on Oxford Pet dataset with `PyTorch Lightning `_ |colab-badge| - Finetuning script for cloth segmentation with `PyTorch Lightning `_ From ad700c11485d6508a1cc31c935f1ffedfd7cd9fc Mon Sep 17 00:00:00 2001 From: Pavel Iakubovskii Date: Fri, 31 May 2024 11:33:44 +0000 Subject: [PATCH 16/16] Add save load --- docs/index.rst | 1 + docs/save_load.rst | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 docs/save_load.rst diff --git a/docs/index.rst b/docs/index.rst index bb75d6e6..54bdea00 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -17,6 +17,7 @@ Welcome to Segmentation Models's documentation! encoders_timm losses metrics + save_load insights diff --git a/docs/save_load.rst b/docs/save_load.rst new file mode 100644 index 00000000..0aec7d50 --- /dev/null +++ b/docs/save_load.rst @@ -0,0 +1,74 @@ +πŸ“‚ Saving and Loading +===================== + +In this section, we will discuss how to save a trained model, push it to the Hugging Face Hub, and load it back for later use. + +Saving and Sharing a Model +-------------------------- + +Once you have trained your model, you can save it using the `.save_pretrained` method. This method saves the model configuration and weights to a directory of your choice. +And, optionally, you can push the model to the Hugging Face Hub by setting the `push_to_hub` parameter to `True`. + +For example: + +.. code:: python + + import segmentation_models_pytorch as smp + + model = smp.Unet('resnet34', encoder_weights='imagenet') + + # After training your model, save it to a directory + model.save_pretrained('./my_model') + + # Or saved and pushed to the Hub simultaneously + model.save_pretrained('username/my-model', push_to_hub=True) + +Loading Trained Model +--------------------- + +Once your model is saved and pushed to the Hub, you can load it back using the `smp.from_pretrained` method. This method allows you to load the model weights and configuration from a directory or directly from the Hub. + +For example: + +.. code:: python + + import segmentation_models_pytorch as smp + + # Load the model from the local directory + model = smp.from_pretrained('./my_model') + + # Alternatively, load the model directly from the Hugging Face Hub + model = smp.from_pretrained('username/my-model') + +Saving model Metrics and Dataset Name +------------------------------------- + +You can simply pass the `metrics` and `dataset` parameters to the `save_pretrained` method to save the model metrics and dataset name in Model Card along with the model configuration and weights. + +For example: + +.. code:: python + + import segmentation_models_pytorch as smp + + model = smp.Unet('resnet34', encoder_weights='imagenet') + + # After training your model, save it to a directory + model.save_pretrained('./my_model', metrics={'accuracy': 0.95}, dataset='my_dataset') + + # Or saved and pushed to the Hub simultaneously + model.save_pretrained('username/my-model', push_to_hub=True, metrics={'accuracy': 0.95}, dataset='my_dataset') + + +Conclusion +---------- + +By following these steps, you can easily save, share, and load your models, facilitating collaboration and reproducibility in your projects. Don't forget to replace the placeholders with your actual model paths and names. + +|colab-badge| + +.. |colab-badge| image:: https://colab.research.google.com/assets/colab-badge.svg + :target: https://colab.research.google.com/github/qubvel/segmentation_models.pytorch/blob/master/examples/binary_segmentation_intro.ipynb + :alt: Open In Colab + +