<h1>
<a href="https://github.com/KevinMusgrave/pytorch-adapt">
<img alt="PyTorch Adapt" src="https://github.com/KevinMusgrave/pytorch-adapt/blob/main/docs/imgs/Logo.png">
</a>
</h1>
<p align="center">
<a href="https://badge.fury.io/py/pytorch-adapt">
<img alt="PyPi version" src="https://badge.fury.io/py/pytorch-adapt.svg">
</a>
</p>
## Why use PyTorch Adapt?
PyTorch Adapt provides tools for **domain adaptation**, a type of machine learning algorithm that repurposes existing models to work in new domains. This library is:
### 1. **Fully featured**
Build a complete train/val domain adaptation pipeline in a few lines of code.
### 2. **Modular**
Use just the parts that suit your needs, whether it's the algorithms, loss functions, or validation methods.
### 3. **Highly customizable**
Customize and combine complex algorithms with ease.
### 4. **Compatible with frameworks**
Add additional functionality to your code by using one of the framework wrappers. Converting an algorithm into a PyTorch Lightning module is as simple as wrapping it with ```Lightning```.
## Documentation
- [**Documentation**](https://kevinmusgrave.github.io/pytorch-adapt/)
- [**Installation instructions**](https://github.com/KevinMusgrave/pytorch-adapt#installation)
- [**List of papers implemented**](https://kevinmusgrave.github.io/pytorch-adapt/algorithms/uda)
## Examples
See the **[examples folder](https://github.com/KevinMusgrave/pytorch-adapt/blob/main/examples/README.md)** for notebooks you can download or run on Google Colab.
## How to...
### Use in vanilla PyTorch
```python
from pytorch_adapt.hooks import DANNHook
from pytorch_adapt.utils.common_functions import batch_to_device
# Assuming that models, optimizers, and dataloader are already created.
hook = DANNHook(optimizers)
for data in tqdm(dataloader):
data = batch_to_device(data, device)
# Optimization is done inside the hook.
# The returned loss is for logging.
_, loss = hook({**models, **data})
```
### Build complex algorithms
Let's customize ```DANNHook``` with:
- minimum class confusion
- virtual adversarial training
```python
from pytorch_adapt.hooks import MCCHook, VATHook
# G and C are the Generator and Classifier models
G, C = models["G"], models["C"]
misc = {"combined_model": torch.nn.Sequential(G, C)}
hook = DANNHook(optimizers, post_g=[MCCHook(), VATHook()])
for data in tqdm(dataloader):
data = batch_to_device(data, device)
_, loss = hook({**models, **data, **misc})
```
### Wrap with your favorite PyTorch framework
First, set up the adapter and dataloaders:
```python
from pytorch_adapt.adapters import DANN
from pytorch_adapt.containers import Models
from pytorch_adapt.datasets import DataloaderCreator
models_cont = Models(models)
adapter = DANN(models=models_cont)
dc = DataloaderCreator(num_workers=2)
dataloaders = dc(**datasets)
```
Then use a framework wrapper:
#### PyTorch Lightning
```python
import pytorch_lightning as pl
from pytorch_adapt.frameworks.lightning import Lightning
L_adapter = Lightning(adapter)
trainer = pl.Trainer(gpus=1, max_epochs=1)
trainer.fit(L_adapter, dataloaders["train"])
```
#### PyTorch Ignite
```python
trainer = Ignite(adapter)
trainer.run(datasets, dataloader_creator=dc)
```
### Check your model's performance
You can do this in vanilla PyTorch:
```python
from pytorch_adapt.validators import SNDValidator
# Assuming predictions have been collected
target_train = {"preds": preds}
validator = SNDValidator()
score = validator(target_train=target_train)
```
You can also do this during training with a framework wrapper:
#### PyTorch Lightning
```python
from pytorch_adapt.frameworks.utils import filter_datasets
validator = SNDValidator()
dataloaders = dc(**filter_datasets(datasets, validator))
train_loader = dataloaders.pop("train")
L_adapter = Lightning(adapter, validator=validator)
trainer = pl.Trainer(gpus=1, max_epochs=1)
trainer.fit(L_adapter, train_loader, list(dataloaders.values()))
```
#### Pytorch Ignite
```python
from pytorch_adapt.validators import ScoreHistory
validator = ScoreHistory(SNDValidator())
trainer = Ignite(adapter, validator=validator)
trainer.run(datasets, dataloader_creator=dc)
```
### Run the above examples
See [this notebook](https://github.com/KevinMusgrave/pytorch-adapt/blob/main/examples/other/ReadmeExamples.ipynb) and [the examples page](https://github.com/KevinMusgrave/pytorch-adapt/tree/main/examples/) for other notebooks.
## Installation
### Pip
```
pip install pytorch-adapt
```
**To get the latest dev version**:
```
pip install pytorch-adapt --pre
```
**To use ```pytorch_adapt.frameworks.lightning```**:
```
pip install pytorch-adapt[lightning]
```
**To use ```pytorch_adapt.frameworks.ignite```**:
```
pip install pytorch-adapt[ignite]
```
### Conda
Coming soon...
### Dependencies
See [setup.py](https://github.com/KevinMusgrave/pytorch-adapt/blob/main/setup.py)
## Acknowledgements
### Contributors
Thanks to the contributors who made pull requests!
| Contributor | Highlights |
| -- | -- |
| [deepseek-eoghan](https://github.com/deepseek-eoghan) | Improved the TargetDataset class |
### Advisors
Thank you to [Ser-Nam Lim](https://research.fb.com/people/lim-ser-nam/), and my research advisor, [Professor Serge Belongie](https://vision.cornell.edu/se3/people/serge-belongie/).
### Logo
Thanks to [Jeff Musgrave](https://www.designgenius.ca/) for designing the logo.
### Citing this library
If you'd like to cite pytorch-adapt in your paper, you can refer to [this paper](https://arxiv.org/abs/2211.15673) by copy-pasting this bibtex reference:
```latex
@article{Musgrave2022PyTorchA,
title={PyTorch Adapt},
author={Kevin Musgrave and Serge J. Belongie and Ser Nam Lim},
journal={ArXiv},
year={2022},
volume={abs/2211.15673}
}
```
### Code references (in no particular order)
- https://github.com/wgchang/DSBN
- https://github.com/jihanyang/AFN
- https://github.com/thuml/Versatile-Domain-Adaptation
- https://github.com/tim-learn/ATDOC
- https://github.com/thuml/CDAN
- https://github.com/takerum/vat_chainer
- https://github.com/takerum/vat_tf
- https://github.com/RuiShu/dirt-t
- https://github.com/lyakaap/VAT-pytorch
- https://github.com/9310gaurav/virtual-adversarial-training
- https://github.com/thuml/Deep-Embedded-Validation
- https://github.com/lr94/abas
- https://github.com/thuml/Batch-Spectral-Penalization
- https://github.com/jvanvugt/pytorch-domain-adaptation
- https://github.com/ptrblck/pytorch_misc
Raw data
{
"_id": null,
"home_page": "https://github.com/KevinMusgrave/pytorch-adapt",
"name": "pytorch-adapt",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.0",
"maintainer_email": "",
"keywords": "",
"author": "Kevin Musgrave",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/db/e5/96520821bbb5f2f38d3f77458e9b47e155b37a35e1e577b34f6dd5a55a49/pytorch-adapt-0.0.83.tar.gz",
"platform": null,
"description": "<h1>\n<a href=\"https://github.com/KevinMusgrave/pytorch-adapt\">\n<img alt=\"PyTorch Adapt\" src=\"https://github.com/KevinMusgrave/pytorch-adapt/blob/main/docs/imgs/Logo.png\">\n</a>\n</h1>\n\n<p align=\"center\">\n <a href=\"https://badge.fury.io/py/pytorch-adapt\">\n <img alt=\"PyPi version\" src=\"https://badge.fury.io/py/pytorch-adapt.svg\">\n </a> \n</p>\n\n## Why use PyTorch Adapt?\nPyTorch Adapt provides tools for **domain adaptation**, a type of machine learning algorithm that repurposes existing models to work in new domains. This library is:\n\n### 1. **Fully featured**\nBuild a complete train/val domain adaptation pipeline in a few lines of code.\n### 2. **Modular**\nUse just the parts that suit your needs, whether it's the algorithms, loss functions, or validation methods.\n### 3. **Highly customizable**\nCustomize and combine complex algorithms with ease.\n### 4. **Compatible with frameworks**\nAdd additional functionality to your code by using one of the framework wrappers. Converting an algorithm into a PyTorch Lightning module is as simple as wrapping it with ```Lightning```.\n\n\n## Documentation\n- [**Documentation**](https://kevinmusgrave.github.io/pytorch-adapt/)\n- [**Installation instructions**](https://github.com/KevinMusgrave/pytorch-adapt#installation)\n- [**List of papers implemented**](https://kevinmusgrave.github.io/pytorch-adapt/algorithms/uda)\n\n## Examples\nSee the **[examples folder](https://github.com/KevinMusgrave/pytorch-adapt/blob/main/examples/README.md)** for notebooks you can download or run on Google Colab.\n\n## How to...\n\n### Use in vanilla PyTorch\n```python\nfrom pytorch_adapt.hooks import DANNHook\nfrom pytorch_adapt.utils.common_functions import batch_to_device\n\n# Assuming that models, optimizers, and dataloader are already created.\nhook = DANNHook(optimizers)\nfor data in tqdm(dataloader):\n data = batch_to_device(data, device)\n # Optimization is done inside the hook.\n # The returned loss is for logging.\n _, loss = hook({**models, **data})\n```\n\n### Build complex algorithms\nLet's customize ```DANNHook``` with:\n\n- minimum class confusion\n- virtual adversarial training\n\n```python\nfrom pytorch_adapt.hooks import MCCHook, VATHook\n\n# G and C are the Generator and Classifier models\nG, C = models[\"G\"], models[\"C\"]\nmisc = {\"combined_model\": torch.nn.Sequential(G, C)}\nhook = DANNHook(optimizers, post_g=[MCCHook(), VATHook()])\nfor data in tqdm(dataloader):\n data = batch_to_device(data, device)\n _, loss = hook({**models, **data, **misc})\n```\n\n### Wrap with your favorite PyTorch framework\nFirst, set up the adapter and dataloaders:\n\n```python\nfrom pytorch_adapt.adapters import DANN\nfrom pytorch_adapt.containers import Models\nfrom pytorch_adapt.datasets import DataloaderCreator\n\nmodels_cont = Models(models)\nadapter = DANN(models=models_cont)\ndc = DataloaderCreator(num_workers=2)\ndataloaders = dc(**datasets)\n```\n\nThen use a framework wrapper:\n\n#### PyTorch Lightning\n```python\nimport pytorch_lightning as pl\nfrom pytorch_adapt.frameworks.lightning import Lightning\n\nL_adapter = Lightning(adapter)\ntrainer = pl.Trainer(gpus=1, max_epochs=1)\ntrainer.fit(L_adapter, dataloaders[\"train\"])\n```\n\n#### PyTorch Ignite\n```python\ntrainer = Ignite(adapter)\ntrainer.run(datasets, dataloader_creator=dc)\n```\n\n### Check your model's performance\nYou can do this in vanilla PyTorch:\n```python\nfrom pytorch_adapt.validators import SNDValidator\n\n# Assuming predictions have been collected\ntarget_train = {\"preds\": preds}\nvalidator = SNDValidator()\nscore = validator(target_train=target_train)\n```\n\nYou can also do this during training with a framework wrapper:\n\n#### PyTorch Lightning\n```python\nfrom pytorch_adapt.frameworks.utils import filter_datasets\n\nvalidator = SNDValidator()\ndataloaders = dc(**filter_datasets(datasets, validator))\ntrain_loader = dataloaders.pop(\"train\")\n\nL_adapter = Lightning(adapter, validator=validator)\ntrainer = pl.Trainer(gpus=1, max_epochs=1)\ntrainer.fit(L_adapter, train_loader, list(dataloaders.values()))\n```\n\n#### Pytorch Ignite\n```python\nfrom pytorch_adapt.validators import ScoreHistory\n\nvalidator = ScoreHistory(SNDValidator())\ntrainer = Ignite(adapter, validator=validator)\ntrainer.run(datasets, dataloader_creator=dc)\n```\n\n### Run the above examples\nSee [this notebook](https://github.com/KevinMusgrave/pytorch-adapt/blob/main/examples/other/ReadmeExamples.ipynb) and [the examples page](https://github.com/KevinMusgrave/pytorch-adapt/tree/main/examples/) for other notebooks.\n\n## Installation\n\n### Pip\n```\npip install pytorch-adapt\n```\n\n**To get the latest dev version**:\n```\npip install pytorch-adapt --pre\n```\n\n**To use ```pytorch_adapt.frameworks.lightning```**:\n```\npip install pytorch-adapt[lightning]\n```\n\n**To use ```pytorch_adapt.frameworks.ignite```**:\n```\npip install pytorch-adapt[ignite]\n```\n\n\n### Conda\nComing soon...\n\n### Dependencies\nSee [setup.py](https://github.com/KevinMusgrave/pytorch-adapt/blob/main/setup.py)\n\n## Acknowledgements\n\n### Contributors\nThanks to the contributors who made pull requests!\n| Contributor | Highlights |\n| -- | -- |\n| [deepseek-eoghan](https://github.com/deepseek-eoghan) | Improved the TargetDataset class |\n\n### Advisors\nThank you to [Ser-Nam Lim](https://research.fb.com/people/lim-ser-nam/), and my research advisor, [Professor Serge Belongie](https://vision.cornell.edu/se3/people/serge-belongie/).\n\n### Logo\nThanks to [Jeff Musgrave](https://www.designgenius.ca/) for designing the logo.\n\n### Citing this library\nIf you'd like to cite pytorch-adapt in your paper, you can refer to [this paper](https://arxiv.org/abs/2211.15673) by copy-pasting this bibtex reference: \n```latex\n@article{Musgrave2022PyTorchA,\n title={PyTorch Adapt},\n author={Kevin Musgrave and Serge J. Belongie and Ser Nam Lim},\n journal={ArXiv},\n year={2022},\n volume={abs/2211.15673}\n}\n```\n\n### Code references (in no particular order)\n- https://github.com/wgchang/DSBN\n- https://github.com/jihanyang/AFN\n- https://github.com/thuml/Versatile-Domain-Adaptation\n- https://github.com/tim-learn/ATDOC\n- https://github.com/thuml/CDAN\n- https://github.com/takerum/vat_chainer\n- https://github.com/takerum/vat_tf\n- https://github.com/RuiShu/dirt-t\n- https://github.com/lyakaap/VAT-pytorch\n- https://github.com/9310gaurav/virtual-adversarial-training\n- https://github.com/thuml/Deep-Embedded-Validation\n- https://github.com/lr94/abas\n- https://github.com/thuml/Batch-Spectral-Penalization\n- https://github.com/jvanvugt/pytorch-domain-adaptation\n- https://github.com/ptrblck/pytorch_misc\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Domain adaptation made easy. Fully featured, modular, and customizable.",
"version": "0.0.83",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c871d37e9f35faa1575092e7ffba9556b0f01c71be4c6ffada31eed5d47e928d",
"md5": "bc0f25c0e0833a74aff488aa983afac7",
"sha256": "d27109f0488f3c76ca4d3a7e3367bf6a69dd0fb246246f2de013d7710509cd05"
},
"downloads": -1,
"filename": "pytorch_adapt-0.0.83-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bc0f25c0e0833a74aff488aa983afac7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.0",
"size": 158229,
"upload_time": "2023-01-30T00:35:53",
"upload_time_iso_8601": "2023-01-30T00:35:53.778619Z",
"url": "https://files.pythonhosted.org/packages/c8/71/d37e9f35faa1575092e7ffba9556b0f01c71be4c6ffada31eed5d47e928d/pytorch_adapt-0.0.83-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "dbe596520821bbb5f2f38d3f77458e9b47e155b37a35e1e577b34f6dd5a55a49",
"md5": "52168fe8ce709c40b145e07905658192",
"sha256": "6b9dbfa5cb1ac55c7223bb89ec19bbc18d3cc17bb74fad9af6d17d0af717f69e"
},
"downloads": -1,
"filename": "pytorch-adapt-0.0.83.tar.gz",
"has_sig": false,
"md5_digest": "52168fe8ce709c40b145e07905658192",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.0",
"size": 95516,
"upload_time": "2023-01-30T00:35:55",
"upload_time_iso_8601": "2023-01-30T00:35:55.214434Z",
"url": "https://files.pythonhosted.org/packages/db/e5/96520821bbb5f2f38d3f77458e9b47e155b37a35e1e577b34f6dd5a55a49/pytorch-adapt-0.0.83.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-30 00:35:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "KevinMusgrave",
"github_project": "pytorch-adapt",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "pytorch-adapt"
}