{"id":25999902,"url":"https://github.com/Algolzw/daclip-uir","last_synced_at":"2025-03-05T18:42:11.619Z","repository":{"id":197561247,"uuid":"698681007","full_name":"Algolzw/daclip-uir","owner":"Algolzw","description":"PyTorch code for \"Controlling Vision-Language Models for Universal Image Restoration\", ICLR 2024.","archived":false,"fork":false,"pushed_at":"2024-02-28T16:19:07.000Z","size":11026,"stargazers_count":468,"open_issues_count":15,"forks_count":22,"subscribers_count":8,"default_branch":"main","last_synced_at":"2024-02-28T17:36:06.494Z","etag":null,"topics":["deep-learning","diffusion-models","face-inpainting","image-deblurring","image-dehazing","image-denoising","image-deraining","image-desnowing","image-restoration","jpeg-artifacts-removal","low-level-vision","low-light-image-enhancement","prompt","pytorch","shadow-removal","vision-language"],"latest_commit_sha":null,"homepage":"https://algolzw.github.io/daclip-uir","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Algolzw.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,"dei":null}},"created_at":"2023-09-30T16:30:07.000Z","updated_at":"2024-04-15T14:31:55.901Z","dependencies_parsed_at":"2024-04-15T14:42:43.136Z","dependency_job_id":null,"html_url":"https://github.com/Algolzw/daclip-uir","commit_stats":null,"previous_names":["algolzw/daclip-uir"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Algolzw%2Fdaclip-uir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Algolzw%2Fdaclip-uir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Algolzw%2Fdaclip-uir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Algolzw%2Fdaclip-uir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Algolzw","download_url":"https://codeload.github.com/Algolzw/daclip-uir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242083065,"owners_count":20069235,"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":["deep-learning","diffusion-models","face-inpainting","image-deblurring","image-dehazing","image-denoising","image-deraining","image-desnowing","image-restoration","jpeg-artifacts-removal","low-level-vision","low-light-image-enhancement","prompt","pytorch","shadow-removal","vision-language"],"created_at":"2025-03-05T18:40:49.477Z","updated_at":"2025-03-05T18:42:11.607Z","avatar_url":"https://github.com/Algolzw.png","language":"Python","funding_links":[],"categories":["图像恢复","Python"],"sub_categories":["资源传输下载"],"readme":"\n## Controlling Vision-Language Models for Universal Image Restoration \u003cbr\u003e\u003csub\u003eOfficial PyTorch Implementation of DA-CLIP. \u003c/sub\u003e\n\n[Project Page](https://algolzw.github.io/daclip-uir) | [Paper](https://arxiv.org/abs/2310.01018) | [Model Card 🤗](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde)\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/camenduru/daclip-uir-colab/blob/main/daclip_uir_gradio_colab.ipynb) [![Hugging Face](https://img.shields.io/badge/Demo-%F0%9F%A4%97%20Hugging%20Face-blue)](https://huggingface.co/spaces/fffiloni/DA-CLIP) [![Replicate](https://replicate.com/cjwbw/daclip-uir/badge)](https://replicate.com/cjwbw/daclip-uir) \n\n![daclip](figs/teaser.jpg)\n\nOur follow-up work [Photo-Realistic Image Restoration in the Wild with Controlled Vision-Language Models](https://arxiv.org/abs/2404.09732) (CVPRW 2024) presents a [posterior sampling](https://github.com/Algolzw/daclip-uir/blob/74b7851827b485287971300e4b2a56ea0f8f1d75/universal-image-restoration/utils/sde_utils.py#L297) for better image generation and handles real-world mixed-degradation images similar to [Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN).\n\n\n\n### Updates\n[**2024.04.16**] Our follow-up paper \"[Photo-Realistic Image Restoration in the Wild with Controlled Vision-Language Models](https://arxiv.org/abs/2404.09732)\" is on [ArXiv](https://arxiv.org/abs/2404.09732) now! \u003cbr\u003e\n[**2024.04.15**]  Updated a [wild-IR](https://github.com/Algolzw/daclip-uir/tree/main/universal-image-restoration/config/wild-ir) model for real-world degradations and the [posterior sampling](https://github.com/Algolzw/daclip-uir/blob/74b7851827b485287971300e4b2a56ea0f8f1d75/universal-image-restoration/utils/sde_utils.py#L297) for better image generation. The pretrained weights [wild-ir.pth](https://drive.google.com/file/d/1DqHL61gZJI-3EGsXPJrhERrrst7ZR6gP/view?usp=sharing) and [wild-daclip_ViT-L-14.pt](https://drive.google.com/file/d/1eVxgvwzwLl5oWSVIgnA2gycV6ewLEVJd/view?usp=sharing) are also provided for wild-ir. \u003cbr\u003e\n[**2024.01.20**]  🎉🎉🎉 Our DA-CLIP paper was accepted by ICLR 2024 🎉🎉🎉 We further provide a more robust model in the [model card](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/tree/main). \u003cbr\u003e\n[**2023.10.25**] Added [dataset links](https://github.com/Algolzw/daclip-uir#dataset-links) for training and testing. \u003cbr\u003e\n[**2023.10.13**] Added the Replicate [demo](https://replicate.com/cjwbw/daclip-uir) and [api](https://replicate.com/cjwbw/daclip-uir/api)🔥. Thanks to [@chenxwh](https://github.com/chenxwh)!!! We updated the Hugging Face [demo](https://huggingface.co/spaces/fffiloni/DA-CLIP)🔥 and online Colab [demo](https://colab.research.google.com/github/camenduru/daclip-uir-colab/blob/main/daclip_uir_gradio_colab.ipynb)🔥. Thanks to [@fffiloni](https://github.com/fffiloni) and [@camenduru](https://github.com/camenduru) !!! We also made a [Model Card](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde) in Hugging Face 🤗 and provided more [examples](https://drive.google.com/file/d/1C1nmP5kJXzxrULxTMVWF5P30qezqP6kn/view?usp=sharing) for testing.\u003cbr\u003e\n[**2023.10.09**] The **pretrained weights** of DA-CLIP and the Universal IR model are released in [link1](https://drive.google.com/file/d/1A6u4CaVrcpcZckGUNzEXqMF8x_JXsZdX/view?usp=sharing) and [link2](https://drive.google.com/file/d/1eXsyrmAbWOvhIY4Wbt5v4IxaggA5aZMG/view?usp=sharing), respectively. In addition, we also provide a [Gradio](https://gradio.app/) app file for the case that you want to [test your own images](https://github.com/Algolzw/daclip-uir#Gradio).\u003cbr\u003e\n\n\n## How to Run the Code?\n\n\n### Dependencies\n\n* OS: Ubuntu 20.04\n* nvidia:\n\t- cuda: 11.4\n* python 3.8\n\n### Install\n\nWe advise you first create a virtual environment with:\n\n```bash\npython3 -m venv .env\nsource .env/bin/activate\npip install -U pip\npip install -r requirements.txt\n\n```\n\n### DA-CLIP Usage\n\nGet into the `universal-image-restoration` directory and run:\n\n```python\nimport torch\nfrom PIL import Image\nimport open_clip\n\ncheckpoint = 'pretrained/daclip_ViT-B-32.pt'\nmodel, preprocess = open_clip.create_model_from_pretrained('daclip_ViT-B-32', pretrained=checkpoint)\ntokenizer = open_clip.get_tokenizer('ViT-B-32')\n\nimage = preprocess(Image.open(\"haze_01.png\")).unsqueeze(0)\ndegradations = ['motion-blurry','hazy','jpeg-compressed','low-light','noisy','raindrop','rainy','shadowed','snowy','uncompleted']\ntext = tokenizer(degradations)\n\nwith torch.no_grad(), torch.cuda.amp.autocast():\n    text_features = model.encode_text(text)\n    image_features, degra_features = model.encode_image(image, control=True)\n    degra_features /= degra_features.norm(dim=-1, keepdim=True)\n    text_features /= text_features.norm(dim=-1, keepdim=True)\n\n    text_probs = (100.0 * degra_features @ text_features.T).softmax(dim=-1)\n    index = torch.argmax(text_probs[0])\n\nprint(f\"Task: {task_name}: {degradations[index]} - {text_probs[0][index]}\")\n```\n\n### Dataset Preparation\n\nPreparing the train and test datasets following our paper Dataset Construction section as:\n\n```bash\n#### for training dataset ####\n#### (uncompleted means inpainting) ####\ndatasets/universal/train\n|--motion-blurry\n|  |--LQ/*.png\n|  |--GT/*.png\n|--hazy\n|--jpeg-compressed\n|--low-light\n|--noisy\n|--raindrop\n|--rainy\n|--shadowed\n|--snowy\n|--uncompleted\n\n#### for testing dataset ####\n#### (the same structure as train) ####\ndatasets/universal/val\n...\n\n#### for clean captions ####\ndatasets/universal/daclip_train.csv\ndatasets/universal/daclip_val.csv\n```\n\nThen get into the `universal-image-restoration/config/daclip-sde` directory and modify the dataset paths in option files in `options/train.yml` and `options/test.yml`. \n\nYou can add more tasks or datasets to both `train` and `val` directories and add the degradation word to `distortion`.\n\n#### Dataset Links\n\n| Degradation |                                  motion-blurry                                  |                                                 hazy                                                 |                                                  jpeg-compressed*                                                  |                                   low-light                                   |                                                noisy* (same to jpeg)                                               |\n|-------------|:-------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------:|\n| Datasets    | [Gopro](https://drive.google.com/file/d/1y4wvPdOG3mojpFCHTqLgriexhbjoWVkK/view) | [RESIDE-6k](https://drive.google.com/drive/folders/1XVD0x74vKQ0-cqazACUZnjUOWURXIeqH?usp=drive_link) | [DIV2K](https://cv.snu.ac.kr/research/EDSR/DIV2K.tar)+[Flickr2K](https://cv.snu.ac.kr/research/EDSR/Flickr2K.tar) | [LOL](https://drive.google.com/file/d/157bjO1_cFuSd0HWDUuAmcHRJDVyWpOxB/view) | [DIV2K](https://cv.snu.ac.kr/research/EDSR/DIV2K.tar)+[Flickr2K](https://cv.snu.ac.kr/research/EDSR/Flickr2K.tar) | \n\n| Degradation |                                    raindrop                                    |                              rainy                             |                                    shadowed                                   |                                                                                         snowy                                                                                         |                                      uncompleted                                      |\n|-------------|:-------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------:|:-----------------------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------:|\n| Datasets    | [RainDrop](https://drive.google.com/open?id=1e7R76s6vwUJxILOcAsthgDLPSnOrQ49K) | Rain100H: [train](http://www.icst.pku.edu.cn/struct/att/RainTrainH.zip), [test](http://www.icst.pku.edu.cn/struct/att/Rain100H.zip) | [SRD](https://drive.google.com/file/d/1W8vBRJYDG9imMgr9I2XaA13tlFIEHOjS/view) | [Snow100K](https://www.google.com/url?q=https%3A%2F%2Fdesnownet.s3.amazonaws.com%2Fdataset_synthetic%2Ftrain%2FSnow100K-training.tar.gz\u0026sa=D\u0026sntz=1\u0026usg=AOvVaw1Zj_7kQaF0c26DaZcoKEOr) | [CelebaHQ-256](https://drive.google.com/file/d/1oYDBcJLT5RDuC4k5C7xOMRkZ9N3kfexu/view?usp=sharing) |\n\nYou should **only extract the train datasets for training**, and all **validation datasets** can be downloaded in the [Google drive](https://drive.google.com/file/d/1JKd1tA7rMoEbI9190daJqL7i6V1L8KUd/view?usp=sharing). For jpeg and noisy datasets, you can generate LQ images using this [script](https://github.com/Algolzw/daclip-uir/blob/main/scripts/generate_LQ.py).\n\n### Training\n\n#### DA-CLIP \nSee [DA-CLIP.md](da-clip/README.md) for details.\n\n#### Universal Image Restoration\nThe main code for training is in `universal-image-restoration/config/daclip-sde` and the core network for DA-CLIP is in `universal-image-restoration/open_clip/daclip_model.py`.\n\n* Put the pretrained [**DA-CLIP weights**](https://drive.google.com/file/d/1A6u4CaVrcpcZckGUNzEXqMF8x_JXsZdX/view?usp=sharing) to `pretrained` directory and check the `daclip` path.\n\n* You can then train the model following below bash scripts:\n\n```bash\ncd universal-image-restoration/config/daclip-sde\n\n# For single GPU:\npython3 train.py -opt=options/train.yml\n\n# For distributed training, need to change the gpu_ids in option file\npython3 -m torch.distributed.launch --nproc_per_node=2 --master_port=4321 train.py -opt=options/train.yml --launcher pytorch\n```\n\nThe models and training logs will save in `log/universal-ir`. \nYou can print your log at time by running `tail -f log/universal-ir/train_universal-ir_***.log -n 100`.\n\n**The same training steps can be used for image restoration in the wild ([wild-ir](https://github.com/Algolzw/daclip-uir/tree/main/universal-image-restoration/config/wild-ir)).**\n\n#### Pretrained Models\n| Model Name   | Description                                     | GoogleDrive                                                                                   | HuggingFace                                                                                      |\n|--------------|-------------------------------------------------|------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|\n| DA-CLIP      | Degradation-aware CLIP model                    | [download](https://drive.google.com/file/d/1A6u4CaVrcpcZckGUNzEXqMF8x_JXsZdX/view?usp=sharing) | [download](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/blob/main/daclip_ViT-B-32.pt) |\n| Universal-IR | DA-CLIP based universal image restoration model | [download](https://drive.google.com/file/d/1eXsyrmAbWOvhIY4Wbt5v4IxaggA5aZMG/view?usp=sharing) | [download](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/blob/main/universal-ir.pth)   |\n| DA-CLIP-mix      | Degradation-aware CLIP model (add Gaussian blur + face inpainting and Gaussian blur + Rainy)                   | [download](https://drive.google.com/file/d/12KZK9Apx74pU23OGG9YSybd6gzN_PuGU/view?usp=sharing) | [download](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/blob/main/daclip_ViT-B-32_mix.pt) |\n| Universal-IR-mix | DA-CLIP based universal image restoration model (add robust training and mix-degradations) | [download](https://drive.google.com/file/d/1HRgsEUCw4_m6CC_XYpJGHNjuIMd1jAcm/view?usp=sharing) | [download](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/blob/main/universal-ir-mix.pth)   |\n| Wild-DA-CLIP      | Degradation-aware CLIP model in the wild (ViT-L-14)                | [download](https://drive.google.com/file/d/1eVxgvwzwLl5oWSVIgnA2gycV6ewLEVJd/view?usp=sharing) | [download](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/blob/main/wild-daclip_ViT-L-14.pt) |\n| Wild-IR | DA-CLIP based image restoration model in the wild | [download](https://drive.google.com/file/d/1DqHL61gZJI-3EGsXPJrhERrrst7ZR6gP/view?usp=sharing) | [download](https://huggingface.co/weblzw/daclip-uir-ViT-B-32-irsde/blob/main/wild-ir.pth)   |\n\n### Evaluation\nTo evalute our method on image restoration, please modify the benchmark path and model path and run\n\n```bash\ncd universal-image-restoration/config/universal-ir\npython test.py -opt=options/test.yml\n```\n\n### Gradio\nHere we provide an [app.py](https://github.com/Algolzw/daclip-uir/tree/main/universal-image-restoration/config/daclip-sde/app.py) file for testing your own images. Before that, you need to download the pretrained weights ([DA-CLIP](https://drive.google.com/file/d/1A6u4CaVrcpcZckGUNzEXqMF8x_JXsZdX/view?usp=sharing) and [UIR](https://drive.google.com/file/d/1eXsyrmAbWOvhIY4Wbt5v4IxaggA5aZMG/view?usp=sharing)) and modify the model path in `options/test.yml`. Then by simply running `python app.py`, you can open `http://localhost:7860` to test the model. (We also provide several images with different degradations in the `images` dir). We also provide more examples from our test dataset in the [google drive](https://drive.google.com/file/d/1C1nmP5kJXzxrULxTMVWF5P30qezqP6kn/view?usp=sharing).\n\n**The same steps can be used for image restoration in the wild ([wild-ir](https://github.com/Algolzw/daclip-uir/tree/main/universal-image-restoration/config/wild-ir)).**\n\n\n### Results\n\n![daclip](figs/UIR_results_radar.jpg)\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eUnified Image Restoration\u003c/strong\u003e (click to expand) \u003c/summary\u003e\n\n![daclip](figs/results-UIR.jpg)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eDegradation-Specific Restoration\u003c/strong\u003e (click to expand) \u003c/summary\u003e\n\n![daclip](figs/results_single.jpg)\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eImage Restoration in the wild\u003c/strong\u003e (click to expand) \u003c/summary\u003e\n\n![daclip](figs/results-wildir.jpg)\n\n\u003c/details\u003e\n\n\n#### Notice!!\n🙁 In testing we found that the current pretrained model is still difficult to process some real-world images  which might have distribution shifts with our training dataset (captured from different devices or with different resolutions or degradations). We regard it as a future work and will try to make our model more practical! We also encourage users who are interested in our work to train their own models with larger dataset and more degradation types.\n\n🙁 BTW, **we also found that directly resizing input images will lead a poor performance for most tasks**. We could try to add the resize step into the training but it always destroys the image quality due to interpolation.\n\n🙁 For the inpainting task our current model only supports face inpainting due to the [dataset limitation](https://github.com/Algolzw/daclip-uir/issues/8#issuecomment-1759528246). We provide our mask [examples](https://github.com/Algolzw/daclip-uir/tree/main/scripts/inpainting_masks) and you can use the [generate\\_masked\\_face](https://github.com/Algolzw/daclip-uir/blob/main/scripts/generate_masked_face.py) script to generate uncompleted faces.\n\n\n---\n\n**Acknowledgment:** Our DA-CLIP is based on [IR-SDE](https://github.com/Algolzw/image-restoration-sde) and [open_clip](https://github.com/mlfoundations/open_clip). Thanks for their code!\n\n#### Contact\nIf you have any question, please contact: ziwei.luo@it.uu.se\n\n\n### Citations\nIf our code helps your research or work, please consider citing our paper.\nThe following are BibTeX references:\n\n```\n@article{luo2023controlling,\n  title={Controlling Vision-Language Models for Universal Image Restoration},\n  author={Luo, Ziwei and Gustafsson, Fredrik K and Zhao, Zheng and Sj{\\\"o}lund, Jens and Sch{\\\"o}n, Thomas B},\n  journal={arXiv preprint arXiv:2310.01018},\n  year={2023}\n}\n\n@article{luo2024photo,\n  title={Photo-Realistic Image Restoration in the Wild with Controlled Vision-Language Models},\n  author={Luo, Ziwei and Gustafsson, Fredrik K and Zhao, Zheng and Sj{\\\"o}lund, Jens and Sch{\\\"o}n, Thomas B},\n  journal={arXiv preprint arXiv:2404.09732},\n  year={2024}\n}\n```\n\n---\n\n\n#### --- Thanks for your interest! --- ####\n\n\u003cdetails\u003e\n\u003csummary\u003estatistics\u003c/summary\u003e\n\n![visitors](https://visitor-badge.laobi.icu/badge?page_id=Algolzw/daclip-uir)\n\n\u003c/details\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAlgolzw%2Fdaclip-uir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAlgolzw%2Fdaclip-uir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAlgolzw%2Fdaclip-uir/lists"}