Package: pytorch-adapt
Why use PyTorch Adapt?PyTorch Adapt provides tools fordomain adaptation, a type of machine learning algorithm that repurposes existing models to work in new domains. This library is:1.Fully featuredBuild a complete train/val domain adaptation pipeline in a few lines of code.2.ModularUse just the parts that suit your needs, whether it's the algorithms, loss functions, or validation methods.3.Highly customizableCustomize and combine complex algorithms with ease.4.Compatible with frameworksAdd 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 withLightning.DocumentationDocumentationInstallation instructionsList of papers implementedExamplesSee theexamples folderfor notebooks you can download or run on Google Colab.How to...Use in vanilla PyTorchfrompytorch_adapt.hooksimportDANNHookfrompytorch_adapt.utils.common_functionsimportbatch_to_device# Assuming that models, optimizers, and dataloader are already created.hook=DANNHook(optimizers)fordataintqdm(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 algorithmsLet's customizeDANNHookwith:minimum class confusionvirtual adversarial trainingfrompytorch_adapt.hooksimportMCCHook,VATHook# G and C are the Generator and Classifier modelsG,C=models["G"],models["C"]misc={"combined_model":torch.nn.Sequential(G,C)}hook=DANNHook(optimizers,post_g=[MCCHook(),VATHook()])fordataintqdm(dataloader):data=batch_to_device(data,device)_,loss=hook({**models,**data,**misc})Wrap with your favorite PyTorch frameworkFirst, set up the adapter and dataloaders:frompytorch_adapt.adaptersimportDANNfrompytorch_adapt.containersimportModelsfrompytorch_adapt.datasetsimportDataloaderCreatormodels_cont=Models(models)adapter=DANN(models=models_cont)dc=DataloaderCreator(num_workers=2)dataloaders=dc(**datasets)Then use a framework wrapper:PyTorch Lightningimportpytorch_lightningasplfrompytorch_adapt.frameworks.lightningimportLightningL_adapter=Lightning(adapter)trainer=pl.Trainer(gpus=1,max_epochs=1)trainer.fit(L_adapter,dataloaders["train"])PyTorch Ignitetrainer=Ignite(adapter)trainer.run(datasets,dataloader_creator=dc)Check your model's performanceYou can do this in vanilla PyTorch:frompytorch_adapt.validatorsimportSNDValidator# Assuming predictions have been collectedtarget_train={"preds":preds}validator=SNDValidator()score=validator(target_train=target_train)You can also do this during training with a framework wrapper:PyTorch Lightningfrompytorch_adapt.frameworks.utilsimportfilter_datasetsvalidator=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 Ignitefrompytorch_adapt.validatorsimportScoreHistoryvalidator=ScoreHistory(SNDValidator())trainer=Ignite(adapter,validator=validator)trainer.run(datasets,dataloader_creator=dc)Run the above examplesSeethis notebookandthe examples pagefor other notebooks.InstallationPippip install pytorch-adaptTo get the latest dev version:pip install pytorch-adapt --preTo usepytorch_adapt.frameworks.lightning:pip install pytorch-adapt[lightning]To usepytorch_adapt.frameworks.ignite:pip install pytorch-adapt[ignite]CondaComing soon...DependenciesSeesetup.pyAcknowledgementsContributorsThanks to the contributors who made pull requests!ContributorHighlightsdeepseek-eoghanImproved the TargetDataset classAdvisorsThank you toSer-Nam Lim, and my research advisor,Professor Serge Belongie.LogoThanks toJeff Musgravefor designing the logo.Citing this libraryIf you'd like to cite pytorch-adapt in your paper, you can refer tothis paperby copy-pasting this bibtex reference:@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/DSBNhttps://github.com/jihanyang/AFNhttps://github.com/thuml/Versatile-Domain-Adaptationhttps://github.com/tim-learn/ATDOChttps://github.com/thuml/CDANhttps://github.com/takerum/vat_chainerhttps://github.com/takerum/vat_tfhttps://github.com/RuiShu/dirt-thttps://github.com/lyakaap/VAT-pytorchhttps://github.com/9310gaurav/virtual-adversarial-traininghttps://github.com/thuml/Deep-Embedded-Validationhttps://github.com/lr94/abashttps://github.com/thuml/Batch-Spectral-Penalizationhttps://github.com/jvanvugt/pytorch-domain-adaptationhttps://github.com/ptrblck/pytorch_misc