{"id":13577350,"url":"https://github.com/KevinMusgrave/pytorch-adapt","last_synced_at":"2025-04-05T11:32:45.702Z","repository":{"id":39633692,"uuid":"394807071","full_name":"KevinMusgrave/pytorch-adapt","owner":"KevinMusgrave","description":"Domain adaptation made easy. Fully featured, modular, and customizable.","archived":false,"fork":false,"pushed_at":"2023-01-30T00:37:10.000Z","size":16157,"stargazers_count":358,"open_issues_count":19,"forks_count":15,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-11-05T06:50:27.757Z","etag":null,"topics":["computer-vision","deep-learning","domain-adaptation","machine-learning","pytorch","transfer-learning"],"latest_commit_sha":null,"homepage":"https://kevinmusgrave.github.io/pytorch-adapt/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KevinMusgrave.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-10T23:40:09.000Z","updated_at":"2024-11-03T11:24:44.000Z","dependencies_parsed_at":"2023-02-16T02:15:34.230Z","dependency_job_id":null,"html_url":"https://github.com/KevinMusgrave/pytorch-adapt","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KevinMusgrave%2Fpytorch-adapt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KevinMusgrave%2Fpytorch-adapt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KevinMusgrave%2Fpytorch-adapt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KevinMusgrave%2Fpytorch-adapt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KevinMusgrave","download_url":"https://codeload.github.com/KevinMusgrave/pytorch-adapt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223130175,"owners_count":17092241,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computer-vision","deep-learning","domain-adaptation","machine-learning","pytorch","transfer-learning"],"created_at":"2024-08-01T15:01:20.730Z","updated_at":"2024-11-05T14:31:02.413Z","avatar_url":"https://github.com/KevinMusgrave.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003ch1\u003e\n\u003ca href=\"https://github.com/KevinMusgrave/pytorch-adapt\"\u003e\n\u003cimg alt=\"PyTorch Adapt\" src=\"https://github.com/KevinMusgrave/pytorch-adapt/blob/main/docs/imgs/Logo.png\"\u003e\n\u003c/a\u003e\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n \u003ca href=\"https://badge.fury.io/py/pytorch-adapt\"\u003e\n     \u003cimg alt=\"PyPi version\" src=\"https://badge.fury.io/py/pytorch-adapt.svg\"\u003e\n \u003c/a\u003e \n\u003c/p\u003e\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKevinMusgrave%2Fpytorch-adapt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKevinMusgrave%2Fpytorch-adapt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKevinMusgrave%2Fpytorch-adapt/lists"}