{"id":18631010,"url":"https://github.com/aimagelab/safe-clip","last_synced_at":"2025-04-11T06:31:21.009Z","repository":{"id":209512124,"uuid":"724067515","full_name":"aimagelab/safe-clip","owner":"aimagelab","description":"Safe-CLIP: Removing NSFW Concepts from Vision-and-Language Models. ECCV 2024","archived":false,"fork":false,"pushed_at":"2024-08-10T10:51:51.000Z","size":18397,"stargazers_count":58,"open_issues_count":1,"forks_count":0,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-25T10:37:45.759Z","etag":null,"topics":["eccv2024","image-to-text","nsfw","retrieval","safety","text-to-image","trustworthy-ai","vision-and-language"],"latest_commit_sha":null,"homepage":"https://arxiv.org/abs/2311.16254","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/aimagelab.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,"publiccode":null,"codemeta":null}},"created_at":"2023-11-27T10:29:51.000Z","updated_at":"2025-03-24T07:21:13.000Z","dependencies_parsed_at":"2024-03-27T11:25:15.562Z","dependency_job_id":"29d00e24-d59a-4ca1-a41b-641cd6c4355f","html_url":"https://github.com/aimagelab/safe-clip","commit_stats":null,"previous_names":["aimagelab/safe-clip"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aimagelab%2Fsafe-clip","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aimagelab%2Fsafe-clip/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aimagelab%2Fsafe-clip/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aimagelab%2Fsafe-clip/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aimagelab","download_url":"https://codeload.github.com/aimagelab/safe-clip/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248355792,"owners_count":21090084,"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":["eccv2024","image-to-text","nsfw","retrieval","safety","text-to-image","trustworthy-ai","vision-and-language"],"created_at":"2024-11-07T05:05:34.244Z","updated_at":"2025-04-11T06:31:20.558Z","avatar_url":"https://github.com/aimagelab.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003eSafe-CLIP: Removing NSFW Concepts from\u003c/br\u003eVision-and-Language Models\n\n  (ECCV 2024)\n  \u003c/h1\u003e  \n\u003c/div\u003e\n\nThis repository contains the reference code for the paper [**Safe-CLIP: Removing NSFW Concepts from Vision-and-Language Models**](https://arxiv.org/abs/2311.16254).\n\n***Warning:** This project involves explicit sexual content, racially insensitive language, and other material that may be harmful or disturbing to certain users. Please use this content solely for research purposes and proceed with caution.*\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"imgs/safe-clip-figure.png\" alt=\"Safe-CLIP\" width=\"820\" /\u003e\n\u003c/p\u003e \n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1Gz2333WX6U7veCUKYwhXF8dXp4UCeLoU?usp=sharing)\n\n## Table of Contents\n\n1. [Overview](#overview)\n2. [Installation](#installation)\n3. [Text-to-Image](#text-to-image)\n4. [Image-to-Text](#image-to-text)\n5. [Citation](#citation)\n\n## Overview\nSafe-CLIP is an ehnanced vision-and-language model designed to mitigate the risks associated with NSFW (Not Safe For Work) content in AI applications.\n\nBased on the CLIP model, Safe-CLIP is fine-tuned to serve the association between linguistic and visual concepts, ensuring **safer outputs** in text-to-image and image-to-text retrieval and generation tasks.\n\n\n**Useful Links**\n- [🤗 HuggingFace Safe-CLIP Collection](https://huggingface.co/collections/aimagelab/safe-clip-668d0a0ca697b69d66433338)\n  - Models:\n    - [HuggingFace Safe-CLIP-ViT-L-14 model](https://huggingface.co/aimagelab/safeclip_vit-l_14)\n    - [HuggingFace Safe-CLIP-ViT-L-14-336px model](https://huggingface.co/aimagelab/safeclip_vit-l_14_336)\n    - [HuggingFace Safe-CLIP-ViT-H-14 model](https://huggingface.co/aimagelab/safeclip_vit-h_14)\n    - [HuggingFace Safe-CLIP-SD-20 model](https://huggingface.co/aimagelab/safeclip_sd_20)\n  - Dataset:\n    - [HuggingFace ViSU-Text Dataset](https://huggingface.co/datasets/aimagelab/ViSU-Text)\n- [📄 Paper](https://arxiv.org/abs/2311.16254)\n- [🎯 Project Page](https://aimagelab.github.io/safe-clip/)\n\n## Installation\nTo create the conda environment named safe-clip use the following instructions.\nWith this environment you have all the packages to run the code inside this repo. \n```\nconda create -n safe-clip python==3.10\nconda activate safe-clip\npip install -r requirements.txt\n```\n\n## Usage of Safe-CLIP\nSee the snippet below for usage with **Transformers**:\n\n```python\nfrom transformers import CLIPModel\n\nmodel_id = \"aimagelab/safeclip_vit-l_14\"\nmodel = CLIPModel.from_pretrained(model_id)\n```\n\nSee the snippet below for usage with **open_clip**:\n\n```python\nimport open_clip\nimport torch\nfrom huggingface_hub import hf_hub_download\n\nclass QuickGELU(torch.nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.value = torch.tensor(1.702, dtype=torch.float32)\n        \n    def forward(self, x):\n        return x * torch.sigmoid(self.value * x)\n\ndef replace_activation(model):\n    for pt_layer in model.transformer.resblocks:\n        pt_layer.mlp.gelu = QuickGELU()\n    \n    for pt_layer in model.visual.transformer.resblocks:\n        pt_layer.mlp.gelu = QuickGELU()\n\n    return model\n\nfile_path = hf_hub_download(repo_id='aimagelab/safeclip_vit-l_14', filename='open_clip_pytorch_model.bin')\nmodel, train_preprocess_oc, preprocess_oc = open_clip.create_model_and_transforms('ViT-L/14', pretrained=file_path)\nmodel= replace_activation(model)\n``` \n\n\n## Text-to-Image\nSection to perform Text-to-Image generation, starting from a user text input.\nIt is possbile to use the \"safeclip_t2i_generation_pipeline.py\" script from the repository or the google colab notebook interactive demo. \n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1Gz2333WX6U7veCUKYwhXF8dXp4UCeLoU?usp=sharing) \n\nWe provide a code snippet to generate images without NSFW contents.\n\n\n```python\nconda activate safe-clip\n\nout_dir=\"your_path\"\ncd \"$out_dir/safe-clip\"\n\npython -u safeclip_t2i_generation_pipeline.py\n``` \n\n## Image-to-Text\nHere we introduce the Image-to-Text generation code.\n\nThe code snippet use the LLaVA architecture to answer questions to an image.\nThe user can specificy an image `image-file` and a question `query` through the parameter of the script. Moreover, you can choose between two mutually exclusive architectures.\n`paper_model` uses one of the [first architecture of LLaVA](https://huggingface.co/liuhaotian/llava-llama-2-13b-chat-lightning-preview), with Lllama 2 as LLM and the image is processed at the resolution of 224px.\n`llava_15` uses [LLaVA 1.5](https://huggingface.co/liuhaotian/llava-v1.5-13b) with Vicuna as LLM and the image is considered with a resolution of 336px.\n\n```python\nconda activate safe-clip\n\nout_dir=\"your_path\"\ncd \"$out_dir/safe-clip/LLaVA_generation\"\n\npython -u main.py --paper_model\n```\n\n## Training\n[`safeclip_training.py`](https://github.com/aimagelab/safe-clip/blob/main/safeclip_training.py) is the python script that can be used to train Safe-CLIP, and in the [`sh_scripts`](https://github.com/aimagelab/safe-clip/tree/main/training/sh_scripts) folder there are the `.sh` sbatch slurm scripts that have been used to launch the python script.\n\n# Citation\n\nPlease cite with the following BibTeX:\n```\n@inproceedings{poppi2024removing,\n  title={{Safe-CLIP: Removing NSFW Concepts from Vision-and-Language Models}},\n  author={Poppi, Samuele and Poppi, Tobia and Cocchi, Federico and Cornia, Marcella and Baraldi, Lorenzo and Cucchiara, Rita},\n  booktitle={Proceedings of the European Conference on Computer Vision},\n  year={2024}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faimagelab%2Fsafe-clip","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faimagelab%2Fsafe-clip","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faimagelab%2Fsafe-clip/lists"}