├── .gitattributes ├── .github ├── FUNDING.yml └── ISSUE_TEMPLATE │ └── bug_report.md ├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── code ├── ASR │ ├── Adapter │ │ ├── README.md │ │ ├── balanced_sampler.py │ │ ├── config │ │ │ ├── adapter_example.yaml │ │ │ ├── adapterfusion_example.yaml │ │ │ ├── finetune_meta_adapter_example.yaml │ │ │ └── meta_adapter_example.yaml │ │ ├── data_load.py │ │ ├── e2e_asr_adaptertransformer.py │ │ ├── train.py │ │ └── utils.py │ ├── CMatch │ │ ├── README.md │ │ ├── config │ │ │ ├── adv_example.yaml │ │ │ ├── ctc_align_example.yaml │ │ │ ├── frame_average_example.yaml │ │ │ ├── mmd_example.yaml │ │ │ ├── pseudo_ctc_pred_example.yaml │ │ │ └── train.yaml │ │ ├── ctc_aligner.py │ │ ├── data_load.py │ │ ├── distances.py │ │ ├── e2e_asr_udatransformer.py │ │ ├── train.py │ │ └── utils.py │ └── readme.md ├── BDA │ └── readme.md ├── DeepDA │ ├── BNM │ │ ├── BNM.sh │ │ ├── BNM.yaml │ │ └── README.md │ ├── DAAN │ │ ├── DAAN.sh │ │ └── DAAN.yaml │ ├── DAN │ │ ├── DAN.sh │ │ ├── DAN.yaml │ │ └── README.md │ ├── DANN │ │ ├── DANN.sh │ │ ├── DANN.yaml │ │ └── readme.md │ ├── DSAN │ │ ├── DSAN.sh │ │ ├── DSAN.yaml │ │ └── README.md │ ├── DeepCoral │ │ ├── DeepCoral.sh │ │ ├── DeepCoral.yaml │ │ └── README.md │ ├── README.md │ ├── backbones.py │ ├── data_loader.py │ ├── loss_funcs │ │ ├── __init__.py │ │ ├── adv.py │ │ ├── bnm.py │ │ ├── coral.py │ │ ├── daan.py │ │ ├── lmmd.py │ │ └── mmd.py │ ├── main.py │ ├── models.py │ ├── requirements.txt │ ├── transfer_losses.py │ └── utils.py ├── DeepDG │ ├── alg │ │ ├── alg.py │ │ ├── algs │ │ │ ├── ANDMask.py │ │ │ ├── CORAL.py │ │ │ ├── DANN.py │ │ │ ├── DIFEX.py │ │ │ ├── ERM.py │ │ │ ├── GroupDRO.py │ │ │ ├── MLDG.py │ │ │ ├── MMD.py │ │ │ ├── Mixup.py │ │ │ ├── RSC.py │ │ │ ├── VREx.py │ │ │ └── base.py │ │ ├── modelopera.py │ │ └── opt.py │ ├── datautil │ │ ├── getdataloader.py │ │ ├── imgdata │ │ │ ├── imgdataload.py │ │ │ └── util.py │ │ ├── mydataloader.py │ │ └── util.py │ ├── network │ │ ├── Adver_network.py │ │ ├── common_network.py │ │ ├── img_network.py │ │ └── util.py │ ├── readme.md │ ├── requirements.txt │ ├── scripts │ │ ├── paramsref.md │ │ └── run.sh │ ├── train.py │ └── utils │ │ └── util.py ├── Integrated sensing and communication (LSTM and VGG 16 model for digit and image classification, will be used for sensing and communication in 6G networks) ├── README.md ├── clip │ ├── README.md │ ├── __init__.py │ ├── clip_model.py │ ├── data │ │ ├── data_loader.py │ │ ├── download_data.py │ │ └── download_data_azcopy.py │ ├── log │ │ └── log.txt │ ├── main.py │ ├── requirements.txt │ ├── test_clip.py │ └── utils.py ├── deep │ ├── B-JMMD │ │ ├── README.md │ │ └── caffe │ │ │ ├── CMakeLists.txt │ │ │ ├── CONTRIBUTING.md │ │ │ ├── CONTRIBUTORS.md │ │ │ ├── INSTALL.md │ │ │ ├── LICENSE │ │ │ ├── Makefile │ │ │ ├── Makefile.config.example │ │ │ ├── README.md │ │ │ ├── caffe.cloc │ │ │ ├── data │ │ │ ├── imageCLEF │ │ │ │ ├── bList.txt │ │ │ │ ├── cList.txt │ │ │ │ ├── iList.txt │ │ │ │ └── pList.txt │ │ │ └── office │ │ │ │ ├── amazon_list.txt │ │ │ │ ├── bList.txt │ │ │ │ ├── cList.txt │ │ │ │ ├── dslr_list.txt │ │ │ │ ├── iList.txt │ │ │ │ ├── pList.txt │ │ │ │ └── webcam_list.txt │ │ │ ├── include │ │ │ └── caffe │ │ │ │ └── layers │ │ │ │ └── bjmmd_layer.hpp │ │ │ ├── kmake.sh │ │ │ ├── models │ │ │ ├── B-JMMD │ │ │ │ ├── alexnet │ │ │ │ │ ├── solver.prototxt │ │ │ │ │ └── train_val.prototxt │ │ │ │ └── resnet │ │ │ │ │ ├── solver.prototxt │ │ │ │ │ └── train_val.prototxt │ │ │ └── bvlc_reference_caffenet │ │ │ │ ├── deploy.prototxt │ │ │ │ ├── readme.md │ │ │ │ ├── solver.prototxt │ │ │ │ └── train_val.prototxt │ │ │ └── src │ │ │ └── caffe │ │ │ ├── layers │ │ │ ├── bjmmd_layer.cpp │ │ │ └── bjmmd_layer.cu │ │ │ └── proto │ │ │ └── caffe.proto │ ├── CSG │ │ ├── README.md │ │ ├── a-domainbed │ │ │ ├── main.py │ │ │ ├── prepare_data.sh │ │ │ ├── run_da.sh │ │ │ ├── run_ood.sh │ │ │ └── visual.py │ │ ├── a-imageclef │ │ │ ├── main.py │ │ │ ├── prepare_data.sh │ │ │ ├── run_da.sh │ │ │ ├── run_ood.sh │ │ │ └── visual.py │ │ ├── a-mnist │ │ │ ├── main.py │ │ │ ├── makedata.py │ │ │ ├── makedata.sh │ │ │ ├── run_da.sh │ │ │ └── run_ood.sh │ │ ├── arch │ │ │ ├── __init__.py │ │ │ ├── backbone.py │ │ │ ├── cnn.py │ │ │ ├── mlp.py │ │ │ └── mlpstru.json │ │ ├── csg-intro.png │ │ ├── distr │ │ │ ├── __init__.py │ │ │ ├── base.py │ │ │ ├── instances.py │ │ │ ├── tools.py │ │ │ └── utils.py │ │ ├── methods │ │ │ ├── __init__.py │ │ │ ├── cnbb.py │ │ │ ├── semvar.py │ │ │ ├── supvae.py │ │ │ └── xdistr.py │ │ ├── requirements.txt │ │ ├── test │ │ │ ├── distr_test.ipynb │ │ │ ├── distr_test.py │ │ │ └── utils_test.py │ │ └── utils │ │ │ ├── __init__.py │ │ │ ├── preprocess │ │ │ ├── __init__.py │ │ │ ├── data_list.py │ │ │ ├── data_loader.py │ │ │ └── data_provider.py │ │ │ ├── reprun.sh │ │ │ ├── utils.py │ │ │ └── utils_main.py │ ├── DAAN │ │ ├── README.md │ │ ├── assets │ │ │ ├── ablation.png │ │ │ ├── arch.png │ │ │ ├── imclef.png │ │ │ └── officehome.png │ │ ├── data_loader.py │ │ ├── functions.py │ │ ├── log │ │ │ └── tmp-2019-10-27-22-13-51.log │ │ ├── model │ │ │ ├── DAAN.py │ │ │ ├── __init__.py │ │ │ └── backbone.py │ │ ├── scripts │ │ │ └── train.sh │ │ └── train.py │ ├── DAN │ │ └── README.md │ ├── DANN(RevGrad) │ │ ├── adv_layer.py │ │ └── readme.md │ ├── DDC_DeepCoral │ │ └── README.md │ ├── DSAN │ │ └── README.md │ ├── DaNN │ │ ├── DaNN.py │ │ ├── data_loader.py │ │ ├── main.py │ │ ├── mmd.py │ │ └── readme.md │ ├── DeepCoral │ │ └── README.md │ ├── DeepMEDA │ │ ├── README.md │ │ ├── ResNet.py │ │ ├── Weight.py │ │ ├── data_loader.py │ │ ├── deep_meda.py │ │ ├── dynamic_factor.py │ │ ├── main.py │ │ └── mmd.py │ ├── Learning-to-Match │ │ └── README.md │ ├── MRAN │ │ ├── MRAN.py │ │ ├── Multi-representation adaptation network for cross-domain image Neural network zhuyongchun.pdf │ │ ├── README.md │ │ ├── ResNet.py │ │ ├── data_loader.py │ │ └── mmd.py │ ├── README.md │ ├── ReMoS │ │ ├── CV_adv │ │ │ ├── DNNtest │ │ │ │ ├── coverage │ │ │ │ │ ├── my_neuron_coverage.py │ │ │ │ │ ├── neuron_coverage.py │ │ │ │ │ ├── pytorch_wrapper.py │ │ │ │ │ ├── strong_neuron_activation_coverage.py │ │ │ │ │ ├── top_k_coverage.py │ │ │ │ │ └── utils │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── common.py │ │ │ │ │ │ ├── keras.py │ │ │ │ │ │ ├── mxnet.py │ │ │ │ │ │ ├── pytorch.py │ │ │ │ │ │ └── tensorflow.py │ │ │ │ ├── eval_nc.py │ │ │ │ └── strategy │ │ │ │ │ ├── __init__.py │ │ │ │ │ ├── adapt.py │ │ │ │ │ ├── deepxplore.py │ │ │ │ │ ├── dlfuzz.py │ │ │ │ │ ├── random.py │ │ │ │ │ └── strategy.py │ │ │ ├── dataset │ │ │ │ ├── cub200.py │ │ │ │ ├── flower102.py │ │ │ │ ├── mit67.py │ │ │ │ ├── stanford_40.py │ │ │ │ └── stanford_dog.py │ │ │ ├── distillation_training.py │ │ │ ├── eval_robustness.py │ │ │ ├── examples │ │ │ │ ├── finetune.sh │ │ │ │ ├── nc_guided_defect.py │ │ │ │ ├── nc_guided_defect.sh │ │ │ │ ├── nc_profile.sh │ │ │ │ ├── penul_guided_defect.py │ │ │ │ └── remos.sh │ │ │ ├── finetune.py │ │ │ ├── finetuner.py │ │ │ ├── model │ │ │ │ ├── __init__.py │ │ │ │ └── fe_resnet.py │ │ │ ├── nc_prune │ │ │ │ ├── analyze_coverage.py │ │ │ │ ├── coverage │ │ │ │ │ ├── my_neuron_coverage.py │ │ │ │ │ ├── pytorch_wrapper.py │ │ │ │ │ ├── strong_neuron_activation_coverage.py │ │ │ │ │ ├── test_max.py │ │ │ │ │ ├── top_k_coverage.py │ │ │ │ │ └── utils │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── common.py │ │ │ │ │ │ ├── keras.py │ │ │ │ │ │ ├── mxnet.py │ │ │ │ │ │ ├── pytorch.py │ │ │ │ │ │ └── tensorflow.py │ │ │ │ ├── my_profile.py │ │ │ │ ├── nc_pruner.py │ │ │ │ └── nc_weight_rank_pruner.py │ │ │ ├── utils.py │ │ │ └── weight_pruner.py │ │ ├── CV_backdoor │ │ │ ├── attack_finetuner.py │ │ │ ├── backdoor_dataset │ │ │ │ ├── cub200.py │ │ │ │ ├── mit67.py │ │ │ │ └── stanford_40.py │ │ │ ├── clean_dataset │ │ │ │ ├── cub200.py │ │ │ │ ├── mit67.py │ │ │ │ └── stanford_40.py │ │ │ ├── eval.py │ │ │ ├── eval_robustness.py │ │ │ ├── examples │ │ │ │ ├── backdoor.py │ │ │ │ ├── eval_backdoor.sh │ │ │ │ ├── r50_baseline.sh │ │ │ │ ├── r50_magprune.sh │ │ │ │ ├── r50_poison.sh │ │ │ │ └── remos │ │ │ │ │ ├── profile.sh │ │ │ │ │ ├── ratio_ncprune_weight_rank.sh │ │ │ │ │ └── remos.sh │ │ │ ├── finetune.py │ │ │ ├── finetuner.py │ │ │ ├── model │ │ │ │ ├── __init__.py │ │ │ │ └── fe_resnet.py │ │ │ ├── prune.py │ │ │ ├── remos │ │ │ │ ├── analyze_coverage.py │ │ │ │ ├── coverage │ │ │ │ │ ├── my_neuron_coverage.py │ │ │ │ │ ├── pytorch_wrapper.py │ │ │ │ │ ├── strong_neuron_activation_coverage.py │ │ │ │ │ ├── test_max.py │ │ │ │ │ ├── top_k_coverage.py │ │ │ │ │ └── utils │ │ │ │ │ │ ├── __init__.py │ │ │ │ │ │ ├── common.py │ │ │ │ │ │ ├── keras.py │ │ │ │ │ │ ├── mxnet.py │ │ │ │ │ │ ├── pytorch.py │ │ │ │ │ │ └── tensorflow.py │ │ │ │ ├── my_profile.py │ │ │ │ ├── nc_pruner.py │ │ │ │ └── remos_pruner.py │ │ │ ├── trigger.py │ │ │ ├── utils.py │ │ │ └── weight_pruner.py │ │ ├── README.md │ │ ├── instructions.md │ │ └── unpack_downloads.sh │ ├── TCP │ │ ├── README.md │ │ ├── dataset.py │ │ ├── finetune.py │ │ ├── mmd.py │ │ ├── prune.py │ │ └── tools.py │ ├── adarnn │ │ ├── README.md │ │ ├── base │ │ │ ├── AdaRNN.py │ │ │ ├── __init__.py │ │ │ ├── loss │ │ │ │ ├── __init__.py │ │ │ │ ├── adv_loss.py │ │ │ │ ├── coral.py │ │ │ │ ├── cos.py │ │ │ │ ├── kl_js.py │ │ │ │ ├── mmd.py │ │ │ │ ├── mutual_info.py │ │ │ │ └── pair_dist.py │ │ │ └── loss_transfer.py │ │ ├── dataset │ │ │ ├── __init__.py │ │ │ ├── data_act.py │ │ │ ├── data_process.py │ │ │ └── data_weather.py │ │ ├── requirements.txt │ │ ├── train_weather.py │ │ ├── transformer_adapt.py │ │ ├── tst │ │ │ ├── __init__.py │ │ │ ├── decoder.py │ │ │ ├── encoder.py │ │ │ ├── loss.py │ │ │ ├── multiHeadAttention.py │ │ │ ├── positionwiseFeedForward.py │ │ │ ├── transformer.py │ │ │ └── utils.py │ │ └── utils │ │ │ ├── __init__.py │ │ │ ├── heat_map.py │ │ │ ├── metrics.py │ │ │ ├── utils.py │ │ │ └── visualize.py │ ├── finetune_AlexNet_ResNet │ │ ├── data │ │ │ └── readme.txt │ │ ├── data_loader.py │ │ ├── finetune_office31.py │ │ └── readme.md │ └── fixed │ │ ├── alg │ │ ├── alg.py │ │ ├── algs │ │ │ ├── Fixed.py │ │ │ └── base.py │ │ ├── modelopera.py │ │ └── opt.py │ │ ├── datautil │ │ ├── actdata │ │ │ ├── cross_people.py │ │ │ └── util.py │ │ ├── getdataloader.py │ │ ├── mydataloader.py │ │ └── util.py │ │ ├── loss │ │ └── margin_loss.py │ │ ├── network │ │ ├── Adver_network.py │ │ ├── act_network.py │ │ └── common_network.py │ │ ├── readme.md │ │ ├── requirements.txt │ │ ├── resources │ │ └── figures │ │ │ └── frame.png │ │ ├── train.py │ │ └── utils │ │ └── util.py ├── distance │ ├── coral_pytorch.py │ ├── mmd_matlab.m │ ├── mmd_numpy_sklearn.py │ ├── mmd_pytorch.py │ └── proxy_a_distance.py ├── feature_extractor │ ├── for_digit_data │ │ ├── digit_data_loader.py │ │ ├── digit_deep_feature.py │ │ └── digit_network.py │ ├── for_image_data │ │ ├── backbone.py │ │ ├── data_load.py │ │ ├── main.py │ │ └── models.py │ └── readme.md ├── traditional │ ├── BDA │ │ ├── BDA.py │ │ ├── matlab │ │ │ ├── BDA.m │ │ │ └── demo_BDA.m │ │ └── readme.md │ ├── CORAL │ │ ├── CORAL.m │ │ ├── CORAL.py │ │ ├── CORAL_SVM.m │ │ └── readme.md │ ├── EasyTL │ │ ├── CORAL_map.m │ │ ├── EasyTL.m │ │ ├── GFK_map.m │ │ ├── demo_amazon_review.m │ │ ├── demo_image.m │ │ ├── demo_office_caltech.m │ │ ├── label_prop.m │ │ └── readme.md │ ├── GFK │ │ ├── GFK.m │ │ ├── GFK.py │ │ ├── getGFKDim.m │ │ └── readme.md │ ├── JDA │ │ ├── JDA.m │ │ ├── JDA.py │ │ └── readme.md │ ├── KMM.py │ ├── MEDA │ │ ├── MEDA.py │ │ ├── matlab │ │ │ ├── GFK_Map.m │ │ │ ├── MEDA.m │ │ │ ├── README.md │ │ │ ├── demo_office_caltech_surf.m │ │ │ ├── estimate_mu.m │ │ │ └── lapgraph.m │ │ ├── readme.md │ │ └── results │ │ │ ├── meda-imageclef.png │ │ │ ├── meda-office31.png │ │ │ ├── meda-officehome.png │ │ │ ├── result1.png │ │ │ └── result2.png │ ├── MyTJM.m │ ├── SA_SVM.m │ ├── SCL.py │ ├── SFA.py │ ├── SVM.m │ ├── TCA │ │ ├── TCA.m │ │ ├── TCA.py │ │ └── readme.md │ ├── TrAdaBoost.py │ ├── pyEasyTL │ │ ├── .gitignore │ │ ├── EasyTL.py │ │ ├── demo_amazon_review.py │ │ ├── demo_image.py │ │ ├── demo_office_caltech.py │ │ ├── intra_alignment.py │ │ ├── label_prop.py │ │ ├── label_prop_v2.py │ │ ├── license │ │ ├── readme.md │ │ ├── requirement.txt │ │ └── results.txt │ ├── readme.md │ └── sot │ │ ├── SOT.py │ │ ├── main.py │ │ └── readme.md └── utils │ ├── feature_vis.py │ └── grl.py ├── data ├── benchmark.md ├── dataset.md └── readme.md ├── doc ├── awesome_paper.md ├── awesome_paper_date.md ├── domain_adaptation.md ├── scholar_TL.md ├── transfer_learning_application.md ├── venues.md └── 迁移学习简介.md ├── docs ├── CNAME ├── _config.yml └── index.md ├── notebooks ├── deep_transfer_tutorial.ipynb └── traditional_transfer_learning.ipynb └── png ├── domain _adaptation.png ├── logo.jpg ├── matlab.png ├── result_office_caltech_decaf.jpg ├── tf.png └── tf2.png /.gitattributes: -------------------------------------------------------------------------------- 1 | *.pdf filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: jindongwang 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Before you open an issue** 11 | If it's code running error, maybe you want to check the python or pytorch version before submitting an issue. 12 | 13 | **Describe the bug** 14 | A clear and concise description of what the bug is. 15 | 16 | **To Reproduce** 17 | Where is this bug happen? 18 | 19 | **Screenshots** 20 | If applicable, add screenshots to help explain your problem. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | *~ 3 | */*~ 4 | code/.DS_Store 5 | .DS_Store 6 | *.pyc 7 | *.pkl 8 | outputs/ 9 | __pycache__/ 10 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Anyone interested in transfer learning is welcomed to contribute to this repo (by pull request): 2 | 3 | - You can add the latest publications / tools / tutorials directly to `readme.md` and `awesome_paper.md`. 4 | - You can add **code** to the code directory. You are welcomed to place the code of your published paper in this repo! 5 | - You are welcomed to update anything helpful. 6 | 7 | 如果你对本项目感兴趣,非常欢迎你加入! 8 | 9 | - 可以推荐最新的相关论文/工具/讲座,将信息通过pull request的方式更新到`readme.md`和`awesome_paper.md`中。 10 | - 推荐代码到**code**文件夹中。欢迎将你论文中的代码开源到本项目中! 11 | - 任何有用的建议都可进行贡献。 12 | 13 | 欢迎! -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jindong Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /code/ASR/Adapter/config/adapter_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 20 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 21 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 22 | 23 | # optimization related 24 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 25 | # opt: noam 26 | accum-grad: 1 27 | grad-clip: 5 28 | patience: 10 29 | epochs: 100 30 | dropout-rate: 0.1 31 | 32 | # transformer specific setting 33 | backend: pytorch 34 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 35 | transformer-input-layer: conv2d # encoder architecture type 36 | transformer-lr: 6.0 37 | transformer-warmup-steps: 1800 38 | transformer-attn-dropout-rate: 0.0 39 | transformer-length-normalized-loss: false 40 | transformer-init: pytorch 41 | 42 | 43 | # Decoding related 44 | beam-size: 10 45 | penalty: 0.0 46 | maxlenratio: 0.0 47 | minlenratio: 0.0 48 | ctc-weight: 0.3 49 | lm-weight: 0.7 50 | 51 | use_adapters: true 52 | load_pretrained_model: "<path-to-your-pretrained-model>:<modules-to-load>:<excluded-modules>" # Example: lid_42.pt::decoder.embed,decoder.output_layer,ctc. 53 | dataset: "ru_cy_it_eu_pt_ar" # <languages for which adapters are initialized>, we recommend to write all the dataset that you want to perform AdapterFusion later (if you want) 54 | adapter_train_languages: "ar" # The language(s) that is really used for this time 55 | adapter_fusion: false 56 | train_adapter_with_head: true 57 | 58 | opt: adam 59 | adam_lr: 0.001 60 | -------------------------------------------------------------------------------- /code/ASR/Adapter/config/adapterfusion_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 20 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 21 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 22 | 23 | # optimization related 24 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 25 | accum-grad: 1 26 | grad-clip: 5 27 | patience: 10 28 | epochs: 200 29 | dropout-rate: 0.1 30 | 31 | # transformer specific setting 32 | backend: pytorch 33 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 34 | transformer-input-layer: conv2d # encoder architecture type 35 | transformer-lr: 0.1 36 | transformer-warmup-steps: 1800 37 | transformer-attn-dropout-rate: 0.0 38 | transformer-length-normalized-loss: false 39 | transformer-init: pytorch 40 | 41 | 42 | # Decoding related 43 | beam-size: 10 44 | penalty: 0.0 45 | maxlenratio: 0.0 46 | minlenratio: 0.0 47 | ctc-weight: 0.3 48 | lm-weight: 0.7 49 | 50 | use_adapters: true 51 | load_pretrained_model: "<path-to-model-with-adapters>:<modules-to-load>:<excluded-modules>" # Example: lid_42_adapters.pt:: 52 | dataset: "ru_cy_it_eu_pt_ar" 53 | fusion_languages: "ru_cy_it_eu_pt_ar" # adapters to fuse 54 | adapter_train_languages: "ar" 55 | sim_adapter: true 56 | train_adapter_with_head: false 57 | 58 | guide_loss_weight: 1.0 59 | guide_loss_weight_decay_steps: 0 60 | opt: adam 61 | adam_lr: 0.001 -------------------------------------------------------------------------------- /code/ASR/Adapter/config/finetune_meta_adapter_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 20 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 21 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 22 | 23 | # optimization related 24 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 25 | opt: noam 26 | accum-grad: 1 27 | grad-clip: 5 28 | patience: 10 29 | epochs: 100 30 | dropout-rate: 0.1 31 | 32 | # transformer specific setting 33 | backend: pytorch 34 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 35 | transformer-input-layer: conv2d # encoder architecture type 36 | transformer-lr: 6.0 37 | transformer-warmup-steps: 1800 38 | transformer-attn-dropout-rate: 0.0 39 | transformer-length-normalized-loss: false 40 | transformer-init: pytorch 41 | 42 | 43 | # Decoding related 44 | beam-size: 10 45 | penalty: 0.0 46 | maxlenratio: 0.0 47 | minlenratio: 0.0 48 | ctc-weight: 0.3 49 | lm-weight: 0.7 50 | 51 | use_adapters: true 52 | load_pretrained_model: "<path-to-model-with-adapters>:<modules-to-load>:<excluded-modules>" # Example: lid_42_meta_adapter.pt:: 53 | adapter_train_languages: "ar" 54 | shared_adapter: "ru_cy_it_eu_pt" # Set to the source languages on which it is meta-trained 55 | dataset: ru_cy_it_eu_pt_ar 56 | adapter_fusion: false 57 | train_adapter_with_head: true 58 | # cs 59 | opt: adam 60 | adam_lr: 0.01 -------------------------------------------------------------------------------- /code/ASR/Adapter/config/meta_adapter_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | 20 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 21 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 22 | 23 | # optimization related 24 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 25 | accum-grad: 1 26 | grad-clip: 5 27 | patience: 0 28 | epochs: 30 29 | dropout-rate: 0.1 30 | 31 | # transformer specific setting 32 | backend: pytorch 33 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 34 | transformer-input-layer: conv2d # encoder architecture type 35 | opt: adam 36 | transformer-lr: 6.0 37 | transformer-warmup-steps: 250 38 | transformer-attn-dropout-rate: 0.0 39 | transformer-length-normalized-loss: false 40 | transformer-init: pytorch 41 | 42 | 43 | # Decoding related 44 | beam-size: 10 45 | penalty: 0.0 46 | maxlenratio: 0.0 47 | minlenratio: 0.0 48 | ctc-weight: 0.3 49 | lm-weight: 0.7 50 | 51 | load_pretrained_model: "<path-to-your-pretrained-model>:<modules-to-load>:<excluded-modules>" # Example: lid_42.pt::decoder.embed,decoder.output_layer,ctc. 52 | use_adapters: true 53 | adapter_train_languages: "ru_cy_it_eu_pt" 54 | dataset: "ru_cy_it_eu_pt" 55 | meta_train: true 56 | shared_adapter: "ru_cy_it_eu_pt" # The name of shared_adapter, other name is also okay 57 | adapter_fusion: false 58 | batch-size: 32 # Real batch size is given by this batch-size * number of languages 59 | train_adapter_with_head: true 60 | 61 | meta_lr: 0.0001 # 1.0 62 | adam_lr: 0.01 # inner learning rate 63 | # cs -------------------------------------------------------------------------------- /code/ASR/CMatch/config/adv_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 # There are two datasets, reduce batch size by half 20 | transformer-lr: 6.0 21 | #batch-size: 64 22 | #transformer-lr: 10.0 23 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 24 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 25 | 26 | # optimization related 27 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 28 | opt: noam 29 | accum-grad: 1 30 | grad-clip: 5 31 | patience: 5 32 | epochs: 100 33 | dropout-rate: 0.1 34 | 35 | # transformer specific setting 36 | backend: pytorch 37 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 38 | transformer-input-layer: conv2d # encoder architecture type 39 | #transformer-lr: 6.0 40 | transformer-warmup-steps: 25000 41 | transformer-attn-dropout-rate: 0.0 42 | transformer-length-normalized-loss: false 43 | transformer-init: pytorch 44 | 45 | 46 | # Decoding related 47 | beam-size: 10 48 | penalty: 0.0 49 | maxlenratio: 0.0 50 | minlenratio: 0.0 51 | ctc-weight: 0.3 52 | lm-weight: 0.7 53 | 54 | # Transfer related 55 | load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best:: 56 | tgt_dataset: <target_dataset_to_adapt_to> 57 | 58 | transfer_type: adv 59 | transfer_loss_weight: 0.3 -------------------------------------------------------------------------------- /code/ASR/CMatch/config/ctc_align_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 # There are two datasets, reduce batch size by half 20 | transformer-lr: 6.0 21 | #batch-size: 64 22 | #transformer-lr: 10.0 23 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 24 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 25 | 26 | # optimization related 27 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 28 | opt: noam 29 | accum-grad: 1 30 | grad-clip: 5 31 | patience: 5 32 | epochs: 100 33 | dropout-rate: 0.1 34 | 35 | # transformer specific setting 36 | backend: pytorch 37 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 38 | transformer-input-layer: conv2d # encoder architecture type 39 | #transformer-lr: 6.0 40 | transformer-warmup-steps: 25000 41 | transformer-attn-dropout-rate: 0.0 42 | transformer-length-normalized-loss: false 43 | transformer-init: pytorch 44 | 45 | 46 | # Decoding related 47 | beam-size: 10 48 | penalty: 0.0 49 | maxlenratio: 0.0 50 | minlenratio: 0.0 51 | ctc-weight: 0.3 52 | lm-weight: 0.7 53 | 54 | # Transfer related 55 | load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best:: 56 | tgt_dataset: <target_dataset_to_adapt_to> 57 | non_char_symbols: <non_character_symbols_to_filter_in_cmatch> # Example: 0_1_2_29_30 58 | transfer_type: cmatch 59 | transfer_loss_weight: 10.0 60 | cmatch_method: ctc_align 61 | tranfer_loss_weight_warmup_steps: 0 62 | self_training: True 63 | pseudo_label_json: <pseudo_label_json_of_target_dataset> -------------------------------------------------------------------------------- /code/ASR/CMatch/config/frame_average_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 # There are two datasets, reduce batch size by half 20 | transformer-lr: 6.0 21 | #batch-size: 64 22 | #transformer-lr: 10.0 23 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 24 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 25 | 26 | # optimization related 27 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 28 | opt: noam 29 | accum-grad: 1 30 | grad-clip: 5 31 | patience: 5 32 | epochs: 100 33 | dropout-rate: 0.1 34 | 35 | # transformer specific setting 36 | backend: pytorch 37 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 38 | transformer-input-layer: conv2d # encoder architecture type 39 | #transformer-lr: 6.0 40 | transformer-warmup-steps: 25000 41 | transformer-attn-dropout-rate: 0.0 42 | transformer-length-normalized-loss: false 43 | transformer-init: pytorch 44 | 45 | 46 | # Decoding related 47 | beam-size: 10 48 | penalty: 0.0 49 | maxlenratio: 0.0 50 | minlenratio: 0.0 51 | ctc-weight: 0.3 52 | lm-weight: 0.7 53 | 54 | # Transfer related 55 | load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best:: 56 | tgt_dataset: <target_dataset_to_adapt_to> 57 | non_char_symbols: <non_character_symbols_to_filter_in_cmatch> # Example: 0_1_2_29_30 58 | transfer_type: cmatch 59 | transfer_loss_weight: 10.0 60 | cmatch_method: frame_average 61 | self_training: True 62 | pseudo_label_json: <pseudo_label_json_of_target_dataset> -------------------------------------------------------------------------------- /code/ASR/CMatch/config/mmd_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 # There are two datasets, reduce batch size by half 20 | transformer-lr: 6.0 21 | #batch-size: 64 22 | #transformer-lr: 10.0 23 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 24 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 25 | 26 | # optimization related 27 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 28 | opt: noam 29 | accum-grad: 1 30 | grad-clip: 5 31 | patience: 5 32 | epochs: 100 33 | dropout-rate: 0.1 34 | 35 | # transformer specific setting 36 | backend: pytorch 37 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 38 | transformer-input-layer: conv2d # encoder architecture type 39 | #transformer-lr: 6.0 40 | transformer-warmup-steps: 25000 41 | transformer-attn-dropout-rate: 0.0 42 | transformer-length-normalized-loss: false 43 | transformer-init: pytorch 44 | 45 | 46 | # Decoding related 47 | beam-size: 10 48 | penalty: 0.0 49 | maxlenratio: 0.0 50 | minlenratio: 0.0 51 | ctc-weight: 0.3 52 | lm-weight: 0.7 53 | 54 | # Transfer related 55 | load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best:: 56 | tgt_dataset: <target_dataset_to_adapt_to> 57 | 58 | transfer_type: mmd 59 | transfer_loss_weight: 10.0 -------------------------------------------------------------------------------- /code/ASR/CMatch/config/pseudo_ctc_pred_example.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 # There are two datasets, reduce batch size by half 20 | transformer-lr: 6.0 21 | #batch-size: 64 22 | #transformer-lr: 10.0 23 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 24 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 25 | 26 | # optimization related 27 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 28 | opt: noam 29 | accum-grad: 1 30 | grad-clip: 5 31 | patience: 5 32 | epochs: 100 33 | dropout-rate: 0.1 34 | 35 | # transformer specific setting 36 | backend: pytorch 37 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 38 | transformer-input-layer: conv2d # encoder architecture type 39 | #transformer-lr: 6.0 40 | transformer-warmup-steps: 25000 41 | transformer-attn-dropout-rate: 0.0 42 | transformer-length-normalized-loss: false 43 | transformer-init: pytorch 44 | 45 | 46 | # Decoding related 47 | beam-size: 10 48 | penalty: 0.0 49 | maxlenratio: 0.0 50 | minlenratio: 0.0 51 | ctc-weight: 0.3 52 | lm-weight: 0.7 53 | 54 | # Transfer related 55 | load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best:: 56 | tgt_dataset: <target_dataset_to_adapt_to> 57 | 58 | transfer_type: cmatch 59 | transfer_loss_weight: 10.0 60 | cmatch_method: pseudo_ctc_pred 61 | tranfer_loss_weight_warmup_steps: 0 62 | self_training: True 63 | pseudo_label_json: <pseudo_label_json_of_target_dataset> 64 | multi_enc_repr_num: 1 # Number of encoder layers to use for adaptation, starting from the last layers 65 | non_char_symbols: <non_character_symbols_to_filter_in_cmatch> # Split by "_", Example: 0_1_2_29_30 -------------------------------------------------------------------------------- /code/ASR/CMatch/config/train.yaml: -------------------------------------------------------------------------------- 1 | # network architecture 2 | # encoder related 3 | elayers: 12 4 | eunits: 2048 5 | # decoder related 6 | dlayers: 6 7 | dunits: 2048 8 | # attention related 9 | adim: 256 10 | aheads: 4 11 | 12 | # hybrid CTC/attention 13 | mtlalpha: 0.3 14 | 15 | # label smoothing 16 | lsm-weight: 0.1 17 | 18 | # minibatch related 19 | batch-size: 32 20 | maxlen-in: 512 # if input length > maxlen-in, batchsize is automatically reduced 21 | maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced 22 | 23 | # optimization related 24 | sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs 25 | opt: noam 26 | accum-grad: 1 27 | grad-clip: 5 28 | patience: 5 29 | epochs: 100 30 | dropout-rate: 0.1 31 | 32 | # transformer specific setting 33 | backend: pytorch 34 | model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E" 35 | transformer-input-layer: conv2d # encoder architecture type 36 | transformer-lr: 6.0 37 | transformer-warmup-steps: 25000 38 | transformer-attn-dropout-rate: 0.0 39 | transformer-length-normalized-loss: false 40 | transformer-init: pytorch 41 | 42 | 43 | # Decoding related 44 | beam-size: 10 45 | penalty: 0.0 46 | maxlenratio: 0.0 47 | minlenratio: 0.0 48 | ctc-weight: 0.3 49 | lm-weight: 0.7 50 | -------------------------------------------------------------------------------- /code/ASR/readme.md: -------------------------------------------------------------------------------- 1 | # Transfer learning for automatic speech recognition (ASR) 2 | 3 | This directory contains code for several transfer learning-based ASR papers. 4 | 5 | ## Domain adaptation 6 | 7 | - **CMatch**: Cross-domain Speech Recognition with Unsupervised Character-level Distribution Matching 8 | - [Paper](https://arxiv.org/abs/2104.07491) 9 | - [Code](https://github.com/jindongwang/transferlearning/tree/master/code/ASR/CMatch) 10 | 11 | ## Finetune and Meta-learning 12 | 13 | - **Adapter**: Exploiting Adapters for Cross-lingual Low-resource Speech Recognition 14 | - [Paper](https://arxiv.org/abs/2105.11905) 15 | - [Code](https://github.com/jindongwang/transferlearning/tree/master/code/ASR/Adapter) -------------------------------------------------------------------------------- /code/BDA/readme.md: -------------------------------------------------------------------------------- 1 | # Balanced Distribution Adaptation 2 | 3 | Since the transfer learning repo was reorganized, please visit https://github.com/jindongwang/transferlearning/tree/master/code/traditional/BDA for the BDA code. -------------------------------------------------------------------------------- /code/DeepDA/BNM/BNM.yaml: -------------------------------------------------------------------------------- 1 | # Backbone 2 | backbone: resnet50 3 | 4 | # Transfer loss related 5 | transfer_loss_weight: 1 6 | transfer_loss: bnm 7 | 8 | # Optimizer related 9 | lr: 0.001 10 | weight_decay: 5e-4 11 | lr_scheduler: True 12 | lr_gamma: 0.0003 13 | lr_decay: 0.75 14 | momentum: 0.9 15 | 16 | # Training related 17 | n_iter_per_epoch: 300 18 | n_epoch: 20 19 | 20 | # Others 21 | seed: 1 22 | num_workers: 3 23 | -------------------------------------------------------------------------------- /code/DeepDA/BNM/README.md: -------------------------------------------------------------------------------- 1 | # BNM 2 | 3 | A PyTorch implementation of '[Towards Discriminability and Diversity: Batch Nuclear-norm Maximization under Label Insufficient Situations](http://arxiv.org/abs/2003.12237)', i.e., BNM method for domain adaptation. 4 | 5 | For more details, you can refer to the paper. 6 | 7 | To run the method, go to the `DeepDA` folder. Then, run `bash BNM/BNM.sh`. -------------------------------------------------------------------------------- /code/DeepDA/DAAN/DAAN.yaml: -------------------------------------------------------------------------------- 1 | # Backbone 2 | backbone: resnet50 3 | 4 | # Transfer loss related 5 | transfer_loss_weight: 1.0 6 | transfer_loss: daan 7 | 8 | # Optimizer related 9 | lr: 0.01 10 | weight_decay: 5e-4 11 | momentum: 0.9 12 | lr_scheduler: True 13 | lr_gamma: 0.0003 14 | lr_decay: 0.75 15 | 16 | # Training related 17 | n_iter_per_epoch: 500 18 | n_epoch: 20 19 | 20 | # Others 21 | seed: 1 22 | num_workers: 3 23 | -------------------------------------------------------------------------------- /code/DeepDA/DAN/DAN.yaml: -------------------------------------------------------------------------------- 1 | # Backbone 2 | backbone: resnet50 3 | 4 | # Transfer loss related 5 | transfer_loss_weight: 0.5 6 | transfer_loss: mmd 7 | 8 | # Optimizer related 9 | lr: 3e-3 10 | weight_decay: 5e-4 11 | lr_scheduler: True 12 | lr_gamma: 0.0003 13 | lr_decay: 0.75 14 | momentum: 0.9 15 | 16 | # Training related 17 | n_iter_per_epoch: 500 18 | n_epoch: 20 19 | 20 | # Others 21 | seed: 1 22 | num_workers: 3 23 | -------------------------------------------------------------------------------- /code/DeepDA/DAN/README.md: -------------------------------------------------------------------------------- 1 | # DAN 2 | A PyTorch implementation of '[Learning Transferable Features with Deep Adaptation Networks](http://ise.thss.tsinghua.edu.cn/~mlong/doc/deep-adaptation-networks-icml15.pdf)'. 3 | The contributions of this paper are summarized as follows. 4 | * They propose a novel deep neural network architecture for domain adaptation, in which all the layers corresponding to task-specific features are adapted in a layerwise manner, hence benefiting from “deep adaptation.” 5 | * They explore multiple kernels for adapting deep representations, which substantially enhances adaptation effectiveness compared to single kernel methods. Our model can yield unbiased deep features with statistical guarantees. -------------------------------------------------------------------------------- /code/DeepDA/DANN/DANN.yaml: -------------------------------------------------------------------------------- 1 | # Backbone 2 | backbone: resnet50 3 | 4 | # Transfer loss related 5 | transfer_loss_weight: 1.0 6 | transfer_loss: adv 7 | 8 | # Optimizer related 9 | lr: 0.01 10 | weight_decay: 0.001 # 5e-4 11 | momentum: 0.9 12 | lr_scheduler: True 13 | lr_gamma: 0.001 14 | lr_decay: 0.75 15 | 16 | # Training related 17 | n_iter_per_epoch: 500 18 | n_epoch: 20 19 | 20 | # Others 21 | seed: 1 22 | num_workers: 3 23 | -------------------------------------------------------------------------------- /code/DeepDA/DANN/readme.md: -------------------------------------------------------------------------------- 1 | # Domain adversarial neural network (DANN/RevGrad) 2 | 3 | This is a Pytorch implementation of Unsupervised domain adaptation by backpropagation (also know as *DANN* or *RevGrad*). 4 | 5 | 6 | **Reference** 7 | 8 | Ganin Y, Lempitsky V. Unsupervised domain adaptation by backpropagation. ICML 2015. 9 | -------------------------------------------------------------------------------- /code/DeepDA/DSAN/DSAN.yaml: -------------------------------------------------------------------------------- 1 | # Backbone 2 | backbone: resnet50 3 | 4 | # Transfer loss related 5 | transfer_loss_weight: 0.5 6 | transfer_loss: lmmd 7 | 8 | # Optimizer related 9 | lr: 0.01 10 | weight_decay: 5e-4 11 | momentum: 0.9 12 | lr_scheduler: True 13 | lr_gamma: 0.0003 14 | lr_decay: 0.75 15 | 16 | # Training related 17 | n_iter_per_epoch: 500 18 | n_epoch: 20 19 | 20 | # Others 21 | seed: 1 22 | num_workers: 3 23 | -------------------------------------------------------------------------------- /code/DeepDA/DSAN/README.md: -------------------------------------------------------------------------------- 1 | # DSAN 2 | 3 | A PyTorch implementation of 'Deep Subdomain Adaptation Network for Image Classification' which has published on IEEE Transactions on Neural Networks and Learning Systems. 4 | The contributions of this paper are summarized as follows. 5 | * They propose a novel deep neural network architecture for Subdomain Adaptation, which can extend the ability of deep adaptation networks by capturing the fine-grained information for each category. 6 | * They show that DSAN which is a non-adversarial method can achieve the remarkable results. In addition, their DSAN is very simple and easy to implement. 7 | 8 | 9 | ## Reference 10 | 11 | ``` 12 | Zhu Y, Zhuang F, Wang J, et al. Deep Subdomain Adaptation Network for Image Classification[J]. IEEE Transactions on Neural Networks and Learning Systems, 2020. 13 | ``` 14 | 15 | or in bibtex style: 16 | 17 | ``` 18 | @article{zhu2020deep, 19 | title={Deep Subdomain Adaptation Network for Image Classification}, 20 | author={Zhu, Yongchun and Zhuang, Fuzhen and Wang, Jindong and Ke, Guolin and Chen, Jingwu and Bian, Jiang and Xiong, Hui and He, Qing}, 21 | journal={IEEE Transactions on Neural Networks and Learning Systems}, 22 | year={2020}, 23 | publisher={IEEE} 24 | } 25 | ``` -------------------------------------------------------------------------------- /code/DeepDA/DeepCoral/DeepCoral.yaml: -------------------------------------------------------------------------------- 1 | # Backbone 2 | backbone: resnet50 3 | 4 | # Transfer loss related 5 | transfer_loss_weight: 10.0 6 | transfer_loss: coral 7 | 8 | # Optimizer related 9 | lr: 3e-3 10 | weight_decay: 5e-4 11 | lr_scheduler: True 12 | lr_gamma: 0.0003 13 | lr_decay: 0.75 14 | momentum: 0.9 15 | 16 | # Training related 17 | n_iter_per_epoch: 500 18 | n_epoch: 20 19 | 20 | # Others 21 | seed: 1 22 | num_workers: 3 23 | -------------------------------------------------------------------------------- /code/DeepDA/DeepCoral/README.md: -------------------------------------------------------------------------------- 1 | # Deep Coral 2 | 3 | A PyTorch implementation of '[Deep CORAL Correlation Alignment for Deep Domain Adaptation](https://arxiv.org/pdf/1607.01719.pdf)'. 4 | The contributions of this paper are summarized as fol- 5 | lows. 6 | * They extend CORAL to incorporate it directly into deep networks by constructing a differentiable loss function that minimizes the difference between source and target correlations–the CORAL loss. 7 | * Compared to CORAL, Deep CORAL approach learns a non-linear transformation that is more powerful and also works seamlessly with deep CNNs. -------------------------------------------------------------------------------- /code/DeepDA/loss_funcs/__init__.py: -------------------------------------------------------------------------------- 1 | from loss_funcs.mmd import * 2 | from loss_funcs.coral import * 3 | from loss_funcs.adv import * 4 | from loss_funcs.lmmd import * 5 | from loss_funcs.daan import * 6 | from loss_funcs.bnm import * -------------------------------------------------------------------------------- /code/DeepDA/loss_funcs/bnm.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | def BNM(src, tar): 4 | """ Batch nuclear-norm maximization, CVPR 2020. 5 | tar: a tensor, softmax target output. 6 | NOTE: this does not require source domain data. 7 | """ 8 | _, out, _ = torch.svd(tar) 9 | loss = -torch.mean(out) 10 | return loss -------------------------------------------------------------------------------- /code/DeepDA/loss_funcs/coral.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | 4 | def CORAL(source, target, **kwargs): 5 | d = source.data.shape[1] 6 | ns, nt = source.data.shape[0], target.data.shape[0] 7 | # source covariance 8 | xm = torch.mean(source, 0, keepdim=True) - source 9 | xc = xm.t() @ xm / (ns - 1) 10 | 11 | # target covariance 12 | xmt = torch.mean(target, 0, keepdim=True) - target 13 | xct = xmt.t() @ xmt / (nt - 1) 14 | 15 | # frobenius norm between source and target 16 | loss = torch.mul((xc - xct), (xc - xct)) 17 | loss = torch.sum(loss) / (4*d*d) 18 | return loss 19 | -------------------------------------------------------------------------------- /code/DeepDA/requirements.txt: -------------------------------------------------------------------------------- 1 | ConfigArgParse==1.4.1 2 | torch==1.13.1 3 | torchvision==0.9.1 4 | scikit-learn 5 | matplotlib 6 | xlwt 7 | pyyaml 8 | -------------------------------------------------------------------------------- /code/DeepDA/transfer_losses.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from loss_funcs import * 4 | 5 | class TransferLoss(nn.Module): 6 | def __init__(self, loss_type, **kwargs): 7 | super(TransferLoss, self).__init__() 8 | self.loss_type = loss_type 9 | if loss_type == "mmd": 10 | self.loss_func = MMDLoss(**kwargs) 11 | elif loss_type == "lmmd": 12 | self.loss_func = LMMDLoss(**kwargs) 13 | elif loss_type == "coral": 14 | self.loss_func = CORAL 15 | elif loss_type == "adv": 16 | self.loss_func = AdversarialLoss(**kwargs) 17 | elif loss_type == "daan": 18 | self.loss_func = DAANLoss(**kwargs) 19 | elif loss_type == "bnm": 20 | self.loss_func = BNM 21 | else: 22 | print("WARNING: No valid transfer loss function is used.") 23 | self.loss_func = lambda x, y: 0 # return 0 24 | 25 | def forward(self, source, target, **kwargs): 26 | return self.loss_func(source, target, **kwargs) -------------------------------------------------------------------------------- /code/DeepDA/utils.py: -------------------------------------------------------------------------------- 1 | class AverageMeter(object): 2 | """Computes and stores the average and current value""" 3 | 4 | def __init__(self): 5 | self.reset() 6 | 7 | def reset(self): 8 | self.val = 0 9 | self.avg = 0 10 | self.sum = 0 11 | self.count = 0 12 | 13 | def update(self, val, n=1): 14 | self.val = val 15 | self.sum += val * n 16 | self.count += n 17 | self.avg = self.sum / self.count 18 | 19 | def str2bool(v): 20 | if isinstance(v, bool): 21 | return v 22 | if v.lower() in ('yes', 'true', 't', 'y', '1'): 23 | return True 24 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): 25 | return False 26 | else: 27 | raise ValueError('Boolean value expected.') -------------------------------------------------------------------------------- /code/DeepDG/alg/alg.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from alg.algs.ERM import ERM 3 | from alg.algs.MMD import MMD 4 | from alg.algs.CORAL import CORAL 5 | from alg.algs.DANN import DANN 6 | from alg.algs.RSC import RSC 7 | from alg.algs.Mixup import Mixup 8 | from alg.algs.MLDG import MLDG 9 | from alg.algs.GroupDRO import GroupDRO 10 | from alg.algs.ANDMask import ANDMask 11 | from alg.algs.VREx import VREx 12 | from alg.algs.DIFEX import DIFEX 13 | 14 | ALGORITHMS = [ 15 | 'ERM', 16 | 'Mixup', 17 | 'CORAL', 18 | 'MMD', 19 | 'DANN', 20 | 'MLDG', 21 | 'GroupDRO', 22 | 'RSC', 23 | 'ANDMask', 24 | 'VREx', 25 | 'DIFEX' 26 | ] 27 | 28 | 29 | def get_algorithm_class(algorithm_name): 30 | if algorithm_name not in globals(): 31 | raise NotImplementedError( 32 | "Algorithm not found: {}".format(algorithm_name)) 33 | return globals()[algorithm_name] 34 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/ANDMask.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | import torch.nn.functional as F 4 | from alg.algs.ERM import ERM 5 | import torch.autograd as autograd 6 | 7 | 8 | class ANDMask(ERM): 9 | def __init__(self, args): 10 | super(ANDMask, self).__init__(args) 11 | 12 | self.tau = args.tau 13 | 14 | def update(self, minibatches, opt, sch): 15 | 16 | total_loss = 0 17 | param_gradients = [[] for _ in self.network.parameters()] 18 | all_x = torch.cat([data[0].cuda().float() for data in minibatches]) 19 | all_logits = self.network(all_x) 20 | all_logits_idx = 0 21 | for i, data in enumerate(minibatches): 22 | x, y = data[0].cuda().float(), data[1].cuda().long() 23 | logits = all_logits[all_logits_idx:all_logits_idx + x.shape[0]] 24 | all_logits_idx += x.shape[0] 25 | 26 | env_loss = F.cross_entropy(logits, y) 27 | total_loss += env_loss 28 | 29 | env_grads = autograd.grad( 30 | env_loss, self.network.parameters(), retain_graph=True) 31 | for grads, env_grad in zip(param_gradients, env_grads): 32 | grads.append(env_grad) 33 | 34 | mean_loss = total_loss / len(minibatches) 35 | 36 | opt.zero_grad() 37 | self.mask_grads(self.tau, param_gradients, self.network.parameters()) 38 | opt.step() 39 | if sch: 40 | sch.step() 41 | 42 | return {'total': mean_loss.item()} 43 | 44 | def mask_grads(self, tau, gradients, params): 45 | 46 | for param, grads in zip(params, gradients): 47 | grads = torch.stack(grads, dim=0) 48 | grad_signs = torch.sign(grads) 49 | mask = torch.mean(grad_signs, dim=0).abs() >= self.tau 50 | mask = mask.to(torch.float32) 51 | avg_grad = torch.mean(grads, dim=0) 52 | 53 | mask_t = (mask.sum() / mask.numel()) 54 | param.grad = mask * avg_grad 55 | param.grad *= (1. / (1e-10 + mask_t)) 56 | 57 | return 0 58 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/CORAL.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | import torch.nn.functional as F 4 | from alg.algs.ERM import ERM 5 | 6 | 7 | class CORAL(ERM): 8 | def __init__(self, args): 9 | super(CORAL, self).__init__(args) 10 | self.args = args 11 | self.kernel_type = "mean_cov" 12 | 13 | def coral(self, x, y): 14 | mean_x = x.mean(0, keepdim=True) 15 | mean_y = y.mean(0, keepdim=True) 16 | cent_x = x - mean_x 17 | cent_y = y - mean_y 18 | cova_x = (cent_x.t() @ cent_x) / (len(x) - 1) 19 | cova_y = (cent_y.t() @ cent_y) / (len(y) - 1) 20 | 21 | mean_diff = (mean_x - mean_y).pow(2).mean() 22 | cova_diff = (cova_x - cova_y).pow(2).mean() 23 | 24 | return mean_diff + cova_diff 25 | 26 | def update(self, minibatches, opt, sch): 27 | objective = 0 28 | penalty = 0 29 | nmb = len(minibatches) 30 | 31 | features = [self.featurizer( 32 | data[0].cuda().float()) for data in minibatches] 33 | classifs = [self.classifier(fi) for fi in features] 34 | targets = [data[1].cuda().long() for data in minibatches] 35 | 36 | for i in range(nmb): 37 | objective += F.cross_entropy(classifs[i], targets[i]) 38 | for j in range(i + 1, nmb): 39 | penalty += self.coral(features[i], features[j]) 40 | 41 | objective /= nmb 42 | if nmb > 1: 43 | penalty /= (nmb * (nmb - 1) / 2) 44 | 45 | opt.zero_grad() 46 | (objective + (self.args.mmd_gamma*penalty)).backward() 47 | opt.step() 48 | if sch: 49 | sch.step() 50 | if torch.is_tensor(penalty): 51 | penalty = penalty.item() 52 | 53 | return {'class': objective.item(), 'coral': penalty, 'total': (objective.item() + (self.args.mmd_gamma*penalty))} 54 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/DANN.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | from alg.modelopera import get_fea 7 | from network import Adver_network, common_network 8 | from alg.algs.base import Algorithm 9 | 10 | 11 | class DANN(Algorithm): 12 | 13 | def __init__(self, args): 14 | 15 | super(DANN, self).__init__(args) 16 | 17 | self.featurizer = get_fea(args) 18 | self.classifier = common_network.feat_classifier( 19 | args.num_classes, self.featurizer.in_features, args.classifier) 20 | self.discriminator = Adver_network.Discriminator( 21 | self.featurizer.in_features, args.dis_hidden, args.domain_num - len(args.test_envs)) 22 | self.args = args 23 | 24 | def update(self, minibatches, opt, sch): 25 | all_x = torch.cat([data[0].cuda().float() for data in minibatches]) 26 | all_y = torch.cat([data[1].cuda().long() for data in minibatches]) 27 | all_z = self.featurizer(all_x) 28 | 29 | disc_input = all_z 30 | disc_input = Adver_network.ReverseLayerF.apply( 31 | disc_input, self.args.alpha) 32 | disc_out = self.discriminator(disc_input) 33 | disc_labels = torch.cat([ 34 | torch.full((data[0].shape[0], ), i, 35 | dtype=torch.int64, device='cuda') 36 | for i, data in enumerate(minibatches) 37 | ]) 38 | 39 | disc_loss = F.cross_entropy(disc_out, disc_labels) 40 | all_preds = self.classifier(all_z) 41 | classifier_loss = F.cross_entropy(all_preds, all_y) 42 | loss = classifier_loss+disc_loss 43 | opt.zero_grad() 44 | loss.backward() 45 | opt.step() 46 | if sch: 47 | sch.step() 48 | return {'total': loss.item(), 'class': classifier_loss.item(), 'dis': disc_loss.item()} 49 | 50 | def predict(self, x): 51 | return self.classifier(self.featurizer(x)) 52 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/ERM.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | from alg.modelopera import get_fea 7 | from network import common_network 8 | from alg.algs.base import Algorithm 9 | 10 | 11 | class ERM(Algorithm): 12 | """ 13 | Empirical Risk Minimization (ERM) 14 | """ 15 | 16 | def __init__(self, args): 17 | super(ERM, self).__init__(args) 18 | self.featurizer = get_fea(args) 19 | self.classifier = common_network.feat_classifier( 20 | args.num_classes, self.featurizer.in_features, args.classifier) 21 | 22 | self.network = nn.Sequential( 23 | self.featurizer, self.classifier) 24 | 25 | def update(self, minibatches, opt, sch): 26 | all_x = torch.cat([data[0].cuda().float() for data in minibatches]) 27 | all_y = torch.cat([data[1].cuda().long() for data in minibatches]) 28 | loss = F.cross_entropy(self.predict(all_x), all_y) 29 | 30 | opt.zero_grad() 31 | loss.backward() 32 | opt.step() 33 | if sch: 34 | sch.step() 35 | return {'class': loss.item()} 36 | 37 | def predict(self, x): 38 | return self.network(x) 39 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/GroupDRO.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | import torch 3 | import torch.nn.functional as F 4 | from alg.algs.ERM import ERM 5 | 6 | class GroupDRO(ERM): 7 | """ 8 | Robust ERM minimizes the error at the worst minibatch 9 | Algorithm 1 from [https://arxiv.org/pdf/1911.08731.pdf] 10 | """ 11 | def __init__(self,args): 12 | super(GroupDRO, self).__init__(args) 13 | self.register_buffer("q", torch.Tensor()) 14 | self.args=args 15 | 16 | def update(self, minibatches, opt,sch): 17 | 18 | if not len(self.q): 19 | self.q = torch.ones(len(minibatches)).cuda() 20 | 21 | losses = torch.zeros(len(minibatches)).cuda() 22 | 23 | for m in range(len(minibatches)): 24 | x, y = minibatches[m][0].cuda().float(),minibatches[m][1].cuda().long() 25 | losses[m] = F.cross_entropy(self.predict(x), y) 26 | self.q[m] *= (self.args.groupdro_eta * losses[m].data).exp() 27 | 28 | self.q /= self.q.sum() 29 | 30 | loss = torch.dot(losses, self.q) 31 | 32 | opt.zero_grad() 33 | loss.backward() 34 | opt.step() 35 | if sch: 36 | sch.step() 37 | 38 | return {'group': loss.item()} -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/Mixup.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import numpy as np 3 | import torch.nn.functional as F 4 | 5 | from datautil.util import random_pairs_of_minibatches 6 | from alg.algs.ERM import ERM 7 | 8 | 9 | class Mixup(ERM): 10 | def __init__(self, args): 11 | super(Mixup, self).__init__(args) 12 | self.args = args 13 | 14 | def update(self, minibatches, opt, sch): 15 | objective = 0 16 | 17 | for (xi, yi, di), (xj, yj, dj) in random_pairs_of_minibatches(self.args, minibatches): 18 | lam = np.random.beta(self.args.mixupalpha, self.args.mixupalpha) 19 | 20 | x = (lam * xi + (1 - lam) * xj).cuda().float() 21 | 22 | predictions = self.predict(x) 23 | 24 | objective += lam * F.cross_entropy(predictions, yi.cuda().long()) 25 | objective += (1 - lam) * \ 26 | F.cross_entropy(predictions, yj.cuda().long()) 27 | 28 | objective /= len(minibatches) 29 | 30 | opt.zero_grad() 31 | objective.backward() 32 | opt.step() 33 | if sch: 34 | sch.step() 35 | return {'class': objective.item()} 36 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/VREx.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | import torch.nn.functional as F 4 | from alg.algs.ERM import ERM 5 | 6 | 7 | class VREx(ERM): 8 | """V-REx algorithm from http://arxiv.org/abs/2003.00688""" 9 | 10 | def __init__(self, args): 11 | super(VREx, self).__init__(args) 12 | self.register_buffer('update_count', torch.tensor([0])) 13 | self.args = args 14 | 15 | def update(self, minibatches, opt, sch): 16 | if self.update_count >= self.args.anneal_iters: 17 | penalty_weight = self.args.lam 18 | else: 19 | penalty_weight = 1.0 20 | 21 | nll = 0. 22 | 23 | all_x = torch.cat([data[0].cuda().float() for data in minibatches]) 24 | all_logits = self.network(all_x) 25 | all_logits_idx = 0 26 | losses = torch.zeros(len(minibatches)) 27 | 28 | for i, data in enumerate(minibatches): 29 | logits = all_logits[all_logits_idx:all_logits_idx + 30 | data[0].shape[0]] 31 | all_logits_idx += data[0].shape[0] 32 | nll = F.cross_entropy(logits, data[1].cuda().long()) 33 | losses[i] = nll 34 | 35 | mean = losses.mean() 36 | penalty = ((losses - mean) ** 2).mean() 37 | loss = mean + penalty_weight * penalty 38 | 39 | opt.zero_grad() 40 | loss.backward() 41 | opt.step() 42 | if sch: 43 | sch.step() 44 | 45 | self.update_count += 1 46 | return {'loss': loss.item(), 'nll': nll.item(), 47 | 'penalty': penalty.item()} 48 | -------------------------------------------------------------------------------- /code/DeepDG/alg/algs/base.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | 4 | 5 | class Algorithm(torch.nn.Module): 6 | 7 | def __init__(self, args): 8 | super(Algorithm, self).__init__() 9 | 10 | def update(self, minibatches, opt, sch): 11 | raise NotImplementedError 12 | 13 | def predict(self, x): 14 | raise NotImplementedError 15 | -------------------------------------------------------------------------------- /code/DeepDG/alg/modelopera.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | from network import img_network 4 | 5 | 6 | def get_fea(args): 7 | if args.dataset == 'dg5': 8 | net = img_network.DTNBase() 9 | elif args.net.startswith('res'): 10 | net = img_network.ResBase(args.net) 11 | else: 12 | net = img_network.VGGBase(args.net) 13 | return net 14 | 15 | 16 | def accuracy(network, loader): 17 | correct = 0 18 | total = 0 19 | 20 | network.eval() 21 | with torch.no_grad(): 22 | for data in loader: 23 | x = data[0].cuda().float() 24 | y = data[1].cuda().long() 25 | p = network.predict(x) 26 | 27 | if p.size(1) == 1: 28 | correct += (p.gt(0).eq(y).float()).sum().item() 29 | else: 30 | correct += (p.argmax(1).eq(y).float()).sum().item() 31 | total += len(x) 32 | network.train() 33 | return correct / total 34 | -------------------------------------------------------------------------------- /code/DeepDG/datautil/imgdata/util.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from torchvision import transforms 3 | from PIL import Image, ImageFile 4 | ImageFile.LOAD_TRUNCATED_IMAGES = True 5 | 6 | 7 | def image_train(dataset, resize_size=256, crop_size=224): 8 | if dataset == 'dg5': 9 | return transforms.Compose([ 10 | transforms.Resize((32, 32)), 11 | transforms.ToTensor(), 12 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) 13 | ]) 14 | 15 | normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], 16 | std=[0.229, 0.224, 0.225]) 17 | 18 | return transforms.Compose([ 19 | transforms.RandomResizedCrop(224, scale=(0.7, 1.0)), 20 | transforms.RandomHorizontalFlip(), 21 | transforms.ColorJitter(0.3, 0.3, 0.3, 0.3), 22 | transforms.RandomGrayscale(), 23 | transforms.ToTensor(), 24 | normalize 25 | ]) 26 | 27 | 28 | def image_test(dataset, resize_size=256, crop_size=224): 29 | if dataset == 'dg5': 30 | return transforms.Compose([ 31 | transforms.Resize((32, 32)), 32 | transforms.ToTensor(), 33 | transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) 34 | ]) 35 | 36 | normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], 37 | std=[0.229, 0.224, 0.225]) 38 | return transforms.Compose([ 39 | transforms.Resize((224, 224)), 40 | transforms.ToTensor(), 41 | normalize 42 | ]) 43 | 44 | 45 | def rgb_loader(path): 46 | with open(path, 'rb') as f: 47 | with Image.open(f) as img: 48 | return img.convert('RGB') 49 | 50 | 51 | def l_loader(path): 52 | with open(path, 'rb') as f: 53 | with Image.open(f) as img: 54 | return img.convert('L') 55 | -------------------------------------------------------------------------------- /code/DeepDG/datautil/mydataloader.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import torch 4 | 5 | 6 | class _InfiniteSampler(torch.utils.data.Sampler): 7 | """Wraps another Sampler to yield an infinite stream.""" 8 | 9 | def __init__(self, sampler): 10 | self.sampler = sampler 11 | 12 | def __iter__(self): 13 | while True: 14 | for batch in self.sampler: 15 | yield batch 16 | 17 | 18 | class InfiniteDataLoader: 19 | def __init__(self, dataset, weights, batch_size, num_workers): 20 | super().__init__() 21 | 22 | if weights: 23 | sampler = torch.utils.data.WeightedRandomSampler(weights, 24 | replacement=True, 25 | num_samples=batch_size) 26 | else: 27 | sampler = torch.utils.data.RandomSampler(dataset, 28 | replacement=True) 29 | 30 | if weights == None: 31 | weights = torch.ones(len(dataset)) 32 | 33 | batch_sampler = torch.utils.data.BatchSampler( 34 | sampler, 35 | batch_size=batch_size, 36 | drop_last=True) 37 | 38 | self._infinite_iterator = iter(torch.utils.data.DataLoader( 39 | dataset, 40 | num_workers=num_workers, 41 | batch_sampler=_InfiniteSampler(batch_sampler) 42 | )) 43 | 44 | def __iter__(self): 45 | while True: 46 | yield next(self._infinite_iterator) 47 | 48 | def __len__(self): 49 | raise ValueError 50 | -------------------------------------------------------------------------------- /code/DeepDG/datautil/util.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import numpy as np 3 | import torch 4 | 5 | 6 | def Nmax(test_envs, d): 7 | for i in range(len(test_envs)): 8 | if d < test_envs[i]: 9 | return i 10 | return len(test_envs) 11 | 12 | 13 | def random_pairs_of_minibatches_by_domainperm(minibatches): 14 | perm = torch.randperm(len(minibatches)).tolist() 15 | pairs = [] 16 | 17 | for i in range(len(minibatches)): 18 | j = i + 1 if i < (len(minibatches) - 1) else 0 19 | 20 | xi, yi = minibatches[perm[i]][0], minibatches[perm[i]][1] 21 | xj, yj = minibatches[perm[j]][0], minibatches[perm[j]][1] 22 | 23 | min_n = min(len(xi), len(xj)) 24 | 25 | pairs.append(((xi[:min_n], yi[:min_n]), (xj[:min_n], yj[:min_n]))) 26 | 27 | return pairs 28 | 29 | 30 | def random_pairs_of_minibatches(args, minibatches): 31 | ld = len(minibatches) 32 | pairs = [] 33 | tdlist = np.arange(ld) 34 | txlist = np.arange(args.batch_size) 35 | for i in range(ld): 36 | for j in range(args.batch_size): 37 | (tdi, tdj), (txi, txj) = np.random.choice(tdlist, 2, 38 | replace=False), np.random.choice(txlist, 2, replace=True) 39 | if j == 0: 40 | xi, yi, di = torch.unsqueeze( 41 | minibatches[tdi][0][txi], dim=0), minibatches[tdi][1][txi], minibatches[tdi][2][txi] 42 | xj, yj, dj = torch.unsqueeze( 43 | minibatches[tdj][0][txj], dim=0), minibatches[tdj][1][txj], minibatches[tdj][2][txj] 44 | else: 45 | xi, yi, di = torch.vstack((xi, torch.unsqueeze(minibatches[tdi][0][txi], dim=0))), torch.hstack( 46 | (yi, minibatches[tdi][1][txi])), torch.hstack((di, minibatches[tdi][2][txi])) 47 | xj, yj, dj = torch.vstack((xj, torch.unsqueeze(minibatches[tdj][0][txj], dim=0))), torch.hstack( 48 | (yj, minibatches[tdj][1][txj])), torch.hstack((dj, minibatches[tdj][2][txj])) 49 | pairs.append(((xi, yi, di), (xj, yj, dj))) 50 | return pairs 51 | -------------------------------------------------------------------------------- /code/DeepDG/network/Adver_network.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Function 4 | 5 | 6 | class ReverseLayerF(Function): 7 | @staticmethod 8 | def forward(ctx, x, alpha): 9 | ctx.alpha = alpha 10 | return x.view_as(x) 11 | 12 | @staticmethod 13 | def backward(ctx, grad_output): 14 | output = grad_output.neg() * ctx.alpha 15 | return output, None 16 | 17 | 18 | class Discriminator(nn.Module): 19 | def __init__(self, input_dim=256, hidden_dim=256, num_domains=4): 20 | super(Discriminator, self).__init__() 21 | self.input_dim = input_dim 22 | self.hidden_dim = hidden_dim 23 | layers = [ 24 | nn.Linear(input_dim, hidden_dim), 25 | nn.BatchNorm1d(hidden_dim), 26 | nn.ReLU(), 27 | nn.Linear(hidden_dim, hidden_dim), 28 | nn.BatchNorm1d(hidden_dim), 29 | nn.ReLU(), 30 | nn.Linear(hidden_dim, num_domains), 31 | ] 32 | self.layers = torch.nn.Sequential(*layers) 33 | 34 | def forward(self, x): 35 | return self.layers(x) 36 | -------------------------------------------------------------------------------- /code/DeepDG/network/common_network.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch.nn as nn 3 | from network.util import init_weights 4 | import torch.nn.utils.weight_norm as weightNorm 5 | 6 | 7 | class feat_bottleneck(nn.Module): 8 | def __init__(self, feature_dim, bottleneck_dim=256, type="ori"): 9 | super(feat_bottleneck, self).__init__() 10 | self.bn = nn.BatchNorm1d(bottleneck_dim, affine=True) 11 | self.relu = nn.ReLU(inplace=True) 12 | self.dropout = nn.Dropout(p=0.5) 13 | self.bottleneck = nn.Linear(feature_dim, bottleneck_dim) 14 | # self.bottleneck.apply(init_weights) 15 | self.type = type 16 | 17 | def forward(self, x): 18 | x = self.bottleneck(x) 19 | if self.type == "bn": 20 | x = self.bn(x) 21 | return x 22 | 23 | 24 | class feat_classifier(nn.Module): 25 | def __init__(self, class_num, bottleneck_dim=256, type="linear"): 26 | super(feat_classifier, self).__init__() 27 | self.type = type 28 | if type == 'wn': 29 | self.fc = weightNorm( 30 | nn.Linear(bottleneck_dim, class_num), name="weight") 31 | # self.fc.apply(init_weights) 32 | else: 33 | self.fc = nn.Linear(bottleneck_dim, class_num) 34 | # self.fc.apply(init_weights) 35 | 36 | def forward(self, x): 37 | x = self.fc(x) 38 | return x 39 | 40 | 41 | class feat_classifier_two(nn.Module): 42 | def __init__(self, class_num, input_dim, bottleneck_dim=256): 43 | super(feat_classifier_two, self).__init__() 44 | self.type = type 45 | self.fc0 = nn.Linear(input_dim, bottleneck_dim) 46 | # self.fc0.apply(init_weights) 47 | self.fc1 = nn.Linear(bottleneck_dim, class_num) 48 | # self.fc1.apply(init_weights) 49 | 50 | def forward(self, x): 51 | x = self.fc0(x) 52 | x = self.fc1(x) 53 | return x 54 | -------------------------------------------------------------------------------- /code/DeepDG/network/util.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch.nn as nn 3 | import numpy as np 4 | 5 | 6 | def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0): 7 | return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low) 8 | 9 | 10 | def init_weights(m): 11 | classname = m.__class__.__name__ 12 | if classname.find('Conv2d') != -1 or classname.find('ConvTranspose2d') != -1: 13 | nn.init.kaiming_uniform_(m.weight) 14 | nn.init.zeros_(m.bias) 15 | elif classname.find('BatchNorm') != -1: 16 | nn.init.normal_(m.weight, 1.0, 0.02) 17 | nn.init.zeros_(m.bias) 18 | elif classname.find('Linear') != -1: 19 | nn.init.xavier_normal_(m.weight) 20 | nn.init.zeros_(m.bias) 21 | -------------------------------------------------------------------------------- /code/DeepDG/requirements.txt: -------------------------------------------------------------------------------- 1 | Please refer to https://hub.docker.com/r/jindongwang/docker. 2 | 3 | CUDA: 10.1 4 | Pytorch: 1.7 5 | Torchvision: 0.8.1 6 | -------------------------------------------------------------------------------- /code/Integrated sensing and communication (LSTM and VGG 16 model for digit and image classification, will be used for sensing and communication in 6G networks): -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /code/clip/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | sys.path.append(os.getcwd()) -------------------------------------------------------------------------------- /code/clip/data/download_data_azcopy.py: -------------------------------------------------------------------------------- 1 | # download datasets from azure blob storage, which is faster than downloading from the website 2 | 3 | import os 4 | import argparse 5 | 6 | DATA_DIR_AZURE = { 7 | 'office-home': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/OfficeHome/', 8 | 'office31': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/office31/', 9 | 'domainnet': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/domainnet/', 10 | 'visda': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/visda/', 11 | 'vlcs': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/VLCS/VLCS/', 12 | 'pacs': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/PACS/PACS/kfold/', 13 | 'terrainc': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/terra_incognita/terra_incognita/', 14 | 'wilds-camelyon': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/wilds/wilds/camelyon17_v1.0/', 15 | 'wilds-fmow': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/wilds/wilds/fmow_v1.1/', 16 | 'wilds-iwildcam': 'https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/wilds/wilds/iwildcam_v2.0/' 17 | } 18 | 19 | def get_args(): 20 | parser = argparse.ArgumentParser() 21 | parser.add_argument('--dataset', type=str, choices=DATA_DIR_AZURE.keys(), default='office31') 22 | parser.add_argument('--to', type=str, default='/data/jindwang/', help='path to save the dataset') 23 | args = parser.parse_args() 24 | return args 25 | 26 | if __name__ == '__main__': 27 | args = get_args() 28 | if not os.path.exists(os.getcwd() + '/azcopy'): 29 | azcopy_path = 'https://wjdcloud.blob.core.windows.net/dataset/azcopy' 30 | print('>>>>Downloading azcopy...') 31 | os.system('wget {}'.format(azcopy_path)) 32 | os.system('chmod +x azcopy') 33 | 34 | print('>>>>Downloading datasets...') 35 | src_path = DATA_DIR_AZURE[args.dataset] 36 | os.system(f'./azcopy copy "{src_path}" "{args.to}" --recursive') 37 | print('>>>>Done!') 38 | 39 | -------------------------------------------------------------------------------- /code/clip/log/log.txt: -------------------------------------------------------------------------------- 1 | This is the log folder. 2 | -------------------------------------------------------------------------------- /code/clip/requirements.txt: -------------------------------------------------------------------------------- 1 | clip==1.0 2 | cvxopt==1.3.0 3 | gdown==4.4.0 4 | numpy==1.22.0 5 | Pillow==9.3.0 6 | pretty_errors==1.2.25 7 | scikit_learn==1.1.3 8 | scipy==1.10.0 9 | torch==1.13.1 10 | torchvision==0.13.1 11 | tqdm==4.63.0 12 | wilds==2.0.0 13 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/README.md: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | # B-JMMD 3 | Balanced joint maximum mean discrepancy for deep transfer learning 4 | ======= 5 | 6 | This is the transfer learning library for the following paper: 7 | 8 | ### Balanced Joint Maximum Mean Discrepancy for Deep Transfer Learning 9 | 10 | ## Citation 11 | If you use this code for your research, please consider citing: 12 | ``` 13 | @article{Chuangji2020Balanced, 14 | title={Balanced Joint Maximum Mean Discrepancy for Deep Transfer Learning}, 15 | author={Chuangji Meng and Cunlu Xu and Qin Lei and Wei Su and Jinzhao Wu}, 16 | journal={Analysis and Applications}, 17 | number={2}, 18 | year={2020}, 19 | } 20 | 21 | ``` 22 | 23 | ## Contact 24 | If you have any problem about our code, feel free to contact 25 | - mengchj16@lzu.edu.cn 26 | - clxu@lzu.edu.cn 27 | - leiqin@mail.lzjtu.cn 28 | 29 | or describe your problem in Issues. 30 | >>>>>>> 7b5bf33... first commit 31 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Issues 4 | 5 | Specific Caffe design and development issues, bugs, and feature requests are maintained by GitHub Issues. 6 | 7 | _Please do not post usage, installation, or modeling questions, or other requests for help to Issues._ 8 | Use the [caffe-users list](https://groups.google.com/forum/#!forum/caffe-users) instead. This helps developers maintain a clear, uncluttered, and efficient view of the state of Caffe. 9 | 10 | When reporting a bug, it's most helpful to provide the following information, where applicable: 11 | 12 | * What steps reproduce the bug? 13 | * Can you reproduce the bug using the latest [master](https://github.com/BVLC/caffe/tree/master), compiled with the `DEBUG` make option? 14 | * What hardware and operating system/distribution are you running? 15 | * If the bug is a crash, provide the backtrace (usually printed by Caffe; always obtainable with `gdb`). 16 | 17 | Try to give your issue a title that is succinct and specific. The devs will rename issues as needed to keep track of them. 18 | 19 | ## Pull Requests 20 | 21 | Caffe welcomes all contributions. 22 | 23 | See the [contributing guide](http://caffe.berkeleyvision.org/development.html) for details. 24 | 25 | Briefly: read commit by commit, a PR should tell a clean, compelling story of _one_ improvement to Caffe. In particular: 26 | 27 | * A PR should do one clear thing that obviously improves Caffe, and nothing more. Making many smaller PRs is better than making one large PR; review effort is superlinear in the amount of code involved. 28 | * Similarly, each commit should be a small, atomic change representing one step in development. PRs should be made of many commits where appropriate. 29 | * Please do rewrite PR history to be clean rather than chronological. Within-PR bugfixes, style cleanups, reversions, etc. should be squashed and should not appear in merged PR history. 30 | * Anything nonobvious from the code should be explained in comments, commit messages, or the PR description, as appropriate. 31 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Contributors 2 | 3 | Caffe is developed by a core set of BVLC members and the open-source community. 4 | 5 | We thank all of our [contributors](https://github.com/BVLC/caffe/graphs/contributors)! 6 | 7 | **For the detailed history of contributions** of a given file, try 8 | 9 | git blame file 10 | 11 | to see line-by-line credits and 12 | 13 | git log --follow file 14 | 15 | to see the change log even across renames and rewrites. 16 | 17 | Please refer to the [acknowledgements](http://caffe.berkeleyvision.org/#acknowledgements) on the Caffe site for further details. 18 | 19 | **Copyright** is held by the original contributor according to the versioning history; see LICENSE. 20 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/INSTALL.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | See http://caffe.berkeleyvision.org/installation.html for the latest 4 | installation instructions. 5 | 6 | Check the users group in case you need help: 7 | https://groups.google.com/forum/#!forum/caffe-users 8 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/caffe.cloc: -------------------------------------------------------------------------------- 1 | Bourne Shell 2 | filter remove_matches ^\s*# 3 | filter remove_inline #.*$ 4 | extension sh 5 | script_exe sh 6 | C 7 | filter remove_matches ^\s*// 8 | filter call_regexp_common C 9 | filter remove_inline //.*$ 10 | extension c 11 | extension ec 12 | extension pgc 13 | C++ 14 | filter remove_matches ^\s*// 15 | filter remove_inline //.*$ 16 | filter call_regexp_common C 17 | extension C 18 | extension cc 19 | extension cpp 20 | extension cxx 21 | extension pcc 22 | C/C++ Header 23 | filter remove_matches ^\s*// 24 | filter call_regexp_common C 25 | filter remove_inline //.*$ 26 | extension H 27 | extension h 28 | extension hh 29 | extension hpp 30 | CUDA 31 | filter remove_matches ^\s*// 32 | filter remove_inline //.*$ 33 | filter call_regexp_common C 34 | extension cu 35 | Python 36 | filter remove_matches ^\s*# 37 | filter docstring_to_C 38 | filter call_regexp_common C 39 | filter remove_inline #.*$ 40 | extension py 41 | make 42 | filter remove_matches ^\s*# 43 | filter remove_inline #.*$ 44 | extension Gnumakefile 45 | extension Makefile 46 | extension am 47 | extension gnumakefile 48 | extension makefile 49 | filename Gnumakefile 50 | filename Makefile 51 | filename gnumakefile 52 | filename makefile 53 | script_exe make 54 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/include/caffe/layers/bjmmd_layer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAFFE_BJMMD_LOSS_LAYER_HPP_ 2 | #define CAFFE_BJMMD_LOSS_LAYER_HPP_ 3 | 4 | #include <vector> 5 | 6 | #include "caffe/blob.hpp" 7 | #include "caffe/layer.hpp" 8 | #include "caffe/proto/caffe.pb.h" 9 | 10 | #include "caffe/layers/loss_layer.hpp" 11 | 12 | namespace caffe { 13 | 14 | template <typename Dtype> 15 | class BJMMDLossLayer : public LossLayer<Dtype> { 16 | public: 17 | explicit BJMMDLossLayer(const LayerParameter& param) 18 | : LossLayer<Dtype>(param) {} 19 | virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom, 20 | const vector<Blob<Dtype>*>& top); 21 | virtual void Reshape(const vector<Blob<Dtype>*>& bottom, 22 | const vector<Blob<Dtype>*>& top); 23 | 24 | virtual inline const char* type() const { return "BJMMDLoss"; } 25 | virtual inline int ExactNumTopBlobs() const { return -1; } 26 | virtual inline int ExactNumBottomBlobs() const { return -1; } 27 | virtual inline int MinBottomBlobs() const { return 2; } 28 | virtual inline int MaxBottomBlobs() const { return 4; } 29 | 30 | protected: 31 | virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom, 32 | const vector<Blob<Dtype>*>& top); 33 | virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom, 34 | const vector<Blob<Dtype>*>& top); 35 | 36 | virtual void Backward_cpu(const vector<Blob<Dtype>*>& top, 37 | const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); 38 | virtual void Backward_gpu(const vector<Blob<Dtype>*>& top, 39 | const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom); 40 | 41 | 42 | int source_num_; 43 | int target_num_; 44 | int total_num_; 45 | int dim_; 46 | int kernel_num_; 47 | Dtype kernel_mul_; 48 | Dtype gamma_; 49 | Dtype sigma_; 50 | bool fix_gamma_; 51 | Blob<Dtype> diff_; 52 | vector<Blob<Dtype>*> kernel_val_; 53 | Blob<Dtype> diff_multiplier_; 54 | Dtype loss_weight_; 55 | Dtype label_loss_weight_; 56 | Dtype balanced_factor_; 57 | Blob<Dtype> delta_; 58 | int label_kernel_num_; 59 | Dtype label_kernel_mul_; 60 | bool auto_sigma_; 61 | }; 62 | 63 | } // namespace caffe 64 | 65 | #endif // CAFFE_BJMMD_LAYER_HPP_ 66 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/kmake.sh: -------------------------------------------------------------------------------- 1 | cd src 2 | protoc ./caffe/proto/caffe.proto --cpp_out=../include/ 3 | cd .. 4 | make -j48 5 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/models/B-JMMD/alexnet/solver.prototxt: -------------------------------------------------------------------------------- 1 | net: "models/BJMMD/alexnet/train_val.prototxt" 2 | test_iter: 795 # target domain: amazon 2817, webcam 795, dslr 498 3 | test_interval: 500 4 | base_lr: 0.003 5 | lr_policy: "inv" 6 | gamma: 0.001 7 | power: 0.75 8 | display: 500 9 | max_iter: 100000 10 | momentum: 0.9 11 | weight_decay: 0.0005 12 | snapshot: 600000 13 | snapshot_prefix: "models/BJMMD/alexnet/trained_model" 14 | snapshot_after_train: false 15 | solver_mode: GPU 16 | random_seed: 8 17 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/models/B-JMMD/resnet/solver.prototxt: -------------------------------------------------------------------------------- 1 | net: "models/BJMMD/resnet/train_val.prototxt" 2 | test_iter: 795 # target domain: amazon 2817, webcam 795, dslr 498 3 | test_interval: 500 4 | base_lr: 0.0003 5 | lr_policy: "inv" 6 | gamma: 0.001 7 | power: 0.75 8 | display: 1000 9 | max_iter: 30000 10 | momentum: 0.9 11 | weight_decay: 0.0005 12 | snapshot: 60000 13 | snapshot_prefix: "models/BJMMD/resnet/trained_model" 14 | snapshot_after_train: false 15 | solver_mode: GPU 16 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/models/bvlc_reference_caffenet/readme.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: BVLC CaffeNet Model 3 | caffemodel: bvlc_reference_caffenet.caffemodel 4 | caffemodel_url: http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel 5 | license: unrestricted 6 | sha1: 4c8d77deb20ea792f84eb5e6d0a11ca0a8660a46 7 | caffe_commit: 709dc15af4a06bebda027c1eb2b3f3e3375d5077 8 | --- 9 | 10 | This model is the result of following the Caffe [ImageNet model training instructions](http://caffe.berkeleyvision.org/gathered/examples/imagenet.html). 11 | It is a replication of the model described in the [AlexNet](http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks) publication with some differences: 12 | 13 | - not training with the relighting data-augmentation; 14 | - the order of pooling and normalization layers is switched (in CaffeNet, pooling is done before normalization). 15 | 16 | This model is snapshot of iteration 310,000. 17 | The best validation performance during training was iteration 313,000 with validation accuracy 57.412% and loss 1.82328. 18 | This model obtains a top-1 accuracy 57.4% and a top-5 accuracy 80.4% on the validation set, using just the center crop. 19 | (Using the average of 10 crops, (4 + 1 center) * 2 mirror, should obtain a bit higher accuracy still.) 20 | 21 | This model was trained by Jeff Donahue @jeffdonahue 22 | 23 | ## License 24 | 25 | This model is released for unrestricted use. 26 | -------------------------------------------------------------------------------- /code/deep/B-JMMD/caffe/models/bvlc_reference_caffenet/solver.prototxt: -------------------------------------------------------------------------------- 1 | net: "models/bvlc_reference_caffenet/train_val.prototxt" 2 | test_iter: 1000 3 | test_interval: 1000 4 | base_lr: 0.01 5 | lr_policy: "step" 6 | gamma: 0.1 7 | stepsize: 100000 8 | display: 20 9 | max_iter: 450000 10 | momentum: 0.9 11 | weight_decay: 0.0005 12 | snapshot: 10000 13 | snapshot_prefix: "models/bvlc_reference_caffenet/caffenet_train" 14 | solver_mode: GPU 15 | -------------------------------------------------------------------------------- /code/deep/CSG/a-domainbed/prepare_data.sh: -------------------------------------------------------------------------------- 1 | git clone https://github.com/facebookresearch/DomainBed 2 | cd DomainBed/ 3 | git checkout 2deb150 4 | python3 -m pip install gdown==3.13.0 5 | # python3 -m pip install wilds==1.1.0 torch_scatter # Installing `torch_scatter` seems quite involved. Turn to edit the files to exclude the import. 6 | vi "+14norm I# " "+15norm I# " "+271norm I# " "+267s/# //" "+270s/# //" "+wq" domainbed/scripts/download.py 7 | vi "+12norm I# " "+13norm I# " "+wq" domainbed/datasets.py 8 | python3 -m domainbed.scripts.download --data_dir=./domainbed/data 9 | 10 | -------------------------------------------------------------------------------- /code/deep/CSG/a-imageclef/prepare_data.sh: -------------------------------------------------------------------------------- 1 | DATAFILE=image_CLEF.zip 2 | wget https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/$DATAFILE 3 | mkdir -p data 4 | unzip $DATAFILE -d data/ 5 | mv $DATAFILE data/ 6 | 7 | -------------------------------------------------------------------------------- /code/deep/CSG/a-mnist/makedata.sh: -------------------------------------------------------------------------------- 1 | python3 makedata.py train --pleft 1. 0. --distr randn --loc 5. --scale 1. 2 | python3 makedata.py test --pleft .5 .5 --distr randn --loc 0. --scale 0. 3 | mv ./data/MNIST/processed/test01_0.5_0.5_randn_0.0_0.0.pt ./data/MNIST/processed/test_01.pt 4 | python3 makedata.py test --pleft .5 .5 --distr randn --loc 0. --scale 2. 5 | -------------------------------------------------------------------------------- /code/deep/CSG/a-mnist/run_da.sh: -------------------------------------------------------------------------------- 1 | REPRUN="../utils/reprun.sh 10" 2 | PYCMD="python3 main.py" 3 | TRAIN="--traindom train01_1.0_0.0_randn_5.0_1.0.pt" 4 | TEST="--testdoms test_01.pt test01_0.5_0.5_randn_0.0_2.0.pt" 5 | 6 | case $1 in 7 | dann) 8 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-4 9 | ;; 10 | cdan) 11 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-6 12 | ;; 13 | dan) 14 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-8 15 | ;; 16 | mdd) 17 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-6 18 | ;; 19 | bnm) 20 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-7 21 | ;; 22 | svae-da) # CSGz-DA 23 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-4 --mu_s .5 --sig_s .5 --pstd_x 3e-2 --qstd_s=-1. --tgt_mvn_prior 1 --src_mvn_prior 1 --wgen 1e-4 --wsup 1. 24 | ;; 25 | svgm-da) # CSG-DA 26 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite --optim RMSprop --lr 3e-4 --wl2 1e-5 --wda 1e-4 --mu_s .5 --sig_s .5 --mu_v .5 --sig_v .5 --corr_sv .9 --pstd_x 3e-2 --qstd_s=-1. --qstd_v=-1. --tgt_mvn_prior 1 --src_mvn_prior 1 --wgen 1e-4 --wsup 1. 27 | ;; 28 | *) 29 | echo "unknown argument $1" 30 | ;; 31 | esac 32 | 33 | -------------------------------------------------------------------------------- /code/deep/CSG/a-mnist/run_ood.sh: -------------------------------------------------------------------------------- 1 | REPRUN="../utils/reprun.sh 10" 2 | PYCMD="python3 main.py" 3 | TRAIN="--traindom train01_1.0_0.0_randn_5.0_1.0.pt" 4 | TEST="--testdoms test_01.pt test01_0.5_0.5_randn_0.0_2.0.pt" 5 | 6 | case $1 in 7 | discr) # CE 8 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 1e-3 --wl2 1e-5 9 | ;; 10 | cnbb) 11 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite-1.5x --optim RMSprop --lr 1e-3 --wl2 1e-5 --reg_w 1e-4 --reg_s 3e-6 --lr_w 1e-3 --n_iter_w 4 12 | ;; 13 | svae) # CSGz 14 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite --optim RMSprop --lr 1e-3 --wl2 1e-5 --mu_s .5 --sig_s .5 --pstd_x 3e-2 --qstd_s=-1. --wgen 1e-4 --wsup 1. --mvn_prior 1 15 | ;; 16 | svgm) # CSG 17 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite --optim RMSprop --lr 1e-3 --wl2 1e-5 --mu_s .5 --sig_s .5 --mu_v .5 --sig_v .5 --corr_sv .9 --pstd_x 3e-2 --qstd_s=-1. --qstd_v=-1. --wgen 1e-4 --wsup 1. --mvn_prior 1 18 | ;; 19 | svgm-ind) # CSG-ind 20 | $REPRUN $PYCMD $1 $TRAIN $TEST --discrstru lite --optim RMSprop --lr 1e-3 --wl2 1e-5 --mu_s .5 --sig_s .5 --mu_v .5 --sig_v .5 --corr_sv .9 --pstd_x 3e-2 --qstd_s=-1. --qstd_v=-1. --wgen 1e-4 --wsup 1. --mvn_prior 1 21 | ;; 22 | *) 23 | echo "unknown argument $1" 24 | ;; 25 | esac 26 | 27 | -------------------------------------------------------------------------------- /code/deep/CSG/arch/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/CSG/arch/__init__.py -------------------------------------------------------------------------------- /code/deep/CSG/arch/mlpstru.json: -------------------------------------------------------------------------------- 1 | { 2 | "MLPsvy1x": { 3 | "lite": { 4 | "dims_postx2prev": [400], 5 | "dim_v": 100, 6 | "dim_parav": 100, 7 | "dims_postv2s": [50], 8 | "dims_posts2prey": [] 9 | }, 10 | "lite-1.5x": { 11 | "dims_postx2prev": [600], 12 | "dim_v": 150, 13 | "dim_parav": 150, 14 | "dims_postv2s": [75], 15 | "dims_posts2prey": [] 16 | }, 17 | "bt882": { 18 | "dims_postx2prev": [400, 200], 19 | "dim_v": 8, 20 | "dim_parav": 8, 21 | "dims_postv2s": [2], 22 | "dims_posts2prey": [] 23 | }, 24 | "irm": { 25 | "dims_postx2prev": [256], 26 | "dim_v": 128, 27 | "dim_parav": 128, 28 | "dims_postv2s": [256], 29 | "dims_posts2prey": [] 30 | }, 31 | "bt882-256": { 32 | "dims_postx2prev": [256, 256], 33 | "dim_v": 8, 34 | "dim_parav": 8, 35 | "dims_postv2s": [2], 36 | "dims_posts2prey": [] 37 | } 38 | }, 39 | "MLPx1sv": { 40 | }, 41 | "MLPx1s": { 42 | }, 43 | "MLPv1s": { 44 | "s2v0": { 45 | "dims_pres2postv": [] 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /code/deep/CSG/csg-intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/CSG/csg-intro.png -------------------------------------------------------------------------------- /code/deep/CSG/distr/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.6 2 | '''Probabilistic Programming Package. 3 | 4 | The prototype is distributions, which can be a conditional one with 5 | functions for parameters to define the dependency. Distribution 6 | multiplication is implemented, as well as the mean, expectation, 7 | sampling with backprop capability, and log-probability. 8 | ''' 9 | 10 | __author__ = "Chang Liu" 11 | __version__ = "1.0.1" 12 | __email__ = "changliu@microsoft.com" 13 | 14 | from .base import Distr, DistrElem 15 | from .instances import Determ, Normal, MVNormal, Catg, Bern 16 | 17 | from .utils import ( append_attrs, 18 | edic, edicify, 19 | fargnames, fedic, wrap4_multi_batchdims, 20 | tensorify, is_scalar, is_same_tensor, 21 | expand_front, flatten_last, reduce_last, swap_dim_ranges, expand_middle, 22 | tcsizeify, tcsize_div, tcsize_broadcast, 23 | ) 24 | from .tools import elbo, elbo_z2xy, elbo_z2xy_twist, elbo_zy2x 25 | 26 | -------------------------------------------------------------------------------- /code/deep/CSG/methods/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.6 2 | __author__ = "Chang Liu" 3 | __email__ = "changliu@microsoft.com" 4 | 5 | from .semvar import SemVar 6 | from .supvae import SupVAE 7 | from .cnbb import CNBBLoss 8 | 9 | -------------------------------------------------------------------------------- /code/deep/CSG/requirements.txt: -------------------------------------------------------------------------------- 1 | requirements can be modified often. Please refer to https://github.com/changliu00/causal-semantic-generative-model/blob/main/requirements.txt for new requirements. -------------------------------------------------------------------------------- /code/deep/CSG/test/utils_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.6 2 | import sys 3 | from time import time 4 | import numpy as np 5 | sys.path.append('..') 6 | import utils 7 | '''Test cases of the 'distr' package. 8 | ''' 9 | 10 | __author__ = "Chang Liu" 11 | __email__ = "changliu@microsoft.com" 12 | 13 | shape = (500, 200, 50) 14 | 15 | arr = np.random.rand(*shape) 16 | length = arr.shape[-1] 17 | for n_win in range(1, 3*shape[-1] + 2): 18 | print(f"{n_win:3d}", end=", ") 19 | lwid = (n_win - 1) // 2 20 | rwid = n_win//2 + 1 21 | t = time(); ma_slim = utils.moving_average_slim(arr, n_win); print(f"{time() - t:.6f}", end=", ") 22 | t = time(); ma_full = utils.moving_average_full(arr, n_win); print(f"{time() - t:.6f}", end=", ") 23 | t = time(); ma_full_check = utils.moving_average_full_checker(arr, n_win); print(f"{time() - t:.6f}", end=", ") 24 | ma_slim_check = ma_full_check[..., lwid: length-rwid+1] 25 | # print(ma_slim.shape, ma_slim_check.shape, ma_full.shape, ma_full_check.shape) 26 | print(f"slim: {np.allclose(ma_slim, ma_slim_check)}, full: {np.allclose(ma_full, ma_full_check)}") 27 | 28 | -------------------------------------------------------------------------------- /code/deep/CSG/utils/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.6 2 | __author__ = "Chang Liu" 3 | __email__ = "changliu@microsoft.com" 4 | 5 | from .utils import * 6 | 7 | -------------------------------------------------------------------------------- /code/deep/CSG/utils/preprocess/__init__.py: -------------------------------------------------------------------------------- 1 | """ This module is adapted from Jindong Wang <jindong.wang@microsoft.com> 2 | <https://github.com/jindongwang>. 3 | """ 4 | -------------------------------------------------------------------------------- /code/deep/CSG/utils/reprun.sh: -------------------------------------------------------------------------------- 1 | n=$1 2 | command=$2 3 | shift 4 | shift 5 | for i in $(seq 1 $n) 6 | do 7 | $command $* 8 | done 9 | 10 | -------------------------------------------------------------------------------- /code/deep/DAAN/assets/ablation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/DAAN/assets/ablation.png -------------------------------------------------------------------------------- /code/deep/DAAN/assets/arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/DAAN/assets/arch.png -------------------------------------------------------------------------------- /code/deep/DAAN/assets/imclef.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/DAAN/assets/imclef.png -------------------------------------------------------------------------------- /code/deep/DAAN/assets/officehome.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/DAAN/assets/officehome.png -------------------------------------------------------------------------------- /code/deep/DAAN/data_loader.py: -------------------------------------------------------------------------------- 1 | from torchvision import datasets, transforms 2 | import torch 3 | import numpy as np 4 | from torchvision import transforms 5 | import os 6 | from PIL import Image, ImageOps 7 | 8 | class ResizeImage(): 9 | def __init__(self, size): 10 | if isinstance(size, int): 11 | self.size = (int(size), int(size)) 12 | else: 13 | self.size = size 14 | def __call__(self, img): 15 | th, tw = self.size 16 | return img.resize((th, tw)) 17 | 18 | class PlaceCrop(object): 19 | 20 | def __init__(self, size, start_x, start_y): 21 | if isinstance(size, int): 22 | self.size = (int(size), int(size)) 23 | else: 24 | self.size = size 25 | self.start_x = start_x 26 | self.start_y = start_y 27 | 28 | def __call__(self, img): 29 | th, tw = self.size 30 | return img.crop((self.start_x, self.start_y, self.start_x + tw, self.start_y + th)) 31 | 32 | def load_training(root_path, dir, batch_size, kwargs): 33 | 34 | transform = transforms.Compose( 35 | [transforms.Resize([256, 256]), 36 | transforms.RandomCrop(224), 37 | transforms.RandomHorizontalFlip(), 38 | transforms.ToTensor()]) 39 | data = datasets.ImageFolder(root=root_path + dir, transform=transform) 40 | train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True, **kwargs) 41 | return train_loader 42 | 43 | def load_testing(root_path, dir, batch_size, kwargs): 44 | start_center = (256 - 224 - 1) / 2 45 | transform = transforms.Compose( 46 | [transforms.Resize([224, 224]), 47 | PlaceCrop(224, start_center, start_center), 48 | transforms.ToTensor()]) 49 | data = datasets.ImageFolder(root=root_path + dir, transform=transform) 50 | test_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=False, drop_last=False, **kwargs) 51 | return test_loader 52 | 53 | 54 | -------------------------------------------------------------------------------- /code/deep/DAAN/functions.py: -------------------------------------------------------------------------------- 1 | from torch.autograd import Function 2 | 3 | class ReverseLayerF(Function): 4 | 5 | @staticmethod 6 | def forward(ctx, x, alpha): 7 | ctx.alpha = alpha 8 | 9 | return x.view_as(x) 10 | 11 | @staticmethod 12 | def backward(ctx, grad_output): 13 | output = grad_output.neg() * ctx.alpha 14 | 15 | return output, None 16 | 17 | -------------------------------------------------------------------------------- /code/deep/DAAN/model/__init__.py: -------------------------------------------------------------------------------- 1 | from . import * -------------------------------------------------------------------------------- /code/deep/DAAN/scripts/train.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export CUDA_VISIBLE_DEVICES=1 4 | 5 | PROJ_ROOT="/home/userxxx/DAAN" 6 | PROJ_NAME="tmp" 7 | LOG_FILE="${PROJ_ROOT}/log/${PROJ_NAME}-`date +'%Y-%m-%d-%H-%M-%S'`.log" 8 | 9 | #echo "GPU: $CUDA_VISIBLE_DEVICES" > ${LOG_FILE} 10 | #python ${PROJ_ROOT}/train.py >> ${LOG_FILE} 2>&1 11 | 12 | python ${PROJ_ROOT}/train.py 2>&1 | tee -a ${LOG_FILE} 13 | -------------------------------------------------------------------------------- /code/deep/DAN/README.md: -------------------------------------------------------------------------------- 1 | # DAN 2 | A PyTorch implementation of '[Learning Transferable Features with Deep Adaptation Networks](http://ise.thss.tsinghua.edu.cn/~mlong/doc/deep-adaptation-networks-icml15.pdf)'. 3 | The contributions of this paper are summarized as follows. 4 | * They propose a novel deep neural network architecture for domain adaptation, in which all the layers corresponding to task-specific features are adapted in a layerwise manner, hence benefiting from “deep adaptation.” 5 | * They explore multiple kernels for adapting deep representations, which substantially enhances adaptation effectiveness compared to single kernel methods. Our model can yield unbiased deep features with statistical guarantees. 6 | 7 | ## Requirement 8 | * python 3 9 | * pytorch 1.0 10 | 11 | ## Usage 12 | 1. You can download Office31 dataset [here](https://pan.baidu.com/s/1o8igXT4#list/path=%2F). And then unrar dataset in ./dataset/. 13 | 2. You can change the `source_name` and `target_name` in `DAN.py` to set different transfer tasks. 14 | 3. Run `python DAN.py`. 15 | 16 | ## Results on Office31 17 | | Method | A - W | D - W | W - D | A - D | D - A | W - A | Average | 18 | |:--------------:|:-----:|:-----:|:-----:|:-----:|:----:|:----:|:-------:| 19 | | DANori | 83.8±0.4 | 96.8±0.2 | 99.5±0.1 | 78.4±0.2 | 66.7±0.3 | 62.7±0.2 | 81.3 | 20 | | DANlast | 81.6±0.7 | 97.2±0.1 | 99.5±0.1 | 80.0±0.7 | 66.2±0.6 | 65.6±0.4 | 81.7 | 21 | | DANmax | 82.6±0.7 | 97.7±0.1 | 100.0±0.0 | 83.1±0.9 | 66.8±0.3 | 66.6±0.4 | 82.8 | 22 | 23 | > Note that the results **DANori** comes from [paper](http://ise.thss.tsinghua.edu.cn/~mlong/doc/multi-adversarial-domain-adaptation-aaai18.pdf) which has the same author as DAN. The **DANlast** is the results of the last epoch, and **DANmax** is the results of the max results in all epoches. Both **DANlast** and **DANmax** are run by myself with the code. 24 | -------------------------------------------------------------------------------- /code/deep/DANN(RevGrad)/adv_layer.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Function 4 | import torch.nn.functional as F 5 | 6 | 7 | class ReverseLayerF(Function): 8 | 9 | @staticmethod 10 | def forward(ctx, x, alpha): 11 | ctx.alpha = alpha 12 | return x.view_as(x) 13 | 14 | @staticmethod 15 | def backward(ctx, grad_output): 16 | output = grad_output.neg() * ctx.alpha 17 | return output, None 18 | 19 | class Discriminator(nn.Module): 20 | def __init__(self, input_dim=256, hidden_dim=256): 21 | super(Discriminator, self).__init__() 22 | self.input_dim = input_dim 23 | self.hidden_dim = hidden_dim 24 | self.dis1 = nn.Linear(input_dim, hidden_dim) 25 | self.bn = nn.BatchNorm1d(hidden_dim) 26 | self.dis2 = nn.Linear(hidden_dim, 1) 27 | 28 | 29 | def forward(self, x): 30 | x = F.relu(self.dis1(x)) 31 | x = self.dis2(self.bn(x)) 32 | x = torch.sigmoid(x) 33 | return x -------------------------------------------------------------------------------- /code/deep/DANN(RevGrad)/readme.md: -------------------------------------------------------------------------------- 1 | # Domain adversarial neural network (DANN/RevGrad) 2 | 3 | Code has been rewritten. Please go to https://github.com/jindongwang/transferlearning/tree/master/code/DeepDA for the unified code implementation. -------------------------------------------------------------------------------- /code/deep/DDC_DeepCoral/README.md: -------------------------------------------------------------------------------- 1 | # Deep Coral and DDC 2 | 3 | 4 | Code has been rewritten. Please go to https://github.com/jindongwang/transferlearning/tree/master/code/DeepDA for the unified code implementation. -------------------------------------------------------------------------------- /code/deep/DSAN/README.md: -------------------------------------------------------------------------------- 1 | # DSAN 2 | A PyTorch implementation of 'Deep Subdomain Adaptation Network for Image Classification' which has published on IEEE Transactions on Neural Networks and Learning Systems. 3 | The contributions of this paper are summarized as follows. 4 | * They propose a novel deep neural network architecture for Subdomain Adaptation, which can extend the ability of deep adaptation networks by capturing the fine-grained information for each category. 5 | * They show that DSAN which is a non-adversarial method can achieve the remarkable results. In addition, their DSAN is very simple and easy to implement. 6 | ## Requirement 7 | * python 3 8 | * pytorch 1.0 9 | 10 | ## Usage 11 | 1. You can download Office31 dataset [here](https://pan.baidu.com/s/1o8igXT4#list/path=%2F). And then unrar dataset in ./dataset/. 12 | 2. You can change the `source_name` and `target_name` in `Config.py` to set different transfer tasks. 13 | 3. Run `python DSAN.py`. 14 | 15 | ## Results on Office31 16 | | Method | A - W | D - W | W - D | A - D | D - A | W - A | Average | 17 | |:--------------:|:-----:|:-----:|:-----:|:-----:|:----:|:----:|:-------:| 18 | | DSAN | 93.6±0.2 | 98.4±0.1 | 100.0±0.0 | 90.2±0.7 | 73.5±0.5 | 74.8±0.4 | 88.4 | 19 | 20 | > Note that for tasks D-A and W-A, setting epochs = 800 or larger could achieve better performance. 21 | 22 | ## Reference 23 | 24 | ``` 25 | Zhu Y, Zhuang F, Wang J, et al. Deep Subdomain Adaptation Network for Image Classification[J]. IEEE Transactions on Neural Networks and Learning Systems, 2020. 26 | ``` 27 | 28 | or in bibtex style: 29 | 30 | ``` 31 | @article{zhu2020deep, 32 | title={Deep Subdomain Adaptation Network for Image Classification}, 33 | author={Zhu, Yongchun and Zhuang, Fuzhen and Wang, Jindong and Ke, Guolin and Chen, Jingwu and Bian, Jiang and Xiong, Hui and He, Qing}, 34 | journal={IEEE Transactions on Neural Networks and Learning Systems}, 35 | year={2020}, 36 | publisher={IEEE} 37 | } 38 | ``` -------------------------------------------------------------------------------- /code/deep/DaNN/DaNN.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | 4 | class DaNN(nn.Module): 5 | def __init__(self, n_input=28 * 28, n_hidden=256, n_class=10): 6 | super(DaNN, self).__init__() 7 | self.layer_input = nn.Linear(n_input, n_hidden) 8 | self.dropout = nn.Dropout(p=0.5) 9 | self.relu = nn.ReLU() 10 | self.layer_hidden = nn.Linear(n_hidden, n_class) 11 | 12 | def forward(self, src, tar): 13 | x_src = self.layer_input(src) 14 | x_tar = self.layer_input(tar) 15 | x_src = self.dropout(x_src) 16 | x_tar = self.dropout(x_tar) 17 | x_src_mmd = self.relu(x_src) 18 | x_tar_mmd = self.relu(x_tar) 19 | y_src = self.layer_hidden(x_src_mmd) 20 | return y_src, x_src_mmd, x_tar_mmd 21 | -------------------------------------------------------------------------------- /code/deep/DaNN/data_loader.py: -------------------------------------------------------------------------------- 1 | import torchvision 2 | import torch 3 | from torchvision import datasets,transforms 4 | 5 | def load_data(root_dir,domain,batch_size): 6 | transform = transforms.Compose([ 7 | transforms.Grayscale(), 8 | transforms.Resize([28, 28]), 9 | transforms.ToTensor(), 10 | transforms.Normalize(mean=(0,),std=(1,)), 11 | ] 12 | ) 13 | image_folder = datasets.ImageFolder( 14 | root=root_dir + domain, 15 | transform=transform 16 | ) 17 | data_loader = torch.utils.data.DataLoader(dataset=image_folder,batch_size=batch_size,shuffle=True,num_workers=2,drop_last=True 18 | ) 19 | return data_loader 20 | 21 | def load_test(root_dir,domain,batch_size): 22 | transform = transforms.Compose([ 23 | transforms.Grayscale(), 24 | transforms.Resize([28, 28]), 25 | transforms.ToTensor(), 26 | transforms.Normalize(mean=(0,), std=(1,)), 27 | ] 28 | ) 29 | image_folder = datasets.ImageFolder( 30 | root=root_dir + domain, 31 | transform=transform 32 | ) 33 | data_loader = torch.utils.data.DataLoader(dataset=image_folder, batch_size=batch_size, shuffle=False, num_workers=2 34 | ) 35 | return data_loader -------------------------------------------------------------------------------- /code/deep/DaNN/readme.md: -------------------------------------------------------------------------------- 1 | # DaNN (Domain Adaptive Neural Networks) 2 | 3 | - - - 4 | 5 | This is the implementation of **Domain Adaptive Neural Networks (DaNN)** using PyTorch. The original paper can be found at https://link.springer.com/chapter/10.1007/978-3-319-13560-1_76. 6 | 7 | DaNN is rather a *simple* neural network (with only 1 hidden layer) for domain adaptation. But its idea is important that brings MMD (maximum mean discrpancy) for adaptation in neural network. From then on, many researches are following this idea to embed MMD or other measurements (e.g. CORAL loss, Wasserstein distance) into deeper (e.g. AlexNet, ResNet) networks. 8 | 9 | I think if you are beginning to learn **deep transfer learning**, it is better to start with the most original and simple one. 10 | 11 | ## Dataset 12 | 13 | The original paper adopted the popular *Office+Caltech10* dataset. You can download them [HERE](https://github.com/jindongwang/transferlearning/blob/master/data/dataset.md#download) and put them into a new folder named `data`. 14 | 15 | - - - 16 | 17 | ## Usage 18 | 19 | Make sure you have Python 3.6 and PyTorch 0.3.0. As for other requirements, I'm sure you are satisfied. 20 | 21 | - `DaNN.py` is the DaNN model 22 | - `mmd.py` is the MMD measurement. You're welcome to change it to others. 23 | - `data_loader.py` is the help function for loading data. 24 | - `main.py` is the main training and test program. You can directly run this file by `python main.py`. 25 | 26 | - - - 27 | 28 | ### Reference 29 | 30 | Ghifary M, Kleijn W B, Zhang M. Domain adaptive neural networks for object recognition[C]//Pacific Rim International Conference on Artificial Intelligence. Springer, Cham, 2014: 898-904. 31 | -------------------------------------------------------------------------------- /code/deep/DeepCoral/README.md: -------------------------------------------------------------------------------- 1 | # Deep Coral 2 | 3 | This implementation has been moved to [DDC_DeepCoral](https://github.com/jindongwang/transferlearning/tree/master/code/deep/DDC_DeepCoral) for better union with the mmd loss. -------------------------------------------------------------------------------- /code/deep/DeepMEDA/README.md: -------------------------------------------------------------------------------- 1 | # DeepMEDA (DDAN) 2 | 3 | A PyTorch implementation of **Transfer Learning with Dynamic Distribution Adaptation** which has published on ACM Transactions on Intelligent Systems and Technology. 4 | 5 | This is also called **DDAN (Deep Dynamic Adaptation Network)**. 6 | 7 | Matlab version is [HERE](https://github.com/jindongwang/transferlearning/tree/master/code/traditional/MEDA/matlab). 8 | 9 | ## Requirement 10 | 11 | * python 3 12 | * pytorch 1.x 13 | * Numpy, scikit-learn 14 | 15 | ## Usage 16 | 17 | 1. You can download Office31 dataset [here](https://pan.baidu.com/s/1o8igXT4#list/path=%2F). Also, other datasets are supported in [here](https://github.com/jindongwang/transferlearning/tree/master/data). 18 | 2. Run `python main.py --src dslr --tar amazon --batch_size 32`. 19 | 20 | > Note that for tasks D-A and W-A, setting epochs = 800 or larger could achieve better performance. 21 | 22 | ## Reference 23 | 24 | ``` 25 | Wang J, Chen Y, Feng W, et al. Transfer learning with dynamic distribution adaptation[J]. 26 | ACM Transactions on Intelligent Systems and Technology (TIST), 2020, 11(1): 1-25. 27 | ``` 28 | 29 | or in bibtex style: 30 | 31 | ``` 32 | @article{wang2020transfer, 33 | title={Transfer learning with dynamic distribution adaptation}, 34 | author={Wang, Jindong and Chen, Yiqiang and Feng, Wenjie and Yu, Han and Huang, Meiyu and Yang, Qiang}, 35 | journal={ACM Transactions on Intelligent Systems and Technology (TIST)}, 36 | volume={11}, 37 | number={1}, 38 | pages={1--25}, 39 | year={2020}, 40 | publisher={ACM New York, NY, USA} 41 | } 42 | ``` -------------------------------------------------------------------------------- /code/deep/DeepMEDA/data_loader.py: -------------------------------------------------------------------------------- 1 | from torchvision import datasets, transforms 2 | import torch 3 | import os 4 | 5 | def load_training(root_path, dir, batch_size, kwargs): 6 | transform = transforms.Compose( 7 | [transforms.Resize([256, 256]), 8 | transforms.RandomCrop(224), 9 | transforms.RandomHorizontalFlip(), 10 | transforms.ToTensor()]) 11 | data = datasets.ImageFolder(root=os.path.join(root_path, dir), transform=transform) 12 | train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True, **kwargs) 13 | return train_loader 14 | 15 | def load_testing(root_path, dir, batch_size, kwargs): 16 | transform = transforms.Compose( 17 | [transforms.Resize([224, 224]), 18 | transforms.ToTensor()]) 19 | data = datasets.ImageFolder(root=os.path.join(root_path, dir), transform=transform) 20 | test_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, **kwargs) 21 | return test_loader -------------------------------------------------------------------------------- /code/deep/DeepMEDA/deep_meda.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import ResNet 4 | import mmd 5 | import dynamic_factor 6 | 7 | class DeepMEDA(nn.Module): 8 | 9 | def __init__(self, num_classes=31, bottle_neck=True): 10 | super(DeepMEDA, self).__init__() 11 | self.feature_layers = ResNet.resnet50(True) 12 | self.mmd_loss = mmd.MMD_loss() 13 | self.bottle_neck = bottle_neck 14 | if bottle_neck: 15 | self.bottle = nn.Linear(2048, 256) 16 | self.cls_fc = nn.Linear(256, num_classes) 17 | else: 18 | self.cls_fc = nn.Linear(2048, num_classes) 19 | 20 | 21 | def forward(self, source, target, s_label): 22 | source = self.feature_layers(source) 23 | if self.bottle_neck: 24 | source = self.bottle(source) 25 | s_pred = self.cls_fc(source) 26 | target = self.feature_layers(target) 27 | if self.bottle_neck: 28 | target = self.bottle(target) 29 | t_label = self.cls_fc(target) 30 | loss_c = self.mmd_loss.conditional(source, target, s_label, torch.nn.functional.softmax(t_label, dim=1)) 31 | loss_m = self.mmd_loss.marginal(source, target) 32 | mu = dynamic_factor.estimate_mu(source.detach().cpu().numpy(), s_label.detach().cpu().numpy(), target.detach().cpu().numpy(), torch.max(t_label, 1)[1].detach().cpu().numpy()) 33 | return s_pred, loss_c, loss_m, mu 34 | 35 | def predict(self, x): 36 | x = self.feature_layers(x) 37 | if self.bottle_neck: 38 | x = self.bottle(x) 39 | return self.cls_fc(x) -------------------------------------------------------------------------------- /code/deep/Learning-to-Match/README.md: -------------------------------------------------------------------------------- 1 | # Learning to Match Distributions for Domain Adaptation 2 | 3 | The code will be released soon. -------------------------------------------------------------------------------- /code/deep/MRAN/Multi-representation adaptation network for cross-domain image Neural network zhuyongchun.pdf: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:2af486e05909c170eca579c12801faddafddafe5351c3275188793c5c114f621 3 | size 890193 4 | -------------------------------------------------------------------------------- /code/deep/MRAN/README.md: -------------------------------------------------------------------------------- 1 | # DAN 2 | A PyTorch implementation of '[Multi-representationadaptationnetworkforcross-domainimage 3 | classification](https://www.sciencedirect.com/science/article/pii/S0893608019301984)'. 4 | The contributions of this paper are summarized as follows. 5 | * We are the first to learn multiple different domain-invariant representations by Inception 6 | Adaptation Module (IAM) for cross-domain image classification. 7 | * A novel Multi-Representation Adaptation Network (MRAN) is proposed to align distributions of multiple different representations which might contain more information about the images. 8 | 9 | ## Requirement 10 | * python 3 11 | * pytorch 1.0 12 | * torchvision 0.2.0 13 | 14 | ## Usage 15 | 1. You can download Office31 dataset [here](https://pan.baidu.com/s/1o8igXT4#list/path=%2F). And then unrar dataset in ./dataset/. 16 | 2. You can change the `source_name` and `target_name` in `MRAN.py` to set different transfer tasks. 17 | 3. Run `python MRAN.py`. 18 | 19 | ## Results on Office31 20 | | Method | A - W | D - W | W - D | A - D | D - A | W - A | Average | 21 | |:--------------:|:-----:|:-----:|:-----:|:-----:|:----:|:----:|:-------:| 22 | | MRAN | 91.4±0.1 | 96.9±0.3 | 99.8±0.2 | 86.4±0.6 | 68.3±0.5 | 70.9±0.6 | 85.6 | 23 | -------------------------------------------------------------------------------- /code/deep/MRAN/data_loader.py: -------------------------------------------------------------------------------- 1 | from torchvision import datasets, transforms 2 | import torch 3 | 4 | def load_training(root_path, dir, batch_size, kwargs): 5 | transform = transforms.Compose( 6 | [transforms.Resize([256, 256]), 7 | transforms.RandomCrop(224), 8 | transforms.RandomHorizontalFlip(), 9 | transforms.ToTensor()]) 10 | data = datasets.ImageFolder(root=root_path + dir, transform=transform) 11 | train_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, drop_last=True, **kwargs) 12 | return train_loader 13 | 14 | def load_testing(root_path, dir, batch_size, kwargs): 15 | transform = transforms.Compose( 16 | [transforms.Resize([224, 224]), 17 | transforms.ToTensor()]) 18 | data = datasets.ImageFolder(root=root_path + dir, transform=transform) 19 | test_loader = torch.utils.data.DataLoader(data, batch_size=batch_size, shuffle=True, **kwargs) 20 | return test_loader -------------------------------------------------------------------------------- /code/deep/MRAN/mmd.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # encoding: utf-8 3 | 4 | 5 | import torch 6 | import numpy as np 7 | from torch.autograd import Variable 8 | min_var_est = 1e-8 9 | 10 | def guassian_kernel(source, target, kernel_mul=2.0, kernel_num=5, fix_sigma=None): 11 | n_samples = int(source.size()[0])+int(target.size()[0]) 12 | total = torch.cat([source, target], dim=0) 13 | total0 = total.unsqueeze(0).expand(int(total.size(0)), int(total.size(0)), int(total.size(1))) 14 | total1 = total.unsqueeze(1).expand(int(total.size(0)), int(total.size(0)), int(total.size(1))) 15 | L2_distance = ((total0-total1)**2).sum(2) 16 | if fix_sigma: 17 | bandwidth = fix_sigma 18 | else: 19 | bandwidth = torch.sum(L2_distance.data) / (n_samples**2-n_samples) 20 | bandwidth /= kernel_mul ** (kernel_num // 2) 21 | bandwidth_list = [bandwidth * (kernel_mul**i) for i in range(kernel_num)] 22 | kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for bandwidth_temp in bandwidth_list] 23 | return sum(kernel_val)#/len(kernel_val) 24 | 25 | def cmmd(source, target, s_label, t_label, kernel_mul=2.0, kernel_num=5, fix_sigma=None): 26 | s_label = s_label.cpu() 27 | s_label = s_label.view(32,1) 28 | s_label = torch.zeros(32, 31).scatter_(1, s_label.data, 1) 29 | s_label = Variable(s_label).cuda() 30 | 31 | t_label = t_label.cpu() 32 | t_label = t_label.view(32, 1) 33 | t_label = torch.zeros(32, 31).scatter_(1, t_label.data, 1) 34 | t_label = Variable(t_label).cuda() 35 | 36 | batch_size = int(source.size()[0]) 37 | kernels = guassian_kernel(source, target, 38 | kernel_mul=kernel_mul, kernel_num=kernel_num, fix_sigma=fix_sigma) 39 | loss = 0 40 | XX = kernels[:batch_size, :batch_size] 41 | YY = kernels[batch_size:, batch_size:] 42 | XY = kernels[:batch_size, batch_size:] 43 | loss += torch.mean(torch.mm(s_label, torch.transpose(s_label, 0, 1)) * XX + 44 | torch.mm(t_label, torch.transpose(t_label, 0, 1)) * YY - 45 | 2 * torch.mm(s_label, torch.transpose(t_label, 0, 1)) * XY) 46 | return loss -------------------------------------------------------------------------------- /code/deep/README.md: -------------------------------------------------------------------------------- 1 | # Deep Transfer Learning on PyTorch 2 | 3 | This directory contains some re-implemented Pytorch codes for deep transfer learning. 4 | 5 | ## Update 6 | 7 | 8 | Some codes have been rewritten. Please go to https://github.com/jindongwang/transferlearning/tree/master/code/DeepDA for the unified code implementation. 9 | 10 | Algorithms in this folder that **will be** moved to DeepDA folder: 11 | 12 | - DeepMEDA 13 | - TCP 14 | - MRAN 15 | - Learning-to-match 16 | 17 | Algorithms in this folder that **have been** moved to DeepDA folder: 18 | - DAN (DDC) 19 | - DANN (RevGrad) 20 | - DCORAL 21 | - DSAN -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/__init__.py -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/common.py: -------------------------------------------------------------------------------- 1 | """ 2 | Provides some useful functions. 3 | """ 4 | 5 | from __future__ import absolute_import 6 | 7 | import os 8 | import time 9 | 10 | import numpy as np 11 | 12 | 13 | def readable_time_str(): 14 | """Get readable time string based on current local time. 15 | 16 | The time string will be formatted as %Y-%m-%d %H:%M:%S. 17 | 18 | Returns 19 | ------- 20 | str 21 | Readable time string. 22 | 23 | """ 24 | return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 25 | 26 | 27 | def user_home_dir(): 28 | """Get path of user home directory. 29 | 30 | Returns 31 | ------- 32 | str 33 | Path of user home directory. 34 | 35 | """ 36 | return os.path.expanduser("~") 37 | 38 | 39 | def to_numpy(data): 40 | """Convert other data type to numpy. If the data itself 41 | is numpy type, then a copy will be made and returned. 42 | 43 | Returns 44 | ------- 45 | numpy.array 46 | Numpy array of passed data. 47 | 48 | """ 49 | if 'mxnet' in str(type(data)): 50 | data = data.asnumpy() 51 | elif 'torch' in str(type(data)): 52 | data = data.cpu().numpy() 53 | elif 'numpy' in str(type(data)): 54 | data = np.copy(data) 55 | return data 56 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/DNNtest/strategy/__init__.py: -------------------------------------------------------------------------------- 1 | # Import strategies. 2 | # from .adapt import AdaptiveParameterizedStrategy 3 | # from .adapt import ParameterizedStrategy 4 | from .deepxplore import UncoveredRandomStrategy 5 | from .dlfuzz import DLFuzzRoundRobin 6 | from .dlfuzz import MostCoveredStrategy 7 | from .random import RandomStrategy 8 | 9 | # Aliases for some strategies. 10 | # Adapt = AdaptiveParameterizedStrategy 11 | DeepXplore = UncoveredRandomStrategy 12 | DLFuzzFirst = MostCoveredStrategy 13 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/DNNtest/strategy/random.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from .strategy import Strategy 4 | from pdb import set_trace as st 5 | 6 | class RandomStrategy(Strategy): 7 | '''A strategy that randomly selects neurons from all neurons. 8 | 9 | This strategy selects neurons from a set of all neurons in the network, 10 | except for the neurons that located in skippable layers. 11 | ''' 12 | 13 | def init(self, covered): 14 | self.num_input, self.num_neurons = covered.shape 15 | 16 | 17 | def select(self, k, ): 18 | '''Seleck k neurons randomly. 19 | 20 | Select k neurons randomly from a set of all neurons in the network, 21 | except for the neurons that located in skippable layers. 22 | 23 | Args: 24 | k: A positive integer. The number of neurons to select. 25 | 26 | Returns: 27 | A list of location of the selected neurons. 28 | ''' 29 | 30 | # Choose k neurons and return their location. 31 | 32 | indices = [ 33 | np.random.choice(self.num_neurons, size=k, replace=False) 34 | for _ in range(self.num_input) 35 | ] 36 | # indices = np.concatenate(indices, axis=0) 37 | return indices 38 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/examples/finetune.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | iter=30000 5 | lr=1e-3 6 | wd=1e-4 7 | mmt=0 8 | 9 | DATASETS=(MIT_67 CUB_200_2011 Flower_102 stanford_40 stanford_dog) 10 | DATASET_NAMES=(MIT67Data CUB200Data Flower102Data Stanford40Data SDog120Data) 11 | DATASET_ABBRS=(mit67 cub200 flower102 stanford40 sdog120) 12 | 13 | for i in 0 14 | do 15 | DATASET=${DATASETS[i]} 16 | DATASET_NAME=${DATASET_NAMES[i]} 17 | DATASET_ABBR=${DATASET_ABBRS[i]} 18 | 19 | DIR=results/baseline/finetune 20 | NAME=resnet18_${DATASET_ABBR} 21 | 22 | CUDA_VISIBLE_DEVICES=$1 python -u finetune.py --iterations ${iter} --datapath data/${DATASET}/ --dataset ${DATASET_NAME} --name $NAME --batch_size 64 --lr ${lr} --network resnet18 --weight_decay ${wd} --momentum ${mmt} --output_dir $DIR 23 | 24 | done -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/examples/nc_guided_defect.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | import os.path as osp 3 | import pandas as pd 4 | from pdb import set_trace as st 5 | import numpy as np 6 | np.set_printoptions(precision = 1) 7 | 8 | root = "results/nc_adv_eval_test" 9 | coverages = ["neuron_coverage", "top_k_coverage", "strong_coverage"] 10 | coverage_names = ["NC", "TKNC", "SNAC"] 11 | strategies = ["deepxplore", "dlfuzz", "dlfuzzfirst"] 12 | strategy_names = ["DeepXplore", "DLFuzzRR", "DLFuzz"] 13 | datasets = ["mit67", "stanford40"] 14 | dataset_names = ["Scenes", "Actions"] 15 | methods = ["Finetune", "DELTA", "Magprune", "ReMoS"] 16 | 17 | dataset = "mit67" 18 | 19 | # for idx, dataset in enumerate(datasets): 20 | # dataset_df = pd.DataFrame(coverage_names) 21 | # for strategy_name, strategy in zip(strategy_names, strategies): 22 | # s_dirs = [] 23 | # for coverage in coverages: 24 | # path = osp.join(root, f"{strategy}_{coverage}_{dataset}_resnet18", "result.csv") 25 | # df = pd.read_csv(path, index_col=0) 26 | # dir = np.around(df['dir'].to_numpy(), 1) 27 | # s_dirs.append(dir) 28 | # dataset_df[strategy_name] = s_dirs 29 | # print(f"Results for {dataset_names[idx]}") 30 | # print(dataset_df) 31 | 32 | m_indexes = pd.MultiIndex.from_product([dataset_names, strategy_names, methods], names=["Dataset", "Strategy", "Techniques"]) 33 | result = pd.DataFrame(np.random.randn(3, 6*4), index=coverage_names, columns=m_indexes) 34 | for dataset_name, dataset in zip(dataset_names, datasets): 35 | for strategy_name, strategy in zip(strategy_names, strategies): 36 | s_dirs = [] 37 | for coverage_name, coverage in zip(coverage_names, coverages): 38 | path = osp.join(root, f"{strategy}_{coverage}_{dataset}_resnet18", "result.csv") 39 | df = pd.read_csv(path, index_col=0) 40 | dir = np.around(df['dir'].to_numpy(), 1) 41 | for idx, method in enumerate(methods): 42 | result[(dataset_name,strategy_name, method)][coverage_name] = round(dir[idx],1) 43 | 44 | print(f"Results for Scenes") 45 | print(result["Scenes"]) 46 | print(f"\nResults for Actions") 47 | print(result["Actions"]) -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/examples/nc_guided_defect.sh: -------------------------------------------------------------------------------- 1 | 2 | 3 | #!/bin/bash 4 | export PYTHONPATH=../..:$PYTHONPATH 5 | 6 | iter=30000 7 | id=1 8 | splmda=0 9 | lmda=0 10 | layer=1234 11 | lr=5e-3 12 | wd=1e-4 13 | mmt=0 14 | 15 | DATASETS=(MIT_67 CUB_200_2011 Flower_102 stanford_40 stanford_dog VisDA) 16 | DATASET_NAMES=(MIT67Data CUB200Data Flower102Data Stanford40Data SDog120Data VisDaDATA) 17 | DATASET_ABBRS=(mit67 cub200 flower102 stanford40 sdog120 visda) 18 | 19 | 20 | COVERAGE=strong_coverage 21 | STRATEGY=deepxplore 22 | 23 | # neuron_coverage top_k_coverage strong_coverage 24 | # random deepxplore dlfuzz dlfuzzfirst 25 | 26 | for i in 3 27 | do 28 | 29 | for COVERAGE in neuron_coverage top_k_coverage strong_coverage 30 | do 31 | for STRATEGY in deepxplore dlfuzz dlfuzzfirst 32 | do 33 | 34 | DATASET=${DATASETS[i]} 35 | DATASET_NAME=${DATASET_NAMES[i]} 36 | DATASET_ABBR=${DATASET_ABBRS[i]} 37 | 38 | DIR=results/baseline/finetune 39 | NAME=resnet18_${DATASET_ABBR}_lr${lr}_iter${iter}_feat${lmda}_wd${wd}_mmt${mmt}_${id} 40 | 41 | FINETUNE_DIR=results/res18_ncprune_sum/finetune/resnet18_${DATASET_ABBR}_lr5e-3_iter30000_feat0_wd1e-4_mmt0_1 42 | DELTA_DIR=results/res18_ncprune_sum/delta/resnet18_${DATASET_ABBR}_lr1e-2_iter10000_feat5e-1_wd1e-4_mmt0_1 43 | WEIGHT_DIR=results/res18_ncprune_sum/weight/resnet18_${DATASET_ABBR}_total0.8_init0.8_per0.1_int10000_lr5e-3_iter10000_feat0_wd1e-4_mmt0_1 44 | NCPRUNE_DIR=results/res18_ncprune_sum/ncprune/resnet18_${DATASET_ABBR}_do_total0.1_trainall_lr5e-3_iter30000_feat0_wd1e-4_mmt0_1 45 | 46 | CUDA_VISIBLE_DEVICES=$1 python -u DNNtest/eval_nc.py --datapath data/${DATASET}/ --dataset ${DATASET_NAME} --name $NAME --network resnet18 --finetune_ckpt $FINETUNE_DIR/ckpt.pth --delta_ckpt $DELTA_DIR/ckpt.pth --weight_ckpt $WEIGHT_DIR/ckpt.pth --ncprune_ckpt $NCPRUNE_DIR/ckpt.pth --output_dir results/nc_adv_eval_test/${STRATEGY}_${COVERAGE}_${DATASET_ABBR}_resnet18 --batch_size 32 --coverage $COVERAGE --strategy $STRATEGY 47 | 48 | done 49 | done 50 | done -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/examples/nc_profile.sh: -------------------------------------------------------------------------------- 1 | 2 | 3 | #!/bin/bash 4 | export PYTHONPATH=../..:$PYTHONPATH 5 | 6 | DATASETS=(MIT_67 CUB_200_2011 Flower_102 stanford_40 stanford_dog VisDA) 7 | DATASET_NAMES=(MIT67Data CUB200Data Flower102Data Stanford40Data SDog120Data VisDaDATA) 8 | DATASET_ABBRS=(mit67 cub200 flower102 stanford40 sdog120 visda) 9 | 10 | # coverages: neuron_coverage top_k_coverage strong_coverage 11 | # strategies: random deepxplore dlfuzz dlfuzzfirst 12 | 13 | for MODEL in resnet18 14 | do 15 | for dataset_idx in 0 16 | do 17 | for COVERAGE in neuron_coverage 18 | do 19 | for STRATEGY in deepxplore 20 | do 21 | 22 | 23 | DATASET=${DATASETS[dataset_idx]} 24 | DATASET_NAME=${DATASET_NAMES[dataset_idx]} 25 | DATASET_ABBR=${DATASET_ABBRS[dataset_idx]} 26 | NAME=${MODEL}_${DATASET_ABBR}_lr${lr}_iter${iter}_feat${lmda}_wd${wd}_mmt${mmt}_${id} 27 | 28 | CUDA_VISIBLE_DEVICES=$1 python -u nc_prune/my_profile.py --datapath data/${DATASET}/ --dataset ${DATASET_NAME} --name $NAME --network $MODEL --output_dir results/nc_profiling/${COVERAGE}_${DATASET_ABBR}_${MODEL} --batch_size 32 --coverage $COVERAGE --strategy $STRATEGY 29 | 30 | done 31 | done 32 | done 33 | done -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/examples/remos.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export PYTHONPATH=../..:$PYTHONPATH 3 | 4 | iter=30000 5 | lr=5e-3 6 | wd=1e-4 7 | mmt=0 8 | 9 | DATASETS=(MIT_67 CUB_200_2011 Flower_102 stanford_40 stanford_dog) 10 | DATASET_NAMES=(MIT67Data CUB200Data Flower102Data Stanford40Data SDog120Data) 11 | DATASET_ABBRS=(mit67 cub200 flower102 stanford40 sdog120) 12 | 13 | 14 | for i in 0 15 | do 16 | for ratio in 0.1 17 | do 18 | for COVERAGE in neuron_coverage 19 | do 20 | 21 | total_ratio=${ratio} 22 | 23 | DATASET=${DATASETS[i]} 24 | DATASET_NAME=${DATASET_NAMES[i]} 25 | DATASET_ABBR=${DATASET_ABBRS[i]} 26 | 27 | NAME=resnet18_${DATASET_ABBR}_total${total_ratio}_lr${lr}_iter${iter}_wd${wd}_mmt${mmt} 28 | DIR=results/remos_$COVERAGE 29 | 30 | CUDA_VISIBLE_DEVICES=$1 python finetune.py --iterations ${iter} --datapath data/${DATASET}/ --dataset ${DATASET_NAME} --name ${NAME} --batch_size 64 --lr ${lr} --network resnet18 --weight_decay ${wd} --momentum ${mmt} --output_dir ${DIR} --method remos --weight_total_ratio $total_ratio --weight_init_prune_ratio $total_ratio --prune_interval $iter --weight_ratio_per_prune 0 --nc_info_dir results/nc_profiling/${COVERAGE}_${DATASET_ABBR}_resnet18 31 | 32 | done 33 | done 34 | done -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/model/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/ReMoS/CV_adv/model/__init__.py -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/nc_prune/coverage/test_max.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from pdb import set_trace as st 3 | 4 | 5 | def get_max_values(array, k): 6 | indexs = tuple() 7 | while k > 0: 8 | idx = np.argwhere(array == array.max()) 9 | idx = np.ascontiguousarray(idx).reshape(-1) 10 | # indexs.append(idx) 11 | indexs += (idx,) 12 | 13 | array[idx] = -np.inf 14 | 15 | print(idx.shape) 16 | k -= len(idx) 17 | 18 | indexs = np.concatenate(indexs) 19 | # return indexs 20 | st() 21 | 22 | 23 | a = np.array([5,12,32,659,-4,5,23,659]).astype(np.float32) 24 | 25 | np.sort(a) 26 | st() 27 | get_max_values(a, 4) -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/__init__.py -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/common.py: -------------------------------------------------------------------------------- 1 | """ 2 | Provides some useful functions. 3 | """ 4 | 5 | from __future__ import absolute_import 6 | 7 | import os 8 | import time 9 | 10 | import numpy as np 11 | 12 | 13 | def readable_time_str(): 14 | """Get readable time string based on current local time. 15 | 16 | The time string will be formatted as %Y-%m-%d %H:%M:%S. 17 | 18 | Returns 19 | ------- 20 | str 21 | Readable time string. 22 | 23 | """ 24 | return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 25 | 26 | 27 | def user_home_dir(): 28 | """Get path of user home directory. 29 | 30 | Returns 31 | ------- 32 | str 33 | Path of user home directory. 34 | 35 | """ 36 | return os.path.expanduser("~") 37 | 38 | 39 | def to_numpy(data): 40 | """Convert other data type to numpy. If the data itself 41 | is numpy type, then a copy will be made and returned. 42 | 43 | Returns 44 | ------- 45 | numpy.array 46 | Numpy array of passed data. 47 | 48 | """ 49 | if 'mxnet' in str(type(data)): 50 | data = data.asnumpy() 51 | elif 'torch' in str(type(data)): 52 | data = data.cpu().numpy() 53 | elif 'numpy' in str(type(data)): 54 | data = np.copy(data) 55 | return data 56 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/backdoor.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | import os.path as osp 3 | import pandas as pd 4 | from pdb import set_trace as st 5 | import numpy as np 6 | 7 | np.set_printoptions(precision = 1) 8 | 9 | root = "remos" 10 | methods = ["finetune", "weight", "retrain", "renofeation", "remos"] 11 | method_names = ["Finetune", "Magprune", "Retrain", "Renofeation", "ReMoS"] 12 | datasets = ["mit67", "cub200", "stanford40"] 13 | dataset_names = ["Scenes", "Birds", "Actions"] 14 | 15 | m_indexes = pd.MultiIndex.from_product([dataset_names, method_names], names=["Dataset", "Techniques"]) 16 | result = pd.DataFrame(np.random.randn(2, 15), index=["Acc", "DIR"], columns=m_indexes) 17 | for dataset_name, dataset in zip(dataset_names, datasets): 18 | for method_name, method in zip(method_names, methods): 19 | path = osp.join(root, method, dataset, "test.tsv") 20 | with open(path) as f: 21 | lines = f.readlines() 22 | acc = float(lines[-1].split()[-1]) 23 | dir = float(lines[-7].split()[-1]) 24 | result[(dataset_name, method_name)]["Acc"] = acc 25 | result[(dataset_name, method_name)]["DIR"] = dir 26 | print(result) 27 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/eval_backdoor.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | export PYTHONPATH=../..:$PYTHONPATH 3 | 4 | 5 | DATASETS=(MIT_67 CUB_200_2011 stanford_40 ) 6 | DATASET_NAMES=(MIT67Data CUB200Data Stanford40Data ) 7 | DATASET_ABBRS=(mit67 cub200 stanford40 ) 8 | 9 | for METHOD in finetune 10 | do 11 | for i in 0 12 | do 13 | 14 | DATASET=${DATASETS[i]} 15 | DATASET_NAME=${DATASET_NAMES[i]} 16 | DATASET_ABBR=${DATASET_ABBRS[i]} 17 | 18 | python eval.py --datapath ../CV_adv/data/${DATASET}/ --dataset ${DATASET_NAME} --network resnet50 --output_dir remos/$METHOD/$DATASET_ABBR/ --is_poison --fixed_pic 19 | 20 | done 21 | done -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/r50_baseline.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | iter=10000 4 | mmt=0 5 | 6 | DATASETS=(MIT_67 CUB_200_2011 stanford_40 ) 7 | DATASET_NAMES=(MIT67Data CUB200Data Stanford40Data) 8 | DATASET_ABBRS=(mit67 cub200 stanford40) 9 | LEARNING_RATE=5e-3 10 | WEIGHT_DECAY=1e-4 11 | portion=0.2 12 | 13 | for i in 0 14 | do 15 | 16 | DATASET=${DATASETS[i]} 17 | DATASET_NAME=${DATASET_NAMES[i]} 18 | DATASET_ABBR=${DATASET_ABBRS[i]} 19 | lr=${LEARNING_RATE} 20 | wd=${WEIGHT_DECAY} 21 | 22 | NAME=${DATASET_ABBR} 23 | DIR=results/r50_backdoor/finetune/ 24 | 25 | CUDA_VISIBLE_DEVICES=$1 python -u finetune.py --teacher_datapath ../CV_adv/data/${DATASET} --teacher_dataset ${DATASET_NAME} --student_datapath ../CV_adv/data/${DATASET} --student_dataset ${DATASET_NAME} --iterations ${iter} --name ${NAME} --batch_size 64 --lr ${lr} --network resnet50 --weight_decay ${wd} --test_interval $iter --momentum ${mmt} --output_dir ${DIR} --argportion ${portion} --teacher_method backdoor_finetune --fixed_pic --checkpoint results/r50_backdoor/backdoor/res50_${DATASET_ABBR}/teacher_ckpt.pth 26 | 27 | 28 | done 29 | 30 | 31 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/r50_magprune.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | iter=10000 4 | mmt=0 5 | 6 | DATASETS=(MIT_67 CUB_200_2011 stanford_40 ) 7 | DATASET_NAMES=(MIT67Data CUB200Data Stanford40Data) 8 | DATASET_ABBRS=(mit67 cub200 stanford40) 9 | LEARNING_RATE=5e-3 10 | WEIGHT_DECAY=1e-4 11 | portion=0.2 12 | 13 | for i in 0 14 | do 15 | 16 | DATASET=${DATASETS[i]} 17 | DATASET_NAME=${DATASET_NAMES[i]} 18 | DATASET_ABBR=${DATASET_ABBRS[i]} 19 | lr=${LEARNING_RATE} 20 | wd=${WEIGHT_DECAY} 21 | 22 | NAME=${DATASET_ABBR} 23 | DIR=results/r50_backdoor/res50_mag 24 | 25 | CUDA_VISIBLE_DEVICES=$1 python -u finetune.py --teacher_datapath ../CV_adv/data/${DATASET} --teacher_dataset ${DATASET_NAME} --student_datapath ../CV_adv/data/${DATASET} --student_dataset ${DATASET_NAME} --iterations ${iter} --name ${NAME} --batch_size 64 --lr ${lr} --network resnet50 --weight_decay ${wd} --beta 1e-2 --momentum ${mmt} --output_dir ${DIR} --argportion ${portion} --teacher_method backdoor_finetune --checkpoint results/r50_backdoor/backdoor/res50_${DATASET_ABBR}/teacher_ckpt.pth --student_method weight --fixed_pic 26 | 27 | 28 | done 29 | 30 | 31 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/r50_poison.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | iter=30000 4 | mmt=0 5 | 6 | DATASETS=(MIT_67 CUB_200_2011 stanford_40 ) 7 | DATASET_NAMES=(MIT67Data CUB200Data Stanford40Data) 8 | DATASET_ABBRS=(mit67 cub200 stanford40) 9 | LEARNING_RATE=5e-3 10 | WEIGHT_DECAY=1e-4 11 | PORTION=0.2 12 | 13 | for i in 0 14 | do 15 | 16 | DATASET=${DATASETS[i]} 17 | DATASET_NAME=${DATASET_NAMES[i]} 18 | DATASET_ABBR=${DATASET_ABBRS[i]} 19 | lr=${LEARNING_RATE} 20 | wd=${WEIGHT_DECAY} 21 | 22 | NAME=res50_${DATASET_ABBR} 23 | newDIR=results/r50_backdoor/backdoor 24 | 25 | CUDA_VISIBLE_DEVICES=$1 python -u finetune.py --teacher_datapath ../CV_adv/data/${DATASET} --teacher_dataset ${DATASET_NAME} --student_datapath ../CV_adv/data/${DATASET} --student_dataset ${DATASET_NAME} --iterations ${iter} --name ${NAME} --batch_size 64 --lr ${lr} --network resnet50 --weight_decay ${wd} --momentum ${mmt} --output_dir ${newDIR} --argportion ${PORTION} --teacher_method backdoor_finetune --fixed_pic 26 | 27 | 28 | done 29 | 30 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/remos/profile.sh: -------------------------------------------------------------------------------- 1 | 2 | 3 | #!/bin/bash 4 | export PYTHONPATH=../..:$PYTHONPATH 5 | 6 | iter=30000 7 | lr=5e-3 8 | wd=1e-4 9 | mmt=0 10 | 11 | DATASETS=(MIT_67 CUB_200_2011 stanford_40 ) 12 | DATASET_NAMES=(MIT67Data CUB200Data Stanford40Data) 13 | DATASET_ABBRS=(mit67 cub200 stanford40) 14 | 15 | COVERAGE=strong_coverage 16 | STRATEGY=deepxplore 17 | 18 | # neuron_coverage top_k_coverage strong_coverage 19 | # random deepxplore dlfuzz dlfuzzfirst 20 | 21 | for i in 0 22 | do 23 | 24 | for COVERAGE in neuron_coverage 25 | do 26 | 27 | DATASET=${DATASETS[i]} 28 | DATASET_NAME=${DATASET_NAMES[i]} 29 | DATASET_ABBR=${DATASET_ABBRS[i]} 30 | 31 | NAME=resnet18_${DATASET_ABBR}_lr${lr}_iter${iter}_wd${wd}_mmt${mmt} 32 | 33 | CUDA_VISIBLE_DEVICES=$1 \ 34 | python -u remos/my_profile.py \ 35 | --datapath ../CV_adv/data/${DATASET}/ \ 36 | --dataset ${DATASET_NAME} \ 37 | --name $NAME \ 38 | --network resnet50 \ 39 | --output_dir results/nc_profiling/${COVERAGE}_${DATASET_ABBR}_resnet50 \ 40 | --batch_size 32 \ 41 | --coverage $COVERAGE \ 42 | --strategy $STRATEGY \ 43 | --checkpoint results/r50_backdoor/backdoor/res50_${DATASET_ABBR}/teacher_ckpt.pth \ 44 | # & 45 | 46 | 47 | done 48 | done -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/examples/remos/remos.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | iter=10000 4 | mmt=0 5 | 6 | DATASETS=(MIT_67 CUB_200_2011 stanford_40 ) 7 | DATASET_NAMES=(MIT67Data CUB200Data Stanford40Data) 8 | DATASET_ABBRS=(mit67 cub200 stanford40) 9 | 10 | LEARNING_RATE=5e-3 11 | WEIGHT_DECAY=1e-4 12 | 13 | COVERAGE=neuron_coverage 14 | portion=0.2 15 | 16 | for nc_ratio in 0.03 17 | do 18 | for i in 0 19 | do 20 | 21 | 22 | DATASET=${DATASETS[i]} 23 | DATASET_NAME=${DATASET_NAMES[i]} 24 | DATASET_ABBR=${DATASET_ABBRS[i]} 25 | lr=${LEARNING_RATE} 26 | wd=${WEIGHT_DECAY} 27 | 28 | NAME=${DATASET_ABBR} 29 | DIR=results/r50_backdoor/remos_res50 30 | 31 | CUDA_VISIBLE_DEVICES=$1 \ 32 | python -u finetune.py \ 33 | --teacher_datapath ../CV_adv/data/${DATASET} \ 34 | --teacher_dataset ${DATASET_NAME} \ 35 | --student_datapath ../CV_adv/data/${DATASET} \ 36 | --student_dataset ${DATASET_NAME} \ 37 | --iterations ${iter} \ 38 | --name ${NAME} \ 39 | --batch_size 64 \ 40 | --lr ${lr} \ 41 | --network resnet50 \ 42 | --weight_decay ${wd} \ 43 | --test_interval 10000 \ 44 | --adv_test_interval -1 \ 45 | --momentum ${mmt} \ 46 | --output_dir ${DIR} \ 47 | --argportion ${portion} \ 48 | --teacher_method backdoor_finetune \ 49 | --checkpoint results/r50_backdoor/backdoor/res50_${DATASET_ABBR}/teacher_ckpt.pth \ 50 | --student_method nc_weight_rank_prune \ 51 | --fixed_pic \ 52 | --prune_interval 1000000 \ 53 | --weight_total_ratio ${nc_ratio} \ 54 | --weight_ratio_per_prune 0 \ 55 | --weight_init_prune_ratio ${nc_ratio} \ 56 | --dropout 1e-1 \ 57 | --nc_info_dir results/nc_profiling/${COVERAGE}_${DATASET_ABBR}_resnet50 \ 58 | # & 59 | 60 | 61 | done 62 | done 63 | 64 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/model/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/ReMoS/CV_backdoor/model/__init__.py -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/prune.py: -------------------------------------------------------------------------------- 1 | import os 2 | import os.path as osp 3 | import sys 4 | import time 5 | import argparse 6 | from pdb import set_trace as st 7 | import json 8 | import random 9 | 10 | import torch 11 | import numpy as np 12 | import torchvision 13 | import torch.nn as nn 14 | import torch.nn.functional as F 15 | import torch.optim as optim 16 | import torchcontrib 17 | 18 | 19 | def weight_prune( 20 | model, 21 | prune_ratio, 22 | ): 23 | total = 0 24 | for name, module in model.named_modules(): 25 | if isinstance(module, nn.Conv2d): 26 | total += module.weight.data.numel() 27 | 28 | conv_weights = torch.zeros(total).cuda() 29 | index = 0 30 | for name, module in model.named_modules(): 31 | if isinstance(module, nn.Conv2d): 32 | size = module.weight.data.numel() 33 | conv_weights[index:(index + size)] = module.weight.data.view(-1).abs().clone() 34 | index += size 35 | 36 | y, i = torch.sort(conv_weights) 37 | thre_index = int(total * prune_ratio) 38 | thre = y[thre_index] 39 | log = f"Pruning threshold: {thre:.4f}" 40 | print(log) 41 | 42 | pruned = 0 43 | zero_flag = False 44 | for name, module in model.named_modules(): 45 | if isinstance(module, nn.Conv2d): 46 | weight_copy = module.weight.data.abs().clone() 47 | mask = weight_copy.gt(thre).float() 48 | 49 | pruned = pruned + mask.numel() - torch.sum(mask) 50 | # Save the update mask to the module 51 | module.mask = mask 52 | if int(torch.sum(mask)) == 0: 53 | zero_flag = True 54 | remain_ratio = int(torch.sum(mask)) / mask.numel() 55 | log = (f"layer {name} \t total params: {mask.numel()} \t " 56 | f"not update params: {int(torch.sum(mask))}({remain_ratio:.2f})") 57 | print(log) 58 | 59 | if zero_flag: 60 | raise RuntimeError("There exists a layer with 0 parameters left.") 61 | log = (f"Total conv params: {total}, not update conv params: {pruned}, " 62 | f"not update ratio: {pruned / total:.2f}") 63 | print(log) 64 | return model 65 | -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/remos/coverage/test_max.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from pdb import set_trace as st 3 | 4 | 5 | def get_max_values(array, k): 6 | indexs = tuple() 7 | while k > 0: 8 | idx = np.argwhere(array == array.max()) 9 | idx = np.ascontiguousarray(idx).reshape(-1) 10 | # indexs.append(idx) 11 | indexs += (idx,) 12 | 13 | array[idx] = -np.inf 14 | 15 | print(idx.shape) 16 | k -= len(idx) 17 | 18 | indexs = np.concatenate(indexs) 19 | # return indexs 20 | st() 21 | 22 | 23 | a = np.array([5,12,32,659,-4,5,23,659]).astype(np.float32) 24 | 25 | np.sort(a) 26 | st() 27 | get_max_values(a, 4) -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/remos/coverage/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/ReMoS/CV_backdoor/remos/coverage/utils/__init__.py -------------------------------------------------------------------------------- /code/deep/ReMoS/CV_backdoor/remos/coverage/utils/common.py: -------------------------------------------------------------------------------- 1 | """ 2 | Provides some useful functions. 3 | """ 4 | 5 | from __future__ import absolute_import 6 | 7 | import os 8 | import time 9 | 10 | import numpy as np 11 | 12 | 13 | def readable_time_str(): 14 | """Get readable time string based on current local time. 15 | 16 | The time string will be formatted as %Y-%m-%d %H:%M:%S. 17 | 18 | Returns 19 | ------- 20 | str 21 | Readable time string. 22 | 23 | """ 24 | return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) 25 | 26 | 27 | def user_home_dir(): 28 | """Get path of user home directory. 29 | 30 | Returns 31 | ------- 32 | str 33 | Path of user home directory. 34 | 35 | """ 36 | return os.path.expanduser("~") 37 | 38 | 39 | def to_numpy(data): 40 | """Convert other data type to numpy. If the data itself 41 | is numpy type, then a copy will be made and returned. 42 | 43 | Returns 44 | ------- 45 | numpy.array 46 | Numpy array of passed data. 47 | 48 | """ 49 | if 'mxnet' in str(type(data)): 50 | data = data.asnumpy() 51 | elif 'torch' in str(type(data)): 52 | data = data.cpu().numpy() 53 | elif 'numpy' in str(type(data)): 54 | data = np.copy(data) 55 | return data 56 | -------------------------------------------------------------------------------- /code/deep/ReMoS/README.md: -------------------------------------------------------------------------------- 1 | # ReMoS 2 | 3 | > Reducing Defect Inheritance in Transfer Learning via Relevant Model Slicing 4 | 5 | This is the implementation for the ICSE 2022 paper **ReMoS: Reducing Defect Inheritance in Transfer Learning via Relevant Model Slicing**. 6 | 7 | [[Paper](https://jd92.wang/assets/files/icse22-remos.pdf)] [[知乎解读](https://zhuanlan.zhihu.com/p/446453487)] [[Video](https://www.bilibili.com/video/BV1mi4y1C7bP)] 8 | 9 | ## Introduction 10 | 11 | Transfer learning is a popular software reuse technique in the deep learning community that enables developers to build custom models (students) based on sophisticated pretrained models (teachers). However, some defects in the teacher model may also be inherited by students, such as well-known adversarial vulnerabilities and backdoors. We propose ReMoS, a relevant model slicing technique to reduce defect inheritance during transfer learning while retaining useful knowledge from the teacher model. Our experiments on seven DNN defects, four DNN models, and eight datasets demonstrate that ReMoS can reduce inherited defects effectively (by **63% to 86%** for CV tasks and by **40% to 61%** for NLP tasks) and efficiently with minimal sacrifice of accuracy (**3%** on average). 12 | 13 | ## Requirements 14 | 15 | See `requirements.txt`. All you need is a simple Pytorch environments with `advertorch` installed. Just `pip install -r requirements.txt`. 16 | 17 | ## Usage 18 | 19 | See [`instructions.md`](instructions.md). 20 | 21 | ## Citation 22 | 23 | If you think this work is helpful to your research, you can cite it as: 24 | 25 | ``` 26 | @inproceedings{zhang2022remos, 27 | title = {ReMoS: Reducing Defect Inheritance in Transfer Learning via Relevant Model Slicing}, 28 | author = {Zhang, Ziqi and Li, Yuanchun and Wang, Jindong and Liu, Bingyan and Li, Ding and Chen, Xiangqun and Guo, Yao and Liu, Yunxin}, 29 | booktitle = {44th International Conference on Software Engineering (ICSE)}, 30 | year = {2022} 31 | } 32 | ``` 33 | 34 | 35 | -------------------------------------------------------------------------------- /code/deep/ReMoS/unpack_downloads.sh: -------------------------------------------------------------------------------- 1 | mv download_pack/cv_data CV_adv/data 2 | mv download_pack/cv_adv_models/* CV_adv/results/ 3 | mv download_pack/cv_backdoor_models/* CV_backdoor/results/ 4 | -------------------------------------------------------------------------------- /code/deep/TCP/README.md: -------------------------------------------------------------------------------- 1 | # [Accelerating Deep Unsupervised Domain Adaptation with Transfer Channel Pruning](https://arxiv.org/abs/1904.02654) 2 | 3 | This is a PyTorch implementation of the IJCNN 2019 paper. 4 | 5 | ## The contributions of this paper are summarized as follows: 6 | 1. We present TCP as a unified approach for accelerating deep unsupervised domain adaptation models. TCP is a generic, accurate, and efficient compression method that can be easily implemented by most deep learning libraries. 7 | 2. TCP is able to reduce negative transfer by considering the cross-domain distribution discrepancy using the proposed Transfer Channel Evaluation module. 8 | 3. Extensive experiments on two public UDA datasets demonstrate the significant superiority of TCP. 9 | 10 | ## Citation: 11 | If you use this code for your research, please consider citing: 12 | 13 | ``` 14 | @inproceedings{yu2019accelerating, 15 | title={Accelerating Deep Unsupervised Domain Adaptation with Transfer Channel Pruning}, 16 | author={Yu, Chaohui and Wang, Jindong and Chen, Yiqiang and Wu, Zijing}, 17 | booktitle={The IEEE International Joint Conference on Neural Networks (IJCNN)}, 18 | year={2019} 19 | } 20 | ``` 21 | 22 | 23 | -------------------------------------------------------------------------------- /code/deep/TCP/dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.backends.cudnn as cudnn 4 | import torch.nn as nn 5 | import torch.nn.parallel 6 | import torch.optim as optim 7 | import torch.utils.data as data 8 | import torchvision.datasets as datasets 9 | import torchvision.models as models 10 | import torchvision.transforms as transforms 11 | from PIL import Image 12 | import glob 13 | import os 14 | 15 | def loader(path, batch_size=16, num_workers=1, pin_memory=True): 16 | normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 17 | return data.DataLoader( 18 | datasets.ImageFolder(path, 19 | transforms.Compose([ 20 | transforms.Resize(256), 21 | transforms.RandomResizedCrop(224), 22 | transforms.RandomHorizontalFlip(), 23 | transforms.ToTensor(), 24 | normalize, 25 | ])), 26 | batch_size=batch_size, 27 | shuffle=True, 28 | num_workers=num_workers, 29 | pin_memory=pin_memory) 30 | 31 | def test_loader(path, batch_size=16, num_workers=1, pin_memory=True): 32 | normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) 33 | return data.DataLoader( 34 | datasets.ImageFolder(path, 35 | transforms.Compose([ 36 | transforms.Resize(256), 37 | transforms.CenterCrop(224), 38 | transforms.ToTensor(), 39 | normalize, 40 | ])), 41 | batch_size=batch_size, 42 | shuffle=False, 43 | num_workers=num_workers, 44 | pin_memory=pin_memory) 45 | -------------------------------------------------------------------------------- /code/deep/adarnn/base/__init__.py: -------------------------------------------------------------------------------- 1 | from base.loss_transfer import TransferLoss 2 | # from base.GRUGATE_ALL_HIDDEN import GRUGATE_ALL_HIDDEN 3 | __all__ = [ 4 | 'MLP', 5 | 'TransferNet', 6 | 'TransferLoss', 7 | 'BaseTrainer', 8 | 'Multitask', 9 | 'AdaRNN', 10 | 'GRUGATE_ALL_HIDDEN' 11 | 'MultitaskGRU', 12 | ] 13 | -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/__init__.py: -------------------------------------------------------------------------------- 1 | from base.loss.adv_loss import adv 2 | from base.loss.coral import CORAL 3 | from base.loss.cos import cosine 4 | from base.loss.kl_js import kl_div, js 5 | from base.loss.mmd import MMD_loss 6 | from base.loss.mutual_info import Mine 7 | from base.loss.pair_dist import pairwise_dist 8 | 9 | __all__ = [ 10 | 'adv', 11 | 'CORAL', 12 | 'cosine', 13 | 'kl_div', 14 | 'js' 15 | 'MMD_loss', 16 | 'Mine', 17 | 'pairwise_dist' 18 | ] -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/adv_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import math 4 | import numpy as np 5 | import torch.nn.functional as F 6 | from torch.autograd import Function 7 | 8 | class ReverseLayerF(Function): 9 | 10 | @staticmethod 11 | def forward(ctx, x, alpha): 12 | ctx.alpha = alpha 13 | return x.view_as(x) 14 | 15 | @staticmethod 16 | def backward(ctx, grad_output): 17 | output = grad_output.neg() * ctx.alpha 18 | return output, None 19 | 20 | 21 | class Discriminator(nn.Module): 22 | def __init__(self, input_dim=256, hidden_dim=256): 23 | super(Discriminator, self).__init__() 24 | self.input_dim = input_dim 25 | self.hidden_dim = hidden_dim 26 | self.dis1 = nn.Linear(input_dim, hidden_dim) 27 | self.dis2 = nn.Linear(hidden_dim, 1) 28 | 29 | def forward(self, x): 30 | x = F.relu(self.dis1(x)) 31 | x = self.dis2(x) 32 | x = torch.sigmoid(x) 33 | return x 34 | 35 | 36 | def adv(source, target, input_dim=256, hidden_dim=512): 37 | domain_loss = nn.BCELoss() 38 | # !!! Pay attention to .cuda !!! 39 | adv_net = Discriminator(input_dim, hidden_dim).cuda() 40 | domain_src = torch.ones(len(source)).cuda() 41 | domain_tar = torch.zeros(len(target)).cuda() 42 | domain_src, domain_tar = domain_src.view(domain_src.shape[0], 1), domain_tar.view(domain_tar.shape[0], 1) 43 | reverse_src = ReverseLayerF.apply(source, 1) 44 | reverse_tar = ReverseLayerF.apply(target, 1) 45 | pred_src = adv_net(reverse_src) 46 | pred_tar = adv_net(reverse_tar) 47 | loss_s, loss_t = domain_loss(pred_src, domain_src), domain_loss(pred_tar, domain_tar) 48 | loss = loss_s + loss_t 49 | return loss -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/coral.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | def CORAL(source, target): 4 | d = source.size(1) 5 | ns, nt = source.size(0), target.size(0) 6 | 7 | # source covariance 8 | tmp_s = torch.ones((1, ns)).cuda() @ source 9 | cs = (source.t() @ source - (tmp_s.t() @ tmp_s) / ns) / (ns - 1) 10 | 11 | # target covariance 12 | tmp_t = torch.ones((1, nt)).cuda() @ target 13 | ct = (target.t() @ target - (tmp_t.t() @ tmp_t) / nt) / (nt - 1) 14 | 15 | # frobenius norm 16 | loss = (cs - ct).pow(2).sum() 17 | loss = loss / (4 * d * d) 18 | 19 | return loss -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/cos.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | def cosine(source, target): 4 | source, target = source.mean(0), target.mean(0) 5 | cos = nn.CosineSimilarity(dim=0) 6 | loss = cos(source, target) 7 | return loss.mean() -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/kl_js.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | def kl_div(source, target): 4 | if len(source) < len(target): 5 | target = target[:len(source)] 6 | elif len(source) > len(target): 7 | source = source[:len(target)] 8 | criterion = nn.KLDivLoss(reduction='batchmean') 9 | loss = criterion(source.log(), target) 10 | return loss 11 | 12 | 13 | def js(source, target): 14 | if len(source) < len(target): 15 | target = target[:len(source)] 16 | elif len(source) > len(target): 17 | source = source[:len(target)] 18 | M = .5 * (source + target) 19 | loss_1, loss_2 = kl_div(source, M), kl_div(target, M) 20 | return .5 * (loss_1 + loss_2) -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/mutual_info.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | class Mine_estimator(nn.Module): 6 | def __init__(self, input_dim=2048, hidden_dim=512): 7 | super(Mine_estimator, self).__init__() 8 | self.mine_model = Mine(input_dim, hidden_dim) 9 | 10 | def forward(self, X, Y): 11 | Y_shffle = Y[torch.randperm(len(Y))] 12 | loss_joint = self.mine_model(X, Y) 13 | loss_marginal = self.mine_model(X, Y_shffle) 14 | ret = torch.mean(loss_joint) - \ 15 | torch.log(torch.mean(torch.exp(loss_marginal))) 16 | loss = -ret 17 | return loss 18 | 19 | 20 | class Mine(nn.Module): 21 | def __init__(self, input_dim=2048, hidden_dim=512): 22 | super(Mine, self).__init__() 23 | self.fc1_x = nn.Linear(input_dim, hidden_dim) 24 | self.fc1_y = nn.Linear(input_dim, hidden_dim) 25 | self.fc2 = nn.Linear(hidden_dim, 1) 26 | 27 | def forward(self, x, y): 28 | h1 = F.leaky_relu(self.fc1_x(x)+self.fc1_y(y)) 29 | h2 = self.fc2(h1) 30 | return h2 -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss/pair_dist.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | 4 | 5 | def pairwise_dist(X, Y): 6 | n, d = X.shape 7 | m, _ = Y.shape 8 | assert d == Y.shape[1] 9 | a = X.unsqueeze(1).expand(n, m, d) 10 | b = Y.unsqueeze(0).expand(n, m, d) 11 | return torch.pow(a - b, 2).sum(2) 12 | 13 | 14 | def pairwise_dist_np(X, Y): 15 | n, d = X.shape 16 | m, _ = Y.shape 17 | assert d == Y.shape[1] 18 | a = np.expand_dims(X, 1) 19 | b = np.expand_dims(Y, 0) 20 | a = np.tile(a, (1, m, 1)) 21 | b = np.tile(b, (n, 1, 1)) 22 | return np.power(a - b, 2).sum(2) 23 | 24 | def pa(X, Y): 25 | XY = np.dot(X, Y.T) 26 | XX = np.sum(np.square(X), axis=1) 27 | XX = np.transpose([XX]) 28 | YY = np.sum(np.square(Y), axis=1) 29 | dist = XX + YY - 2 * XY 30 | 31 | return dist 32 | 33 | 34 | if __name__ == '__main__': 35 | import sys 36 | args = sys.argv 37 | data = args[0] 38 | print(data) 39 | 40 | # a = torch.arange(1, 7).view(2, 3) 41 | # b = torch.arange(12, 21).view(3, 3) 42 | # print(pairwise_dist(a, b)) 43 | 44 | # a = np.arange(1, 7).reshape((2, 3)) 45 | # b = np.arange(12, 21).reshape((3, 3)) 46 | # print(pa(a, b)) -------------------------------------------------------------------------------- /code/deep/adarnn/base/loss_transfer.py: -------------------------------------------------------------------------------- 1 | from base.loss import adv_loss, CORAL, kl_js, mmd, mutual_info, cosine, pairwise_dist 2 | 3 | 4 | class TransferLoss(object): 5 | def __init__(self, loss_type='cosine', input_dim=512): 6 | """ 7 | Supported loss_type: mmd(mmd_lin), mmd_rbf, coral, cosine, kl, js, mine, adv 8 | """ 9 | self.loss_type = loss_type 10 | self.input_dim = input_dim 11 | 12 | def compute(self, X, Y): 13 | """Compute adaptation loss 14 | 15 | Arguments: 16 | X {tensor} -- source matrix 17 | Y {tensor} -- target matrix 18 | 19 | Returns: 20 | [tensor] -- transfer loss 21 | """ 22 | if self.loss_type == 'mmd_lin' or self.loss_type =='mmd': 23 | mmdloss = mmd.MMD_loss(kernel_type='linear') 24 | loss = mmdloss(X, Y) 25 | elif self.loss_type == 'coral': 26 | loss = CORAL(X, Y) 27 | elif self.loss_type == 'cosine' or self.loss_type == 'cos': 28 | loss = 1 - cosine(X, Y) 29 | elif self.loss_type == 'kl': 30 | loss = kl_js.kl_div(X, Y) 31 | elif self.loss_type == 'js': 32 | loss = kl_js.js(X, Y) 33 | elif self.loss_type == 'mine': 34 | mine_model = mutual_info.Mine_estimator( 35 | input_dim=self.input_dim, hidden_dim=60).cuda() 36 | loss = mine_model(X, Y) 37 | elif self.loss_type == 'adv': 38 | loss = adv_loss.adv(X, Y, input_dim=self.input_dim, hidden_dim=32) 39 | elif self.loss_type == 'mmd_rbf': 40 | mmdloss = mmd.MMD_loss(kernel_type='rbf') 41 | loss = mmdloss(X, Y) 42 | elif self.loss_type == 'pairwise': 43 | pair_mat = pairwise_dist(X, Y) 44 | import torch 45 | loss = torch.norm(pair_mat) 46 | 47 | return loss 48 | 49 | if __name__ == "__main__": 50 | import torch 51 | trans_loss = TransferLoss('adv') 52 | a = (torch.randn(5,512) * 10).cuda() 53 | b = (torch.randn(5,512) * 10).cuda() 54 | print(trans_loss.compute(a, b)) 55 | -------------------------------------------------------------------------------- /code/deep/adarnn/dataset/__init__.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /code/deep/adarnn/requirements.txt: -------------------------------------------------------------------------------- 1 | pretty_errors==1.2.18 2 | visdom==0.1.8.9 3 | matplotlib==3.3.1 4 | numpy==1.22.0 5 | torchvision==0.7.0 6 | torch==1.6.0 7 | tqdm==4.48.2 8 | pandas==1.1.1 9 | scikit_learn==0.23.2 10 | -------------------------------------------------------------------------------- /code/deep/adarnn/tst/__init__.py: -------------------------------------------------------------------------------- 1 | from .transformer import Transformer -------------------------------------------------------------------------------- /code/deep/adarnn/tst/loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | 5 | class OZELoss(nn.Module): 6 | """Custom loss for TRNSys metamodel. 7 | 8 | Compute, for temperature and consumptions, the intergral of the squared differences 9 | over time. Sum the log with a coeficient ``alpha``. 10 | 11 | .. math:: 12 | \Delta_T = \sqrt{\int (y_{est}^T - y^T)^2} 13 | 14 | \Delta_Q = \sqrt{\int (y_{est}^Q - y^Q)^2} 15 | 16 | loss = log(1 + \Delta_T) + \\alpha \cdot log(1 + \Delta_Q) 17 | 18 | Parameters: 19 | ----------- 20 | alpha: 21 | Coefficient for consumption. Default is ``0.3``. 22 | """ 23 | 24 | def __init__(self, reduction: str = 'mean', alpha: float = 0.3): 25 | super().__init__() 26 | 27 | self.alpha = alpha 28 | self.reduction = reduction 29 | 30 | self.base_loss = nn.MSELoss(reduction=self.reduction) 31 | 32 | def forward(self, 33 | y_true: torch.Tensor, 34 | y_pred: torch.Tensor) -> torch.Tensor: 35 | """Compute the loss between a target value and a prediction. 36 | 37 | Parameters 38 | ---------- 39 | y_true: 40 | Target value. 41 | y_pred: 42 | Estimated value. 43 | 44 | Returns 45 | ------- 46 | Loss as a tensor with gradient attached. 47 | """ 48 | delta_Q = self.base_loss(y_pred[..., :-1], y_true[..., :-1]) 49 | delta_T = self.base_loss(y_pred[..., -1], y_true[..., -1]) 50 | 51 | if self.reduction == 'none': 52 | delta_Q = delta_Q.mean(dim=(1, 2)) 53 | delta_T = delta_T.mean(dim=(1)) 54 | 55 | return torch.log(1 + delta_T) + self.alpha * torch.log(1 + delta_Q) 56 | -------------------------------------------------------------------------------- /code/deep/adarnn/tst/positionwiseFeedForward.py: -------------------------------------------------------------------------------- 1 | from typing import Optional 2 | 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | 7 | 8 | class PositionwiseFeedForward(nn.Module): 9 | """Position-wise Feed Forward Network block from Attention is All You Need. 10 | 11 | Apply two linear transformations to each input, separately but indetically. We 12 | implement them as 1D convolutions. Input and output have a shape (batch_size, d_model). 13 | 14 | Parameters 15 | ---------- 16 | d_model: 17 | Dimension of input tensor. 18 | d_ff: 19 | Dimension of hidden layer, default is 2048. 20 | """ 21 | 22 | def __init__(self, 23 | d_model: int, 24 | d_ff: Optional[int] = 2048): 25 | """Initialize the PFF block.""" 26 | super().__init__() 27 | 28 | self._linear1 = nn.Linear(d_model, d_ff) 29 | self._linear2 = nn.Linear(d_ff, d_model) 30 | 31 | def forward(self, x: torch.Tensor) -> torch.Tensor: 32 | """Propagate forward the input through the PFF block. 33 | 34 | Apply the first linear transformation, then a relu actvation, 35 | and the second linear transformation. 36 | 37 | Parameters 38 | ---------- 39 | x: 40 | Input tensor with shape (batch_size, K, d_model). 41 | 42 | Returns 43 | ------- 44 | Output tensor with shape (batch_size, K, d_model). 45 | """ 46 | return self._linear2(F.relu(self._linear1(x))) 47 | -------------------------------------------------------------------------------- /code/deep/adarnn/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from utils import utils 2 | from utils.visualize import Visualize 3 | from utils import metrics 4 | from utils import heat_map 5 | 6 | __all__ = { 7 | 'utils', 8 | 'Visualize', 9 | 'metrics', 10 | 'heat_map' 11 | } -------------------------------------------------------------------------------- /code/deep/adarnn/utils/heat_map.py: -------------------------------------------------------------------------------- 1 | import random 2 | from matplotlib import pyplot as plt 3 | from matplotlib import cm 4 | from matplotlib import axes 5 | from matplotlib.font_manager import FontProperties 6 | 7 | 8 | def draw_heatmap(data, filename): 9 | 10 | 11 | #作图阶段 12 | fig = plt.figure() 13 | #定义画布为1*1个划分,并在第1个位置上进行作图 14 | ax = fig.add_subplot(111) 15 | #作图并选择热图的颜色填充风格,这里选择hot 16 | im = ax.imshow(data, cmap=plt.cm.hot_r) 17 | #增加右侧的颜色刻度条 18 | plt.colorbar(im) 19 | #增加标题 20 | plt.title("weight heatmap") 21 | #show 22 | plt.show() 23 | plt.savefig(filename) 24 | 25 | # d = draw() -------------------------------------------------------------------------------- /code/deep/adarnn/utils/visualize.py: -------------------------------------------------------------------------------- 1 | # A wrapper of Visdom for visualization 2 | 3 | import visdom 4 | import numpy as np 5 | import time 6 | 7 | class Visualize(object): 8 | def __init__(self, port=8097, env='env'): 9 | self.port = port 10 | self.env = env 11 | self.vis = visdom.Visdom(port=self.port, env=self.env) 12 | 13 | def plot_line(self, Y, global_step, title='title', legend=['legend']): 14 | """ Plot line 15 | Inputs: 16 | Y (list): values to plot, a list 17 | global_step (int): global step 18 | """ 19 | y = np.array(Y).reshape((1, len(Y))) 20 | self.vis.line( 21 | Y = y, 22 | X = np.array([global_step]), 23 | win = title, 24 | opts = dict( 25 | title = title, 26 | height = 360, 27 | width = 400, 28 | legend = legend, 29 | ), 30 | update = 'new' if global_step==0 else 'append' 31 | ) 32 | 33 | def heat_map(self, X, title='title'): 34 | self.vis.heatmap( 35 | X = X, 36 | win = title, 37 | opts=dict( 38 | title = title, 39 | width = 360, 40 | height = 400, 41 | ) 42 | ) 43 | 44 | def log(self, info, title='log_text'): 45 | """ 46 | self.log({'loss':1, 'lr':0.0001}) 47 | """ 48 | 49 | log_text = ('[{time}] {info} <br>'.format( 50 | time=time.strftime('%m%d_%H%M%S'),\ 51 | info=info)) 52 | self.vis.text(log_text, title, append=True) 53 | 54 | 55 | if __name__ == '__main__': 56 | vvv = visdom.Visdom(port=8097, env='test') 57 | def test(): 58 | vis = Visualize(env='test') 59 | import time 60 | for i in range(10): 61 | y = np.random.rand(1, 2) 62 | title = 'Two values' 63 | legend = ['value 1', 'value 2'] 64 | vis.plot_line([y[0,0], y[0,1]], i, title, legend) 65 | vvv.line(Y=np.array([y[0,0], y[0,1]]).reshape((1,2)), X=np.array([i]),win='test2', update='append') 66 | time.sleep(2) 67 | test() -------------------------------------------------------------------------------- /code/deep/finetune_AlexNet_ResNet/data/readme.txt: -------------------------------------------------------------------------------- 1 | Download the Office-31 dataset (raw images) and extract it into this directory. 2 | This directory should look like: 3 | data 4 | --OFFICE31 5 | ----amazon 6 | ------class1 7 | ------class2 8 | ... 9 | ----webcam 10 | ------(same as amazon) 11 | ----dslr 12 | ------(same as amazon) -------------------------------------------------------------------------------- /code/deep/fixed/alg/alg.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from alg.algs.Fixed import Fixed 3 | 4 | ALGORITHMS = [ 5 | 'Fixed', 6 | ] 7 | 8 | 9 | def get_algorithm_class(algorithm_name): 10 | """Return the algorithm class with the given name.""" 11 | if algorithm_name not in globals(): 12 | raise NotImplementedError( 13 | "Algorithm not found: {}".format(algorithm_name)) 14 | return globals()[algorithm_name] 15 | -------------------------------------------------------------------------------- /code/deep/fixed/alg/algs/base.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | 4 | 5 | class Algorithm(torch.nn.Module): 6 | def __init__(self, args): 7 | super(Algorithm, self).__init__() 8 | 9 | def update(self, minibatches): 10 | raise NotImplementedError 11 | 12 | def predict(self, x): 13 | raise NotImplementedError 14 | -------------------------------------------------------------------------------- /code/deep/fixed/alg/modelopera.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | from network import act_network 4 | 5 | 6 | def get_fea(args): 7 | net = act_network.ActNetwork(args.dataset) 8 | return net 9 | 10 | 11 | def accuracy(network, loader, weights, usedpredict='p'): 12 | correct = 0 13 | total = 0 14 | weights_offset = 0 15 | 16 | network.eval() 17 | with torch.no_grad(): 18 | for data in loader: 19 | x = data[0].cuda().float() 20 | y = data[1].cuda().long() 21 | if usedpredict == 'p': 22 | p = network.predict(x) 23 | else: 24 | p = network.predict1(x) 25 | if weights is None: 26 | batch_weights = torch.ones(len(x)) 27 | else: 28 | batch_weights = weights[weights_offset: 29 | weights_offset + len(x)] 30 | weights_offset += len(x) 31 | batch_weights = batch_weights.cuda() 32 | if p.size(1) == 1: 33 | correct += (p.gt(0).eq(y).float() * 34 | batch_weights.view(-1, 1)).sum().item() 35 | else: 36 | correct += (p.argmax(1).eq(y).float() * 37 | batch_weights).sum().item() 38 | total += batch_weights.sum().item() 39 | network.train() 40 | 41 | return correct / total 42 | -------------------------------------------------------------------------------- /code/deep/fixed/alg/opt.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | 4 | 5 | def get_params(alg, args): 6 | if args.schuse: 7 | if args.schusech == 'cos': 8 | init_lr = args.lr 9 | else: 10 | init_lr = 1.0 11 | else: 12 | init_lr = args.lr 13 | params = [ 14 | {'params': alg.featurizer.parameters(), 'lr': args.lr_decay1 * init_lr}, 15 | {'params': alg.bottleneck.parameters(), 'lr': args.lr_decay2 * init_lr}, 16 | {'params': alg.classifier.parameters(), 'lr': args.lr_decay2 * init_lr} 17 | ] 18 | params.append({'params': alg.discriminator.parameters(), 19 | 'lr': args.lr_decay2 * init_lr}) 20 | return params 21 | 22 | 23 | def get_optimizer(alg, args): 24 | params = get_params(alg, args) 25 | optimizer = torch.optim.Adam( 26 | params, lr=args.lr, weight_decay=args.weight_decay, betas=(args.beta1, 0.9)) 27 | return optimizer 28 | 29 | 30 | def get_scheduler(optimizer, args): 31 | return None 32 | -------------------------------------------------------------------------------- /code/deep/fixed/datautil/actdata/cross_people.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | from datautil.actdata.util import * 4 | from datautil.util import mydataset, Nmax 5 | import numpy as np 6 | 7 | 8 | class ActList(mydataset): 9 | def __init__(self, args, dataset, root_dir, people_group, group_num, transform=None, target_transform=None): 10 | super(ActList, self).__init__(args) 11 | self.domain_num = 0 12 | self.dataset = dataset 13 | self.task = 'cross_people' 14 | self.transform = transform 15 | self.target_transform = target_transform 16 | x, cy, py, sy = loaddata_from_numpy(self.dataset, root_dir) 17 | self.people_group = people_group 18 | self.position = np.sort(np.unique(sy)) 19 | self.comb_position(x, cy, py, sy) 20 | self.x = self.x[:, :, np.newaxis, :] 21 | self.transform = None 22 | self.x = torch.tensor(self.x).float() 23 | self.dlabels = np.ones(self.labels.shape) * \ 24 | (group_num-Nmax(args, group_num)) 25 | 26 | def comb_position(self, x, cy, py, sy): 27 | for i, peo in enumerate(self.people_group): 28 | index = np.where(py == peo)[0] 29 | tx, tcy, tsy = x[index], cy[index], sy[index] 30 | for j, sen in enumerate(self.position): 31 | index = np.where(tsy == sen)[0] 32 | if j == 0: 33 | ttx, ttcy = tx[index], tcy[index] 34 | else: 35 | ttx = np.hstack((ttx, tx[index])) 36 | if i == 0: 37 | self.x, self.labels = ttx, ttcy 38 | else: 39 | self.x, self.labels = np.vstack( 40 | (self.x, ttx)), np.hstack((self.labels, ttcy)) 41 | 42 | def set_x(self, x): 43 | self.x = x 44 | -------------------------------------------------------------------------------- /code/deep/fixed/datautil/actdata/util.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from torchvision import transforms 3 | import numpy as np 4 | 5 | 6 | def act_train(): 7 | return transforms.Compose([ 8 | transforms.ToTensor() 9 | ]) 10 | 11 | 12 | def act_test(): 13 | return transforms.Compose([ 14 | transforms.ToTensor() 15 | ]) 16 | 17 | 18 | def loaddata_from_numpy(dataset='dsads', root_dir='./data/act/'): 19 | x = np.load(root_dir+dataset+'/'+dataset+'_x.npy') 20 | ty = np.load(root_dir+dataset+'/'+dataset+'_y.npy') 21 | cy, py, sy = ty[:, 0], ty[:, 1], ty[:, 2] 22 | return x, cy, py, sy 23 | -------------------------------------------------------------------------------- /code/deep/fixed/datautil/mydataloader.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch 3 | 4 | 5 | class _InfiniteSampler(torch.utils.data.Sampler): 6 | def __init__(self, sampler): 7 | self.sampler = sampler 8 | 9 | def __iter__(self): 10 | while True: 11 | for batch in self.sampler: 12 | yield batch 13 | 14 | 15 | class InfiniteDataLoader: 16 | def __init__(self, dataset, weights, batch_size, num_workers): 17 | super().__init__() 18 | self.dataset = dataset 19 | if weights: 20 | sampler = torch.utils.data.WeightedRandomSampler(weights, 21 | replacement=True, 22 | num_samples=batch_size) 23 | else: 24 | sampler = torch.utils.data.RandomSampler(dataset, 25 | replacement=True) 26 | 27 | if weights == None: 28 | weights = torch.ones(len(dataset)) 29 | 30 | batch_sampler = torch.utils.data.BatchSampler( 31 | sampler, 32 | batch_size=batch_size, 33 | drop_last=True) 34 | 35 | self._infinite_iterator = iter(torch.utils.data.DataLoader( 36 | dataset, 37 | num_workers=num_workers, 38 | batch_sampler=_InfiniteSampler(batch_sampler) 39 | )) 40 | 41 | def __iter__(self): 42 | while True: 43 | yield next(self._infinite_iterator) 44 | 45 | def __len__(self): 46 | raise ValueError 47 | -------------------------------------------------------------------------------- /code/deep/fixed/network/Adver_network.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Function 4 | 5 | 6 | class ReverseLayerF(Function): 7 | @staticmethod 8 | def forward(ctx, x, alpha): 9 | ctx.alpha = alpha 10 | return x.view_as(x) 11 | 12 | @staticmethod 13 | def backward(ctx, grad_output): 14 | output = grad_output.neg() * ctx.alpha 15 | return output, None 16 | 17 | 18 | class Discriminator(nn.Module): 19 | def __init__(self, input_dim=256, hidden_dim=256, num_domains=4): 20 | super(Discriminator, self).__init__() 21 | self.input_dim = input_dim 22 | self.hidden_dim = hidden_dim 23 | layers = [ 24 | nn.Linear(input_dim, hidden_dim), 25 | nn.BatchNorm1d(hidden_dim), 26 | nn.ReLU(), 27 | nn.Linear(hidden_dim, hidden_dim), 28 | nn.BatchNorm1d(hidden_dim), 29 | nn.ReLU(), 30 | nn.Linear(hidden_dim, num_domains), 31 | ] 32 | self.layers = torch.nn.Sequential(*layers) 33 | 34 | def forward(self, x): 35 | return self.layers(x) 36 | -------------------------------------------------------------------------------- /code/deep/fixed/network/act_network.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch.nn as nn 3 | 4 | var_size = { 5 | 'usc': { 6 | 'in_size': 6, 7 | 'ker_size': 6, 8 | 'fc_size': 32*46 9 | } 10 | } 11 | 12 | 13 | class ActNetwork(nn.Module): 14 | def __init__(self, taskname): 15 | super(ActNetwork, self).__init__() 16 | self.taskname = taskname 17 | self.conv1 = nn.Sequential( 18 | nn.Conv2d(in_channels=var_size[taskname]['in_size'], out_channels=16, kernel_size=( 19 | 1, var_size[taskname]['ker_size'])), 20 | nn.BatchNorm2d(16), 21 | nn.ReLU(), 22 | nn.MaxPool2d(kernel_size=(1, 2), stride=2) 23 | ) 24 | self.conv2 = nn.Sequential( 25 | nn.Conv2d(in_channels=16, out_channels=32, kernel_size=( 26 | 1, var_size[taskname]['ker_size'])), 27 | nn.BatchNorm2d(32), 28 | nn.ReLU(), 29 | nn.MaxPool2d(kernel_size=(1, 2), stride=2) 30 | ) 31 | self.in_features = var_size[taskname]['fc_size'] 32 | 33 | def forward(self, x): 34 | x = self.conv2(self.conv1(x)) 35 | x = x.view(-1, self.in_features) 36 | return x 37 | 38 | def getfea(self, x): 39 | x = self.conv2(self.conv1(x)) 40 | return x 41 | -------------------------------------------------------------------------------- /code/deep/fixed/network/common_network.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | import torch.nn as nn 3 | import torch.nn.utils.weight_norm as weightNorm 4 | 5 | 6 | class feat_bottleneck(nn.Module): 7 | def __init__(self, feature_dim, bottleneck_dim=256, type="ori"): 8 | super(feat_bottleneck, self).__init__() 9 | self.bn = nn.BatchNorm1d(bottleneck_dim, affine=True) 10 | self.relu = nn.ReLU(inplace=True) 11 | self.dropout = nn.Dropout(p=0.5) 12 | self.bottleneck = nn.Linear(feature_dim, bottleneck_dim) 13 | self.type = type 14 | 15 | def forward(self, x): 16 | x = self.bottleneck(x) 17 | if self.type == "bn": 18 | x = self.bn(x) 19 | return x 20 | 21 | 22 | class feat_classifier(nn.Module): 23 | def __init__(self, class_num, bottleneck_dim=256, type="linear"): 24 | super(feat_classifier, self).__init__() 25 | self.type = type 26 | if type == 'wn': 27 | self.fc = weightNorm( 28 | nn.Linear(bottleneck_dim, class_num), name="weight") 29 | else: 30 | self.fc = nn.Linear(bottleneck_dim, class_num) 31 | 32 | def forward(self, x): 33 | x = self.fc(x) 34 | return x 35 | -------------------------------------------------------------------------------- /code/deep/fixed/requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.22.0 2 | Pillow==9.3.0 3 | scikit-learn==0.23.2 4 | scipy==1.10.0 5 | tqdm==4.62.3 -------------------------------------------------------------------------------- /code/deep/fixed/resources/figures/frame.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/deep/fixed/resources/figures/frame.png -------------------------------------------------------------------------------- /code/distance/coral_pytorch.py: -------------------------------------------------------------------------------- 1 | # Compute CORAL loss using pytorch 2 | # Reference: DCORAL: Correlation Alignment for Deep Domain Adaptation, ECCV-16. 3 | 4 | import torch 5 | 6 | def CORAL_loss(source, target): 7 | d = source.data.shape[1] 8 | ns, nt = source.data.shape[0], target.data.shape[0] 9 | # source covariance 10 | xm = torch.mean(source, 0, keepdim=True) - source 11 | xc = xm.t() @ xm / (ns - 1) 12 | 13 | # target covariance 14 | xmt = torch.mean(target, 0, keepdim=True) - target 15 | xct = xmt.t() @ xmt / (nt - 1) 16 | 17 | # frobenius norm between source and target 18 | loss = torch.mul((xc - xct), (xc - xct)) 19 | loss = torch.sum(loss) / (4*d*d) 20 | return loss 21 | 22 | # Another implementation: 23 | # Two implementations are the same. Just different formulation format. 24 | # def CORAL(source, target): 25 | # d = source.size(1) 26 | # ns, nt = source.size(0), target.size(0) 27 | # # source covariance 28 | # tmp_s = torch.ones((1, ns)).to(DEVICE) @ source 29 | # cs = (source.t() @ source - (tmp_s.t() @ tmp_s) / ns) / (ns - 1) 30 | 31 | # # target covariance 32 | # tmp_t = torch.ones((1, nt)).to(DEVICE) @ target 33 | # ct = (target.t() @ target - (tmp_t.t() @ tmp_t) / nt) / (nt - 1) 34 | 35 | # # frobenius norm 36 | # loss = torch.norm(cs - ct, p='fro').pow(2) 37 | # loss = loss / (4 * d * d) 38 | # return loss -------------------------------------------------------------------------------- /code/distance/mmd_matlab.m: -------------------------------------------------------------------------------- 1 | % Compute MMD distance using Matlab 2 | % Inputs X and Y are all matrices (n_sample * dim) 3 | 4 | function [res] = mmd_matlab(X, Y, kernel_type, gamma) 5 | if (nargin < 3) 6 | disp('Please input the kernel_type!'); 7 | res = -9999; 8 | return; 9 | end 10 | switch kernel_type 11 | case 'linear' 12 | res = mmd_linear(X, Y); 13 | case 'rbf' 14 | if (nargin < 4) 15 | gamma = 1; 16 | end 17 | res = mmd_rbf(X, Y, gamma); 18 | end 19 | end 20 | 21 | function [res] = mmd_linear(X, Y) 22 | delta = mean(X) - mean(Y); 23 | res = delta * delta'; 24 | end 25 | 26 | function [res] = mmd_rbf(X, Y, gamma) 27 | ker = 'rbf'; 28 | XX = kernel(ker, X', [], gamma); 29 | YY = kernel(ker, Y', [], gamma); 30 | XY = kernel(ker, X', Y', gamma); 31 | res = mean(mean(XX)) + mean(mean(YY)) - 2 * mean(mean(XY)); 32 | end 33 | 34 | function K = kernel(ker,X,X2,gamma) 35 | 36 | switch ker 37 | case 'linear' 38 | 39 | if isempty(X2) 40 | K = X'*X; 41 | else 42 | K = X'*X2; 43 | end 44 | 45 | case 'rbf' 46 | 47 | n1sq = sum(X.^2,1); 48 | n1 = size(X,2); 49 | 50 | if isempty(X2) 51 | D = (ones(n1,1)*n1sq)' + ones(n1,1)*n1sq -2*X'*X; 52 | else 53 | n2sq = sum(X2.^2,1); 54 | n2 = size(X2,2); 55 | D = (ones(n2,1)*n1sq)' + ones(n1,1)*n2sq -2*X'*X2; 56 | end 57 | K = exp(-gamma*D); 58 | 59 | case 'sam' 60 | 61 | if isempty(X2) 62 | D = X'*X; 63 | else 64 | D = X'*X2; 65 | end 66 | K = exp(-gamma*acos(D).^2); 67 | 68 | otherwise 69 | error(['Unsupported kernel ' ker]) 70 | end 71 | end -------------------------------------------------------------------------------- /code/distance/proxy_a_distance.py: -------------------------------------------------------------------------------- 1 | # Compute A-distance using numpy and sklearn 2 | # Reference: Analysis of representations in domain adaptation, NIPS-07. 3 | 4 | import numpy as np 5 | from sklearn import svm 6 | 7 | def proxy_a_distance(source_X, target_X, verbose=False): 8 | """ 9 | Compute the Proxy-A-Distance of a source/target representation 10 | """ 11 | nb_source = np.shape(source_X)[0] 12 | nb_target = np.shape(target_X)[0] 13 | 14 | if verbose: 15 | print('PAD on', (nb_source, nb_target), 'examples') 16 | 17 | C_list = np.logspace(-5, 4, 10) 18 | 19 | half_source, half_target = int(nb_source/2), int(nb_target/2) 20 | train_X = np.vstack((source_X[0:half_source, :], target_X[0:half_target, :])) 21 | train_Y = np.hstack((np.zeros(half_source, dtype=int), np.ones(half_target, dtype=int))) 22 | 23 | test_X = np.vstack((source_X[half_source:, :], target_X[half_target:, :])) 24 | test_Y = np.hstack((np.zeros(nb_source - half_source, dtype=int), np.ones(nb_target - half_target, dtype=int))) 25 | 26 | best_risk = 1.0 27 | for C in C_list: 28 | clf = svm.SVC(C=C, kernel='linear', verbose=False) 29 | clf.fit(train_X, train_Y) 30 | 31 | train_risk = np.mean(clf.predict(train_X) != train_Y) 32 | test_risk = np.mean(clf.predict(test_X) != test_Y) 33 | 34 | if verbose: 35 | print('[ PAD C = %f ] train risk: %f test risk: %f' % (C, train_risk, test_risk)) 36 | 37 | if test_risk > .5: 38 | test_risk = 1. - test_risk 39 | 40 | best_risk = min(best_risk, test_risk) 41 | 42 | return 2 * (1. - 2 * best_risk) -------------------------------------------------------------------------------- /code/feature_extractor/for_image_data/models.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import backbone 4 | 5 | 6 | class Network(nn.Module): 7 | def __init__(self, base_net='alexnet', n_class=31): 8 | super(Network, self).__init__() 9 | self.n_class = n_class 10 | self.base_network = backbone.network_dict[base_net]() 11 | self.classifier_layer = nn.Linear( 12 | self.base_network.output_num(), n_class) 13 | self.classifier_layer.weight.data.normal_(0, 0.005) 14 | self.classifier_layer.bias.data.fill_(0.1) 15 | 16 | def forward(self, x): 17 | features = self.base_network(x) 18 | clf = self.classifier_layer(features) 19 | return clf 20 | 21 | def get_features(self, x): 22 | features = self.base_network(x) 23 | return features 24 | -------------------------------------------------------------------------------- /code/traditional/BDA/matlab/demo_BDA.m: -------------------------------------------------------------------------------- 1 | %% Choose domains from Office+Caltech 2 | %%% 'Caltech10', 'amazon', 'webcam', 'dslr' 3 | src = 'caltech.mat'; 4 | tgt = 'amazon.mat'; 5 | 6 | %% Load data 7 | load(['data/' src]); % source domain 8 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 9 | Xs = zscore(fts,1); clear fts 10 | Ys = labels; clear labels 11 | 12 | load(['data/' tgt]); % target domain 13 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 14 | Xt = zscore(fts,1); clear fts 15 | Yt = labels; clear labels 16 | 17 | %% Set algorithm options 18 | options.gamma = 1.0; 19 | options.lambda = 0.1; 20 | options.kernel_type = 'linear'; 21 | options.T = 10; 22 | options.dim = 100; 23 | options.mu = 0; 24 | options.mode = 'W-BDA'; 25 | %% Run algorithm 26 | [Acc,acc_ite,~] = BDA(Xs,Ys,Xt,Yt,options); 27 | fprintf('Acc:%.2f',Acc); 28 | -------------------------------------------------------------------------------- /code/traditional/BDA/readme.md: -------------------------------------------------------------------------------- 1 | ## Balanced Distributon Adaptation (BDA) 2 | 3 | This directory contains the code for paper [Balanced Distribution Adaptation for Transfer Learning](http://jd92.wang/assets/files/a08_icdm17.pdf). 4 | 5 | We support both Matlab and Python. 6 | 7 | The test dataset can be downloaded [HERE](https://github.com/jindongwang/transferlearning/tree/master/data). 8 | 9 | ### Important Notice 10 | 11 | Please note that the original BDA (with $\mu$) has been extensively extended by MEDA (Manifold Embedded Distribution Alignment) published in ACM International Conference on Multimedia (ACMMM) 2018. Its code can be found [here](https://github.com/jindongwang/transferlearning/tree/master/code/traditional/MEDA). 12 | 13 | Hence, from now on, the name **BDA** will specifically refer to **W-BDA** (for imbalanced transfer) in this ICDM paper. Therefore, the code default will also be the imbalanced version. If still want to use the original BDA, you can change the settings in the code. 14 | 15 | We also provide a notebook for you to directly run two algorithms using **Jupyter notebook**. 16 | 17 | You can easily download the notebook from [here](https://github.com/jindongwang/transferlearning/tree/master/notebooks/traditional_transfer_learning.ipynb). 18 | 19 | We also put it on [Google Colab](https://colab.research.google.com/drive/1w4WkCrZeCKaornJo66P4hxC7kzm4l70U?usp=sharing). 20 | 21 | Try and experiment on them! 22 | 23 | ### Citation 24 | You can cite this paper as 25 | 26 | - Bibtex style: 27 | ``` 28 | @inproceedings{wang2017balanced, 29 | title={Balanced Distribution Adaptation for Transfer Learning}, 30 | author={Wang, Jindong and Chen, Yiqiang and Hao, Shuji and Feng, Wenjie and Shen, Zhiqi}, 31 | booktitle={The IEEE International conference on data mining (ICDM)}, 32 | year={2017}, 33 | pages={1129--1134} 34 | } 35 | ``` 36 | 37 | - GB/T 7714 style: 38 | 39 | Jindong Wang, Yiqiang Chen, Shuji Hao, Wenjie Feng, and Zhiqi Shen. Balanced Distribution Adaptation for Transfer Learning. ICDM 2017. pp. 1129-1134. 40 | 41 | 42 | -------------------------------------------------------------------------------- /code/traditional/CORAL/CORAL.m: -------------------------------------------------------------------------------- 1 | function [Xs_new] = CORAL(Xs,Xt) 2 | % Inputs: 3 | %%% Xs:source domain feature matrix, ns * m 4 | %%% Xt:target domain feature matrix, nt * m 5 | % Output: 6 | %%% Xs_new:transformed new source domain matrix, ns * m 7 | 8 | cov_src = cov(Xs) + eye(size(Xs,2)); 9 | cov_tar = cov(Xt) + eye(size(Xt,2)); 10 | A_coral = cov_src^(-1/2) * cov_tar^(1/2); 11 | Xs_new = Xs * A_coral; 12 | end -------------------------------------------------------------------------------- /code/traditional/CORAL/CORAL_SVM.m: -------------------------------------------------------------------------------- 1 | function [acc,y_pred,time_pass] = CORAL_SVM(Xs,Ys,Xt,Yt) 2 | %% This combines CORAL and SVM. Very simple, very easy to use. 3 | %% Please download libsvm and add it to Matlab path before using SVM. 4 | 5 | time_start = clock(); 6 | %CORAL 7 | Xs = double(Xs); 8 | Xt = double(Xt); 9 | Ys = double(Ys); 10 | Yt = double(Yt); 11 | cov_source = cov(Xs) + eye(size(Xs, 2)); 12 | cov_target = cov(Xt) + eye(size(Xt, 2)); 13 | A_coral = cov_source^(-1/2)*cov_target^(1/2); 14 | Sim_coral = double(Xs * A_coral * Xt'); 15 | [acc,y_pred] = SVM_Accuracy(double(Xs), A_coral, double(Yt), Sim_coral, double(Ys)); 16 | time_end = clock(); 17 | time_pass = etime(time_end,time_start); 18 | end 19 | 20 | function [res,predicted_label] = SVM_Accuracy (trainset, M,testlabelsref,Sim,trainlabels) 21 | % Using Libsvm 22 | Sim_Trn = trainset * M * trainset'; 23 | index = [1:1:size(Sim,1)]'; 24 | Sim = [[1:1:size(Sim,2)]' Sim']; 25 | Sim_Trn = [index Sim_Trn ]; 26 | 27 | C = [0.001 0.01 0.1 1.0 10 100 1000 10000]; 28 | parfor i = 1 :size(C,2) 29 | model(i) = libsvmtrain(trainlabels, Sim_Trn, sprintf('-t 4 -c %d -v 2 -q',C(i))); 30 | end 31 | [val indx]=max(model); 32 | CVal = C(indx); 33 | model = svmtrain(trainlabels, Sim_Trn, sprintf('-t 4 -c %d -q',CVal)); 34 | [predicted_label, accuracy, decision_values] = svmpredict(testlabelsref, Sim, model); 35 | res = accuracy(1,1); 36 | end -------------------------------------------------------------------------------- /code/traditional/CORAL/readme.md: -------------------------------------------------------------------------------- 1 | # CORrelation ALignment (CORAL) 2 | 3 | This is the implementation of CORrelation ALignment (CORAL) in Matlab and Python. 4 | 5 | ## Usage 6 | 7 | - Python: just use the `CORAL.py` file. 8 | - Matlab: if you want to use CORAL to transform features, use `CORAL.m`. Besides, if you want to do both CORAL transformation and SVM prediction, you can use `CORAL_SVM.m`. 9 | 10 | The test dataset can be downloaded [HERE](https://github.com/jindongwang/transferlearning/tree/master/data). 11 | 12 | The python file can be used directly, while the matlab code just contains the core function of TCA. To use the matlab code, you need to learn from the code of [BDA](https://github.com/jindongwang/transferlearning/tree/master/code/BDA) and set out the parameters. 13 | 14 | **Reference** 15 | 16 | Sun B, Feng J, Saenko K. Return of frustratingly easy domain adaptation[C]//AAAI. 2016, 6(7): 8. -------------------------------------------------------------------------------- /code/traditional/EasyTL/CORAL_map.m: -------------------------------------------------------------------------------- 1 | function [Xs_new] = CORAL_map(Xs,Xt) 2 | cov_src = cov(Xs) + eye(size(Xs,2)); 3 | cov_tar = cov(Xt) + eye(size(Xt,2)); 4 | A_coral = cov_src^(-1/2) * cov_tar^(1/2); 5 | Xs_new = Xs * A_coral; 6 | end -------------------------------------------------------------------------------- /code/traditional/EasyTL/demo_amazon_review.m: -------------------------------------------------------------------------------- 1 | %% Test Amazon review dataset 2 | 3 | addpath('data/text/amazon_review_400/'); 4 | str_domains = {'books','dvd','elec','kitchen'}; 5 | list_acc = []; 6 | for i = 1 : 4 7 | for j = 1 : 4 8 | if i == j 9 | continue; 10 | end 11 | fprintf('%s - %s\n',str_domains{i}, str_domains{j}); 12 | 13 | %% Load data 14 | load([str_domains{i},'_400.mat']); 15 | Xs = fts; clear fts; 16 | Ys = labels; clear labels; 17 | load([str_domains{j},'_400.mat']); 18 | Xt = fts; clear fts; 19 | Yt = labels; clear labels; 20 | Ys = Ys + 1; 21 | Yt = Yt + 1; 22 | 23 | Xs = Xs ./ repmat(sum(Xs,2),1,size(Xs,2)); 24 | Xs = zscore(Xs,1); 25 | Xt = Xt ./ repmat(sum(Xt,2),1,size(Xt,2)); 26 | Xt = zscore(Xt,1); 27 | 28 | % EasyTL without intra-domain alignment [EasyTL(c)] 29 | [Acc1, ~] = EasyTL(Xs,Ys,Xt,Yt,'raw'); 30 | fprintf('Acc: %f\n',Acc1); 31 | 32 | % EasyTL with CORAL for intra-domain alignment 33 | [Acc2, ~] = EasyTL(Xs,Ys,Xt,Yt); 34 | fprintf('Acc: %f\n',Acc2); 35 | 36 | list_acc = [list_acc;[Acc1,Acc2]]; 37 | 38 | end 39 | end -------------------------------------------------------------------------------- /code/traditional/EasyTL/demo_image.m: -------------------------------------------------------------------------------- 1 | % Test image datasets (ImageCLEF or Office-Home) using resnet50 features 2 | 3 | img_dataset = 'image-clef'; % 'image-clef' or 'office-home' 4 | 5 | if strcmp(img_dataset,'image-clef') 6 | str_domains = {'c', 'i', 'p'}; 7 | addpath('Image-CLEF DA dataset path'); % You need to change this path 8 | elseif strcmp(img_dataset,'office-home') 9 | str_domains = {'Art', 'Clipart', 'Product', 'RealWorld'}; 10 | addpath('Office-Home dataset path'); % You need to change this path 11 | end 12 | 13 | list_acc = []; 14 | for i = 1 : 3 15 | for j = 1 : 3 16 | if i == j 17 | continue; 18 | end 19 | src = str_domains{i}; 20 | tgt = str_domains{j}; 21 | fprintf('%s - %s\n',src, tgt); 22 | 23 | data = load([src '_' src '.csv']); 24 | fts = data(1:end,1:end-1); 25 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 26 | Xs = zscore(fts, 1); 27 | Ys = data(1:end,end) + 1; 28 | 29 | data = load([src '_' tgt '.csv']); 30 | fts = data(1:end,1:end-1); 31 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 32 | Xt = zscore(fts, 1); 33 | Yt = data(1:end,end) + 1; 34 | 35 | % EasyTL without intra-domain alignment [EasyTL(c)] 36 | [Acc1, ~] = EasyTL(Xs,Ys,Xt,Yt,'raw'); 37 | fprintf('Acc: %f\n',Acc1); 38 | 39 | % EasyTL with CORAL for intra-domain alignment 40 | [Acc2, ~] = EasyTL(Xs,Ys,Xt,Yt); 41 | fprintf('Acc: %f\n',Acc2); 42 | 43 | list_acc = [list_acc;[Acc1,Acc2]]; 44 | 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /code/traditional/EasyTL/demo_office_caltech.m: -------------------------------------------------------------------------------- 1 | % Test with Office-Caltech datasets 2 | 3 | str_domains = { 'Caltech10', 'amazon', 'webcam', 'dslr' }; 4 | list_acc = []; 5 | for i = 1 : 4 6 | for j = 1 : 4 7 | if i == j 8 | continue; 9 | end 10 | src = str_domains{i}; 11 | tgt = str_domains{j}; 12 | load(['data/office+caltech--surf/' src '_SURF_L10.mat']); % source domain 13 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 14 | Xs = zscore(fts,1); clear fts 15 | Ys = labels; clear labels 16 | 17 | load(['data/office+caltech--surf/' tgt '_SURF_L10.mat']); % target domain 18 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 19 | Xt = zscore(fts,1); clear fts 20 | Yt = labels; clear labels 21 | 22 | % EasyTL without intra-domain alignment [EasyTL(c)] 23 | [Acc1, ~] = EasyTL(Xs,Ys,Xt,Yt,'raw'); 24 | fprintf('Acc: %f\n',Acc1); 25 | 26 | % EasyTL with CORAL for intra-domain alignment 27 | [Acc2, ~] = EasyTL(Xs,Ys,Xt,Yt); 28 | fprintf('Acc: %f\n',Acc2); 29 | 30 | list_acc = [list_acc;[Acc1,Acc2]]; 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /code/traditional/EasyTL/label_prop.m: -------------------------------------------------------------------------------- 1 | function [Mcj] = label_prop(C,nt,Dct,lp) 2 | % Inputs: 3 | %%% C : Number of share classes between src and tar 4 | %%% nt : Number of target domain samples 5 | %%% Dct : All d_ct in matrix form, nt * C 6 | %%% lp : Type of linear programming: linear (default) | binary 7 | % Outputs: 8 | %%% Mcj : all M_ct in matrix form, m * C 9 | 10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 11 | 12 | intcon = C * nt; % Number of all variables to be solved (all x_ct and o_t) 13 | if nargin == 3 14 | lp = 'linear'; 15 | end 16 | 17 | Aeq = zeros(nt, intcon); 18 | 19 | Beq = ones(nt ,1); 20 | for i = 1 : nt 21 | Aeq(i,(i - 1) * C + 1 : i * C) = 1; 22 | end 23 | %for i = 1 : nt 24 | % lllll =i 25 | % all_zeros = zeros(1,intcon); 26 | % all_zeros((i - 1) * C + 1 : i * C) = 1; 27 | % Aeq = [Aeq;all_zeros]; 28 | % Beq = [Beq;1]; 29 | % end 30 | D_vec = reshape(Dct',1,intcon); 31 | CC = double(D_vec); 32 | 33 | 34 | 35 | 36 | 37 | A = []; 38 | B = []; 39 | for i = 1 : C 40 | all_zeros = zeros(1,intcon); 41 | j = i : C : C * nt; 42 | all_zeros(j) = -1; 43 | A = [A;all_zeros]; 44 | B = [B;-1]; 45 | end 46 | 47 | lb_12 = zeros(intcon,1); 48 | ub_12 = ones(intcon,1); 49 | 50 | 51 | % options = optimoptions('linprog','Algorithm','interior-point'); 52 | 53 | if strcmp(lp,'binary') 54 | 55 | X = intlinprog(CC,intcon,A,B,Aeq,Beq,lb_12,ub_12); 56 | else 57 | 58 | X = linprog(CC,A,B,Aeq,Beq,lb_12,ub_12); 59 | end 60 | Mct_vec = X(1:C*nt); 61 | Mcj = reshape(Mct_vec,C,nt)'; % M matrix, nt * C 62 | end -------------------------------------------------------------------------------- /code/traditional/GFK/readme.md: -------------------------------------------------------------------------------- 1 | # Geodesic Flow Kernwl (GFK) 2 | 3 | This is the implementation of Geodesic Flow Kernel (GFK) in both Matlab and Python. 4 | 5 | ## Matlab version 6 | 7 | Just use `GFK.m`. Note that `getGFKDim.m` is the implementation of *subspace disagreement measurement* proposed in GFK paper. 8 | 9 | ## Python version 10 | 11 | **Note:** We may not be able to use `bob` any more. Therefore, the code of GFK in python is also not available since to achieve the same results in Matlab, we need to use `bob` library. 12 | 13 | Therefore, I can do nothing about it except suggesting that you use the Matlab version instead. If you still want to use Python code, then you can try to call the Matlab code in your Python script by following some tutorials online. 14 | 15 | See the `GFK.py` file. 16 | 17 | Requirements: 18 | - Python 3 19 | - Numpy and Scipy 20 | - Sklearn 21 | - **Bob** 22 | 23 | **Note:** This Python version is wrapped from Bob: https://www.idiap.ch/software/bob/docs/bob/bob.learn.linear/stable/_modules/bob/learn/linear/GFK.html#GFKMachine. 24 | 25 | Therefore, if you want to run and use this program, you should install `bob` by following its official instructions in [here](https://www.idiap.ch/software/bob/docs/bob/docs/stable/bob/bob/doc/install.html). 26 | 27 | There are many libraries in bob. A minimum request is to install `bob.math` and `bob.learn` by following the instructions. 28 | 29 | ## Run 30 | 31 | The test dataset can be downloaded [HERE](https://github.com/jindongwang/transferlearning/tree/master/data). Then, you can run the file. 32 | 33 | The python file can be used directly, while the matlab code just contains the core function of TCA. To use the matlab code, you need to learn from the code of [BDA](https://github.com/jindongwang/transferlearning/tree/master/code/BDA) and set out the parameters. 34 | 35 | ### Reference 36 | 37 | Gong B, Shi Y, Sha F, et al. Geodesic flow kernel for unsupervised domain adaptation[C]//2012 IEEE Conference on Computer Vision and Pattern Recognition. IEEE, 2012: 2066-2073. 38 | 39 | If you have any questions, please open an issue. -------------------------------------------------------------------------------- /code/traditional/JDA/readme.md: -------------------------------------------------------------------------------- 1 | # Joint Distribution Adaptation 2 | 3 | This is the implementation of Joint Distribution Adaptation (JDA) in Python and Matlab. 4 | 5 | Remark: The core of JDA is a generalized eigendecompsition problem. In Matlab, it can be solved by calling `eigs()` function. In Python, the implementation `scipy.linalg.eig()` function can do the same thing. However, they are a bit different. So the results may be different. 6 | 7 | The test dataset can be downloaded [HERE](https://github.com/jindongwang/transferlearning/tree/master/data). 8 | 9 | The python file can be used directly, while the matlab code just contains the core function of TCA. To use the matlab code, you need to learn from the code of [BDA](https://github.com/jindongwang/transferlearning/tree/master/code/BDA) and set out the parameters. 10 | 11 | **Reference** 12 | 13 | Long M, Wang J, Ding G, et al. Transfer feature learning with joint distribution adaptation[C]//Proceedings of the IEEE international conference on computer vision. 2013: 2200-2207. -------------------------------------------------------------------------------- /code/traditional/MEDA/matlab/GFK_Map.m: -------------------------------------------------------------------------------- 1 | function [Xs_new,Xt_new,G] = GFK_Map(Xs,Xt,dim) 2 | Ps = pca(Xs); 3 | Pt = pca(Xt); 4 | G = GFK_core([Ps,null(Ps')], Pt(:,1:dim)); 5 | sq_G = real(G^(0.5)); 6 | Xs_new = (sq_G * Xs')'; 7 | Xt_new = (sq_G * Xt')'; 8 | end 9 | 10 | function G = GFK_core(Q,Pt) 11 | % Input: Q = [Ps, null(Ps')], where Ps is the source subspace, column-wise orthonormal 12 | % Pt: target subsapce, column-wise orthonormal, D-by-d, d < 0.5*D 13 | % Output: G = \int_{0}^1 \Phi(t)\Phi(t)' dt 14 | 15 | % ref: Geodesic Flow Kernel for Unsupervised Domain Adaptation. 16 | % B. Gong, Y. Shi, F. Sha, and K. Grauman. 17 | % Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR), Providence, RI, June 2012. 18 | 19 | % Contact: Boqing Gong (boqinggo@usc.edu) 20 | 21 | N = size(Q,2); % 22 | dim = size(Pt,2); 23 | 24 | % compute the principal angles 25 | QPt = Q' * Pt; 26 | [V1,V2,V,Gam,Sig] = gsvd(QPt(1:dim,:), QPt(dim+1:end,:)); 27 | V2 = -V2; 28 | theta = real(acos(diag(Gam))); % theta is real in theory. Imaginary part is due to the computation issue. 29 | 30 | % compute the geodesic flow kernel 31 | eps = 1e-20; 32 | B1 = 0.5.*diag(1+sin(2*theta)./2./max(theta,eps)); 33 | B2 = 0.5.*diag((-1+cos(2*theta))./2./max(theta,eps)); 34 | B3 = B2; 35 | B4 = 0.5.*diag(1-sin(2*theta)./2./max(theta,eps)); 36 | G = Q * [V1, zeros(dim,N-dim); zeros(N-dim,dim), V2] ... 37 | * [B1,B2,zeros(dim,N-2*dim);B3,B4,zeros(dim,N-2*dim);zeros(N-2*dim,N)]... 38 | * [V1, zeros(dim,N-dim); zeros(N-dim,dim), V2]' * Q'; 39 | end -------------------------------------------------------------------------------- /code/traditional/MEDA/matlab/README.md: -------------------------------------------------------------------------------- 1 | # MEDA: Manifold Embedded Distribution Alignment 2 | 3 | This is the official Matlab code for MEDA. 4 | 5 | ## Usage 6 | 7 | After download the data from [here](https://github.com/jindongwang/transferlearning/tree/master/code/traditional/MEDA#office-31-dataset), you can run `demo_office_caltech_surf.m`. 8 | 9 | More results can be found in [here](https://github.com/jindongwang/transferlearning/tree/master/code/traditional/MEDA) -------------------------------------------------------------------------------- /code/traditional/MEDA/matlab/demo_office_caltech_surf.m: -------------------------------------------------------------------------------- 1 | % DEMO for testing MEDA on Office+Caltech10 datasets 2 | str_domains = {'Caltech10', 'amazon', 'webcam', 'dslr'}; 3 | list_acc = []; 4 | for i = 1 : 4 5 | for j = 1 : 4 6 | if i == j 7 | continue; 8 | end 9 | src = str_domains{i}; 10 | tgt = str_domains{j}; 11 | load(['data/' src '_SURF_L10.mat']); % source domain 12 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 13 | Xs = zscore(fts,1); clear fts 14 | Ys = labels; clear labels 15 | 16 | load(['data/' tgt '_SURF_L10.mat']); % target domain 17 | fts = fts ./ repmat(sum(fts,2),1,size(fts,2)); 18 | Xt = zscore(fts,1); clear fts 19 | Yt = labels; clear labels 20 | 21 | % meda 22 | options.d = 20; 23 | options.rho = 1.0; 24 | options.p = 10; 25 | options.lambda = 10.0; 26 | options.eta = 0.1; 27 | options.T = 10; 28 | [Acc,~,~,~] = MEDA(Xs,Ys,Xt,Yt,options); 29 | fprintf('%s --> %s: %.2f accuracy \n\n', src, tgt, Acc * 100); 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /code/traditional/MEDA/matlab/estimate_mu.m: -------------------------------------------------------------------------------- 1 | function [mu,adist_m,adist_c] = estimate_mu(Xs,Ys,Xt,Yt) 2 | C = length(unique(Ys)); 3 | list_adist_c = []; 4 | epsilon = 1e-3; 5 | for i = 1 : C 6 | index_i = Ys == i; 7 | Xsi = Xs(index_i,:); 8 | index_j = Yt == i; 9 | Xtj = Xt(index_j,:); 10 | adist_i = adist(Xsi,Xtj); 11 | list_adist_c = [list_adist_c;adist_i]; 12 | end 13 | adist_c = mean(list_adist_c); 14 | 15 | adist_m = adist(Xs,Xt); 16 | mu = adist_c / (adist_c + adist_m); 17 | if mu > 1 % Theoretically mu <= 1, but calculation may be over 1 18 | mu = 1; 19 | elseif mu <= epsilon 20 | mu = 0; 21 | end 22 | end 23 | 24 | function dist = adist(Xs,Xt) 25 | Yss = ones(size(Xs,1),1); 26 | Ytt = ones(size(Xt,1),1) * 2; 27 | 28 | % The results of fitclinear() may vary in a very small range, since Matlab uses SGD to optimize SVM. 29 | % The fluctuation is very small, ignore it 30 | model_linear = fitclinear([Xs;Xt],[Yss;Ytt],'learner','svm'); 31 | ypred = model_linear.predict([Xs;Xt]); 32 | error = mae([Yss;Ytt],ypred); 33 | dist = 2 * (1 - 2 * error); 34 | end -------------------------------------------------------------------------------- /code/traditional/MEDA/results/meda-imageclef.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/traditional/MEDA/results/meda-imageclef.png -------------------------------------------------------------------------------- /code/traditional/MEDA/results/meda-office31.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/traditional/MEDA/results/meda-office31.png -------------------------------------------------------------------------------- /code/traditional/MEDA/results/meda-officehome.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/traditional/MEDA/results/meda-officehome.png -------------------------------------------------------------------------------- /code/traditional/MEDA/results/result1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/traditional/MEDA/results/result1.png -------------------------------------------------------------------------------- /code/traditional/MEDA/results/result2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/code/traditional/MEDA/results/result2.png -------------------------------------------------------------------------------- /code/traditional/SVM.m: -------------------------------------------------------------------------------- 1 | function [acc,y_pred,time_pass] = SVM(Xs,Ys,Xt,Yt) 2 | Xs = double(Xs); 3 | Xt = double(Yt); 4 | time_start = clock(); 5 | [acc,y_pred] = LinAccuracy(Xs,Xt,Ys,Yt); 6 | time_end = clock(); 7 | time_pass = etime(time_end,time_start); 8 | end 9 | 10 | function [acc,predicted_label] = LinAccuracy(trainset,testset,trainlbl,testlbl) 11 | model = trainSVM_Model(trainset,trainlbl); 12 | [predicted_label, accuracy, decision_values] = svmpredict(testlbl, testset, model); 13 | acc = accuracy(1,1); 14 | end 15 | 16 | function svmmodel = trainSVM_Model(trainset,trainlbl) 17 | C = [0.001 0.01 0.1 1.0 10 100 ]; 18 | parfor i = 1 :size(C,2) 19 | model(i) = libsvmtrain(double(trainlbl), sparse(double((trainset))),sprintf('-c %d -q -v 2',C(i) )); 20 | end 21 | [val indx]=max(model); 22 | CVal = C(indx); 23 | svmmodel = libsvmtrain(double(trainlbl), sparse(double((trainset))),sprintf('-c %d -q',CVal)); 24 | end -------------------------------------------------------------------------------- /code/traditional/TCA/readme.md: -------------------------------------------------------------------------------- 1 | # Transfer Component Analysis 2 | 3 | This is the implementation of Transfer Component Analysis (TCA) in Python and Matlab. 4 | 5 | Remark: The core of TCA is a generalized eigendecompsition problem. In Matlab, it can be solved by calling `eigs()` function. In Python, the implementation `scipy.linalg.eig()` function can do the same thing. However, they are a bit different. So the results may be different. 6 | 7 | The test dataset can be downloaded [HERE](https://github.com/jindongwang/transferlearning/tree/master/data). 8 | 9 | The python file can be used directly, while the matlab code just contains the core function of TCA. To use the matlab code, you need to learn from the code of [BDA](https://github.com/jindongwang/transferlearning/tree/master/code/BDA) and set out the parameters. 10 | 11 | **Reference** 12 | 13 | Pan S J, Tsang I W, Kwok J T, et al. Domain adaptation via transfer component analysis[J]. IEEE Transactions on Neural Networks, 2011, 22(2): 199-210. -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/.gitignore: -------------------------------------------------------------------------------- 1 | data/ 2 | *.pyc 3 | *.mps 4 | *.sol -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/demo_amazon_review.py: -------------------------------------------------------------------------------- 1 | import scipy.io 2 | import scipy.stats 3 | import numpy as np 4 | from EasyTL import EasyTL 5 | import time 6 | 7 | if __name__ == "__main__": 8 | datadir = r"D:\Datasets\EasyTL\amazon_review" 9 | str_domain = ["books", "dvd", "elec", "kitchen"] 10 | list_acc = [] 11 | 12 | for i in range(len(str_domain)): 13 | for j in range(len(str_domain)): 14 | if i == j: 15 | continue 16 | 17 | print("{} - {}".format(str_domain[i], str_domain[j])) 18 | 19 | mat1 = scipy.io.loadmat(datadir + "/{}_400.mat".format(str_domain[i])) 20 | Xs = mat1["fts"] 21 | Ys = mat1["labels"] 22 | mat2 = scipy.io.loadmat(datadir + "/{}_400.mat".format(str_domain[j])) 23 | Xt = mat2["fts"] 24 | Yt = mat2["labels"] 25 | 26 | Ys += 1 27 | Yt += 1 28 | 29 | Xs = Xs / np.tile(np.sum(Xs,axis=1).reshape(-1,1), [1, Xs.shape[1]]) 30 | Xs = scipy.stats.mstats.zscore(Xs); 31 | Xt = Xt / np.tile(np.sum(Xt,axis=1).reshape(-1,1), [1, Xt.shape[1]]) 32 | Xt = scipy.stats.mstats.zscore(Xt); 33 | 34 | Xs[np.isnan(Xs)] = 0 35 | Xt[np.isnan(Xt)] = 0 36 | 37 | t0 = time.time() 38 | Acc1, _ = EasyTL(Xs,Ys,Xt,Yt,"raw") 39 | t1 = time.time() 40 | print("Time Elapsed: {:.2f} sec".format(t1 - t0)) 41 | Acc2, _ = EasyTL(Xs,Ys,Xt,Yt) 42 | t2 = time.time() 43 | print("Time Elapsed: {:.2f} sec".format(t2 - t1)) 44 | 45 | print('EasyTL(c) Acc: {:.1f} % || EasyTL Acc: {:.1f} %'.format(Acc1*100, Acc2*100)) 46 | list_acc.append([Acc1,Acc2]) 47 | 48 | acc = np.array(list_acc) 49 | avg = np.mean(acc, axis=0) 50 | print('EasyTL(c) AVG Acc: {:.1f} %'.format(avg[0]*100)) 51 | print('EasyTL AVG Acc: {:.1f} %'.format(avg[1]*100)) -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/demo_office_caltech.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import scipy.io 3 | import scipy.stats 4 | import numpy as np 5 | from EasyTL import EasyTL 6 | 7 | if __name__ == "__main__": 8 | 9 | datadir = r"D:\Datasets\EasyTL\surf" 10 | str_domain = [ 'Caltech10', 'amazon', 'webcam', 'dslr' ] 11 | list_acc = [] 12 | 13 | for i in range(len(str_domain)): 14 | for j in range(len(str_domain)): 15 | if i == j: 16 | continue 17 | 18 | print("{} - {}".format(str_domain[i], str_domain[j])) 19 | 20 | mat1 = scipy.io.loadmat(datadir + "/{}_SURF_L10.mat".format(str_domain[i])) 21 | Xs = mat1["fts"] 22 | Ys = mat1["labels"] 23 | 24 | mat2 = scipy.io.loadmat(datadir + "/{}_SURF_L10.mat".format(str_domain[j])) 25 | Xt = mat2["fts"] 26 | Yt = mat2["labels"] 27 | 28 | Xs = Xs / np.tile(np.sum(Xs,axis=1).reshape(-1,1), [1, Xs.shape[1]]) 29 | Xs = scipy.stats.mstats.zscore(Xs); 30 | Xt = Xt / np.tile(np.sum(Xt,axis=1).reshape(-1,1), [1, Xt.shape[1]]) 31 | Xt = scipy.stats.mstats.zscore(Xt); 32 | 33 | Xs[np.isnan(Xs)] = 0 34 | Xt[np.isnan(Xt)] = 0 35 | 36 | Acc1, _ = EasyTL(Xs,Ys,Xt,Yt,'raw') 37 | Acc2, _ = EasyTL(Xs,Ys,Xt,Yt) 38 | 39 | print('EasyTL(c) Acc: {:.1f} % || EasyTL Acc: {:.1f} %'.format(Acc1*100, Acc2*100)) 40 | list_acc.append([Acc1,Acc2]) 41 | 42 | acc = np.array(list_acc) 43 | avg = np.mean(acc, axis=0) 44 | print('EasyTL(c) AVG Acc: {:.1f} %'.format(avg[0]*100)) 45 | print('EasyTL AVG Acc: {:.1f} %'.format(avg[1]*100)) 46 | -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/intra_alignment.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import scipy 3 | from sklearn.decomposition import PCA 4 | import math 5 | 6 | def GFK_map(Xs, Xt): 7 | pass 8 | 9 | def gsvd(A, B): 10 | pass 11 | 12 | def getAngle(Ps, Pt, DD): 13 | 14 | Q = np.hstack((Ps, scipy.linalg.null_space(Ps.T))) 15 | dim = Pt.shape[1] 16 | QPt = Q.T @ Pt 17 | A, B = QPt[:dim, :], QPt[dim:, :] 18 | U,V,X,C,S = gsvd(A, B) 19 | alpha = np.zeros([1, DD]) 20 | for i in range(DD): 21 | alpha[0][i] = math.sin(np.real(math.acos(C[i][i]*math.pi/180))) 22 | 23 | return alpha 24 | 25 | def getGFKDim(Xs, Xt): 26 | Pss = PCA().fit(Xs).components_.T 27 | Pts = PCA().fit(Xt).components_.T 28 | Psstt = PCA().fit(np.vstack((Xs, Xt))).components_.T 29 | 30 | DIM = round(Xs.shape[1]*0.5) 31 | res = -1 32 | 33 | for d in range(1, DIM+1): 34 | Ps = Pss[:, :d] 35 | Pt = Pts[:, :d] 36 | Pst = Psstt[:, :d] 37 | alpha1 = getAngle(Ps, Pst, d) 38 | alpha2 = getAngle(Pt, Pst, d) 39 | D = (alpha1 + alpha2) * 0.5 40 | check = [round(D[1, dd]*100) == 100 for dd in range(d)] 41 | if True in check: 42 | res = list(map(lambda i: i == True, check)).index(True) 43 | return res 44 | 45 | def PCA_map(Xs, Xt): 46 | dim = getGFKDim(Xs, Xt) 47 | X = np.vstack((Xs, Xt)) 48 | X_new = PCA().fit_transform(X)[:, :dim] 49 | Xs_new = X_new[:Xs.shape[0], :] 50 | Xt_new = X_new[Xs.shape[0]:, :] 51 | return Xs_new, Xt_new 52 | 53 | def CORAL_map(Xs,Xt): 54 | Ds = Xs.copy() 55 | Dt = Xt.copy() 56 | 57 | cov_src = np.ma.cov(Ds.T) + np.eye(Ds.shape[1]) 58 | cov_tar = np.ma.cov(Dt.T) + np.eye(Dt.shape[1]) 59 | 60 | Cs = scipy.linalg.sqrtm(np.linalg.inv(np.array(cov_src))) 61 | Ct = scipy.linalg.sqrtm(np.array(cov_tar)) 62 | Xs_new = np.linalg.multi_dot([Ds, Cs, Ct]) 63 | 64 | return Xs_new 65 | -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/label_prop.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy.optimize import linprog 3 | import pulp 4 | 5 | def label_prop(C, nt, Dct, lp="linear"): 6 | 7 | #Inputs: 8 | # C : Number of share classes between src and tar 9 | # nt : Number of target domain samples 10 | # Dct : All d_ct in matrix form, nt * C 11 | # lp : Type of linear programming: linear (default) | binary 12 | #Outputs: 13 | # Mcj : all M_ct in matrix form, m * C 14 | 15 | intcon = C * nt 16 | Aeq = np.zeros([nt, intcon]) 17 | Beq = np.ones([nt, 1]) 18 | for i in range(nt): 19 | Aeq[i, i*C:(i+1)*C] = 1; 20 | 21 | D_vec = np.reshape(Dct, (1, intcon)) 22 | CC = np.asarray(D_vec, dtype=np.double) 23 | 24 | A = np.array([]) 25 | B = -1 * np.ones([C, 1]) 26 | for i in range(C): 27 | all_zeros = np.zeros([1, intcon]) 28 | for j in range(i, C * nt, C): 29 | all_zeros[0][j] = -1 30 | if i == 0: 31 | A = all_zeros 32 | else: 33 | A = np.vstack((A, all_zeros)) 34 | 35 | if lp == "binary": 36 | print("not implemented yet!") 37 | else: 38 | res = linprog(CC,A,B,Aeq,Beq, bounds=tuple((0, 1) for _ in range(intcon))) 39 | Mct_vec = res.get("x")[0:C*nt] 40 | Mcj = Mct_vec.reshape((C,nt), order="F").T 41 | 42 | return Mcj 43 | -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/label_prop_v2.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pulp 3 | 4 | def label_prop(C, nt, Dct, lp="linear"): 5 | 6 | #Inputs: 7 | # C : Number of share classes between src and tar 8 | # nt : Number of target domain samples 9 | # Dct : All d_ct in matrix form, nt * C 10 | # lp : Type of linear programming: linear (default) | binary 11 | #Outputs: 12 | # Mcj : all M_ct in matrix form, m * C 13 | 14 | Dct = abs(Dct) 15 | model = pulp.LpProblem("Cost minimising problem", pulp.LpMinimize) 16 | Mcj = pulp.LpVariable.dicts("Probability", 17 | ((i, j) for i in range(C) for j in range(nt)), 18 | lowBound=0, 19 | upBound=1, 20 | cat='Continuous') 21 | 22 | # Objective Function 23 | model += ( 24 | pulp.lpSum([Dct[j, i]*Mcj[(i, j)] for i in range(C) for j in range(nt)]) 25 | ) 26 | 27 | # Constraints 28 | for j in range(nt): 29 | model += pulp.lpSum([Mcj[(i, j)] for i in range(C)]) == 1 30 | for i in range(C): 31 | model += pulp.lpSum([Mcj[(i, j)] for j in range(nt)]) >= 1 32 | 33 | # Solve our problem 34 | model.solve() 35 | pulp.LpStatus[model.status] 36 | Output = [[Mcj[i, j].varValue for i in range(C)] for j in range(nt)] 37 | 38 | return np.array(Output) 39 | -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/license: -------------------------------------------------------------------------------- 1 | Copyright (C) 2020 KodeWorker 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/readme.md: -------------------------------------------------------------------------------- 1 | # pyEasyTL 2 | 3 | ## Introduction 4 | This [project](https://github.com/KodeWorker/pyEasyTL) is the implementation of EasyTL in Python. 5 | The EasyTL paper on the [website](http://transferlearning.xyz/code/traditional/EasyTL/) show this domain adaptation method is intuitive and parametric-free. 6 | The MATLAB source code is on this [Repo](https://github.com/jindongwang/transferlearning/tree/master/code/traditional/EasyTL). 7 | 8 | - scipy.optimize.linprog is slower than PuLP 9 | - M^(-1/2) = (M^(-1))^(1/2) = scipy.linalg.sqrtm(np.linalg.inv(np.array(cov_src))) 10 | - scipy.linalg.sqrtm will introduce complex number [#3549](https://github.com/scipy/scipy/issues/3549) and cause our Dct parameter to be a complex array. 11 | 12 | ## To Do 13 | - PCA_map in intra_alignment.py 14 | - GFK_map in intra_alignment.py 15 | 16 | ## Dev Log 17 | - **2020/02/25** PuLP type conversion problems (can't convert complex to float) is fixed 18 | - **2020/02/24** write label_prop_v2.py using PuLP 19 | - **2020/02/05** implement get_ma_dist and get_cosine_dist in EasyTL.py (fixed) 20 | - **2020/02/03** more distance measurement in get_class_center 21 | - **2020/01/31** CORAL_map still has some issue. (fixed) 22 | - **2020/01/31** The primitive results of Amazon dataset show that we'v successfully implemented the EasyTL(c). 23 | 24 | # Reference: 25 | 1. Easy Transfer Learning By Exploiting Intra-domain Structures 26 | 2. Geodesic Flow Kernel for Unsupervised Domain Adaptation -------------------------------------------------------------------------------- /code/traditional/pyEasyTL/requirement.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | scipy 3 | pandas 4 | pulp -------------------------------------------------------------------------------- /code/traditional/readme.md: -------------------------------------------------------------------------------- 1 | # Traditional transfer learning 2 | 3 | Here we implement several popular traditional transfer learning methods. 4 | 5 | Most of the datasets are based on Office-Caltech10 Surf features, which can be downloaded here: https://www.jianguoyun.com/p/DaKoCGIQmN7PCBju9KsD, with password `PRSZD9`. 6 | 7 | We also provide a notebook for you to directly run two algorithms using **Jupyter notebook**. 8 | 9 | You can easily download the notebook from [here](https://github.com/jindongwang/transferlearning/tree/master/notebooks/traditional_transfer_learning.ipynb). 10 | 11 | We also put it on [Google Colab](https://colab.research.google.com/drive/1w4WkCrZeCKaornJo66P4hxC7kzm4l70U?usp=sharing). 12 | 13 | Try and experiment on them! -------------------------------------------------------------------------------- /code/traditional/sot/main.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | from SOT import SOT,load_from_file 3 | import numpy as np 4 | 5 | if __name__=='__main__': 6 | tsot=SOT('ACT','./clustertemp/',19,0.5,1,3) 7 | Sx,Sy,Tx,Ty=load_from_file('./data/','MDA_JCPOT_ACT.json','D','H') 8 | spath='./data/test_MDA_JCPOT_ACT_diag_SG.json' 9 | tpath='./clustertemp/data/test_MDA_JCPOT_ACT_19_diag_TG.json' 10 | tmodelpath='./clustertemp/model/test_MDA_JCPOT_ACT_19_diag_H' 11 | pred,acc=tsot.fit_predict(Sx,Sy,Tx,Ty,spath,'D',tpath,tmodelpath,'H') 12 | print(acc) -------------------------------------------------------------------------------- /code/utils/feature_vis.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from sklearn import datasets 4 | from sklearn.manifold import TSNE 5 | 6 | 7 | class FeatureVisualize(object): 8 | ''' 9 | Visualize features by TSNE 10 | ''' 11 | 12 | def __init__(self, features, labels): 13 | ''' 14 | features: (m,n) 15 | labels: (m,) 16 | ''' 17 | self.features = features 18 | self.labels = labels 19 | 20 | def plot_tsne(self, save_eps=False): 21 | ''' Plot TSNE figure. Set save_eps=True if you want to save a .eps file. 22 | ''' 23 | tsne = TSNE(n_components=2, init='pca', random_state=0) 24 | features = tsne.fit_transform(self.features) 25 | x_min, x_max = np.min(features, 0), np.max(features, 0) 26 | data = (features - x_min) / (x_max - x_min) 27 | del features 28 | for i in range(data.shape[0]): 29 | plt.text(data[i, 0], data[i, 1], str(self.labels[i]), 30 | color=plt.cm.Set1(self.labels[i] / 10.), 31 | fontdict={'weight': 'bold', 'size': 9}) 32 | plt.xticks([]) 33 | plt.yticks([]) 34 | plt.title('T-SNE') 35 | if save_eps: 36 | plt.savefig('tsne.eps', dpi=600, format='eps') 37 | plt.show() 38 | 39 | 40 | if __name__ == '__main__': 41 | digits = datasets.load_digits(n_class=5) 42 | features, labels = digits.data, digits.target 43 | print(features.shape) 44 | print(labels.shape) 45 | vis = FeatureVisualize(features, labels) 46 | vis.plot_tsne(save_eps=True) 47 | -------------------------------------------------------------------------------- /code/utils/grl.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Gradient reversal layer. Reference: 3 | Ganin et al. Unsupervised domain adaptation by backpropagation. ICML 2015. 4 | ''' 5 | 6 | import torch 7 | import numpy as np 8 | 9 | # For pytorch version > 1.0 10 | # Usage: 11 | # b = GradReverse.apply(a, 1) # 1 is the lambda value, you are free to set it 12 | class GradReverse(torch.autograd.Function): 13 | 14 | @staticmethod 15 | def forward(ctx, x, lambd, **kwargs: None): 16 | ctx.lambd = lambd 17 | return x.view_as(x) 18 | 19 | @staticmethod 20 | def backward(ctx, *grad_output): 21 | return grad_output * -ctx.lambd, None 22 | 23 | # For pytorch version 1.0 24 | # Usage: 25 | # grl = GradientReverseLayer(1) # 1 is the lambda value, you are free to set it 26 | # b = GradReverse(a) 27 | class GradientReverseLayer(torch.autograd.Function): 28 | def __init__(self, lambd=1): 29 | self.lambd = lambd 30 | 31 | def forward(self, input): 32 | output = input * 1.0 33 | return output 34 | 35 | def backward(self, grad_output): 36 | return -self.lambd * grad_output 37 | -------------------------------------------------------------------------------- /doc/domain_adaptation.md: -------------------------------------------------------------------------------- 1 | ### Domain adaptation介绍及代表性文章梳理 2 | 3 | Domain adaptation,DA,中文可翻译为域适配、域匹配、域适应,是迁移学习中的一类非常重要的问题,也是一个持续的研究热点。Domain adaptation可用于计算机视觉、物体识别、文本分类、声音识别等常见应用中。这个问题的基本定义是,假设源域和目标域的类别空间一样,特征空间也一样,但是数据的分布不一样,如何利用有标定的源域数据,来学习目标域数据的标定? 4 | 5 | 事实上,根据目标域中是否有少量的标定可用,可以将domain adaptation大致分为无监督(目标域中完全无label)和半监督(目标域中有少量label)两大类。我们这里偏重介绍无监督。 6 | 7 | [视觉domain adaptation综述](https://mega.nz/#!hWQ3HLhJ!GTCIUTVDcmnn3f7-Ulhjs_MxGv6xnFyp1nayemt9Nis) 8 | 9 | 关于迁移学习的理论方面,有三篇连贯式的理论分析文章连续发表在NIPS和Machine Learning上:[理论分析](https://mega.nz/#F!ULoGFYDK!O3TQRZwrNeqTncNMIfXNTg) 10 | - - - 11 | 12 | #### 形式化 13 | 14 | 给定:有标定的,以及无标定的 15 | 16 | 求:的标定 (在实验环境中,是有标定的,仅用来测试算法精度) 17 | 18 | 条件: 19 | - ,即源域和目标域的特征空间相同(都是维) 20 | - ,即源域和目标域的类别空间相同 21 | - ) ),即源域和目标域的数据分布不同 22 | 23 | - - - 24 | 25 | #### 例子 26 | 27 | 比如说,同样都是一台电脑,在不同角度,不同光照,以及不同背景下拍照,图像的数据具有不同的分布,但是从根本上来说,都是一台电脑的图像。Domain adaptation要做的就是,如何根据这些不同分布的数据,很好地学习缺失的标定。 28 | 29 |  30 | 31 | 代表方法和文章请见这里:https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#deep-domain-adaptation 32 | -------------------------------------------------------------------------------- /doc/迁移学习简介.md: -------------------------------------------------------------------------------- 1 | ### 迁移学习简介 2 | 3 | 迁移学习(transfer learning)通俗来讲,就是运用已有的知识来学习新的知识,核心是找到已有知识和新知识之间的相似性,用成语来说就是举一反三。由于直接对目标域从头开始学习成本太高,我们故而转向运用已有的相关知识来辅助尽快地学习新知识。比如,已经会下中国象棋,就可以类比着来学习国际象棋;已经会编写Java程序,就可以类比着来学习C#;已经学会英语,就可以类比着来学习法语;等等。世间万事万物皆有共性,如何合理地找寻它们之间的相似性,进而利用这个桥梁来帮助学习新知识,是迁移学习的核心问题。 4 | 5 |  6 | 7 | 图1不同位置、不同传感器的迁移标定。已知一个房间中A点的WiFi信号与相应的人体行为,如何标定另一个房间中C点的蓝牙信号? 8 | 9 | 具体地,在迁移学习中,我们已有的知识叫做源域(source domain),要学习的新知识叫目标域(target domain)。迁移学习研究如何把源域的知识迁移到目标域上。特别地,在机器学习领域中,迁移学习研究如何将已有模型应用到新的不同的、但是有一定关联的领域中。传统机器学习在应对数据的分布、维度,以及模型的输出变化等任务时,模型不够灵活、结果不够好,而迁移学习放松了这些假设。在数据分布、特征维度以及模型输出变化条件下,有机地利用源域中的知识来对目标域更好地建模。另外,在有标定数据缺乏的情况下,迁移学习可以很好地利用相关领域有标定的数据完成数据的标定。 10 | 11 |  12 | 13 | 图2 迁移学习与传统机器学习的不同。(a)传统机器学习对不同的学习任务建立不同的模型,(b)迁移学习利用源域中的数据将知识迁移到目标域,完成模型建立。插图来自:Sinno Jialin Pan and Qiang Yang, A survey on transfer learning. IEEE TKDE 2010. 14 | 15 | 迁移学习按照学习方式可以分为基于样本的迁移,基于特征的迁移,基于模型的迁移,以及基于关系的迁移。基于样本的迁移通过对源域中有标定样本的加权利用完成知识迁移;基于特征的迁移通过将源域和目标域映射到相同的空间(或者将其中之一映射到另一个的空间中)并最小化源域和目标域的距离来完成知识迁移;基于模型的迁移将源域和目标域的模型与样本结合起来调整模型的参数;基于关系的迁移则通过在源域中学习概念之间的关系,然后将其类比到目标域中,完成知识的迁移。 16 | 17 | 理论上,任何领域之间都可以做迁移学习。但是,如果源域和目标域之间相似度不够,迁移结果并不会理想,出现所谓的负迁移情况。比如,一个人会骑自行车,就可以类比学电动车;但是如果类比着学开汽车,那就有点天方夜谭了。如何找到相似度尽可能高的源域和目标域,是整个迁移过程最重要的前提。 18 | 19 | 迁移学习方面,代表人物有香港科技大学的Qiang Yang教授,南洋理工大学的Sinno Jialin Pan,以及第四范式的CEO戴文渊等。代表文献是Sinno Jialin Pan和Qiang Yang的A survey on transfer learning。 20 | 21 | 作者网站:http://jd92.wang. 22 | 23 | [参考资料] 24 | 25 | [1] Pan S J, Yang Q. A survey on transfer learning[J]. IEEE Transactions on knowledge and data engineering, 2010, 22(10): 1345-1359. 26 | 27 | [2] Introduction to Transfer Learning: http://jd92.wang/assets/files/l03_transferlearning.pdf。 28 | 29 | [3] Qiang Yang: http://www.cse.ust.hk/~qyang/. 30 | 31 | [4] Sinno Jialin Pan: http://www.ntu.edu.sg/home/sinnopan/. 32 | 33 | [5] Wenyuan Dai: https://scholar.google.com/citations?user=AGR9pP0AAAAJ&hl=zh-CN. 34 | 35 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | transferlearning.xyz -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | remote_theme: just-the-docs/just-the-docs 2 | title: Transfer Learning 3 | # description: Dates to be announced. 4 | search_enabled: false 5 | heading_anchors: true -------------------------------------------------------------------------------- /png/domain _adaptation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/png/domain _adaptation.png -------------------------------------------------------------------------------- /png/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/png/logo.jpg -------------------------------------------------------------------------------- /png/matlab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/png/matlab.png -------------------------------------------------------------------------------- /png/result_office_caltech_decaf.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/png/result_office_caltech_decaf.jpg -------------------------------------------------------------------------------- /png/tf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/png/tf.png -------------------------------------------------------------------------------- /png/tf2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jindongwang/transferlearning/f951df1188858f6eb561b89e37deb93295ff1c82/png/tf2.png --------------------------------------------------------------------------------