MCPcopy Create free account
hub / github.com/apple/ml-4m / decode_text

Function decode_text

fourm/utils/plotting_utils.py:473–510  ·  view source on GitHub ↗

Decodes a text sequence from a model dictionary. Args: mod_dict (dict): Model output dictionary. key (str): Key of the text modality to decode. text_tokenizer (tokenizers.Tokenizer): Text tokenizer.

(mod_dict, key, text_tokenizer)

Source from the content-addressed store, hash-verified

471 return all_metadata
472
473def decode_text(mod_dict, key, text_tokenizer):
474 """
475 Decodes a text sequence from a model dictionary.
476
477 Args:
478 mod_dict (dict): Model output dictionary.
479 key (str): Key of the text modality to decode.
480 text_tokenizer (tokenizers.Tokenizer): Text tokenizer.
481 """
482 input_texts, target_texts, merged_texts = [], [], []
483
484 sentinel_ids = set(get_sentinel_to_id_mapping(text_tokenizer).values())
485 B = mod_dict[key]['tensor'].shape[0]
486
487 for i in range(B):
488
489 input_seq = mod_dict[key]['tensor'][i]
490 input_seq = input_seq[mod_dict[key]['input_mask'][i] == 0]
491 input_seq = input_seq.tolist()
492
493 target_seq = mod_dict[key]['tensor'][i]
494 target_seq = target_seq[mod_dict[key]['target_mask'][i] == 0]
495 target_seq = target_seq.tolist()
496
497 merged_seq = merge_span_masking(input_seq, target_seq, sentinel_ids=sentinel_ids)
498
499 input_text = text_tokenizer.decode(input_seq, skip_special_tokens=False)
500 target_text = text_tokenizer.decode(target_seq, skip_special_tokens=False)
501 merged_text = text_tokenizer.decode(merged_seq, skip_special_tokens=False)
502
503 input_texts.append(input_text)
504 target_texts.append(target_text)
505 merged_texts.append(merged_text)
506
507 if B == 1:
508 input_texts, target_texts, merged_texts = input_texts[0], target_texts[0], merged_texts[0]
509
510 return input_texts, target_texts, merged_texts
511
512def decode_sam_instances(mod_dict, tokenizers, text_tokenizer, key='sam_instance', image_size=224, token_len=16):
513 '''

Callers 5

decode_color_paletteFunction · 0.85
decode_human_posesFunction · 0.85
decode_metadataFunction · 0.85
decode_sam_instancesFunction · 0.85
decode_dictFunction · 0.85

Calls 3

merge_span_maskingFunction · 0.90
decodeMethod · 0.80

Tested by

no test coverage detected