百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pytorch-adapt

> 编程语言
开源

域适配变得简单。功能全面、模块化且可自定义。

397 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

域适配变得简单。功能全面、模块化且可自定义。

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
  • Installation instructions
  • List of papers implemented

Examples

See the examples folder 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 and the examples page for other notebooks.

Installation

Pip

bash
pip install pytorch-adapt

To get the latest dev version:

bash
pip install pytorch-adapt --pre

To use pytorch_adapt.frameworks.lightning:

bash
pip install pytorch-adapt[lightning]

To use pytorch_adapt.frameworks.ignite:

bash
pip install pytorch-adapt[ignite]

Conda

Coming soon...

Dependencies

See setup.py

Acknowledgements

Contributors

Thanks to the contributors who made pull requests!

Contributor Highlights
deepseek-eoghan Improved the TargetDataset class

Advisors

Thank you to Ser-Nam Lim, and my research advisor, Professor Serge Belongie.

Logo

Thanks to Jeff Musgrave for designing the logo.

Citing this library

If you'd like to cite pytorch-adapt in your paper, you can refer to this paper 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

Issues· 20 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Pythoncomputer-visiondeep-learningdomain-adaptationmachine-learning

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言