| 376 | |
| 377 | |
| 378 | class Qwen2VLTemplate(QwenTemplate): |
| 379 | |
| 380 | def replace_tag(self, media_type: Literal['image', 'video', 'audio'], index: int, |
| 381 | example: Dict[str, Any]) -> List[Context]: |
| 382 | assert media_type in {'image', 'video'} |
| 383 | if media_type == 'image': |
| 384 | return ['<|vision_start|><|image_pad|><|vision_end|>'] |
| 385 | else: |
| 386 | return ['<|vision_start|><|video_pad|><|vision_end|>'] |
| 387 | |
| 388 | def replace_object(self, index: int, example: Dict[str, Any]) -> List[Context]: |
| 389 | objects = example.get('objects') |
| 390 | if objects: |
| 391 | object_ = objects[index] |
| 392 | return ['<|object_ref_start|>', object_['caption'], '<|object_ref_end|>'] |
| 393 | else: |
| 394 | return ['<ref-object>'] |
| 395 | |
| 396 | def replace_box(self, index: int, example: Dict[str, Any]) -> List[Context]: |
| 397 | objects = example.get('objects') |
| 398 | if objects: |
| 399 | object_ = objects[index] |
| 400 | if isinstance(object_['bbox'][0], list): |
| 401 | all_objects = '' |
| 402 | for sub_object in object_['bbox']: |
| 403 | all_objects += (f'<|box_start|>({sub_object[0]},{sub_object[1]}),' |
| 404 | f'({sub_object[2]},{sub_object[3]})<|box_end|>') |
| 405 | return [all_objects] |
| 406 | else: |
| 407 | return [ |
| 408 | f'<|box_start|>({object_["bbox"][0]},{object_["bbox"][1]}),' |
| 409 | f'({object_["bbox"][2]},{object_["bbox"][3]})<|box_end|>' |
| 410 | ] |
| 411 | else: |
| 412 | return ['<bbox>'] |
| 413 | |
| 414 | def _encode(self, example: Dict[str, Any]) -> Tuple[Dict[str, Any], Dict[str, Any]]: |
| 415 | inputs, _ = super()._encode(example) |
| 416 | if len(inputs) == 0: |
| 417 | return inputs, {} |
| 418 | processor = self.tokenizer.processor |
| 419 | input_ids = inputs['input_ids'] |
| 420 | labels = inputs['labels'] |
| 421 | images = example.get('images') or [] |
| 422 | videos = example.get('videos') or [] |
| 423 | for media_type in ['images', 'videos']: |
| 424 | if locals()[media_type]: |
| 425 | if media_type == 'images': |
| 426 | images = load_batch(images, _process_image_qwen) |
| 427 | media_token = 151655 |
| 428 | media_inputs = processor.image_processor(images=images, videos=None, return_tensors='pt') |
| 429 | media_grid_thw = media_inputs['image_grid_thw'] |
| 430 | else: |
| 431 | videos = load_batch(videos, load_video_qwen2) |
| 432 | media_inputs = processor.image_processor(images=None, videos=videos, return_tensors='pt') |
| 433 | media_grid_thw = media_inputs['video_grid_thw'] |
| 434 | media_token = 151656 |
| 435 | idx_list = _findall(input_ids, media_token) |
no outgoing calls
no test coverage detected
searching dependent graphs…