1from PIL import Image
2import torch
3from torchvision import transforms
4
5from model import InstructCLIP
6from utils import get_sd_components, normalize
7
8parser = argparse.ArgumentParser(description="Simple example of estimating edit instruction from image pair")
9parser.add_argument(
10 "--pretrained_instructclip_name_or_path",
11 type=str,
12 default="SherryXTChen/Instruct-CLIP",
13 help=(
14 "instructclip pretrained checkpoints"
15 ),
16)
17parser.add_argument(
18 "--pretrained_model_name_or_path",
19 type=str,
20 default="runwayml/stable-diffusion-v1-5",
21 help=(
22 "sd pretrained checkpoints"
23 ),
24)
25parser.add_argument(
26 "--input_path",
27 type=str,
28 default="assets/1_input.jpg",
29 help=(
30 "Input image path"
31 )
32)
33parser.add_argument(
34 "--output_path",
35 type=str,
36 default="assets/1_output.jpg",
37 help=(
38 "Output image path"
39 )
40)
41args = parser.parse_args()
42device = "cuda"
43
44# load model for edit instruction estimation
45model = InstructCLIP.from_pretrained("SherryXTChen/Instruct-CLIP")
46model = model.to(device).eval()
47
48# load model to preprocess/encode image to latent space
49tokenizer, _, vae, _, _ = get_sd_components(args, device, torch.float32)
50
51# prepare image input
52transform = transforms.Compose([
53 transforms.ToTensor(),
54 transforms.Normalize(mean=[0.5], std=[0.5]),
55])
56image_list = [args.input_path, args.output_path]
57image_list = [
58 transform(Image.open(f).resize((512, 512))).unsqueeze(0).to(device)
59 for f in image_list
60]
61
62with torch.no_grad():
63 image_list = [vae.encode(x).latent_dist.sample() * vae.config.scaling_factor for x in image_list]
64
65 # get image feature
66 zero_timesteps = torch.zeros_like(torch.tensor([0])).to(device)
67 img_feat = model.get_image_features(
68 inp=image_list[0], out=image_list[1], inp_t=zero_timesteps, out_t=zero_timesteps)
69 img_feat = normalize(img_feat)
70
71 # get edit instruction
72 pred_instruct_input_ids = model.text_decoder.infer(img_feat[:1])[0]
73 pred_instruct = tokenizer.decode(pred_instruct_input_ids, skip_special_tokens=True)
74 print(pred_instruct) # as a 3 d sculpture
1@misc{chen2025instructclipimprovinginstructionguidedimage,
2 title={Instruct-CLIP: Improving Instruction-Guided Image Editing with Automated Data Refinement Using Contrastive Learning},
3 author={Sherry X. Chen and Misha Sra and Pradeep Sen},
4 year={2025},
5 eprint={2503.18406},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2503.18406},
9}