Views
No views yet


1conda install -n chatrex python=3.9
2pip install torch==2.1.2 torchvision==0.16.2 --index-url https://download.pytorch.org/whl/cu121
3git clone https://github.com/IDEA-Research/ChatRex.git
4cd ChatRex
5pip install -v -e .
6# install deformable attention for universal proposal network
7cd chatrex/upn/ops
8pip install -v -e .1mkdir checkpoints
2mkdir checkpoints/upn
3# download UPN checkpoint
4wget -O checkpoints/upn/upn_large.pth https://github.com/IDEA-Research/ChatRex/releases/download/upn-large/upn_large.pthpython tests/test_upn_install.pytests folder.python tests/test_chatrex_install.pyprediction: <obj0> shows a brown dog lying on a bed. The dog is resting comfortably, possibly sleeping, and is positioned on the left side of the bed
1import torch
2from PIL import Image
3from tools.visualize import plot_boxes_to_image
4from chatrex.upn import UPNWrapper
5
6ckpt_path = "checkpoints/upn_checkpoints/upn_large.pth"
7test_image_path = "tests/images/test_upn.jpeg"
8
9model = UPNWrapper(ckpt_path)
10# fine-grained prompt
11fine_grained_proposals = model.inference(
12 test_image_path, prompt_type="fine_grained_prompt"
13)
14# filter by score (default: 0.3) and nms (default: 0.8)
15fine_grained_filtered_proposals = model.filter(
16 fine_grained_proposals, min_score=0.3, nms_value=0.8
17)
18## output is a dict with keys: "original_xyxy_boxes", "scores"
19## - "original_xyxy_boxes": list of boxes in xyxy format in shape (B, N, 4)
20## - "scores": list of scores for each box in shape (B, N)
21
22# coarse-grained prompt
23coarse_grained_proposals = model.inference(
24 test_image_path, prompt_type="coarse_grained_prompt"
25)
26coarse_grained_filtered_proposals = model.filter(
27 coarse_grained_proposals, min_score=0.3, nms_value=0.8
28)
29
30## output is a dict with keys: "original_xyxy_boxes", "scores"
31## - "original_xyxy_boxes": list of boxes in xyxy format in shape (B, N, 4)
32## - "scores": list of scores for each box in shape (B, N)1
2from chatrex.tools.visualize import plot_boxes_to_image
3image = Image.open(test_image_path)
4fine_grained_vis_image, _ = plot_boxes_to_image(
5 image.copy(),
6 fine_grained_filtered_proposals["original_xyxy_boxes"][0],
7 fine_grained_filtered_proposals["scores"][0],
8)
9fine_grained_vis_image.save("tests/test_image_fine_grained.jpeg")
10print(f"fine-grained proposal is saved at tests/test_image_fine_grained.jpeg")
11
12coarse_grained_vis_image, _ = plot_boxes_to_image(
13 image.copy(),
14 coarse_grained_filtered_proposals["original_xyxy_boxes"][0],
15 coarse_grained_filtered_proposals["scores"][0],
16)
17coarse_grained_vis_image.save("tests/test_image_coarse_grained.jpeg")
18print(f"coarse-grained proposal is saved at tests/test_image_coarse_grained.jpeg")
191# Single Object Detection
2Please detect dog in this image. Answer the question with object indexes.
3Please detect the man in yellow shirt in this image. Answer the question with object indexes.
4
5# multiple object detection, use ; to separate the objects
6Please detect person; pigeon in this image. Answer the question with object indexes.
7Please detect person in the car; cat below the table in this image. Answer the question with object indexes.1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
4
5from chatrex.tools.visualize import visualize_chatrex_output
6from chatrex.upn import UPNWrapper
7
8if __name__ == "__main__":
9 # load the processor
10 processor = AutoProcessor.from_pretrained(
11 "IDEA-Research/ChatRex-7B",
12 trust_remote_code=True,
13 device_map="cuda",
14 )
15
16 print(f"loading chatrex model...")
17 # load chatrex model
18 model = AutoModelForCausalLM.from_pretrained(
19 "IDEA-Research/ChatRex-7B",
20 trust_remote_code=True,
21 use_safetensors=True,
22 ).to("cuda")
23
24 # load upn model
25 print(f"loading upn model...")
26 ckpt_path = "checkpoints/upn_checkpoints/upn_large.pth"
27 model_upn = UPNWrapper(ckpt_path)
28 test_image_path = "tests/images/test_chatrex_detection.jpg"
29
30 # get upn predictions
31 fine_grained_proposals = model_upn.inference(
32 test_image_path, prompt_type="fine_grained_prompt"
33 )
34 fine_grained_filtered_proposals = model_upn.filter(
35 fine_grained_proposals, min_score=0.3, nms_value=0.8
36 )
37
38 inputs = processor.process(
39 image=Image.open(test_image_path),
40 question="Please detect person; pigeon in this image. Answer the question with object indexes.",
41 bbox=fine_grained_filtered_proposals["original_xyxy_boxes"][
42 0
43 ], # box in xyxy format
44 )
45
46 inputs = {k: v.to("cuda") for k, v in inputs.items()}
47
48 # perform inference
49 gen_config = GenerationConfig(
50 max_new_tokens=512,
51 do_sample=False,
52 eos_token_id=processor.tokenizer.eos_token_id,
53 pad_token_id=(
54 processor.tokenizer.pad_token_id
55 if processor.tokenizer.pad_token_id is not None
56 else processor.tokenizer.eos_token_id
57 ),
58 )
59 with torch.autocast(device_type="cuda", enabled=True, dtype=torch.bfloat16):
60 prediction = model.generate(
61 inputs, gen_config=gen_config, tokenizer=processor.tokenizer
62 )
63 print(f"prediction:", prediction)
64
65 # visualize the prediction
66 vis_image = visualize_chatrex_output(
67 Image.open(test_image_path),
68 fine_grained_filtered_proposals["original_xyxy_boxes"][0],
69 prediction,
70 font_size=15,
71 draw_width=5,
72 )
73 vis_image.save("tests/test_chatrex_detection.jpeg")
74 print(f"prediction is saved at tests/test_chatrex_detection.jpeg")1<ground>person</ground><objects><obj10><obj14><obj15><obj27><obj28><obj32><obj33><obj35><obj38><obj47><obj50></objects>
2<ground>pigeon</ground><objects><obj0><obj1><obj2><obj3><obj4><obj5><obj6><obj7><obj8><obj9><obj11><obj12><obj13><obj16><obj17><obj18><obj19><obj20><obj21><obj22><obj23><obj24><obj25><obj26><obj29><obj31><obj37><obj39><obj40><obj41><obj44><obj49></objects>
1# Single Object Detection
2## caption in category name
3What is the category name of <obji>? Answer the question with its category name in free format.
4
5## caption in short phrase
6Can you provide me with a short phrase to describe <obji>? Answer the question with a short phrase.
7
8## caption in referring style
9Can you provide me with a brief description of <obji>? Answer the question with brief description.
10
11## caption in one sentence
12Can you provide me with a one sentence of <obji>? Answer the question with one sentence description.
13
14# multiple object detection, use ; to separate the objects1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
4
5from chatrex.tools.visualize import visualize_chatrex_output
6from chatrex.upn import UPNWrapper
7
8if __name__ == "__main__":
9 # load the processor
10 processor = AutoProcessor.from_pretrained(
11 "IDEA-Research/ChatRex-7B",
12 trust_remote_code=True,
13 device_map="cuda",
14 )
15
16 print(f"loading chatrex model...")
17 # load chatrex model
18 model = AutoModelForCausalLM.from_pretrained(
19 "IDEA-Research/ChatRex-7B",
20 trust_remote_code=True,
21 use_safetensors=True,
22 ).to("cuda")
23
24 test_image_path = "tests/images/test_chatrex_install.jpg"
25
26 inputs = processor.process(
27 image=Image.open(test_image_path),
28 question="Can you provide a one sentence description of <obj0> in the image? Answer the question with a one sentence description.",
29 bbox=[[73.88417, 56.62228, 227.69223, 216.34338]],
30 )
31
32 inputs = {k: v.to("cuda") for k, v in inputs.items()}
33
34 # perform inference
35 gen_config = GenerationConfig(
36 max_new_tokens=512,
37 do_sample=False,
38 eos_token_id=processor.tokenizer.eos_token_id,
39 pad_token_id=(
40 processor.tokenizer.pad_token_id
41 if processor.tokenizer.pad_token_id is not None
42 else processor.tokenizer.eos_token_id
43 ),
44 )
45 with torch.autocast(device_type="cuda", enabled=True, dtype=torch.bfloat16):
46 prediction = model.generate(
47 inputs, gen_config=gen_config, tokenizer=processor.tokenizer
48 )
49 print(f"prediction:", prediction)
50
51 # visualize the prediction
52 vis_image = visualize_chatrex_output(
53 Image.open(test_image_path),
54 [[73.88417, 56.62228, 227.69223, 216.34338]],
55 prediction,
56 font_size=15,
57 draw_width=5,
58 )
59 vis_image.save("tests/test_chatrex_region_caption.jpeg")
60 print(f"prediction is saved at tests/test_chatrex_region_caption.jpeg")<ground>A brown dog is lying on a bed, appearing relaxed and comfortable</ground><objects><obj0></objects>
1# Brief Grounded Imager Caption
2Please breifly describe this image in one sentence and detect all the mentioned objects. Answer the question with grounded answer.
3
4# Detailed Grounded Image Caption
5Please provide a detailed description of the image and detect all the mentioned objects. Answer the question with grounded object indexes.1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
4
5from chatrex.tools.visualize import visualize_chatrex_output
6from chatrex.upn import UPNWrapper
7
8if __name__ == "__main__":
9 # load the processor
10 processor = AutoProcessor.from_pretrained(
11 "IDEA-Research/ChatRex-7B",
12 trust_remote_code=True,
13 device_map="cuda",
14 )
15
16 print(f"loading chatrex model...")
17 # load chatrex model
18 model = AutoModelForCausalLM.from_pretrained(
19 "IDEA-Research/ChatRex-7B",
20 trust_remote_code=True,
21 use_safetensors=True,
22 ).to("cuda")
23
24 # load upn model
25 print(f"loading upn model...")
26 ckpt_path = "checkpoints/upn_checkpoints/upn_large.pth"
27 model_upn = UPNWrapper(ckpt_path)
28 test_image_path = "tests/images/test_chatrex_grounded_caption.jpg"
29
30 # get upn predictions
31 fine_grained_proposals = model_upn.inference(
32 test_image_path, prompt_type="fine_grained_prompt"
33 )
34 fine_grained_filtered_proposals = model_upn.filter(
35 fine_grained_proposals, min_score=0.3, nms_value=0.8
36 )
37
38 inputs = processor.process(
39 image=Image.open(test_image_path),
40 question="Please breifly describe this image in one sentence and detect all the mentioned objects. Answer the question with grounded answer.",
41 bbox=fine_grained_filtered_proposals["original_xyxy_boxes"][
42 0
43 ], # box in xyxy format
44 )
45
46 inputs = {k: v.to("cuda") for k, v in inputs.items()}
47
48 # perform inference
49 gen_config = GenerationConfig(
50 max_new_tokens=512,
51 do_sample=False,
52 eos_token_id=processor.tokenizer.eos_token_id,
53 pad_token_id=(
54 processor.tokenizer.pad_token_id
55 if processor.tokenizer.pad_token_id is not None
56 else processor.tokenizer.eos_token_id
57 ),
58 )
59 with torch.autocast(device_type="cuda", enabled=True, dtype=torch.bfloat16):
60 prediction = model.generate(
61 inputs, gen_config=gen_config, tokenizer=processor.tokenizer
62 )
63 print(f"prediction:", prediction)
64
65 # visualize the prediction
66 vis_image = visualize_chatrex_output(
67 Image.open(test_image_path),
68 fine_grained_filtered_proposals["original_xyxy_boxes"][0],
69 prediction,
70 font_size=15,
71 draw_width=5,
72 )
73 vis_image.save("tests/test_chatrex_grounded_image_caption.jpeg")
74 print(f"prediction is saved at tests/test_chatrex_grounded_image_caption.jpeg")The image depicts a cozy living room with a <ground>plaid couch,</ground><objects><obj2></objects> a <ground>wooden TV stand</ground><objects><obj3></objects>holding a <ground>black television,</ground><objects><obj1></objects> a <ground>red armchair,</ground><objects><obj4></objects> and a <ground>whiteboard</ground><objects><obj0></objects>with writing on the wall, accompanied by a <ground>framed poster</ground><objects><obj6></objects>of a <ground>couple.</ground><objects><obj9><obj11></objects>
Answer the question in Grounded format. Question1import torch
2from PIL import Image
3from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
4
5from chatrex.tools.visualize import visualize_chatrex_output
6from chatrex.upn import UPNWrapper
7
8if __name__ == "__main__":
9 # load the processor
10 processor = AutoProcessor.from_pretrained(
11 "IDEA-Research/ChatRex-7B",
12 trust_remote_code=True,
13 device_map="cuda",
14 )
15
16 print(f"loading chatrex model...")
17 # load chatrex model
18 model = AutoModelForCausalLM.from_pretrained(
19 "IDEA-Research/ChatRex-7B",
20 trust_remote_code=True,
21 use_safetensors=True,
22 ).to("cuda")
23
24 # load upn model
25 print(f"loading upn model...")
26 ckpt_path = "checkpoints/upn_checkpoints/upn_large.pth"
27 model_upn = UPNWrapper(ckpt_path)
28 test_image_path = "tests/images/test_grounded_conversation.jpg"
29
30 # get upn predictions
31 fine_grained_proposals = model_upn.inference(
32 test_image_path, prompt_type="coarse_grained_prompt"
33 )
34 fine_grained_filtered_proposals = model_upn.filter(
35 fine_grained_proposals, min_score=0.3, nms_value=0.8
36 )
37
38 inputs = processor.process(
39 image=Image.open(test_image_path),
40 question="Answer the question in grounded format. This is a photo of my room, and can you tell me what kind of person I am? ",
41 bbox=fine_grained_filtered_proposals["original_xyxy_boxes"][
42 0
43 ], # box in xyxy format
44 )
45
46 inputs = {k: v.to("cuda") for k, v in inputs.items()}
47
48 # perform inference
49 gen_config = GenerationConfig(
50 max_new_tokens=512,
51 do_sample=False,
52 eos_token_id=processor.tokenizer.eos_token_id,
53 pad_token_id=(
54 processor.tokenizer.pad_token_id
55 if processor.tokenizer.pad_token_id is not None
56 else processor.tokenizer.eos_token_id
57 ),
58 )
59 with torch.autocast(device_type="cuda", enabled=True, dtype=torch.bfloat16):
60 prediction = model.generate(
61 inputs, gen_config=gen_config, tokenizer=processor.tokenizer
62 )
63 print(f"prediction:", prediction)
64
65 # visualize the prediction
66 vis_image = visualize_chatrex_output(
67 Image.open(test_image_path),
68 fine_grained_filtered_proposals["original_xyxy_boxes"][0],
69 prediction,
70 font_size=30,
71 draw_width=10,
72 )
73 vis_image.save("tests/test_chatrex_grounded_conversation.jpeg")
74 print(f"prediction is saved at tests/test_chatrex_grounded_conversation.jpeg")
75Based on the items in the image, it can be inferred that the <ground>person</ground><objects><obj1></objects> who owns this room has an interest in fitness and possibly enjoys reading. The presence of the <ground>dumbbell</ground><objects><obj2></objects> suggests a commitment to physical activity, while the <ground>book</ground><objects><obj3></objects> indicates a liking for literature or reading. The <ground>sneaker</ground><objects><obj0></objects>s and the <ground>plush toy</ground><objects><obj1></objects> add a personal touch, suggesting that the <ground>person</ground><objects><obj1></objects> might also value comfort and perhaps has a playful or nostalgic side. However, without more context, it is not possible to accurately determine the individual's specific traits or <ground>person</ground><objects><obj1></objects>ality.
@misc{jiang2024chatrextamingmultimodalllm,
title={ChatRex: Taming Multimodal LLM for Joint Perception and Understanding},
author={Qing Jiang and Gen Luo and Yuqin Yang and Yuda Xiong and Yihao Chen and Zhaoyang Zeng and Tianhe Ren and Lei Zhang},
year={2024},
eprint={2411.18363},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2411.18363},
}