yuccaaa commited on
Commit
ad719e4
·
verified ·
1 Parent(s): 7b614fa

Upload ms-swift/examples/infer/demo_grounding.py with huggingface_hub

Browse files
ms-swift/examples/infer/demo_grounding.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # pip install git+https://github.com/huggingface/transformers.git # transformers>=4.49
2
+ import os
3
+ import re
4
+ from typing import Literal
5
+
6
+ os.environ['CUDA_VISIBLE_DEVICES'] = '0'
7
+
8
+
9
+ def draw_bbox_qwen2_vl(image, response, norm_bbox: Literal['norm1000', 'none']):
10
+ matches = re.findall(
11
+ r'<\|object_ref_start\|>(.*?)<\|object_ref_end\|><\|box_start\|>\((\d+),(\d+)\),\((\d+),(\d+)\)<\|box_end\|>',
12
+ response)
13
+ ref = []
14
+ bbox = []
15
+ for match_ in matches:
16
+ ref.append(match_[0])
17
+ bbox.append(list(match_[1:]))
18
+ draw_bbox(image, ref, bbox, norm_bbox=norm_bbox)
19
+
20
+
21
+ def infer_grounding():
22
+ from swift.llm import PtEngine, RequestConfig, BaseArguments, InferRequest, safe_snapshot_download
23
+ output_path = 'bbox.png'
24
+ image = load_image('http://modelscope-open.oss-cn-hangzhou.aliyuncs.com/images/animal.png')
25
+ infer_request = InferRequest(messages=[{'role': 'user', 'content': 'Task: Object Detection'}], images=[image])
26
+
27
+ request_config = RequestConfig(max_tokens=512, temperature=0)
28
+ adapter_path = safe_snapshot_download('swift/test_grounding')
29
+ args = BaseArguments.from_pretrained(adapter_path)
30
+
31
+ engine = PtEngine(args.model, adapters=[adapter_path])
32
+ resp_list = engine.infer([infer_request], request_config)
33
+ response = resp_list[0].choices[0].message.content
34
+ print(f'lora-response: {response}')
35
+
36
+ draw_bbox_qwen2_vl(image, response, norm_bbox=args.norm_bbox)
37
+ print(f'output_path: {output_path}')
38
+ image.save(output_path)
39
+
40
+
41
+ if __name__ == '__main__':
42
+ from swift.llm import draw_bbox, load_image
43
+ infer_grounding()