Full Code of jindongwang/transferlearning for AI

master d77ef50a2e7e cached
461 files
3.2 MB
871.2k tokens
1793 symbols
1 requests
Download .txt
Showing preview only (3,471K chars total). Download the full file or copy to clipboard to get everything.
Repository: jindongwang/transferlearning
Branch: master
Commit: d77ef50a2e7e
Files: 461
Total size: 3.2 MB

Directory structure:
gitextract_9o7g7l9e/

├── .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
│   │   │   ├── 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
│   │   │   ├── 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
│   │   │   ├── 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
│   │       ├── 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
│   │   ├── 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

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitattributes
================================================
*.pdf filter=lfs diff=lfs merge=lfs -text


================================================
FILE: .github/FUNDING.yml
================================================
# These are supported funding model platforms

github: jindongwang
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']


================================================
FILE: .github/ISSUE_TEMPLATE/bug_report.md
================================================
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Before you open an issue**
If it's code running error, maybe you want to check the python or pytorch version before submitting an issue.

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Where is this bug happen?

**Screenshots**
If applicable, add screenshots to help explain your problem.


================================================
FILE: .gitignore
================================================
.idea/
*~
*/*~
code/.DS_Store
.DS_Store
*.pyc
*.pkl
outputs/
__pycache__/


================================================
FILE: CONTRIBUTING.md
================================================
Anyone interested in transfer learning is welcomed to contribute to this repo (by pull request):

- You can add the latest publications / tools / tutorials directly to `readme.md` and `awesome_paper.md`.
- You can add **code** to the code directory. You are welcomed to place the code of your published paper in this repo!
- You are welcomed to update anything helpful.

如果你对本项目感兴趣,非常欢迎你加入!

- 可以推荐最新的相关论文/工具/讲座,将信息通过pull request的方式更新到`readme.md`和`awesome_paper.md`中。
- 推荐代码到**code**文件夹中。欢迎将你论文中的代码开源到本项目中!
- 任何有用的建议都可进行贡献。

欢迎!

================================================
FILE: LICENSE
================================================
MIT License

Copyright (c) 2018 Jindong Wang

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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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.


================================================
FILE: README.md
================================================
[![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url]
[![Stargazers][stars-shield]][stars-url]
[![Issues][issues-shield]][issues-url]

<h1 align="center">
  <br>
  <img src="png/logo.jpg" alt="Transfer Leanring" width="500">
</h1>

<h4 align="center">Everything about Transfer Learning. 迁移学习.</h4>

<p align="center">
  <strong><a href="#0papers-论文">Papers</a></strong> •
  <strong><a href="#1introduction-and-tutorials-简介与教程">Tutorials</a></strong> •
  <a href="#2transfer-learning-areas-and-papers-研究领域与相关论文">Research areas</a> •
  <a href="#3theory-and-survey-理论与综述">Theory</a> •
  <a href="#3theory-and-survey-理论与综述">Survey</a> •
  <strong><a href="https://github.com/jindongwang/transferlearning/tree/master/code">Code</a></strong> •
  <strong><a href="#7datasets-and-benchmarks-数据集与评测结果">Dataset & benchmark</a></strong>
</p>
<p align="center">
  <a href="#6transfer-learning-thesis-硕博士论文">Thesis</a> •
  <a href="#5transfer-learning-scholars-著名学者">Scholars</a> •
  <a href="#8transfer-learning-challenges-迁移学习比赛">Contests</a> •
  <a href="#journals-and-conferences">Journal/conference</a> •
  <a href="#applications-迁移学习应用">Applications</a> •
  <a href="#other-resources-其他资源">Others</a> •
  <a href="#contributing-欢迎参与贡献">Contributing</a>
</p>

**Widely used by top conferences and journals:** 
- Conferences: [[CVPR'22](https://openaccess.thecvf.com/content/CVPR2022W/FaDE-TCV/html/Zhang_Segmenting_Across_Places_The_Need_for_Fair_Transfer_Learning_With_CVPRW_2022_paper.html)] [[NeurIPS'21](https://proceedings.neurips.cc/paper/2021/file/731b03008e834f92a03085ef47061c4a-Paper.pdf)] [[IJCAI'21](https://arxiv.org/abs/2103.03097)] [[ESEC/FSE'20](https://dl.acm.org/doi/abs/10.1145/3368089.3409696)] [[IJCNN'20](https://ieeexplore.ieee.org/abstract/document/9207556)] [[ACMMM'18](https://dl.acm.org/doi/abs/10.1145/3240508.3240512)] [[ICME'19](https://ieeexplore.ieee.org/abstract/document/8784776/)]
- Journals: [[IEEE TKDE](https://ieeexplore.ieee.org/abstract/document/9782500/)] [[ACM TIST](https://dl.acm.org/doi/abs/10.1145/3360309)] [[Information sciences](https://www.sciencedirect.com/science/article/pii/S0020025520308458)] [[Neurocomputing](https://www.sciencedirect.com/science/article/pii/S0925231221007025)] [[IEEE Transactions on Cognitive and Developmental Systems](https://ieeexplore.ieee.org/abstract/document/9659817)]

```
@Misc{transferlearning.xyz,
howpublished = {\url{http://transferlearning.xyz}},   
title = {Everything about Transfer Learning and Domain Adapation},  
author = {Wang, Jindong and others}  
}  
```

[![Awesome](https://awesome.re/badge.svg)](https://awesome.re) [![MIT License](https://img.shields.io/badge/license-MIT-green.svg)](https://opensource.org/licenses/MIT) [![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE) [![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu) 

Related Codes:
  - Large language model evaluation: [[llm-eval](https://llm-eval.github.io/)]
  - Large language model enhancement: [[llm-enhance](https://llm-enhance.github.io/)]
  - Robust machine learning: [[robustlearn: robust machine learning](https://github.com/microsoft/robustlearn)]
  - Semi-supervised learning: [[USB: unified semi-supervised learning benchmark](https://github.com/microsoft/Semi-supervised-learning)] | [[TorchSSL: a unified SSL library](https://github.com/TorchSSL/TorchSSL)] 
  - LLM benchmark: [[PromptBench: adversarial robustness of prompts of LLMs](https://github.com/microsoft/promptbench)]
  - Federated learning: [[PersonalizedFL: library for personalized federated learning](https://github.com/microsoft/PersonalizedFL)]
  - Activity recognition and machine learning [[Activity recognition](https://github.com/jindongwang/activityrecognition)]|[[Machine learning](https://github.com/jindongwang/MachineLearning)]

- - -

**NOTE:** You can directly open the code in [Gihub Codespaces](https://docs.github.com/en/codespaces/getting-started/quickstart#introduction) on the web to run them without downloading! Also, try [github.dev](https://github.dev/jindongwang/transferlearning).

## 0.Papers (论文)

[Awesome transfer learning papers (迁移学习文章汇总)](https://github.com/jindongwang/transferlearning/tree/master/doc/awesome_paper.md)

- [Paperweekly](http://www.paperweekly.site/collections/231/papers): A website to recommend and read paper notes

**Latest papers**: 

- By topic: [doc/awesome_papers.md](/doc/awesome_paper.md)
- By date: [doc/awesome_paper_date.md](/doc/awesome_paper_date.md)

*Updated at 2024-02-18:*

- Simulations of Common Unsupervised Domain Adaptation Algorithms for Image Classification [[arxiv](https://arxiv.org/abs/2502.10694)]
  - Unsupervised domain adaptaiton for image classification

- Semantics-aware Test-time Adaptation for 3D Human Pose Estimation [[arxiv](https://arxiv.org/abs/2502.10724)]
  - Test-time adaptation for3D human pose estimation

- Transfer Learning of CATE with Kernel Ridge Regression [[arxiv](https://arxiv.org/abs/2502.11331)]
  - Transfer learning with kernel ridge regression

- Why Domain Generalization Fail? A View of Necessity and Sufficiency [[arxiv](https://arxiv.org/abs/2502.10716)] 
  - Analyze why domain generalization fail from the view of necessity and sufficiency


*Updated at 2024-02-11:*

- Beyond Batch Learning: Global Awareness Enhanced Domain Adaptation [[arxiv](https://arxiv.org/abs/2502.06272)]
  - Global awareness for enhanced domain adaptation

- - -

## 1.Introduction and Tutorials (简介与教程)

Want to quickly learn transfer learning?想尽快入门迁移学习?看下面的教程。

- Books 书籍
  - **Introduction to Transfer Learning: Algorithms and Practice** [[Buy or read](https://link.springer.com/book/9789811975837)]
  - **《迁移学习》(杨强)** [[Buy](https://item.jd.com/12930984.html)] [[English version](https://www.cambridge.org/core/books/transfer-learning/CCFFAFE3CDBC245047F1DEC71D9EF3C7)]
  - **《迁移学习导论》(王晋东、陈益强著)** [[Homepage](http://jd92.wang/tlbook)] [[Buy](https://item.jd.com/13272157.html)]

- Blogs 博客
  - [Zhihu blogs - 知乎专栏《小王爱迁移》系列文章](https://zhuanlan.zhihu.com/p/130244395)
	
- Video tutorials 视频教程
  - Transfer learning 迁移学习:
    - [Recent advance of transfer learning - 2022年最新迁移学习发展现状探讨](https://www.bilibili.com/video/BV1nY411E7Uc/)
    - [Definitions of transfer learning area - 迁移学习领域名词解释](https://www.bilibili.com/video/BV1fu411o7BW) [[Article](https://zhuanlan.zhihu.com/p/428097044)]
    - [Transfer learning by Hung-yi Lee @ NTU - 台湾大学李宏毅的视频讲解(中文视频)](https://www.youtube.com/watch?v=qD6iD4TFsdQ)
  - Domain generalization 领域泛化:
    - [IJCAI-ECAI'22 tutorial on domain generalization - 领域泛化tutorial](https://dgresearch.github.io/)
    - [Domain generalization - 迁移学习新兴研究方向领域泛化](https://www.bilibili.com/video/BV1ro4y1S7dd/)
  - Domain adaptation 领域自适应:
    - [Domain adaptation - 迁移学习中的领域自适应方法(中文)](https://www.bilibili.com/video/BV1T7411R75a/) 
  

- Brief introduction and slides 简介与ppt资料
  - [Recent advance of transfer learning](https://jd92.wang/assets/files/l16_aitime.pdf)
  - [Domain generalization survey](http://jd92.wang/assets/files/DGSurvey-ppt.pdf)
  - [Brief introduction in Chinese](https://github.com/jindongwang/transferlearning/blob/master/doc/%E8%BF%81%E7%A7%BB%E5%AD%A6%E4%B9%A0%E7%AE%80%E4%BB%8B.md)
	- [PPT (English)](http://jd92.wang/assets/files/l03_transferlearning.pdf) | [PPT (中文)](http://jd92.wang/assets/files/l08_tl_zh.pdf)
  - 迁移学习中的领域自适应方法 Domain adaptation: [PDF](http://jd92.wang/assets/files/l12_da.pdf) | [Video on Bilibili](https://www.bilibili.com/video/BV1T7411R75a/) | [Video on Youtube](https://www.youtube.com/watch?v=RbIsHNtluwQ&t=22s)
  - Tutorial on transfer learning by Qiang Yang: [IJCAI'13](http://ijcai13.org/files/tutorial_slides/td2.pdf) | [2016 version](http://kddchina.org/file/IntroTL2016.pdf)

- Talk is cheap, show me the code 动手教程、代码、数据 
  - [Pytorch tutorial on transfer learning](https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html)
	- [Pytorch finetune](https://github.com/jindongwang/transferlearning/tree/master/code/AlexNet_ResNet)
	- [DeepDA: a unified deep domain adaptation toolbox](https://github.com/jindongwang/transferlearning/tree/master/code/DeepDA)
	- [DeepDG: a unified deep domain generalization toolbox](https://github.com/jindongwang/transferlearning/tree/master/code/DeepDG)
	- [更多 More...](https://github.com/jindongwang/transferlearning/tree/master/code)

- [Transfer Learning Scholars and Labs - 迁移学习领域的著名学者、代表工作及实验室介绍](https://github.com/jindongwang/transferlearning/blob/master/doc/scholar_TL.md)
- [Negative transfer - 负迁移](https://www.zhihu.com/question/66492194/answer/242870418)

- - -

## 2.Transfer Learning Areas and Papers (研究领域与相关论文)

- [Survey](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#survey)
- [Theory](#theory)
- [Per-training/Finetuning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#per-trainingfinetuning)
- [Knowledge distillation](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#knowledge-distillation)
- [Traditional domain adaptation](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#traditional-domain-adaptation)
- [Deep domain adaptation](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#deep-domain-adaptation)
- [Domain generalization](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#domain-generalization)
- [Source-free domain adaptation](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#source-free-domain-adaptation)
- [Multi-source domain adaptation](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#multi-source-domain-adaptation)
- [Heterogeneous transfer learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#heterogeneous-transfer-learning)
- [Online transfer learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#online-transfer-learning)
- [Zero-shot / few-shot learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#zero-shot--few-shot-learning)
- [Multi-task learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#multi-task-learning)
- [Transfer reinforcement learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#transfer-reinforcement-learning)
- [Transfer metric learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#transfer-metric-learning)
- [Federated transfer learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#federated-transfer-learning)
- [Lifelong transfer learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#lifelong-transfer-learning)
- [Safe transfer learning](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#safe-transfer-learning)
- [Transfer learning applications](https://github.com/jindongwang/transferlearning/blob/master/doc/awesome_paper.md#transfer-learning-applications)

- - -

## 3.Theory and Survey (理论与综述)

Here are some articles on transfer learning theory and survey.

**Survey (综述文章):**

- 2023 Source-Free Unsupervised Domain Adaptation: A Survey [[arxiv](http://arxiv.org/abs/2301.00265)]
- 2022 [Transfer Learning for Future Wireless Networks: A Comprehensive Survey](https://arxiv.org/abs/2102.07572)
- 2022 [A Review of Deep Transfer Learning and Recent Advancements](https://arxiv.org/abs/2201.09679)
- 2022 [Transferability in Deep Learning: A Survey](https://paperswithcode.com/paper/transferability-in-deep-learning-a-survey), from Mingsheng Long in THU.
- 2021 Domain generalization: IJCAI-21 [Generalizing to Unseen Domains: A Survey on Domain Generalization](https://arxiv.org/abs/2103.03097) | [知乎文章](https://zhuanlan.zhihu.com/p/354740610) | [微信公众号](https://mp.weixin.qq.com/s/DsoVDYqLB1N7gj9X5UnYqw)
  - First survey on domain generalization
  - 第一篇对Domain generalization (领域泛化)的综述
- 2021 Vision-based activity recognition: [A Survey of Vision-Based Transfer Learning in Human Activity Recognition](https://www.mdpi.com/2079-9292/10/19/2412)
- 2021 ICSAI [A State-of-the-Art Survey of Transfer Learning in Structural Health Monitoring](https://ieeexplore.ieee.org/abstract/document/9664171)
- 2020 [Transfer learning: survey and classification](https://link.springer.com/chapter/10.1007/978-981-15-5345-5_13), Advances in Intelligent Systems and Computing. 
- 2020 迁移学习最新survey,来自中科院计算所庄福振团队,发表在Proceedings of the IEEE: [A Comprehensive Survey on Transfer Learning](https://arxiv.org/abs/1911.02685)
- 2020 负迁移的综述:[Overcoming Negative Transfer: A Survey](https://arxiv.org/abs/2009.00909)
- 2020 知识蒸馏的综述: [Knowledge Distillation: A Survey](https://arxiv.org/abs/2006.05525)
- 用transfer learning进行sentiment classification的综述:[A Survey of Sentiment Analysis Based on Transfer Learning](https://ieeexplore.ieee.org/abstract/document/8746210) 
- 2019 一篇新survey:[Transfer Adaptation Learning: A Decade Survey](https://arxiv.org/abs/1903.04687)
- 2018 一篇迁移度量学习的综述: [Transfer Metric Learning: Algorithms, Applications and Outlooks](https://arxiv.org/abs/1810.03944)
- 2018 一篇最近的非对称情况下的异构迁移学习综述:[Asymmetric Heterogeneous Transfer Learning: A Survey](https://arxiv.org/abs/1804.10834)
- 2018 Neural style transfer的一个survey:[Neural Style Transfer: A Review](https://arxiv.org/abs/1705.04058)
- 2018 深度domain adaptation的一个综述:[Deep Visual Domain Adaptation: A Survey](https://www.sciencedirect.com/science/article/pii/S0925231218306684)
- 2017 多任务学习的综述,来自香港科技大学杨强团队:[A survey on multi-task learning](https://arxiv.org/abs/1707.08114)
- 2017 异构迁移学习的综述:[A survey on heterogeneous transfer learning](https://link.springer.com/article/10.1186/s40537-017-0089-0)
- 2017 跨领域数据识别的综述:[Cross-dataset recognition: a survey](https://arxiv.org/abs/1705.04396)
- 2016 [A survey of transfer learning](https://pan.baidu.com/s/1gfgXLXT)。其中交代了一些比较经典的如同构、异构等学习方法代表性文章。
- 2015 中文综述:[迁移学习研究进展](https://pan.baidu.com/s/1bpautob)
- 2010 [A survey on transfer learning](http://ieeexplore.ieee.org/abstract/document/5288526/)
- Survey on applications - 应用导向的综述:
	- 视觉domain adaptation综述:[Visual Domain Adaptation: A Survey of Recent Advances](https://pan.baidu.com/s/1o8BR7Vc)
	- 迁移学习应用于行为识别综述:[Transfer Learning for Activity Recognition: A Survey](https://pan.baidu.com/s/1kVABOYr)
	- 迁移学习与增强学习:[Transfer Learning for Reinforcement Learning Domains: A Survey](https://pan.baidu.com/s/1slfr0w1)
	- 多个源域进行迁移的综述:[A Survey of Multi-source Domain Adaptation](https://pan.baidu.com/s/1eSGREF4)。

**Theory (理论文章):**

- ICML-20 [Few-shot domain adaptation by causal mechanism transfer](https://arxiv.org/pdf/2002.03497.pdf)
	- The first work on causal transfer learning
	- 日本理论组大佬Sugiyama的工作,causal transfer learning
- CVPR-19 [Characterizing and Avoiding Negative Transfer](https://arxiv.org/abs/1811.09751)
	- Characterizing and avoid negative transfer
	- 形式化并提出如何避免负迁移
- ICML-20 [On Learning Language-Invariant Representations for Universal Machine Translation](https://arxiv.org/abs/2008.04510)
  - Theory for universal machine translation
  - 对统一机器翻译模型进行了理论论证
- NIPS-06 [Analysis of Representations for Domain Adaptation](https://dl.acm.org/citation.cfm?id=2976474)
- ML-10 [A Theory of Learning from Different Domains](https://link.springer.com/article/10.1007/s10994-009-5152-4)
- NIPS-08 [Learning Bounds for Domain Adaptation](http://papers.nips.cc/paper/3212-learning-bounds-for-domain-adaptation)
- COLT-09 [Domain adaptation: Learning bounds and algorithms](https://arxiv.org/abs/0902.3430)
- MMD paper:[A Hilbert Space Embedding for Distributions](https://link.springer.com/chapter/10.1007/978-3-540-75225-7_5) and [A Kernel Two-Sample Test](http://www.jmlr.org/papers/v13/gretton12a.html)
- Multi-kernel MMD paper: [Optimal kernel choice for large-scale two-sample tests](http://papers.nips.cc/paper/4727-optimal-kernel-choice-for-large-scale-two-sample-tests)

_ _ _

## 4.Code (代码)

Unified codebases for:
- [Deep domain adaptation](https://github.com/jindongwang/transferlearning/tree/master/code/DeepDA)
- [Deep domain generalization](https://github.com/jindongwang/transferlearning/tree/master/code/DeepDG)
- See all codes here: https://github.com/jindongwang/transferlearning/tree/master/code.

More: see [HERE](https://github.com/jindongwang/transferlearning/tree/master/code) and [HERE](https://colab.research.google.com/drive/1MVuk95mMg4ecGyUAIG94vedF81HtWQAr?usp=sharing) for an instant run using Google's Colab.

_ _ _

## 5.Transfer Learning Scholars (著名学者)

Here are some transfer learning scholars and labs.

**全部列表以及代表工作性见[这里](https://github.com/jindongwang/transferlearning/blob/master/doc/scholar_TL.md)** 

Please note that this list is far not complete. A full list can be seen in [here](https://github.com/jindongwang/transferlearning/blob/master/doc/scholar_TL.md). Transfer learning is an active field. *If you are aware of some scholars, please add them here.*

_ _ _

## 6.Transfer Learning Thesis (硕博士论文)

Here are some popular thesis on transfer learning.

[这里](https://pan.baidu.com/share/init?surl=iuzZhHdumrD64-yx_VAybA), 提取码:txyz。

- - -

## 7.Datasets and Benchmarks (数据集与评测结果)

Please see [HERE](https://github.com/jindongwang/transferlearning/blob/master/data) for the popular transfer learning **datasets and benchmark** results.

[这里](https://github.com/jindongwang/transferlearning/blob/master/data)整理了常用的公开数据集和一些已发表的文章在这些数据集上的实验结果。

- - -

## 8.Transfer Learning Challenges (迁移学习比赛)

- [Visual Domain Adaptation Challenge (VisDA)](http://ai.bu.edu/visda-2018/)

- - -

## Journals and Conferences

See [here](https://github.com/jindongwang/transferlearning/blob/master/doc/venues.md) for a full list of related journals and conferences.

- - -

## Applications (迁移学习应用)

- [Computer vision](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#computer-vision)
- [Medical and healthcare](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#medical-and-healthcare)
- [Natural language processing](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#natural-language-processing)
- [Time series](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#time-series)
- [Speech](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#speech)
- [Multimedia](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#multimedia)
- [Recommendation](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#recommendation)
- [Human activity recognition](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#human-activity-recognition)
- [Autonomous driving](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#autonomous-driving)
- [Others](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md#others)

See [HERE](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md) for transfer learning applications.

迁移学习应用请见[这里](https://github.com/jindongwang/transferlearning/blob/master/doc/transfer_learning_application.md)。

- - -

## Other Resources (其他资源)

- Call for papers:
  - [Advances in Transfer Learning: Theory, Algorithms, and Applications](https://www.frontiersin.org/research-topics/21133/advances-in-transfer-learning-theory-algorithms-and-applications), DDL: October 2021

- Related projects:
  - Salad: [A semi-supervised domain adaptation library](https://domainadaptation.org)

- - -

## Contributing (欢迎参与贡献)

If you are interested in contributing, please refer to [HERE](https://github.com/jindongwang/transferlearning/blob/master/CONTRIBUTING.md) for instructions in contribution.

- - -

### Copyright notice

> ***[Notes]This Github repo can be used by following the corresponding licenses. I want to emphasis that it may contain some PDFs or thesis, which were downloaded by me and can only be used for academic purposes. The copyrights of these materials are owned by corresponding publishers or organizations. All this are for better academic research. If any of the authors or publishers have concerns, please contact me to delete or replace them.***

[contributors-shield]: https://img.shields.io/github/contributors/jindongwang/transferlearning.svg?style=for-the-badge
[contributors-url]: https://github.com/jindongwang/transferlearning/graphs/contributors
[forks-shield]: https://img.shields.io/github/forks/jindongwang/transferlearning.svg?style=for-the-badge
[forks-url]: https://github.com/jindongwang/transferlearning/network/members
[stars-shield]: https://img.shields.io/github/stars/jindongwang/transferlearning.svg?style=for-the-badge
[stars-url]: https://github.com/jindongwang/transferlearning/stargazers
[issues-shield]: https://img.shields.io/github/issues/jindongwang/transferlearning.svg?style=for-the-badge
[issues-url]: https://github.com/jindongwang/transferlearning/issues
[license-shield]: https://img.shields.io/github/license/jindongwang/transferlearning.svg?style=for-the-badge
[license-url]: https://github.com/jindongwang/transferlearning/blob/main/LICENSE.txt


================================================
FILE: code/ASR/Adapter/README.md
================================================
# Adapter-based Cross-lingual ASR with EasyEspnet

**NOTICE:** The latest code of Adapter has been moved to https://github.com/microsoft/NeuralSpeech/tree/master/AdapterASR for unified organization. Please refer to that repo.

---

This is a Adapter-based cross-lingual ASR implementation of our works [1, 2] built on top of [EasyEspnet](https://github.com/jindongwang/EasyEspnet). Please refer to it for the basic introduction, installation and usage.

## Run

After extracting features using Espnet and set the data folder path as introduced in [EasyEspnet](https://github.com/jindongwang/EasyEspnet).

You need to check or modify in `train.py arg_list`, config should be in ESPnet config style (remember to include decoding information if you want to compute cer/wer), then, you can run train.py. For example, 

```
python train.py --root_path commonvoice/asr1 --dataset ar --config config/adapter_example.yaml
```

dataset here refers to the language code used in the Common Voice corpus. Results (log, model, snapshots) are saved in results_(dataset)/(config_name) by default.

### Adapters

Adapters is a parameter-efficient way for cross-lingual ASR as introduced in [1, 2]. Given a pre-trained multilingual ASR model, the adapters are injected into the model, during adaptation, only the adapters and language-specific heads are updated, while the main body of the model is frozen, resulting in much faster training speed and making the adaptation more stable. Please refer to our SimAdapter paper [2] for details.

To train the adapters, generally you need to specify the `load_pretrained_model` in the config files to load the multilingual ASR model. We found that by splitting the training of language-specific heads and adapters, the adaptation performance can be further improved [2]. You may also train the language-specific heads first and set `train_adapter_with_head` to `false` during adapters' training.

For the vanilla Adapter method, we provide an example config in `config/adapter_example.yaml`, please refer to it for modification to train your own adapter.

For the Meta-Adapter, there are two stages, the first step is pre-training, we provide an example config in `config/meta_adapter_example.yaml`, please refer to it for modification to train your own meta-adapter; the second stage is fine-tuning, please refer to the `config/finetune_meta_adapter_example.yaml`. Please refer to our meta-adapter paper [1] for details.

### SimAdapter

To leverage the information from multiple source / target adapters, we introduce SimAdapter for cross-lingual ASR [2]. To train a SimAdapter, you need a model with multiple adapters/meta-adapters injected and pre-trained. During SimAdapter, we initialize attention layers after every adapter outputs to fuse the information from multiple adapters (languages). During training of SimAdapter, parameters of the main body, the adapters, and the language heads are all frozen. Only the SimAdapter's fusion layers are trained. Please refer to our SimAdapter paper [2] for details.

We also provide a template config for you to perform SimAdapter: `config/simadapter_example.yaml`

### Demo

TBD

## Decoding and WER/CER evaluation

Set `--decoding_mode` to `true` to perform decoding and CER/WER evaluation. For example:

```
python train.py --root_path commonvoice/asr1 --dataset ar --decoding_mode true --config config/adapter_example.yaml
```

## Distributed training

Following EasyEspnet, you can also perform distributed training which is much faster. For example, using 4 GPUs, 1 node: 

```
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m torch.distributed.launch --nproc_per_node=4 train.py --dist_train true --root_path commonvoice/asr1 --dataset ar --config config/adapter_example.yaml
```

## Acknowledgement

- ESPNet: https://github.com/espnet/espnet
- EasyEspnet: https://github.com/jindongwang/EasyEspnet

## Contact

- [Wenxin Hou](https://houwenxin.github.io/): houwx001@gmail.com
- [Jindong Wang](http://www.jd92.wang/): jindongwang@outlook.com



## References

[1] Wenxin Hou, Yidong Wang, Shengzhou Gao, Takahiro Shinozaki, “Meta-Adapter: Efficient Cross-Lingual Adaptation with Meta-Learning”, in Proc. IEEE International Conference on Acoustics, Speech, and Signal Processing (ICASSP), Toronto, Ontario, Canada, June 2021. [[paper]](https://ieeexplore.ieee.org/document/9414959)

[2] Wenxin Hou, Han Zhu, Yidong Wang, Jindong Wang, Tao Qin, Renjun Xu, Takahiro Shinozaki, ”Exploiting Adapters for Cross-lingual Low-resource Speech Recognition”, Arxiv preprint. [[paper]](https://arxiv.org/abs/2105.11905)

================================================
FILE: code/ASR/Adapter/balanced_sampler.py
================================================
import torch
import torch.utils.data
import random
import collections
import logging

import numpy as np
from torch.utils.data.sampler import BatchSampler, WeightedRandomSampler

# https://github.com/khornlund/pytorch-balanced-sampler
class BalancedBatchSampler(torch.utils.data.sampler.Sampler):
    '''
    https://github.com/galatolofederico/pytorch-balanced-batch/blob/master/sampler.py
    '''
    def __init__(self, dataset, labels=None):
        self.labels = labels
        self.dataset = collections.defaultdict(list)
        self.balanced_max = 0
        # Save all the indices for all the classes
        for idx in range(0, len(dataset)):
            label = self._get_label(dataset, idx)
            #break
            self.dataset[label].append(idx)
            self.balanced_max = max(self.balanced_max, len(self.dataset[label]))
            #len(self.dataset[label]) if len(self.dataset[label]) > self.balanced_max else self.balanced_max
        
        # Oversample the classes with fewer elements than the max
        for label in self.dataset:
            while len(self.dataset[label]) < self.balanced_max:
                self.dataset[label].append(random.choice(self.dataset[label]))
        self.keys = list(self.dataset.keys())
        logging.warning(self.keys)
        self.currentkey = 0
        self.indices = [-1] * len(self.keys)

    def __iter__(self):
        while self.indices[self.currentkey] < self.balanced_max - 1:
            self.indices[self.currentkey] += 1
            yield self.dataset[self.keys[self.currentkey]][self.indices[self.currentkey]]
            self.currentkey = (self.currentkey + 1) % len(self.keys)
        self.indices = [-1] * len(self.keys)
    
    def _get_label(self, dataset, idx):
        #logging.warning(len(dataset))
        # logging.warning(dataset[idx])
        return dataset[idx][0][1]['category']#[1]['output'][0]['token'].split(' ')[1]
    # def _get_label(self, dataset, idx, labels = None):
    #     if self.labels is not None:
    #         return self.labels[idx].item()
    #     else:
    #         raise Exception("You should pass the tensor of labels to the constructor as second argument")

    def __len__(self):
        return self.balanced_max * len(self.keys)



================================================
FILE: code/ASR/Adapter/config/adapter_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
# opt: noam
accum-grad: 1
grad-clip: 5
patience: 10
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
transformer-lr: 6.0
transformer-warmup-steps: 1800
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

use_adapters: true
load_pretrained_model: "<path-to-your-pretrained-model>:<modules-to-load>:<excluded-modules>" # Example: lid_42.pt::decoder.embed,decoder.output_layer,ctc.
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)
adapter_train_languages: "ar" # The language(s) that is really used for this time
adapter_fusion: false
train_adapter_with_head: true

opt: adam
adam_lr: 0.001


================================================
FILE: code/ASR/Adapter/config/adapterfusion_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
accum-grad: 1
grad-clip: 5
patience: 10
epochs: 200
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
transformer-lr: 0.1
transformer-warmup-steps: 1800
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

use_adapters: true
load_pretrained_model: "<path-to-model-with-adapters>:<modules-to-load>:<excluded-modules>"  # Example: lid_42_adapters.pt::
dataset: "ru_cy_it_eu_pt_ar"
fusion_languages: "ru_cy_it_eu_pt_ar" # adapters to fuse
adapter_train_languages: "ar"
sim_adapter: true
train_adapter_with_head: false

guide_loss_weight: 1.0
guide_loss_weight_decay_steps: 0
opt: adam
adam_lr: 0.001

================================================
FILE: code/ASR/Adapter/config/finetune_meta_adapter_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 10
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
transformer-lr: 6.0
transformer-warmup-steps: 1800
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

use_adapters: true
load_pretrained_model: "<path-to-model-with-adapters>:<modules-to-load>:<excluded-modules>"  # Example: lid_42_meta_adapter.pt::
adapter_train_languages: "ar"
shared_adapter: "ru_cy_it_eu_pt" # Set to the source languages on which it is meta-trained
dataset: ru_cy_it_eu_pt_ar
adapter_fusion: false
train_adapter_with_head: true
# cs
opt: adam
adam_lr: 0.01

================================================
FILE: code/ASR/Adapter/config/meta_adapter_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related

maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
accum-grad: 1
grad-clip: 5
patience: 0
epochs: 30
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
opt: adam
transformer-lr: 6.0
transformer-warmup-steps: 250
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

load_pretrained_model: "<path-to-your-pretrained-model>:<modules-to-load>:<excluded-modules>" # Example: lid_42.pt::decoder.embed,decoder.output_layer,ctc.
use_adapters: true
adapter_train_languages: "ru_cy_it_eu_pt"
dataset: "ru_cy_it_eu_pt"
meta_train: true
shared_adapter: "ru_cy_it_eu_pt" # The name of shared_adapter, other name is also okay
adapter_fusion: false
batch-size: 32 # Real batch size is given by this batch-size * number of languages
train_adapter_with_head: true

meta_lr: 0.0001 # 1.0
adam_lr: 0.01 # inner learning rate
# cs

================================================
FILE: code/ASR/Adapter/data_load.py
================================================
from espnet.utils.training.batchfy import make_batchset
from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence
import torch
import os
import json
import kaldiio
import random
import logging
import sentencepiece as spm
from balanced_sampler import BalancedBatchSampler
#cv mt cnh ky dv sl el lv fy-NL sah
data_config = {
    "template100": {
        "train": "dump/train_template/deltafalse/data_unigram100.json",
        "val": "dump/dev_template/deltafalse/data_unigram100.json",
        "test": "dump/test_template/deltafalse/data_unigram100.json",
        "token": "data/template_lang_char/train_template_unigram100_units.txt",
        "prefix": "/D_data/commonvoice/asr1/",
        "bpemodel": "data/template_lang_char/train_template_unigram100.model",
    },
    "template150": {
        "train": "dump/train_template/deltafalse/data_unigram150.json",
        "val": "dump/dev_template/deltafalse/data_unigram150.json",
        "test": "dump/test_template/deltafalse/data_unigram150.json",
        "token": "data/template_lang_char/train_template_unigram150_units.txt",
        "prefix": "/D_data/commonvoice/asr1/",
        "bpemodel": "data/template_lang_char/train_template_unigram150.model",
    },
}
low_resource_languages = ["ro", "cs", "br", "ar", "uk"]

def read_json_file(fname):
    with open(fname, "rb") as f:
        contents = json.load(f)["utts"]
    return contents
def load_json(train_json_file, dev_json_file, test_json_file):
    train_json = read_json_file(train_json_file)
    if os.path.isfile(dev_json_file):
        dev_json = read_json_file(dev_json_file)
    else:
        n_samples = len(train_json)
        train_size = int(0.9 * n_samples)
        logging.warning(
            f"No dev set provided, will split the last {n_samples - train_size} (10%) samples from training data"
        )
        train_json_item = list(train_json.items())
        # random.shuffle(train_json_item)
        train_json = dict(train_json_item[:train_size])
        dev_json = dict(train_json_item[train_size:])

        # Save temp dev set
        with open(dev_json_file, "w") as f:
            json.dump({"utts": dev_json}, f)
        logging.warning(f"Temporary dev set saved: {dev_json_file}")
    test_json = read_json_file(test_json_file)
    return train_json, dev_json, test_json


def load_data(root_path, dataset, args):
    def collate(minibatch):
        fbanks = []
        tokens = []
        for _, info in minibatch[0]:
            fbanks.append(
                torch.tensor(
                    kaldiio.load_mat(
                        info["input"][0]["feat"].replace(
                            data_config[dataset]["prefix"], root_path
                        )
                    )
                )
            )
            tokens.append(
                torch.tensor([int(s) for s in info["output"][0]["tokenid"].split()])
            )
        ilens = torch.tensor([x.shape[0] for x in fbanks])
        return (
            pad_sequence(fbanks, batch_first=True, padding_value=0),
            ilens,
            pad_sequence(tokens, batch_first=True, padding_value=-1),
        )
    language = dataset
    if language in low_resource_languages:
        template_key = "template100"
    else:
        template_key = "template150"
    data_config[dataset] = data_config[template_key].copy()   
    for key in ["train", "val", "test", "token"]:
        data_config[dataset][key] = data_config[template_key][key].replace("template", dataset)
    train_json = os.path.join(root_path, data_config[dataset]["train"])
    dev_json = (
        os.path.join(root_path, data_config[dataset]["val"])
        if data_config[dataset]["val"]
        else f"{root_path}/tmp_dev_set_{dataset}.json"
    )
    test_json = os.path.join(root_path, data_config[dataset]["test"])
    train_json, dev_json, test_json = load_json(train_json, dev_json, test_json)
    _, info = next(iter(train_json.items()))
    idim = info["input"][0]["shape"][1]
    odim = info["output"][0]["shape"][1]

    use_sortagrad = False  # args.sortagrad == -1 or args.sortagrad > 0
    # trainset = make_batchset(train_json, batch_size, max_length_in=800, max_length_out=150)
    trainset = make_batchset(
        train_json,
        args.batch_size,
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=args.ngpu if (args.ngpu > 1 and not args.dist_train) else 1,
        shortest_first=use_sortagrad,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    # devset = make_batchset(dev_json, batch_size, max_length_in=800, max_length_out=150)
    devset = make_batchset(
        dev_json,
        args.batch_size if args.ngpu <= 1 else int(args.batch_size / args.ngpu),
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=1,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    testset = make_batchset(
        test_json,
        args.batch_size if args.ngpu <= 1 else int(args.batch_size / args.ngpu),
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=1,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    if args.dist_train and args.ngpu > 1:
        train_sampler = torch.utils.data.distributed.DistributedSampler(trainset)
    else:
        train_sampler = None
    train_loader = DataLoader(
        trainset,
        batch_size=1,
        collate_fn=collate,
        num_workers=args.n_iter_processes,
        shuffle=(train_sampler is None),
        pin_memory=True,
        sampler=train_sampler,
    )
    dev_loader = DataLoader(
        devset,
        batch_size=1,
        collate_fn=collate,
        shuffle=False,
        num_workers=args.n_iter_processes,
        pin_memory=True,
    )
    test_loader = DataLoader(
        testset,
        batch_size=1,
        collate_fn=collate,
        shuffle=False,
        num_workers=args.n_iter_processes,
        pin_memory=True,
    )
    return (train_loader, dev_loader, test_loader), (idim, odim)


def load_multilingual_data(root_path, datasets, args, languages):
    def collate(minibatch):
        out = []
        for b in minibatch:
            fbanks = []
            tokens = []
            language = None
            for _, info in b:
                fbanks.append(
                    torch.tensor(
                        kaldiio.load_mat(
                            info["input"][0]["feat"].replace(
                                data_config[dataset]["prefix"], root_path
                            )
                        )
                    )
                )
                tokens.append(
                    torch.tensor([int(s) for s in info["output"][0]["tokenid"].split()])
                )
                if language is not None:
                    assert language == info['category']
                else:
                    language = info['category']
            ilens = torch.tensor([x.shape[0] for x in fbanks])
            out.append((
            pad_sequence(fbanks, batch_first=True, padding_value=0),
            ilens,
            pad_sequence(tokens, batch_first=True, padding_value=-1),
            language,
        ))
        return out[0] if len(out) == 1 else out
    idim = None
    odim_dict = {}
    mtl_train_json, mtl_dev_json, mtl_test_json = {}, {}, {}
    for idx, dataset in enumerate(datasets):
        language = dataset
        if language in low_resource_languages:
            template_key = "template100"
        else:
            template_key = "template150"
        data_config[dataset] = data_config[template_key].copy()   
        for key in ["train", "val", "test", "token"]:
            data_config[dataset][key] = data_config[template_key][key].replace("template", dataset)
        
        train_json = os.path.join(root_path, data_config[dataset]["train"])
        dev_json = (
            os.path.join(root_path, data_config[dataset]["val"])
            if data_config[dataset]["val"]
            else f"{root_path}/tmp_dev_set_{dataset}.json"
        )
        test_json = os.path.join(root_path, data_config[dataset]["test"])
        train_json, dev_json, test_json = load_json(train_json, dev_json, test_json)
        for key in train_json.keys():
            train_json[key]['category'] = language
        for key in dev_json.keys():
            dev_json[key]['category'] = language
        for key in test_json.keys():
            test_json[key]['category'] = language
        #print(train_json)
        _, info = next(iter(train_json.items()))
        if idim is not None:
            assert idim == info["input"][0]["shape"][1]
        else:
            idim = info["input"][0]["shape"][1]
        odim_dict[language] = info["output"][0]["shape"][1]
        
        # Break if not in specified languages
        if dataset not in languages:
            continue

        mtl_train_json.update(train_json)
        mtl_dev_json.update(dev_json)
        mtl_test_json.update(test_json)
        #print(len(mtl_train_json), len(train_json))
    train_json, dev_json, test_json = mtl_train_json, mtl_dev_json, mtl_test_json
    use_sortagrad = False  # args.sortagrad == -1 or args.sortagrad > 0
    # trainset = make_batchset(train_json, batch_size, max_length_in=800, max_length_out=150)
    if args.ngpu > 1 and not args.dist_train:
        min_batch_size = args.ngpu
    else:
        min_batch_size = 1
    if args.meta_train:
        min_batch_size = 2 * min_batch_size
    trainset = make_batchset(
        train_json,
        args.batch_size,
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=min_batch_size,
        shortest_first=use_sortagrad,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    # devset = make_batchset(dev_json, batch_size, max_length_in=800, max_length_out=150)
    devset = make_batchset(
        dev_json,
        args.batch_size if args.ngpu <= 1 else int(args.batch_size / args.ngpu),
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=1,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    testset = make_batchset(
        test_json,
        args.batch_size if args.ngpu <= 1 else int(args.batch_size / args.ngpu),
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=1,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    if args.dist_train and args.ngpu > 1:
        train_sampler = torch.utils.data.distributed.DistributedSampler(trainset)
    elif args.meta_train:
        train_sampler = BalancedBatchSampler(trainset)
    else:
        train_sampler = None
    train_loader = DataLoader(
        trainset,
        batch_size=1 if not args.meta_train else len(languages),
        collate_fn=collate,
        num_workers=args.n_iter_processes,
        shuffle=(train_sampler is None),
        pin_memory=True,
        sampler=train_sampler,
    )
    dev_loader = DataLoader(
        devset,
        batch_size=1,
        collate_fn=collate,
        shuffle=False,
        num_workers=args.n_iter_processes,
        pin_memory=True,
    )
    test_loader = DataLoader(
        testset,
        batch_size=1,
        collate_fn=collate,
        shuffle=False,
        num_workers=args.n_iter_processes,
        pin_memory=True,
    )
    return (train_loader, dev_loader, test_loader), (idim, odim_dict)

def load_token_list(token_file):
    with open(token_file, "r") as f:
        token_list = [entry.split()[0] for entry in f]
    token_list.insert(0, "<blank>")
    token_list.append("<eos>")
    return token_list

def load_bpemodel(root_path, dataset):
    if dataset in low_resource_languages:
        template_key = "template100"
    else:
        template_key = "template150"
    bpemodel_path = os.path.join(root_path, data_config[template_key]["bpemodel"]).replace("template", dataset)
    bpemodel = spm.SentencePieceProcessor()
    bpemodel.Load(bpemodel_path)
    return bpemodel


================================================
FILE: code/ASR/Adapter/e2e_asr_adaptertransformer.py
================================================
# Copyright 2019 Shigeki Karita
#  Apache 2.0  (http://www.apache.org/licenses/LICENSE-2.0)

"""Transformer speech recognition model (pytorch)."""

from argparse import Namespace
from distutils.util import strtobool
import logging
import math

import numpy
import torch

from espnet.nets.pytorch_backend.e2e_asr_transformer import *

from espnet.nets.pytorch_backend.transformer.attention import MultiHeadedAttention
from espnet.nets.pytorch_backend.transformer.embedding import PositionalEncoding

from espnet.nets.pytorch_backend.transformer.encoder_layer import EncoderLayer
from espnet.nets.pytorch_backend.transformer.decoder_layer import DecoderLayer

from espnet.nets.pytorch_backend.transformer.layer_norm import LayerNorm
from espnet.nets.pytorch_backend.transformer.positionwise_feed_forward import (
    PositionwiseFeedForward,  # noqa: H301
)
from espnet.nets.pytorch_backend.transformer.repeat import repeat
from espnet.nets.pytorch_backend.e2e_asr_transformer import E2E as E2ETransformer
from espnet.nets.pytorch_backend.transformer.encoder import Encoder
from espnet.nets.pytorch_backend.transformer.decoder import Decoder

from espnet.nets.pytorch_backend.transformer.label_smoothing_loss import (
    LabelSmoothingLoss,  # noqa: H301
)
from data_load import low_resource_languages
low_resource_adapter_dim = 64
high_resource_adapter_dim = 128

class SimAdapter(MultiHeadedAttention):
    def __init__(self, n_feat, dropout_rate, fusion_languages=None, num_shared_layers=-1):
        """Construct an MultiHeadedAttention object."""
        super(MultiHeadedAttention, self).__init__()
        self.linear_q = torch.nn.Linear(n_feat, n_feat)
        self.linear_k = torch.nn.Linear(n_feat, n_feat)
        self.linear_v = torch.nn.Linear(n_feat, n_feat, bias=False)
        self.linear_v.weight.data = (
            torch.zeros(n_feat, n_feat) + 0.000001
        ).fill_diagonal_(1.0)
        self.attn = None
        self.num_shared_layers = num_shared_layers
        self.dropout = torch.nn.Dropout(p=dropout_rate)
        self.temperature = 1.0
        self.fusion_languages = fusion_languages

    def forward_qkv(self, query, key, value):
        q = self.linear_q(query) # (batch, time, d_k)
        k = self.linear_k(key) # (batch, time, n_adapters, d_k)
        v = self.linear_v(value) # (batch, time, n_adapters, d_k)
        return q, k, v

    def forward_attention(self, value, scores):
        self.attn = torch.softmax(scores, dim=-1)  # (batch, time, n_adapters)
        p_attn = self.dropout(self.attn)
        x = torch.matmul(p_attn.unsqueeze(2), value)
        # (batch, time, 1, n_adapters), (batch, time, n_adapters, d_k)
        x = torch.squeeze(x, dim=2)
        return x  # (batch, time, d_k)

    def forward(self, query, key, value, residual=None):
        q, k, v = self.forward_qkv(query, key, value)
        # q: (batch, time, 1, d_k); k, v: (batch, time, n_adapters, d_k)
        scores = torch.matmul(q.unsqueeze(2), k.transpose(-2, -1)) / math.sqrt(self.temperature)
        scores = torch.squeeze(scores, dim=2)
        # scores: (batch, time, n_adapters)
        out = self.forward_attention(v, scores)
        if residual is not None:
            out = out + residual
        return out

class Adapter(torch.nn.Module):
    def __init__(self, adapter_dim, embed_dim):
        super().__init__()
        self.layer_norm = LayerNorm(embed_dim)
        self.down_project = torch.nn.Linear(embed_dim, adapter_dim, bias=False)
        self.up_project = torch.nn.Linear(adapter_dim, embed_dim, bias=False)

    def forward(self, z):
        normalized_z = self.layer_norm(z)
        h = torch.nn.functional.relu(self.down_project(normalized_z))
        return self.up_project(h) + z
class CustomEncoderLayer(EncoderLayer):
    def forward(self, x, mask, cache=None):
        residual = x
        if self.normalize_before:
            x = self.norm1(x)
        if cache is None:
            x_q = x
        else:
            assert cache.shape == (x.shape[0], x.shape[1] - 1, self.size)
            x_q = x[:, -1:, :]
            residual = residual[:, -1:, :]
            mask = None if mask is None else mask[:, -1:, :]
        if self.concat_after:
            x_concat = torch.cat((x, self.self_attn(x_q, x, x, mask)), dim=-1)
            x = residual + self.concat_linear(x_concat)
        else:
            x = residual + self.dropout(self.self_attn(x_q, x, x, mask))
        if not self.normalize_before:
            x = self.norm1(x)
        residual = x
        if self.normalize_before:
            x = self.norm2(x)
            x_norm = x
        x = residual + self.dropout(self.feed_forward(x))
        if not self.normalize_before:
            x = self.norm2(x)
        if cache is not None:
            x = torch.cat([cache, x], dim=1)
        return x, x_norm, mask
class AdaptiveEncoderLayer(CustomEncoderLayer):
    def __init__(
        self,
        languages,
        size,
        self_attn,
        feed_forward,
        dropout_rate,
        normalize_before=True,
        concat_after=False,
        sim_adapter_layer=None,
        shared_adapter=None,
        use_adapters=True,
    ):
        super().__init__(size,
            self_attn,
            feed_forward,
            dropout_rate,
            normalize_before,
            concat_after,
        )
        self.use_adapters = use_adapters
        if use_adapters:
            self.adapters = torch.nn.ModuleDict()
            self.shared_adapter = shared_adapter
            if shared_adapter:
                languages = [shared_adapter]
            for lang in languages:
                if lang in low_resource_languages or self.shared_adapter:
                    adapter_dim = low_resource_adapter_dim
                else:
                    adapter_dim = high_resource_adapter_dim
                self.adapters[lang] = Adapter(adapter_dim, size)
            self.sim_adapter = sim_adapter_layer

    def forward(self, x, mask, language, cache=None, use_sim_adapter=True):
        x, x_norm, mask = super().forward(x, mask, cache=cache)

        if not self.use_adapters:
            return x, mask, language, cache, use_sim_adapter
        if self.shared_adapter:
            assert len(self.adapters.keys()) == 1
            language = list(self.adapters.keys())[0]
        if (not use_sim_adapter) or not (self.sim_adapter):
            out = self.adapters[language](x)
        else:
            out = []
            fusion_languages = list(self.sim_adapter.keys())[0]
            for lang in fusion_languages.split("_"):
                if lang != "self":
                    out.append(self.adapters[lang](x))
                else:
                    out.append(x)
            out = torch.stack(out).permute(1, 2, 0, 3) # B, T, n_adapters, F
            out = self.sim_adapter[fusion_languages](x, out, out, residual=x_norm)
            #out = self.adapters['cs'](x)
        return out, mask, language, cache, use_sim_adapter

class AdaptiveEncoder(Encoder):
    def __init__(
        self,
        languages,
        idim,
        selfattention_layer_type="selfattn",
        attention_dim=256,
        attention_heads=4,
        conv_wshare=4,
        conv_kernel_length=11,
        conv_usebias=False,
        linear_units=2048,
        num_blocks=6,
        dropout_rate=0.1,
        positional_dropout_rate=0.1,
        attention_dropout_rate=0.0,
        input_layer="conv2d",
        pos_enc_class=PositionalEncoding,
        normalize_before=True,
        concat_after=False,
        positionwise_layer_type="linear",
        positionwise_conv_kernel_size=1,
        padding_idx=-1,
        sim_adapter=False,
        shared_adapter=None,
        use_adapters=True,
        fusion_languages=None,
    ):
        super().__init__(idim,
            selfattention_layer_type,
            attention_dim,
            attention_heads,
            conv_wshare,
            conv_kernel_length,
            conv_usebias,
            linear_units,
            num_blocks,
            dropout_rate,
            positional_dropout_rate,
            attention_dropout_rate,
            input_layer,
            pos_enc_class,
            normalize_before,
            concat_after,
            positionwise_layer_type,
            positionwise_conv_kernel_size,
            padding_idx)
        positionwise_layer, positionwise_layer_args = self.get_positionwise_layer(
            positionwise_layer_type,
            attention_dim,
            linear_units,
            dropout_rate,
            positionwise_conv_kernel_size,
        )
        if selfattention_layer_type == "selfattn":
            logging.info("encoder self-attention layer type = self-attention")
            self.encoders = repeat(
                num_blocks,
                lambda lnum: AdaptiveEncoderLayer(
                    languages,
                    attention_dim,
                    MultiHeadedAttention(
                        attention_heads, attention_dim, attention_dropout_rate
                    ),
                    positionwise_layer(*positionwise_layer_args),
                    dropout_rate,
                    normalize_before,
                    concat_after,
                    torch.nn.ModuleDict({"_".join(sorted(fusion_languages)): SimAdapter(attention_dim, attention_dropout_rate, fusion_languages)}) if sim_adapter else None,
                    shared_adapter,
                    use_adapters,
                ),
            )
        else:
            raise NotImplementedError("Only support self-attention encoder layer")
    def forward(self, xs, masks, language, use_sim_adapter=True):
        xs, masks = self.embed(xs, masks)
        xs, masks, _, _, _ = self.encoders(xs, masks, language, None, use_sim_adapter)
        if self.normalize_before:
            xs = self.after_norm(xs)
        return xs, masks
class CustomDecoderLayer(DecoderLayer):
    def forward(self, tgt, tgt_mask, memory, memory_mask, cache=None):
        residual = tgt
        if self.normalize_before:
            tgt = self.norm1(tgt)
        if cache is None:
            tgt_q = tgt
            tgt_q_mask = tgt_mask
        else:
            # compute only the last frame query keeping dim: max_time_out -> 1
            assert cache.shape == (
                tgt.shape[0],
                tgt.shape[1] - 1,
                self.size,
            ), f"{cache.shape} == {(tgt.shape[0], tgt.shape[1] - 1, self.size)}"
            tgt_q = tgt[:, -1:, :]
            residual = residual[:, -1:, :]
            tgt_q_mask = None
            if tgt_mask is not None:
                tgt_q_mask = tgt_mask[:, -1:, :]
        if self.concat_after:
            tgt_concat = torch.cat(
                (tgt_q, self.self_attn(tgt_q, tgt, tgt, tgt_q_mask)), dim=-1
            )
            x = residual + self.concat_linear1(tgt_concat)
        else:
            x = residual + self.dropout(self.self_attn(tgt_q, tgt, tgt, tgt_q_mask))
        if not self.normalize_before:
            x = self.norm1(x)
        residual = x
        if self.normalize_before:
            x = self.norm2(x)
        if self.concat_after:
            x_concat = torch.cat(
                (x, self.src_attn(x, memory, memory, memory_mask)), dim=-1
            )
            x = residual + self.concat_linear2(x_concat)
        else:
            x = residual + self.dropout(self.src_attn(x, memory, memory, memory_mask))
        if not self.normalize_before:
            x = self.norm2(x)
        residual = x
        if self.normalize_before:
            x = self.norm3(x)
            x_norm = x
        x = residual + self.dropout(self.feed_forward(x))
        if not self.normalize_before:
            x = self.norm3(x)
        if cache is not None:
            x = torch.cat([cache, x], dim=1)
        return x, x_norm, tgt_mask, memory, memory_mask
class AdaptiveDecoderLayer(CustomDecoderLayer):
    def __init__(
        self,
        languages,
        size,
        self_attn,
        src_attn,
        feed_forward,
        dropout_rate,
        normalize_before=True,
        concat_after=False,
        sim_adapter_layer=None,
        shared_adapter=None,
        use_adapters=True,
    ):
        super().__init__(size,
        self_attn,
        src_attn,
        feed_forward,
        dropout_rate,
        normalize_before,
        concat_after
        )
        self.use_adapters = use_adapters
        if use_adapters:
            self.adapters = torch.nn.ModuleDict()
            self.shared_adapter = shared_adapter
            if shared_adapter:
                languages = [shared_adapter]
            for lang in languages:
                if lang in low_resource_languages or self.shared_adapter:
                    adapter_dim = low_resource_adapter_dim
                else:
                    adapter_dim = high_resource_adapter_dim
                self.adapters[lang] = Adapter(adapter_dim, size)
            self.sim_adapter = sim_adapter_layer
            
    def forward(self, tgt, tgt_mask, memory, memory_mask, language, cache=None, use_sim_adapter=True):
        x, x_norm, tgt_mask, memory, memory_mask = super().forward(tgt, tgt_mask, memory, memory_mask, cache=cache)
        if not self.use_adapters:
            return x, tgt_mask, memory, memory_mask, language, cache, use_sim_adapter
        if self.shared_adapter:
            assert len(self.adapters.keys()) == 1
            language = list(self.adapters.keys())[0]
        if (not use_sim_adapter) or (not self.sim_adapter):
            out = self.adapters[language](x)
        else:
            out = []
            fusion_languages = list(self.sim_adapter.keys())[0]
            for lang in fusion_languages.split("_"):
                if lang != "self":
                    out.append(self.adapters[lang](x))
                else:
                    out.append(x)
            out = torch.stack(out).permute(1, 2, 0, 3) # B, T, n_adapters, F
            out = self.sim_adapter[fusion_languages](x, out, out, residual=x_norm)
            
        return out, tgt_mask, memory, memory_mask, language, cache, use_sim_adapter


class AdaptiveDecoder(Decoder):
    def __init__(
        self,
        languages,
        odim_dict,
        selfattention_layer_type="selfattn",
        attention_dim=256,
        attention_heads=4,
        conv_wshare=4,
        conv_kernel_length=11,
        conv_usebias=False,
        linear_units=2048,
        num_blocks=6,
        dropout_rate=0.1,
        positional_dropout_rate=0.1,
        self_attention_dropout_rate=0.0,
        src_attention_dropout_rate=0.0,
        input_layer="embed",
        use_output_layer=True,
        pos_enc_class=PositionalEncoding,
        normalize_before=True,
        concat_after=False,
        sim_adapter=False,
        shared_adapter=False,
        use_adapters=True,
        fusion_languages=None,
    ):
        super().__init__(1,
        selfattention_layer_type,
        attention_dim,
        attention_heads,
        conv_wshare,
        conv_kernel_length,
        conv_usebias,
        linear_units,
        num_blocks,
        dropout_rate,
        positional_dropout_rate,
        self_attention_dropout_rate,
        src_attention_dropout_rate,
        input_layer,
        use_output_layer,
        pos_enc_class,
        normalize_before,
        concat_after)
        if input_layer == "embed":
            self.embed = torch.nn.ModuleDict()
            for lang in odim_dict.keys():
                self.embed[lang] = torch.nn.Sequential(
                    torch.nn.Embedding(odim_dict[lang], attention_dim),
                    pos_enc_class(attention_dim, positional_dropout_rate),
                )
        else:
            raise NotImplementedError("only support embed embedding layer")
        assert self_attention_dropout_rate == src_attention_dropout_rate
        if selfattention_layer_type == "selfattn":
            logging.info("decoder self-attention layer type = self-attention")
            self.decoders = repeat(
                num_blocks,
                lambda lnum: AdaptiveDecoderLayer(
                    languages,
                    attention_dim,
                    MultiHeadedAttention(
                        attention_heads, attention_dim, self_attention_dropout_rate
                    ),
                    MultiHeadedAttention(
                        attention_heads, attention_dim, src_attention_dropout_rate
                    ),
                    PositionwiseFeedForward(attention_dim, linear_units, dropout_rate),
                    dropout_rate,
                    normalize_before,
                    concat_after,
                    torch.nn.ModuleDict({"_".join(sorted(fusion_languages)): SimAdapter(attention_dim, self_attention_dropout_rate, fusion_languages)}) if sim_adapter else None,
                    shared_adapter,
                    use_adapters,
                ),
            )
        else:
            raise NotImplementedError("Only support self-attention decoder layer")
        if use_output_layer:
            self.output_layer = torch.nn.ModuleDict()
            for lang in odim_dict.keys():
                self.output_layer[lang] = torch.nn.Linear(attention_dim, odim_dict[lang])
        else:
            self.output_layer = None
    def forward(self, tgt, tgt_mask, memory, memory_mask, language, use_sim_adapter=True):
        x = self.embed[language](tgt)
        x, tgt_mask, memory, memory_mask, _, _, _ = self.decoders(
            x, tgt_mask, memory, memory_mask, language, None, use_sim_adapter
        )
        if self.normalize_before:
            x = self.after_norm(x)
        if self.output_layer is not None:
            x = self.output_layer[language](x)
        return x, tgt_mask
    def forward_one_step(self, tgt, tgt_mask, memory, memory_mask, language, cache=None):
        x = self.embed[language](tgt)
        if cache is None:
            cache = [None] * len(self.decoders)
        new_cache = []
        for c, decoder in zip(cache, self.decoders):
            x, tgt_mask, memory, memory_mask, _, _, _ = decoder(
                x, tgt_mask, memory, None, language, cache=c,
            )
            new_cache.append(x)
        if self.normalize_before:
            y = self.after_norm(x[:, -1])
        else:
            y = x[:, -1]
        if self.output_layer is not None:
            y = torch.log_softmax(self.output_layer[language](y), dim=-1)
        return y, new_cache

class E2E(E2ETransformer):
    def __init__(self, idim, odim_dict, args, languages, ignore_id=-1):
        super().__init__(idim, 1, args, ignore_id)
        if args.transformer_attn_dropout_rate is None:
            args.transformer_attn_dropout_rate = args.dropout_rate
        if args.fusion_languages:
            args.fusion_languages = args.fusion_languages.split("_")
        self.fusion_languages = sorted(args.fusion_languages) if args.fusion_languages else sorted(languages)
        self.encoder = AdaptiveEncoder(
            languages=languages,
            idim=idim,
            selfattention_layer_type=args.transformer_encoder_selfattn_layer_type,
            attention_dim=args.adim,
            attention_heads=args.aheads,
            conv_wshare=args.wshare,
            conv_kernel_length=args.ldconv_encoder_kernel_length,
            conv_usebias=args.ldconv_usebias,
            linear_units=args.eunits,
            num_blocks=args.elayers,
            input_layer=args.transformer_input_layer,
            dropout_rate=args.dropout_rate,
            positional_dropout_rate=args.dropout_rate,
            attention_dropout_rate=args.transformer_attn_dropout_rate,
            sim_adapter=args.sim_adapter,
            shared_adapter=args.shared_adapter,
            use_adapters=args.use_adapters,
            fusion_languages=self.fusion_languages
        )
        if args.mtlalpha < 1:
            self.decoder = AdaptiveDecoder(
                languages=languages,
                odim_dict=odim_dict,
                selfattention_layer_type=args.transformer_decoder_selfattn_layer_type,
                attention_dim=args.adim,
                attention_heads=args.aheads,
                conv_wshare=args.wshare,
                conv_kernel_length=args.ldconv_decoder_kernel_length,
                conv_usebias=args.ldconv_usebias,
                linear_units=args.dunits,
                num_blocks=args.dlayers,
                dropout_rate=args.dropout_rate,
                positional_dropout_rate=args.dropout_rate,
                self_attention_dropout_rate=args.transformer_attn_dropout_rate,
                src_attention_dropout_rate=args.transformer_attn_dropout_rate,
                sim_adapter=args.sim_adapter,
                shared_adapter=args.shared_adapter,
                use_adapters=args.use_adapters,
                fusion_languages=self.fusion_languages,
            )
        else:
            self.decoder = None
        self.soss = {lang: odim_dict[lang] - 1 for lang in languages}
        self.eoss = {lang: odim_dict[lang] - 1 for lang in languages}
        self.odim_dict = odim_dict

        self.criterion = torch.nn.ModuleDict()
        for lang in languages:
            self.criterion[lang] = LabelSmoothingLoss(
                                        self.odim_dict[lang],
                                        self.ignore_id,
                                        args.lsm_weight,
                                        args.transformer_length_normalized_loss,
                                    )
        if args.mtlalpha > 0.0:
            self.ctc = torch.nn.ModuleDict()
            for lang in languages:
                self.ctc[lang] = CTC(
                    odim_dict[lang], args.adim, args.dropout_rate, ctc_type=args.ctc_type, reduce=True
                )
        else:
            self.ctc = None

        if args.report_cer or args.report_wer:
            self.error_calculator = ErrorCalculator(
                args.char_list,
                args.sym_space,
                args.sym_blank,
                args.report_cer,
                args.report_wer,
            )
        else:
            self.error_calculator = None

        self.reset_parameters(args)
        # Adapter
        self.meta_train = args.meta_train
        self.shared_adapter = args.shared_adapter
        self.sim_adapter = False
        self.use_adapters = args.use_adapters
        if self.use_adapters:
            for p in self.parameters():
                p.requires_grad = False
            self.sim_adapter = args.sim_adapter
            if not args.sim_adapter:
                adapter_train_languages = args.adapter_train_languages
                self.enable_adapter_training(adapter_train_languages, 
                            shared_adapter=args.shared_adapter, enable_head=args.train_adapter_with_head)
            else:
                self.reset_sim_adapter_parameters()
                self.enable_sim_adapter_training()
        self.recognize_language_branch = None # Set default recognize language for decoding

    def reset_sim_adapter_parameters(self):
        key = "_".join(self.fusion_languages)
        for layer in self.encoder.encoders:
            layer.sim_adapter[key].linear_v.weight.data = (
                torch.zeros(self.adim, self.adim) + 0.000001
            ).fill_diagonal_(1.0)
        for layer in self.decoder.decoders:
            layer.sim_adapter[key].linear_v.weight.data = (
                torch.zeros(self.adim, self.adim) + 0.000001
            ).fill_diagonal_(1.0)
    def enable_sim_adapter_training(self):
        key = "_".join(self.fusion_languages)
        logging.warning(f"Unfreezing the SimAdapter parameters: {key}")
        for layer in self.encoder.encoders:
            for p in layer.sim_adapter[key].parameters():
                p.requires_grad = True
            # for p in layer.adapter_norm.parameters():
            #     p.requires_grad = True
        for layer in self.decoder.decoders:
            for p in layer.sim_adapter[key].parameters():
                p.requires_grad = True
    def get_fusion_guide_loss(self, language):
        device = next(self.parameters()).device
        if language not in self.fusion_languages:
            return torch.tensor(0.0).to(device)
        guide_loss = torch.tensor(0.0).to(device)
        loss_fn = torch.nn.CrossEntropyLoss(reduction='mean')
        
        lang_id = sorted(self.fusion_languages).index(language)
        key = "_".join(self.fusion_languages)
        target = torch.tensor(lang_id).unsqueeze(0).to(device)
        for layer in self.encoder.encoders:
            logits = layer.sim_adapter[key].attn.mean(axis=(0, 1)).unsqueeze(0) # (batch, time, n_adapters)
            guide_loss = guide_loss + loss_fn(logits.exp(), target)
            layer.sim_adapter.attn = None
        for layer in self.decoder.decoders:
            logits = layer.sim_adapter[key].attn.mean(axis=(0, 1)).unsqueeze(0) # (batch, time, n_adapters)
            guide_loss = guide_loss + loss_fn(logits.exp(), target)
            layer.sim_adapter[key].attn = None
        return guide_loss

    def get_fusion_regularization_loss(self):
        reg_loss = 0.0
        fusion_reg_loss_weight = 0.01
        device = next(self.parameters()).device
        key = "_".join(self.fusion_languages)
        target = torch.zeros((self.adim, self.adim)).fill_diagonal_(1.0).to(device)
        for layer in self.encoder.encoders:
            reg_loss = reg_loss + fusion_reg_loss_weight * (target - layer.sim_adapter[key].linear_v.weight).pow(2).sum()
        for layer in self.decoder.decoders:
            reg_loss = reg_loss + fusion_reg_loss_weight * (target - layer.sim_adapter[key].linear_v.weight).pow(2).sum()
        return reg_loss

    def enable_adapter_training(self, specified_languages=None, shared_adapter=False, enable_head=False):
        # Unfreeze the adapter parameters
        if specified_languages:
            enable_languages = specified_languages
        else:
            enable_languages = self.criterion.keys()
        logging.warning(f"Unfreezing the adapter parameters of {' '.join(enable_languages)}")
        for lang in enable_languages:
            if enable_head:
                for p in self.decoder.embed[lang].parameters():
                    p.requires_grad = True
                for p in self.decoder.output_layer[lang].parameters():
                    p.requires_grad = True
                for p in self.ctc[lang].parameters():
                    p.requires_grad = True
            if shared_adapter:
                lang = shared_adapter
            for layer in self.encoder.encoders:
                for p in layer.adapters[lang].parameters():
                    p.requires_grad = True
            for layer in self.decoder.decoders:
                for p in layer.adapters[lang].parameters():
                    p.requires_grad = True
    
    def forward(self, xs_pad, ilens, ys_pad, language, use_sim_adapter=True):
        # 1. forward encoder
        xs_pad = xs_pad[:, : max(ilens)]  # for data parallel
        src_mask = make_non_pad_mask(ilens.tolist()).to(xs_pad.device).unsqueeze(-2)
        hs_pad, hs_mask = self.encoder(xs_pad, src_mask, language, use_sim_adapter=True)
        self.hs_pad = hs_pad

        # 2. forward decoder
        if self.decoder is not None:
            ys_in_pad, ys_out_pad = add_sos_eos(
                ys_pad, self.soss[language], self.eoss[language], self.ignore_id
            )
            ys_mask = target_mask(ys_in_pad, self.ignore_id)
            pred_pad, pred_mask = self.decoder(ys_in_pad, ys_mask, hs_pad, hs_mask, language, use_sim_adapter=True)
            self.pred_pad = pred_pad

            # 3. compute attention loss
            loss_att = self.criterion[language](pred_pad, ys_out_pad)
            self.acc = th_accuracy(
                pred_pad.view(-1, self.odim_dict[language]), ys_out_pad, ignore_label=self.ignore_id
            )
        else:
            loss_att = None
            self.acc = None

        # TODO(karita) show predicted text
        # TODO(karita) calculate these stats
        cer_ctc = None
        if self.mtlalpha == 0.0:
            loss_ctc = None
        else:
            batch_size = xs_pad.size(0)
            hs_len = hs_mask.view(batch_size, -1).sum(1)
            ctc_pred_pad = self.ctc[language].ctc_lo(hs_pad)
            loss_ctc = self.ctc[language](hs_pad.view(batch_size, -1, self.adim), hs_len, ys_pad)
            if not self.training and self.error_calculator is not None:
                ys_hat = self.ctc[language].argmax(hs_pad.view(batch_size, -1, self.adim)).data
                cer_ctc = self.error_calculator[language](ys_hat.cpu(), ys_pad.cpu(), is_ctc=True)
            # for visualization
            if not self.training:
                self.ctc[language].softmax(hs_pad)
        
        # 5. compute cer/wer
        if self.training or self.error_calculator is None or self.decoder is None:
            cer, wer = None, None
        else:
            ys_hat = pred_pad.argmax(dim=-1)
            cer, wer = self.error_calculator(ys_hat.cpu(), ys_pad.cpu())

        # copied from e2e_asr
        alpha = self.mtlalpha
        if alpha == 0:
            self.loss = loss_att
            loss_att_data = float(loss_att)
            loss_ctc_data = None
        elif alpha == 1:
            self.loss = loss_ctc
            loss_att_data = None
            loss_ctc_data = float(loss_ctc)
        else:
            self.loss = alpha * loss_ctc + (1 - alpha) * loss_att
            loss_att_data = float(loss_att)
            loss_ctc_data = float(loss_ctc)
        
        if self.sim_adapter and self.training:
            guide_loss = self.get_fusion_guide_loss(language)
        loss_data = float(self.loss)
        if loss_data < CTC_LOSS_THRESHOLD and not math.isnan(loss_data):
            self.reporter.report(
                loss_ctc_data, loss_att_data, self.acc, cer_ctc, cer, wer, loss_data
            )
        else:
            logging.warning("loss (=%f) is not correct", loss_data)
        if self.training and not self.meta_train:
            if not self.sim_adapter:
                guide_loss = torch.tensor(0.0).cuda()
            return (self.loss, guide_loss)
        return self.loss
    
    def calculate_all_ctc_probs(self, xs_pad, ilens, ys_pad, language):
        """E2E CTC probability calculation.
        :param torch.Tensor xs_pad: batch of padded input sequences (B, Tmax)
        :param torch.Tensor ilens: batch of lengths of input sequences (B)
        :param torch.Tensor ys_pad: batch of padded token id sequence tensor (B, Lmax)
        :return: CTC probability (B, Tmax, vocab)
        :rtype: float ndarray
        """
        ret = None
        if self.mtlalpha == 0:
            return ret

        self.eval()
        with torch.no_grad():
            self.forward(xs_pad, ilens, ys_pad, language)
        for name, m in self.named_modules():
            if isinstance(m, CTC) and m.probs is not None:
                ret = m.probs.cpu().numpy()
        self.train()
        return ret
    
    def calculate_all_attentions(self, xs_pad, ilens, ys_pad, language):
        """E2E attention calculation.
        :param torch.Tensor xs_pad: batch of padded input sequences (B, Tmax, idim)
        :param torch.Tensor ilens: batch of lengths of input sequences (B)
        :param torch.Tensor ys_pad: batch of padded token id sequence tensor (B, Lmax)
        :return: attention weights (B, H, Lmax, Tmax)
        :rtype: float ndarray
        """
        self.eval()
        with torch.no_grad():
            self.forward(xs_pad, ilens, ys_pad, language)
        ret = dict()
        for name, m in self.named_modules():
            if (
                isinstance(m, MultiHeadedAttention)
                or isinstance(m, DynamicConvolution)
                or isinstance(m, RelPositionMultiHeadedAttention)
            ):
                ret[name] = m.attn.cpu().numpy()
            if isinstance(m, DynamicConvolution2D):
                ret[name + "_time"] = m.attn_t.cpu().numpy()
                ret[name + "_freq"] = m.attn_f.cpu().numpy()
        self.train()
        return ret
    
    def calculate_sim_adapter_attentions(self, xs_pad, ilens, ys_pad, language):
        self.eval()
        with torch.no_grad():
            self.forward(xs_pad, ilens, ys_pad, language)
        ret = dict()
        for name, m in self.named_modules():
            if (
                isinstance(m, MultiHeadedAttention)
                or isinstance(m, DynamicConvolution)
                or isinstance(m, RelPositionMultiHeadedAttention)
            ) and "sim_adapter" in name:
                ret[name] = m.attn.cpu().numpy()
        return ret

    def encode(self, x, language):
        self.eval()
        x = torch.as_tensor(x).unsqueeze(0)
        enc_output, _ = self.encoder(x, None, language)
        return enc_output.squeeze(0)
    
    def set_recognize_language_branch(self, language):
        self.recognize_language_branch = language
    def recognize_batch(self, xs, recog_args, char_list=None, rnnlm=None, language=None):
        assert language is not None or self.recognize_language_branch is not None, \
                                "Recognize language is not specified"
        if language is None:
            language = self.recognize_language_branch
        prev = self.training
        self.eval()
        ilens = numpy.fromiter((xx.shape[0] for xx in xs), dtype=numpy.int64)
        # subsample frame
        xs = [xx[:: self.subsample[0], :] for xx in xs]
        xs = [to_device(self, to_torch_tensor(xx).float()) for xx in xs]

        xs_pad = pad_list(xs, 0.0)

        src_mask = make_non_pad_mask(ilens.tolist()).to(xs_pad.device).unsqueeze(-2)

        # 1. Encoder
        hs_pad, hs_mask = self.encoder(xs_pad, src_mask, language)
        hlens = torch.tensor([int(sum(mask[0])) for mask in hs_mask])

        # calculate log P(z_t|X) for CTC scores
        if recog_args.ctc_weight > 0.0:
            lpz = self.ctc[language].log_softmax(hs_pad)
            normalize_score = False
        else:
            lpz = None
            normalize_score = True

        logging.info("max input length: " + str(hs_pad.size(1)))

        # search params
        batch = len(hlens)
        beam = recog_args.beam_size
        penalty = recog_args.penalty
        ctc_weight = getattr(recog_args, "ctc_weight", 0)  # for NMT
        att_weight = 1.0 - ctc_weight

        n_bb = batch * beam
        pad_b = to_device(hs_pad, torch.arange(batch) * beam).view(-1, 1)
        max_hlens = hlens

        if recog_args.maxlenratio == 0:
            maxlens = max_hlens
        else:
            maxlens = [
                max(1, int(recog_args.maxlenratio * max_hlen)) for max_hlen in max_hlens
            ]
        minlen = min([int(recog_args.minlenratio * max_hlen) for max_hlen in max_hlens])
        logging.info("max output lengths: " + str(maxlens))
        logging.info("min output length: " + str(minlen))

        vscores = to_device(hs_pad, torch.zeros(batch, beam))
        rnnlm_state = None

        import six

        # initialize hypothesis
        yseq = [[self.soss[language]] for _ in six.moves.range(n_bb)]
        accum_odim_ids = [self.soss[language] for _ in six.moves.range(n_bb)]
        stop_search = [False for _ in six.moves.range(batch)]
        nbest_hyps = [[] for _ in six.moves.range(batch)]
        ended_hyps = [[] for _ in six.moves.range(batch)]

        exp_hs_mask = (
            hs_mask.unsqueeze(1).repeat(1, beam, 1, 1).contiguous()
        )  # (batch, beam, 1, T)
        exp_hs_mask = exp_hs_mask.view(n_bb, hs_mask.size()[1], hs_mask.size()[2])
        exp_h = (
            hs_pad.unsqueeze(1).repeat(1, beam, 1, 1).contiguous()
        )  # (batch, beam, T, F)
        exp_h = exp_h.view(n_bb, hs_pad.size()[1], hs_pad.size()[2])

        ctc_scorer, ctc_state = None, None
        if lpz is not None:
            scoring_num = min(
                int(beam * CTC_SCORING_RATIO) if att_weight > 0.0 else 0,
                lpz.size(-1),
            )
            ctc_scorer = CTCPrefixScoreTH(lpz, hlens, 0, self.eoss[language])

        for i in six.moves.range(max(maxlens)):
            logging.debug("position " + str(i))

            # get nbest local scores and their ids
            ys_mask = subsequent_mask(i + 1).to(hs_pad.device).unsqueeze(0)

            ys = torch.tensor(yseq).to(hs_pad.device)
            vy = to_device(hs_pad, torch.LongTensor(self._get_last_yseq(yseq)))

            # local_att_scores (n_bb = beam * batch, vocab)
            if self.decoder is not None:
                local_att_scores = self.decoder.forward_one_step(
                    ys, ys_mask, exp_h, memory_mask=exp_hs_mask, language=language,
                )[0]
            else:
                local_att_scores = to_device(
                    hs_pad, torch.zeros((n_bb, lpz.size(-1)), dtype=lpz.dtype)
                )

            if rnnlm:
                rnnlm_state, local_lm_scores = rnnlm.buff_predict(rnnlm_state, vy, n_bb)
                local_scores = local_att_scores + recog_args.lm_weight * local_lm_scores
            else:
                local_scores = local_att_scores

            # ctc
            if ctc_scorer:
                local_scores = att_weight * local_att_scores
                local_scores[:, 0] = self.logzero  # avoid choosing blank
                part_ids = (
                    torch.topk(local_scores, scoring_num, dim=-1)[1]
                    if scoring_num > 0
                    else None
                )
                local_ctc_scores, ctc_state = ctc_scorer(
                    yseq, ctc_state, part_ids
                )  # local_ctc_scores (n_bb, odim)

                local_scores = local_scores + ctc_weight * local_ctc_scores
                if rnnlm:
                    local_scores = local_scores + recog_args.lm_weight * local_lm_scores
            
            local_scores = local_scores.view(batch, beam, self.odim_dict[language])
            if i == 0:
                local_scores[:, 1:, :] = self.logzero

            # accumulate scores
            eos_vscores = local_scores[:, :, self.eoss[language]] + vscores
            vscores = vscores.view(batch, beam, 1).repeat(1, 1, self.odim_dict[language])
            vscores[:, :, self.eoss[language]] = self.logzero
            vscores = (vscores + local_scores).view(batch, -1)  # (batch, odim * beam)

            # global pruning
            accum_best_scores, accum_best_ids = torch.topk(vscores, beam, 1)

            accum_odim_ids = (
                torch.fmod(accum_best_ids, self.odim_dict[language]).view(-1).data.cpu().tolist()
            )
            accum_padded_beam_ids = (
                (accum_best_ids // self.odim_dict[language] + pad_b).view(-1).data.cpu().tolist()
            )
            y_prev = yseq[:][:]
            yseq = self._index_select_list(yseq, accum_padded_beam_ids)
            yseq = self._append_ids(yseq, accum_odim_ids)
            vscores = accum_best_scores

            vidx = to_device(hs_pad, torch.LongTensor(accum_padded_beam_ids))

            # pick ended hyps
            if i >= minlen:
                k = 0
                penalty_i = (i + 1) * penalty
                thr = accum_best_scores[:, -1]
                for samp_i in six.moves.range(batch):
                    if stop_search[samp_i]:
                        k = k + beam
                        continue
                    for beam_j in six.moves.range(beam):
                        _vscore = None
                        if eos_vscores[samp_i, beam_j] > thr[samp_i]:
                            yk = y_prev[k][:]
                            if len(yk) <= maxlens[samp_i]:
                                _vscore = eos_vscores[samp_i][beam_j] + penalty_i
                            rnnlm_idx = k
                        elif i == maxlens[samp_i] - 1:
                            yk = yseq[k][:]
                            _vscore = vscores[samp_i][beam_j] + penalty_i
                            rnnlm_idx = accum_padded_beam_ids[k]
                        if _vscore:
                            yk.append(self.eoss[language])
                            if rnnlm:
                                _vscore += recog_args.lm_weight * rnnlm.final(
                                    rnnlm_state, index=rnnlm_idx
                                )
                            ended_hyps[samp_i].append(
                                {"yseq": yk, "score": _vscore.data.cpu().numpy()}
                            )
                        k = k + 1
            # end detection
            stop_search = [
                stop_search[samp_i]
                or end_detect(ended_hyps[samp_i], i)
                or i >= maxlens[samp_i]
                for samp_i in six.moves.range(batch)
            ]

            stop_search_summary = list(set(stop_search))

            if len(stop_search_summary) == 1 and stop_search_summary[0]:
                break

            if rnnlm:
                rnnlm_state = self._index_select_lm_state(rnnlm_state, 0, vidx)
            if ctc_scorer:
                ctc_state = ctc_scorer.index_select_state(ctc_state, accum_best_ids)

        torch.cuda.empty_cache()

        dummy_hyps = [
            {"yseq": [self.soss[language], self.eoss[language]], "score": numpy.array([-float("inf")])}
        ]
        ended_hyps = [
            ended_hyps[samp_i] if len(ended_hyps[samp_i]) != 0 else dummy_hyps
            for samp_i in six.moves.range(batch)
        ]
        if normalize_score:
            for samp_i in six.moves.range(batch):
                for x in ended_hyps[samp_i]:
                    x["score"] /= len(x["yseq"])

        nbest_hyps = [
            sorted(ended_hyps[samp_i], key=lambda x: x["score"], reverse=True)[
                : min(len(ended_hyps[samp_i]), recog_args.nbest)
            ]
            for samp_i in six.moves.range(batch)
        ]
        if prev:
            self.train()
        return nbest_hyps


================================================
FILE: code/ASR/Adapter/train.py
================================================
import logging
import os
import collections
from espnet.bin.asr_train import get_parser
from espnet.utils.dynamic_import import dynamic_import
from espnet.utils.deterministic_utils import set_deterministic_pytorch
from espnet.asr.pytorch_backend.asr_init import freeze_modules

from torch.nn.parallel import data_parallel
from torch.nn.utils.clip_grad import clip_grad_norm_
import torch
import numpy as np

import data_load
import random
import json
import sys
from utils import setup_logging, str2bool, dict_average
from utils import load_pretrained_model, load_head_from_pretrained_model, torch_save, torch_load
from utils import recognize_and_evaluate
import math
from e2e_asr_adaptertransformer import E2E as E2EAdapterTransformer
import matplotlib.pyplot as plt
import copy

def add_custom_arguments(parser):
    # EasyEspnet arguments
    parser.add_argument('--data_file', type=str, default=None)
    parser.add_argument("--root_path", type=str, required=True, help="Path to the ESPnet features, e.g.: <espnet_path>/egs/commonvoice/asr1/")
    #parser.add_argument("--root_path", type=str, default="/opt/espnet/egs/an4/asr1/")
    parser.add_argument('--dataset', type=str, required=True, help="Dataset name to be referred in data_load, e.g.: an4")
    #parser.add_argument("--dataset", type=str, default="an4")
    parser.add_argument("--exp", type=str, default="exp")
    parser.add_argument("--decoding_mode", type=str2bool, default=False, help="if true, then only perform decoding test")
    parser.add_argument("--load_pretrained_model", type=str, default="", nargs="?",
                    help="<model_path>:<load_modules>:<exclude_modules>")
    parser.add_argument("--compute_cer", type=str2bool, default=True)
    parser.add_argument("--decoding_config", type=str, default=None)
    parser.add_argument(
        "--bpemodel", type=bool, default=True
    )  # Set to true when testing CER/WERs
    parser.add_argument("--dist_train", type=str2bool, default=False)
    parser.add_argument("--local_rank", type=int, default=0)
    parser.add_argument("--result_label", type=str, default=None)
    parser.add_argument("--recog_json", type=str, default=None)
    parser.add_argument("--adam_lr", type=float, default=1e-3)
    
    # Adapter-related
    parser.add_argument("--use_adapters", type=str2bool, default=True, help="whether to inject adapters into the model")
    parser.add_argument("--shared_adapter", type=str, default=None, help="Share one adapter across all languages")
    parser.add_argument('--adapter_train_languages', type=str, default=None,
                                help="Splitted by _, e.g., en_zh_jp")
    parser.add_argument("--train_adapter_with_head", type=str2bool, 
                        default=False, help="whether to train adapter with language-specific head jointly")
    # SimAdapter-related
    parser.add_argument("--sim_adapter", type=str2bool, default=False)
    parser.add_argument("--fusion_languages", type=str, default=None)
    parser.add_argument("--guide_loss_weight", type=float, default=0.1)
    parser.add_argument("--guide_loss_weight_decay_steps", type=int, default=0)

    # MAML-related
    parser.add_argument("--meta_train", type=str2bool, default=False)
    parser.add_argument("--meta_lr", type=float, default=1.0, help="used for meta-learning outer step")
    
    # Others
    parser.add_argument("--load_head_from_pretrained_model", type=str, default="", nargs="?",
                    help="<model_path>")

def train_epoch(dataloader, model, optimizer, epoch=None):
    model.train()
    stats = collections.defaultdict(list)
    for batch_idx, data in enumerate(dataloader):
        fbank, seq_lens, tokens, language = data
        fbank, seq_lens, tokens = fbank.cuda(), seq_lens.cuda(), tokens.cuda()
        if isinstance(optimizer, dict):
            optimizer[language].zero_grad()
        else:
            optimizer.zero_grad()
        model.zero_grad()
        if args.ngpu <= 1 or args.dist_train:
            ctc_att_loss, sim_adapter_guide_loss = model(fbank, seq_lens, tokens, language)# .mean() # / self.accum_grad
        else:
            # apex does not support torch.nn.DataParallel
            ctc_att_loss, sim_adapter_guide_loss = (
                data_parallel(model, (fbank, seq_lens, tokens, language), range(args.ngpu))# .mean() # / self.accum_grad
            )
        loss = ctc_att_loss.mean()
        if args.sim_adapter:
            if hasattr(model, "module"):
                sim_adapter_reg_loss = model.module.get_fusion_regularization_loss()
            else:
                sim_adapter_reg_loss = model.get_fusion_regularization_loss()
            loss = loss + sim_adapter_reg_loss
            stats["sim_adapter_reg_loss_lst"].append(sim_adapter_reg_loss.item())
            if args.guide_loss_weight > 0:
                if args.guide_loss_weight_decay_steps > 0:
                    n_batch = len(dataloader)
                    current_iter = float(batch_idx + (epoch - 1) * n_batch)
                    frac_done = 1.0 * float(current_iter) / args.guide_loss_weight_decay_steps
                    current_weight = args.guide_loss_weight * max(0., 1. - frac_done)
                    stats["sim_adapter_guide_loss_weight"] = current_weight
                else:
                    current_weight = args.guide_loss_weight
                sim_adapter_guide_loss = sim_adapter_guide_loss.mean()
                loss = loss + current_weight * sim_adapter_guide_loss
                stats["sim_adapter_guide_loss_lst"].append(sim_adapter_guide_loss.item())

        if not hasattr(model, "module"):
            if hasattr(model, "acc") and model.acc is not None:
                stats["acc_lst"].append(model.acc)
                model.acc = None
        else:
            if hasattr(model, "acc") and model.module.acc is not None:
                stats["acc_lst"].append(model.module.acc)
                model.module.acc = None
        loss.backward()
        grad_norm = clip_grad_norm_(model.parameters(), args.grad_clip)
        if math.isnan(grad_norm):
            logging.warning("grad norm is nan. Do not update model.")
        else:
            if isinstance(optimizer, dict):
                optimizer[language].step()
            else:
                optimizer.step()
            stats["loss_lst"].append(loss.item())
        logging.warning(f"Training batch: {batch_idx+1}/{len(dataloader)}")
    return dict_average(stats)

def train_maml_epoch(dataloader, model, optimizer, epoch=None):
    model.train()
    stats = collections.defaultdict(list)
    
    for batch_idx, total_batches in enumerate(dataloader):
        i = batch_idx # current iteration in epoch
        len_dataloader = len(dataloader) # total iteration in epoch
        meta_iters = args.epochs * len_dataloader
        current_iter = float(i + (epoch - 1) * len_dataloader)
        frac_done = 1.0 * float(current_iter) / meta_iters
        current_outerstepsize = args.meta_lr * (1. - frac_done)

        weights_original = copy.deepcopy(model.state_dict())
        new_weights = []
        for total_batch in total_batches: # Iter by languages
            in_batch_size = int(total_batch[0].shape[0] / 2) # In-language batch size
            for meta_step in range(2): # Meta-train & meta-valid
                if meta_step == 1:
                    last_backup = copy.deepcopy(model.state_dict())
                else:
                    last_backup = None
                batch = list(copy.deepcopy(total_batch))
                for i_batch in range(len(batch)-1):
                    batch[i_batch] = batch[i_batch][meta_step*in_batch_size:(1+meta_step)*in_batch_size]
                batch = tuple(batch)
                
                fbank, seq_lens, tokens, language = batch
                fbank, seq_lens, tokens = fbank.cuda(), seq_lens.cuda(), tokens.cuda()
                optimizer.zero_grad()
                model.zero_grad()
                if args.ngpu <= 1 or args.dist_train:
                    loss = model(fbank, seq_lens, tokens, language).mean() # / self.accum_grad
                else:
                    # apex does not support torch.nn.DataParallel
                    loss = (
                        data_parallel(model, (fbank, seq_lens, tokens, language), range(args.ngpu)).mean() # / self.accum_grad
                    )
                # print(loss.item())
                loss.backward()
                grad_norm = clip_grad_norm_(model.parameters(), args.grad_clip)
                if math.isnan(grad_norm):
                    logging.warning("grad norm is nan. Do not update model.")
                else:
                    optimizer.step()
                
                if meta_step == 1: # Record meta valid
                    if not hasattr(model, "module"):
                        if hasattr(model, "acc") and model.acc is not None:
                            stats["acc_lst"].append(model.acc)
                            model.acc = None
                    else:
                        if hasattr(model, "acc") and model.module.acc is not None:
                            stats["acc_lst"].append(model.module.acc)
                            model.module.acc = None
                    stats["loss_lst"].append(loss.item())
                    stats["meta_lr"] = current_outerstepsize
                    optimizer.zero_grad()

            for name in last_backup:
                # Compute meta-gradient
                last_backup[name] = model.state_dict()[name] - last_backup[name]
            # Change back to the original parameters for the new language
            new_weights.append(last_backup) # updates.append(subtract_vars(self._model_state.export_variables(), last_backup))
            model.load_state_dict({ name: weights_original[name] for name in weights_original})
        
        ws = len(new_weights)
        # Compute average meta-gradient
        fweights = { name : new_weights[0][name]/float(ws) for name in new_weights[0] }
        for i in range(1, ws):
            for name in new_weights[i]:
                fweights[name] = fweights[name] + new_weights[i][name] / float(ws)
        model.load_state_dict({name : weights_original[name] + (fweights[name] * current_outerstepsize) for name in weights_original})

        logging.warning(f"Training batch: {batch_idx+1}/{len(dataloader)}")
    return dict_average(stats)

def test(epoch, dataloader, model, model_path=None, language=None, visualize_sim_adapter=False):
    if model_path:
        torch_load(model_path, model)
    orig_model = None
    if hasattr(model, "module"):
        orig_model = model
        model = model.module
    model.eval()
    stats = collections.defaultdict(list)
    for batch_idx, data in enumerate(dataloader):
        logging.warning(f"Testing batch: {batch_idx+1}/{len(dataloader)}")
        if len(data) == 4:
            fbank, seq_lens, tokens, language = data
        else:
            assert language is not None
            fbank, seq_lens, tokens = data
        fbank, seq_lens, tokens = fbank.cuda(), seq_lens.cuda(), tokens.cuda()
        with torch.no_grad():
            loss = model(fbank, seq_lens, tokens, language)
        
        if visualize_sim_adapter:
            atts = model.calculate_sim_adapter_attentions(fbank, seq_lens, tokens, language)
            init_mat = lambda: np.zeros((len(model.fusion_languages),))
            avg_atts = collections.defaultdict(init_mat)
            count = collections.defaultdict(int)
            for key in atts.keys():
                avg_atts[key] = avg_atts[key] + atts[key].sum(axis=(0, 1))
                count[key] = count[key] + atts[key].shape[0] * atts[key].shape[1]
        stats["loss_lst"].append(loss.item())
        if not hasattr(model, "module"):
            if model.acc is not None:
                stats["acc_lst"].append(model.acc)
                model.acc = None
        else:
            if model.module.acc is not None:
                stats["acc_lst"].append(model.module.acc)
                model.module.acc = None
    if visualize_sim_adapter:
        for key in avg_atts.keys():
            avg_atts[key] = avg_atts[key] / count[key]
            logging.warning(f"Attention scores of {key}: {avg_atts[key]}")
        fig = plt.figure(figsize=(16, 8))
        ax = fig.subplots()
        atts, labels = [], []
        for key in avg_atts.keys():
            atts.append(avg_atts[key])
            labels.append(key)
        atts = np.stack(atts)
        tick_marks = np.arange(len(labels))
        ax.set_yticks(tick_marks)
        ax.set_yticklabels(labels)
        x_labels = list(sorted(model.fusion_languages))
        ax.set_xticks(np.arange(len(x_labels)))
        ax.set_xticklabels(x_labels)
        ax.imshow(atts)
        import itertools
        for i, j in itertools.product(range(atts.shape[0]), range(atts.shape[1])):
            plt.text(j, i, "{:0.2f}".format(atts[i, j]),
                    horizontalalignment="center",
                    color="white")
        fig.tight_layout()
        fig.savefig(f"{args.outdir}/att_{epoch}.png")
        plt.close()
    if orig_model is not None:
        model = orig_model
    return dict_average(stats)

def train(dataloaders, model, optimizer, save_path):
    train_loader, val_loader, test_loader = dataloaders
    best_loss = float("inf")
    early_stop = 0
    log_json = []
    for epoch in range(args.start_epoch, args.epochs + 1):
        early_stop += 1
        epoch_stats = collections.OrderedDict(epoch=epoch)
        train_stats = train_epoch(train_loader, model, optimizer, epoch)
        valid_stats = test(f"val_{epoch}", val_loader, model, visualize_sim_adapter=args.sim_adapter)
        if best_loss > valid_stats["loss"]:  # Save loss best model
            best_loss = valid_stats["loss"]
            torch_save(model, save_path)
            early_stop = 0

        test_stats = test(f"test_{epoch}", test_loader, model)
        logging.warning(
            f"Epoch: {epoch}, Iteration: {epoch * len(train_loader)}, "
            + f"train loss: {train_stats['loss']:.4f}, dev loss: {valid_stats['loss']:.3f}, test loss: {test_stats['loss']:.3f}"
        )

        torch_save(model, f"{args.outdir}/snapshot.ep.{epoch}", optimizer=optimizer)
        for key in sorted(list(set(list(train_stats.keys()) + list(test_stats.keys())))):
            if not key.endswith("_lst"):
                if key in train_stats:
                    epoch_stats[f"main/{key}"] = train_stats[key]
                if key in valid_stats:
                    epoch_stats[f"validation/main/{key}"] = valid_stats[key]
                if key in test_stats:
                    epoch_stats[f"test/main/{key}"] = test_stats[key]
                
        log_json.append(epoch_stats)
        with open(f"{args.outdir}/log", "w") as f:
            json.dump(log_json, f,
                indent=4,
                ensure_ascii=False,
                separators=(",", ": "),
            )
            logging.warning(f"Log saved at {args.outdir}/log")
            
        if args.patience > 0 and early_stop >= args.patience:
            test_stats = test("test_best", test_loader, model, save_path)
            logging.warning(f"=====Early stop! Final best test loss: {test_stats['loss']}")
            break

if __name__ == "__main__":
    # 执行该命令运行4 GPU训练:CUDA_VISIBLE_DEVICES=0,1,2,3 python -m torch.distributed.launch --nproc_per_node=2 train.py
    setup_logging(verbose=0)  # Should come first before other package import logging
    parser = get_parser()
    add_custom_arguments(parser)

    arg_list = sys.argv[1:] + [
        "--dict", '',
        #"--dataset", "_".join("cv mt cnh ky dv sl el lv fyNL sah".split()),
    ]
    if "--config" not in arg_list:
        arg_list += ["--config", "config/train.yaml"]
    if "--outdir" not in arg_list:
        arg_list += ["--outdir", '']

    args, _ = parser.parse_known_args(arg_list)
    # Use all GPUs
    ngpu = torch.cuda.device_count() if args.ngpu is None else args.ngpu
    os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(
                [str(item) for item in range(ngpu)])
    logging.warning(f"ngpu: {ngpu}")
    
    # set random seed
    logging.info("random seed = %d" % args.seed)
    random.seed(args.seed)
    np.random.seed(args.seed)
    set_deterministic_pytorch(args)
    torch.cuda.manual_seed(args.seed)
    if ngpu > 1:
        torch.cuda.manual_seed_all(args.seed) # multi-gpu setting

    model_module =  "e2e_asr_adaptertransformer:E2E"
    model_class = E2EAdapterTransformer
    model_class.add_arguments(parser)
    args = parser.parse_args(arg_list)

    setattr(args, "conf_name", ".".join(os.path.basename(args.config).split(".")[:-1]))
    if not args.outdir:
        args.outdir = f"./outputs/results_{args.dataset}/{args.conf_name}"
    if not os.path.exists(args.outdir):
        os.makedirs(args.outdir)
    
    setattr(args, "ngpu", ngpu)
    if args.data_file is not None:
        args.root_path = args.data_file
    
    if args.ngpu > 1:

        if args.opt == "noam" and hasattr(args, "transformer_lr"):
            logging.warning(f"Multi-GPU training: increase transformer lr {args.transformer_lr} --> {args.transformer_lr * np.sqrt(args.ngpu)}")
            args.transformer_lr = args.transformer_lr * np.sqrt(args.ngpu)
        elif (args.opt == "adam" or args.meta_train) and hasattr(args, "adam_lr"):
            logging.warning(f"Multi-GPU training: increase adam lr {args.adam_lr} --> {args.adam_lr * np.sqrt(args.ngpu)}")
            args.adam_lr = args.adam_lr * np.sqrt(args.ngpu)

        if args.dist_train:
            torch.distributed.init_process_group(backend="nccl")
            local_rank = torch.distributed.get_rank()
            args.local_rank = local_rank
            torch.cuda.set_device(local_rank)
            device = torch.device("cuda", local_rank)
        else:
            logging.warning(
                "Training batch size is automatically increased (%d -> %d)"
                % (args.batch_size, args.batch_size * args.ngpu)
            )
            args.batch_size *= args.ngpu
    
    if args.accum_grad > 1:
        logging.warning(
                "gradient accumulation is not implemented. batch size is increased (%d -> %d)"
                % (args.batch_size, args.batch_size * args.accum_grad)
            )
        args.batch_size *= args.accum_grad
        args.accum_grad = 1

    dataloaders = {}
    token_dict = {}
    idim, odim_dict = None, {}
    args.dataset = args.dataset.split("_")
    languages = args.dataset
    data_load_languages = languages

    if args.adapter_train_languages is not None:
        args.adapter_train_languages = args.adapter_train_languages.split("_")
        data_load_languages = args.adapter_train_languages
    else:
        logging.warning("adapter_train_languages is None, will use all datasets for training")
        args.adapter_train_languages = args.dataset
    
    dataloaders, (idim, odim_dict) = data_load.load_multilingual_data(args.root_path, args.dataset, args, data_load_languages)
    for idx, data_set in enumerate(args.dataset):
        if languages[idx] not in data_load_languages:
            continue
        token_dict[languages[idx]] = data_load.load_token_list(
                os.path.join(args.root_path, data_load.data_config[data_set]["token"])
        )
    setattr(args, "char_list", token_dict)
    model = model_class(idim, odim_dict, args, languages)

    model_conf = args.outdir + "/model.json"
    with open(model_conf, "wb") as f:
        logging.info("writing a model config file to " + model_conf)
        f.write(
            json.dumps(
                (idim, odim_dict, vars(args)),
                indent=4,
                ensure_ascii=False,
                sort_keys=True,
            ).encode("utf_8")
        )

    model.cuda()
    if args.freeze_mods:
        model, model_params = freeze_modules(model, args.freeze_mods)
    else:
        model_params = model.parameters()

    logging.warning("Trainable parameters:")
    for name, parameter in model.named_parameters():
        if parameter.requires_grad:
            logging.warning(name)
            
    # Setup an optimizer
    if args.meta_train:
        logging.warning(f"Use Adam optimizer with lr={args.adam_lr}, beta0=0 for meta-training inner step")
        optimizer = torch.optim.Adam(model_params, lr=args.adam_lr, betas=(0, 0.999), weight_decay=args.weight_decay)
    else:
        if args.opt == "adadelta":
            optimizer = torch.optim.Adadelta(
                model_params, rho=0.95, eps=args.eps, weight_decay=args.weight_decay
            )
        elif args.opt == "adam":
            logging.warning(f"Using Adam optimizer with lr={args.adam_lr}")
            optimizer = torch.optim.Adam(model_params, lr=args.adam_lr, weight_decay=args.weight_decay)
        elif args.opt == "noam":
            from espnet.nets.pytorch_backend.transformer.optimizer import get_std_opt
            optimizer = get_std_opt(
                model_params, args.adim, args.transformer_warmup_steps, args.transformer_lr
            )
    
    if len(args.adapter_train_languages) > 1 and not args.sim_adapter and not args.shared_adapter:
        model_params = collections.defaultdict(list)
        optimizer = {}
        for lang in args.adapter_train_languages:
            for name, parameter in model.named_parameters():
                if parameter.requires_grad and lang in name.split("."):
                    model_params[lang].append(parameter)
            logging.warning(f"Number of trainable parameters for language {lang} " + str(sum(p.numel() for p in model_params[lang])))
            optimizer[lang] = torch.optim.Adam(model_params[lang], lr=args.adam_lr, weight_decay=args.weight_decay)
    
    # Resume from a snapshot
    if args.resume:
        logging.warning("resumed from %s" % args.resume)
        torch_load(args.resume, model, optimizer)
        setattr(args, "start_epoch", int(args.resume.split('.')[-1]) + 1)
    else:
        setattr(args, "start_epoch", 1)

    if args.load_pretrained_model:
        model_path, modules_to_load, exclude_modules = args.load_pretrained_model.split(":")
        logging.warning("load pretrained model from %s" % args.load_pretrained_model)
        load_pretrained_model(model=model, model_path=model_path, 
                                modules_to_load=modules_to_load, exclude_modules=exclude_modules)
    if args.load_head_from_pretrained_model:
        logging.warning("load pretrained model head from %s" % args.load_head_from_pretrained_model)
        load_head_from_pretrained_model(model=model, model_path=args.load_head_from_pretrained_model)
        
    logging.warning(
        "Total parameter of the model = "
        + str(sum(p.numel() for p in model.parameters()))
    )
    logging.warning(
        "Trainable parameter of the model = "
        + str(sum(p.numel() for p in filter(lambda x: x.requires_grad, model.parameters())))
    )

    if args.ngpu > 1 and args.dist_train:
        model = torch.nn.parallel.DistributedDataParallel(model, 
                                                        device_ids=[local_rank],
                                                        output_device=local_rank,
                                                        find_unused_parameters=True,
                                                        )
    
    save_path = f"{args.outdir}/model.loss.best"
    if args.meta_train:
        train_epoch = train_maml_epoch
    if not args.decoding_mode:
        train(dataloaders, model, optimizer, save_path)
    if args.compute_cer and args.local_rank == 0:
        # For CER/WER computing
        for idx, dataset in enumerate(args.dataset):
            language = languages[idx]
            if args.adapter_train_languages and not (language in args.adapter_train_languages):
                continue
            if args.bpemodel and "bpemodel" in data_load.data_config[dataset]:
                logging.warning(f"load bpe model for dataset {dataset}")
                args.bpemodel = data_load.load_bpemodel(args.root_path, dataset)
            dataloaders, _ = data_load.load_data(args.root_path, dataset, args)
            splits = ["test", "val"]
            for split in splits:
                logging.warning(f"---------Recognizing {dataset} {split}----------")
                args.result_label = f"{args.outdir}/{dataset}_{split}_recog.json"
                if not data_load.data_config[dataset][split]:
                    split_path = os.path.join(args.root_path, f"{args.root_path}/tmp_dev_set_{dataset}.json")
                else:
                    split_path = data_load.data_config[dataset][split]
                args.recog_json = os.path.join(args.root_path, split_path)
                idx = ["train", "val", "test"].index(split)
                test_stats = test(f"{split}_best", dataloaders[idx],
                                model, 
                                save_path, 
                                language=language, 
                                visualize_sim_adapter=args.sim_adapter)
                logging.warning(f"Loss: {test_stats['loss']}")
                err_dict = recognize_and_evaluate(dataloaders[idx], model, args, language, 
                                    model_path=save_path, wer=True, write_to_json=True)
                logging.warning(f"CER: {err_dict['cer']['err']}")
                logging.warning(f"WER: {err_dict['wer']['err']}")

================================================
FILE: code/ASR/Adapter/utils.py
================================================
import torch
import logging
from espnet.asr.asr_utils import add_results_to_json
import argparse
import numpy as np
import collections
import json


def load_head_from_pretrained_model(model, model_path):
    model_dict = torch.load(model_path, map_location=lambda storage, loc: storage)
    if "model" in model_dict.keys():
        model_dict = model_dict["model"]
    src_dict = {k: v for k, v in model_dict.items() if "decoder.embed." in k or "ctc." in k or "decoder.output_layer." in k}
    dst_state = model.state_dict()
    dst_state.update(src_dict)
    for key in dst_state.keys():
        if key in src_dict.keys():
            logging.info("loading " + key)
    model.load_state_dict(dst_state)

def load_adapter_from_pretrained_model(model, model_path, src_adapter, tgt_adapter):
    '''
    src_adapter, tgt_adapter: str, names of the source and target adapters to load parameters
    '''
    model_dict = torch.load(model_path, map_location=lambda storage, loc: storage)
    if "model" in model_dict.keys():
        model_dict = model_dict["model"]
    src_dict = {k.replace(src_adapter, tgt_adapter): v for k, v in model_dict.items() if f"adapters.{src_adapter}" in k}
    dst_state = model.state_dict()
    dst_state.update(src_dict)
    for key in dst_state.keys():
        if key in src_dict.keys():
            logging.info("loading " + key)
    model.load_state_dict(dst_state)

# ====================  EasyEspnet functions =================================

def str2bool(str):
	return True if str.lower() == 'true' else False
def setup_logging(verbose=1):
    if verbose > 0:
        logging.basicConfig(
            level=logging.INFO,
            format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
        )
    else:
        logging.basicConfig(
            level=logging.WARN,
            format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
        )
        logging.warning("Skip DEBUG/INFO messages")

# Training stats
def dict_average(dic):
    avg_key, avg_val = [], []
    for key, lst in dic.items():
        if key.endswith("_lst"):
            avg_key.append(key[:-4])
            avg_val.append(np.mean(lst))
    for key, val in zip(avg_key, avg_val):
        dic[key] = val
    return dic

# Load and save
def load_pretrained_model(model, model_path, modules_to_load=None, exclude_modules=None):
    '''
    load_pretrained_model(model=model, model_path="", 
                                modules_to_load=None, exclude_modules="")
    '''
    model_dict = torch.load(model_path, map_location=lambda storage, loc: storage)
    if exclude_modules:
        for e in exclude_modules.split(","):
            model_dict = {k: v for k, v in model_dict.items() if not k.startswith(e)}

    if not modules_to_load:
        src_dict = model_dict
    else:
        src_dict = {}
        for module in modules_to_load.split(","):
            src_dict.update({k: v for k, v in model_dict.items() if k.startswith(module)})
    
    dst_state = model.state_dict()
    dst_state.update(src_dict)
    model.load_state_dict(dst_state)
def torch_save(model, save_path, optimizer=None, local_rank=0):
    if local_rank != 0:
        return
    if hasattr(model, "module"):
        state_dict = model.module.state_dict() if not optimizer else collections.OrderedDict(model=model.module.state_dict(), optimizer=optimizer.state_dict())
    else:
        state_dict = model.state_dict() if not optimizer else collections.OrderedDict(model=model.state_dict(), optimizer=optimizer.state_dict())
    torch.save(state_dict, save_path)
def torch_load(snapshot_path, model, optimizer=None):
    # load snapshot
    snapshot_dict = torch.load(snapshot_path, map_location=lambda storage, loc: storage)
    if not "model" in snapshot_dict.keys():
        model_dict = snapshot_dict
        snapshot_dict = collections.OrderedDict(model=model_dict)
    if hasattr(model, "module"):
        model.module.load_state_dict(snapshot_dict["model"])
    else:
        model.load_state_dict(snapshot_dict["model"])
    if optimizer:
        optimizer.load_state_dict(snapshot_dict["optimizer"])
    del snapshot_dict

# Decoding
def compute_wer(ref, hyp, normalize=False):
    """Compute Word Error Rate.
        [Reference]
            https://martin-thoma.com/word-error-rate-calculation/
    Args:
        ref (list): words in the reference transcript
        hyp (list): words in the predicted transcript
        normalize (bool, optional): if True, divide by the length of ref
    Returns:
        wer (float): Word Error Rate between ref and hyp
        n_sub (int): the number of substitution
        n_ins (int): the number of insertion
        n_del (int): the number of deletion
    """
    # Initialisation
    d = np.zeros((len(ref) + 1) * (len(hyp) + 1), dtype=np.uint16)
    d = d.reshape((len(ref) + 1, len(hyp) + 1))
    for i in range(len(ref) + 1):
        for j in range(len(hyp) + 1):
            if i == 0:
                d[0][j] = j
            elif j == 0:
                d[i][0] = i

    # Computation
    for i in range(1, len(ref) + 1):
        for j in range(1, len(hyp) + 1):
            if ref[i - 1] == hyp[j - 1]:
                d[i][j] = d[i - 1][j - 1]
            else:
                sub_tmp = d[i - 1][j - 1] + 1
                ins_tmp = d[i][j - 1] + 1
                del_tmp = d[i - 1][j] + 1
                d[i][j] = min(sub_tmp, ins_tmp, del_tmp)

    wer = d[len(ref)][len(hyp)]

    # Find out the manipulation steps
    x = len(ref)
    y = len(hyp)
    error_list = []
    while True:
        if x == 0 and y == 0:
            break
        else:
            if x > 0 and y > 0:
                if d[x][y] == d[x - 1][y - 1] and ref[x - 1] == hyp[y - 1]:
                    error_list.append("C")
                    x = x - 1
                    y = y - 1
                elif d[x][y] == d[x][y - 1] + 1:
                    error_list.append("I")
                    y = y - 1
                elif d[x][y] == d[x - 1][y - 1] + 1:
                    error_list.append("S")
                    x = x - 1
                    y = y - 1
                else:
                    error_list.append("D")
                    x = x - 1
            elif x == 0 and y > 0:
                if d[x][y] == d[x][y - 1] + 1:
                    error_list.append("I")
                    y = y - 1
                else:
                    error_list.append("D")
                    x = x - 1
            elif y == 0 and x > 0:
                error_list.append("D")
                x = x - 1
            else:
                raise ValueError

    n_sub = error_list.count("S")
    n_ins = error_list.count("I")
    n_del = error_list.count("D")
    n_cor = error_list.count("C")

    assert wer == (n_sub + n_ins + n_del)
    assert n_cor == (len(ref) - n_sub - n_del)

    if normalize:
        wer /= len(ref)
    return wer, n_sub, n_ins, n_del, n_cor

def token2text(tokens, bpemodel=None):
    if bpemodel:
        text = bpemodel.decode_pieces(tokens)
    else:
        text = (
            " ".join(tokens)
            .replace(" ", "")
            .replace("<space>", " ")
        )  # sclite does not consider the number of spaces when splitting
    return text
def recognize_and_evaluate(dataloader, model, args, model_path=None, wer=False, write_to_json=False):
    if model_path:
        torch_load(model_path, model)
    orig_model = model
    if hasattr(model, "module"):
        model = model.module
    if write_to_json:
        # read json data
        assert args.result_label and args.recog_json
        with open(args.recog_json, "rb") as f:
            js = json.load(f)["utts"]
            new_js = {}
    model.eval()
    recog_args = {
        "beam_size": args.beam_size,
        "penalty": args.penalty,
        "ctc_weight": args.ctc_weight,
        "maxlenratio": args.maxlenratio,
        "minlenratio": args.minlenratio,
        "lm_weight": args.lm_weight,
        "rnnlm": args.rnnlm,
        "nbest": args.nbest,
        "space": args.sym_space,
        "blank": args.sym_blank,
    }
    recog_args = argparse.Namespace(**recog_args)

    #progress_bar = tqdm(dataloader)
    #progress_bar.set_description("Testing CER/WERs")
    err_dict = (
        dict(cer=None)
        if not wer
        else dict(cer=collections.defaultdict(int), wer=collections.defaultdict(int))
    )
    with torch.no_grad():
        for batch_idx, data in enumerate(dataloader):
            logging.warning(f"Testing CER/WERs: {batch_idx+1}/{len(dataloader)}")
            fbank, ilens, tokens = data
            fbanks = []
            for i, fb in enumerate(fbank):
                fbanks.append(fb[: ilens[i], :])
            fbank = fbanks
            nbest_hyps = model.recognize_batch(
               fbank, recog_args, char_list=None, rnnlm=None
            )
            y_hats = [nbest_hyp[0]["yseq"][1:-1] for nbest_hyp in nbest_hyps]
            if write_to_json:
                for utt_idx in range(len(fbank)):
                    name = dataloader.dataset[batch_idx][utt_idx][0]
                    new_js[name] = add_results_to_json(
                        js[name], nbest_hyps[utt_idx], args.char_list
                    )
            for i, y_hat in enumerate(y_hats):
                y_true = tokens[i]

                hyp_token = [
                    args.char_list[int(idx)] for idx in y_hat if int(idx) != -1
                ]
                ref_token = [
                    args.char_list[int(idx)] for idx in y_true if int(idx) != -1
                ]
                for key in sorted(err_dict.keys()):  # cer then wer
                    if key == "wer":
                        ref_token = token2text(ref_token, args.bpemodel).split()
                        hyp_token = token2text(hyp_token, args.bpemodel).split()
                        logging.debug("HYP: " + str(hyp_token))
                        logging.debug("REF: " + str(ref_token))
                    utt_err, utt_nsub, utt_nins, utt_ndel, utt_ncor = compute_wer(
                        ref_token, hyp_token
                    )

                    err_dict[key]["n_word"] += len(ref_token)
                    if utt_err != 0:
                        err_dict[key]["n_err"] += utt_err  # Char / word error
                        err_dict[key]["n_ser"] += 1  # Sentence error
                    err_dict[key]["n_cor"] += utt_ncor
                    err_dict[key]["n_sub"] += utt_nsub
                    err_dict[key]["n_ins"] += utt_nins
                    err_dict[key]["n_del"] += utt_ndel
                    err_dict[key]["n_sent"] += 1

    for key in err_dict.keys():
        err_dict[key]["err"] = err_dict[key]["n_err"] / err_dict[key]["n_word"] * 100.0
        err_dict[key]["ser"] = err_dict[key]["n_ser"] / err_dict[key]["n_word"] * 100.0
    torch.cuda.empty_cache()
    if write_to_json:
        with open(args.result_label, "wb") as f:
            f.write(
                json.dumps(
                    {"utts": new_js}, indent=4, ensure_ascii=False, sort_keys=True
                ).encode("utf_8")
            )
    model = orig_model
    return err_dict

================================================
FILE: code/ASR/CMatch/README.md
================================================
# CMatch: Cross-domain Speech Recognition with Unsupervised Character-level Distribution Matching

**NOTICE:** The latest code of CMatch has been moved to https://github.com/microsoft/NeuralSpeech/tree/master/CMatchASR for unified organization. Please refer to that repo.

---

This project implements our paper [Cross-domain Speech Recognition with Unsupervised Character-level Distribution Matching](https://arxiv.org/abs/2104.07491) based on [EasyEspnet](https://github.com/jindongwang/EasyEspnet). Please refer to [EasyEspnet](https://github.com/jindongwang/EasyEspnet) for the program introduction, installation and usage. And our paper [1] for the method and technical details.

## Run

After extracting features using Espnet and set the data folder path as introduced in [EasyEspnet](https://github.com/jindongwang/EasyEspnet).

You need to check or modify in `train.py arg_list`, config should be in ESPnet config style (remember to include decoding information if you want to compute cer/wer), then, you can run train.py. For example, 

```
python train.py --root_path libriadapt/asr1 --dataset libriadapt_en_us_clean_matrix --config config/train.yaml
```

This commands pre-trains an ASR model on the specified dataset. Results (log, model, snapshots) are saved in results_(dataset)/(config_name) by default.

### Cross-domain Adaptation

We provide three methods for cross-domain adaptation in this implementation: adversarial training (adv), maximum mean discrepancy (mmd) and our CMatch.

For the adversarial and mmd, you can refer to and modify the target dataset, pre-trained model setting in `config/{mmd, adv}_example.yaml`, and then execute this command for example:

```
python train.py --root_path libriadapt/asr1 --dataset libriadapt_en_us_clean_matrix --config config/mmd_example.yaml
```

For the CMatch method, there are two steps.

#### Step 1:
First we need to obtain the pseudo labels, for example, suppose we trained the source model using the command above, we need to re-execute it with the `--pseudo_labeling` being set `true` and the `--tgt_dataset` specifying the target dataset:

```
python train.py --root_path libriadapt/asr1 --dataset libriadapt_en_us_clean_matrix --config config/train.yaml --pseudo_labeling true --tgt_dataset libriadapt_en_us_clean_respeaker
```

So that we will get the pseudo labels for `libriadapt_en_us_clean_respeaker` dataset.

#### Step 2:
Then we can refer to and modify the target dataset, pre-trained model, non-character symbol, and pseudo label setting in `config/{ctc_align, pseudo_ctc_pred, frame_average}_example.yaml`, and then execute this command for example:
```
python train.py --root_path libriadapt/asr1 --dataset libriadapt_en_us_clean_matrix --config config/ctc_align_example.yaml
```

### Demo

TBD

## Decoding and WER/CER evaluation

Set `--decoding_mode` to `true` to perform decoding and CER/WER evaluation. For example:

```
python train.py --root_path libriadapt/asr1 --dataset libriadapt_en_us_clean_matrix --config config/train.yaml --decoding_mode true
```

## Distributed training

Following EasyEspnet, you can also perform distributed training which is much faster. For example, using 4 GPUs, 1 node: 

```
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m torch.distributed.launch --nproc_per_node=4 train.py --dist_train true --root_path libriadapt/asr1 --dataset libriadapt_en_us_clean_matrix --config config/mmd_example.yaml
```

## Acknowledgement

- ESPNet: https://github.com/espnet/espnet
- EasyEspnet: https://github.com/jindongwang/EasyEspnet
- NeuralSP: https://github.com/hirofumi0810/neural_sp

## Contact

- [Wenxin Hou](https://houwenxin.github.io/): houwx001@gmail.com
- [Jindong Wang](http://www.jd92.wang/): jindongwang@outlook.com



## References

[1] Wenxin Hou, Jindong Wang, Xu Tan, Tao Qin, Takahiro Shinozaki, "Cross-domain Speech Recognition with Unsupervised Character-level Distribution Matching", submitted to INTERSPEECH 2021.


================================================
FILE: code/ASR/CMatch/config/adv_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32 # There are two datasets, reduce batch size by half
transformer-lr: 6.0
#batch-size: 64
#transformer-lr: 10.0
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 5
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
#transformer-lr: 6.0
transformer-warmup-steps: 25000
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

# Transfer related
load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best::
tgt_dataset: <target_dataset_to_adapt_to>

transfer_type: adv
transfer_loss_weight: 0.3

================================================
FILE: code/ASR/CMatch/config/ctc_align_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32 # There are two datasets, reduce batch size by half
transformer-lr: 6.0
#batch-size: 64
#transformer-lr: 10.0
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 5
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
#transformer-lr: 6.0
transformer-warmup-steps: 25000
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

# Transfer related
load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best::
tgt_dataset: <target_dataset_to_adapt_to>
non_char_symbols: <non_character_symbols_to_filter_in_cmatch> # Example: 0_1_2_29_30
transfer_type: cmatch
transfer_loss_weight: 10.0
cmatch_method: ctc_align
tranfer_loss_weight_warmup_steps: 0
self_training: True
pseudo_label_json: <pseudo_label_json_of_target_dataset>

================================================
FILE: code/ASR/CMatch/config/frame_average_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32 # There are two datasets, reduce batch size by half
transformer-lr: 6.0
#batch-size: 64
#transformer-lr: 10.0
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 5
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
#transformer-lr: 6.0
transformer-warmup-steps: 25000
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

# Transfer related
load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best::
tgt_dataset: <target_dataset_to_adapt_to>
non_char_symbols: <non_character_symbols_to_filter_in_cmatch> # Example: 0_1_2_29_30
transfer_type: cmatch
transfer_loss_weight: 10.0
cmatch_method: frame_average
self_training: True
pseudo_label_json: <pseudo_label_json_of_target_dataset>

================================================
FILE: code/ASR/CMatch/config/mmd_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32 # There are two datasets, reduce batch size by half
transformer-lr: 6.0
#batch-size: 64
#transformer-lr: 10.0
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 5
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
#transformer-lr: 6.0
transformer-warmup-steps: 25000
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

# Transfer related
load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best::
tgt_dataset: <target_dataset_to_adapt_to>

transfer_type: mmd
transfer_loss_weight: 10.0

================================================
FILE: code/ASR/CMatch/config/pseudo_ctc_pred_example.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32 # There are two datasets, reduce batch size by half
transformer-lr: 6.0
#batch-size: 64
#transformer-lr: 10.0
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 5
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
#transformer-lr: 6.0
transformer-warmup-steps: 25000
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7

# Transfer related
load_pretrained_model: <pretrained_model:modules_to_load:excluded_modules> # Example: <path_to_source_model>/model.loss.best::
tgt_dataset: <target_dataset_to_adapt_to>

transfer_type: cmatch
transfer_loss_weight: 10.0
cmatch_method: pseudo_ctc_pred
tranfer_loss_weight_warmup_steps: 0
self_training: True
pseudo_label_json: <pseudo_label_json_of_target_dataset>
multi_enc_repr_num: 1 # Number of encoder layers to use for adaptation, starting from the last layers
non_char_symbols: <non_character_symbols_to_filter_in_cmatch> # Split by "_", Example: 0_1_2_29_30

================================================
FILE: code/ASR/CMatch/config/train.yaml
================================================
# network architecture
# encoder related
elayers: 12
eunits: 2048
# decoder related
dlayers: 6
dunits: 2048
# attention related
adim: 256
aheads: 4

# hybrid CTC/attention
mtlalpha: 0.3

# label smoothing
lsm-weight: 0.1

# minibatch related
batch-size: 32
maxlen-in: 512  # if input length  > maxlen-in, batchsize is automatically reduced
maxlen-out: 150 # if output length > maxlen-out, batchsize is automatically reduced

# optimization related
sortagrad: 0 # Feed samples from shortest to longest ; -1: enabled for all epochs, 0: disabled, other: enabled for 'other' epochs
opt: noam
accum-grad: 1
grad-clip: 5
patience: 5
epochs: 100
dropout-rate: 0.1

# transformer specific setting
backend: pytorch
model-module: "espnet.nets.pytorch_backend.e2e_asr_transformer:E2E"
transformer-input-layer: conv2d     # encoder architecture type
transformer-lr: 6.0
transformer-warmup-steps: 25000
transformer-attn-dropout-rate: 0.0
transformer-length-normalized-loss: false
transformer-init: pytorch


# Decoding related
beam-size: 10
penalty: 0.0
maxlenratio: 0.0
minlenratio: 0.0
ctc-weight: 0.3
lm-weight: 0.7


================================================
FILE: code/ASR/CMatch/ctc_aligner.py
================================================
import torch
import numpy as np

'''
Borrowed and modified from neural_sp: 
https://github.com/hirofumi0810/neural_sp/blob/154d9248b54e3888797fd81f93adc4700a75509a/neural_sp/models/seq2seq/decoders/ctc.py#L625 
'''

LOG_0 = -1e10
LOG_1 = 0

def np2tensor(array, device=None):
    """Convert form np.ndarray to torch.Tensor.
    Args:
        array (np.ndarray): A tensor of any sizes
    Returns:
        tensor (torch.Tensor):
    """
    tensor = torch.from_numpy(array).to(device)
    return tensor
def pad_list(xs, pad_value=0., pad_left=False):
    """Convert list of Tensors to a single Tensor with padding.
    Args:
        xs (list): A list of length `[B]`, which contains Tensors of size `[T, input_size]`
        pad_value (float):
        pad_left (bool):
    Returns:
        xs_pad (FloatTensor): `[B, T, input_size]`
    """
    bs = len(xs)
    max_time = max(x.size(0) for x in xs)
    xs_pad = xs[0].new_zeros(bs, max_time, * xs[0].size()[1:]).fill_(pad_value)
    for b in range(bs):
        if len(xs[b]) == 0:
            continue
        if pad_left:
            xs_pad[b, -xs[b].size(0):] = xs[b]
        else:
            xs_pad[b, :xs[b].size(0)] = xs[b]
    return xs_pad
class CTCForcedAligner(object):
    def __init__(self, blank=0, char_list=None):
        self.blank = blank
        self.symbols = [0, 1, 2, 29, 30]
        self.char_list = char_list
    def __call__(self, logits, elens, ys):
        """Forced alignment with references.
        Args:
            logits (FloatTensor): `[B, T, vocab]`
            elens (List): length `[B]`
            ys (List): length `[B]`, each of which contains a list of size `[L]`
            ylens (List): length `[B]`
        Returns:
            trigger_points (IntTensor): `[B, L]`
        """
        ylens = np2tensor(np.fromiter([len(y) for y in ys], dtype=np.int32))
        with torch.no_grad():
            ys = [np2tensor(np.fromiter(y, dtype=np.int64), logits.device) for y in ys]
            ys_in_pad = pad_list(ys, 0)
            # zero padding
            mask = make_pad_mask(elens.to(logits.device))
            mask = mask.unsqueeze(2).expand_as(logits)
            logits = logits.masked_fill_(mask == 0, LOG_0)
            log_probs = torch.log_softmax(logits, dim=-1).transpose(0, 1)  # `[T, B, vocab]`

            trigger_points = self.align(log_probs, elens, ys_in_pad, ylens)
        return trigger_points

    def align(self, log_probs, elens, ys, ylens, add_eos=True):
        """Calculte the best CTC alignment with the forward-backward algorithm.
        Args:
            log_probs (FloatTensor): `[T, B, vocab]`
            elens (FloatTensor): `[B]`
            ys (FloatTensor): `[B, L]`
            ylens (FloatTensor): `[B]`
            add_eos (bool): Use the last time index as a boundary corresponding to <eos>
        Returns:
            trigger_points (IntTensor): `[B, L]`
        """
        xmax, bs, vocab = log_probs.size()

        path = _label_to_path(ys, self.blank)
        path_lens = 2 * ylens.long() + 1

        ymax = ys.size(1)

        max_path_len = path.size(1)
        assert ys.size() == (bs, ymax), ys.size()
        assert path.size() == (bs, ymax * 2 + 1)

        alpha = log_probs.new_zeros(bs, max_path_len).fill_(LOG_0)
        alpha[:, 0] = LOG_1
        beta = alpha.clone()
        gamma = alpha.clone()

        batch_index = torch.arange(bs, dtype=torch.int64).unsqueeze(1)
        frame_index = torch.arange(xmax, dtype=torch.int64).unsqueeze(1).unsqueeze(2)
        log_probs_fwd_bwd = log_probs[frame_index, batch_index, path]
        same_transition = (path[:, :-2] == path[:, 2:])
        outside = torch.arange(max_path_len, dtype=torch.int64) >= path_lens.unsqueeze(1)
        log_probs_gold = log_probs[:, batch_index, path]

        # forward algorithm
        for t in range(xmax):
            alpha = _computes_transition(alpha, same_transition, outside,
                                         log_probs_fwd_bwd[t], log_probs_gold[t])

        # backward algorithm
        r_path = _flip_path(path, path_lens)
        log_probs_inv = _flip_label_probability(log_probs, elens.long())  # `[T, B, vocab]`
        log_probs_fwd_bwd = _flip_path_probability(log_probs_fwd_bwd, elens.long(), path_lens)  # `[T, B, 2*L+1]`
        r_same_transition = (r_path[:, :-2] == r_path[:, 2:])
        log_probs_inv_gold = log_probs_inv[:, batch_index, r_path]
        for t in range(xmax):
            beta = _computes_transition(beta, r_same_transition, outside,
                                        log_probs_fwd_bwd[t], log_probs_inv_gold[t])

        # pick up the best CTC path
        best_aligns = log_probs.new_zeros((bs, xmax), dtype=torch.int64)

        # forward algorithm
        log_probs_fwd_bwd = _flip_path_probability(log_probs_fwd_bwd, elens.long(), path_lens)
        for t in range(xmax):
            gamma = _computes_transition(gamma, same_transition, outside,
                                         log_probs_fwd_bwd[t], log_probs_gold[t],
                                         skip_accum=True)

            # select paths where gamma is valid
            log_probs_fwd_bwd[t] = log_probs_fwd_bwd[t].masked_fill_(gamma == LOG_0, LOG_0)

            # pick up the best alignment
            offsets = log_probs_fwd_bwd[t].argmax(1)
            for b in range(bs):
                if t <= elens[b] - 1:
                    token_idx = path[b, offsets[b]]
                    best_aligns[b, t] = token_idx

            # remove the rest of paths
            gamma = log_probs.new_zeros(bs, max_path_len).fill_(LOG_0)
            for b in range(bs):
                gamma[b, offsets[b]] = LOG_1

        # pick up trigger points
        trigger_aligns = torch.zeros((bs, xmax), dtype=torch.int64)
        trigger_aligns_avg = torch.zeros((bs, xmax), dtype=torch.int64)
        trigger_points = log_probs.new_zeros((bs, ymax + 1), dtype=torch.int32)  # +1 for <eos>
        for b in range(bs):
            n_triggers = 0
            if add_eos:
                trigger_points[b, ylens[b]] = elens[b] - 1
                # NOTE: use the last time index as a boundary corresponding to <eos>
                # Otherwise, index: 0 is used for <eos>
            last_token_idx = None
            count = 0
            for t in range(elens[b]):
                token_idx = best_aligns[b, t]
                if token_idx in self.symbols:
                    trigger_aligns_avg[b, t] = last_token_idx if last_token_idx else 0
                    count += 1
                if token_idx == self.blank:
                    continue
                if not (t == 0 or token_idx != best_aligns[b, t - 1]):
                    continue
                
                # NOTE: select the most left trigger points
                trigger_aligns[b, t] = token_idx
                last_token_idx = token_idx
                trigger_points[b, n_triggers] = t
                n_triggers += 1

        assert ylens.sum() == (trigger_aligns != 0).sum()
        
        return trigger_aligns_avg



def _flip_label_probability(log_probs, xlens):
    """Flips a label probability matrix.
    This function rotates a label probability matrix and flips it.
    ``log_probs[i, b, l]`` stores log probability of label ``l`` at ``i``-th
    input in ``b``-th batch.
    The rotated matrix ``r`` is defined as
    ``r[i, b, l] = log_probs[i + xlens[b], b, l]``
    Args:
        cum_log_prob (FloatTensor): `[T, B, vocab]`
        xlens (LongTensor): `[B]`
    Returns:
        FloatTensor: `[T, B, vocab]`
    """
    xmax, bs, vocab = log_probs.size()
    rotate = (torch.arange(xmax, dtype=torch.int64)[:, None] + xlens) % xmax
    return torch.flip(log_probs[rotate[:, :, None],
                                torch.arange(bs, dtype=torch.int64)[None, :, None],
                                torch.arange(vocab, dtype=torch.int64)[None, None, :]], dims=[0])


def _flip_path_probability(cum_log_prob, xlens, path_lens):
    """Flips a path probability matrix.
    This function returns a path probability matrix and flips it.
    ``cum_log_prob[i, b, t]`` stores log probability at ``i``-th input and
    at time ``t`` in a output sequence in ``b``-th batch.
    The rotated matrix ``r`` is defined as
    ``r[i, j, k] = cum_log_prob[i + xlens[j], j, k + path_lens[j]]``
    Args:
        cum_log_prob (FloatTensor): `[T, B, 2*L+1]`
        xlens (LongTensor): `[B]`
        path_lens (LongTensor): `[B]`
    Returns:
        FloatTensor: `[T, B, 2*L+1]`
    """
    xmax, bs, max_path_len = cum_log_prob.size()
    rotate_input = ((torch.arange(xmax, dtype=torch.int64)[:, None] + xlens) % xmax)
    rotate_label = ((torch.arange(max_path_len, dtype=torch.int64) + path_lens[:, None]) % max_path_len)
    return torch.flip(cum_log_prob[rotate_input[:, :, None],
                                   torch.arange(bs, dtype=torch.int64)[None, :, None],
                                   rotate_label], dims=[0, 2])


def _computes_transition(seq_log_prob, same_transition, outside,
                         cum_log_prob, log_prob_yt, skip_accum=False):
    bs, max_path_len = seq_log_prob.size()
    mat = seq_log_prob.new_zeros(3, bs, max_path_len).fill_(LOG_0)
    mat[0, :, :] = seq_log_prob
    mat[1, :, 1:] = seq_log_prob[:, :-1]
    mat[2, :, 2:] = seq_log_prob[:, :-2]
    # disable transition between the same symbols
    # (including blank-to-blank)
    mat[2, :, 2:][same_transition] = LOG_0
    seq_log_prob = torch.logsumexp(mat, dim=0)  # overwrite
    seq_log_prob[outside] = LOG_0
    if not skip_accum:
        cum_log_prob += seq_log_prob
    seq_log_prob += log_prob_yt
    return seq_log_prob

def make_pad_mask(seq_lens):
    """Make mask for padding.
    Args:
        seq_lens (IntTensor): `[B]`
    Returns:
        mask (IntTensor): `[B, T]`
    """
    bs = seq_lens.size(0)
    max_time = seq_lens.max()
    seq_range = torch.arange(0, max_time, dtype=torch.int32, device=seq_lens.device)
    seq_range = seq_range.unsqueeze(0).expand(bs, max_time)
    mask = seq_range < seq_lens.unsqueeze(-1)
    return mask

def _label_to_path(labels, blank):
    path = labels.new_zeros(labels.size(0), labels.size(1) * 2 + 1).fill_(blank).long()
    path[:, 1::2] = labels
    return path
def _flip_path(path, path_lens):
    """Flips label sequence.
    This function rotates a label sequence and flips it.
    ``path[b, t]`` stores a label at time ``t`` in ``b``-th batch.
    The rotated matrix ``r`` is defined as
    ``r[b, t] = path[b, t + path_lens[b]]``
    .. ::
       a b c d .     . a b c d    d c b a .
       e f . . .  -> . . . e f -> f e . . .
       g h i j k     g h i j k    k j i h g
    Args:
        path (FloatTensor): `[B, 2*L+1]`
        path_lens (LongTensor): `[B]`
    Returns:
        FloatTensor: `[B, 2*L+1]`
    """
    bs = path.size(0)
    max_path_len = path.size(1)
    rotate = (torch.arange(max_path_len) + path_lens[:, None]) % max_path_len
    return torch.flip(path[torch.arange(bs, dtype=torch.int64)[:, None], rotate], dims=[1])

================================================
FILE: code/ASR/CMatch/data_load.py
================================================
from espnet.utils.training.batchfy import make_batchset
from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence
import torch
import os
import json
import kaldiio
import random
import logging
import sentencepiece as spm


data_config = {
    "librispeech": {
        "train": "dump/train_960/deltafalse/data_unigram5000.json",
        "val": "dump/dev_clean/deltafalse/data_unigram5000.json",
        "test": "dump/test_clean/deltafalse/data_unigram5000.json",
        "token": "data/lang_char/train_960_unigram5000_units.txt",
        "prefix": "/espnet/egs/librispeech/asr1/",
    },
    "wsj": {
        "train": "dump/train_si284/deltafalse/data.json",
        "val": "dump/test_dev93/deltafalse/data.json",
        "test": "dump/test_eval92/deltafalse/data.json",
        "token": "data/lang_1char/train_si284_units.txt",
        "prefix": "/opt/espnet/egs/wsj/asr1/",
    },
    "an4": {
        "train": "dump/train_nodev/deltafalse/data.json",
        "val": "dump/train_dev/deltafalse/data.json",
        "test": "dump/test/deltafalse/data.json",
        "token": "data/lang_1char/train_nodev_units.txt",
        "prefix": "/home/jindwang/mine/espnet/egs/an4/asr1/",
    },
    "libriadapt_en_us_clean_matrix": {
        "train": "dump/en_us_clean_matrix/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_clean_matrix/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_clean_matrix/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_clean_matrix/train_unigram31.model",
    },
    "libriadapt_en_us_clean_usb": {
        "train": "dump/en_us_clean_usb/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_clean_usb/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_clean_usb/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_clean_usb/train_unigram31.model",
    },
    "libriadapt_en_us_clean_pseye": {
        "train": "dump/en_us_clean_pseye/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_clean_pseye/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_clean_pseye/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_clean_pseye/train_unigram31.model",
    },
    "libriadapt_en_us_clean_respeaker": {
        "train": "dump/en_us_clean_respeaker/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_clean_respeaker/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_clean_respeaker/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_clean_respeaker/train_unigram31.model",
    },
    "libriadapt_en_us_rain_respeaker": {
        "train": "dump/en_us_rain_respeaker/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_rain_respeaker/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_rain_respeaker/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_rain_respeaker/train_unigram31.model",
    },
    "libriadapt_en_us_wind_respeaker": {
        "train": "dump/en_us_wind_respeaker/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_wind_respeaker/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_wind_respeaker/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_wind_respeaker/train_unigram31.model",
    },
    "libriadapt_en_us_laughter_respeaker": {
        "train": "dump/en_us_laughter_respeaker/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_laughter_respeaker/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_laughter_respeaker/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_laughter_respeaker/train_unigram31.model",
    },
    "libriadapt_en_us_clean_shure": {
        "train": "dump/en_us_clean_shure/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_us_clean_shure/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_us_clean_shure/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_us_clean_shure/train_unigram31.model",
    },
    "libriadapt_en_gb_clean_shure": {
        "train": "dump/en_gb_clean_shure/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_gb_clean_shure/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_gb_clean_shure/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_gb_clean_shure/train_unigram31.model",
    },
    "libriadapt_en_in_clean_shure": {
        "train": "dump/en_in_clean_shure/train/deltafalse/data_unigram31.json",
        "val": None,
        "test": "dump/en_in_clean_shure/test/deltafalse/data_unigram31.json",
        "token": "data/lang_char/en_in_clean_shure/train_unigram31_units.txt",
        "prefix": "/D_data/libriadapt_processed/asr1/",
        "bpemodel": "data/lang_char/en_in_clean_shure/train_unigram31.model",
    },
}


def read_json_file(fname):
    with open(fname, "rb") as f:
        contents = json.load(f)["utts"]
    return contents


def load_json(train_json_file, dev_json_file, test_json_file):
    train_json = read_json_file(train_json_file)
    if os.path.isfile(dev_json_file) and not "tmp_dev_set" in dev_json_file:
        dev_json = read_json_file(dev_json_file)
    else:
        n_samples = len(train_json)
        train_size = int(0.9 * n_samples)
        logging.warning(
            f"No dev set provided, will split the last {n_samples - train_size} (10%) samples from training data"
        )
        train_json_item = list(train_json.items())
        # random.shuffle(train_json_item)
        train_json = dict(train_json_item[:train_size])
        dev_json = dict(train_json_item[train_size:])
        # Save temp dev set
        with open(dev_json_file, "w") as f:
            json.dump({"utts": dev_json}, f)
        logging.warning(f"Temporary dev set saved: {dev_json_file}")
    test_json = read_json_file(test_json_file)
    logging.warning(f"#Train Json {train_json_file}: {len(train_json)}")
    logging.warning(f"#Dev Json {dev_json_file}: {len(dev_json)}")
    logging.warning(f"#Test Json {test_json_file}: {len(test_json)}")
    return train_json, dev_json, test_json


def load_data(root_path, dataset, args, 
            pseudo_label_json=None, pseudo_label_filtering=True, use_pseudo_label=True):
    def collate(minibatch):
        fbanks = []
        tokens = []
        for _, info in minibatch[0]:
            fbanks.append(
                torch.tensor(
                    kaldiio.load_mat(
                        info["input"][0]["feat"].replace(
                            data_config[dataset]["prefix"], root_path
                        )
                    )
                )
            )
            if use_pseudo_label and "pseudo_tokenid" in info["output"][0].keys():
                tokens.append(
                    torch.tensor([int(s) for s in info["output"][0]["pseudo_tokenid"].split()])
                )
            else:
                tokens.append(
                    torch.tensor([int(s) for s in info["output"][0]["tokenid"].split()])
                )
        ilens = torch.tensor([x.shape[0] for x in fbanks])
        return (
            pad_sequence(fbanks, batch_first=True, padding_value=0),
            ilens,
            pad_sequence(tokens, batch_first=True, padding_value=-1),
        )

    train_json = os.path.join(root_path, data_config[dataset]["train"])
    dev_json = (
        os.path.join(root_path, data_config[dataset]["val"])
        if data_config[dataset]["val"]
        else f"{root_path}/tmp_dev_set_{dataset}.json"
    )
    test_json = os.path.join(root_path, data_config[dataset]["test"])
    train_json, dev_json, test_json = load_json(train_json, dev_json, test_json)
    _, info = next(iter(train_json.items()))

    if use_pseudo_label and pseudo_label_json:
        psuedo_label_json = read_json_file(pseudo_label_json)
        assert psuedo_label_json.keys() == train_json.keys() or list(psuedo_label_json.keys())[:25685] == list(train_json.keys()), \
                    "Keys of pseudo label and training data not matched"
        for key in train_json.keys():
            train_json[key]['output'][0]['pseudo_tokenid'] = ' '.join(psuedo_label_json[key]['output'][0]['rec_tokenid'].split()[:-1])
            train_json[key]['output'][0]["score"] = psuedo_label_json[key]['output'][0]['score']
    if use_pseudo_label and pseudo_label_json and pseudo_label_filtering:
        filtered_sample = 0
        filtered_ratio = 0.3 if pseudo_label_filtering else 0.0
        train_json = sorted(train_json.items(), key=lambda x:x[1]['output'][0]['score'], reverse=True)
        sample_num = len(train_json)
        train_json = train_json[:int(sample_num * (1 - filtered_ratio))]
        logging.warning(f"Filtering: {len(train_json)}/{sample_num} pseudo-labelled samples are kept")
        train_json = dict(train_json)
        filtered_sample = 0

    idim = info["input"][0]["shape"][1]
    odim = info["output"][0]["shape"][1]

    use_sortagrad = False  # args.sortagrad == -1 or args.sortagrad > 0
    trainset = make_batchset(
        train_json,
        args.batch_size,
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=args.ngpu if (args.ngpu > 1 and not args.dist_train) else 1,
        shortest_first=use_sortagrad,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    devset = make_batchset(
        dev_json,
        args.batch_size if args.ngpu <= 1 else int(args.batch_size / args.ngpu),
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=1,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    testset = make_batchset(
        test_json,
        args.batch_size if args.ngpu <= 1 else int(args.batch_size / args.ngpu),
        args.maxlen_in,
        args.maxlen_out,
        args.minibatches,
        min_batch_size=1,
        count=args.batch_count,
        batch_bins=args.batch_bins,
        batch_frames_in=args.batch_frames_in,
        batch_frames_out=args.batch_frames_out,
        batch_frames_inout=args.batch_frames_inout,
        iaxis=0,
        oaxis=0,
    )
    if args.dist_train and args.ngpu > 1:
        train_sampler = torch.utils.data.distributed.DistributedSampler(trainset)
    else:
        train_sampler = None
    train_loader = DataLoader(
        trainset,
        batch_size=1,
        collate_fn=collate,
        num_workers=args.n_iter_processes,
        shuffle=(train_sampler is None and not args.pseudo_labeling),
        pin_memory=True,
        sampler=train_sampler,
    )
    dev_loader = DataLoader(
        devset,
        batch_size=1,
        collate_fn=collate,
        shuffle=False,
        num_workers=args.n_iter_processes,
        pin_memory=True,
    )
    test_loader = DataLoader(
        testset,
        batch_size=1,
        collate_fn=collate,
        shuffle=False,
        num_workers=args.n_iter_processes,
        pin_memory=True,
    )
    return (train_loader, dev_loader, test_loader), (idim, odim)

def load_token_list(token_file):
    with open(token_file, "r") as f:
        token_list = [entry.split()[0] for entry in f]
    token_list.insert(0, "<blank>")
    token_list.append("<eos>")
    return token_list


def load_bpemodel(root_path, dataset):
    bpemodel_path = os.path.join(root_path, data_config[dataset]["bpemodel"])
    bpemodel = spm.SentencePieceProcessor()
    bpemodel.Load(bpemodel_path)
    return bpemodel


================================================
FILE: code/ASR/CMatch/distances.py
================================================
import torch

def CORAL(source, target):
    DEVICE = source.device
    d = source.size(1)
    ns, nt = source.size(0), target.size(0)

    # source covariance
    tmp_s = torch.ones((1, ns)).to(DEVICE) @ source
    cs = (source.t() @ source - (tmp_s.t() @ tmp_s) / ns) / (ns - 1)

    # target covariance
    tmp_t = torch.ones((1, nt)).to(DEVICE) @ target
    ct = (target.t() @ target - (tmp_t.t() @ tmp_t) / nt) / (nt - 1)

    # frobenius norm
    loss = (cs - ct).pow(2).sum().sqrt()
    loss = loss / (4 * d * d)
    return loss


class MMD_loss(torch.nn.Module):
    def __init__(self, kernel_type='rbf', kernel_mul=2.0, kernel_num=5):
        super(MMD_loss, self).__init__()
        self.kernel_num = kernel_num
        self.kernel_mul = kernel_mul
        self.fix_sigma = None
        self.kernel_type = kernel_type

    def guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5, fix_sigma=None):
        n_samples = int(source.size()[0]) + int(target.size()[0])
        total = torch.cat([source, target], dim=0)
        total0 = total.unsqueeze(0).expand(
            int(total.size(0)), int(total.size(0)), int(total.size(1)))
        total1 = total.unsqueeze(1).expand(
            int(total.size(0)), int(total.size(0)), int(total.size(1)))
        L2_distance = ((total0-total1)**2).sum(2)
        if fix_sigma:
            bandwidth = fix_sigma
        else:
            bandwidth = torch.sum(L2_distance.data) / (n_samples**2-n_samples)
        bandwidth /= kernel_mul ** (kernel_num // 2)
        bandwidth_list = [bandwidth * (kernel_mul**i)
                          for i in range(kernel_num)]
        kernel_val = [torch.exp(-L2_distance / bandwidth_temp)
                      for bandwidth_temp in bandwidth_list]
        return sum(kernel_val)

    def linear_mmd2(self, f_of_X, f_of_Y):
        loss = 0.0
        delta = f_of_X.float().mean(0) - f_of_Y.float().mean(0)
        loss = delta.dot(delta.T)
        return loss

    def forward(self, source, target):
        if self.kernel_type == 'linear':
            return self.linear_mmd2(source, target)
        elif self.kernel_type == 'rbf':
            batch_size = int(source.size()[0])
            kernels = self.guassian_kernel(
                source, target, kernel_mul=self.kernel_mul, kernel_num=self.kernel_num, fix_sigma=self.fix_sigma)
            XX = torch.mean(kernels[:batch_size, :batch_size])
            YY = torch.mean(kernels[batch_size:, batch_size:])
            XY = torch.mean(kernels[:batch_size, batch_size:])
            YX = torch.mean(kernels[batch_size:, :batch_size])
            loss = torch.mean(XX + YY - XY - YX)
            return loss

================================================
FILE: code/ASR/CMatch/e2e_asr_udatransformer.py
================================================
import collections
from espnet.nets.pytorch_backend.e2e_asr_transformer import *
from espnet.nets.pytorch_backend.e2e_asr_transformer import E2E as SpeechTransformer
from espnet.nets.pytorch_backend.transformer.encoder import *
from espnet.nets.pytorch_backend.transformer.decoder import *
from espnet.nets.pytorch_backend.transformer.encoder_layer import EncoderLayer
from espnet.nets.pytorch_backend.transformer.decoder_layer import DecoderLayer
import torch
from distances import CORAL, MMD_loss
import numpy as np
from ctc_aligner import CTCForcedAligner

def adapt_loss(source, target, adapt_loss="mmd"):
    if adapt_loss == "mmd": # 1.0 level
        mmd_loss = MMD_loss()
        loss = mmd_loss(source, target)
    elif adapt_loss == "mmd_linear":
        mmd_loss = MMD_loss(kernel_type="linear")
        loss = mmd_loss(source, target)
    elif adapt_loss == "coral": # 1e-4 level
        loss = CORAL(source, target)
    else:
        raise NotImplementedError(f"Adapt loss type {adapt_loss} is not implemented")
    return loss

class Discriminator(torch.nn.Module):
    def __init__(self, input_dim=256, hidden_dim=256):
        super(Discriminator, self).__init__()
        self.input_dim = input_dim
        self.hidden_dim = hidden_dim
        self.dis1 = torch.nn.Linear(input_dim, hidden_dim)
        self.bn = torch.nn.BatchNorm1d(hidden_dim)
        self.dis2 = torch.nn.Linear(hidden_dim, 1)
    def forward(self, x):
        x = torch.nn.functional.relu(self.dis1(x))
        x = self.dis2(self.bn(x.permute(0, 2, 1)).permute(0, 2, 1))
        x = torch.sigmoid(x)
        return x

class ReverseLayerF(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, alpha):
        ctx.alpha = alpha
        return x.view_as(x)
    @staticmethod
    def backward(ctx, grad_output):
        output = grad_output.neg() * ctx.alpha
        return output, None

class CustomEncoderLayer(EncoderLayer):
    def forward(self, x, mask, cache=None):
        residual = x
        if self.normalize_before:
            x = self.norm1(x)
            self.x_norm = x
        if cache is None:
            x_q = x
        else:
            assert cache.shape == (x.shape[0], x.shape[1] - 1, self.size)
            x_q = x[:, -1:, :]
            residual = residual[:, -1:, :]
            mask = None if mask is None else mask[:, -1:, :]

        if self.concat_after:
            x_concat = torch.cat((x, self.self_attn(x_q, x, x, mask)), dim=-1)
            x = residual + self.concat_linear(x_concat)
        else:
            x = residual + self.dropout(self.self_attn(x_q, x, x, mask))
        if not self.normalize_before:
            x = self.norm1(x)

        residual = x
        if self.normalize_before:
            x = self.norm2(x)
        x = residual + self.dropout(self.feed_forward(x))
        if not self.normalize_before:
            x = self.norm2(x)

        if cache is not None:
            x = torch.cat([cache, x], dim=1)
        return x, mask

class CustomEncoder(Encoder):
    def __init__(
        self,
        idim,
        selfattention_layer_type="selfattn",
        attention_dim=256,
        attention_heads=4,
        conv_wshare=4,
        conv_kernel_length=11,
        conv_usebias=False,
        linear_units=2048,
        num_blocks=6,
        dropout_rate=0.1,
        positional_dropout_rate=0.1,
        attention_dropout_rate=0.0,
        input_layer="conv2d",
        pos_enc_class=PositionalEncoding,
        normalize_before=True,
        concat_after=False,
        positionwise_layer_type="linear",
        positionwise_conv_kernel_size=1,
        padding_idx=-1,
    ):
        super().__init__(idim,
                selfattention_layer_type,
                attention_dim,
                attention_heads,
                conv_wshare,
                conv_kernel_length,
                conv_usebias,
                linear_units,
                num_blocks,
                dropout_rate,
                positional_dropout_rate,
                attention_dropout_rate,
                input_layer,
                pos_enc_class,
                normalize_before,
                concat_after,
                positionwise_layer_type,
                positionwise_conv_kernel_size,
                padding_idx)

        positionwise_layer, positionwise_layer_args = self.get_positionwise_layer(
            positionwise_layer_type,
            attention_dim,
            linear_units,
            dropout_rate,
            positionwise_conv_kernel_size,
        )
        encoder_selfattn_layer = MultiHeadedAttention
        encoder_selfattn_layer_args = [
            (
                attention_heads,
                attention_dim,
                attention_dropout_rate,
            )
        ] * num_blocks
        self.encoders = repeat(
            num_blocks,
            lambda lnum: CustomEncoderLayer(
                attention_dim,
                encoder_selfattn_layer(*encoder_selfattn_layer_args[lnum]),
                positionwise_layer(*positionwise_layer_args),
                dropout_rate,
                normalize_before,
                concat_after,
            ),
        )
    def forward(self, xs, masks, return_repr=False):
        """Encode input sequence.
        Args:
            xs (torch.Tensor): Input tensor (#batch, time, idim).
            masks (torch.Tensor): Mask tensor (#batch, time).
        Returns:
            torch.Tensor: Output tensor (#batch, time, attention_dim).
            torch.Tensor: Mask tensor (#batch, time).
        """
        xs, masks = self.embed(xs, masks)
        #xs, masks = self.encoders(xs, masks)
        final_repr = []
        for layer_idx, e in enumerate(self.encoders):
            xs, masks = e(xs, masks)
            if return_repr and layer_idx > 0:
                assert e.x_norm is not None
                final_repr.append(e.x_norm)
                #print(e.x_norm.mean(), xs.mean())
                e.x_norm = None
        if self.normalize_before:
            xs = self.after_norm(xs)
            final_repr.append(xs)
        return (xs, masks) if not return_repr else (xs, masks, final_repr)

class CustomDecoderLayer(DecoderLayer):
    def forward(self, tgt, tgt_mask, memory, memory_mask, cache=None):
        """Compute decoded features.
        Args:
            tgt (torch.Tensor): Input tensor (#batch, maxlen_out, size).
            tgt_mask (torch.Tensor): Mask for input tensor (#batch, maxlen_out).
            memory (torch.Tensor): Encoded memory, float32 (#batch, maxlen_in, size).
            memory_mask (torch.Tensor): Encoded memory mask (#batch, maxlen_in).
            cache (List[torch.Tensor]): List of cached tensors.
                Each tensor shape should be (#batch, maxlen_out - 1, size).
        Returns:
            torch.Tensor: Output tensor(#batch, maxlen_out, size).
            torch.Tensor: Mask for output tensor (#batch, maxlen_out).
            torch.Tensor: Encoded memory (#batch, maxlen_in, size).
            torch.Tensor: Encoded memory mask (#batch, maxlen_in).
        """
        residual = tgt
        if self.normalize_before:
            tgt = self.norm1(tgt)
            self.x_norm = tgt
        if cache is None:
            tgt_q = tgt
            tgt_q_mask = tgt_mask
        else:
            # compute only the last frame query keeping dim: max_time_out -> 1
            assert cache.shape == (
                tgt.shape[0],
                tgt.shape[1] - 1,
                self.size,
            ), f"{cache.shape} == {(tgt.shape[0], tgt.shape[1] - 1, self.size)}"
            tgt_q = tgt[:, -1:, :]
            residual = residual[:, -1:, :]
            tgt_q_mask = None
            if tgt_mask is not None:
                tgt_q_mask = tgt_mask[:, -1:, :]

        if self.concat_after:
            tgt_concat = torch.cat(
                (tgt_q, self.self_attn(tgt_q, tgt, tgt, tgt_q_mask)), dim=-1
            )
            x = residual + self.concat_linear1(tgt_concat)
        else:
            x = residual + self.dropout(self.self_attn(tgt_q, tgt, tgt, tgt_q_mask))
        if not self.normalize_before:
            x = self.norm1(x)

        residual = x
        if self.normalize_before:
            x = self.norm2(x)
        if self.concat_after:
            x_concat = torch.cat(
                (x, self.src_attn(x, memory, memory, memory_mask)), dim=-1
            )
            x = residual + self.concat_linear2(x_concat)
        else:
            x = residual + self.dropout(self.src_attn(x, memory, memory, memory_mask))
        if not self.normalize_before:
            x = self.norm2(x)

        residual = x
        if self.normalize_before:
            x = self.norm3(x)
        x = residual + self.dropout(self.feed_forward(x))
        if not self.normalize_before:
            x = self.norm3(x)

        if cache is not None:
            x = torch.cat([cache, x], dim=1)

        return x, tgt_mask, memory, memory_mask
class CustomDecoder(Decoder):
    def __init__(
        self,
        odim,
        selfattention_layer_type="selfattn",
        attention_dim=256,
        attention_heads=4,
        conv_wshare=4,
        conv_kernel_length=11,
        conv_usebias=False,
        linear_units=2048,
        num_blocks=6,
        dropout_rate=0.1,
        positional_dropout_rate=0.1,
        self_attention_dropout_rate=0.0,
        src_attention_dropout_rate=0.0,
        input_layer="embed",
        use_output_layer=True,
        pos_enc_class=PositionalEncoding,
        normalize_before=True,
        concat_after=False,
    ):
        super().__init__(odim,
            selfattention_layer_type,
            attention_dim,
            attention_heads,
            conv_wshare,
            conv_kernel_length,
            conv_usebias,
            linear_units,
            num_blocks,
            dropout_rate,
            positional_dropout_rate,
            self_attention_dropout_rate,
            src_attention_dropout_rate,
            input_layer,
            use_output_layer,
            pos_enc_class,
            normalize_before,
            concat_after,
        )
        decoder_selfattn_layer = MultiHeadedAttention
        decoder_selfattn_layer_args = [
            (
                attention_heads,
                attention_dim,
                self_attention_dropout_rate,
            )
        ] * num_blocks
        self.decoders = repeat(
                num_blocks,
                lambda lnum: CustomDecoderLayer(
                    attention_dim,
                    decoder_selfattn_layer(*decoder_selfattn_layer_args[lnum]),
                    MultiHeadedAttention(
                        attention_heads, attention_dim, src_attention_dropout_rate
                    ),
                    PositionwiseFeedForward(attention_dim, linear_units, dropout_rate),
                    dropout_rate,
                    normalize_before,
                    concat_after,
                ),
            )

    def forward(self, tgt, tgt_mask, memory, memory_mask, return_repr=False):
        """Forward decoder.
        Args:
            tgt (torch.Tensor): Input token ids, int64 (#batch, maxlen_out) if
                input_layer == "embed". In the other case, input tensor
                (#batch, maxlen_out, odim).
            tgt_mask (torch.Tensor): Input token mask (#batch, maxlen_out).
                dtype=torch.uint8 in PyTorch 1.2- and dtype=torch.bool in PyTorch 1.2+
                (include 1.2).
            memory (torch.Tensor): Encoded memory, float32 (#batch, maxlen_in, feat).
            memory_mask (torch.Tensor): Encoded memory mask (#batch, maxlen_in).
                dtype=torch.uint8 in PyTorch 1.2- and dtype=torch.bool in PyTorch 1.2+
                (include 1.2).
        Returns:
            torch.Tensor: Decoded token score before softmax (#batch, maxlen_out, odim)
                   if use_output_layer is True. In the other case,final block outputs
                   (#batch, maxlen_out, attention_dim).
            torch.Tensor: Score mask before softmax (#batch, maxlen_out).
        """
        x = self.embed(tgt)
        # x, tgt_mask, memory, memory_mask = self.decoders(
        #     x, tgt_mask, memory, memory_mask
        # )
        final_repr = []
        for layer_idx, decoder in enumerate(self.decoders):
            x, tgt_mask, memory, memory_mask = decoder(
                x, tgt_mask, memory, memory_mask
            )
            if return_repr and layer_idx > 0:
                assert decoder.x_norm is not None
                final_repr.append(decoder.x_norm)
                decoder.x_norm = None
        if self.normalize_before:
            x = self.after_norm(x)
            final_repr.append(x)
        if self.output_layer is not None:
            x = self.output_layer(x)
        return (x, tgt_mask, None) if not return_repr else (x, tgt_mask, final_repr)
        
class CustomSpeechTransformer(SpeechTransformer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        idim, odim = args
        args = kwargs["args"]
        self.encoder = CustomEncoder(
            idim=idim,
            selfattention_layer_type=args.transformer_encoder_selfattn_layer_type,
            attention_dim=args.adim,
            attention_heads=args.aheads,
            conv_wshare=args.wshare,
            conv_kernel_length=args.ldconv_encoder_kernel_length,
            conv_usebias=args.ldconv_usebias,
            linear_units=args.eunits,
            num_blocks=args.elayers,
            input_layer=args.transformer_input_layer,
            dropout_rate=args.dropout_rate,
            positional_dropout_rate=args.dropout_rate,
            attention_dropout_rate=args.transformer_attn_dropout_rate,
        )
        self.decoder = CustomDecoder(
            odim=odim,
            selfattention_layer_type=args.transformer_decoder_selfattn_layer_type,
            attention_dim=args.adim,
            attention_heads=args.aheads,
            conv_wshare=args.wshare,
            conv_kernel_length=args.ldconv_decoder_kernel_length,
            conv_usebias=args.ldconv_usebias,
            linear_units=args.dunits,
            num_blocks=args.dlayers,
            dropout_rate=args.dropout_rate,
            positional_dropout_rate=args.dropout_rate,
            self_attention_dropout_rate=args.transformer_attn_dropout_rate,
            src_attention_dropout_rate=args.transformer_attn_dropout_rate,
        )
class UDASpeechTransformer(CustomSpeechTransformer):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        args = kwargs["args"]
        assert hasattr(args, "transfer_type")
        assert isinstance(args.self_training, bool)
        self.loss_type = args.transfer_type
        self.self_training = args.self_training
        self.n_classes = len(args.char_list)

        self.multi_enc_repr_num = args.multi_enc_repr_num
        self.multi_dec_repr_num = args.multi_dec_repr_num
        self.use_dec_repr = args.use_dec_repr
        self.pseudo_ctc_confidence_thr = args.pseudo_ctc_confidence_thr # Threshold for filtering CTC outputs
        self.cmatch_method = args.cmatch_method
        self.ctc_aligner = CTCForcedAligner(char_list=None)
        self.char_list = args.char_list
        self.bpemodel = None
        self.non_char_symbols = list(map(int, args.non_char_symbols.split("_")))
        if self.loss_type:
            if "cmatch" in self.loss_type:
                assert args.cmatch_method is not None, "CMatch method is required."
                assert self.non_char_symbols is not None, "Non-character symbol list must be specified"
            elif self.loss_type == "adv":
                self.domain_classifier = Discriminator(input_dim=self.adim, hidden_dim=self.adim)
    
    def forward(self, 
                xs_pad, 
                ilens, 
                ys_pad, 
                tgt_xs_pad=None, 
                tgt_ilens=None, 
                tgt_ys_pad=None):
        """E2E forward.
        :param torch.Tensor xs_pad: batch of padded source sequences (B, Tmax, idim)
        :param torch.Tensor ilens: batch of lengths of source sequences (B)
        :param torch.Tensor ys_pad: batch of padded target sequences (B, Lmax)
        :return: ctc loss value
        :rtype: torch.Tensor
        :return: attention loss value
        :rtype: torch.Tensor
        :return: accuracy in attention decoder
        :rtype: float
        """
        dec_return_repr = True
        enc_return_repr = True

        # 1. forward encoder
        xs_pad = xs_pad[:, : max(ilens)]  # for data parallel
        src_mask = make_non_pad_mask(ilens.tolist()).to(xs_pad.device).unsqueeze(-2)
        if enc_return_repr:
            hs_pad, hs_mask, src_enc_repr = self.encoder(xs_pad, src_mask, return_repr=enc_return_repr)
            src_enc_repr = src_enc_repr[-self.multi_enc_repr_num:]
        else:
            hs_pad, hs_mask = self.encoder(xs_pad, src_mask)
        self.hs_pad = hs_pad

        # 2. forward decoder
        if self.decoder is not None:
            ys_in_pad, ys_out_pad = add_sos_eos(
                ys_pad, self.sos, self.eos, self.ignore_id
            )
            ys_mask = target_mask(ys_in_pad, self.ignore_id)
            pred_pad, pred_mask, src_dec_repr = self.decoder(ys_in_pad, ys_mask, hs_pad, hs_mask, return_repr=dec_return_repr)

            if src_dec_repr:
                src_dec_repr = src_dec_repr[-self.multi_dec_repr_num:]
            self.pred_pad = pred_pad

            # 3. compute attention loss
            loss_att = self.criterion(pred_pad, ys_out_pad)
            self.acc = th_accuracy(
                pred_pad.view(-1, self.odim), ys_out_pad, ignore_label=self.ignore_id
            )
        else:
            loss_att = None
            self.acc = None

        cer_ctc = None
        if self.mtlalpha == 0.0:
            loss_ctc = None
        else:
            batch_size = xs_pad.size(0)
            hs_len = hs_mask.view(batch_size, -1).sum(1)
            loss_ctc = self.ctc(hs_pad.view(batch_size, -1, self.adim), hs_len, ys_pad)
            if not self.training and self.error_calculator is not None:
                ys_hat = self.ctc.argmax(hs_pad.view(batch_size, -1, self.adim)).data
                cer_ctc = self.error_calculator(ys_hat.cpu(), ys_pad.cpu(), is_ctc=True)
            # for visualization
            if not self.training:
                self.ctc.softmax(hs_pad)

        # 5. compute cer/wer
        if self.training or self.error_calculator is None or self.decoder is None:
            cer, wer = None, None
        else:
            ys_hat = pred_pad.argmax(dim=-1)
            cer, wer = self.error_calculator(ys_hat.cpu(), ys_pad.cpu())
        
        src_hlens = torch.tensor([int(sum(mask[0])) for mask in hs_mask])
        if self.cmatch_method == "pseudo_ctc_pred":
            src_hs_flatten = torch.cat([hs_pad[i, :src_hlens[i], :].view(-1, self.adim) for i in range(len(hs_pad))]) # hs_pad: B * T, F
            src_ctc_softmax = torch.nn.functional.softmax(self.ctc.ctc_lo(src_hs_flatten), dim=1)
        else:
            src_ctc_softmax = None

        # Domain adversarial loss
        if tgt_xs_pad is not None and tgt_ilens is not None:
            src_ys_pad = ys_pad
            src_hs_pad, src_hs_mask = hs_pad, hs_mask
            tgt_xs_pad = tgt_xs_pad[:, : max(tgt_ilens)]  # for data parallel
            tgt_src_mask = make_non_pad_mask(tgt_ilens.tolist()).to(tgt_xs_pad.device).unsqueeze(-2)
            
            if enc_return_repr:
                tgt_hs_pad, tgt_hs_mask, tgt_enc_repr = self.encoder(tgt_xs_pad, tgt_src_mask, return_repr=enc_return_repr)
                tgt_enc_repr = tgt_enc_repr[-self.multi_enc_repr_num:]
            else:
                tgt_hs_pad, tgt_hs_mask = self.encoder(tgt_xs_pad, tgt_src_mask)

            src_ys_out_pad = ys_out_pad
            src_ys_out_flatten = src_ys_out_pad.contiguous().view(-1)

            if tgt_ys_pad is not None:
                tgt_ys_in_pad, tgt_ys_out_pad = add_sos_eos(
                    tgt_ys_pad, self.sos, self.eos, self.ignore_id
                )
                tgt_ys_mask = target_mask(tgt_ys_in_pad, self.ignore_id)
                tgt_pred_pad, tgt_pred_mask, tgt_dec_repr = self.decoder(tgt_ys_in_pad, tgt_ys_mask, tgt_hs_pad, tgt_hs_mask, return_repr=dec_return_repr)
                if tgt_dec_repr:
                    tgt_dec_repr = tgt_dec_repr[-self.multi_dec_repr_num:]
                tgt_ys_out_flatten = tgt_ys_out_pad.contiguous().view(-1)
            
            tgt_hlens = torch.tensor([int(sum(mask[0])) for mask in tgt_hs_mask])
            if self.cmatch_method == "pseudo_ctc_pred":
                tgt_hs_flatten = torch.cat([tgt_hs_pad[i, :tgt_hlens[i], :].view(-1, self.adim) for i in range(len(tgt_hs_pad))]) # hs_pad: B * T, F
                tgt_ctc_softmax = torch.nn.functional.softmax(self.ctc.ctc_lo(tgt_hs_flatten), dim=1)
            else:
                tgt_ctc_softmax = None

            if self.self_training:
                src_loss_att = loss_att
                src_loss_ctc = loss_ctc
                tgt_batch_size = tgt_xs_pad.size(0)
                tgt_hs_len = tgt_hs_mask.view(tgt_batch_size, -1).sum(1)
                tgt_loss_ctc = self.ctc(tgt_hs_pad.view(tgt_batch_size, -1, self.adim), tgt_hs_len, tgt_ys_pad)
                tgt_loss_att = self.criterion(tgt_pred_pad, tgt_ys_out_pad)

                loss_att = (src_loss_att + tgt_loss_att) / 2
                loss_ctc = (src_loss_ctc + tgt_loss_ctc) / 2
                self.acc = (self.acc + th_accuracy(
                    tgt_pred_pad.view(-1, self.odim), tgt_ys_out_pad, ignore_label=self.ignore_id
                )) / 2
            
            uda_loss = torch.tensor(0.0).cuda()
            if not self.loss_type:
                uda_loss = torch.tensor(0.0).cuda()
            elif self.loss_type == "adv":
                uda_loss = self.adversarial_loss(src_hs_pad, tgt_hs_pad)
            elif self.loss_type == "cmatch":
                assert tgt_ys_pad is not None
                assert len(src_enc_repr) == self.multi_enc_repr_num and len(src_enc_repr) in [1, 3, 6, 9, 12], len(src_enc_repr)
                for layer_idx in range(len(src_enc_repr)):
                    src_hs_flatten, src_ys_flatten, tgt_hs_flatten, tgt_ys_flatten \
                                = self.get_enc_repr(src_enc_repr[layer_idx], 
                                                    src_hlens, 
                                                    tgt_enc_repr[layer_idx], 
                                                    tgt_hlens,
                                                    src_ys_pad,
                                                    tgt_ys_pad,
                                                    method=self.cmatch_method,
                                                    src_ctc_softmax=src_ctc_softmax,
                                                    tgt_ctc_softmax=tgt_ctc_softmax,)
                    layer_uda_loss = self.cmatch_loss_func(self.n_classes,
                                            src_hs_flatten, 
                                            src_ys_flatten, 
                                            tgt_hs_flatten, 
                                            tgt_ys_flatten)
                    uda_loss = uda_loss + layer_uda_loss if uda_loss else layer_uda_loss
                if self.use_dec_repr:
                    assert len(src_dec_repr) == self.multi_dec_repr_num, len(src_dec_repr)
                    # No need to calculate decoder matching loss
                    for layer_idx in range(len(src_dec_repr)):
                        src_repr_flatten = src_dec_repr[layer_idx].contiguous().view(-1, self.adim)
                        tgt_repr_flatten = tgt_dec_repr[layer_idx].contiguous().view(-1, self.adim)
                        layer_uda_loss = self.cmatch_loss_func(self.n_classes,
                                                        src_repr_flatten, 
                                                        src_ys_out_flatten, 
                                                        tgt_repr_flatten, 
                                                        tgt_ys_out_flatten)
                        uda_loss = uda_loss + layer_uda_loss
            elif self.loss_type in ["coral", "mmd"]:
                # (B, T, F) --> (B, F)
                uda_loss = adapt_loss(torch.mean(src_hs_pad, dim=1), 
                                    torch.mean(tgt_hs_pad, dim=1), 
                                    adapt_loss=self.loss_type)
            else:
                raise NotImplementedError(f"loss type {self.loss_type} is not implemented")

        alpha = self.mtlalpha
        if alpha == 0:
            self.loss = loss_att
            loss_att_data = float(loss_att)
            loss_ctc_data = None
        elif alpha == 1:
            self.loss = loss_ctc
            loss_att_data = None
            loss_ctc_data = float(loss_ctc)
        else:
            self.loss = alpha * loss_ctc + (1 - alpha) * loss_att
            loss_att_data = float(loss_att)
            loss_ctc_data = float(loss_ctc)

        loss_data = float(self.loss)
        if loss_data < CTC_LOSS_THRESHOLD and not math.isnan(loss_data):
            self.reporter.report(
                loss_ctc_data, loss_att_data, self.acc, cer_ctc, cer, wer, loss_data
            )
        else:
            logging.warning("loss (=%f) is not correct", loss_data)
        return (self.loss, uda_loss) if (self.training and uda_loss is not None) else self.loss
    
    def adversarial_loss(self, src_hs_pad, tgt_hs_pad, alpha=1.0):
        loss_fn = torch.nn.BCELoss()
        src_hs_pad = ReverseLayerF.apply(src_hs_pad, alpha)
        
        tgt_hs_pad = ReverseLayerF.apply(tgt_hs_pad, alpha)
        src_domain = self.domain_classifier(src_hs_pad).view(-1, 1) # B, T, 1
        tgt_domain = self.domain_classifier(tgt_hs_pad).view(-1, 1) # B, T, 1
        device = src_hs_pad.device
        src_label = torch.ones(len(src_domain)).long().to(device)
        tgt_label = torch.zeros(len(tgt_domain)).long().to(device)
        domain_pred = torch.cat([src_domain, tgt_domain], dim=0)
        domain_label = torch.cat([src_label, tgt_label], dim=0)
        uda_loss = loss_fn(domain_pred, domain_label[:, None].float()) # B, 1
        return uda_loss

    def get_enc_repr(self, 
                    src_hs_pad, 
                    src_hlens, 
                    tgt_hs_pad, 
                    tgt_hlens, 
                    src_ys_pad, 
                    tgt_ys_pad, 
                    method,
                    src_ctc_softmax=None,
                    tgt_ctc_softmax=None):
        src_ys = [y[y != self.ignore_id] for y in src_ys_pad]
        tgt_ys = [y[y != self.ignore_id] for y in tgt_ys_pad]
        if meth
Download .txt
gitextract_9o7g7l9e/

├── .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
│   │   │   ├── 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
│   │   │   ├── 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
│   │   │   ├── 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
│   │       ├── 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
│   │   ├── 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
Download .txt
SYMBOL INDEX (1793 symbols across 247 files)

FILE: code/ASR/Adapter/balanced_sampler.py
  class BalancedBatchSampler (line 11) | class BalancedBatchSampler(torch.utils.data.sampler.Sampler):
    method __init__ (line 15) | def __init__(self, dataset, labels=None):
    method __iter__ (line 36) | def __iter__(self):
    method _get_label (line 43) | def _get_label(self, dataset, idx):
    method __len__ (line 53) | def __len__(self):

FILE: code/ASR/Adapter/data_load.py
  function read_json_file (line 33) | def read_json_file(fname):
  function load_json (line 37) | def load_json(train_json_file, dev_json_file, test_json_file):
  function load_data (line 60) | def load_data(root_path, dataset, args):
  function load_multilingual_data (line 184) | def load_multilingual_data(root_path, datasets, args, languages):
  function load_token_list (line 348) | def load_token_list(token_file):
  function load_bpemodel (line 355) | def load_bpemodel(root_path, dataset):

FILE: code/ASR/Adapter/e2e_asr_adaptertransformer.py
  class SimAdapter (line 38) | class SimAdapter(MultiHeadedAttention):
    method __init__ (line 39) | def __init__(self, n_feat, dropout_rate, fusion_languages=None, num_sh...
    method forward_qkv (line 54) | def forward_qkv(self, query, key, value):
    method forward_attention (line 60) | def forward_attention(self, value, scores):
    method forward (line 68) | def forward(self, query, key, value, residual=None):
  class Adapter (line 79) | class Adapter(torch.nn.Module):
    method __init__ (line 80) | def __init__(self, adapter_dim, embed_dim):
    method forward (line 86) | def forward(self, z):
  class CustomEncoderLayer (line 90) | class CustomEncoderLayer(EncoderLayer):
    method forward (line 91) | def forward(self, x, mask, cache=None):
  class AdaptiveEncoderLayer (line 119) | class AdaptiveEncoderLayer(CustomEncoderLayer):
    method __init__ (line 120) | def __init__(
    method forward (line 154) | def forward(self, x, mask, language, cache=None, use_sim_adapter=True):
  class AdaptiveEncoder (line 177) | class AdaptiveEncoder(Encoder):
    method __init__ (line 178) | def __init__(
    method forward (line 252) | def forward(self, xs, masks, language, use_sim_adapter=True):
  class CustomDecoderLayer (line 258) | class CustomDecoderLayer(DecoderLayer):
    method forward (line 259) | def forward(self, tgt, tgt_mask, memory, memory_mask, cache=None):
  class AdaptiveDecoderLayer (line 309) | class AdaptiveDecoderLayer(CustomDecoderLayer):
    method __init__ (line 310) | def __init__(
    method forward (line 346) | def forward(self, tgt, tgt_mask, memory, memory_mask, language, cache=...
  class AdaptiveDecoder (line 369) | class AdaptiveDecoder(Decoder):
    method __init__ (line 370) | def __init__(
    method forward (line 454) | def forward(self, tgt, tgt_mask, memory, memory_mask, language, use_si...
    method forward_one_step (line 464) | def forward_one_step(self, tgt, tgt_mask, memory, memory_mask, languag...
  class E2E (line 482) | class E2E(E2ETransformer):
    method __init__ (line 483) | def __init__(self, idim, odim_dict, args, languages, ignore_id=-1):
    method reset_sim_adapter_parameters (line 584) | def reset_sim_adapter_parameters(self):
    method enable_sim_adapter_training (line 594) | def enable_sim_adapter_training(self):
    method get_fusion_guide_loss (line 605) | def get_fusion_guide_loss(self, language):
    method get_fusion_regularization_loss (line 625) | def get_fusion_regularization_loss(self):
    method enable_adapter_training (line 637) | def enable_adapter_training(self, specified_languages=None, shared_ada...
    method forward (line 661) | def forward(self, xs_pad, ilens, ys_pad, language, use_sim_adapter=True):
    method calculate_all_ctc_probs (line 740) | def calculate_all_ctc_probs(self, xs_pad, ilens, ys_pad, language):
    method calculate_all_attentions (line 761) | def calculate_all_attentions(self, xs_pad, ilens, ys_pad, language):
    method calculate_sim_adapter_attentions (line 786) | def calculate_sim_adapter_attentions(self, xs_pad, ilens, ys_pad, lang...
    method encode (line 800) | def encode(self, x, language):
    method set_recognize_language_branch (line 806) | def set_recognize_language_branch(self, language):
    method recognize_batch (line 808) | def recognize_batch(self, xs, recog_args, char_list=None, rnnlm=None, ...

FILE: code/ASR/Adapter/train.py
  function add_custom_arguments (line 26) | def add_custom_arguments(parser):
  function train_epoch (line 69) | def train_epoch(dataloader, model, optimizer, epoch=None):
  function train_maml_epoch (line 129) | def train_maml_epoch(dataloader, model, optimizer, epoch=None):
  function test (line 205) | def test(epoch, dataloader, model, model_path=None, language=None, visua...
  function train (line 272) | def train(dataloaders, model, optimizer, save_path):

FILE: code/ASR/Adapter/utils.py
  function load_head_from_pretrained_model (line 10) | def load_head_from_pretrained_model(model, model_path):
  function load_adapter_from_pretrained_model (line 22) | def load_adapter_from_pretrained_model(model, model_path, src_adapter, t...
  function str2bool (line 39) | def str2bool(str):
  function setup_logging (line 41) | def setup_logging(verbose=1):
  function dict_average (line 55) | def dict_average(dic):
  function load_pretrained_model (line 66) | def load_pretrained_model(model, model_path, modules_to_load=None, exclu...
  function torch_save (line 86) | def torch_save(model, save_path, optimizer=None, local_rank=0):
  function torch_load (line 94) | def torch_load(snapshot_path, model, optimizer=None):
  function compute_wer (line 109) | def compute_wer(ref, hyp, normalize=False):
  function token2text (line 194) | def token2text(tokens, bpemodel=None):
  function recognize_and_evaluate (line 204) | def recognize_and_evaluate(dataloader, model, args, model_path=None, wer...

FILE: code/ASR/CMatch/ctc_aligner.py
  function np2tensor (line 12) | def np2tensor(array, device=None):
  function pad_list (line 21) | def pad_list(xs, pad_value=0., pad_left=False):
  class CTCForcedAligner (line 41) | class CTCForcedAligner(object):
    method __init__ (line 42) | def __init__(self, blank=0, char_list=None):
    method __call__ (line 46) | def __call__(self, logits, elens, ys):
    method align (line 69) | def align(self, log_probs, elens, ys, ylens, add_eos=True):
  function _flip_label_probability (line 177) | def _flip_label_probability(log_probs, xlens):
  function _flip_path_probability (line 197) | def _flip_path_probability(cum_log_prob, xlens, path_lens):
  function _computes_transition (line 219) | def _computes_transition(seq_log_prob, same_transition, outside,
  function make_pad_mask (line 236) | def make_pad_mask(seq_lens):
  function _label_to_path (line 250) | def _label_to_path(labels, blank):
  function _flip_path (line 254) | def _flip_path(path, path_lens):

FILE: code/ASR/CMatch/data_load.py
  function read_json_file (line 118) | def read_json_file(fname):
  function load_json (line 124) | def load_json(train_json_file, dev_json_file, test_json_file):
  function load_data (line 149) | def load_data(root_path, dataset, args,
  function load_token_list (line 287) | def load_token_list(token_file):
  function load_bpemodel (line 295) | def load_bpemodel(root_path, dataset):

FILE: code/ASR/CMatch/distances.py
  function CORAL (line 3) | def CORAL(source, target):
  class MMD_loss (line 22) | class MMD_loss(torch.nn.Module):
    method __init__ (line 23) | def __init__(self, kernel_type='rbf', kernel_mul=2.0, kernel_num=5):
    method guassian_kernel (line 30) | def guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5...
    method linear_mmd2 (line 49) | def linear_mmd2(self, f_of_X, f_of_Y):
    method forward (line 55) | def forward(self, source, target):

FILE: code/ASR/CMatch/e2e_asr_udatransformer.py
  function adapt_loss (line 13) | def adapt_loss(source, target, adapt_loss="mmd"):
  class Discriminator (line 26) | class Discriminator(torch.nn.Module):
    method __init__ (line 27) | def __init__(self, input_dim=256, hidden_dim=256):
    method forward (line 34) | def forward(self, x):
  class ReverseLayerF (line 40) | class ReverseLayerF(torch.autograd.Function):
    method forward (line 42) | def forward(ctx, x, alpha):
    method backward (line 46) | def backward(ctx, grad_output):
  class CustomEncoderLayer (line 50) | class CustomEncoderLayer(EncoderLayer):
    method forward (line 51) | def forward(self, x, mask, cache=None):
  class CustomEncoder (line 83) | class CustomEncoder(Encoder):
    method __init__ (line 84) | def __init__(
    method forward (line 152) | def forward(self, xs, masks, return_repr=False):
  class CustomDecoderLayer (line 176) | class CustomDecoderLayer(DecoderLayer):
    method forward (line 177) | def forward(self, tgt, tgt_mask, memory, memory_mask, cache=None):
  class CustomDecoder (line 246) | class CustomDecoder(Decoder):
    method __init__ (line 247) | def __init__(
    method forward (line 310) | def forward(self, tgt, tgt_mask, memory, memory_mask, return_repr=False):
  class CustomSpeechTransformer (line 349) | class CustomSpeechTransformer(SpeechTransformer):
    method __init__ (line 350) | def __init__(self, *args, **kwargs):
  class UDASpeechTransformer (line 384) | class UDASpeechTransformer(CustomSpeechTransformer):
    method __init__ (line 385) | def __init__(self, *args, **kwargs):
    method forward (line 410) | def forward(self,
    method adversarial_loss (line 605) | def adversarial_loss(self, src_hs_pad, tgt_hs_pad, alpha=1.0):
    method get_enc_repr (line 620) | def get_enc_repr(self,
    method cmatch_loss_func (line 678) | def cmatch_loss_func(self, n_classes,

FILE: code/ASR/CMatch/train.py
  function add_custom_arguments (line 24) | def add_custom_arguments(parser):
  function test (line 68) | def test(dataloader, model, model_path=None):
  function train (line 90) | def train(dataloaders, model, optimizer, save_path):
  function train_epoch (line 130) | def train_epoch(dataloader, model, optimizer, epoch=None):
  function train_uda_epoch (line 160) | def train_uda_epoch(train_loaders, model, optimizer, epoch):

FILE: code/ASR/CMatch/utils.py
  function str2bool (line 9) | def str2bool(str):
  function setup_logging (line 12) | def setup_logging(verbose=1):
  function dict_average (line 26) | def dict_average(dic):
  function load_pretrained_model (line 37) | def load_pretrained_model(model, model_path, modules_to_load=None, exclu...
  function torch_save (line 57) | def torch_save(model, save_path, optimizer=None, local_rank=0):
  function torch_load (line 65) | def torch_load(snapshot_path, model, optimizer=None):
  function compute_wer (line 80) | def compute_wer(ref, hyp, normalize=False):
  function recognize_and_evaluate (line 164) | def recognize_and_evaluate(dataloader, model, args, model_path=None, wer...

FILE: code/DeepDA/backbones.py
  function get_backbone (line 12) | def get_backbone(name):
  class DaNNBackbone (line 20) | class DaNNBackbone(nn.Module):
    method __init__ (line 21) | def __init__(self, n_input=224*224*3, n_hidden=256):
    method forward (line 28) | def forward(self, x):
    method output_num (line 35) | def output_num(self):
  class AlexNetBackbone (line 39) | class AlexNetBackbone(nn.Module):
    method __init__ (line 40) | def __init__(self):
    method forward (line 50) | def forward(self, x):
    method output_num (line 56) | def output_num(self):
  class ResNetBackbone (line 59) | class ResNetBackbone(nn.Module):
    method __init__ (line 60) | def __init__(self, network_type):
    method forward (line 75) | def forward(self, x):
    method output_num (line 88) | def output_num(self):

FILE: code/DeepDA/data_loader.py
  function load_data (line 4) | def load_data(data_folder, batch_size, train, num_workers=0, **kwargs):
  function get_data_loader (line 27) | def get_data_loader(dataset, batch_size, shuffle=True, drop_last=False, ...
  class _InfiniteSampler (line 33) | class _InfiniteSampler(torch.utils.data.Sampler):
    method __init__ (line 35) | def __init__(self, sampler):
    method __iter__ (line 38) | def __iter__(self):
  class InfiniteDataLoader (line 43) | class InfiniteDataLoader:
    method __init__ (line 44) | def __init__(self, dataset, batch_size, shuffle=True, drop_last=False,...
    method __iter__ (line 64) | def __iter__(self):
    method __len__ (line 68) | def __len__(self):

FILE: code/DeepDA/loss_funcs/adv.py
  class LambdaSheduler (line 7) | class LambdaSheduler(nn.Module):
    method __init__ (line 8) | def __init__(self, gamma=1.0, max_iter=1000, **kwargs):
    method lamb (line 14) | def lamb(self):
    method step (line 19) | def step(self):
  class AdversarialLoss (line 22) | class AdversarialLoss(nn.Module):
    method __init__ (line 26) | def __init__(self, gamma=1.0, max_iter=1000, use_lambda_scheduler=True...
    method forward (line 33) | def forward(self, source, target):
    method get_adversarial_result (line 43) | def get_adversarial_result(self, x, source=True, lamb=1.0):
  class ReverseLayerF (line 56) | class ReverseLayerF(Function):
    method forward (line 58) | def forward(ctx, x, alpha):
    method backward (line 63) | def backward(ctx, grad_output):
  class Discriminator (line 67) | class Discriminator(nn.Module):
    method __init__ (line 68) | def __init__(self, input_dim=256, hidden_dim=256):
    method forward (line 84) | def forward(self, x):

FILE: code/DeepDA/loss_funcs/bnm.py
  function BNM (line 3) | def BNM(src, tar):

FILE: code/DeepDA/loss_funcs/coral.py
  function CORAL (line 4) | def CORAL(source, target, **kwargs):

FILE: code/DeepDA/loss_funcs/daan.py
  class DAANLoss (line 3) | class DAANLoss(AdversarialLoss, LambdaSheduler):
    method __init__ (line 4) | def __init__(self, num_class, gamma=1.0, max_iter=1000, **kwargs):
    method forward (line 14) | def forward(self, source, target, source_logits, target_logits):
    method get_local_adversarial_result (line 30) | def get_local_adversarial_result(self, x, logits, c, source=True, lamb...
    method update_dynamic_factor (line 47) | def update_dynamic_factor(self, epoch_length):

FILE: code/DeepDA/loss_funcs/lmmd.py
  class LMMDLoss (line 6) | class LMMDLoss(MMDLoss, LambdaSheduler):
    method __init__ (line 7) | def __init__(self, num_class, kernel_type='rbf', kernel_mul=2.0, kerne...
    method forward (line 16) | def forward(self, source, target, source_label, target_logits):
    method cal_weight (line 43) | def cal_weight(self, source_label, target_logits):

FILE: code/DeepDA/loss_funcs/mmd.py
  class MMDLoss (line 4) | class MMDLoss(nn.Module):
    method __init__ (line 5) | def __init__(self, kernel_type='rbf', kernel_mul=2.0, kernel_num=5, fi...
    method guassian_kernel (line 12) | def guassian_kernel(self, source, target, kernel_mul, kernel_num, fix_...
    method linear_mmd2 (line 31) | def linear_mmd2(self, f_of_X, f_of_Y):
    method forward (line 37) | def forward(self, source, target):

FILE: code/DeepDA/main.py
  function get_parser (line 11) | def get_parser():
  function set_random_seed (line 54) | def set_random_seed(seed=0):
  function load_data (line 63) | def load_data(args):
  function get_model (line 77) | def get_model(args):
  function get_optimizer (line 82) | def get_optimizer(model, args):
  function get_scheduler (line 88) | def get_scheduler(optimizer, args):
  function test (line 92) | def test(model, target_test_loader, args):
  function train (line 109) | def train(source_loader, target_train_loader, target_test_loader, model,...
  function main (line 171) | def main():

FILE: code/DeepDA/models.py
  class TransferNet (line 7) | class TransferNet(nn.Module):
    method __init__ (line 8) | def __init__(self, num_class, base_net='resnet50', transfer_loss='mmd'...
    method forward (line 33) | def forward(self, source, target, source_label):
    method get_parameters (line 60) | def get_parameters(self, initial_lr=1.0):
    method predict (line 83) | def predict(self, x):
    method epoch_based_processing (line 90) | def epoch_based_processing(self, *args, **kwargs):

FILE: code/DeepDA/transfer_losses.py
  class TransferLoss (line 5) | class TransferLoss(nn.Module):
    method __init__ (line 6) | def __init__(self, loss_type, **kwargs):
    method forward (line 25) | def forward(self, source, target, **kwargs):

FILE: code/DeepDA/utils.py
  class AverageMeter (line 1) | class AverageMeter(object):
    method __init__ (line 4) | def __init__(self):
    method reset (line 7) | def reset(self):
    method update (line 13) | def update(self, val, n=1):
  function str2bool (line 19) | def str2bool(v):

FILE: code/DeepDG/alg/alg.py
  function get_algorithm_class (line 29) | def get_algorithm_class(algorithm_name):

FILE: code/DeepDG/alg/algs/ANDMask.py
  class ANDMask (line 8) | class ANDMask(ERM):
    method __init__ (line 9) | def __init__(self, args):
    method update (line 14) | def update(self, minibatches, opt, sch):
    method mask_grads (line 44) | def mask_grads(self, tau, gradients, params):

FILE: code/DeepDG/alg/algs/CORAL.py
  class CORAL (line 7) | class CORAL(ERM):
    method __init__ (line 8) | def __init__(self, args):
    method coral (line 13) | def coral(self, x, y):
    method update (line 26) | def update(self, minibatches, opt, sch):

FILE: code/DeepDG/alg/algs/DANN.py
  class DANN (line 11) | class DANN(Algorithm):
    method __init__ (line 13) | def __init__(self, args):
    method update (line 24) | def update(self, minibatches, opt, sch):
    method predict (line 50) | def predict(self, x):

FILE: code/DeepDG/alg/algs/DIFEX.py
  class DIFEX (line 12) | class DIFEX(Algorithm):
    method __init__ (line 13) | def __init__(self, args):
    method teanettrain (line 35) | def teanettrain(self, dataloaders, epochs, opt1, sch1):
    method coral (line 58) | def coral(self, x, y):
    method update (line 71) | def update(self, minibatches, opt, sch):
    method predict (line 115) | def predict(self, x):

FILE: code/DeepDG/alg/algs/ERM.py
  class ERM (line 11) | class ERM(Algorithm):
    method __init__ (line 16) | def __init__(self, args):
    method update (line 25) | def update(self, minibatches, opt, sch):
    method predict (line 37) | def predict(self, x):

FILE: code/DeepDG/alg/algs/GroupDRO.py
  class GroupDRO (line 6) | class GroupDRO(ERM):
    method __init__ (line 11) | def __init__(self,args):
    method update (line 16) | def update(self, minibatches, opt,sch):

FILE: code/DeepDG/alg/algs/MLDG.py
  class MLDG (line 12) | class MLDG(ERM):
    method __init__ (line 13) | def __init__(self, args):
    method update (line 17) | def update(self, minibatches, opt, sch):

FILE: code/DeepDG/alg/algs/MMD.py
  class MMD (line 8) | class MMD(ERM):
    method __init__ (line 9) | def __init__(self, args):
    method my_cdist (line 14) | def my_cdist(self, x1, x2):
    method gaussian_kernel (line 22) | def gaussian_kernel(self, x, y, gamma=[0.001, 0.01, 0.1, 1, 10, 100,
    method mmd (line 32) | def mmd(self, x, y):
    method update (line 38) | def update(self, minibatches, opt, sch):

FILE: code/DeepDG/alg/algs/Mixup.py
  class Mixup (line 9) | class Mixup(ERM):
    method __init__ (line 10) | def __init__(self, args):
    method update (line 14) | def update(self, minibatches, opt, sch):

FILE: code/DeepDG/alg/algs/RSC.py
  class RSC (line 10) | class RSC(ERM):
    method __init__ (line 11) | def __init__(self, args):
    method update (line 17) | def update(self, minibatches, opt, sch):

FILE: code/DeepDG/alg/algs/VREx.py
  class VREx (line 7) | class VREx(ERM):
    method __init__ (line 10) | def __init__(self, args):
    method update (line 15) | def update(self, minibatches, opt, sch):

FILE: code/DeepDG/alg/algs/base.py
  class Algorithm (line 5) | class Algorithm(torch.nn.Module):
    method __init__ (line 7) | def __init__(self, args):
    method update (line 10) | def update(self, minibatches, opt, sch):
    method predict (line 13) | def predict(self, x):

FILE: code/DeepDG/alg/modelopera.py
  function get_fea (line 6) | def get_fea(args):
  function accuracy (line 16) | def accuracy(network, loader):

FILE: code/DeepDG/alg/opt.py
  function get_params (line 5) | def get_params(alg, args, inner=False, alias=True, isteacher=False):
  function get_optimizer (line 49) | def get_optimizer(alg, args, inner=False, alias=True, isteacher=False):
  function get_scheduler (line 56) | def get_scheduler(optimizer, args):

FILE: code/DeepDG/datautil/getdataloader.py
  function get_img_dataloader (line 11) | def get_img_dataloader(args):

FILE: code/DeepDG/datautil/imgdata/imgdataload.py
  class ImageDataset (line 10) | class ImageDataset(object):
    method __init__ (line 11) | def __init__(self, dataset, task, root_dir, domain_name, domain_label=...
    method set_labels (line 35) | def set_labels(self, tlabels=None, label_type='domain_label'):
    method target_trans (line 42) | def target_trans(self, y):
    method input_trans (line 48) | def input_trans(self, x):
    method __getitem__ (line 54) | def __getitem__(self, index):
    method __len__ (line 61) | def __len__(self):

FILE: code/DeepDG/datautil/imgdata/util.py
  function image_train (line 7) | def image_train(dataset, resize_size=256, crop_size=224):
  function image_test (line 28) | def image_test(dataset, resize_size=256, crop_size=224):
  function rgb_loader (line 45) | def rgb_loader(path):
  function l_loader (line 51) | def l_loader(path):

FILE: code/DeepDG/datautil/mydataloader.py
  class _InfiniteSampler (line 6) | class _InfiniteSampler(torch.utils.data.Sampler):
    method __init__ (line 9) | def __init__(self, sampler):
    method __iter__ (line 12) | def __iter__(self):
  class InfiniteDataLoader (line 18) | class InfiniteDataLoader:
    method __init__ (line 19) | def __init__(self, dataset, weights, batch_size, num_workers):
    method __iter__ (line 44) | def __iter__(self):
    method __len__ (line 48) | def __len__(self):

FILE: code/DeepDG/datautil/util.py
  function Nmax (line 6) | def Nmax(test_envs, d):
  function random_pairs_of_minibatches_by_domainperm (line 13) | def random_pairs_of_minibatches_by_domainperm(minibatches):
  function random_pairs_of_minibatches (line 30) | def random_pairs_of_minibatches(args, minibatches):

FILE: code/DeepDG/network/Adver_network.py
  class ReverseLayerF (line 6) | class ReverseLayerF(Function):
    method forward (line 8) | def forward(ctx, x, alpha):
    method backward (line 13) | def backward(ctx, grad_output):
  class Discriminator (line 18) | class Discriminator(nn.Module):
    method __init__ (line 19) | def __init__(self, input_dim=256, hidden_dim=256, num_domains=4):
    method forward (line 34) | def forward(self, x):

FILE: code/DeepDG/network/common_network.py
  class feat_bottleneck (line 7) | class feat_bottleneck(nn.Module):
    method __init__ (line 8) | def __init__(self, feature_dim, bottleneck_dim=256, type="ori"):
    method forward (line 17) | def forward(self, x):
  class feat_classifier (line 24) | class feat_classifier(nn.Module):
    method __init__ (line 25) | def __init__(self, class_num, bottleneck_dim=256, type="linear"):
    method forward (line 36) | def forward(self, x):
  class feat_classifier_two (line 41) | class feat_classifier_two(nn.Module):
    method __init__ (line 42) | def __init__(self, class_num, input_dim, bottleneck_dim=256):
    method forward (line 50) | def forward(self, x):

FILE: code/DeepDG/network/img_network.py
  class VGGBase (line 9) | class VGGBase(nn.Module):
    method __init__ (line 10) | def __init__(self, vgg_name):
    method forward (line 20) | def forward(self, x):
  class ResBase (line 31) | class ResBase(nn.Module):
    method __init__ (line 32) | def __init__(self, res_name):
    method forward (line 46) | def forward(self, x):
  class DTNBase (line 60) | class DTNBase(nn.Module):
    method __init__ (line 61) | def __init__(self):
    method forward (line 79) | def forward(self, x):
  class LeNetBase (line 85) | class LeNetBase(nn.Module):
    method __init__ (line 86) | def __init__(self):
    method forward (line 99) | def forward(self, x):

FILE: code/DeepDG/network/util.py
  function calc_coeff (line 6) | def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
  function init_weights (line 10) | def init_weights(m):

FILE: code/DeepDG/train.py
  function get_args (line 15) | def get_args():

FILE: code/DeepDG/utils/util.py
  function set_random_seed (line 11) | def set_random_seed(seed=0):
  function save_checkpoint (line 21) | def save_checkpoint(filename, alg, args):
  function train_valid_target_eval_names (line 29) | def train_valid_target_eval_names(args):
  function alg_loss_dict (line 45) | def alg_loss_dict(args):
  function print_args (line 61) | def print_args(args, print_list):
  function print_environ (line 70) | def print_environ():
  class Tee (line 81) | class Tee:
    method __init__ (line 82) | def __init__(self, fname, mode="a"):
    method write (line 86) | def write(self, message):
    method flush (line 91) | def flush(self):
  function img_param_init (line 96) | def img_param_init(args):

FILE: code/clip/clip_model.py
  class ClipModel (line 14) | class ClipModel(object):
    method __init__ (line 28) | def __init__(self, model_name='Vit-B/32', device='cuda', logger=None):
    method index_to_model (line 39) | def index_to_model(self, index):
    method get_model_name_by_index (line 43) | def get_model_name_by_index(index):
    method get_image_features (line 48) | def get_image_features(self, image, need_preprocess=False):
    method get_text_feature (line 55) | def get_text_feature(self, text):
    method get_text_features_list (line 61) | def get_text_features_list(self, texts, train=False):
    method get_similarity (line 74) | def get_similarity(self, image_features, text_features):
    method get_topk (line 80) | def get_topk(self, image, text, k=1):
    method feature_extraction (line 85) | def feature_extraction(self, dataloader):
    method finetune (line 101) | def finetune(self, dataloader, testloader, optimizer, nepochs=10, save...
    method evaluate (line 138) | def evaluate(self, dataloader, modelpath=None):

FILE: code/clip/data/data_loader.py
  class ImageTextData (line 10) | class ImageTextData(object):
    method __init__ (line 12) | def __init__(self, dataset, root, preprocess, prompt='a picture of a'):
    method __getitem__ (line 32) | def __getitem__(self, index):
    method __len__ (line 39) | def __len__(self):
    method get_data_name_by_index (line 43) | def get_data_name_by_index(index):

FILE: code/clip/data/download_data.py
  function stage_path (line 20) | def stage_path(data_dir, name):
  function download_and_extract (line 29) | def download_and_extract(url, dst, remove=True):
  function download_vlcs (line 97) | def download_vlcs(data_dir):
  function download_mnist (line 107) | def download_mnist(data_dir):
  function download_pacs (line 115) | def download_pacs(data_dir):
  function download_office_home (line 128) | def download_office_home(data_dir):
  function download_domain_net (line 141) | def download_domain_net(data_dir):
  function download_terra_incognita (line 167) | def download_terra_incognita(data_dir):
  function download_sviro (line 250) | def download_sviro(data_dir):

FILE: code/clip/data/download_data_azcopy.py
  function get_args (line 19) | def get_args():

FILE: code/clip/main.py
  function get_args (line 12) | def get_args():
  function main (line 35) | def main(args):
  function sweep_index (line 71) | def sweep_index(model=-1, data=-1):
  function sweep (line 87) | def sweep(model=-1, data=-1):

FILE: code/clip/test_clip.py
  function get_args (line 44) | def get_args():
  function load_data (line 52) | def load_data(dataset):
  function load_model (line 73) | def load_model(modelname):
  function classify_imagenetr (line 79) | def classify_imagenetr(imagenet_r, imagenetr_labels, model, preprocess, ...
  function perform_inference (line 105) | def perform_inference(model_index, data_index):
  function gather_res (line 121) | def gather_res(mid):
  function test (line 132) | def test():
  function sweep (line 142) | def sweep():

FILE: code/clip/utils.py
  function load_csv (line 7) | def load_csv(folder, src_domain, tar_domain):
  function kernel (line 14) | def kernel(ker, X1, X2, gamma):
  function set_seed (line 35) | def set_seed(seed):
  function set_gpu (line 43) | def set_gpu(gpuid):
  function get_logger (line 47) | def get_logger(logger_name, log_file, level=logging.INFO):
  function gather_res (line 61) | def gather_res(model_name_lst, data_name_lst):
  function convert_models_to_fp32 (line 73) | def convert_models_to_fp32(model):

FILE: code/deep/B-JMMD/caffe/include/caffe/layers/bjmmd_layer.hpp
  type caffe (line 12) | namespace caffe {
    class BJMMDLossLayer (line 15) | class BJMMDLossLayer : public LossLayer<Dtype> {
      method BJMMDLossLayer (line 17) | explicit BJMMDLossLayer(const LayerParameter& param)
      method ExactNumTopBlobs (line 25) | virtual inline int ExactNumTopBlobs() const { return -1; }
      method ExactNumBottomBlobs (line 26) | virtual inline int ExactNumBottomBlobs() const { return -1; }
      method MinBottomBlobs (line 27) | virtual inline int MinBottomBlobs() const { return 2; }
      method MaxBottomBlobs (line 28) | virtual inline int MaxBottomBlobs() const { return 4; }

FILE: code/deep/B-JMMD/caffe/src/caffe/layers/bjmmd_layer.cpp
  type caffe (line 5) | namespace caffe {

FILE: code/deep/CSG/a-domainbed/main.py
  class MergeIters (line 17) | class MergeIters:
    method __init__ (line 18) | def __init__(self, *itrs):
    method __iter__ (line 23) | def __iter__(self):
    method __len__ (line 27) | def __len__(self): return self.len

FILE: code/deep/CSG/a-domainbed/visual.py
  class MergeIters (line 36) | class MergeIters:
    method __init__ (line 37) | def __init__(self, *itrs):
    method __iter__ (line 42) | def __iter__(self):
    method __len__ (line 46) | def __len__(self): return self.len
  function get_input_transform (line 48) | def get_input_transform():
  function get_visual (line 59) | def get_visual(ag, ckpt, archtype, shape_x, dim_y,

FILE: code/deep/CSG/a-imageclef/visual.py
  function get_input_transform (line 32) | def get_input_transform():
  function get_visual (line 43) | def get_visual(ag, ckpt, archtype, shape_x, dim_y,

FILE: code/deep/CSG/a-mnist/makedata.py
  function select_xy (line 12) | def select_xy(dataset, selected_y = (0,1), piltransf = None, ytransf = N...
  function get_shift_transf (line 20) | def get_shift_transf(pleft: list, distr: str, loc: float, scale: float):

FILE: code/deep/CSG/arch/backbone.py
  class AlexNetFc (line 12) | class AlexNetFc(nn.Module):
    method __init__ (line 13) | def __init__(self):
    method forward (line 22) | def forward(self, x):
    method output_num (line 28) | def output_num(self):
  class ResNet18Fc (line 32) | class ResNet18Fc(nn.Module):
    method __init__ (line 33) | def __init__(self):
    method forward (line 47) | def forward(self, x):
    method output_num (line 60) | def output_num(self):
  class ResNet34Fc (line 64) | class ResNet34Fc(nn.Module):
    method __init__ (line 65) | def __init__(self):
    method forward (line 79) | def forward(self, x):
    method output_num (line 92) | def output_num(self):
  class ResNet50Fc (line 96) | class ResNet50Fc(nn.Module):
    method __init__ (line 97) | def __init__(self):
    method forward (line 111) | def forward(self, x):
    method output_num (line 124) | def output_num(self):
  class ResNet101Fc (line 128) | class ResNet101Fc(nn.Module):
    method __init__ (line 129) | def __init__(self):
    method forward (line 143) | def forward(self, x):
    method output_num (line 156) | def output_num(self):
  class ResNet152Fc (line 160) | class ResNet152Fc(nn.Module):
    method __init__ (line 161) | def __init__(self):
    method forward (line 175) | def forward(self, x):
    method output_num (line 188) | def output_num(self):

FILE: code/deep/CSG/arch/cnn.py
  function init_linear (line 27) | def init_linear(nnseq, wmean, wstd, bval):
  class CNNsvy1x (line 34) | class CNNsvy1x(nn.Module):
    method __init__ (line 35) | def __init__(self, backbone_stru: str, dim_bottleneck: int, dim_s: int...
    method _get_bb (line 158) | def _get_bb(self, x):
    method _get_bn (line 164) | def _get_bn(self, x):
    method v1x (line 170) | def v1x(self, x):
    method std_v1x (line 174) | def std_v1x(self, x):
    method s1vx (line 181) | def s1vx(self, v, x):
    method std_s1vx (line 188) | def std_s1vx(self, v, x):
    method s1x (line 199) | def s1x(self, x):
    method std_s1x (line 201) | def std_s1x(self, x):
    method y1s (line 207) | def y1s(self, s):
    method ys1x (line 210) | def ys1x(self, x):
    method forward (line 214) | def forward(self, x):
    method parameter_groups (line 217) | def parameter_groups(self):
    method save (line 220) | def save(self, path): tc.save(self.state_dict(), path)
    method load (line 221) | def load(self, path):
  function weights_init (line 226) | def weights_init(m):
  class CNN_DCGANvar_224 (line 235) | class CNN_DCGANvar_224(nn.Module):
    method __init__ (line 237) | def __init__(self, dim_in, dim_feat, dim_chanl = 3):
    method forward (line 266) | def forward(self, val):
  class CNN_DCGANpretr_224 (line 270) | class CNN_DCGANpretr_224(nn.Module):
    method __init__ (line 271) | def __init__(self, dim_in, dim_feat = None, dim_chanl = None):
    method forward (line 298) | def forward(self, val):
    method parameter_groups (line 302) | def parameter_groups(self):
  class CNN_PGANpretr_224 (line 305) | class CNN_PGANpretr_224(nn.Module):
    method __init__ (line 306) | def __init__(self, dim_in, dim_feat = None, dim_chanl = None):
    method forward (line 326) | def forward(self, val):
    method parameter_groups (line 330) | def parameter_groups(self):
  class CNNx1sv (line 334) | class CNNx1sv(nn.Module):
    method __init__ (line 335) | def __init__(self, dim_xside: int, dim_s: int, dim_v: int, dim_feat: i...
    method x1sv (line 340) | def x1sv(self, s, v):
    method forward (line 342) | def forward(self, s, v): return self.x1sv(s, v)
    method parameter_groups (line 343) | def parameter_groups(self):
    method save (line 346) | def save(self, path): tc.save(self.state_dict(), path)
    method load (line 347) | def load(self, path):
  class CNNx1s (line 351) | class CNNx1s(nn.Module):
    method __init__ (line 352) | def __init__(self, dim_xside: int, dim_s: int, dim_feat: int, dectype:...
    method x1s (line 357) | def x1s(self, s): return self.net(s)
    method forward (line 358) | def forward(self, s): return self.x1s(s)
    method parameter_groups (line 359) | def parameter_groups(self):
    method save (line 362) | def save(self, path): tc.save(self.state_dict(), path)
    method load (line 363) | def load(self, path):

FILE: code/deep/CSG/arch/mlp.py
  function init_linear (line 16) | def init_linear(nnseq, wmean, wstd, bval):
  function mlp_constructor (line 22) | def mlp_constructor(dims, actv = "Sigmoid", lastactv = True): # `Sequent...
  class MLPBase (line 30) | class MLPBase(nn.Module):
    method save (line 31) | def save(self, path): tc.save(self.state_dict(), path)
    method load (line 32) | def load(self, path):
    method load_or_save (line 35) | def load_or_save(self, filename):
  class MLP (line 42) | class MLP(MLPBase):
    method __init__ (line 43) | def __init__(self, dims, actv = "Sigmoid"):
    method forward (line 47) | def forward(self, x): return self.f_x2y(x).squeeze(-1)
  class MLPsvy1x (line 49) | class MLPsvy1x(MLPBase):
    method __init__ (line 50) | def __init__(self, dim_x, dims_postx2prev, dim_v, dim_parav, dims_post...
    method _get_prev (line 109) | def _get_prev(self, x):
    method v1x (line 115) | def v1x(self, x):
    method std_v1x (line 120) | def std_v1x(self, x):
    method _get_parav (line 126) | def _get_parav(self, x):
    method s1vx (line 132) | def s1vx(self, v, x):
    method std_s1vx (line 135) | def std_s1vx(self, v, x):
    method s1x (line 142) | def s1x(self, x):
    method std_s1x (line 144) | def std_s1x(self, x):
    method y1s (line 147) | def y1s(self, s):
    method ys1x (line 150) | def ys1x(self, x):
    method forward (line 154) | def forward(self, x):
  class MLPx1sv (line 157) | class MLPx1sv(MLPBase):
    method __init__ (line 158) | def __init__(self, dim_s = None, dims_pres2parav = None, dim_v = None,...
    method x1sv (line 173) | def x1sv(self, s, v): return self.f_vparav2x(tc.cat([v, self.f_s2parav...
    method forward (line 174) | def forward(self, s, v): return self.x1sv(s, v)
  class MLPx1s (line 176) | class MLPx1s(MLPBase):
    method __init__ (line 177) | def __init__(self, dim_s = None, dims_pres2postx = None, dim_x = None,
    method x1s (line 189) | def x1s(self, s): return self.f_s2x(s)
    method forward (line 190) | def forward(self, s): return self.x1s(s)
  class MLPv1s (line 192) | class MLPv1s(MLPBase):
    method __init__ (line 193) | def __init__(self, dim_s = None, dims_pres2postv = None, dim_v = None,
    method v1s (line 204) | def v1s(self, s): return self.f_s2v(s)
    method forward (line 205) | def forward(self, s): return self.v1s(s)
  function create_discr_from_json (line 207) | def create_discr_from_json(stru_name: str, dim_x: int, dim_y: int, actv:...
  function create_gen_from_json (line 214) | def create_gen_from_json(model_type: str="MLPx1sv", discr: MLPsvy1x=None...

FILE: code/deep/CSG/distr/base.py
  class Distr (line 18) | class Distr:
    method all_names (line 27) | def all_names() -> set: return set(Distr._shapes_all_vars)
    method clear (line 29) | def clear(): Distr._shapes_all_vars.clear()
    method has_name (line 31) | def has_name(name: str) -> bool: return name in Distr._shapes_all_vars
    method has_names (line 33) | def has_names(names: set) -> bool:
    method shape_var (line 36) | def shape_var(name: str) -> tc.Size: return Distr._shapes_all_vars[name]
    method shapes_var (line 38) | def shapes_var(names: set) -> dict:
    method shape_bat (line 41) | def shape_bat(conds: edic) -> tc.Size:
    method shape_bat_broadcast (line 46) | def shape_bat_broadcast(conds: edic) -> tc.Size:
    method broadcast_vars (line 51) | def broadcast_vars(conds: edic) -> edic:
    method __init__ (line 56) | def __init__(self, *, names: set=set(), names_shapes: dict={}, parents...
    method names (line 71) | def names(self) -> set: return self._names
    method parents (line 73) | def parents(self) -> set: return self._parents
    method is_root (line 75) | def is_root(self) -> bool: return self._is_root
    method __repr__ (line 77) | def __repr__(self) -> str:
    method mean (line 80) | def mean(self, conds: edic=edic(), n_mc: int=10, repar: bool=True) -> ...
    method expect (line 84) | def expect(self, fn, conds: edic=edic(), n_mc: int=10, repar: bool=Tru...
    method rdraw (line 94) | def rdraw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic()) -> ed...
    method draw (line 98) | def draw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic(), repar:...
    method logp (line 102) | def logp(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # log_probs
    method logp_cartes (line 106) | def logp_cartes(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # ...
    method __mul__ (line 116) | def __mul__(self, other): # p(z|w1) * p(x|z,w2) -> p(z,x|w1,w2)
    method marg (line 169) | def marg(self, mnames: set, n_mc: int=10):
  class DistrElem (line 223) | class DistrElem(Distr):
    method __init__ (line 224) | def __init__(self, name: str, shape: tc.Size, device = None, **params):
    method name (line 256) | def name(self): return self._name
    method shape (line 258) | def shape(self): return self._shape
    method device (line 260) | def device(self): return self._device
    method mode (line 262) | def mode(self, conds: edic=edic(), repar: bool=True) -> edic:

FILE: code/deep/CSG/distr/instances.py
  class Determ (line 24) | class Determ(DistrElem):
    method __init__ (line 25) | def __init__(self, name: str, val, shape = None, checkval: bool=False,...
    method mean (line 29) | def mean(self, conds: edic=edic(), n_mc: int=10, repar: bool=True) -> ...
    method mode (line 33) | def mode(self, conds: edic=edic(), repar: bool=True) -> edic:
    method draw (line 37) | def draw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic(), repar:...
    method logp (line 42) | def logp(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # log_probs
    method entropy (line 53) | def entropy(self, conds: edic=edic()) -> tc.Tensor:
  class Normal (line 57) | class Normal(DistrElem):
    method __init__ (line 58) | def __init__(self, name: str, mean = 0., std = 1., shape = None, devic...
    method mean (line 62) | def mean(self, conds: edic=edic(), n_mc: int=10, repar: bool=True) -> ...
    method mode (line 66) | def mode(self, conds: edic=edic(), repar: bool=True) -> edic:
    method draw (line 70) | def draw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic(), repar:...
    method logp (line 76) | def logp(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # log_probs
    method entropy (line 84) | def entropy(self, conds: edic=edic()) -> tc.Tensor:
  class MVNormal (line 90) | class MVNormal(DistrElem):
    method __init__ (line 91) | def __init__(self, name: str, mean = 0., cov = None, prec = None, std_...
    method mean (line 158) | def mean(self, conds: edic=edic(), n_mc: int=10, repar: bool=True) -> ...
    method mode (line 162) | def mode(self, conds: edic=edic(), repar: bool=True) -> edic:
    method draw (line 166) | def draw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic(), repar:...
    method logp (line 173) | def logp(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # log_probs
    method entropy (line 182) | def entropy(self, conds: edic=edic()) -> tc.Tensor:
  class Catg (line 188) | class Catg(DistrElem):
    method __init__ (line 189) | def __init__(self, name: str, *, probs = None, logits = None, shape = ...
    method _probsfn (line 218) | def _probsfn(self): return lambda conds: tcdu.logits_to_probs(self._lo...
    method _logitsfn (line 220) | def _logitsfn(self): return lambda conds: tcdu.probs_to_logits(self._p...
    method mode (line 223) | def mode(self, conds: edic=edic(), repar: bool=True) -> edic:
    method draw (line 227) | def draw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic(), repar:...
    method logp (line 237) | def logp(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # log_probs
    method entropy (line 245) | def entropy(self, conds: edic=edic()) -> tc.Tensor:
  class Bern (line 250) | class Bern(DistrElem):
    method __init__ (line 251) | def __init__(self, name: str, *, probs = None, logits = None, shape = ...
    method _probsfn (line 258) | def _probsfn(self): return lambda conds: tcdu.logits_to_probs(self._lo...
    method _logitsfn (line 260) | def _logitsfn(self): return lambda conds: tcdu.probs_to_logits(self._p...
    method mode (line 263) | def mode(self, conds: edic=edic(), repar: bool=True) -> edic:
    method draw (line 267) | def draw(self, shape_mc: tc.Size=tc.Size(), conds: edic=edic(), repar:...
    method logp (line 274) | def logp(self, vals: edic, conds: edic=edic()) -> tc.Tensor: # log_probs
    method entropy (line 281) | def entropy(self, conds: edic=edic()) -> tc.Tensor:

FILE: code/deep/CSG/distr/tools.py
  function elbo (line 19) | def elbo(p_joint: Distr, q_cond: Distr, obs: edic, n_mc: int=10, repar: ...
  function elbo_z2xy (line 25) | def elbo_z2xy(p_zx: Distr, p_y1z: Distr, q_z1x: Distr, obs_xy: edic, n_m...
  function elbo_z2xy_twist (line 47) | def elbo_z2xy_twist(pt_zx: Distr, p_y1z: Distr, p_z: Distr, pt_z: Distr,...
  function elbo_zy2x (line 69) | def elbo_zy2x(p_zyx: Distr, q_y1x: Distr, q_z1xy: Distr, obs_x: edic, n_...

FILE: code/deep/CSG/distr/utils.py
  class edic (line 19) | class edic(dict):
    method __and__ (line 20) | def __and__(self, other): return edic({k:other[k] for k in set(self) &...
    method __rand__ (line 21) | def __rand__(self, other): return edic({k:self[k] for k in set(other) ...
    method __or__ (line 22) | def __or__(self, other): return edic({**self, **other}) # |
    method __ror__ (line 23) | def __ror__(self, other): return edic({**other, **self}) # |
    method __sub__ (line 24) | def __sub__(self, other): return edic({k:v for k,v in self.items() if ...
    method __rsub__ (line 25) | def __rsub__(self, other): return edic({k:v for k,v in other.items() i...
    method isdisjoint (line 26) | def isdisjoint(self, other) -> bool: return set(self).isdisjoint(set(o...
    method sub (line 28) | def sub(self, it, fn = None):
    method sub_expand_front (line 30) | def sub_expand_front(self, it, shape: tc.Size):
    method subedic (line 32) | def subedic(self, it, fn = None, use_default = False, default = None):
    method sublist (line 39) | def sublist(self, it, fn = None, use_default = False, default = None):
    method key0 (line 47) | def key0(self): return next(iter(self))
    method value0 (line 48) | def value0(self): return next(iter(self.values()))
    method item0 (line 49) | def item0(self): return next(iter(self.items()))
    method mean (line 51) | def mean(self, dim, keepdim: bool=False):
    method expand_front (line 53) | def expand_front(self, shape: tc.Size):
    method broadcast (line 55) | def broadcast(self):
  function edicify (line 58) | def edicify(*args) -> tuple:
  function fargnames (line 62) | def fargnames(fn) -> set:
  function fedic (line 66) | def fedic(fn):
  function wrap4_multi_batchdims (line 73) | def wrap4_multi_batchdims(fn, ndim_vars = 1):
  function append_attrs (line 115) | def append_attrs(obj, vardict, attrs): # obj = self, vardict = locals(),...
  function tensorify (line 119) | def tensorify(device=None, *args) -> tuple:
  function is_scalar (line 122) | def is_scalar(ten: tc.Tensor) -> bool:
  function is_same_tensor (line 125) | def is_same_tensor(ten1: tc.Tensor, ten2: tc.Tensor) -> bool:
  function expand_front (line 130) | def expand_front(ten: tc.Tensor, shape: tc.Size) -> tc.Tensor:
  function flatten_last (line 133) | def flatten_last(ten: tc.Tensor, ndims: int):
  function reduce_last (line 137) | def reduce_last(reducefn, ten: tc.Tensor, ndims: int): # tc.distribution...
  function swap_dim_ranges (line 142) | def swap_dim_ranges(ten: tc.Tensor, dims1: tuple, dims2: tuple) -> tc.Te...
  function expand_middle (line 157) | def expand_middle(ten: tc.Tensor, shape: tc.Size, pos: int) -> tc.Tensor:
  function tcsizeify (line 166) | def tcsizeify(*args) -> tuple:
  function tcsize_div (line 169) | def tcsize_div(sz1: tc.Size, sz2: tc.Size) -> tc.Size:
  function tcsize_broadcast (line 173) | def tcsize_broadcast(*sizes) -> tc.Size:
  function normalize_probs (line 185) | def normalize_probs(probs: tc.Tensor) -> tc.Tensor:
  function normalize_logits (line 188) | def normalize_logits(logits: tc.Tensor) -> tc.Tensor:
  function precision_to_scale_tril (line 191) | def precision_to_scale_tril(P: tc.Tensor) -> tc.Tensor:

FILE: code/deep/CSG/methods/cnbb.py
  class CNBBLoss (line 13) | class CNBBLoss:
    method __init__ (line 14) | def __init__(self, f_feat, actv, f_logit, dim_y, reg_w, reg_s, lr, n_i...
    method __call__ (line 23) | def __call__(self, x, y):

FILE: code/deep/CSG/methods/semvar.py
  class SemVar (line 16) | class SemVar:
    method _get_priors (line 18) | def _get_priors(mean_s, std_s, shape_s, mean_v, std_v, shape_v, corr_s...
    method __init__ (line 90) | def __init__(self, shape_s, shape_v, shape_x, dim_y,
    method parameters (line 153) | def parameters(self):
    method state_dict (line 156) | def state_dict(self):
    method load_state_dict (line 159) | def load_state_dict(self, state_dict: dict):
    method get_lossfn (line 163) | def get_lossfn(self, n_mc_q: int=0, reduction: str="mean", mode: str="...
    method llh (line 196) | def llh(self, x: tc.Tensor, y: tc.LongTensor=None, n_mc_marg: int=64, ...
    method logit_y1x_src (line 214) | def logit_y1x_src(self, x: tc.Tensor, n_mc_q: int=0, repar: bool=True):
    method generate (line 233) | def generate(self, shape_mc: tc.Size=tc.Size(), mode: str="src") -> tu...

FILE: code/deep/CSG/methods/supvae.py
  class SupVAE (line 14) | class SupVAE:
    method _get_priors (line 16) | def _get_priors(mean_s, std_s, shape_s, mvn_prior: bool = False, devic...
    method __init__ (line 38) | def __init__(self, shape_s, shape_x, dim_y,
    method parameters (line 78) | def parameters(self):
    method state_dict (line 81) | def state_dict(self):
    method load_state_dict (line 84) | def load_state_dict(self, state_dict: dict):
    method get_lossfn (line 88) | def get_lossfn(self, n_mc_q: int=0, reduction: str="mean", mode: str="...
    method llh (line 119) | def llh(self, x: tc.Tensor, y: tc.LongTensor=None, n_mc_marg: int=64, ...
    method logit_y1x_src (line 137) | def logit_y1x_src(self, x: tc.Tensor, n_mc_q: int=0, repar: bool=True):
    method generate (line 156) | def generate(self, shape_mc: tc.Size=tc.Size(), mode: str="src") -> tu...

FILE: code/deep/CSG/methods/xdistr.py
  function elbo_z2xy (line 14) | def elbo_z2xy(p_zx: Distr, p_y1z: Distr, q_z1x: Distr, obs_xy: edic, n_m...
  function elbo_z2xy_twist (line 36) | def elbo_z2xy_twist(pt_zx: Distr, p_y1z: Distr, p_z: Distr, pt_z: Distr,...
  function elbo_fixllh (line 58) | def elbo_fixllh(p_prior: Distr, p_llh: Distr, q_cond: Distr, obs: edic, ...
  function elbo_z2xy_twist_fixpt (line 68) | def elbo_z2xy_twist_fixpt(p_x1z: Distr, p_y1z: Distr, p_z: Distr, pt_z: ...

FILE: code/deep/CSG/test/distr_test.py
  function test_fun (line 18) | def test_fun(title, p_z, p_x1z):

FILE: code/deep/CSG/utils/preprocess/data_list.py
  function make_dataset (line 13) | def make_dataset(image_list, labels):
  function pil_loader (line 25) | def pil_loader(path):
  function accimage_loader (line 32) | def accimage_loader(path):
  function default_loader (line 41) | def default_loader(path):
  class ImageList (line 49) | class ImageList(object):
    method __init__ (line 70) | def __init__(self, image_list, labels=None, transform=None, target_tra...
    method __getitem__ (line 83) | def __getitem__(self, index):
    method __len__ (line 99) | def __len__(self):

FILE: code/deep/CSG/utils/preprocess/data_loader.py
  class PlaceCrop (line 13) | class PlaceCrop(object):
    method __init__ (line 14) | def __init__(self, size, start_x, start_y):
    method __call__ (line 22) | def __call__(self, img):
  class ResizeImage (line 27) | class ResizeImage():
    method __init__ (line 28) | def __init__(self, size):
    method __call__ (line 34) | def __call__(self, img):
  class myDataset (line 39) | class myDataset(data.Dataset):
    method __init__ (line 40) | def __init__(self, root, transform=None, train=True):
    method __getitem__ (line 66) | def __getitem__(self, index):
    method __len__ (line 78) | def __len__(self):
  function load_training (line 82) | def load_training(root_path, domain, batch_size, kwargs, train_val_split...
  function load_testing (line 106) | def load_testing(root_path, domain, batch_size, kwargs):
  function load_train_valid_split (line 124) | def load_train_valid_split(dataset, batch_size, kwargs, val_ratio=0.4, r...
  function load_data (line 142) | def load_data(root_path, source_dir, target_dir, batch_size):
  function load_all_test (line 152) | def load_all_test(root_path, dataset, batch_size, train, kwargs):

FILE: code/deep/CSG/utils/preprocess/data_provider.py
  class ResizeImage (line 8) | class ResizeImage():
    method __init__ (line 9) | def __init__(self, size):
    method __call__ (line 14) | def __call__(self, img):
  class PlaceCrop (line 19) | class PlaceCrop(object):
    method __init__ (line 21) | def __init__(self, size, start_x, start_y):
    method __call__ (line 29) | def __call__(self, img):
  function load_images (line 34) | def load_images(images_file_path, batch_size, resize_size=256, is_train=...

FILE: code/deep/CSG/utils/utils.py
  function unique_filename (line 15) | def unique_filename(prefix: str="", suffix: str="", n_digits: int=2, cou...
  class Averager (line 23) | class Averager:
    method __init__ (line 25) | def __init__(self):
    method reset (line 28) | def reset(self):
    method update (line 34) | def update(self, val, nrep = 1):
    method val (line 41) | def val(self): return self._val
    method avg (line 43) | def avg(self): return self._avg
    method sum (line 45) | def sum(self): return self._sum
    method count (line 47) | def count(self): return self._count
  function repeat_iterator (line 49) | def repeat_iterator(itr, n_repeat):
  class RepeatIterator (line 53) | class RepeatIterator:
    method __init__ (line 54) | def __init__(self, itr, n_repeat):
    method __iter__ (line 59) | def __iter__(self):
    method __len__ (line 62) | def __len__(self): return self.len
  function zip_longer (line 64) | def zip_longer(itr1, itr2):
  function zip_longest (line 72) | def zip_longest(*itrs):
  class ZipLongest (line 79) | class ZipLongest:
    method __init__ (line 80) | def __init__(self, *itrs):
    method __iter__ (line 85) | def __iter__(self):
    method __len__ (line 90) | def __len__(self): return self.len
  class CyclicLoader (line 93) | class CyclicLoader:
    method __init__ (line 94) | def __init__(self, datalist: list, shuffle: bool=True, cycle: int=None):
    method iter (line 102) | def iter(self):
    method next (line 105) | def next(self, n: int) -> tuple:
    method back (line 109) | def back(self, n: int):
  function boolstr (line 113) | def boolstr(s: str) -> bool:
  function getlist (line 125) | def getlist(ls: list, ids: list) -> list:
  function interleave (line 128) | def interleave(*lists) -> list:
  function flatten (line 131) | def flatten(ls: list, depth: int=None) -> list:
  class SlicesAt (line 139) | class SlicesAt:
    method __init__ (line 140) | def __init__(self, axis: int, ndim: int):
    method __getitem__ (line 145) | def __getitem__(self, idx):
  function moving_average_slim (line 151) | def moving_average_slim(arr: np.ndarray, n_win: int=2, axis: int=-1) -> ...
  function moving_average_full (line 159) | def moving_average_full(arr: np.ndarray, n_win: int=2, axis: int=-1) -> ...
  function moving_average_full_checker (line 176) | def moving_average_full_checker(arr: np.ndarray, n_win: int=2, axis: int...
  class Plotter (line 187) | class Plotter:
    method __init__ (line 188) | def __init__(self, var_xlab: dict, metr_ylab: dict, tab_items: list=None,
    method _get_res (line 202) | def _get_res(self, dataholder): # does not change `self`
    method load (line 249) | def load(self, *triplets):
    method clear (line 256) | def clear(self):
    method plot (line 260) | def plot(self, variables: list=None, metrics: list=None,
    method inspect (line 303) | def inspect(self, metr: str, ids: list=None, var: str=None, vals: list...
    method tabulate (line 327) | def tabulate(self, **tbformat):

FILE: code/deep/CSG/utils/utils_main.py
  function auto_load (line 39) | def auto_load(dc_vars, names, ckpt):
  function get_frame (line 47) | def get_frame(discr, gen, dc_vars, device = None, discr_src = None):
  function get_discr (line 86) | def get_discr(archtype, dc_vars):
  function get_gen (line 106) | def get_gen(archtype, dc_vars, discr):
  function get_models (line 123) | def get_models(archtype, dc_vars, ckpt = None, device = None):
  function load_ckpt (line 145) | def load_ckpt(filename: str, loadmodel: bool=False, device: tc.device=No...
  function get_ce_or_bce_loss (line 152) | def get_ce_or_bce_loss(discr, dim_y: int, reduction: str="mean"):
  function add_ce_loss (line 161) | def add_ce_loss(lossobj, celossfn, ag):
  function ood_methods (line 170) | def ood_methods(discr, frame, ag, dim_y, cnbb_actv):
  function da_methods (line 184) | def da_methods(discr, frame, ag, dim_x, dim_y, device, ckpt, discr_src =...
  class ParamGroupsCollector (line 268) | class ParamGroupsCollector:
    method __init__ (line 269) | def __init__(self, lr):
    method reset (line 272) | def reset(self, lr):
    method collect_params (line 276) | def collect_params(self, *models):
  class ShrinkRatio (line 290) | class ShrinkRatio:
    method __init__ (line 291) | def __init__(self, w_iter, decay_rate):
    method __call__ (line 295) | def __call__(self, n_iter):
  function acc_with_logits (line 299) | def acc_with_logits(model: tc.nn.Module, x: tc.Tensor, y: tc.LongTensor,...
  function evaluate_acc (line 304) | def evaluate_acc(discr, input_loader, is_binary, device):
  function evaluate_llhx (line 311) | def evaluate_llhx(frame, input_loader, n_marg_llh, use_q_llh, mode, devi...
  class ResultsContainer (line 318) | class ResultsContainer:
    method __init__ (line 319) | def __init__(self, len_ts, frame, ag, is_binary, device, ckpt = None):
    method update (line 334) | def update(self, *, epk, loss):
    method evaluate (line 338) | def evaluate(self, discr, dname, dpart, loader, llh_mode, i = None):
    method summary (line 351) | def summary(self, dname, dpart, i = None):
  function dc_state_dict (line 367) | def dc_state_dict(dc_vars, *name_list):
  function is_ood (line 372) | def is_ood(mode):
  function process_continue_run (line 375) | def process_continue_run(ag):
  function main_stem (line 387) | def main_stem(ag, ckpt, archtype, shape_x, dim_y,
  function get_parser (line 524) | def get_parser():
  function print_infrstru_info (line 597) | def print_infrstru_info():

FILE: code/deep/DAAN/data_loader.py
  class ResizeImage (line 8) | class ResizeImage():
    method __init__ (line 9) | def __init__(self, size):
    method __call__ (line 14) | def __call__(self, img):
  class PlaceCrop (line 18) | class PlaceCrop(object):
    method __init__ (line 20) | def __init__(self, size, start_x, start_y):
    method __call__ (line 28) | def __call__(self, img):
  function load_training (line 32) | def load_training(root_path, dir, batch_size, kwargs):
  function load_testing (line 43) | def load_testing(root_path, dir, batch_size, kwargs):

FILE: code/deep/DAAN/functions.py
  class ReverseLayerF (line 3) | class ReverseLayerF(Function):
    method forward (line 6) | def forward(ctx, x, alpha):
    method backward (line 12) | def backward(ctx, grad_output):

FILE: code/deep/DAAN/model/DAAN.py
  class DAANNet (line 9) | class DAANNet(nn.Module):
    method __init__ (line 11) | def __init__(self, num_classes=65, base_net='ResNet50'):
    method forward (line 42) | def forward(self, source, target, s_label, DEV, alpha=0.0):

FILE: code/deep/DAAN/model/backbone.py
  class AlexNetFc (line 10) | class AlexNetFc(nn.Module):
    method __init__ (line 11) | def __init__(self):
    method forward (line 19) | def forward(self, x):
  class ResNet18Fc (line 26) | class ResNet18Fc(nn.Module):
    method __init__ (line 27) | def __init__(self):
    method forward (line 40) | def forward(self, x):
  class ResNet34Fc (line 53) | class ResNet34Fc(nn.Module):
    method __init__ (line 54) | def __init__(self):
    method forward (line 67) | def forward(self, x):
  class ResNet50Fc (line 80) | class ResNet50Fc(nn.Module):
    method __init__ (line 81) | def __init__(self):
    method forward (line 94) | def forward(self, x):
  class ResNet101Fc (line 107) | class ResNet101Fc(nn.Module):
    method __init__ (line 108) | def __init__(self):
    method forward (line 121) | def forward(self, x):
  class ResNet152Fc (line 134) | class ResNet152Fc(nn.Module):
    method __init__ (line 135) | def __init__(self):
    method forward (line 148) | def forward(self, x):

FILE: code/deep/DAAN/train.py
  function load_data (line 56) | def load_data():
  function print_learning_rate (line 62) | def print_learning_rate(optimizer):
  function train (line 73) | def train(epoch, model, source_loader, target_loader):
  function test (line 161) | def test(model, test_loader):

FILE: code/deep/DANN(RevGrad)/adv_layer.py
  class ReverseLayerF (line 7) | class ReverseLayerF(Function):
    method forward (line 10) | def forward(ctx, x, alpha):
    method backward (line 15) | def backward(ctx, grad_output):
  class Discriminator (line 19) | class Discriminator(nn.Module):
    method __init__ (line 20) | def __init__(self, input_dim=256, hidden_dim=256):
    method forward (line 29) | def forward(self, x):

FILE: code/deep/DaNN/DaNN.py
  class DaNN (line 4) | class DaNN(nn.Module):
    method __init__ (line 5) | def __init__(self, n_input=28 * 28, n_hidden=256, n_class=10):
    method forward (line 12) | def forward(self, src, tar):

FILE: code/deep/DaNN/data_loader.py
  function load_data (line 5) | def load_data(root_dir,domain,batch_size):
  function load_test (line 21) | def load_test(root_dir,domain,batch_size):

FILE: code/deep/DaNN/main.py
  function mmd_loss (line 26) | def mmd_loss(x_src, x_tar):
  function train (line 30) | def train(model, optimizer, epoch, data_src, data_tar):
  function test (line 69) | def test(model, data_tar, e):

FILE: code/deep/DaNN/mmd.py
  function linear_mmd2 (line 17) | def linear_mmd2(f_of_X, f_of_Y):
  function poly_mmd2 (line 32) | def poly_mmd2(f_of_X, f_of_Y, d=1, alpha=1.0, c=2.0):
  function _mix_rbf_kernel (line 49) | def _mix_rbf_kernel(X, Y, sigma_list):
  function mix_rbf_mmd2 (line 67) | def mix_rbf_mmd2(X, Y, sigma_list, biased=True):
  function mix_rbf_mmd2_and_ratio (line 73) | def mix_rbf_mmd2_and_ratio(X, Y, sigma_list, biased=True):
  function _mmd2 (line 84) | def _mmd2(K_XX, K_XY, K_YY, const_diagonal=False, biased=False):
  function _mmd2_and_ratio (line 118) | def _mmd2_and_ratio(K_XX, K_XY, K_YY, const_diagonal=False, biased=False):
  function _mmd2_and_variance (line 124) | def _mmd2_and_variance(K_XX, K_XY, K_YY, const_diagonal=False, biased=Fa...

FILE: code/deep/DeepMEDA/ResNet.py
  function conv3x3 (line 18) | def conv3x3(in_planes, out_planes, stride=1):
  class BasicBlock (line 24) | class BasicBlock(nn.Module):
    method __init__ (line 27) | def __init__(self, inplanes, planes, stride=1, downsample=None):
    method forward (line 37) | def forward(self, x):
  class Bottleneck (line 56) | class Bottleneck(nn.Module):
    method __init__ (line 59) | def __init__(self, inplanes, planes, stride=1, downsample=None):
    method forward (line 72) | def forward(self, x):
  class ResNet (line 95) | class ResNet(nn.Module):
    method __init__ (line 97) | def __init__(self, block, layers, num_classes=1000):
    method _make_layer (line 121) | def _make_layer(self, block, planes, blocks, stride=1):
    method forward (line 138) | def forward(self, x):
  function resnet50 (line 154) | def resnet50(pretrained=False, **kwargs):

FILE: code/deep/DeepMEDA/Weight.py
  function convert_to_onehot (line 3) | def convert_to_onehot(sca_label, class_num=31):
  class Weight (line 6) | class Weight:
    method cal_weight (line 9) | def cal_weight(s_label, t_label, type='visual', batch_size=32, class_n...

FILE: code/deep/DeepMEDA/data_loader.py
  function load_training (line 5) | def load_training(root_path, dir, batch_size, kwargs):
  function load_testing (line 15) | def load_testing(root_path, dir, batch_size, kwargs):

FILE: code/deep/DeepMEDA/deep_meda.py
  class DeepMEDA (line 7) | class DeepMEDA(nn.Module):
    method __init__ (line 9) | def __init__(self, num_classes=31, bottle_neck=True):
    method forward (line 21) | def forward(self, source, target, s_label):
    method predict (line 35) | def predict(self, x):

FILE: code/deep/DeepMEDA/dynamic_factor.py
  function proxy_a_distance (line 4) | def proxy_a_distance(source_X, target_X, verbose=False):
  function estimate_mu (line 42) | def estimate_mu(_X1, _Y1, _X2, _Y2):

FILE: code/deep/DeepMEDA/main.py
  function load_data (line 12) | def load_data(root_path, src, tar, batch_size):
  function train_epoch (line 21) | def train_epoch(epoch, model, dataloaders):
  function test (line 66) | def test(model, dataloader):
  function get_args (line 85) | def get_args():

FILE: code/deep/DeepMEDA/mmd.py
  class MMD_loss (line 7) | class MMD_loss(nn.Module):
    method __init__ (line 8) | def __init__(self, kernel_type='rbf', kernel_mul=2.0, kernel_num=5):
    method guassian_kernel (line 15) | def guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5...
    method linear_mmd2 (line 34) | def linear_mmd2(self, f_of_X, f_of_Y):
    method marginal (line 40) | def marginal(self, source, target):
    method conditional (line 54) | def conditional(self, source, target, s_label, t_label, kernel_mul=2.0...

FILE: code/deep/MRAN/MRAN.py
  function load_data (line 55) | def load_data():
  function train (line 62) | def train(epoch, model, source_loader, target_loader):
  function test (line 102) | def test(model, test_loader):

FILE: code/deep/MRAN/ResNet.py
  function conv3x3 (line 18) | def conv3x3(in_planes, out_planes, stride=1):
  class BasicBlock (line 24) | class BasicBlock(nn.Module):
    method __init__ (line 27) | def __init__(self, inplanes, planes, stride=1, downsample=None):
    method forward (line 37) | def forward(self, x):
  class Bottleneck (line 56) | class Bottleneck(nn.Module):
    method __init__ (line 59) | def __init__(self, inplanes, planes, stride=1, downsample=None):
    method forward (line 72) | def forward(self, x):
  class ResNet (line 95) | class ResNet(nn.Module):
    method __init__ (line 97) | def __init__(self, block, layers, num_classes=1000):
    method _make_layer (line 121) | def _make_layer(self, block, planes, blocks, stride=1):
    method forward (line 138) | def forward(self, x):
  class MRANNet (line 151) | class MRANNet(nn.Module):
    method __init__ (line 153) | def __init__(self, num_classes=31):
    method forward (line 158) | def forward(self, source, target, s_label):
  class BasicConv2d (line 165) | class BasicConv2d(nn.Module):
    method __init__ (line 167) | def __init__(self, in_channels, out_channels, **kwargs):
    method forward (line 172) | def forward(self, x):
  class InceptionA (line 177) | class InceptionA(nn.Module):
    method __init__ (line 179) | def __init__(self, in_channels, pool_features, num_classes):
    method forward (line 196) | def forward(self, source, target, s_label):
  function resnet50 (line 257) | def resnet50(pretrained=False, **kwargs):

FILE: code/deep/MRAN/data_loader.py
  function load_training (line 4) | def load_training(root_path, dir, batch_size, kwargs):
  function load_testing (line 14) | def load_testing(root_path, dir, batch_size, kwargs):

FILE: code/deep/MRAN/mmd.py
  function guassian_kernel (line 10) | def guassian_kernel(source, target, kernel_mul=2.0, kernel_num=5, fix_si...
  function cmmd (line 25) | def cmmd(source, target, s_label, t_label, kernel_mul=2.0, kernel_num=5,...

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/my_neuron_coverage.py
  class MyNeuronCoverage (line 14) | class MyNeuronCoverage:
    method __init__ (line 28) | def __init__(self, threshold=0.5):
    method init_structure (line 40) | def init_structure(self, intermediate_layer_outputs, features_index):
    method update (line 55) | def update(self, intermediate_layer_outputs, features_index):
    method report (line 111) | def report(self, *args):
    method get (line 132) | def get(self, threshold):
    method _scale (line 157) | def _scale(intermediate_layer_output):
    method _calc_1 (line 182) | def _calc_1(intermediate_layer_output, features_index, threshold):
    method _calc_2 (line 224) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/neuron_coverage.py
  class NeuronCoverage (line 13) | class NeuronCoverage:
    method __init__ (line 27) | def __init__(self, thresholds=(0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,...
    method update (line 36) | def update(self, intermediate_layer_outputs, features_index):
    method report (line 102) | def report(self, *args):
    method get (line 117) | def get(self, threshold):
    method _scale (line 139) | def _scale(intermediate_layer_output):
    method _calc_1 (line 164) | def _calc_1(intermediate_layer_output, features_index):
    method _calc_2 (line 203) | def _calc_2(intermediate_layer_output, features_index):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/pytorch_wrapper.py
  class PyTorchModel (line 14) | class PyTorchModel:
    method __init__ (line 33) | def __init__(self, model, intermedia_mode=""):
    method predict (line 41) | def predict(self, dataset, callbacks, batch_size=16):
    method one_sample_intermediate_layer_outputs (line 115) | def one_sample_intermediate_layer_outputs(self, sample, callbacks, ):
    method one_sample_intermediate_layer_outputs_with_grad (line 136) | def one_sample_intermediate_layer_outputs_with_grad(self, sample, call...
    method _intermediate_layers (line 172) | def _intermediate_layers(self, module, pre_name=""):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/strong_neuron_activation_coverage.py
  class StrongNeuronActivationCoverage (line 14) | class StrongNeuronActivationCoverage(MyNeuronCoverage):
    method __init__ (line 15) | def __init__(self, k=5):
    method _calc_1 (line 24) | def _calc_1(intermediate_layer_output, features_index, k):
    method _calc_2 (line 73) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/top_k_coverage.py
  class TopKNeuronCoverage (line 14) | class TopKNeuronCoverage(MyNeuronCoverage):
    method __init__ (line 15) | def __init__(self, k=5):
    method _calc_1 (line 24) | def _calc_1(intermediate_layer_output, features_index, threshold):
    method _calc_2 (line 72) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/common.py
  function readable_time_str (line 13) | def readable_time_str():
  function user_home_dir (line 27) | def user_home_dir():
  function to_numpy (line 39) | def to_numpy(data):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/keras.py
  class ImageNetValData (line 16) | class ImageNetValData():
    class ImageNetValDataX (line 43) | class ImageNetValDataX():
      method __init__ (line 45) | def __init__(self, dir, filenames, fashion, size, transform):
      method __len__ (line 52) | def __len__(self):
      method __getitem__ (line 55) | def __getitem__(self, index):
    method __init__ (line 91) | def __init__(self, fashion, size, transform=None, shuffle=False, seed=...
    method __len__ (line 112) | def __len__(self):
    method filenames (line 116) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 120) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 137) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_data (line 343) | def cifar10_data(train=False, num_max=None):
  function mnist_data (line 374) | def mnist_data(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/mxnet.py
  class ImageNetValDataset (line 16) | class ImageNetValDataset(mxnet.gluon.data.Dataset):
    method __init__ (line 45) | def __init__(self, resize_size, center_crop_size, preprocess, shuffle=...
    method __len__ (line 67) | def __len__(self):
    method __getitem__ (line 70) | def __getitem__(self, index):
    method filenames (line 82) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 86) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 104) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_dataset (line 228) | def cifar10_dataset(train=False, num_max=None):
  function mnist_dataset (line 251) | def mnist_dataset(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/pytorch.py
  class ImageNetValDataset (line 17) | class ImageNetValDataset(torch.utils.data.Dataset):
    method __init__ (line 46) | def __init__(self, resize_size, center_crop_size, preprocess, shuffle=...
    method __len__ (line 68) | def __len__(self):
    method __getitem__ (line 71) | def __getitem__(self, index):
    method filenames (line 82) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 86) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 104) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_dataset (line 228) | def cifar10_dataset(train=False, num_max=None):
  function mnist_dataset (line 253) | def mnist_dataset(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_adv/DNNtest/coverage/utils/tensorflow.py
  class ImageNetValData (line 22) | class ImageNetValData():
    class ImageNetValDataX (line 54) | class ImageNetValDataX():
      method __init__ (line 56) | def __init__(self, dir, filenames, width, height, fashion, transform):
      method __len__ (line 64) | def __len__(self):
      method __getitem__ (line 67) | def __getitem__(self, index):
      method _smallest_size_at_least (line 98) | def _smallest_size_at_least(self, height, width, smallest_side):
      method _aspect_preserving_resize (line 108) | def _aspect_preserving_resize(self, image, smallest_side):
      method _central_crop (line 120) | def _central_crop(self, image_list, crop_height, crop_width):
      method _crop (line 130) | def _crop(self, image, offset_height, offset_width, crop_height, cro...
    method __init__ (line 142) | def __init__(self, width, height, fashion, transform=None, label_offse...
    method __len__ (line 163) | def __len__(self):
    method filenames (line 167) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 171) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 191) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_test_data (line 535) | def cifar10_test_data(num_max=None):
  function mnist_test_data (line 557) | def mnist_test_data(num_max=None):

FILE: code/deep/ReMoS/CV_adv/DNNtest/eval_nc.py
  function adv_whitebox (line 47) | def adv_whitebox(model, loader, args, bounds,):
  function adv_teacher_plain (line 111) | def adv_teacher_plain(model, loader, args, bounds, students):
  function get_strategy (line 192) | def get_strategy(args, model):
  function get_coverage (line 205) | def get_coverage(args,):
  function compute_selected_neuron_value (line 216) | def compute_selected_neuron_value(
  function adv_coverage (line 235) | def adv_coverage(
  function get_args (line 345) | def get_args():
  function load_student (line 386) | def load_student(ckpt, args, num_classes):

FILE: code/deep/ReMoS/CV_adv/DNNtest/strategy/adapt.py
  class FeatureMatrix (line 18) | class FeatureMatrix:
    method __init__ (line 38) | def __init__(self, network):
    method update (line 131) | def update(self, covered_count, objective_covered):
    method matrix (line 191) | def matrix(self):
    method dot (line 196) | def dot(self, vector):
  class ParameterizedStrategy (line 208) | class ParameterizedStrategy(Strategy):
    method __init__ (line 219) | def __init__(self, network, bound=5):
    method select (line 251) | def select(self, k):
    method init (line 268) | def init(self, covered, label, **kwargs):
    method update (line 303) | def update(self, covered, label, **kwargs):
  class AdaptiveParameterizedStrategy (line 328) | class AdaptiveParameterizedStrategy(ParameterizedStrategy):
    method __init__ (line 340) | def __init__(self, network, bound=5, size=100, history=300, remainder=...
    method init (line 386) | def init(self, covered, label, **kwargs):
    method update (line 407) | def update(self, covered, label, **kwargs):
    method next (line 427) | def next(self):

FILE: code/deep/ReMoS/CV_adv/DNNtest/strategy/deepxplore.py
  class UncoveredRandomStrategy (line 6) | class UncoveredRandomStrategy(Strategy):
    method __init__ (line 17) | def __init__(self, network):
    method select (line 38) | def select(self, k):
    method init (line 66) | def init(self, covered, **kwargs):
    method update (line 89) | def update(self, covered, **kwargs):

FILE: code/deep/ReMoS/CV_adv/DNNtest/strategy/dlfuzz.py
  class DLFuzzRoundRobin (line 7) | class DLFuzzRoundRobin(Strategy):
    method __init__ (line 23) | def __init__(self, network, weight_portion=0.1, order=None):
    method select (line 81) | def select(self, k):
    method init (line 126) | def init(self, covered, **kwargs):
    method update (line 149) | def update(self, covered, **kwargs):
    method next (line 166) | def next(self):
  class MostCoveredStrategy (line 178) | class MostCoveredStrategy(DLFuzzRoundRobin):
    method __init__ (line 191) | def __init__(self, model):
    method next (line 218) | def next(self):

FILE: code/deep/ReMoS/CV_adv/DNNtest/strategy/random.py
  class RandomStrategy (line 6) | class RandomStrategy(Strategy):
    method init (line 13) | def init(self, covered):
    method select (line 17) | def select(self, k, ):

FILE: code/deep/ReMoS/CV_adv/DNNtest/strategy/strategy.py
  class Strategy (line 4) | class Strategy(ABC):
    method __init__ (line 7) | def __init__(self, network):
    method __call__ (line 18) | def __call__(self, k):
    method select (line 27) | def select(self, k):
    method init (line 41) | def init(self, **kwargs):
    method update (line 60) | def update(self, covered, **kwargs):
    method next (line 79) | def next(self):

FILE: code/deep/ReMoS/CV_adv/dataset/cub200.py
  class CUB200Data (line 8) | class CUB200Data(data.Dataset):
    method __init__ (line 9) | def __init__(self, root, is_train=True, transform=None, shots=-1, seed...
    method __getitem__ (line 71) | def __getitem__(self, index):
    method __len__ (line 85) | def __len__(self):

FILE: code/deep/ReMoS/CV_adv/dataset/flower102.py
  class Flower102Data (line 10) | class Flower102Data(data.Dataset):
    method __init__ (line 11) | def __init__(self, root, is_train=True, transform=None, shots=-1, seed...
    method __getitem__ (line 62) | def __getitem__(self, index):
    method __len__ (line 73) | def __len__(self):

FILE: code/deep/ReMoS/CV_adv/dataset/mit67.py
  class MIT67Data (line 11) | class MIT67Data(data.Dataset):
    method __init__ (line 12) | def __init__(self, root, is_train=False, transform=None, shots=-1, see...
    method __getitem__ (line 63) | def __getitem__(self, index):
    method __len__ (line 74) | def __len__(self):

FILE: code/deep/ReMoS/CV_adv/dataset/stanford_40.py
  class Stanford40Data (line 10) | class Stanford40Data(data.Dataset):
    method __init__ (line 11) | def __init__(self, root, is_train=False, transform=None, shots=-1, see...
    method __getitem__ (line 63) | def __getitem__(self, index):
    method __len__ (line 74) | def __len__(self):

FILE: code/deep/ReMoS/CV_adv/dataset/stanford_dog.py
  class SDog120Data (line 11) | class SDog120Data(data.Dataset):
    method __init__ (line 12) | def __init__(self, root, is_train=True, transform=None, shots=5, seed=...
    method __getitem__ (line 62) | def __getitem__(self, index):
    method __len__ (line 72) | def __len__(self):

FILE: code/deep/ReMoS/CV_adv/distillation_training.py
  function linear_l2 (line 35) | def linear_l2(model):
  function l2sp (line 43) | def l2sp(model, reg):
  function test (line 64) | def test(model, teacher, loader, loss=False):
  function train (line 109) | def train(
  function get_args (line 348) | def get_args():
  function record_act (line 389) | def record_act(self, input, output):

FILE: code/deep/ReMoS/CV_adv/eval_robustness.py
  function advtest (line 8) | def advtest(model, loader, adversary, args):
  function record_act (line 45) | def record_act(self, input, output):
  function get_args (line 48) | def get_args():
  function myloss (line 66) | def myloss(yhat, y):

FILE: code/deep/ReMoS/CV_adv/finetune.py
  function get_args (line 28) | def get_args():

FILE: code/deep/ReMoS/CV_adv/finetuner.py
  class Finetuner (line 20) | class Finetuner(object):
    method __init__ (line 21) | def __init__(
    method init_models (line 37) | def init_models(self):
    method adv_eval (line 59) | def adv_eval(self):
    method compute_loss (line 67) | def compute_loss(
    method test (line 78) | def test(self, loader):
    method train (line 97) | def train(self):

FILE: code/deep/ReMoS/CV_adv/model/fe_resnet.py
  function conv3x3 (line 24) | def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
  function conv1x1 (line 30) | def conv1x1(in_planes, out_planes, stride=1):
  class BasicBlock (line 35) | class BasicBlock(nn.Module):
    method __init__ (line 39) | def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
    method forward (line 58) | def forward(self, x):
  class Bottleneck (line 78) | class Bottleneck(nn.Module):
    method __init__ (line 82) | def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
    method forward (line 99) | def forward(self, x):
  class ResNet (line 122) | class ResNet(nn.Module):
    method __init__ (line 124) | def __init__(self, block, layers, num_classes=1000, zero_init_residual...
    method _make_layer (line 183) | def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
    method _forward_impl (line 207) | def _forward_impl(self, x):
    method forward (line 233) | def forward(self, x):
  function _resnet (line 236) | def _resnet(arch, block, layers, pretrained, progress, **kwargs):
  function resnet18_dropout (line 250) | def resnet18_dropout(pretrained=False, progress=True, **kwargs):
  function resnet34_dropout (line 260) | def resnet34_dropout(pretrained=False, progress=True, **kwargs):
  function resnet50_dropout (line 270) | def resnet50_dropout(pretrained=False, progress=True, **kwargs):
  function resnet101_dropout (line 281) | def resnet101_dropout(pretrained=False, progress=True, **kwargs):
  function feresnet18 (line 291) | def feresnet18(pretrained=False, progress=True, **kwargs):
  function feresnet34 (line 302) | def feresnet34(pretrained=False, progress=True, **kwargs):
  function feresnet50 (line 313) | def feresnet50(pretrained=False, progress=True, **kwargs):
  function feresnet101 (line 324) | def feresnet101(pretrained=False, progress=True, **kwargs):
  function feresnet152 (line 335) | def feresnet152(pretrained=False, progress=True, **kwargs):
  function resnext50_32x4d (line 346) | def resnext50_32x4d(pretrained=False, progress=True, **kwargs):
  function resnext101_32x8d (line 359) | def resnext101_32x8d(pretrained=False, progress=True, **kwargs):
  function wide_resnet50_2 (line 372) | def wide_resnet50_2(pretrained=False, progress=True, **kwargs):
  function wide_resnet101_2 (line 388) | def wide_resnet101_2(pretrained=False, progress=True, **kwargs):

FILE: code/deep/ReMoS/CV_adv/nc_prune/analyze_coverage.py
  function get_args (line 37) | def get_args():

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/my_neuron_coverage.py
  class MyNeuronCoverage (line 14) | class MyNeuronCoverage:
    method __init__ (line 28) | def __init__(self, threshold=0.5):
    method init_structure (line 40) | def init_structure(self, intermediate_layer_outputs, features_index):
    method update (line 55) | def update(
    method report (line 140) | def report(self, *args):
    method get (line 161) | def get(self, ):
    method _scale (line 186) | def _scale(intermediate_layer_output):
    method _calc_1 (line 211) | def _calc_1(intermediate_layer_output, features_index, threshold):
    method _calc_2 (line 253) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/pytorch_wrapper.py
  class PyTorchModel (line 15) | class PyTorchModel:
    method __init__ (line 34) | def __init__(self, model, intermedia_mode=""):
    method predict (line 44) | def predict(self, dataset, callbacks, batch_size=16):
    method one_sample_intermediate_layer_outputs (line 74) | def one_sample_intermediate_layer_outputs(self, sample, callbacks, ):
    method _intermediate_layers (line 101) | def _intermediate_layers(self, module, pre_name=""):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/strong_neuron_activation_coverage.py
  class StrongNeuronActivationCoverage (line 14) | class StrongNeuronActivationCoverage(MyNeuronCoverage):
    method __init__ (line 15) | def __init__(self, k=5):
    method _calc_1 (line 24) | def _calc_1(intermediate_layer_output, features_index, k):
    method _calc_2 (line 73) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/test_max.py
  function get_max_values (line 5) | def get_max_values(array, k):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/top_k_coverage.py
  class TopKNeuronCoverage (line 14) | class TopKNeuronCoverage(MyNeuronCoverage):
    method __init__ (line 15) | def __init__(self, k=5):
    method _calc_1 (line 24) | def _calc_1(intermediate_layer_output, features_index, threshold):
    method _calc_2 (line 72) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/common.py
  function readable_time_str (line 13) | def readable_time_str():
  function user_home_dir (line 27) | def user_home_dir():
  function to_numpy (line 39) | def to_numpy(data):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/keras.py
  class ImageNetValData (line 16) | class ImageNetValData():
    class ImageNetValDataX (line 43) | class ImageNetValDataX():
      method __init__ (line 45) | def __init__(self, dir, filenames, fashion, size, transform):
      method __len__ (line 52) | def __len__(self):
      method __getitem__ (line 55) | def __getitem__(self, index):
    method __init__ (line 91) | def __init__(self, fashion, size, transform=None, shuffle=False, seed=...
    method __len__ (line 112) | def __len__(self):
    method filenames (line 116) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 120) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 137) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_data (line 343) | def cifar10_data(train=False, num_max=None):
  function mnist_data (line 374) | def mnist_data(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/mxnet.py
  class ImageNetValDataset (line 16) | class ImageNetValDataset(mxnet.gluon.data.Dataset):
    method __init__ (line 45) | def __init__(self, resize_size, center_crop_size, preprocess, shuffle=...
    method __len__ (line 67) | def __len__(self):
    method __getitem__ (line 70) | def __getitem__(self, index):
    method filenames (line 82) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 86) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 104) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_dataset (line 228) | def cifar10_dataset(train=False, num_max=None):
  function mnist_dataset (line 251) | def mnist_dataset(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/pytorch.py
  class ImageNetValDataset (line 17) | class ImageNetValDataset(torch.utils.data.Dataset):
    method __init__ (line 46) | def __init__(self, resize_size, center_crop_size, preprocess, shuffle=...
    method __len__ (line 68) | def __len__(self):
    method __getitem__ (line 71) | def __getitem__(self, index):
    method filenames (line 82) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 86) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 104) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_dataset (line 228) | def cifar10_dataset(train=False, num_max=None):
  function mnist_dataset (line 253) | def mnist_dataset(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_adv/nc_prune/coverage/utils/tensorflow.py
  class ImageNetValData (line 22) | class ImageNetValData():
    class ImageNetValDataX (line 54) | class ImageNetValDataX():
      method __init__ (line 56) | def __init__(self, dir, filenames, width, height, fashion, transform):
      method __len__ (line 64) | def __len__(self):
      method __getitem__ (line 67) | def __getitem__(self, index):
      method _smallest_size_at_least (line 98) | def _smallest_size_at_least(self, height, width, smallest_side):
      method _aspect_preserving_resize (line 108) | def _aspect_preserving_resize(self, image, smallest_side):
      method _central_crop (line 120) | def _central_crop(self, image_list, crop_height, crop_width):
      method _crop (line 130) | def _crop(self, image, offset_height, offset_width, crop_height, cro...
    method __init__ (line 142) | def __init__(self, width, height, fashion, transform=None, label_offse...
    method __len__ (line 163) | def __len__(self):
    method filenames (line 167) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 171) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 191) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_test_data (line 535) | def cifar10_test_data(num_max=None):
  function mnist_test_data (line 557) | def mnist_test_data(num_max=None):

FILE: code/deep/ReMoS/CV_adv/nc_prune/my_profile.py
  function get_coverage (line 38) | def get_coverage(args,):
  function compute_selected_neuron_value (line 49) | def compute_selected_neuron_value(
  function log_coverage (line 68) | def log_coverage(model, loader, args, ):
  function record_act (line 107) | def record_act(self, input, output):
  function get_args (line 110) | def get_args():

FILE: code/deep/ReMoS/CV_adv/nc_prune/nc_pruner.py
  class NCPruner (line 33) | class NCPruner(WeightPruner):
    method __init__ (line 34) | def __init__(
    method prune_record (line 46) | def prune_record(self, log):
    method init_prune (line 50) | def init_prune(self):
    method check_param_num (line 57) | def check_param_num(self):
    method load_nc_info (line 68) | def load_nc_info(self,):
    method weight_prune (line 80) | def weight_prune(
    method final_check_param_num (line 157) | def final_check_param_num(self):

FILE: code/deep/ReMoS/CV_adv/nc_prune/nc_weight_rank_pruner.py
  class NCWeightRankPruner (line 35) | class NCWeightRankPruner(NCPruner):
    method __init__ (line 36) | def __init__(
    method load_nc_info (line 48) | def load_nc_info(self,):
    method weight_prune (line 60) | def weight_prune(
    method final_check_param_num (line 170) | def final_check_param_num(self):

FILE: code/deep/ReMoS/CV_adv/utils.py
  class MovingAverageMeter (line 6) | class MovingAverageMeter(object):
    method __init__ (line 9) | def __init__(self, name, fmt=':f', momentum=0.9):
    method reset (line 15) | def reset(self):
    method update (line 20) | def update(self, val, n=1):
    method __str__ (line 24) | def __str__(self):
  class ProgressMeter (line 29) | class ProgressMeter(object):
    method __init__ (line 30) | def __init__(self, num_batches, meters, prefix="", output_dir=None):
    method display (line 37) | def display(self, batch):
    method _get_batch_fmtstr (line 46) | def _get_batch_fmtstr(self, num_batches):
  class CrossEntropyLabelSmooth (line 52) | class CrossEntropyLabelSmooth(nn.Module):
    method __init__ (line 53) | def __init__(self, num_classes, epsilon=0.1):
    method forward (line 59) | def forward(self, inputs, targets):
  function linear_l2 (line 69) | def linear_l2(model, beta_lmda):
  function l2sp (line 78) | def l2sp(model, reg):
  function advtest_fast (line 99) | def advtest_fast(model, loader, adversary, args):

FILE: code/deep/ReMoS/CV_adv/weight_pruner.py
  class WeightPruner (line 34) | class WeightPruner(Finetuner):
    method __init__ (line 35) | def __init__(
    method prune_record (line 58) | def prune_record(self, log):
    method init_prune (line 62) | def init_prune(self):
    method iterative_prune (line 69) | def iterative_prune(self, iteration):
    method check_param_num (line 89) | def check_param_num(self):
    method weight_prune (line 101) | def weight_prune(
    method final_check_param_num (line 166) | def final_check_param_num(self):

FILE: code/deep/ReMoS/CV_backdoor/attack_finetuner.py
  class AttackFinetuner (line 29) | class AttackFinetuner(object):
    method __init__ (line 30) | def __init__(
    method init_models (line 46) | def init_models(self):
    method adv_eval (line 70) | def adv_eval(self):
    method compute_loss (line 92) | def compute_loss(self, batch, label, ce
    method test (line 106) | def test(self, ):
    method train (line 129) | def train(self, ):

FILE: code/deep/ReMoS/CV_backdoor/backdoor_dataset/cub200.py
  function addtrigger (line 10) | def addtrigger(img, firefox, fixed_pic):
  function add4trig (line 21) | def add4trig(img, firefox):
  class CUB200Data (line 32) | class CUB200Data(data.Dataset):
    method __init__ (line 33) | def __init__(self, root, is_train=True, transform=None, shots=-1, seed...
    method __getitem__ (line 108) | def __getitem__(self, index):
    method __len__ (line 139) | def __len__(self):

FILE: code/deep/ReMoS/CV_backdoor/backdoor_dataset/mit67.py
  function addtrigger (line 12) | def addtrigger(img, firefox, fixed_pic):
  function add4trig (line 23) | def add4trig(img, firefox):
  class MIT67Data (line 34) | class MIT67Data(data.Dataset):
    method __init__ (line 35) | def __init__(self, root, is_train=False, transform=None, shots=-1, see...
    method __getitem__ (line 98) | def __getitem__(self, index):
    method __len__ (line 128) | def __len__(self):

FILE: code/deep/ReMoS/CV_backdoor/backdoor_dataset/stanford_40.py
  function addtrigger (line 11) | def addtrigger(img, firefox, fixed_pic):
  function add4trig (line 22) | def add4trig(img, firefox):
  class Stanford40Data (line 33) | class Stanford40Data(data.Dataset):
    method __init__ (line 34) | def __init__(self, root, is_train=False, transform=None, shots=-1, see...
    method __getitem__ (line 94) | def __getitem__(self, index):
    method __len__ (line 123) | def __len__(self):

FILE: code/deep/ReMoS/CV_backdoor/clean_dataset/cub200.py
  class CUB200Data (line 8) | class CUB200Data(data.Dataset):
    method __init__ (line 9) | def __init__(self, root, is_train=True, transform=None, shots=-1, seed...
    method __getitem__ (line 71) | def __getitem__(self, index):
    method __len__ (line 85) | def __len__(self):

FILE: code/deep/ReMoS/CV_backdoor/clean_dataset/mit67.py
  class MIT67Data (line 11) | class MIT67Data(data.Dataset):
    method __init__ (line 12) | def __init__(self, root, is_train=False, transform=None, shots=-1, see...
    method __getitem__ (line 63) | def __getitem__(self, index):
    method __len__ (line 74) | def __len__(self):

FILE: code/deep/ReMoS/CV_backdoor/clean_dataset/stanford_40.py
  class Stanford40Data (line 10) | class Stanford40Data(data.Dataset):
    method __init__ (line 11) | def __init__(self, root, is_train=False, transform=None, shots=-1, see...
    method __getitem__ (line 63) | def __getitem__(self, index):
    method __len__ (line 74) | def __len__(self):

FILE: code/deep/ReMoS/CV_backdoor/eval.py
  function get_args (line 47) | def get_args():
  function target_test (line 71) | def target_test(model, loader):
  function clean_test (line 95) | def clean_test(model, loader):
  function untarget_test (line 116) | def untarget_test(model, loader):
  function testing (line 140) | def testing(model, test_loader):
  function generate_dataloader (line 180) | def generate_dataloader(args, normalize, seed):

FILE: code/deep/ReMoS/CV_backdoor/eval_robustness.py
  function advtest (line 21) | def advtest(model, loader, adversary, args):
  function record_act (line 66) | def record_act(self, input, output):
  function get_args (line 69) | def get_args():
  function myloss (line 87) | def myloss(yhat, y):

FILE: code/deep/ReMoS/CV_backdoor/finetune.py
  function get_args (line 43) | def get_args():
  function target_test (line 113) | def target_test(model, loader):
  function clean_test (line 137) | def clean_test(model, loader):
  function untarget_test (line 158) | def untarget_test(model, loader):
  function testing (line 182) | def testing(model, test_loader):
  function generate_dataloader (line 222) | def generate_dataloader(args, normalize, seed):

FILE: code/deep/ReMoS/CV_backdoor/finetuner.py
  class Finetuner (line 26) | class Finetuner(object):
    method __init__ (line 27) | def __init__(
    method init_models (line 44) | def init_models(self):
    method adv_eval (line 68) | def adv_eval(self):
    method compute_loss (line 77) | def compute_loss(
    method test (line 92) | def test(self, ):
    method train (line 115) | def train(self, ):

FILE: code/deep/ReMoS/CV_backdoor/model/fe_resnet.py
  function conv3x3 (line 24) | def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
  function conv1x1 (line 30) | def conv1x1(in_planes, out_planes, stride=1):
  class BasicBlock (line 35) | class BasicBlock(nn.Module):
    method __init__ (line 39) | def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
    method forward (line 58) | def forward(self, x):
  class Bottleneck (line 78) | class Bottleneck(nn.Module):
    method __init__ (line 82) | def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
    method forward (line 99) | def forward(self, x):
  class ResNet (line 122) | class ResNet(nn.Module):
    method __init__ (line 124) | def __init__(self, block, layers, num_classes=1000, zero_init_residual...
    method _make_layer (line 183) | def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
    method _forward_impl (line 207) | def _forward_impl(self, x):
    method forward (line 233) | def forward(self, x):
  function _resnet (line 236) | def _resnet(arch, block, layers, pretrained, progress, **kwargs):
  function resnet18_dropout (line 250) | def resnet18_dropout(pretrained=False, progress=True, **kwargs):
  function resnet34_dropout (line 260) | def resnet34_dropout(pretrained=False, progress=True, **kwargs):
  function resnet50_dropout (line 270) | def resnet50_dropout(pretrained=False, progress=True, **kwargs):
  function resnet101_dropout (line 281) | def resnet101_dropout(pretrained=False, progress=True, **kwargs):
  function feresnet18 (line 291) | def feresnet18(pretrained=False, progress=True, **kwargs):
  function feresnet34 (line 302) | def feresnet34(pretrained=False, progress=True, **kwargs):
  function feresnet50 (line 313) | def feresnet50(pretrained=False, progress=True, **kwargs):
  function feresnet101 (line 324) | def feresnet101(pretrained=False, progress=True, **kwargs):
  function feresnet152 (line 335) | def feresnet152(pretrained=False, progress=True, **kwargs):
  function resnext50_32x4d (line 346) | def resnext50_32x4d(pretrained=False, progress=True, **kwargs):
  function resnext101_32x8d (line 359) | def resnext101_32x8d(pretrained=False, progress=True, **kwargs):
  function wide_resnet50_2 (line 372) | def wide_resnet50_2(pretrained=False, progress=True, **kwargs):
  function wide_resnet101_2 (line 388) | def wide_resnet101_2(pretrained=False, progress=True, **kwargs):

FILE: code/deep/ReMoS/CV_backdoor/prune.py
  function weight_prune (line 19) | def weight_prune(

FILE: code/deep/ReMoS/CV_backdoor/remos/analyze_coverage.py
  function get_args (line 45) | def get_args():

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/my_neuron_coverage.py
  class MyNeuronCoverage (line 14) | class MyNeuronCoverage:
    method __init__ (line 28) | def __init__(self, threshold=0.5):
    method init_structure (line 40) | def init_structure(self, intermediate_layer_outputs, features_index):
    method update (line 55) | def update(
    method report (line 139) | def report(self, *args):
    method get (line 160) | def get(self, ):
    method _scale (line 185) | def _scale(intermediate_layer_output):
    method _calc_1 (line 210) | def _calc_1(intermediate_layer_output, features_index, threshold):
    method _calc_2 (line 252) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/pytorch_wrapper.py
  class PyTorchModel (line 15) | class PyTorchModel:
    method __init__ (line 34) | def __init__(self, model, intermedia_mode=""):
    method predict (line 44) | def predict(self, dataset, callbacks, batch_size=16):
    method one_sample_intermediate_layer_outputs (line 74) | def one_sample_intermediate_layer_outputs(self, sample, callbacks, ):
    method _intermediate_layers (line 101) | def _intermediate_layers(self, module, pre_name=""):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/strong_neuron_activation_coverage.py
  class StrongNeuronActivationCoverage (line 14) | class StrongNeuronActivationCoverage(MyNeuronCoverage):
    method __init__ (line 15) | def __init__(self, k=5):
    method _calc_1 (line 24) | def _calc_1(intermediate_layer_output, features_index, k):
    method _calc_2 (line 73) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/test_max.py
  function get_max_values (line 5) | def get_max_values(array, k):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/top_k_coverage.py
  class TopKNeuronCoverage (line 14) | class TopKNeuronCoverage(MyNeuronCoverage):
    method __init__ (line 15) | def __init__(self, k=5):
    method _calc_1 (line 24) | def _calc_1(intermediate_layer_output, features_index, threshold):
    method _calc_2 (line 72) | def _calc_2(intermediate_layer_output, features_index, threshold):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/utils/common.py
  function readable_time_str (line 13) | def readable_time_str():
  function user_home_dir (line 27) | def user_home_dir():
  function to_numpy (line 39) | def to_numpy(data):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/utils/keras.py
  class ImageNetValData (line 16) | class ImageNetValData():
    class ImageNetValDataX (line 43) | class ImageNetValDataX():
      method __init__ (line 45) | def __init__(self, dir, filenames, fashion, size, transform):
      method __len__ (line 52) | def __len__(self):
      method __getitem__ (line 55) | def __getitem__(self, index):
    method __init__ (line 91) | def __init__(self, fashion, size, transform=None, shuffle=False, seed=...
    method __len__ (line 112) | def __len__(self):
    method filenames (line 116) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 120) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 137) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_data (line 343) | def cifar10_data(train=False, num_max=None):
  function mnist_data (line 374) | def mnist_data(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/utils/mxnet.py
  class ImageNetValDataset (line 16) | class ImageNetValDataset(mxnet.gluon.data.Dataset):
    method __init__ (line 45) | def __init__(self, resize_size, center_crop_size, preprocess, shuffle=...
    method __len__ (line 67) | def __len__(self):
    method __getitem__ (line 70) | def __getitem__(self, index):
    method filenames (line 82) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 86) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 104) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_dataset (line 228) | def cifar10_dataset(train=False, num_max=None):
  function mnist_dataset (line 251) | def mnist_dataset(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/utils/pytorch.py
  class ImageNetValDataset (line 17) | class ImageNetValDataset(torch.utils.data.Dataset):
    method __init__ (line 46) | def __init__(self, resize_size, center_crop_size, preprocess, shuffle=...
    method __len__ (line 68) | def __len__(self):
    method __getitem__ (line 71) | def __getitem__(self, index):
    method filenames (line 82) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 86) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 104) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_dataset (line 228) | def cifar10_dataset(train=False, num_max=None):
  function mnist_dataset (line 253) | def mnist_dataset(train=False, num_max=None):

FILE: code/deep/ReMoS/CV_backdoor/remos/coverage/utils/tensorflow.py
  class ImageNetValData (line 22) | class ImageNetValData():
    class ImageNetValDataX (line 54) | class ImageNetValDataX():
      method __init__ (line 56) | def __init__(self, dir, filenames, width, height, fashion, transform):
      method __len__ (line 64) | def __len__(self):
      method __getitem__ (line 67) | def __getitem__(self, index):
      method _smallest_size_at_least (line 98) | def _smallest_size_at_least(self, height, width, smallest_side):
      method _aspect_preserving_resize (line 108) | def _aspect_preserving_resize(self, image, smallest_side):
      method _central_crop (line 120) | def _central_crop(self, image_list, crop_height, crop_width):
      method _crop (line 130) | def _crop(self, image, offset_height, offset_width, crop_height, cro...
    method __init__ (line 142) | def __init__(self, width, height, fashion, transform=None, label_offse...
    method __len__ (line 163) | def __len__(self):
    method filenames (line 167) | def filenames(self):
  function imagenet_benchmark_zoo_model_names (line 171) | def imagenet_benchmark_zoo_model_names():
  function imagenet_benchmark_zoo (line 191) | def imagenet_benchmark_zoo(model_name, data_original_shuffle=True, data_...
  function cifar10_test_data (line 535) | def cifar10_test_data(num_max=None):
  function mnist_test_data (line 557) | def mnist_test_data(num_max=None):

FILE: code/deep/ReMoS/CV_backdoor/remos/my_profile.py
  function adv_whitebox (line 39) | def adv_whitebox(model, loader, args, bounds,):
  function get_coverage (line 103) | def get_coverage(args,):
  function compute_selected_neuron_value (line 114) | def compute_selected_neuron_value(
  function log_coverage (line 133) | def log_coverage(model, loader, args, ):
  function record_act (line 171) | def record_act(self, input, output):
  function get_args (line 174) | def get_args():
  function load_student (line 210) | def load_student(ckpt, args, num_classes):

FILE: code/deep/ReMoS/CV_backdoor/remos/nc_pruner.py
  class NCPruner (line 27) | class NCPruner(WeightPruner):
    method __init__ (line 28) | def __init__(
    method prune_record (line 40) | def prune_record(self, log):
    method init_prune (line 44) | def init_prune(self):
    method check_param_num (line 51) | def check_param_num(self):
    method load_nc_info (line 62) | def load_nc_info(self,):
    method weight_prune (line 74) | def weight_prune(
    method final_check_param_num (line 151) | def final_check_param_num(self):

FILE: code/deep/ReMoS/CV_backdoor/remos/remos_pruner.py
  class ReMoSPruner (line 29) | class ReMoSPruner(NCPruner):
    method __init__ (line 30) | def __init__(
    method load_nc_info (line 42) | def load_nc_info(self,):
    method weight_prune (line 54) | def weight_prune(
    method final_check_param_num (line 168) | def final_check_param_num(self):

FILE: code/deep/ReMoS/CV_backdoor/trigger.py
  function teacher_train (line 38) | def teacher_train(teacher, args):

FILE: code/deep/ReMoS/CV_backdoor/utils.py
  class MovingAverageMeter (line 22) | class MovingAverageMeter(object):
    method __init__ (line 24) | def __init__(self, name, fmt=':f', momentum=0.9):
    method reset (line 30) | def reset(self):
    method update (line 35) | def update(self, val, n=1):
    method __str__ (line 39) | def __str__(self):
  class ProgressMeter (line 43) | class ProgressMeter(object):
    method __init__ (line 44) | def __init__(self, num_batches, meters, prefix="", output_dir=None):
    method display (line 53) | def display(self, batch):
    method _get_batch_fmtstr (line 62) | def _get_batch_fmtstr(self, num_batches):
  class CrossEntropyLabelSmooth (line 67) | class CrossEntropyLabelSmooth(nn.Module):
    method __init__ (line 68) | def __init__(self, num_classes, epsilon = 0.1):
    method forward (line 74) | def forward(self, inputs, targets):
  function linear_l2 (line 82) | def linear_l2(model, beta_lmda):
  function l2sp (line 90) | def l2sp(model, reg):
  function advtest_fast (line 110) | def advtest_fast(model, loader, adversary, args):

FILE: code/deep/ReMoS/CV_backdoor/weight_pruner.py
  class WeightPruner (line 24) | class WeightPruner(Finetuner):
    method __init__ (line 25) | def __init__(
    method prune_record (line 48) | def prune_record(self, log):
    method init_prune (line 52) | def init_prune(self):
    method iterative_prune (line 59) | def iterative_prune(self, iteration):
    method check_param_num (line 79) | def check_param_num(self):
    method weight_prune (line 91) | def weight_prune(
    method final_check_param_num (line 157) | def final_check_param_num(self):

FILE: code/deep/TCP/dataset.py
  function loader (line 15) | def loader(path, batch_size=16, num_workers=1, pin_memory=True):
  function test_loader (line 31) | def test_loader(path, batch_size=16, num_workers=1, pin_memory=True):

FILE: code/deep/TCP/finetune.py
  class DANNet (line 27) | class DANNet(nn.Module):
    method __init__ (line 28) | def __init__(self):
    method forward (line 46) | def forward(self, source, target):
  class FilterPrunner (line 59) | class FilterPrunner:
    method __init__ (line 60) | def __init__(self, model):
    method reset (line 64) | def reset(self):
    method forward (line 72) | def forward(self, x, x_target):  # NOTE: whether to add target data
    method compute_rank_1 (line 108) | def compute_rank_1(self, grad):
    method compute_rank_2 (line 121) | def compute_rank_2(self, grad):
    method lowest_ranking_filters (line 134) | def lowest_ranking_filters(self, num):
    method normalize_ranks_per_layer (line 163) | def normalize_ranks_per_layer(self):
    method get_prunning_plan (line 173) | def get_prunning_plan(self, num_filters_to_prune):
  class PrunningFineTuner_VGGnet (line 195) | class PrunningFineTuner_VGGnet:
    method __init__ (line 196) | def __init__(self, train_path, test_path, model):
    method test (line 212) | def test(self):
    method train (line 231) | def train(self, optimizer = None, epoches = 10, save_name=None):
    method train_epoch (line 250) | def train_epoch(self, optimizer = None, epoch = 0, epoches = 0, rank_f...
    method get_candidates_to_prune (line 294) | def get_candidates_to_prune(self, num_filters_to_prune):
    method total_num_filters (line 300) | def total_num_filters(self):
    method prune (line 307) | def prune(self, perc_ind, perchan):
  function total_num_channels (line 359) | def total_num_channels(model):
  function get_args (line 367) | def get_args():

FILE: code/deep/TCP/mmd.py
  function mmd_linear (line 13) | def mmd_linear(f_of_X, f_of_Y):
  function guassian_kernel (line 18) | def guassian_kernel(source, target, kernel_mul=2.0, kernel_num=5, fix_si...
  function mmd_rbf_accelerate (line 34) | def mmd_rbf_accelerate(source, target, kernel_mul=2.0, kernel_num=5, fix...
  function mmd_rbf_noaccelerate (line 46) | def mmd_rbf_noaccelerate(source, target, kernel_mul=2.0, kernel_num=5, f...
  function pairwise_distance (line 60) | def pairwise_distance(x, y):
  function gaussian_kernel_matrix (line 74) | def gaussian_kernel_matrix(x, y, sigmas):
  function maximum_mean_discrepancy (line 81) | def maximum_mean_discrepancy(x, y, kernel= gaussian_kernel_matrix):
  function mmd_loss (line 86) | def mmd_loss(source_features, target_features):

FILE: code/deep/TCP/prune.py
  function replace_layers (line 9) | def replace_layers(model, i, indexes, layers):
  function prune_vgg16_conv_layer (line 14) | def prune_vgg16_conv_layer(model, layer_index, filter_index):

FILE: code/deep/TCP/tools.py
  function print_layers_num (line 11) | def print_layers_num():
  function print_model_parm_nums (line 27) | def print_model_parm_nums(model):
  function print_model_parm_flops (line 34) | def print_model_parm_flops(model):
  function total_num_channels (line 123) | def total_num_channels(model):

FILE: code/deep/adarnn/base/AdaRNN.py
  class AdaRNN (line 7) | class AdaRNN(nn.Module):
    method __init__ (line 12) | def __init__(self, use_bottleneck=False, bottleneck_width=256, n_input...
    method init_layers (line 69) | def init_layers(self):
    method forward_pre_train (line 74) | def forward_pre_train(self, x, len_win=0):
    method gru_features (line 100) | def gru_features(self, x, predict=False):
    method process_gate_weight (line 115) | def process_gate_weight(self, out, index):
    method get_features (line 126) | def get_features(self, output_list):
    method forward_Boosting (line 134) | def forward_Boosting(self, x, weight_mat=None):
    method update_weight_Boosting (line 163) | def update_weight_Boosting(self, weight_mat, dist_old, dist_new):
    method predict (line 174) | def predict(self, x):

FILE: code/deep/adarnn/base/loss/adv_loss.py
  class ReverseLayerF (line 8) | class ReverseLayerF(Function):
    method forward (line 11) | def forward(ctx, x, alpha):
    method backward (line 16) | def backward(ctx, grad_output):
  class Discriminator (line 21) | class Discriminator(nn.Module):
    method __init__ (line 22) | def __init__(self, input_dim=256, hidden_dim=256):
    method forward (line 29) | def forward(self, x):
  function adv (line 36) | def adv(source, target, input_dim=256, hidden_dim=512):

FILE: code/deep/adarnn/base/loss/coral.py
  function CORAL (line 3) | def CORAL(source, target):

FILE: code/deep/adarnn/base/loss/cos.py
  function cosine (line 3) | def cosine(source, target):

FILE: code/deep/adarnn/base/loss/kl_js.py
  function kl_div (line 3) | def kl_div(source, target):
  function js (line 13) | def js(source, target):

FILE: code/deep/adarnn/base/loss/mmd.py
  class MMD_loss (line 4) | class MMD_loss(nn.Module):
    method __init__ (line 5) | def __init__(self, kernel_type='linear', kernel_mul=2.0, kernel_num=5):
    method guassian_kernel (line 12) | def guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5...
    method linear_mmd (line 31) | def linear_mmd(self, X, Y):
    method forward (line 36) | def forward(self, source, target):

FILE: code/deep/adarnn/base/loss/mutual_info.py
  class Mine_estimator (line 5) | class Mine_estimator(nn.Module):
    method __init__ (line 6) | def __init__(self, input_dim=2048, hidden_dim=512):
    method forward (line 10) | def forward(self, X, Y):
  class Mine (line 20) | class Mine(nn.Module):
    method __init__ (line 21) | def __init__(self, input_dim=2048, hidden_dim=512):
    method forward (line 27) | def forward(self, x, y):

FILE: code/deep/adarnn/base/loss/pair_dist.py
  function pairwise_dist (line 5) | def pairwise_dist(X, Y):
  function pairwise_dist_np (line 14) | def pairwise_dist_np(X, Y):
  function pa (line 24) | def pa(X, Y):

FILE: code/deep/adarnn/base/loss_transfer.py
  class TransferLoss (line 4) | class TransferLoss(object):
    method __init__ (line 5) | def __init__(self, loss_type='cosine', input_dim=512):
    method compute (line 12) | def compute(self, X, Y):

FILE: code/deep/adarnn/dataset/data_act.py
  function format_data_x (line 12) | def format_data_x(datafile):
  function format_data_y (line 33) | def format_data_y(datafile):
  function load_data (line 41) | def load_data(data_folder, domain):
  function onehot_to_label (line 82) | def onehot_to_label(y_onehot):
  class data_loader (line 87) | class data_loader(Dataset):
    method __init__ (line 88) | def __init__(self, samples, labels, t):
    method __getitem__ (line 93) | def __getitem__(self, index):
    method __len__ (line 100) | def __len__(self):
  function normalize (line 104) | def normalize(x):

FILE: code/deep/adarnn/dataset/data_process.py
  function load_act_data (line 12) | def load_act_data(data_folder, batch_size=64, domain="1_20"):
  function load_weather_data (line 27) | def load_weather_data(file_path, batch_size=6, station='Changping'):
  function get_split_time (line 42) | def get_split_time(num_domain=2, mode='pre_process', data_file = None, s...
  function TDC (line 54) | def TDC(num_domain, data_file, station, dis_type = 'coral'):
  function load_weather_data_multi_domain (line 113) | def load_weather_data_multi_domain(file_path, batch_size=6, station='Cha...

FILE: code/deep/adarnn/dataset/data_weather.py
  class data_loader (line 15) | class data_loader(Dataset):
    method __init__ (line 16) | def __init__(self, df_feature, df_label, df_label_reg, t=None):
    method __getitem__ (line 34) | def __getitem__(self, index):
    method __len__ (line 41) | def __len__(self):
  function create_dataset (line 45) | def create_dataset(df, station, start_date, end_date, mean=None, std=None):
  function create_dataset_shallow (line 69) | def create_dataset_shallow(df, station, start_date, end_date, mean=None,...
  function get_dataset_statistic (line 91) | def get_dataset_statistic(df, station, start_date, end_date):
  function get_weather_data (line 111) | def get_weather_data(data_file, station, start_time, end_time, batch_siz...
  function get_weather_data_shallow (line 121) | def get_weather_data_shallow(data_file, station, start_time, end_time, b...
  function get_weather_data_statistic (line 130) | def get_weather_data_statistic(data_file, station, start_time, end_time):

FILE: code/deep/adarnn/train_weather.py
  function pprint (line 19) | def pprint(*text):
  function get_model (line 30) | def get_model(name='AdaRNN'):
  function train_AdaRNN (line 35) | def train_AdaRNN(args, model, optimizer, train_loader_list, epoch, dist_...
  function train_epoch_transfer_Boosting (line 108) | def train_epoch_transfer_Boosting(model, optimizer, train_loader_list, e...
  function get_index (line 174) | def get_index(num_domain=2):
  function train_epoch_transfer (line 182) | def train_epoch_transfer(args, model, optimizer, train_loader_list):
  function count_parameters (line 244) | def count_parameters(model):
  function test_epoch (line 248) | def test_epoch(model, test_loader, prefix='Test'):
  function test_epoch_inference (line 272) | def test_epoch_inference(model, test_loader, prefix='Test'):
  function inference (line 305) | def inference(model, data_loader):
  function inference_all (line 311) | def inference_all(output_path, model, model_path, loaders):
  function transform_type (line 330) | def transform_type(init_weight):
  function main_transfer (line 338) | def main_transfer(args):
  function get_args (line 415) | def get_args():

FILE: code/deep/adarnn/transformer_adapt.py
  function pprint (line 19) | def pprint(*text):
  function count_parameters (line 30) | def count_parameters(model):
  function train_epoch (line 34) | def train_epoch(args, model, optimizer, src_train_loader,  trg_train_loa...
  function test_epoch (line 74) | def test_epoch(model, test_loader, prefix='Test'):
  function test_epoch_inference (line 99) | def test_epoch_inference(model, test_loader, prefix='Test'):
  function inference (line 133) | def inference(model, data_loader):
  function inference_all (line 139) | def inference_all(output_path, model, model_path, loaders):
  function main_transfer (line 159) | def main_transfer(args):
  function get_args (line 246) | def get_args():

FILE: code/deep/adarnn/tst/decoder.py
  class Decoder (line 10) | class Decoder(nn.Module):
    method __init__ (line 37) | def __init__(self,
    method forward (line 71) | def forward(self, x: torch.Tensor, memory: torch.Tensor) -> torch.Tensor:

FILE: code/deep/adarnn/tst/encoder.py
  class Encoder (line 10) | class Encoder(nn.Module):
    method __init__ (line 37) | def __init__(self,
    method forward (line 69) | def forward(self, x: torch.Tensor) -> torch.Tensor:
    method attention_map (line 99) | def attention_map(self) -> torch.Tensor:

FILE: code/deep/adarnn/tst/loss.py
  class OZELoss (line 5) | class OZELoss(nn.Module):
    method __init__ (line 24) | def __init__(self, reduction: str = 'mean', alpha: float = 0.3):
    method forward (line 32) | def forward(self,

FILE: code/deep/adarnn/tst/multiHeadAttention.py
  class MultiHeadAttention (line 11) | class MultiHeadAttention(nn.Module):
    method __init__ (line 33) | def __init__(self,
    method forward (line 56) | def forward(self,
    method attention_map (line 118) | def attention_map(self) -> torch.Tensor:
  class MultiHeadAttentionChunk (line 128) | class MultiHeadAttentionChunk(MultiHeadAttention):
    method __init__ (line 154) | def __init__(self,
    method forward (line 175) | def forward(self,
  class MultiHeadAttentionWindow (line 236) | class MultiHeadAttentionWindow(MultiHeadAttention):
    method __init__ (line 265) | def __init__(self,
    method forward (line 293) | def forward(self,

FILE: code/deep/adarnn/tst/positionwiseFeedForward.py
  class PositionwiseFeedForward (line 8) | class PositionwiseFeedForward(nn.Module):
    method __init__ (line 22) | def __init__(self,
    method forward (line 31) | def forward(self, x: torch.Tensor) -> torch.Tensor:

FILE: code/deep/adarnn/tst/transformer.py
  class Transformer (line 9) | class Transformer(nn.Module):
    method __init__ (line 55) | def __init__(self,
    method forward (line 107) | def forward(self, x: torch.Tensor) -> torch.Tensor:
    method adapt_encoding_weight (line 177) | def adapt_encoding_weight(self, list_encoding, loss_type, train_type, ...
    method update_weight_Boosting (line 205) | def update_weight_Boosting(self, weight_mat, dist_old, dist_new):

FILE: code/deep/adarnn/tst/utils.py
  function generate_original_PE (line 7) | def generate_original_PE(length: int, d_model: int) -> torch.Tensor:
  function generate_regular_PE (line 32) | def generate_regular_PE(length: int, d_model: int, period: Optional[int]...
  function generate_local_map_mask (line 58) | def generate_local_map_mask(chunk_size: int,

FILE: code/deep/adarnn/utils/heat_map.py
  function draw_heatmap (line 8) | def draw_heatmap(data, filename):

FILE: code/deep/adarnn/utils/metrics.py
  function robust_zscore_tensor (line 5) | def robust_zscore_tensor(x):
  function calc_ic (line 14) | def calc_ic(pred, label):
  function calc_r2 (line 20) | def calc_r2(pred, label):
  function metric_fn (line 24) | def metric_fn(pred, label, metric='IC'):
  function robust_zscore (line 34) | def robust_zscore(x):
  function calc_all_metrics (line 42) | def calc_all_metrics(pred):
  function logcosh (line 63) | def logcosh(pred, label):

FILE: code/deep/adarnn/utils/utils.py
  class AverageMeter (line 11) | class AverageMeter(object):
    method __init__ (line 12) | def __init__(self):
    method reset (line 15) | def reset(self):
    method update (line 22) | def update(self, val, n=1):
  function average_params (line 30) | def average_params(params_list):
  function zscore (line 49) | def zscore(x):
  function calc_loss (line 53) | def calc_loss(pred, label):
  function calc_corr (line 57) | def calc_corr(pred, label):
  function test_ic (line 61) | def test_ic(model_list, data_list, device, verbose=True, ic_type='spearm...
  function test_ic_daily (line 97) | def test_ic_daily(model_list, data_list, device, verbose=True, ic_type='...
  function test_ic_uni (line 135) | def test_ic_uni(model, data_loader, model_path=None, ic_type='spearman',...
  function calc_ic (line 160) | def calc_ic(x, y, ic_type='pearson'):
  function create_dir (line 168) | def create_dir(path):
  function handle_nan (line 172) | def handle_nan(x):
  class Log_Loss (line 176) | class Log_Loss(nn.Module):
    method __init__ (line 177) | def __init__(self):
    method forward (line 180) | def forward(self, ytrue, ypred):
  function spearman_corr (line 184) | def spearman_corr(x, y):
  function spearman_corr2 (line 190) | def spearman_corr2(x, y):
  function pearson_corr (line 196) | def pearson_corr(x, y):
  function dir_exist (line 202) | def dir_exist(dirs):

FILE: code/deep/adarnn/utils/visualize.py
  class Visualize (line 7) | class Visualize(object):
    method __init__ (line 8) | def __init__(self, port=8097, env='env'):
    method plot_line (line 13) | def plot_line(self, Y, global_step, title='title', legend=['legend']):
    method heat_map (line 33) | def heat_map(self, X, title='title'):
    method log (line 44) | def log(self, info, title='log_text'):
  function test (line 57) | def test():

FILE: code/deep/finetune_AlexNet_ResNet/data_loader.py
  function load_data (line 6) | def load_data(root_path, dir, batch_size, phase):
  function load_train (line 25) | def load_train(root_path, dir, batch_size, phase):

FILE: code/deep/finetune_AlexNet_ResNet/finetune_office31.py
  function load_model (line 34) | def load_model(name='alexnet'):
  function get_optimizer (line 50) | def get_optimizer(model_name):
  function lr_schedule (line 72) | def lr_schedule(optimizer, epoch):
  function test (line 84) | def test(model, target_test_loader):
  function finetune (line 99) | def finetune(model, dataloaders, optimizer):

FILE: code/deep/fixed/alg/alg.py
  function get_algorithm_class (line 9) | def get_algorithm_class(algorithm_name):

FILE: code/deep/fixed/alg/algs/Fixed.py
  class Fixed (line 12) | class Fixed(Algorithm):
    method __init__ (line 14) | def __init__(self, args):
    method update (line 29) | def update(self, minibatches, opt, sch):
    method predict (line 59) | def predict(self, x):

FILE: code/deep/fixed/alg/algs/base.py
  class Algorithm (line 5) | class Algorithm(torch.nn.Module):
    method __init__ (line 6) | def __init__(self, args):
    method update (line 9) | def update(self, minibatches):
    method predict (line 12) | def predict(self, x):

FILE: code/deep/fixed/alg/modelopera.py
  function get_fea (line 6) | def get_fea(args):
  function accuracy (line 11) | def accuracy(network, loader, weights, usedpredict='p'):

FILE: code/deep/fixed/alg/opt.py
  function get_params (line 5) | def get_params(alg, args):
  function get_optimizer (line 23) | def get_optimizer(alg, args):
  function get_scheduler (line 30) | def get_scheduler(optimizer, args):

FILE: code/deep/fixed/datautil/actdata/cross_people.py
  class ActList (line 8) | class ActList(mydataset):
    method __init__ (line 9) | def __init__(self, args, dataset, root_dir, people_group, group_num, t...
    method comb_position (line 26) | def comb_position(self, x, cy, py, sy):
    method set_x (line 42) | def set_x(self, x):

FILE: code/deep/fixed/datautil/actdata/util.py
  function act_train (line 6) | def act_train():
  function act_test (line 12) | def act_test():
  function loaddata_from_numpy (line 18) | def loaddata_from_numpy(dataset='dsads', root_dir='./data/act/'):

FILE: code/deep/fixed/datautil/getdataloader.py
  function get_dataloader (line 11) | def get_dataloader(args, trdatalist, tedatalist):
  function get_act_dataloader (line 52) | def get_act_dataloader(args):

FILE: code/deep/fixed/datautil/mydataloader.py
  class _InfiniteSampler (line 5) | class _InfiniteSampler(torch.utils.data.Sampler):
    method __init__ (line 6) | def __init__(self, sampler):
    method __iter__ (line 9) | def __iter__(self):
  class InfiniteDataLoader (line 15) | class InfiniteDataLoader:
    method __init__ (line 16) | def __init__(self, dataset, weights, batch_size, num_workers):
    method __iter__ (line 41) | def __iter__(self):
    method __len__ (line 45) | def __len__(self):

FILE: code/deep/fixed/datautil/util.py
  function Nmax (line 7) | def Nmax(args, d):
  class mydataset (line 14) | class mydataset(object):
    method __init__ (line 15) | def __init__(self, args):
    method set_labels (line 25) | def set_labels(self, tlabels=None, label_type='domain_label'):
    method set_labels_by_index (line 32) | def set_labels_by_index(self, tlabels=None, tindex=None, label_type='d...
    method target_trans (line 38) | def target_trans(self, y):
    method input_trans (line 44) | def input_trans(self, x):
    method __getitem__ (line 50) | def __getitem__(self, index):
    method __len__ (line 56) | def __len__(self):
  class subdataset (line 60) | class subdataset(mydataset):
    method __init__ (line 61) | def __init__(self, args, dataset, indices):
  function split_trian_val_test (line 72) | def split_trian_val_test(args, da, rate=0.8):
  function make_weights_for_balanced_classes (line 84) | def make_weights_for_balanced_classes(dataset):
  function random_pairs_of_minibatches_by_domainperm (line 105) | def random_pairs_of_minibatches_by_domainperm(minibatches):

FILE: code/deep/fixed/loss/margin_loss.py
  function _max_with_relu (line 8) | def _max_with_relu(a, b):
  function _get_grad (line 12) | def _get_grad(out_, in_):
  class LargeMarginLoss (line 20) | class LargeMarginLoss:
    method __init__ (line 21) | def __init__(self,
    method __call__ (line 43) | def __call__(self, logits, onehot_labels, feature_maps):

FILE: code/deep/fixed/network/Adver_network.py
  class ReverseLayerF (line 6) | class ReverseLayerF(Function):
    method forward (line 8) | def forward(ctx, x, alpha):
    method backward (line 13) | def backward(ctx, grad_output):
  class Discriminator (line 18) | class Discriminator(nn.Module):
    method __init__ (line 19) | def __init__(self, input_dim=256, hidden_dim=256, num_domains=4):
    method forward (line 34) | def forward(self, x):

FILE: code/deep/fixed/network/act_network.py
  class ActNetwork (line 13) | class ActNetwork(nn.Module):
    method __init__ (line 14) | def __init__(self, taskname):
    method forward (line 33) | def forward(self, x):
    method getfea (line 38) | def getfea(self, x):

FILE: code/deep/fixed/network/common_network.py
  class feat_bottleneck (line 6) | class feat_bottleneck(nn.Module):
    method __init__ (line 7) | def __init__(self, feature_dim, bottleneck_dim=256, type="ori"):
    method forward (line 15) | def forward(self, x):
  class feat_classifier (line 22) | class feat_classifier(nn.Module):
    method __init__ (line 23) | def __init__(self, class_num, bottleneck_dim=256, type="linear"):
    method forward (line 32) | def forward(self, x):

FILE: code/deep/fixed/utils/util.py
  function set_random_seed (line 10) | def set_random_seed(seed=0):
  function train_valid_target_eval_names (line 19) | def train_valid_target_eval_names(args):
  function print_row (line 30) | def print_row(row, colwidth=10, latex=False):
  class Tee (line 45) | class Tee:
    method __init__ (line 46) | def __init__(self, fname, mode="a"):
    method write (line 50) | def write(self, message):
    method flush (line 55) | def flush(self):
  function act_param_init (line 60) | def act_param_init(args):
  function get_args (line 69) | def get_args():
  function init_args (line 124) | def init_args(args):

FILE: code/distance/coral_pytorch.py
  function CORAL_loss (line 6) | def CORAL_loss(source, target):

FILE: code/distance/mmd_numpy_sklearn.py
  function mmd_linear (line 7) | def mmd_linear(X, Y):
  function mmd_rbf (line 28) | def mmd_rbf(X, Y, gamma=1.0):
  function mmd_poly (line 47) | def mmd_poly(X, Y, degree=2, gamma=1, coef0=0):

FILE: code/distance/mmd_pytorch.py
  class MMD_loss (line 7) | class MMD_loss(nn.Module):
    method __init__ (line 8) | def __init__(self, kernel_type='rbf', kernel_mul=2.0, kernel_num=5):
    method guassian_kernel (line 15) | def guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5...
    method linear_mmd2 (line 34) | def linear_mmd2(self, f_of_X, f_of_Y):
    method forward (line 40) | def forward(self, source, target):

FILE: code/distance/proxy_a_distance.py
  function proxy_a_distance (line 7) | def proxy_a_distance(source_X, target_X, verbose=False):

FILE: code/feature_extractor/for_digit_data/digit_data_loader.py
  class GetDataset (line 20) | class GetDataset(data.Dataset):
    method __init__ (line 31) | def __init__(self, data, label,
    method __getitem__ (line 38) | def __getitem__(self, index):
    method __len__ (line 67) | def __len__(self):
  function dense_to_one_hot (line 71) | def dense_to_one_hot(labels_dense):
  function load_mnist (line 84) | def load_mnist(path, scale=True, usps=False, all_use=True):
  function load_svhn (line 116) | def load_svhn(path_train, path_test):
  function load_usps (line 129) | def load_usps(path, all_use=True):
  function load_dataset (line 151) | def load_dataset(domain, root_dir):
  function load_data (line 163) | def load_data(domain, root_dir, batch_size):

FILE: code/feature_extractor/for_digit_data/digit_deep_feature.py
  function finetune (line 42) | def finetune(model, dataloaders, optimizer):
  function extract_feature (line 90) | def extract_feature(model, model_path, dataloader, source, data_name):
  function classify_1nn (line 109) | def classify_1nn():

FILE: code/feature_extractor/for_digit_data/digit_network.py
  class Network (line 10) | class Network(nn.Module):
    method __init__ (line 12) | def __init__(self):
    method forward (line 35) | def forward(self, input_data):
    method get_feature (line 45) | def get_feature(self, input_data):

FILE: code/feature_extractor/for_image_data/backbone.py
  class AlexNetFc (line 9) | class AlexNetFc(nn.Module):
    method __init__ (line 10) | def __init__(self):
    method forward (line 20) | def forward(self, x):
    method output_num (line 26) | def output_num(self):
  class ResNet18Fc (line 30) | class ResNet18Fc(nn.Module):
    method __init__ (line 31) | def __init__(self):
    method forward (line 45) | def forward(self, x):
    method output_num (line 58) | def output_num(self):
  class ResNet34Fc (line 62) | class ResNet34Fc(nn.Module):
    method __init__ (line 63) | def __init__(self):
    method forward (line 77) | def forward(self, x):
    method output_num (line 90) | def output_num(self):
  class ResNet50Fc (line 94) | class ResNet50Fc(nn.Module):
    method __init__ (line 95) | def __init__(self):
    method forward (line 109) | def forward(self, x):
    method output_num (line 122) | def output_num(self):
  class ResNet101Fc (line 126) | class ResNet101Fc(nn.Module):
    method __init__ (line 127) | def __init__(self):
    method forward (line 141) | def forward(self, x):
    method output_num (line 154) | def output_num(self):
  class ResNet152Fc (line 158) | class ResNet152Fc(nn.Module):
    method __init__ (line 159) | def __init__(self):
    method forward (line 173) | def forward(self, x):
    method output_num (line 186) | def output_num(self):

FILE: code/feature_extractor/for_image_data/data_load.py
  function load_data (line 7) | def load_data(data_folder, batch_size, phase='train', train_val_split=Tr...
  class ImageCLEF (line 46) | class ImageCLEF(torch.utils.data.Dataset):
    method __init__ (line 47) | def __init__(self, root_dir, domain, transform=None):
    method __getitem__ (line 59) | def __getitem__(self, index):
    method __len__ (line 67) | def __len__(self):
  function load_imageclef_train (line 70) | def load_imageclef_train(root_path, domain, batch_size, phase):
  function load_imageclef_test (line 96) | def load_imageclef_test(root_path, domain, batch_size, phase):

FILE: code/feature_extractor/for_image_data/main.py
  function get_optimizer (line 49) | def get_optimizer(model):
  function lr_schedule (line 61) | def lr_schedule(optimizer, epoch):
  function finetune (line 74) | def finetune(model, dataloaders, optimizer, criterion, best_model_path, ...
  class FeatureExtractor (line 128) | class FeatureExtractor(nn.Module):
    method __init__ (line 129) | def __init__(self, model, extracted_layers):
    method forward (line 135) | def forward(self, x):
  function extract_feature (line 146) | def extract_feature(model, dataloader, save_path, load_from_disk=True, m...
  function classify_1nn (line 173) | def classify_1nn(data_train, data_test):

FILE: code/feature_extractor/for_image_data/models.py
  class Network (line 6) | class Network(nn.Module):
    method __init__ (line 7) | def __init__(self, base_net='alexnet', n_class=31):
    method forward (line 16) | def forward(self, x):
    method get_features (line 21) | def get_features(self, x):

FILE: code/traditional/BDA/BDA.py
  function kernel (line 16) | def kernel(ker, X1, X2, gamma):
  function proxy_a_distance (line 36) | def proxy_a_distance(source_X, target_X):
  function estimate_mu (line 55) | def estimate_mu(_X1, _Y1, _X2, _Y2):
  class BDA (line 75) | class BDA:
    method __init__ (line 76) | def __init__(self, kernel_type='primal', dim=30, lamb=1, mu=0.5, gamma...
    method fit_predict (line 97) | def fit_predict(self, Xs, Ys, Xt, Yt):

FILE: code/traditional/CORAL/CORAL.py
  class CORAL (line 14) | class CORAL:
    method __init__ (line 15) | def __init__(self):
    method fit (line 18) | def fit(self, Xs, Xt):
    method fit_predict (line 33) | def fit_predict(self, Xs, Ys, Xt, Yt):

FILE: code/traditional/GFK/GFK.py
  class GFK (line 15) | class GFK:
    method __init__ (line 16) | def __init__(self, dim=20):
    method fit (line 24) | def fit(self, Xs, Xt, norm_inputs=None):
    method fit_predict (line 97) | def fit_predict(self, Xs, Ys, Xt, Yt):
    method principal_angles (line 113) | def principal_angles(self, Ps, Pt):
    method train_pca (line 126) | def train_pca(self, data, mu_data, std_data, subspace_dim):
    method znorm (line 155) | def znorm(self, data):
    method subspace_disagreement_measure (line 164) | def subspace_disagreement_measure(self, Ps, Pt, Pst):

FILE: code/traditional/JDA/JDA.py
  function kernel (line 13) | def kernel(ker, X1, X2, gamma):
  class JDA (line 30) | class JDA:
    method __init__ (line 31) | def __init__(self, kernel_type='primal', dim=30, lamb=1, gamma=1, T=10):
    method fit_predict (line 46) | def fit_predict(self, Xs, Ys, Xt, Yt):

FILE: code/traditional/KMM.py
  function kernel (line 19) | def kernel(ker, X1, X2, gamma):
  class KMM (line 33) | class KMM:
    method __init__ (line 34) | def __init__(self, kernel_type='linear', gamma=1.0, B=1.0, eps=None):
    method fit (line 47) | def fit(self, Xs, Xt):
  function load_data (line 70) | def load_data(folder, domain):
  function knn_classify (line 76) | def knn_classify(Xs, Ys, Xt, Yt, k=1, norm=False):

FILE: code/traditional/MEDA/MEDA.py
  function kernel (line 16) | def kernel(ker, X1, X2, gamma):
  function proxy_a_distance (line 33) | def proxy_a_distance(source_X, target_X):
  class MEDA (line 49) | class MEDA:
    method __init__ (line 50) | def __init__(self, kernel_type='primal', dim=30, lamb=1, rho=1.0, eta=...
    method estimate_mu (line 71) | def estimate_mu(self, _X1, _Y1, _X2, _Y2):
    method fit_predict (line 90) | def fit_predict(self, Xs, Ys, Xt, Yt):

FILE: code/traditional/SCL.py
  class SCL (line 8) | class SCL(object):
    method __init__ (line 12) | def __init__(self, l2=1.0, num_pivots=10, base_classifer=LinearSVC()):
    method fit (line 19) | def fit(self, Xs, Xt):
    method transform (line 49) | def transform(self, X):
    method fit_predict (line 58) | def fit_predict(self, Xs, Xt, X_test, Ys, Y_test):

FILE: code/traditional/SFA.py
  class SFA (line 11) | class SFA:
    method __init__ (line 16) | def __init__(self, l=500, K=100, base_classifer=svm.SVC()):
    method fit (line 27) | def fit(self, Xs, Xt):
    method transform (line 78) | def transform(self, X):
    method fit_predict (line 81) | def fit_predict(self, Xs, Xt, X_test, Ys, Y_test):

FILE: code/traditional/TCA/TCA.py
  function kernel (line 14) | def kernel(ker, X1, X2, gamma):
  class TCA (line 34) | class TCA:
    method __init__ (line 35) | def __init__(self, kernel_type='primal', dim=30, lamb=1, gamma=1):
    method fit (line 48) | def fit(self, Xs, Xt):
    method fit_predict (line 75) | def fit_predict(self, Xs, Ys, Xt, Yt):
    method fit_new (line 94) | def fit_new(self, Xs, Xt, Xt2):
    method fit_predict_new (line 128) | def fit_predict_new(self, Xt, Xs, Ys, Xt2, Yt2):
  function train_valid (line 150) | def train_valid():

FILE: code/traditional/TrAdaBoost.py
  class TrAdaBoost (line 4) | class TrAdaBoost:
    method __init__ (line 5) | def __init__(self, iters=10):
    method fit_predict (line 13) | def fit_predict(self, trans_S, trans_A, label_S, label_A, test):
    method calculate_P (line 85) | def calculate_P(self, weights, label):
    method train_classify (line 90) | def train_classify(self, trans_data, trans_label, test_data, P):
    method calculate_error_rate (line 96) | def calculate_error_rate(self, label_R, label_H, weight):

FILE: code/traditional/pyEasyTL/EasyTL.py
  function get_cosine_dist (line 6) | def get_cosine_dist(A, B):
  function get_ma_dist (line 25) | def get_ma_dist(A, B):
  function get_class_center (line 42) | def get_class_center(Xs,Ys,Xt,dist):
  function EasyTL (line 74) | def EasyTL(Xs,Ys,Xt,Yt,intra_align="coral",dist="euclidean",lp="linear"):

FILE: code/traditional/pyEasyTL/intra_alignment.py
  function GFK_map (line 6) | def GFK_map(Xs, Xt):
  function gsvd (line 9) | def gsvd(A, B):
  function getAngle (line 12) | def getAngle(Ps, Pt, DD):
  function getGFKDim (line 25) | def getGFKDim(Xs, Xt):
  function PCA_map (line 45) | def PCA_map(Xs, Xt):
  function CORAL_map (line 53) | def CORAL_map(Xs,Xt):

FILE: code/traditional/pyEasyTL/label_prop.py
  function label_prop (line 5) | def label_prop(C, nt, Dct, lp="linear"):

FILE: code/traditional/pyEasyTL/label_prop_v2.py
  function label_prop (line 4) | def label_prop(C, nt, Dct, lp="linear"):

FILE: code/traditional/sot/SOT.py
  function norm_max (line 16) | def norm_max(x):
  function load_from_file (line 22) | def load_from_file(root_dir,filename,ss,ts):
  class MyEncoder (line 37) | class MyEncoder(json.JSONEncoder):
    method default (line 38) | def default(self, obj):
  function gmm_target (line 48) | def gmm_target(X,w,c,rootdir,filepath,modelpath,targetname,covtype='diag'):
  function gmm_source_class (line 72) | def gmm_source_class(X,w,slist,covtype='diag'):
  function gmm_source (line 94) | def gmm_source(xs,ys,filepath,sourcename,slist=[],covtype='diag'):
  function entropic_partial_wasserstein (line 120) | def entropic_partial_wasserstein(a, b, M, reg, m=1, numItermax=500,
  class SOT (line 143) | class SOT:
    method __init__ (line 144) | def __init__(self,taskname='ACT',root_dir='./clustertemp/',d=200,
    method get_target (line 154) | def get_target(self,filepath,modelpath,targetname):
    method get_source (line 161) | def get_source(self,filepath,sourcename):
    method partot_DA (line 167) | def partot_DA(self,Sx,Sy,Tx,b,xt1,ttt=1):
    method fit_predict (line 191) | def fit_predict(self, Sx, Sy, Tx, Ty,sfilepath,sourcename,tfilepath,tm...

FILE: code/utils/feature_vis.py
  class FeatureVisualize (line 7) | class FeatureVisualize(object):
    method __init__ (line 12) | def __init__(self, features, labels):
    method plot_tsne (line 20) | def plot_tsne(self, save_eps=False):

FILE: code/utils/grl.py
  class GradReverse (line 12) | class GradReverse(torch.autograd.Function):
    method forward (line 15) | def forward(ctx, x, lambd, **kwargs: None):
    method backward (line 20) | def backward(ctx, *grad_output):
  class GradientReverseLayer (line 27) | class GradientReverseLayer(torch.autograd.Function):
    method __init__ (line 28) | def __init__(self, lambd=1):
    method forward (line 31) | def forward(self, input):
    method backward (line 35) | def backward(self, grad_output):
Condensed preview — 461 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (3,509K chars).
[
  {
    "path": ".gitattributes",
    "chars": 42,
    "preview": "*.pdf filter=lfs diff=lfs merge=lfs -text\n"
  },
  {
    "path": ".github/FUNDING.yml",
    "chars": 645,
    "preview": "# These are supported funding model platforms\n\ngithub: jindongwang\npatreon: # Replace with a single Patreon username\nope"
  },
  {
    "path": ".github/ISSUE_TEMPLATE/bug_report.md",
    "chars": 439,
    "preview": "---\nname: Bug report\nabout: Create a report to help us improve\ntitle: ''\nlabels: ''\nassignees: ''\n\n---\n\n**Before you ope"
  },
  {
    "path": ".gitignore",
    "chars": 74,
    "preview": ".idea/\n*~\n*/*~\ncode/.DS_Store\n.DS_Store\n*.pyc\n*.pkl\noutputs/\n__pycache__/\n"
  },
  {
    "path": "CONTRIBUTING.md",
    "chars": 528,
    "preview": "Anyone interested in transfer learning is welcomed to contribute to this repo (by pull request):\n\n- You can add the late"
  },
  {
    "path": "LICENSE",
    "chars": 1069,
    "preview": "MIT License\n\nCopyright (c) 2018 Jindong Wang\n\nPermission is hereby granted, free of charge, to any person obtaining a co"
  },
  {
    "path": "README.md",
    "chars": 21549,
    "preview": "[![Contributors][contributors-shield]][contributors-url]\n[![Forks][forks-shield]][forks-url]\n[![Stargazers][stars-shield"
  },
  {
    "path": "code/ASR/Adapter/README.md",
    "chars": 4652,
    "preview": "# Adapter-based Cross-lingual ASR with EasyEspnet\r\n\r\n**NOTICE:** The latest code of Adapter has been moved to https://gi"
  },
  {
    "path": "code/ASR/Adapter/balanced_sampler.py",
    "chars": 2255,
    "preview": "import torch\nimport torch.utils.data\nimport random\nimport collections\nimport logging\n\nimport numpy as np\nfrom torch.util"
  },
  {
    "path": "code/ASR/Adapter/config/adapter_example.yaml",
    "chars": 1620,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/Adapter/config/adapterfusion_example.yaml",
    "chars": 1487,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/Adapter/config/finetune_meta_adapter_example.yaml",
    "chars": 1483,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/Adapter/config/meta_adapter_example.yaml",
    "chars": 1635,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/Adapter/data_load.py",
    "chars": 13219,
    "preview": "from espnet.utils.training.batchfy import make_batchset\nfrom torch.utils.data import DataLoader\nfrom torch.nn.utils.rnn "
  },
  {
    "path": "code/ASR/Adapter/e2e_asr_adaptertransformer.py",
    "chars": 42203,
    "preview": "# Copyright 2019 Shigeki Karita\n#  Apache 2.0  (http://www.apache.org/licenses/LICENSE-2.0)\n\n\"\"\"Transformer speech recog"
  },
  {
    "path": "code/ASR/Adapter/train.py",
    "chars": 25561,
    "preview": "import logging\nimport os\nimport collections\nfrom espnet.bin.asr_train import get_parser\nfrom espnet.utils.dynamic_import"
  },
  {
    "path": "code/ASR/Adapter/utils.py",
    "chars": 11166,
    "preview": "import torch\nimport logging\nfrom espnet.asr.asr_utils import add_results_to_json\nimport argparse\nimport numpy as np\nimpo"
  },
  {
    "path": "code/ASR/CMatch/README.md",
    "chars": 4023,
    "preview": "# CMatch: Cross-domain Speech Recognition with Unsupervised Character-level Distribution Matching\r\n\r\n**NOTICE:** The lat"
  },
  {
    "path": "code/ASR/CMatch/config/adv_example.yaml",
    "chars": 1451,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/CMatch/config/ctc_align_example.yaml",
    "chars": 1677,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/CMatch/config/frame_average_example.yaml",
    "chars": 1645,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/CMatch/config/mmd_example.yaml",
    "chars": 1452,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/CMatch/config/pseudo_ctc_pred_example.yaml",
    "chars": 1800,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/CMatch/config/train.yaml",
    "chars": 1106,
    "preview": "# network architecture\n# encoder related\nelayers: 12\neunits: 2048\n# decoder related\ndlayers: 6\ndunits: 2048\n# attention "
  },
  {
    "path": "code/ASR/CMatch/ctc_aligner.py",
    "chars": 11037,
    "preview": "import torch\nimport numpy as np\n\n'''\nBorrowed and modified from neural_sp: \nhttps://github.com/hirofumi0810/neural_sp/bl"
  },
  {
    "path": "code/ASR/CMatch/data_load.py",
    "chars": 12664,
    "preview": "from espnet.utils.training.batchfy import make_batchset\nfrom torch.utils.data import DataLoader\nfrom torch.nn.utils.rnn "
  },
  {
    "path": "code/ASR/CMatch/distances.py",
    "chars": 2668,
    "preview": "import torch\n\ndef CORAL(source, target):\n    DEVICE = source.device\n    d = source.size(1)\n    ns, nt = source.size(0), "
  },
  {
    "path": "code/ASR/CMatch/e2e_asr_udatransformer.py",
    "chars": 31468,
    "preview": "import collections\nfrom espnet.nets.pytorch_backend.e2e_asr_transformer import *\nfrom espnet.nets.pytorch_backend.e2e_as"
  },
  {
    "path": "code/ASR/CMatch/train.py",
    "chars": 19068,
    "preview": "import logging\nimport os\nimport collections\nfrom espnet.bin.asr_train import get_parser\nfrom espnet.utils.dynamic_import"
  },
  {
    "path": "code/ASR/CMatch/utils.py",
    "chars": 10126,
    "preview": "import torch\nimport logging\nfrom espnet.asr.asr_utils import add_results_to_json\nimport argparse\nimport numpy as np\nimpo"
  },
  {
    "path": "code/ASR/readme.md",
    "chars": 647,
    "preview": "# Transfer learning for automatic speech recognition (ASR)\n\nThis directory contains code for several transfer learning-b"
  },
  {
    "path": "code/BDA/readme.md",
    "chars": 197,
    "preview": "# Balanced Distribution Adaptation\n\nSince the transfer learning repo was reorganized, please visit https://github.com/ji"
  },
  {
    "path": "code/DeepDA/BNM/BNM.sh",
    "chars": 2744,
    "preview": "#!/usr/bin/env bash\nGPU_ID=0\ndata_dir=/data/jindwang/office31\n# Office31\nCUDA_VISIBLE_DEVICES=$GPU_ID python main.py --c"
  },
  {
    "path": "code/DeepDA/BNM/BNM.yaml",
    "chars": 300,
    "preview": "# Backbone\nbackbone: resnet50\n\n# Transfer loss related\ntransfer_loss_weight: 1\ntransfer_loss: bnm\n\n# Optimizer related\nl"
  },
  {
    "path": "code/DeepDA/BNM/README.md",
    "chars": 343,
    "preview": "# BNM\n\nA PyTorch implementation of '[Towards Discriminability and Diversity: Batch Nuclear-norm Maximization under Label"
  },
  {
    "path": "code/DeepDA/DAAN/DAAN.sh",
    "chars": 2821,
    "preview": "#!/usr/bin/env bash\nGPU_ID=3\ndata_dir=/home/houwx/tl/datasets/office31\n# Office31\nCUDA_VISIBLE_DEVICES=$GPU_ID python ma"
  },
  {
    "path": "code/DeepDA/DAAN/DAAN.yaml",
    "chars": 302,
    "preview": "# Backbone\nbackbone: resnet50\n\n# Transfer loss related\ntransfer_loss_weight: 1.0\ntransfer_loss: daan\n\n# Optimizer relate"
  },
  {
    "path": "code/DeepDA/DAN/DAN.sh",
    "chars": 2767,
    "preview": "#!/usr/bin/env bash\nGPU_ID=0\ndata_dir=/home/houwx/tl/datasets/office31\n# Office31\nCUDA_VISIBLE_DEVICES=$GPU_ID python ma"
  },
  {
    "path": "code/DeepDA/DAN/DAN.yaml",
    "chars": 301,
    "preview": "# Backbone\nbackbone: resnet50\n\n# Transfer loss related\ntransfer_loss_weight: 0.5\ntransfer_loss: mmd\n\n# Optimizer related"
  },
  {
    "path": "code/DeepDA/DAN/README.md",
    "chars": 686,
    "preview": "# DAN\nA PyTorch implementation of '[Learning Transferable Features with Deep Adaptation Networks](http://ise.thss.tsingh"
  },
  {
    "path": "code/DeepDA/DANN/DANN.sh",
    "chars": 2822,
    "preview": "#!/usr/bin/env bash\nGPU_ID=2\ndata_dir=/home/houwx/tl/datasets/office31\n# Office31\nCUDA_VISIBLE_DEVICES=$GPU_ID python ma"
  },
  {
    "path": "code/DeepDA/DANN/DANN.yaml",
    "chars": 308,
    "preview": "# Backbone\nbackbone: resnet50\n\n# Transfer loss related\ntransfer_loss_weight: 1.0\ntransfer_loss: adv\n\n# Optimizer related"
  },
  {
    "path": "code/DeepDA/DANN/readme.md",
    "chars": 275,
    "preview": "# Domain adversarial neural network (DANN/RevGrad)\n\nThis is a Pytorch implementation of Unsupervised domain adaptation b"
  },
  {
    "path": "code/DeepDA/DSAN/DSAN.sh",
    "chars": 2821,
    "preview": "#!/usr/bin/env bash\nGPU_ID=3\ndata_dir=/home/houwx/tl/datasets/office31\n# Office31\nCUDA_VISIBLE_DEVICES=$GPU_ID python ma"
  },
  {
    "path": "code/DeepDA/DSAN/DSAN.yaml",
    "chars": 302,
    "preview": "# Backbone\nbackbone: resnet50\n\n# Transfer loss related\ntransfer_loss_weight: 0.5\ntransfer_loss: lmmd\n\n# Optimizer relate"
  },
  {
    "path": "code/DeepDA/DSAN/README.md",
    "chars": 1144,
    "preview": "# DSAN\n\nA PyTorch implementation of 'Deep Subdomain Adaptation Network for Image Classification' which has published on "
  },
  {
    "path": "code/DeepDA/DeepCoral/DeepCoral.sh",
    "chars": 3092,
    "preview": "#!/usr/bin/env bash\nGPU_ID=1\ndata_dir=/home/houwx/tl/datasets/office31\n# Office31\nCUDA_VISIBLE_DEVICES=$GPU_ID python ma"
  },
  {
    "path": "code/DeepDA/DeepCoral/DeepCoral.yaml",
    "chars": 304,
    "preview": "# Backbone\nbackbone: resnet50\n\n# Transfer loss related\ntransfer_loss_weight: 10.0\ntransfer_loss: coral\n\n# Optimizer rela"
  },
  {
    "path": "code/DeepDA/DeepCoral/README.md",
    "chars": 543,
    "preview": "# Deep Coral\n\nA PyTorch implementation of '[Deep CORAL Correlation Alignment for Deep Domain Adaptation](https://arxiv.o"
  },
  {
    "path": "code/DeepDA/README.md",
    "chars": 5777,
    "preview": "# DeepDA: Deep Domain Adaptation Toolkit\n\nA lightweight, easy-to-extend, easy-to-learn, high-performance, and for-fair-c"
  },
  {
    "path": "code/DeepDA/backbones.py",
    "chars": 2629,
    "preview": "import torch.nn as nn\nfrom torchvision import models\n\nresnet_dict = {\n    \"resnet18\": models.resnet18,\n    \"resnet34\": m"
  },
  {
    "path": "code/DeepDA/data_loader.py",
    "chars": 2826,
    "preview": "from torchvision import datasets, transforms\nimport torch\n\ndef load_data(data_folder, batch_size, train, num_workers=0, "
  },
  {
    "path": "code/DeepDA/loss_funcs/__init__.py",
    "chars": 177,
    "preview": "from loss_funcs.mmd import *\nfrom loss_funcs.coral import *\nfrom loss_funcs.adv import *\nfrom loss_funcs.lmmd import *\nf"
  },
  {
    "path": "code/DeepDA/loss_funcs/adv.py",
    "chars": 2858,
    "preview": "import torch\nimport torch.nn as nn\nfrom torch.autograd import Function\nimport torch.nn.functional as F\nimport numpy as n"
  },
  {
    "path": "code/DeepDA/loss_funcs/bnm.py",
    "chars": 261,
    "preview": "import torch\n\ndef BNM(src, tar):\n    \"\"\" Batch nuclear-norm maximization, CVPR 2020.\n    tar: a tensor, softmax target o"
  },
  {
    "path": "code/DeepDA/loss_funcs/coral.py",
    "chars": 508,
    "preview": "import torch\n\n\ndef CORAL(source, target, **kwargs):\n    d = source.data.shape[1]\n    ns, nt = source.data.shape[0], targ"
  },
  {
    "path": "code/DeepDA/loss_funcs/daan.py",
    "chars": 2405,
    "preview": "from loss_funcs.adv import *\n\nclass DAANLoss(AdversarialLoss, LambdaSheduler):\n    def __init__(self, num_class, gamma=1"
  },
  {
    "path": "code/DeepDA/loss_funcs/lmmd.py",
    "chars": 3835,
    "preview": "from loss_funcs.mmd import MMDLoss\nfrom loss_funcs.adv import LambdaSheduler\nimport torch\nimport numpy as np\n\nclass LMMD"
  },
  {
    "path": "code/DeepDA/loss_funcs/mmd.py",
    "chars": 2174,
    "preview": "import torch\nimport torch.nn as nn\n\nclass MMDLoss(nn.Module):\n    def __init__(self, kernel_type='rbf', kernel_mul=2.0, "
  },
  {
    "path": "code/DeepDA/main.py",
    "chars": 8082,
    "preview": "import configargparse\nimport data_loader\nimport os\nimport torch\nimport models\nimport utils\nfrom utils import str2bool\nim"
  },
  {
    "path": "code/DeepDA/models.py",
    "chars": 3872,
    "preview": "import torch\nimport torch.nn as nn\nfrom transfer_losses import TransferLoss\nimport backbones\n\n\nclass TransferNet(nn.Modu"
  },
  {
    "path": "code/DeepDA/requirements.txt",
    "chars": 91,
    "preview": "ConfigArgParse==1.4.1\ntorch==1.13.1\ntorchvision==0.9.1\nscikit-learn\nmatplotlib\nxlwt\npyyaml\n"
  },
  {
    "path": "code/DeepDA/transfer_losses.py",
    "chars": 932,
    "preview": "import torch\nimport torch.nn as nn\nfrom loss_funcs import *\n\nclass TransferLoss(nn.Module):\n    def __init__(self, loss_"
  },
  {
    "path": "code/DeepDA/utils.py",
    "chars": 662,
    "preview": "class AverageMeter(object):\n    \"\"\"Computes and stores the average and current value\"\"\"\n\n    def __init__(self):\n       "
  },
  {
    "path": "code/DeepDG/alg/alg.py",
    "chars": 743,
    "preview": "# coding=utf-8\nfrom alg.algs.ERM import ERM\nfrom alg.algs.MMD import MMD\nfrom alg.algs.CORAL import CORAL\nfrom alg.algs."
  },
  {
    "path": "code/DeepDG/alg/algs/ANDMask.py",
    "chars": 1827,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn.functional as F\nfrom alg.algs.ERM import ERM\nimport torch.autograd as autogr"
  },
  {
    "path": "code/DeepDG/alg/algs/CORAL.py",
    "chars": 1639,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn.functional as F\nfrom alg.algs.ERM import ERM\n\n\nclass CORAL(ERM):\n    def __i"
  },
  {
    "path": "code/DeepDG/alg/algs/DANN.py",
    "chars": 1752,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom alg.modelopera import get_fea\nfr"
  },
  {
    "path": "code/DeepDG/alg/algs/DIFEX.py",
    "chars": 5157,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.fft\n\nfrom alg.modelopera "
  },
  {
    "path": "code/DeepDG/alg/algs/ERM.py",
    "chars": 1056,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nfrom alg.modelopera import get_fea\nfr"
  },
  {
    "path": "code/DeepDG/alg/algs/GroupDRO.py",
    "chars": 1056,
    "preview": "#coding=utf-8\nimport torch\nimport torch.nn.functional as F\nfrom alg.algs.ERM import ERM\n\nclass GroupDRO(ERM):\n    \"\"\"\n  "
  },
  {
    "path": "code/DeepDG/alg/algs/MLDG.py",
    "chars": 2186,
    "preview": "# coding=utf-8\nimport torch\nimport copy\nimport torch.nn.functional as F\n\nfrom alg.opt import *\nimport torch.autograd as "
  },
  {
    "path": "code/DeepDG/alg/algs/MMD.py",
    "chars": 2026,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn.functional as F\n\nfrom alg.algs.ERM import ERM\n\n\nclass MMD(ERM):\n    def __in"
  },
  {
    "path": "code/DeepDG/alg/algs/Mixup.py",
    "chars": 986,
    "preview": "# coding=utf-8\nimport numpy as np\nimport torch.nn.functional as F\n\nfrom datautil.util import random_pairs_of_minibatches"
  },
  {
    "path": "code/DeepDG/alg/algs/RSC.py",
    "chars": 2138,
    "preview": "# coding=utf-8\nimport numpy as np\nimport torch\nimport torch.nn.functional as F\nimport torch.autograd as autograd\n\nfrom a"
  },
  {
    "path": "code/DeepDG/alg/algs/VREx.py",
    "chars": 1399,
    "preview": "# coding=utf-8\nimport torch\nimport torch.nn.functional as F\nfrom alg.algs.ERM import ERM\n\n\nclass VREx(ERM):\n    \"\"\"V-REx"
  },
  {
    "path": "code/DeepDG/alg/algs/base.py",
    "chars": 278,
    "preview": "# coding=utf-8\nimport torch\n\n\nclass Algorithm(torch.nn.Module):\n\n    def __init__(self, args):\n        super(Algorithm, "
  },
  {
    "path": "code/DeepDG/alg/modelopera.py",
    "chars": 803,
    "preview": "# coding=utf-8\nimport torch\nfrom network import img_network\n\n\ndef get_fea(args):\n    if args.dataset == 'dg5':\n        n"
  },
  {
    "path": "code/DeepDG/alg/opt.py",
    "chars": 2318,
    "preview": "# coding=utf-8\nimport torch\n\n\ndef get_params(alg, args, inner=False, alias=True, isteacher=False):\n    if args.schuse:\n "
  },
  {
    "path": "code/DeepDG/datautil/getdataloader.py",
    "chars": 2418,
    "preview": "# coding=utf-8\nimport numpy as np\nimport sklearn.model_selection as ms\nfrom torch.utils.data import DataLoader\n\nimport d"
  },
  {
    "path": "code/DeepDG/datautil/imgdata/imgdataload.py",
    "chars": 2178,
    "preview": "# coding=utf-8\nfrom torch.utils.data import Dataset\nimport numpy as np\nfrom datautil.util import Nmax\nfrom datautil.imgd"
  },
  {
    "path": "code/DeepDG/datautil/imgdata/util.py",
    "chars": 1595,
    "preview": "# coding=utf-8\nfrom torchvision import transforms\nfrom PIL import Image, ImageFile\nImageFile.LOAD_TRUNCATED_IMAGES = Tru"
  },
  {
    "path": "code/DeepDG/datautil/mydataloader.py",
    "chars": 1397,
    "preview": "# coding=utf-8\n\nimport torch\n\n\nclass _InfiniteSampler(torch.utils.data.Sampler):\n    \"\"\"Wraps another Sampler to yield a"
  },
  {
    "path": "code/DeepDG/datautil/util.py",
    "chars": 1912,
    "preview": "# coding=utf-8\nimport numpy as np\nimport torch\n\n\ndef Nmax(test_envs, d):\n    for i in range(len(test_envs)):\n        if "
  },
  {
    "path": "code/DeepDG/network/Adver_network.py",
    "chars": 964,
    "preview": "import torch\nimport torch.nn as nn\nfrom torch.autograd import Function\n\n\nclass ReverseLayerF(Function):\n    @staticmetho"
  },
  {
    "path": "code/DeepDG/network/common_network.py",
    "chars": 1685,
    "preview": "# coding=utf-8\nimport torch.nn as nn\nfrom network.util import init_weights\nimport torch.nn.utils.weight_norm as weightNo"
  },
  {
    "path": "code/DeepDG/network/img_network.py",
    "chars": 3295,
    "preview": "# coding=utf-8\nimport torch.nn as nn\nfrom torchvision import models\n\nvgg_dict = {\"vgg11\": models.vgg11, \"vgg13\": models."
  },
  {
    "path": "code/DeepDG/network/util.py",
    "chars": 688,
    "preview": "# coding=utf-8\nimport torch.nn as nn\nimport numpy as np\n\n\ndef calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_it"
  },
  {
    "path": "code/DeepDG/readme.md",
    "chars": 8386,
    "preview": "# DeepDG: Deep Domain Generalization Toolkit\n\nAn easy-to-learn, easy-to-extend, and for-fair-comparison toolkit based on"
  },
  {
    "path": "code/DeepDG/requirements.txt",
    "chars": 105,
    "preview": "Please refer to https://hub.docker.com/r/jindongwang/docker.\n\nCUDA: 10.1\nPytorch: 1.7\nTorchvision: 0.8.1\n"
  },
  {
    "path": "code/DeepDG/scripts/paramsref.md",
    "chars": 2426,
    "preview": "## We offer a hyperparameter reference for our implementations.\n\n### Common hyperparameters\n* max_epoch:120\n* lr: 0.001 "
  },
  {
    "path": "code/DeepDG/scripts/run.sh",
    "chars": 3472,
    "preview": "dataset='PACS'\nalgorithm=('MLDG' 'ERM' 'DANN' 'RSC' 'Mixup' 'MMD' 'CORAL' 'VREx') \ntest_envs=2\ngpu_ids=0\ndata_dir='/home"
  },
  {
    "path": "code/DeepDG/train.py",
    "chars": 8530,
    "preview": "# coding=utf-8\n\nimport os\nimport sys\nimport time\nimport numpy as np\nimport argparse\n\nfrom alg.opt import *\nfrom alg impo"
  },
  {
    "path": "code/DeepDG/utils/util.py",
    "chars": 4166,
    "preview": "# coding=utf-8\nimport random\nimport numpy as np\nimport torch\nimport sys\nimport os\nimport torchvision\nimport PIL\n\n\ndef se"
  },
  {
    "path": "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)",
    "chars": 1,
    "preview": "\n"
  },
  {
    "path": "code/README.md",
    "chars": 32153,
    "preview": "# code_transfer_learning\n\n*Some useful transfer learning and domain adaptation codes*\n\n> It is a waste of time looking f"
  },
  {
    "path": "code/clip/README.md",
    "chars": 8395,
    "preview": "# CLIP for transfer learning datasets\n\nThis demo shows you how to use OpenAI's [CLIP](https://openai.com/blog/clip/) mod"
  },
  {
    "path": "code/clip/__init__.py",
    "chars": 49,
    "preview": "import os\nimport sys\nsys.path.append(os.getcwd())"
  },
  {
    "path": "code/clip/clip_model.py",
    "chars": 6618,
    "preview": "import clip\nfrom PIL import Image\nimport torch\nimport numpy as np\nfrom tqdm import tqdm\nimport torch.nn as nn\n\nimport to"
  },
  {
    "path": "code/clip/data/data_loader.py",
    "chars": 2910,
    "preview": "import torchvision.datasets as datasets\nfrom torchvision import transforms\nimport os\nimport clip\n\nfrom PIL import ImageF"
  },
  {
    "path": "code/clip/data/download_data.py",
    "chars": 9102,
    "preview": "# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved\n\nfrom torchvision.datasets import MNIST\nimport xm"
  },
  {
    "path": "code/clip/data/download_data_azcopy.py",
    "chars": 2011,
    "preview": "# download datasets from azure blob storage, which is faster than downloading from the website\n\nimport os\nimport argpars"
  },
  {
    "path": "code/clip/log/log.txt",
    "chars": 24,
    "preview": "This is the log folder.\n"
  },
  {
    "path": "code/clip/main.py",
    "chars": 4909,
    "preview": "import argparse\nimport numpy as np\nimport os\nimport pretty_errors\nimport torch\nimport torch.optim as optim\n\nfrom clip_mo"
  },
  {
    "path": "code/clip/requirements.txt",
    "chars": 181,
    "preview": "clip==1.0\ncvxopt==1.3.0\ngdown==4.4.0\nnumpy==1.22.0\nPillow==9.3.0\npretty_errors==1.2.25\nscikit_learn==1.1.3\nscipy==1.10.0"
  },
  {
    "path": "code/clip/test_clip.py",
    "chars": 4650,
    "preview": "import numpy as np\nfrom torchvision import transforms, datasets\nfrom PIL import Image\nimport torch\nimport argparse\nimpor"
  },
  {
    "path": "code/clip/utils.py",
    "chars": 2314,
    "preview": "import logging\nimport torch\nimport random\nimport sys\nimport numpy as np\n\ndef load_csv(folder, src_domain, tar_domain):\n "
  },
  {
    "path": "code/deep/B-JMMD/README.md",
    "chars": 829,
    "preview": "<<<<<<< HEAD\n# B-JMMD\nBalanced joint maximum mean discrepancy for deep transfer learning\n=======\n\nThis is the transfer l"
  },
  {
    "path": "code/deep/B-JMMD/caffe/CMakeLists.txt",
    "chars": 3012,
    "preview": "cmake_minimum_required(VERSION 2.8.7)\nif(POLICY CMP0046)\n  cmake_policy(SET CMP0046 NEW)\nendif()\nif(POLICY CMP0054)\n  cm"
  },
  {
    "path": "code/deep/B-JMMD/caffe/CONTRIBUTING.md",
    "chars": 1917,
    "preview": "# Contributing\n\n## Issues\n\nSpecific Caffe design and development issues, bugs, and feature requests are maintained by Gi"
  },
  {
    "path": "code/deep/B-JMMD/caffe/CONTRIBUTORS.md",
    "chars": 620,
    "preview": "# Contributors\n\nCaffe is developed by a core set of BVLC members and the open-source community.\n\nWe thank all of our [co"
  },
  {
    "path": "code/deep/B-JMMD/caffe/INSTALL.md",
    "chars": 210,
    "preview": "# Installation\n\nSee http://caffe.berkeleyvision.org/installation.html for the latest\ninstallation instructions.\n\nCheck t"
  },
  {
    "path": "code/deep/B-JMMD/caffe/LICENSE",
    "chars": 2095,
    "preview": "COPYRIGHT\n\nAll contributions by the University of California:\nCopyright (c) 2014, 2015, The Regents of the University of"
  },
  {
    "path": "code/deep/B-JMMD/caffe/Makefile",
    "chars": 24074,
    "preview": "PROJECT := caffe\n\nCONFIG_FILE := Makefile.config\n# Explicitly check for the config file, otherwise make -k will proceed "
  },
  {
    "path": "code/deep/B-JMMD/caffe/Makefile.config.example",
    "chars": 4292,
    "preview": "## Refer to http://caffe.berkeleyvision.org/installation.html\n# Contributions simplifying and improving our build system"
  },
  {
    "path": "code/deep/B-JMMD/caffe/README.md",
    "chars": 3469,
    "preview": "# Deep Transfer Learning on Caffe\n\nThis is a caffe library for deep transfer learning. We fork the repository with versi"
  },
  {
    "path": "code/deep/B-JMMD/caffe/caffe.cloc",
    "chars": 1180,
    "preview": "Bourne Shell\n    filter remove_matches ^\\s*#\n    filter remove_inline #.*$\n    extension sh\n    script_exe sh\nC\n    filt"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/imageCLEF/bList.txt",
    "chars": 32678,
    "preview": "b/252.car-side-101/252_0446.jpg 6\nb/252.car-side-101/252_0337.jpg 6\nb/252.car-side-101/252_0493.jpg 6\nb/252.car-side-101"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/imageCLEF/cList.txt",
    "chars": 27100,
    "preview": "/home/mcj/dataset/imageCLEF/c/113_0114.jpg 2\n/home/mcj/dataset/imageCLEF/c/113_0025.jpg 2\n/home/mcj/dataset/imageCLEF/c/"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/imageCLEF/iList.txt",
    "chars": 31530,
    "preview": "/home/mcj/dataset/imageCLEF/i/n02691156_7563.JPEG 0\n/home/mcj/dataset/imageCLEF/i/n02691156_30456.JPEG 0\n/home/mcj/datas"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/imageCLEF/pList.txt",
    "chars": 28900,
    "preview": "/home/mcj/dataset/imageCLEF/p/2008_006463.jpg 0\n/home/mcj/dataset/imageCLEF/p/2011_000163.jpg 0\n/home/mcj/dataset/imageC"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/amazon_list.txt",
    "chars": 258028,
    "preview": "/home/mcj/dataset/office/domain_adaptation_images/amazon/images/calculator/frame_0001.jpg 5\n/home/mcj/dataset/office/dom"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/bList.txt",
    "chars": 32678,
    "preview": "b/252.car-side-101/252_0446.jpg 6\nb/252.car-side-101/252_0337.jpg 6\nb/252.car-side-101/252_0493.jpg 6\nb/252.car-side-101"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/cList.txt",
    "chars": 27100,
    "preview": "/home/mcj/dataset/imageCLEF/c/113_0114.jpg 2\n/home/mcj/dataset/imageCLEF/c/113_0025.jpg 2\n/home/mcj/dataset/imageCLEF/c/"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/dslr_list.txt",
    "chars": 44736,
    "preview": "/home/mcj/dataset/office/domain_adaptation_images/dslr/images/calculator/frame_0001.jpg 5\n/home/mcj/dataset/office/domai"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/iList.txt",
    "chars": 31530,
    "preview": "/home/mcj/dataset/imageCLEF/i/n02691156_7563.JPEG 0\n/home/mcj/dataset/imageCLEF/i/n02691156_30456.JPEG 0\n/home/mcj/datas"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/pList.txt",
    "chars": 28900,
    "preview": "/home/mcj/dataset/imageCLEF/p/2008_006463.jpg 0\n/home/mcj/dataset/imageCLEF/p/2011_000163.jpg 0\n/home/mcj/dataset/imageC"
  },
  {
    "path": "code/deep/B-JMMD/caffe/data/office/webcam_list.txt",
    "chars": 72846,
    "preview": "/home/mcj/dataset/office/domain_adaptation_images/webcam/images/calculator/frame_0001.jpg 5\n/home/mcj/dataset/office/dom"
  },
  {
    "path": "code/deep/B-JMMD/caffe/include/caffe/layers/bjmmd_layer.hpp",
    "chars": 1905,
    "preview": "#ifndef CAFFE_BJMMD_LOSS_LAYER_HPP_\n#define CAFFE_BJMMD_LOSS_LAYER_HPP_\n\n#include <vector>\n\n#include \"caffe/blob.hpp\"\n#i"
  },
  {
    "path": "code/deep/B-JMMD/caffe/kmake.sh",
    "chars": 78,
    "preview": "cd src\nprotoc ./caffe/proto/caffe.proto --cpp_out=../include/\ncd ..\nmake -j48\n"
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/B-JMMD/alexnet/solver.prototxt",
    "chars": 385,
    "preview": "net: \"models/BJMMD/alexnet/train_val.prototxt\"\ntest_iter: 795 # target domain: amazon 2817, webcam 795, dslr 498\ntest_in"
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/B-JMMD/alexnet/train_val.prototxt",
    "chars": 7838,
    "preview": "name: \"amazon_to_webcam\"\nlayer {\n  name: \"source_data\"\n  type: \"ImageData\"\n  top: \"source_data\"\n  top: \"source_label\"\n  "
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/B-JMMD/resnet/solver.prototxt",
    "chars": 368,
    "preview": "net: \"models/BJMMD/resnet/train_val.prototxt\"\ntest_iter: 795 # target domain: amazon 2817, webcam 795, dslr 498\ntest_int"
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/B-JMMD/resnet/train_val.prototxt",
    "chars": 36370,
    "preview": "name: \"amazon_to_webcam\"\nlayer {\n  name: \"source_data\"\n  type: \"ImageData\"\n  top: \"source_data\"\n  top: \"source_label\"\n  "
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/bvlc_reference_caffenet/deploy.prototxt",
    "chars": 2878,
    "preview": "name: \"CaffeNet\"\nlayer {\n  name: \"data\"\n  type: \"Input\"\n  top: \"data\"\n  input_param { shape: { dim: 10 dim: 3 dim: 227 d"
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/bvlc_reference_caffenet/readme.md",
    "chars": 1307,
    "preview": "---\nname: BVLC CaffeNet Model\ncaffemodel: bvlc_reference_caffenet.caffemodel\ncaffemodel_url: http://dl.caffe.berkeleyvis"
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/bvlc_reference_caffenet/solver.prototxt",
    "chars": 315,
    "preview": "net: \"models/bvlc_reference_caffenet/train_val.prototxt\"\ntest_iter: 1000\ntest_interval: 1000\nbase_lr: 0.01\nlr_policy: \"s"
  },
  {
    "path": "code/deep/B-JMMD/caffe/models/bvlc_reference_caffenet/train_val.prototxt",
    "chars": 5707,
    "preview": "name: \"CaffeNet\"\nlayer {\n  name: \"data\"\n  type: \"Data\"\n  top: \"data\"\n  top: \"label\"\n  include {\n    phase: TRAIN\n  }\n  t"
  },
  {
    "path": "code/deep/B-JMMD/caffe/src/caffe/layers/bjmmd_layer.cpp",
    "chars": 2677,
    "preview": "#include <vector>\n\n#include \"caffe/layers/bjmmd_layer.hpp\"\n\nnamespace caffe {\n\ntemplate <typename Dtype>\nvoid BJMMDLossL"
  },
  {
    "path": "code/deep/B-JMMD/caffe/src/caffe/layers/bjmmd_layer.cu",
    "chars": 17791,
    "preview": "#include <vector>\n#include <cfloat>\n#include <algorithm>\n\n#include \"caffe/layers/bjmmd_layer.hpp\"\n#include \"caffe/util/m"
  },
  {
    "path": "code/deep/B-JMMD/caffe/src/caffe/proto/caffe.proto",
    "chars": 58928,
    "preview": "syntax = \"proto2\";\n\npackage caffe;\n\n// Specifies the shape (dimensions) of a Blob.\nmessage BlobShape {\n  repeated int64 "
  },
  {
    "path": "code/deep/CSG/README.md",
    "chars": 2684,
    "preview": "# Learning Causal Semantic Representation for Out-of-Distribution Prediction\n\nThis repository is the official implementa"
  },
  {
    "path": "code/deep/CSG/a-domainbed/main.py",
    "chars": 8273,
    "preview": "#!/usr/bin/env python3.6\nimport warnings\nimport sys\nimport torch as tc\nsys.path.append(\"..\")\nfrom utils.utils_main impor"
  },
  {
    "path": "code/deep/CSG/a-domainbed/prepare_data.sh",
    "chars": 511,
    "preview": "git clone https://github.com/facebookresearch/DomainBed\ncd DomainBed/\ngit checkout 2deb150\npython3 -m pip install gdown="
  },
  {
    "path": "code/deep/CSG/a-domainbed/run_da.sh",
    "chars": 4146,
    "preview": "REPRUN=\"../utils/reprun.sh 10\"\nPYCMD=\"python3 main.py\"\n\ncase $1 in\n\tdann)\n\t\t# Results taken from \"In search of lost doma"
  },
  {
    "path": "code/deep/CSG/a-domainbed/run_ood.sh",
    "chars": 3536,
    "preview": "REPRUN=\"../utils/reprun.sh 10\"\nPYCMD=\"python3 main.py\"\n\ncase $1 in\n\tdiscr) # CE\n\t\t# Results taken from \"In search of los"
  },
  {
    "path": "code/deep/CSG/a-domainbed/visual.py",
    "chars": 17190,
    "preview": "#!/usr/bin/env python3.6\nimport warnings\nimport sys\nsys.path.append(\"..\")\nfrom utils.utils_main import main_stem, get_pa"
  },
  {
    "path": "code/deep/CSG/a-imageclef/main.py",
    "chars": 3270,
    "preview": "#!/usr/bin/env python3.6\nimport warnings\nimport sys\nsys.path.append(\"..\")\nfrom utils.utils_main import main_stem, get_pa"
  },
  {
    "path": "code/deep/CSG/a-imageclef/prepare_data.sh",
    "chars": 168,
    "preview": "DATAFILE=image_CLEF.zip\nwget https://transferlearningdrive.blob.core.windows.net/teamdrive/dataset/$DATAFILE\nmkdir -p da"
  },
  {
    "path": "code/deep/CSG/a-imageclef/run_da.sh",
    "chars": 4402,
    "preview": "REPRUN=\"../utils/reprun.sh 10\"\nPYCMD=\"python3 main.py\"\n\ncase $1 in\n\tdann)\n\t\t# Results taken from \"Conditional adversaria"
  },
  {
    "path": "code/deep/CSG/a-imageclef/run_ood.sh",
    "chars": 4910,
    "preview": "REPRUN=\"../utils/reprun.sh 10\"\nPYCMD=\"python3 main.py\"\n\ncase $1 in\n\tdiscr) # CE\n\t\t# Results taken from \"Conditional adve"
  },
  {
    "path": "code/deep/CSG/a-imageclef/visual.py",
    "chars": 11529,
    "preview": "#!/usr/bin/env python3.6\nimport warnings\nimport sys\nsys.path.append(\"..\")\nfrom utils.utils_main import main_stem, get_pa"
  },
  {
    "path": "code/deep/CSG/a-mnist/main.py",
    "chars": 3897,
    "preview": "#!/usr/bin/env python3.6\nimport warnings\nimport sys\nimport torch as tc\nsys.path.append(\"..\")\nfrom utils.utils_main impor"
  },
  {
    "path": "code/deep/CSG/a-mnist/makedata.py",
    "chars": 2073,
    "preview": "#!/usr/bin/env python3.6\n'''For generating MNIST-01 and its shifted interventional datasets.\n'''\nimport torch as tc\nimpo"
  },
  {
    "path": "code/deep/CSG/a-mnist/makedata.sh",
    "chars": 314,
    "preview": "python3 makedata.py train --pleft 1. 0. --distr randn --loc 5. --scale 1.\npython3 makedata.py test  --pleft .5 .5 --dist"
  },
  {
    "path": "code/deep/CSG/a-mnist/run_da.sh",
    "chars": 1306,
    "preview": "REPRUN=\"../utils/reprun.sh 10\"\nPYCMD=\"python3 main.py\"\nTRAIN=\"--traindom train01_1.0_0.0_randn_5.0_1.0.pt\"\nTEST=\"--testd"
  },
  {
    "path": "code/deep/CSG/a-mnist/run_ood.sh",
    "chars": 1164,
    "preview": "REPRUN=\"../utils/reprun.sh 10\"\nPYCMD=\"python3 main.py\"\nTRAIN=\"--traindom train01_1.0_0.0_randn_5.0_1.0.pt\"\nTEST=\"--testd"
  },
  {
    "path": "code/deep/CSG/arch/__init__.py",
    "chars": 0,
    "preview": ""
  },
  {
    "path": "code/deep/CSG/arch/backbone.py",
    "chars": 6075,
    "preview": "\"\"\" This file is adapted from <https://github.com/thuml/MDD/blob/master/model/backbone.py>\n\"\"\"\nimport numpy as np\nimport"
  },
  {
    "path": "code/deep/CSG/arch/cnn.py",
    "chars": 15657,
    "preview": "#!/usr/bin/env python3.6\n''' CNN Architecture\n\nBased on the architecture in MDD <https://github.com/thuml/MDD>,\nand leve"
  },
  {
    "path": "code/deep/CSG/arch/mlp.py",
    "chars": 10084,
    "preview": "#!/usr/bin/env python3.6\n'''Multi-Layer Perceptron Architecture.\n\nFor causal discriminative model and the corresponding "
  },
  {
    "path": "code/deep/CSG/arch/mlpstru.json",
    "chars": 1125,
    "preview": "{\n    \"MLPsvy1x\": {\n        \"lite\": {\n            \"dims_postx2prev\": [400],\n            \"dim_v\": 100,\n            \"dim_p"
  },
  {
    "path": "code/deep/CSG/distr/__init__.py",
    "chars": 856,
    "preview": "#!/usr/bin/env python3.6\n'''Probabilistic Programming Package.\n\nThe prototype is distributions, which can be a condition"
  },
  {
    "path": "code/deep/CSG/distr/base.py",
    "chars": 14414,
    "preview": "#!/usr/bin/env python3.6\n'''Probabilistic Programming Package.\n\nThe prototype is distributions, which can be a condition"
  },
  {
    "path": "code/deep/CSG/distr/instances.py",
    "chars": 15849,
    "preview": "#!/usr/bin/env python3.6\n'''Probabilistic Programming Package.\n\nThe prototype is distributions, which can be a condition"
  },
  {
    "path": "code/deep/CSG/distr/tools.py",
    "chars": 4511,
    "preview": "#!/usr/bin/env python3.6\n'''Probabilistic Programming Package.\n\nThe prototype is distributions, which can be a condition"
  },
  {
    "path": "code/deep/CSG/distr/utils.py",
    "chars": 8911,
    "preview": "#!/usr/bin/env python3.6\n'''Probabilistic Programming Package.\n\nThe prototype is distributions, which can be a condition"
  },
  {
    "path": "code/deep/CSG/methods/__init__.py",
    "chars": 170,
    "preview": "#!/usr/bin/env python3.6\n__author__ = \"Chang Liu\"\n__email__ = \"changliu@microsoft.com\"\n\nfrom .semvar import SemVar\nfrom "
  },
  {
    "path": "code/deep/CSG/methods/cnbb.py",
    "chars": 2255,
    "preview": "#!/usr/bin/env python3.6\n\"\"\" Implementation of the CNBB method \"ConvNet with Batch Balancing\".\n\nBased on the original de"
  },
  {
    "path": "code/deep/CSG/methods/semvar.py",
    "chars": 14508,
    "preview": "#!/usr/bin/env python3.6\n''' The Semantic-Variation Generative Model.\n\nI.e., the proposed Causal Semantic Generative mod"
  },
  {
    "path": "code/deep/CSG/methods/supvae.py",
    "chars": 8690,
    "preview": "#!/usr/bin/env python3.6\n'''Supervised VAE (no s-v split, the CSGz ablation baseline)\n'''\nimport sys\nimport math\nimport "
  },
  {
    "path": "code/deep/CSG/methods/xdistr.py",
    "chars": 5561,
    "preview": "#!/usr/bin/env python3.6\n\"\"\" Modification to the `distr` package for the structure of the\n    Causal Semantic Generative"
  },
  {
    "path": "code/deep/CSG/requirements.txt",
    "chars": 163,
    "preview": "requirements can be modified often. Please refer to https://github.com/changliu00/causal-semantic-generative-model/blob/"
  },
  {
    "path": "code/deep/CSG/test/distr_test.ipynb",
    "chars": 10218,
    "preview": "{\n \"cells\": [\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n "
  },
  {
    "path": "code/deep/CSG/test/distr_test.py",
    "chars": 2525,
    "preview": "#!/usr/bin/env python3.6\nimport sys\nimport torch as tc\nsys.path.append('..')\nimport distr as ds\nfrom distr.utils import "
  },
  {
    "path": "code/deep/CSG/test/utils_test.py",
    "chars": 989,
    "preview": "#!/usr/bin/env python3.6\nimport sys\nfrom time import time\nimport numpy as np\nsys.path.append('..')\nimport utils\n'''Test "
  },
  {
    "path": "code/deep/CSG/utils/__init__.py",
    "chars": 110,
    "preview": "#!/usr/bin/env python3.6\n__author__ = \"Chang Liu\"\n__email__ = \"changliu@microsoft.com\"\n\nfrom .utils import *\n\n"
  },
  {
    "path": "code/deep/CSG/utils/preprocess/__init__.py",
    "chars": 116,
    "preview": "\"\"\" This module is adapted from Jindong Wang <jindong.wang@microsoft.com>\n    <https://github.com/jindongwang>.\n\"\"\"\n"
  },
  {
    "path": "code/deep/CSG/utils/preprocess/data_list.py",
    "chars": 3281,
    "preview": "#from __future__ import print_function, division\n\nimport torch\nimport numpy as np\nfrom sklearn.preprocessing import Stan"
  },
  {
    "path": "code/deep/CSG/utils/preprocess/data_loader.py",
    "chars": 5938,
    "preview": "# coding=utf-8\nfrom torchvision import datasets, transforms\nimport torch\nfrom torch.utils import data\nimport numpy as np"
  },
  {
    "path": "code/deep/CSG/utils/preprocess/data_provider.py",
    "chars": 2378,
    "preview": "import numpy as np\nfrom .data_list import ImageList\nimport torch.utils.data as util_data\nfrom torchvision import transfo"
  },
  {
    "path": "code/deep/CSG/utils/reprun.sh",
    "chars": 71,
    "preview": "n=$1\ncommand=$2\nshift\nshift\nfor i in $(seq 1 $n)\ndo\n\t$command $*\ndone\n\n"
  },
  {
    "path": "code/deep/CSG/utils/utils.py",
    "chars": 14667,
    "preview": "#!/usr/bin/env python3.6\nimport os\nimport warnings\nfrom itertools import product, chain\nimport math\nimport torch as tc\ni"
  },
  {
    "path": "code/deep/CSG/utils/utils_main.py",
    "chars": 29180,
    "preview": "#!/usr/bin/env python3.6\nimport os, sys\nimport argparse\nfrom copy import deepcopy\nfrom functools import partial\nimport t"
  },
  {
    "path": "code/deep/DAAN/README.md",
    "chars": 2435,
    "preview": "# [Transfer Learning with Dynamic Adversarial Adaptation Network](https://arxiv.org/abs/1909.08184)\n\n## Prerequisites:\n\n"
  },
  {
    "path": "code/deep/DAAN/data_loader.py",
    "chars": 1804,
    "preview": "from torchvision import datasets, transforms\r\nimport torch\r\nimport numpy as np\r\nfrom torchvision import transforms\r\nimpo"
  },
  {
    "path": "code/deep/DAAN/functions.py",
    "chars": 306,
    "preview": "from torch.autograd import Function\n\nclass ReverseLayerF(Function):\n\n    @staticmethod\n    def forward(ctx, x, alpha):\n "
  },
  {
    "path": "code/deep/DAAN/log/tmp-2019-10-27-22-13-51.log",
    "chars": 282704,
    "preview": "\rTrain epoch = 1:   0%|                                  | 0/138 [00:00<?, ?it/s]\rTrain epoch = 1:   1%|▏               "
  },
  {
    "path": "code/deep/DAAN/model/DAAN.py",
    "chars": 3370,
    "preview": "import torch.nn as nn\r\nfrom torch.nn import init\r\nfrom torch.autograd import Variable\r\nfrom functions import ReverseLaye"
  },
  {
    "path": "code/deep/DAAN/model/__init__.py",
    "chars": 15,
    "preview": "from . import *"
  },
  {
    "path": "code/deep/DAAN/model/backbone.py",
    "chars": 5244,
    "preview": "import numpy as np\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torchvision import models\nfrom torch.autog"
  },
  {
    "path": "code/deep/DAAN/scripts/train.sh",
    "chars": 322,
    "preview": "#!/bin/bash\n\nexport CUDA_VISIBLE_DEVICES=1\n\nPROJ_ROOT=\"/home/userxxx/DAAN\"\nPROJ_NAME=\"tmp\"\nLOG_FILE=\"${PROJ_ROOT}/log/${"
  },
  {
    "path": "code/deep/DAAN/train.py",
    "chars": 8818,
    "preview": "from __future__ import print_function\r\nimport argparse\r\nimport torch\r\nimport torch.nn as nn\r\nimport torch.nn.functional "
  },
  {
    "path": "code/deep/DAN/README.md",
    "chars": 1774,
    "preview": "# DAN\nA PyTorch implementation of '[Learning Transferable Features with Deep Adaptation Networks](http://ise.thss.tsingh"
  },
  {
    "path": "code/deep/DANN(RevGrad)/adv_layer.py",
    "chars": 860,
    "preview": "import torch\nimport torch.nn as nn\nfrom torch.autograd import Function\nimport torch.nn.functional as F\n\n\nclass ReverseLa"
  },
  {
    "path": "code/deep/DANN(RevGrad)/readme.md",
    "chars": 198,
    "preview": "# Domain adversarial neural network (DANN/RevGrad)\n\nCode has been rewritten. Please go to https://github.com/jindongwang"
  },
  {
    "path": "code/deep/DDC_DeepCoral/README.md",
    "chars": 169,
    "preview": "# Deep Coral and DDC\n\n\nCode has been rewritten. Please go to https://github.com/jindongwang/transferlearning/tree/master"
  },
  {
    "path": "code/deep/DSAN/README.md",
    "chars": 1802,
    "preview": "# DSAN\nA PyTorch implementation of 'Deep Subdomain Adaptation Network for Image Classification' which has published on I"
  },
  {
    "path": "code/deep/DaNN/DaNN.py",
    "chars": 679,
    "preview": "import torch.nn as nn\n\n\nclass DaNN(nn.Module):\n    def __init__(self, n_input=28 * 28, n_hidden=256, n_class=10):\n      "
  },
  {
    "path": "code/deep/DaNN/data_loader.py",
    "chars": 1139,
    "preview": "import torchvision\nimport torch\nfrom torchvision import datasets,transforms\n\ndef load_data(root_dir,domain,batch_size):\n"
  }
]

// ... and 261 more files (download for full content)

About this extraction

This page contains the full source code of the jindongwang/transferlearning GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 461 files (3.2 MB), approximately 871.2k tokens, and a symbol index with 1793 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!