Python影像辨識筆記(三十四):Transfer-Learning-Library: Domain Adaptation and Finetune

知乎討論區

介紹

Transfer-Learning-Library是一個使用PyTorch開發的Library,實現了很多著名的Domain Adaptation演算法,根據README顯示,目前支援以下演算法,詳細的程式碼在examples/domain_adaptation底下。

Domain Adaptation for Classification

  • Domain-Adversarial Training of Neural Networks (DANN, ICML 2015)
  • Learning Transferable Features with Deep Adaptation Networks (DAN, ICML 2015)
  • Deep Transfer Learning with Joint Adaptation Networks (JAN, ICML 2017)
  • Conditional Adversarial Domain Adaptation (CDAN, NIPS 2018)
  • Maximum Classifier Discrepancy for Unsupervised Domain Adaptation (MCD, CVPR 2018)
  • Larger Norm More Transferable: An Adaptive Feature Norm Approach for Unsupervised Domain Adaptation (AFN, ICCV 2019)
  • Bridging Theory and Algorithm for Domain Adaptation (MDD, ICML 2019)
  • Minimum Class Confusion for Versatile Domain Adaptation (MCC, ECCV 2020)

Partial Domain Adaptation

  • Partial Adversarial Domain Adaptation (PADA, ECCV 2018)
  • Importance Weighted Adversarial Nets for Partial Domain Adaptation (IWAN, CVPR 2018)

Open-set Domain Adaptation

  • Open Set Domain Adaptation by Backpropagation (OSBP, ECCV 2018)

Domain Adaptation for Segmentation

  • Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks (CycleGAN, ICCV 2017)
  • CyCADA: Cycle-Consistent Adversarial Domain Adaptation (ICML 2018)
  • ADVENT: Adversarial Entropy Minimization for Domain Adaptation in Semantic Segmentation (CVPR 2019)
  • FDA: Fourier Domain Adaptation for Semantic Segmentation (CVPR 2020)

Domain Adaptation for Keypoint Detection

  • Regressive Domain Adaptation for Unsupervised Keypoint Detection (RegDA, CVPR 2021)

Finetune for Classification

  • DEep Learning Transfer using Feature Map with Attention for convolutional networks (DELTA, ICLR 2019)
  • Catastrophic Forgetting Meets Negative Transfer: Batch Spectral Shrinkage for Safe Transfer Learning (BSS, NIPS 2019)
  • Stochastic Normalization (StochNorm, NIPS 2020)
  • Co-Tuning for Transfer Learning (Co-Tuning, NIPS 2020).

環境安裝

git https://github.com/thuml/Transfer-Learning-Library
cd Transfer-Learning-Library
pip install torch==1.7.1+cu92 torchvision==0.8.2+cu92 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.htmlpip install -r requirements.txt

使用自己的資料訓練

  1. 準備dataset
# 新增以下名稱的資料夾,並在內部,建立2個txt檔案和1個資料夾。其中,建立的資料夾(image_folder)要包含欲訓練的圖片檔案(JPG, PNG..........)
Transfer-Learning-Library/data/datasetname/
# Transfer-Learning-Library/data/datasetname/image_folder/
# Transfer-Learning-Library/data/datasetname/datasetname_source.txt
# Transfer-Learning-Library/data/datasetname/datasetname_target.txt
datasetname_source.txt包含了所有要作為source domain的圖片資訊
datasetname_target.txt包含了所有要作為target domain的圖片資訊
# 圖片範例資訊格式如下:# 第一項路徑名稱;第二項是label名稱image_folder/123.jpg 0
image_folder/456.jpg 1
image_folder/789.jpg 2
# 如果圖片資料夾在其他顆硬碟或目錄,而且檔案很大該怎麼修改訓練的指令?要將datasetname_source.txt和datasetname_target.txt兩個檔案放到path/to/img_folder/當中path/to/img_folder/datasetname_source.txt
path/to/img_folder/datasetname_target.txt
CUDA_VISIBLE_DEVICES=0 python mcc.py path/to/img_folder -d Phison -s source -t target -a resnet18 --epochs 30 --seed 0 --temperature 2.5 --bottleneck-dim 2048 --log logs/mcc/Phison_c5_target

2. 新增datasetname.py

Transfer-Learning-Library/common/vision/datasets/datasetname.py

2. 修改__init__.py

Transfer-Learning-Library/common/vision/datasets/__init__.pyfrom .imagelist import ImageList
from .office31 import Office31
from .officehome import OfficeHome
from .visda2017 import VisDA2017
from .officecaltech import OfficeCaltech
from .domainnet import DomainNet
from .aircrafts import Aircraft
from .cub200 import CUB200
from .stanford_cars import StanfordCars
from .stanford_dogs import StanfordDogs
from .coco70 import COCO70
from .oxfordpet import OxfordIIITPet
from .datasetname import datasetname #新增
__all__ = ['ImageList', 'Office31', 'OfficeHome', "VisDA2017", "OfficeCaltech", "DomainNet",
"Aircraft", "cub200", "StanfordCars", "StanfordDogs", "COCO70", "OxfordIIITPet", 'datasetname'] #新增

3.使用MCC算法進行訓練

$ cd /Transfer-Learning-Library/examples/domain_adaptation/classification# -d中的datasetname要和datasetname.py中的class名稱一致
# /Transfer-Learning-Library/data/datasetname/是包含dataset的資料夾,裡面要包含datasetname_source.txt和datasetname_target.txt
$ CUDA_VISIBLE_DEVICES=0 python mcc.py /root/notebooks/Transfer-Learning-Library/data/datasetname/ -d datasetname -s source -t target -a resnet18 --epochs 30 --seed 0 --temperature 2.5 --bottleneck-dim 2048 --log logs/mcc/dataset_log_name## 指令說明
使用第0顆GPU,執行mcc.py,資料夾目錄/root/notebooks/Transfer-Learning-Library/data/datasetname/,使用datasetname.py中的datasetname method

Machine Learning | Deep Learning | https://linktr.ee/yanwei