Views
No views yet


1import torch
2from PIL import Image
3from transformers import (
4 AutoImageProcessor,
5 AutoTokenizer,
6 AutoModelForCausalLM,
7)
8
9
10model_root = "qihoo360/fg-clip-large"
11image_size=336
12model = AutoModelForCausalLM.from_pretrained(model_root,trust_remote_code=True).cuda()
13
14device = model.device
15
16tokenizer = AutoTokenizer.from_pretrained(model_root)
17image_processor = AutoImageProcessor.from_pretrained(model_root)1
2img_root = "FG-CLIP/use_imgs/cat_dfclor.jpg"
3image = Image.open(img_root).convert("RGB")
4image = image.resize((image_size,image_size))
5
6image_input = image_processor.preprocess(image, return_tensors='pt')['pixel_values'].to(device)
7
8# NOTE Short captions: max_length=77 && walk_short_pos=True
9walk_short_pos = True
10captions=["a photo of a cat", "a photo of a dog"]
11caption_input = torch.tensor(tokenizer(captions, max_length=77, padding="max_length", truncation=True).input_ids, dtype=torch.long, device=device)
12
13# NOTE Long captions: max_length=248 && walk_short_pos=False
14# ......
15
16with torch.no_grad():
17 image_feature = model.get_image_features(image_input)
18 text_feature = model.get_text_features(caption_input,walk_short_pos=walk_short_pos)
19 image_feature = image_feature / image_feature.norm(p=2, dim=-1, keepdim=True)
20 text_feature = text_feature / text_feature.norm(p=2, dim=-1, keepdim=True)
21
22logits_per_image = image_feature @ text_feature.T
23logits_per_image = model.logit_scale.exp() * logits_per_image
24probs = logits_per_image.softmax(dim=1)
25print(probs)
261
2import math
3import matplotlib
4matplotlib.use('Agg')
5import matplotlib.pyplot as plt
6
7
8img_root = "FG-CLIP/use_imgs/cat_dfclor.jpg"
9image = Image.open(img_root).convert("RGB")
10image = image.resize((image_size,image_size))
11
12image_input = image_processor.preprocess(image, return_tensors='pt')['pixel_values'].to(device)
13
14with torch.no_grad():
15 dense_image_feature = model.get_image_dense_features(image_input)
16 captions = ["white cat"]
17 caption_input = torch.tensor(tokenizer(captions, max_length=77, padding="max_length", truncation=True).input_ids, dtype=torch.long, device=device)
18 text_feature = model.get_text_features(caption_input,walk_short_pos=True)
19 text_feature = text_feature / text_feature.norm(p=2, dim=-1, keepdim=True)
20 dense_image_feature = dense_image_feature / dense_image_feature.norm(p=2, dim=-1, keepdim=True)
21
22
23
24similarity = dense_image_feature.squeeze() @ text_feature.squeeze().T
25similarity = similarity.cpu().numpy()
26patch_size = int(math.sqrt(similarity.shape[0]))
27
28
29original_shape = (patch_size, patch_size)
30show_image = similarity.reshape(original_shape)
31
32
33plt.figure(figsize=(6, 6))
34plt.imshow(show_image)
35plt.title('similarity Visualization')
36plt.axis('off')
37plt.savefig("FG-CLIP/use_imgs/FGCLIP_dfcolor_cat.png")
38
@article{xie2025fg,
title={FG-CLIP: Fine-Grained Visual and Textual Alignment},
author={Xie, Chunyu and Wang, Bin and Kong, Fanjing and Li, Jincheng and Liang, Dawei and Zhang, Gengshen and Leng, Dawei and Yin, Yuhui},
journal={arXiv preprint arXiv:2505.05071},
year={2025}
}