{"id":13488034,"url":"https://github.com/Con6924/SPM","last_synced_at":"2025-03-27T23:32:36.865Z","repository":{"id":214128454,"uuid":"735771946","full_name":"Con6924/SPM","owner":"Con6924","description":"Official implementation of paper \"One-dimensional Adapter to Rule Them All: Concepts, Diffusion Models and Erasing Applications\".","archived":false,"fork":false,"pushed_at":"2023-12-28T11:33:20.000Z","size":5261,"stargazers_count":124,"open_issues_count":1,"forks_count":11,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-10-30T23:36:22.860Z","etag":null,"topics":["concept-erasing","diffusion-models"],"latest_commit_sha":null,"homepage":"https://lyumengyao.github.io/projects/spm","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Con6924.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-12-26T03:19:16.000Z","updated_at":"2024-10-29T15:32:55.000Z","dependencies_parsed_at":"2024-01-16T09:02:17.385Z","dependency_job_id":null,"html_url":"https://github.com/Con6924/SPM","commit_stats":null,"previous_names":["con6924/spm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Con6924%2FSPM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Con6924%2FSPM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Con6924%2FSPM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Con6924%2FSPM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Con6924","download_url":"https://codeload.github.com/Con6924/SPM/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245944020,"owners_count":20697945,"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":["concept-erasing","diffusion-models"],"created_at":"2024-07-31T18:01:08.477Z","updated_at":"2025-03-27T23:32:31.829Z","avatar_url":"https://github.com/Con6924.png","language":"Python","funding_links":[],"categories":["Remove Concept"],"sub_categories":[],"readme":"# Concept Semi-Permeable Membrane\n\n### ✏️ [Project Page](https://lyumengyao.github.io/projects/spm) | 📄 [arXiv](https://arxiv.org/abs/2312.16145) | 🤗 Hugging Face\n\n![sample](./assets/sample.png)\n\n_(The generation samples demonstrating 'Cat' SPM, which is trained on SD v1.4 and applied on [RealisticVision](https://huggingface.co/SG161222/Realistic\\_Vision\\_V5.1\\_noVAE).\nThe upper row shows the original generation and the lower row shows the SPM-equipped ones.)_\n\nWe propose **Concept \u003cu\u003eS\u003c/u\u003eemi-\u003cu\u003eP\u003c/u\u003eermeable \u003cu\u003eM\u003c/u\u003eembrane (SPM)**, as a solution to erase or edit concepts for diffusion models (DMs).\n\nBriefly, it can achieve two main purposes:\n\n- Prevent the generation of **target concept** from the DMs, while\n- Preserve the generation of **non-target concept** of the DMs.\n\nSPM has following advantages:\n\n- **Data-free**: no extra text or image data is needed for training SPM.\n- **Lightweight**: the trainable parameters of the SPM is only 0.5% of the DM. A SPM for SD v1.4 only takes 1.7MB space for storage.\n- **Customizable**: once obtained, SPMs of different target concept can be equipped simultaneously on the DM according to your needs.\n- **Model-transferable**: SPM trained on certain DM can be directly transfered to other personalized models without additional tuning. e.g. SD v1.4 -\u003e SD v1.5 / [Dreamshaper-8](https://huggingface.co/Lykon/dreamshaper-8) or any other similar community models.\n\n## Getting Started\n\n### 0. Setup\n\nWe use Conda to setup the training environments:\n\n```bash\nconda create -n spm python=3.10\npip install torch torchvision --index-url https://download.pytorch.org/whl/cu118\npip install xformers\npip install -r requirements.txt\n```\n\nAdditionally, you can setup [**SD-WebUI**](https://github.com/AUTOMATIC1111/stable-diffusion-webui) for generation with SPMs as well.\n\n### 1. Training SPMs\n\nIn the [**demo.ipynb**](https://github.com/Con6924/SPM/blob/main/demo.ipynb) notebook we provide tutorials for setting up configs and training. Please refer to the notebook for further details.\n\n### 2. Generate with SPMs\n\nWe provide three approaches to generate images after acquiring SPMs:\n\n#### (Recommended) Generate with our provided code\n\nFirst, you need to setup your generation config. [**configs/generaion.yaml**](https://github.com/Con6924/SPM/blob/main/configs/generaion.yaml) is an example. Then you can generate images by running the following commands:\n\n```shell\npython infer_spm.py \\\n\t\t--config ${generation_config}\t\\\n\t\t--spm_path ${spm_1} ... ${spm_n} \\\n\t\t--base_model ${base_model_path_or_link} \\ \t\t\t\t\t\t\t\t\t\t\t# e.g. CompVis/stable-diffusion-v1-4\n```\n\n#### Generate in the notebook demo\n\nThe [**demo.ipynb**](https://github.com/Con6924/SPM/blob/main/demo.ipynb) notebook also offers codes for generate samples with single or multi SPMs. \n\n*Notice: In this way, the Facilitate Transfer mechanism of SPM will NOT be activated. SPMs will have a relatively higher impact on non-targeted concepts.*\n\n#### Generate with SD-WebUI\n\nThe SPMs can be well adapted to the SD-WebUI for more generation options. You can load SPM as a LoRA module to the desired SD model. \n\n*Notice: In this way, the Facilitate Transfer mechanism of SPM will NOT be activated. SPMs will have a relatively higher impact on non-targeted concepts.*\n\n### 3. Evaluate SPMs\n\nTo validate the provided results in our paper, you can run the following code to evaluate the trained SPMs on the four pre-defined tasks. To check the detailed arguments explaination, just run ``python evaluate_task.py -h`` .\n\n```shell\naccelerate launch --num_processes ${num_gpus} evaluate_task.py \\\n\t\t--task ${task} \\\n\t\t--task_args ${task_args} \\\n\t\t--img_save_path ${img_save_path} \\\n\t\t--save_path ${save_path}\n```\n\n## Model Zoo\n\nTrained SPM for SD v1.x:\n\n| Task Type         | SPM                                                          |\n| ----------------- | ------------------------------------------------------------ |\n| General Concepts  | [Snoopy](https://drive.google.com/file/d/1_dWwFd3OB4ZLjfayPUoaVP3Fi-OGJ3KW/view?usp=drive_link), [Mickey](https://drive.google.com/file/d/1PPAP7kEU7fCc94ZVqln0epRgPup0-xM4/view?usp=drive_link), [Spongebob](https://drive.google.com/file/d/1h13BLLQUThTABBl3gH2DnMYmWJlvPKxV/view?usp=drive_link), [Pikachu](https://drive.google.com/file/d/1Dqon-QvOEBReLPj1cu9vth_F7xAS0quq/view?usp=drive_link), [Donald Duck](https://drive.google.com/file/d/1AMkxh7EUdnM1LA4xVwVNNGymbjGQNlOB/view?usp=drive_link), [Cat](https://drive.google.com/file/d/1mnQFX7HUzx7wIaeNv8ErFOzrWXN2ximA/view?usp=drive_link), \u003cbr /\u003e[Wonder Woman (-\u003eGal Gadot)](https://drive.google.com/file/d/1riAVU11lNeI0aA8CSFsUj3xycRRChMKC/view?usp=drive_link), [Luke Skywalker (-\u003eDarth Vader)](https://drive.google.com/file/d/1SfF-557PfWGqZz2Vi9Fmy21Ygj2rTxaF/view?usp=drive_link), [Joker (-\u003eHeath Ledger)](https://drive.google.com/file/d/1y8UFxy4TT8M-fXfvDyFieSdfIz4sS_ON/view?usp=drive_link), [Joker (-\u003eBatman)](https://drive.google.com/file/d/1zoHUWriLewCiF7mJiwKAoL6rk90ZSPKL/view?usp=drive_link) |\n| Artistic Styles   | [Van Gogh](https://drive.google.com/file/d/1qSvoVDOHZfmUo-NyyKUnIEbcMsdzo_w1/view?usp=drive_link), [Picasso](https://drive.google.com/file/d/1SkEBAdJ1W0Mfd-9JAhl06x2lS4_ekrbz/view?usp=drive_link), [Rembrant](https://drive.google.com/file/d/1lJXdbvlfsKpGhPPc-jMrx7Ukb38Cwk1Q/view?usp=drive_link), [Comic](https://drive.google.com/file/d/1Wqtii81ZAKly8JpHGtGcrI6oiOeRdX_7/view?usp=drive_link)                           |\n| Explicit Contents | [Nudity](https://drive.google.com/file/d/1yJ1Eq9Z326h4zQH5-dmmH7oaOPxjIzvN/view?usp=drive_link)                                                       |\n\nSPM for SD v2.x and SDXL will be released in the future.\n\n## References\n\nThis repo is the code for the paper *One-dimentional Adapter to Rule Them All: Concepts, Diffusion Models and Erasing Applications*.\n\nThanks for the creative ideas of the pioneer researches:\n\n- https://github.com/rohitgandikota/erasing: **Erasing Concepts from Diffusion Models**\n- https://github.com/nupurkmr9/concept-ablation: **Ablating Concepts in Text-to-Image Diffusion Models**\n- https://github.com/clear-nus/selective-amnesia: **Selective Amnesia: A Continual Learning Approach for Forgetting in Deep Generative Models**\n\nIn addition, these repos inspires the implementation of ours:\n\n- https://github.com/p1atdev/LECO: **Low-rank adaptation for Erasing COncepts from diffusion models**\n- https://github.com/cloneofsimo/lora: **Low-rank Adaptation for Fast Text-to-Image Diffusion Fine-tuning**\n- https://github.com/kohya-ss/sd-scripts: **Training, generation and utility scripts for Stable Diffusion** \n\nIf you find this repo useful, you can cite our work as follows:\n\n```tex\n@misc{lyu2023onedimensional,\n      title={One-dimensional Adapter to Rule Them All: Concepts, Diffusion Models and Erasing Applications}, \n      author={Mengyao Lyu and Yuhong Yang and Haiwen Hong and Hui Chen and Xuan Jin and Yuan He and Hui Xue and Jungong Han and Guiguang Ding},\n      year={2023},\n      eprint={2312.16145},\n      archivePrefix={arXiv},\n      primaryClass={cs.CV}\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCon6924%2FSPM","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCon6924%2FSPM","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCon6924%2FSPM/lists"}