Instructions to use yinasing/Instruct-CLIP with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use yinasing/Instruct-CLIP with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline from diffusers.utils import load_image # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("yinasing/Instruct-CLIP", dtype=torch.bfloat16, device_map="cuda") prompt = "Turn this cat into a dog" input_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/cat.png") image = pipe(image=input_image, prompt=prompt).images[0] - Notebooks
- Google Colab
- Kaggle
Commit ·
26b68e9
0
Parent(s):
Duplicate from SherryXTChen/Instruct-CLIP
Browse filesCo-authored-by: Sherry Chen <SherryXTChen@users.noreply.huggingface.co>
- .gitattributes +35 -0
- README.md +126 -0
- model.safetensors +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
base_model:
|
| 3 |
+
- SherryXTChen/LatentDiffusionDINOv2
|
| 4 |
+
datasets:
|
| 5 |
+
- timbrooks/instructpix2pix-clip-filtered
|
| 6 |
+
- SherryXTChen/InstructCLIP-InstructPix2Pix-Data
|
| 7 |
+
language:
|
| 8 |
+
- en
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
pipeline_tag: image-to-image
|
| 11 |
+
library_name: diffusers
|
| 12 |
+
tags:
|
| 13 |
+
- model_hub_mixin
|
| 14 |
+
- pytorch_model_hub_mixin
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# InstructCLIP: Improving Instruction-Guided Image Editing with Automated Data Refinement Using Contrastive Learning (CVPR 2025)
|
| 18 |
+
|
| 19 |
+
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration.
|
| 20 |
+
The model is based on the paper [Instruct-CLIP: Improving Instruction-Guided Image Editing with Automated Data Refinement Using Contrastive Learning](https://huggingface.co/papers/2503.18406).
|
| 21 |
+
|
| 22 |
+
[Arxiv](http://arxiv.org/abs/2503.18406) | [Image Editing Model](https://huggingface.co/SherryXTChen/InstructCLIP-InstructPix2Pix) | [Data Refinement Model](https://huggingface.co/SherryXTChen/Instruct-CLIP) | [Data](https://huggingface.co/datasets/SherryXTChen/InstructCLIP-InstructPix2Pix-Data)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
## Capabilities
|
| 26 |
+
|
| 27 |
+
<p align="center">
|
| 28 |
+
<img src="https://raw.githubusercontent.com/SherryXTChen/Instruct-CLIP/refs/heads/main/assets/teaser_2.png" alt="Figure 2" width="50%">
|
| 29 |
+
</p>
|
| 30 |
+
|
| 31 |
+
## Installation
|
| 32 |
+
```
|
| 33 |
+
pip install -r requirements.txt
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
## Edit Instruction Refinement Inference
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
from PIL import Image
|
| 40 |
+
import torch
|
| 41 |
+
from torchvision import transforms
|
| 42 |
+
|
| 43 |
+
from model import InstructCLIP
|
| 44 |
+
from utils import get_sd_components, normalize
|
| 45 |
+
|
| 46 |
+
parser = argparse.ArgumentParser(description="Simple example of estimating edit instruction from image pair")
|
| 47 |
+
parser.add_argument(
|
| 48 |
+
"--pretrained_instructclip_name_or_path",
|
| 49 |
+
type=str,
|
| 50 |
+
default="SherryXTChen/Instruct-CLIP",
|
| 51 |
+
help=(
|
| 52 |
+
"instructclip pretrained checkpoints"
|
| 53 |
+
),
|
| 54 |
+
)
|
| 55 |
+
parser.add_argument(
|
| 56 |
+
"--pretrained_model_name_or_path",
|
| 57 |
+
type=str,
|
| 58 |
+
default="runwayml/stable-diffusion-v1-5",
|
| 59 |
+
help=(
|
| 60 |
+
"sd pretrained checkpoints"
|
| 61 |
+
),
|
| 62 |
+
)
|
| 63 |
+
parser.add_argument(
|
| 64 |
+
"--input_path",
|
| 65 |
+
type=str,
|
| 66 |
+
default="assets/1_input.jpg",
|
| 67 |
+
help=(
|
| 68 |
+
"Input image path"
|
| 69 |
+
)
|
| 70 |
+
)
|
| 71 |
+
parser.add_argument(
|
| 72 |
+
"--output_path",
|
| 73 |
+
type=str,
|
| 74 |
+
default="assets/1_output.jpg",
|
| 75 |
+
help=(
|
| 76 |
+
"Output image path"
|
| 77 |
+
)
|
| 78 |
+
)
|
| 79 |
+
args = parser.parse_args()
|
| 80 |
+
device = "cuda"
|
| 81 |
+
|
| 82 |
+
# load model for edit instruction estimation
|
| 83 |
+
model = InstructCLIP.from_pretrained("SherryXTChen/Instruct-CLIP")
|
| 84 |
+
model = model.to(device).eval()
|
| 85 |
+
|
| 86 |
+
# load model to preprocess/encode image to latent space
|
| 87 |
+
tokenizer, _, vae, _, _ = get_sd_components(args, device, torch.float32)
|
| 88 |
+
|
| 89 |
+
# prepare image input
|
| 90 |
+
transform = transforms.Compose([
|
| 91 |
+
transforms.ToTensor(),
|
| 92 |
+
transforms.Normalize(mean=[0.5], std=[0.5]),
|
| 93 |
+
])
|
| 94 |
+
image_list = [args.input_path, args.output_path]
|
| 95 |
+
image_list = [
|
| 96 |
+
transform(Image.open(f).resize((512, 512))).unsqueeze(0).to(device)
|
| 97 |
+
for f in image_list
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
with torch.no_grad():
|
| 101 |
+
image_list = [vae.encode(x).latent_dist.sample() * vae.config.scaling_factor for x in image_list]
|
| 102 |
+
|
| 103 |
+
# get image feature
|
| 104 |
+
zero_timesteps = torch.zeros_like(torch.tensor([0])).to(device)
|
| 105 |
+
img_feat = model.get_image_features(
|
| 106 |
+
inp=image_list[0], out=image_list[1], inp_t=zero_timesteps, out_t=zero_timesteps)
|
| 107 |
+
img_feat = normalize(img_feat)
|
| 108 |
+
|
| 109 |
+
# get edit instruction
|
| 110 |
+
pred_instruct_input_ids = model.text_decoder.infer(img_feat[:1])[0]
|
| 111 |
+
pred_instruct = tokenizer.decode(pred_instruct_input_ids, skip_special_tokens=True)
|
| 112 |
+
print(pred_instruct) # as a 3 d sculpture
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
## Citation
|
| 116 |
+
```bibtex
|
| 117 |
+
@misc{chen2025instructclipimprovinginstructionguidedimage,
|
| 118 |
+
title={Instruct-CLIP: Improving Instruction-Guided Image Editing with Automated Data Refinement Using Contrastive Learning},
|
| 119 |
+
author={Sherry X. Chen and Misha Sra and Pradeep Sen},
|
| 120 |
+
year={2025},
|
| 121 |
+
eprint={2503.18406},
|
| 122 |
+
archivePrefix={arXiv},
|
| 123 |
+
primaryClass={cs.CV},
|
| 124 |
+
url={https://arxiv.org/abs/2503.18406},
|
| 125 |
+
}
|
| 126 |
+
```
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b0bce6162c4e81ebe0f86e17821e99ca68f4cc5aeaa8b8fec3471c98f75b419d
|
| 3 |
+
size 3578069780
|