From ca6720560851f64a597173b4d86ae6db888890b5 Mon Sep 17 00:00:00 2001 From: RainBus Date: Tue, 21 May 2024 19:41:56 +0800 Subject: [PATCH] init --- .gitignore | 162 + Adapter.py | 101 + Average.png | Bin 0 -> 546329 bytes Caltech101.png | Bin 0 -> 394166 bytes DTD.png | Bin 0 -> 384752 bytes EuroSAT.png | Bin 0 -> 338883 bytes FGVCAircraft.png | Bin 0 -> 387739 bytes Flowers102.png | Bin 0 -> 369838 bytes Food101.png | Bin 0 -> 328985 bytes GTP-CLIP_ACC.jpg | Bin 0 -> 61472 bytes ImageNet.png | Bin 0 -> 356042 bytes OxfordPets.png | Bin 0 -> 392845 bytes SUN397.png | Bin 0 -> 402271 bytes StanfordCars.png | Bin 0 -> 419575 bytes UCF101.png | Bin 0 -> 382613 bytes checkpoints/log.txt | 71112 +++++++++++++++++++++++ clip/__init__.py | 1 + clip/bpe_simple_vocab_16e6.txt.gz | Bin 0 -> 1356917 bytes clip/clip.py | 220 + clip/model.py | 436 + clip/simple_tokenizer.py | 134 + clip_make_pic.py | 605 + daln/grl.py | 77 + daln/nwd.py | 25 + data/__init__.py | 0 data/folder_new.py | 140 + data/prepare_data.py | 233 + data/prepare_data_init.py | 233 + data/prepare_data_shot1.py | 233 + data/prepare_data_shot2.py | 233 + data/prepare_data_shot4.py | 233 + data/prepare_data_shot8.py | 233 + datasets/__init__.py | 33 + datasets/caltech101.py | 24 + datasets/caltech101_tsne.py | 24 + datasets/dtd.py | 79 + datasets/eurosat.py | 50 + datasets/fgvc.py | 54 + datasets/food101.py | 24 + datasets/imagenet.py | 221 + datasets/imagenet_sketch.py | 76 + datasets/imagenetv2.py | 82 + datasets/oxford_flowers.py | 67 + datasets/oxford_pets.py | 125 + datasets/stanford_cars.py | 48 + datasets/sun397.py | 50 + datasets/ucf101.py | 51 + datasets/utils.py | 377 + engine/__init__.py | 0 engine/partial_model.py | 424 + gc-clip_make_pic.py | 605 + gpt_file/caltech_prompt.json | 5202 ++ gpt_file/caltech_prompt_chat.json | 789 + gpt_file/dtd_prompt.json | 2446 + gpt_file/eurosat_prompt.json | 122 + gpt_file/fgvc_prompt.json | 5202 ++ gpt_file/food101_prompt.json | 5254 ++ gpt_file/imagenet_prompt.json | 51898 +++++++++++++++++ gpt_file/oxford_flowers_prompt.json | 1226 + gpt_file/oxford_pets_prompt.json | 446 + gpt_file/stanford_cars_prompt.json | 10194 ++++ gpt_file/sun397_prompt.json | 20646 +++++++ gpt_file/ucf101_prompt.json | 5254 ++ images/image_0001.jpg | Bin 0 -> 10067 bytes images/image_0002.jpg | Bin 0 -> 10756 bytes images/image_0003.jpg | Bin 0 -> 22111 bytes images/image_0004.jpg | Bin 0 -> 10946 bytes images/image_0005.jpg | Bin 0 -> 17994 bytes images/image_0006.jpg | Bin 0 -> 13120 bytes images/image_0007.jpg | Bin 0 -> 16736 bytes images/image_0008.jpg | Bin 0 -> 7894 bytes images/image_0009.jpg | Bin 0 -> 6687 bytes images/image_0010.jpg | Bin 0 -> 8488 bytes images/image_0011.jpg | Bin 0 -> 14111 bytes images/image_0012.jpg | Bin 0 -> 14406 bytes images/image_0013.jpg | Bin 0 -> 8903 bytes images/image_0014.jpg | Bin 0 -> 23315 bytes images/image_0015.jpg | Bin 0 -> 36421 bytes images/image_0023.jpg | Bin 0 -> 15446 bytes images/image_0024.jpg | Bin 0 -> 11161 bytes images/image_0025.jpg | Bin 0 -> 8834 bytes images/image_0026.jpg | Bin 0 -> 14397 bytes images/image_0027.jpg | Bin 0 -> 23135 bytes images/image_0028.jpg | Bin 0 -> 13244 bytes images/image_0029.jpg | Bin 0 -> 9678 bytes images/image_0030.jpg | Bin 0 -> 17539 bytes images/image_0031.jpg | Bin 0 -> 11797 bytes images/image_0032.jpg | Bin 0 -> 10591 bytes images/image_0033.jpg | Bin 0 -> 12802 bytes images/image_0034.jpg | Bin 0 -> 11553 bytes images/image_0035.jpg | Bin 0 -> 17228 bytes images/image_0036.jpg | Bin 0 -> 13982 bytes images/image_0037.jpg | Bin 0 -> 8564 bytes images/image_0038.jpg | Bin 0 -> 15975 bytes images/image_0039.jpg | Bin 0 -> 10799 bytes images/image_0040.jpg | Bin 0 -> 10440 bytes images/image_0041.jpg | Bin 0 -> 10453 bytes images/image_0042.jpg | Bin 0 -> 7874 bytes images_2/image_0001.jpg | Bin 0 -> 16209 bytes images_2/image_0002.jpg | Bin 0 -> 12726 bytes images_2/image_0003.jpg | Bin 0 -> 17970 bytes images_2/image_0004.jpg | Bin 0 -> 14017 bytes images_2/image_0005.jpg | Bin 0 -> 22228 bytes images_2/image_0006.jpg | Bin 0 -> 10825 bytes images_2/image_0007.jpg | Bin 0 -> 9218 bytes images_2/image_0008.jpg | Bin 0 -> 12115 bytes images_2/image_0009.jpg | Bin 0 -> 5091 bytes images_2/image_0010.jpg | Bin 0 -> 12469 bytes images_2/image_0011.jpg | Bin 0 -> 7966 bytes images_2/image_0012.jpg | Bin 0 -> 12857 bytes images_2/image_0013.jpg | Bin 0 -> 30467 bytes images_2/image_0014.jpg | Bin 0 -> 15313 bytes images_2/image_0015.jpg | Bin 0 -> 12545 bytes images_2/image_0069.jpg | Bin 0 -> 12270 bytes images_2/image_0070.jpg | Bin 0 -> 16423 bytes images_2/image_0071.jpg | Bin 0 -> 25800 bytes images_2/image_0072.jpg | Bin 0 -> 14511 bytes images_2/image_0073.jpg | Bin 0 -> 10739 bytes images_2/image_0074.jpg | Bin 0 -> 14857 bytes images_2/image_0075.jpg | Bin 0 -> 15549 bytes images_2/image_0076.jpg | Bin 0 -> 15043 bytes images_2/image_0077.jpg | Bin 0 -> 27676 bytes images_2/image_0078.jpg | Bin 0 -> 9976 bytes images_2/image_0079.jpg | Bin 0 -> 8742 bytes images_2/image_0080.jpg | Bin 0 -> 13225 bytes images_2/image_0084.jpg | Bin 0 -> 16611 bytes images_2/image_0085.jpg | Bin 0 -> 14245 bytes images_2/image_0086.jpg | Bin 0 -> 15343 bytes images_2/image_0087.jpg | Bin 0 -> 25292 bytes images_2/image_0088.jpg | Bin 0 -> 11209 bytes images_2/image_0089.jpg | Bin 0 -> 14445 bytes images_2/image_0090.jpg | Bin 0 -> 15674 bytes images_2/image_0091.jpg | Bin 0 -> 16798 bytes images_3/image_0001.jpg | Bin 0 -> 14055 bytes images_3/image_0002.jpg | Bin 0 -> 9498 bytes images_3/image_0003.jpg | Bin 0 -> 19951 bytes images_3/image_0004.jpg | Bin 0 -> 20727 bytes images_3/image_0005.jpg | Bin 0 -> 14567 bytes images_3/image_0006.jpg | Bin 0 -> 26390 bytes images_3/image_0007.jpg | Bin 0 -> 11712 bytes images_3/image_0008.jpg | Bin 0 -> 10113 bytes images_3/image_0009.jpg | Bin 0 -> 10069 bytes images_3/image_0010.jpg | Bin 0 -> 13594 bytes images_3/image_0011.jpg | Bin 0 -> 11299 bytes images_3/image_0012.jpg | Bin 0 -> 18198 bytes images_3/image_0013.jpg | Bin 0 -> 10564 bytes images_3/image_0014.jpg | Bin 0 -> 8613 bytes images_3/image_0015.jpg | Bin 0 -> 12267 bytes images_3/image_0016.jpg | Bin 0 -> 13885 bytes images_3/image_0017.jpg | Bin 0 -> 14289 bytes images_3/image_0018.jpg | Bin 0 -> 13201 bytes images_3/image_0019.jpg | Bin 0 -> 15061 bytes images_3/image_0020.jpg | Bin 0 -> 26274 bytes images_3/image_0021.jpg | Bin 0 -> 26859 bytes images_3/image_0022.jpg | Bin 0 -> 15236 bytes images_3/image_0023.jpg | Bin 0 -> 13917 bytes images_3/image_0024.jpg | Bin 0 -> 19200 bytes images_3/image_0025.jpg | Bin 0 -> 8151 bytes images_3/image_0026.jpg | Bin 0 -> 8466 bytes images_3/image_0027.jpg | Bin 0 -> 14537 bytes images_3/image_0028.jpg | Bin 0 -> 13171 bytes images_3/image_0029.jpg | Bin 0 -> 8112 bytes images_3/image_0030.jpg | Bin 0 -> 12747 bytes images_3/image_0031.jpg | Bin 0 -> 9871 bytes images_3/image_0032.jpg | Bin 0 -> 8511 bytes images_3/image_0033.jpg | Bin 0 -> 14069 bytes images_3/image_0034.jpg | Bin 0 -> 10775 bytes images_3/image_0035.jpg | Bin 0 -> 10711 bytes main.py | 316 + main_1_17.py | 404 + main_1_18.py | 383 + main_DALN.py | 283 + main_cbp.py | 294 + main_coral_loss.py | 373 + main_coral_loss_adapter.py | 354 + main_coral_loss_cross_attention.py | 380 + main_coral_loss_gpt3.py | 480 + main_coral_loss_gpt3_final.py | 457 + main_coral_loss_mixup.py | 373 + main_fusion.py | 265 + main_fusion_adapter.py | 267 + main_fusion_adapter_muti_task.py | 272 + main_fusion_adapter_v2.py | 267 + main_supervised.py | 353 + main_supervised_warm.py | 421 + main_tip_adapter.py | 207 + main_toal_self_supervised.py | 358 + main_toal_self_supervised_noadapter.py | 305 + make_pic.py | 610 + models/CB_Loss.py | 41 + models/DomainClassifierSource.py | 48 + models/DomainClassifierTarget.py | 44 + models/EntropyMinimizationPrinciple.py | 46 + models/SmoothCrossEntropy.py | 12 + models/__init__.py | 0 models/resnet.py | 261 + new_main.py | 421 + new_test.py | 194 + new_trainer.py | 488 + opts.py | 36 + res.py | 59 + run.sh | 1 + test_imagenet.py | 419 + test_t_sne.py | 126 + tip_adapter_ImageNet.py | 611 + trainer.py | 503 + trainer_1_17.py | 507 + trainer_1_18.py | 480 + trainer_self_supervised.py | 545 + trainer_self_supervised_noadapter.py | 531 + trainer_supervised.py | 520 + trainer_supervised_warm.py | 612 + utils/loss_utils.py | 174 + utils/utils.py | 432 + zero_test.py | 462 + zero_test_imagenet.py | 469 + 折线图.py | 36 + 217 files changed, 201004 insertions(+) create mode 100644 .gitignore create mode 100644 Adapter.py create mode 100644 Average.png create mode 100644 Caltech101.png create mode 100644 DTD.png create mode 100644 EuroSAT.png create mode 100644 FGVCAircraft.png create mode 100644 Flowers102.png create mode 100644 Food101.png create mode 100644 GTP-CLIP_ACC.jpg create mode 100644 ImageNet.png create mode 100644 OxfordPets.png create mode 100644 SUN397.png create mode 100644 StanfordCars.png create mode 100644 UCF101.png create mode 100644 checkpoints/log.txt create mode 100644 clip/__init__.py create mode 100644 clip/bpe_simple_vocab_16e6.txt.gz create mode 100644 clip/clip.py create mode 100644 clip/model.py create mode 100644 clip/simple_tokenizer.py create mode 100644 clip_make_pic.py create mode 100644 daln/grl.py create mode 100644 daln/nwd.py create mode 100644 data/__init__.py create mode 100644 data/folder_new.py create mode 100644 data/prepare_data.py create mode 100644 data/prepare_data_init.py create mode 100644 data/prepare_data_shot1.py create mode 100644 data/prepare_data_shot2.py create mode 100644 data/prepare_data_shot4.py create mode 100644 data/prepare_data_shot8.py create mode 100644 datasets/__init__.py create mode 100644 datasets/caltech101.py create mode 100644 datasets/caltech101_tsne.py create mode 100644 datasets/dtd.py create mode 100644 datasets/eurosat.py create mode 100644 datasets/fgvc.py create mode 100644 datasets/food101.py create mode 100644 datasets/imagenet.py create mode 100644 datasets/imagenet_sketch.py create mode 100644 datasets/imagenetv2.py create mode 100644 datasets/oxford_flowers.py create mode 100644 datasets/oxford_pets.py create mode 100644 datasets/stanford_cars.py create mode 100644 datasets/sun397.py create mode 100644 datasets/ucf101.py create mode 100644 datasets/utils.py create mode 100644 engine/__init__.py create mode 100644 engine/partial_model.py create mode 100644 gc-clip_make_pic.py create mode 100644 gpt_file/caltech_prompt.json create mode 100644 gpt_file/caltech_prompt_chat.json create mode 100644 gpt_file/dtd_prompt.json create mode 100644 gpt_file/eurosat_prompt.json create mode 100644 gpt_file/fgvc_prompt.json create mode 100644 gpt_file/food101_prompt.json create mode 100644 gpt_file/imagenet_prompt.json create mode 100644 gpt_file/oxford_flowers_prompt.json create mode 100644 gpt_file/oxford_pets_prompt.json create mode 100644 gpt_file/stanford_cars_prompt.json create mode 100644 gpt_file/sun397_prompt.json create mode 100644 gpt_file/ucf101_prompt.json create mode 100644 images/image_0001.jpg create mode 100644 images/image_0002.jpg create mode 100644 images/image_0003.jpg create mode 100644 images/image_0004.jpg create mode 100644 images/image_0005.jpg create mode 100644 images/image_0006.jpg create mode 100644 images/image_0007.jpg create mode 100644 images/image_0008.jpg create mode 100644 images/image_0009.jpg create mode 100644 images/image_0010.jpg create mode 100644 images/image_0011.jpg create mode 100644 images/image_0012.jpg create mode 100644 images/image_0013.jpg create mode 100644 images/image_0014.jpg create mode 100644 images/image_0015.jpg create mode 100644 images/image_0023.jpg create mode 100644 images/image_0024.jpg create mode 100644 images/image_0025.jpg create mode 100644 images/image_0026.jpg create mode 100644 images/image_0027.jpg create mode 100644 images/image_0028.jpg create mode 100644 images/image_0029.jpg create mode 100644 images/image_0030.jpg create mode 100644 images/image_0031.jpg create mode 100644 images/image_0032.jpg create mode 100644 images/image_0033.jpg create mode 100644 images/image_0034.jpg create mode 100644 images/image_0035.jpg create mode 100644 images/image_0036.jpg create mode 100644 images/image_0037.jpg create mode 100644 images/image_0038.jpg create mode 100644 images/image_0039.jpg create mode 100644 images/image_0040.jpg create mode 100644 images/image_0041.jpg create mode 100644 images/image_0042.jpg create mode 100644 images_2/image_0001.jpg create mode 100644 images_2/image_0002.jpg create mode 100644 images_2/image_0003.jpg create mode 100644 images_2/image_0004.jpg create mode 100644 images_2/image_0005.jpg create mode 100644 images_2/image_0006.jpg create mode 100644 images_2/image_0007.jpg create mode 100644 images_2/image_0008.jpg create mode 100644 images_2/image_0009.jpg create mode 100644 images_2/image_0010.jpg create mode 100644 images_2/image_0011.jpg create mode 100644 images_2/image_0012.jpg create mode 100644 images_2/image_0013.jpg create mode 100644 images_2/image_0014.jpg create mode 100644 images_2/image_0015.jpg create mode 100644 images_2/image_0069.jpg create mode 100644 images_2/image_0070.jpg create mode 100644 images_2/image_0071.jpg create mode 100644 images_2/image_0072.jpg create mode 100644 images_2/image_0073.jpg create mode 100644 images_2/image_0074.jpg create mode 100644 images_2/image_0075.jpg create mode 100644 images_2/image_0076.jpg create mode 100644 images_2/image_0077.jpg create mode 100644 images_2/image_0078.jpg create mode 100644 images_2/image_0079.jpg create mode 100644 images_2/image_0080.jpg create mode 100644 images_2/image_0084.jpg create mode 100644 images_2/image_0085.jpg create mode 100644 images_2/image_0086.jpg create mode 100644 images_2/image_0087.jpg create mode 100644 images_2/image_0088.jpg create mode 100644 images_2/image_0089.jpg create mode 100644 images_2/image_0090.jpg create mode 100644 images_2/image_0091.jpg create mode 100644 images_3/image_0001.jpg create mode 100644 images_3/image_0002.jpg create mode 100644 images_3/image_0003.jpg create mode 100644 images_3/image_0004.jpg create mode 100644 images_3/image_0005.jpg create mode 100644 images_3/image_0006.jpg create mode 100644 images_3/image_0007.jpg create mode 100644 images_3/image_0008.jpg create mode 100644 images_3/image_0009.jpg create mode 100644 images_3/image_0010.jpg create mode 100644 images_3/image_0011.jpg create mode 100644 images_3/image_0012.jpg create mode 100644 images_3/image_0013.jpg create mode 100644 images_3/image_0014.jpg create mode 100644 images_3/image_0015.jpg create mode 100644 images_3/image_0016.jpg create mode 100644 images_3/image_0017.jpg create mode 100644 images_3/image_0018.jpg create mode 100644 images_3/image_0019.jpg create mode 100644 images_3/image_0020.jpg create mode 100644 images_3/image_0021.jpg create mode 100644 images_3/image_0022.jpg create mode 100644 images_3/image_0023.jpg create mode 100644 images_3/image_0024.jpg create mode 100644 images_3/image_0025.jpg create mode 100644 images_3/image_0026.jpg create mode 100644 images_3/image_0027.jpg create mode 100644 images_3/image_0028.jpg create mode 100644 images_3/image_0029.jpg create mode 100644 images_3/image_0030.jpg create mode 100644 images_3/image_0031.jpg create mode 100644 images_3/image_0032.jpg create mode 100644 images_3/image_0033.jpg create mode 100644 images_3/image_0034.jpg create mode 100644 images_3/image_0035.jpg create mode 100644 main.py create mode 100644 main_1_17.py create mode 100644 main_1_18.py create mode 100644 main_DALN.py create mode 100644 main_cbp.py create mode 100644 main_coral_loss.py create mode 100644 main_coral_loss_adapter.py create mode 100644 main_coral_loss_cross_attention.py create mode 100644 main_coral_loss_gpt3.py create mode 100644 main_coral_loss_gpt3_final.py create mode 100644 main_coral_loss_mixup.py create mode 100644 main_fusion.py create mode 100644 main_fusion_adapter.py create mode 100644 main_fusion_adapter_muti_task.py create mode 100644 main_fusion_adapter_v2.py create mode 100644 main_supervised.py create mode 100644 main_supervised_warm.py create mode 100644 main_tip_adapter.py create mode 100644 main_toal_self_supervised.py create mode 100644 main_toal_self_supervised_noadapter.py create mode 100644 make_pic.py create mode 100644 models/CB_Loss.py create mode 100644 models/DomainClassifierSource.py create mode 100644 models/DomainClassifierTarget.py create mode 100644 models/EntropyMinimizationPrinciple.py create mode 100644 models/SmoothCrossEntropy.py create mode 100644 models/__init__.py create mode 100644 models/resnet.py create mode 100644 new_main.py create mode 100644 new_test.py create mode 100644 new_trainer.py create mode 100644 opts.py create mode 100644 res.py create mode 100644 run.sh create mode 100644 test_imagenet.py create mode 100644 test_t_sne.py create mode 100644 tip_adapter_ImageNet.py create mode 100644 trainer.py create mode 100644 trainer_1_17.py create mode 100644 trainer_1_18.py create mode 100644 trainer_self_supervised.py create mode 100644 trainer_self_supervised_noadapter.py create mode 100644 trainer_supervised.py create mode 100644 trainer_supervised_warm.py create mode 100644 utils/loss_utils.py create mode 100644 utils/utils.py create mode 100644 zero_test.py create mode 100644 zero_test_imagenet.py create mode 100644 折线图.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b6caf3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,162 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ diff --git a/Adapter.py b/Adapter.py new file mode 100644 index 0000000..7b8f5e4 --- /dev/null +++ b/Adapter.py @@ -0,0 +1,101 @@ +import torch.nn as nn +from clip import clip +import torch + + + +class Weight_Adapter(nn.Module): + def __init__(self, args, classnames,init_weights): + super().__init__() + self.classnames = classnames + if args.name in ["ViT-B/16", "ViT-B/32"]: + n_input = 512 + elif args.name in ["RN50", "RN50x16"]: + n_input = 1024 + n_output = 2 * len(classnames) + self.linear = nn.Linear(n_input, n_output, bias=False) + self.linear.weight.data = init_weights # Initialize linear layer weights + + def forward(self, x): + x = self.linear(x) + return x +class Classifier(nn.Module): + def __init__(self, args, classnames,init_weights): + super().__init__() + self.classnames = classnames + if args.name in ["ViT-B/16", "ViT-B/32"]: + n_input = 512 + elif args.name in ["RN50", "RN50x16"]: + n_input = 1024 + n_output = len(classnames) + self.linear = nn.Linear(n_input, n_output, bias=False) + self.linear.weight.data = init_weights # Initialize linear layer weights + + def forward(self, x): + x = self.linear(x) + return x + +class Adapter(nn.Module): + def __init__(self, args, classnames,init_weights): + super().__init__() + self.classnames = classnames + if args.name in ["ViT-B/16", "ViT-B/32"]: + n_input = 512 + elif args.name in ["RN50", "RN50x16"]: + n_input = 1024 + n_output = len(classnames) + self.linear = nn.Linear(n_input, n_output, bias=False) + self.linear.weight.data = init_weights # Initialize linear layer weights + + def forward(self, x): + x = self.linear(x) + return x + +class Linear(nn.Module): + def __init__(self, args): + super().__init__() + if args.name in ["ViT-B/16", "ViT-B/32"]: + n_input = 512 + elif args.name in ["RN50", "RN50x16"]: + n_input = 1024 + self.linear = nn.Linear(n_input, n_input, bias=False) + + def forward(self, x): + x = self.linear(x) + return x + +class Res_Adapter(nn.Module): + def __init__(self, n_input, ): + super().__init__() + self.residual_ratio = 0.5 + self.fc = nn.Sequential( + nn.Linear(n_input, n_input // 4, bias=False), + nn.ReLU(inplace=True), + nn.Linear(n_input // 4, n_input, bias=False), + nn.ReLU(inplace=True) + + ) + + def forward(self, x): + a = self.fc(x) + x = self.residual_ratio * a + (1 - self.residual_ratio) * x + + return x + +def all_classifier(classnames, templates, model): + with torch.no_grad(): + zeroshot_weights = [] + for classname in classnames: + classname = classname.replace('_', ' ') + texts = [template.format(classname) for template in templates] # format with class + texts = clip.tokenize(texts).cuda() # tokenizeclip.tokenize向量化文字 + class_embeddings = model.encode_text(texts) # embed with text encoder + class_embeddings /= class_embeddings.norm(dim=-1, keepdim=True) + class_embedding = class_embeddings.mean(dim=0) + class_embedding /= class_embedding.norm() + zeroshot_weights.append(class_embedding) + + zeroshot_weights = torch.stack(zeroshot_weights, dim=1).cuda() + return zeroshot_weights + + diff --git a/Average.png b/Average.png new file mode 100644 index 0000000000000000000000000000000000000000..24f7b50d955c8c6dedca5906a23c36f7671cf07a GIT binary patch literal 546329 zcmeFZhgX%?wl=QGQBSNKr0YSP_rAv__EhaJ21VyTBAPR~| zlU@ZiY<9WQ#rF~{m%8P zR;}8k@Z%}9RjW4S(SQEE7Jp*TKIMjAq@3h0I;q>4Ik{YRFkPj5+3Bjaos+eN(KpVf z4vrRfw&KD^g^wKi=8BWkRmWo@A~yf$F9_Q?n2U_Z#AxF~zPkFOw&SW*dZ+0>tJy++ zII(K=s#OZ7PH4C?K6bc7u=mYLj^66r%KmcwJLcNyz_L!=L+8(**y!iCM(OQ-uS24~ z?CcMX%74~BFIx7qpC7M;-=!~4v3(sslku@`Y>W5oBgd+bF_~Apny;n$bN738n%C7Z zB0DCF#xxXy_kV>4`0xK6eD?IF%71@DzwTwr`||(){ki@B`?qZW{Puq=&OfX1Kex(1 ztMSij{P%F2`)4!!vl;%`46GIS=Pdj$MDb5$_@^@bQyKoL469cCzf>5{r9M`P-Wh%D zn%{|E*0LXCU$yFqQ>W)lhvY=XzBt8Df%A_PLudcGwP$^_nk9#@-i{!d>5Ctq{jr(; z{L1Tx@(Mo-KR!_{Qhxhr z=Z-zzi}nv16gn7Ej@>WU?FneJEfhK(#G^mxM4$B2UgNbc__az|K|x`0p=%}3$4Az4 zzEo9iN#lLMs#U+chK7WkN;7zFR5+63-xDO~l`Uu+D`u*w=e;!by2x!ZZ?@Z?Jy+}5 zWWC%k8kum1IF@O9jo8{%KA z`l(yoefncwPoT8XWbM(Q>5;bbW_4mKufUEYSr-8+u&U&b4*x2^X65&z+|64}Q}k!pC{sgji?5f#b5etqZMW)r8C zW^i6j4S(+AGn`r|jJ4*b=gM~+C_A&qlTE+S`S#8umTbq4(E4Be`SaId+T?cRwD)xs zx&-Z45RjyAQ*`-{-qqh6^<4AYZ-cG z|4gsp)!g5IZ0UJt9CVx8`Q5iXo}XS{%)5H|fx4oyg@wiOskiFs4Jy)1>`I=cg&7ei zMH7parJ0~8+k(VDE$#%5b`+io6}a$F-k;;9${K-aDaZcO15wu7E-Y8l>^uxTxoUHG`&b75LP#q4nGQ9FZAA6vw8I`m)%j_XrSFO@u+}yxM;RQ#4+7Q{~3-jXcLCM@v?gPbRDm)of1H7oT_M zzz<^o;f6GRNsPoIwd3V;r7X<$1eM1t+^dq>haF?6o}hAjmu=p+_mA4~aOyjqU&M>0 zpZCYDEkToY(jq!pSGFGQ@^Gw)5RLnj|GY|*f&0kGgzp2X)Nmhj@_F;-4UR(S8-J1F z9XQV(tgm-T^z;6yH4c*DC|R61dwRGo>Zr)|>5tlVT&q`oKet2L*;>zQuJ3iSc4prE zP?B+XsW1IhLyvKJhC>y9daXoTDT*BD