Skip to content

Commit

Permalink
Integration of comet_ml (#56)
Browse files Browse the repository at this point in the history
* add comet_ml callback

* fix typo

* add comet to readme

* add link to notebook

* train for more epochs

* add TrainingCallbacks in doc

* apply isort and black

* prepare release

* typing

* Update README.md

* Update README.md

* update gitignore

* add viz in notebooks
  • Loading branch information
clementchadebec committed Oct 19, 2022
1 parent b045748 commit a04ea06
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 35 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ examples/notebooks/mlruns/*
mlruns/*
examples/scripts/reproducibility/reproducibility/*
examples/scripts/reproducibility/sync.ipynb


examples/notebooks/my_offline_runs/*



Expand Down
60 changes: 52 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
This library implements some of the most common (Variational) Autoencoder models under a unified implementation. In particular, it
provides the possibility to perform benchmark experiments and comparisons by training
the models with the same autoencoding neural network architecture. The feature *make your own autoencoder*
allows you to train any of these models with your own data and own Encoder and Decoder neural networks. It integrates experiment monitoring tools such [wandb](https://wandb.ai/) and [mlflow](https://mlflow.org/) 🧪 and allows model sharing and loading from the [HuggingFace Hub](https://huggingface.co/models) 🤗 in a few lines of code.
allows you to train any of these models with your own data and own Encoder and Decoder neural networks. It integrates experiment monitoring tools such [wandb](https://wandb.ai/), [mlflow](https://mlflow.org/) or [comet-ml](https://www.comet.com/site/) 🧪 and allows model sharing and loading from the [HuggingFace Hub](https://huggingface.co/models) 🤗 in a few lines of code.


## Quick access:
- [Installation](#installation)
- [Implemented models](#available-models) / [Implemented samplers](#available-samplers)
- [Reproducibility statement](#reproducibility) / [Results flavor](#results)
- [Model training](#launching-a-model-training) / [Data generation](#launching-data-generation) / [Custom network architectures](#define-you-own-autoencoder-architecture)
- [Model sharing with 🤗 Hub](#sharing-your-models-with-the-huggingface-hub-) / [Experiment tracking with `wandb`](#monitoring-your-experiments-with-wandb-) / [Experiment tracking with `mlflow`](#monitoring-your-experiments-with-mlflow-)
- [Model sharing with 🤗 Hub](#sharing-your-models-with-the-huggingface-hub-) / [Experiment tracking with `wandb`](#monitoring-your-experiments-with-wandb-) / [Experiment tracking with `mlflow`](#monitoring-your-experiments-with-mlflow-) / [Experiment tracking with `comet_ml`](#monitoring-your-experiments-with-comet_ml-)
- [Tutorials](#getting-your-hands-on-the-code) / [Documentation](https://pythae.readthedocs.io/en/latest/)
- [Contributing 🚀](#contributing-) / [Issues 🛠️](#dealing-with-issues-%EF%B8%8F)
- [Citing this repository](#citation)
Expand Down Expand Up @@ -361,9 +361,9 @@ Equivalently, you can download or reload any Pythae's model directly from the Hu
>>> my_downloaded_vae = AutoModel.load_from_hf_hub(hf_hub_path="path_to_hf_repo")
```

## Monitoring your experiments with **Wandb** 🧪
Pythae also integrates the experiement tracking tool [wandb](https://wandb.ai/) allowing users to store their configs, monitor their trainings and compare runs through a graphic interface. To be able use this feature you will need:
- a valid wand account
## Monitoring your experiments with `wandb` 🧪
Pythae also integrates the experiment tracking tool [wandb](https://wandb.ai/) allowing users to store their configs, monitor their trainings and compare runs through a graphic interface. To be able use this feature you will need:
- a valid wandb account
- the package `wandb` installed in your virtual env. If not you can install it with
```
$ pip install wandb
Expand Down Expand Up @@ -405,8 +405,8 @@ Launching an experiment monitoring with `wandb` in pythae is pretty simple. The
```
See the detailed tutorial

## Monitoring your experiments with **mlflow** 🧪
Pythae also integrates the experiement tracking tool [mlflow](https://mlflow.org/) allowing users to store their configs, monitor their trainings and compare runs through a graphic interface. To be able use this feature you will need:
## Monitoring your experiments with `mlflow` 🧪
Pythae also integrates the experiment tracking tool [mlflow](https://mlflow.org/) allowing users to store their configs, monitor their trainings and compare runs through a graphic interface. To be able use this feature you will need:
- the package `mlfow` installed in your virtual env. If not you can install it with
```
$ pip install mlflow
Expand Down Expand Up @@ -446,6 +446,48 @@ $ mlflow ui
```
See the detailed tutorial

## Monitoring your experiments with `comet_ml` 🧪
Pythae also integrates the experiment tracking tool [comet_ml](https://www.comet.com/site/) allowing users to store their configs, monitor their trainings and compare runs through a graphic interface. To be able use this feature you will need:
- the package `comet_ml` installed in your virtual env. If not you can install it with
```
$ pip install comet_ml
```

### Creating a `CometCallback`
Launching an experiment monitoring with `comet_ml` in pythae is pretty simple. The only thing a user needs to do is create a `CometCallback` instance...

```python
>>> # Create you callback
>>> from pythae.trainers.training_callbacks import CometCallback
>>> callbacks = [] # the TrainingPipeline expects a list of callbacks
>>> comet_cb = CometCallback() # Build the callback
>>> # SetUp the callback
>>> comet_cb.setup(
... training_config=training_config, # training config
... model_config=model_config, # model config
... api_key="your_comet_api_key", # specify your comet api-key
... project_name="your_comet_project", # specify your wandb project
... #offline_run=True, # run in offline mode
... #offline_directory='my_offline_runs' # set the directory to store the offline runs
... )
>>> callbacks.append(comet_cb) # Add it to the callbacks list
```
...and then pass it to the `TrainingPipeline`.
```python
>>> pipeline = TrainingPipeline(
... training_config=config,
... model=model
... )
>>> pipeline(
... train_data=train_dataset,
... eval_data=eval_dataset,
... callbacks=callbacks # pass the callbacks to the TrainingPipeline and you are done!
... )
>>> # You can log to https://comet.com/your_comet_username/your_comet_project to monitor your training
```
See the detailed tutorial


## Getting your hands on the code

To help you to understand the way pythae works and how you can train your models with this library we also
Expand All @@ -461,6 +503,8 @@ provide tutorials:

- [mlflow_experiment_monitoring.ipynb](https://github.com/clementchadebec/benchmark_VAE/tree/main/examples/notebooks) shows you how to monitor you experiments using `mlflow` [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/clementchadebec/benchmark_VAE/blob/main/examples/notebooks/mlflow_experiment_monitoring.ipynb)

- [comet_experiment_monitoring.ipynb](https://github.com/clementchadebec/benchmark_VAE/tree/main/examples/notebooks) shows you how to monitor you experiments using `comet_ml` [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/clementchadebec/benchmark_VAE/blob/main/examples/notebooks/comet_experiment_monitoring.ipynb)

- [models_training](https://github.com/clementchadebec/benchmark_VAE/tree/main/examples/notebooks/models_training) folder provides notebooks showing how to train each implemented model and how to sample from it using `pythae.samplers`.

- [scripts](https://github.com/clementchadebec/benchmark_VAE/tree/main/examples/scripts) folder provides in particular an example of a training script to train the models on benchmark data sets (mnist, cifar10, celeba ...)
Expand Down Expand Up @@ -549,4 +593,4 @@ If you find this work useful or use it in your research, please consider citing
url = {https://arxiv.org/abs/2206.08309},
year = {2022}
}
```
```
2 changes: 0 additions & 2 deletions docs/source/models/nn/pythae_benchmarks_nn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ ResNets
~pythae.models.nn.benchmarks.cifar.Encoder_ResNet_VAE_CIFAR
~pythae.models.nn.benchmarks.cifar.Encoder_ResNet_SVAE_CIFAR
~pythae.models.nn.benchmarks.cifar.Decoder_ResNet_AE_CIFAR
~pythae.models.nn.benchmarks.cifar.Discriminator_ResNet_CIFAR
:nosignatures:

CELEBA-64
Expand Down Expand Up @@ -94,7 +93,6 @@ ConvNets
~pythae.models.nn.benchmarks.celeba.Encoder_ResNet_VAE_CELEBA
~pythae.models.nn.benchmarks.celeba.Encoder_ResNet_SVAE_CELEBA
~pythae.models.nn.benchmarks.celeba.Decoder_ResNet_AE_CELEBA
~pythae.models.nn.benchmarks.celeba.Discriminator_ResNet_CELEBA
:nosignatures:

.. note::
Expand Down
18 changes: 18 additions & 0 deletions docs/source/trainers/pythae.trainer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Trainers
pythae.trainers.coupled_trainer
pythae.trainers.adversarial_trainer
pythae.trainers.coupled_optimizer_adversarial_trainer
pythae.training_callbacks


.. automodule::
Expand All @@ -21,4 +22,21 @@ Trainers
~pythae.trainers.CoupledOptimizerTrainer
~pythae.trainers.AdversarialTrainer
~pythae.trainers.CoupledOptimizerAdversarialTrainer
:nosignatures:

----------------------------------
Training Callbacks
----------------------------------

.. automodule::
pythae.trainers.training_callbacks

.. autosummary::
~pythae.trainers.training_callbacks.TrainingCallback
~pythae.trainers.training_callbacks.CallbackHandler
~pythae.trainers.training_callbacks.MetricConsolePrinterCallback
~pythae.trainers.training_callbacks.ProgressBarCallback
~pythae.trainers.training_callbacks.WandbCallback
~pythae.trainers.training_callbacks.MLFlowCallback
~pythae.trainers.training_callbacks.CometCallback
:nosignatures:
27 changes: 27 additions & 0 deletions docs/source/trainers/pythae.training_callbacks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
**********************************
TrainingCallbacks
**********************************

.. automodule::
pythae.trainers.training_callbacks

.. autoclass:: pythae.trainers.training_callbacks.TrainingCallback
:members:

.. autoclass:: pythae.trainers.training_callbacks.CallbackHandler
:members:

.. autoclass:: pythae.trainers.training_callbacks.MetricConsolePrinterCallback
:members:

.. autoclass:: pythae.trainers.training_callbacks.ProgressBarCallback
:members:

.. autoclass:: pythae.trainers.training_callbacks.WandbCallback
:members:

.. autoclass:: pythae.trainers.training_callbacks.MLFlowCallback
:members:

.. autoclass:: pythae.trainers.training_callbacks.CometCallback
:members:
206 changes: 206 additions & 0 deletions examples/notebooks/comet_experiment_monitoring.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutorial - Comet ml experiments monitoring\n",
"\n",
"In this notebook, we will see how to monitor your experiments using the integrated **comet_ml** callbacks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install the library\n",
"%pip install pythae"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Train your Pythae model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torchvision.datasets as datasets\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mnist_trainset = datasets.MNIST(root='../data', train=True, download=True, transform=None)\n",
"\n",
"train_dataset = mnist_trainset.data[:-10000].reshape(-1, 1, 28, 28) / 255.\n",
"eval_dataset = mnist_trainset.data[-10000:].reshape(-1, 1, 28, 28) / 255."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pythae.models import BetaVAE, BetaVAEConfig\n",
"from pythae.trainers import BaseTrainerConfig\n",
"from pythae.pipelines.training import TrainingPipeline\n",
"from pythae.models.nn.benchmarks.mnist import Encoder_ResNet_VAE_MNIST, Decoder_ResNet_AE_MNIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"training_config = BaseTrainerConfig(\n",
" output_dir='my_model',\n",
" learning_rate=1e-4,\n",
" batch_size=100,\n",
" num_epochs=10, # Change this to train the model a bit more,\n",
" steps_predict=3\n",
")\n",
"\n",
"\n",
"model_config = BetaVAEConfig(\n",
" input_dim=(1, 28, 28),\n",
" latent_dim=16,\n",
" beta=2.\n",
"\n",
")\n",
"\n",
"model = BetaVAE(\n",
" model_config=model_config,\n",
" encoder=Encoder_ResNet_VAE_MNIST(model_config), \n",
" decoder=Decoder_ResNet_AE_MNIST(model_config) \n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Before lauching the pipeline, you will need to build your `CometCallback`\n",
"\n",
"To be able to access this feature you will need:\n",
"- a valid comet_ml acccount\n",
"- the `comet_ml` package installed in your virtual env. You can install it by running (`pip install comet_ml`)\n",
"- Your `api_key` when setting up the `CometCallback`. Note that you may need to run `comet init --api-key` to set up your api-key locally and be able to synchronize your offline runs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Before being allowed to monitor your experiments you may need to run the following\n",
"# !pip install comet_ml"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create you callback\n",
"from pythae.trainers.training_callbacks import CometCallback\n",
"\n",
"callbacks = [] # the TrainingPipeline expects a list of callbacks\n",
"\n",
"comet_cb = CometCallback() # Build the callback \n",
"\n",
"# SetUp the callback \n",
"comet_cb.setup(\n",
" training_config=training_config, # training config\n",
" model_config=model_config, # model config\n",
" api_key=\"your_comet_api_key\", # specify your comet api-key\n",
" project_name=\"your_comet_project\", # specify your wandb project\n",
" #offline_run=True, # run in offline mode\n",
" #offline_directory='my_offline_runs' # set the directory to store the offline runs\n",
")\n",
"\n",
"callbacks.append(comet_cb) # Add it to the callbacks list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pipeline = TrainingPipeline(\n",
" training_config=training_config,\n",
" model=model\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pipeline(\n",
" train_data=train_dataset,\n",
" eval_data=eval_dataset,\n",
" callbacks=callbacks # pass the callbacks to the TrainingPipeline and you are done!\n",
")\n",
"# You can log to https://comet.com/your_comet_username/your_comet_project to monitor your training"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Or you can alternatively ability to view the Comet UI in the jupyter notebook\n",
"import comet_ml\n",
"\n",
"experiment = comet_ml.get_global_experiment()\n",
"experiment.display()"
]
}
],
"metadata": {
"interpreter": {
"hash": "3efa06c4da850a09a4898b773c7e91b0da3286dbbffa369a8099a14a8fa43098"
},
"kernelspec": {
"display_name": "Python 3.8.11 64-bit ('pythae_dev': conda)",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit a04ea06

Please sign in to comment.