{"id":13576539,"url":"https://github.com/luogen1996/RepAdapter","last_synced_at":"2025-04-05T08:32:09.813Z","repository":{"id":134126094,"uuid":"569156492","full_name":"luogen1996/RepAdapter","owner":"luogen1996","description":"Official implementation of \"Towards Efficient Visual Adaption via Structural Re-parameterization\".","archived":false,"fork":false,"pushed_at":"2024-04-18T05:18:59.000Z","size":152,"stargazers_count":181,"open_issues_count":8,"forks_count":24,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-04-03T11:12:49.926Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luogen1996.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-11-22T07:52:47.000Z","updated_at":"2025-03-28T10:00:14.000Z","dependencies_parsed_at":"2023-08-20T02:19:59.879Z","dependency_job_id":"0e36e33d-0d45-4f54-949d-619158001d87","html_url":"https://github.com/luogen1996/RepAdapter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luogen1996%2FRepAdapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luogen1996%2FRepAdapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luogen1996%2FRepAdapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luogen1996%2FRepAdapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luogen1996","download_url":"https://codeload.github.com/luogen1996/RepAdapter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247311765,"owners_count":20918337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T15:01:11.230Z","updated_at":"2025-04-05T08:32:09.480Z","avatar_url":"https://github.com/luogen1996.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# RepAdapter\n\nOfficial implementation of \"[Towards Efficient Visual Adaption via Structural Re-parameterization](https://arxiv.org/pdf/2302.08106.pdf)\".\nRepadapter is a parameter-efficient and computationally friendly adapter for giant vision models, which can be seamlessly integrated into most\n vision models via structural re-parameterization. Compared to Full Tuning, RepAdapter saves up to 25% training time, 20% GPU memory, and 94.6% storage cost of ViT-B/16 on VTAB-1k.\n\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"./misc/RepAdapter.jpg\" width=\"1000\"\u003e\n\u003c/p\u003e\n\n## Updates \n- (2023/02/16) Release our RepAdapter project.\n\n## Data Preparation\nWe provide two ways for preparing VTAB-1k:\n- Download the source datasets, please refer to [NOAH](https://github.com/ZhangYuanhan-AI/NOAH/#data-preparation).\n- We provide the prepared datasets, which can be download from  [google drive](https://drive.google.com/file/d/1yZKwiKdsBzTfBgnStRveYMokc7GMMd5p/view?usp=share_link).\n\nAfter that, the file structure should look like:\n```\n$ROOT/data\n|-- cifar\n|-- caltech101\n......\n|-- diabetic_retinopathy\n```\n \n- Download the [pretrained ViT-B/16](https://storage.googleapis.com/vit_models/imagenet21k/ViT-B_16.npz) to `./ViT-B_16.npz`\n\n## Training and Evaluation\n1. Search the hyper-parameter s for RepAdapter (optional)\n```sh \nbash search_repblock.sh\n``` \n\n2. Train RepAdapter\n```sh \nbash train_repblock.sh\n``` \n\n3. Test RepAdapter\n```sh \npython test.py --method repblock --dataset \u003cdataset-name\u003e \n```\n\n## Usage Example\nThe following is a simple example of using RepAdapter to load and train a model\n\n1. Import repadapter.py from module\n```python \nfrom repadapter import set_repadapter, save_repadapter,load_repadapter\n``` \n\n2. Insert RepAdapter layers into all linear layers in the model\n```python\nset_repadapter(model=model)\n``` \nIf you need to train only specific linear layers, you can modify set_repadapter to use regular expressions to match specific names.\n```python\nimport re\nimport torch.nn as nn\ndef set_repadapter(model, pattern):\n    # Compile regular expression patterns\n    regex = re.compile(pattern)\n    for name, module in model.named_modules():\n        # Check if the module is a linear layer and if the name matches a regular expression\n        if isinstance(module, nn.Linear) and regex.match(name):\n```\n\n3. Set the requires_grad attribute of the model parameters.\n- Set the requires_grad attribute of the model parameters as needed to determine which parameters require training.\n```python\ntrainable = []\nfor n, p in model.named_parameters():\n    if any([x in n for x in ['repadapter']]):\n        trainable.append(p)\n        p.requires_grad = True\n    else:\n        p.requires_grad = False\n```\n\n4. Save the checkpoint of RepAdapter\n- After training is completed, generally only the repadapter parameters of the model are saved. This can save a significant amount of disk space, which is one of the advantages of using RepAdapter.\n```python\nimport os\nsave_repadapter(os.path.join(output_dir,\"final.pt\"), model=model)\n```\n\n5. Load the checkpoint of RepAdapter\n- If you need to load a model that has been saved after training, the model needs to execute set_repadapter before loading.\n```python\nload_repadapter(load_path, model=model)\n```\n\n6. Reparameterize the model\n- merge_repadapter is used after model training to simplify the model structure, reducing the model size and inference time.\n- merge_repadapter takes the model and the save path of the repadapter as inputs and performs reparameterization.\n```python\nmerge_repadapter(model,load_path=None,has_loaded=False)\n```\n\n\n## Citation\n\nIf this repository is helpful for your research, or you want to refer the provided results in your paper, consider cite:\n```BibTeX\n@article{luo2023towards,\n  title={Towards Efficient Visual Adaption via Structural Re-parameterization},\n  author={Luo, Gen and Huang, Minglang and Zhou, Yiyi  and Sun, Xiaoshuai and Jiang, Guangnan and Wang, Zhiyu and Ji, Rongrong},\n  journal={arXiv preprint arXiv:2302.08106},\n  year={2023}\n}\n```\n \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluogen1996%2FRepAdapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluogen1996%2FRepAdapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluogen1996%2FRepAdapter/lists"}