Add tiny processor fixture for testing
Browse files- .gitattributes +2 -0
- config.json +152 -0
- generation_config.json +9 -0
- md.py +33 -0
- ocr.py +73 -0
- preprocessor_config.json +5 -0
- tokenizer.json +0 -0
- tokenizer_config.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
output.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
receipt_00008.png filter=lfs diff=lfs merge=lfs -text
|
config.json
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Kosmos2_5ForConditionalGeneration"
|
| 4 |
+
],
|
| 5 |
+
"latent_query_num": 2048,
|
| 6 |
+
"model_type": "kosmos-2.5",
|
| 7 |
+
"text_config": {
|
| 8 |
+
"_name_or_path": "",
|
| 9 |
+
"activation_dropout": 0.0,
|
| 10 |
+
"activation_function": "gelu",
|
| 11 |
+
"add_cross_attention": false,
|
| 12 |
+
"architectures": null,
|
| 13 |
+
"attention_dropout": 0.0,
|
| 14 |
+
"attention_heads": 16,
|
| 15 |
+
"bad_words_ids": null,
|
| 16 |
+
"begin_suppress_tokens": null,
|
| 17 |
+
"bos_token_id": 0,
|
| 18 |
+
"chunk_size_feed_forward": 0,
|
| 19 |
+
"cross_attention_hidden_size": null,
|
| 20 |
+
"decoder_start_token_id": null,
|
| 21 |
+
"dropout": 0,
|
| 22 |
+
"early_stopping": false,
|
| 23 |
+
"embed_dim": 1536,
|
| 24 |
+
"pad_token_id": 1,
|
| 25 |
+
"eos_token_id": 2,
|
| 26 |
+
"exponential_decay_length_penalty": null,
|
| 27 |
+
"ffn_dim": 6144,
|
| 28 |
+
"finetuning_task": null,
|
| 29 |
+
"forced_bos_token_id": null,
|
| 30 |
+
"forced_eos_token_id": null,
|
| 31 |
+
"id2label": {
|
| 32 |
+
"0": "LABEL_0",
|
| 33 |
+
"1": "LABEL_1"
|
| 34 |
+
},
|
| 35 |
+
"init_std": 0.02,
|
| 36 |
+
"is_decoder": false,
|
| 37 |
+
"is_encoder_decoder": false,
|
| 38 |
+
"label2id": {
|
| 39 |
+
"LABEL_0": 0,
|
| 40 |
+
"LABEL_1": 1
|
| 41 |
+
},
|
| 42 |
+
"layer_norm_eps": 1e-05,
|
| 43 |
+
"layerdrop": 0.0,
|
| 44 |
+
"layers": 24,
|
| 45 |
+
"max_length": 20,
|
| 46 |
+
"max_position_embeddings": 4096,
|
| 47 |
+
"min_length": 0,
|
| 48 |
+
"model_type": "kosmos_2_5_text_model",
|
| 49 |
+
"num_return_sequences": 1,
|
| 50 |
+
"output_attentions": false,
|
| 51 |
+
"output_hidden_states": false,
|
| 52 |
+
"output_scores": false,
|
| 53 |
+
"prefix": null,
|
| 54 |
+
"problem_type": null,
|
| 55 |
+
"pruned_heads": {},
|
| 56 |
+
"remove_invalid_values": false,
|
| 57 |
+
"return_dict": true,
|
| 58 |
+
"return_dict_in_generate": false,
|
| 59 |
+
"scale_embedding": true,
|
| 60 |
+
"sep_token_id": null,
|
| 61 |
+
"suppress_tokens": null,
|
| 62 |
+
"task_specific_params": null,
|
| 63 |
+
"tf_legacy_loss": false,
|
| 64 |
+
"tie_encoder_decoder": false,
|
| 65 |
+
"tie_word_embeddings": true,
|
| 66 |
+
"tokenizer_class": null,
|
| 67 |
+
"torch_dtype": null,
|
| 68 |
+
"torchscript": false,
|
| 69 |
+
"use_bfloat16": false,
|
| 70 |
+
"use_cache": true,
|
| 71 |
+
"vocab_size": 108481
|
| 72 |
+
},
|
| 73 |
+
"torch_dtype": "float32",
|
| 74 |
+
"transformers_version": "4.42.0.dev0",
|
| 75 |
+
"vision_config": {
|
| 76 |
+
"_name_or_path": "",
|
| 77 |
+
"add_cross_attention": false,
|
| 78 |
+
"architectures": null,
|
| 79 |
+
"attention_dropout": 0.0,
|
| 80 |
+
"bad_words_ids": null,
|
| 81 |
+
"begin_suppress_tokens": null,
|
| 82 |
+
"bos_token_id": null,
|
| 83 |
+
"chunk_size_feed_forward": 0,
|
| 84 |
+
"cross_attention_hidden_size": null,
|
| 85 |
+
"intermediate_size": 3968,
|
| 86 |
+
"head_dim": 64,
|
| 87 |
+
"decoder_start_token_id": null,
|
| 88 |
+
"dense_act_fn": "gelu_new",
|
| 89 |
+
"diversity_penalty": 0.0,
|
| 90 |
+
"do_sample": false,
|
| 91 |
+
"dropout_rate": 0.0,
|
| 92 |
+
"early_stopping": false,
|
| 93 |
+
"encoder_no_repeat_ngram_size": 0,
|
| 94 |
+
"eos_token_id": null,
|
| 95 |
+
"exponential_decay_length_penalty": null,
|
| 96 |
+
"finetuning_task": null,
|
| 97 |
+
"forced_bos_token_id": null,
|
| 98 |
+
"forced_eos_token_id": null,
|
| 99 |
+
"hidden_size": 1536,
|
| 100 |
+
"id2label": {
|
| 101 |
+
"0": "LABEL_0",
|
| 102 |
+
"1": "LABEL_1"
|
| 103 |
+
},
|
| 104 |
+
"initializer_factor": 1.0,
|
| 105 |
+
"initializer_range": 1e-10,
|
| 106 |
+
"is_decoder": false,
|
| 107 |
+
"is_encoder_decoder": false,
|
| 108 |
+
"label2id": {
|
| 109 |
+
"LABEL_0": 0,
|
| 110 |
+
"LABEL_1": 1
|
| 111 |
+
},
|
| 112 |
+
"layer_norm_eps": 1e-06,
|
| 113 |
+
"length_penalty": 1.0,
|
| 114 |
+
"max_length": 4096,
|
| 115 |
+
"min_length": 0,
|
| 116 |
+
"model_type": "kosmos_2_5_vision_model",
|
| 117 |
+
"no_repeat_ngram_size": 0,
|
| 118 |
+
"num_attention_heads": 24,
|
| 119 |
+
"num_beam_groups": 1,
|
| 120 |
+
"num_beams": 1,
|
| 121 |
+
"num_hidden_layers": 18,
|
| 122 |
+
"num_return_sequences": 1,
|
| 123 |
+
"output_attentions": false,
|
| 124 |
+
"output_hidden_states": false,
|
| 125 |
+
"output_scores": false,
|
| 126 |
+
"pad_token_id": null,
|
| 127 |
+
"patch_embed_hidden_size": 768,
|
| 128 |
+
"prefix": null,
|
| 129 |
+
"problem_type": null,
|
| 130 |
+
"pruned_heads": {},
|
| 131 |
+
"remove_invalid_values": false,
|
| 132 |
+
"repetition_penalty": 1.0,
|
| 133 |
+
"return_dict": true,
|
| 134 |
+
"return_dict_in_generate": false,
|
| 135 |
+
"sep_token_id": null,
|
| 136 |
+
"max_num_patches": 4096,
|
| 137 |
+
"suppress_tokens": null,
|
| 138 |
+
"task_specific_params": null,
|
| 139 |
+
"temperature": 1.0,
|
| 140 |
+
"tf_legacy_loss": false,
|
| 141 |
+
"tie_encoder_decoder": false,
|
| 142 |
+
"tie_word_embeddings": true,
|
| 143 |
+
"tokenizer_class": null,
|
| 144 |
+
"top_k": 50,
|
| 145 |
+
"top_p": 1.0,
|
| 146 |
+
"torch_dtype": null,
|
| 147 |
+
"torchscript": false,
|
| 148 |
+
"typical_p": 1.0,
|
| 149 |
+
"use_bfloat16": false
|
| 150 |
+
},
|
| 151 |
+
"vocab_size": 8704
|
| 152 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": false,
|
| 3 |
+
"bos_token_id": 0,
|
| 4 |
+
"eos_token_id": 2,
|
| 5 |
+
"pad_token_id": 1,
|
| 6 |
+
"transformers_version": "4.42.0.dev0",
|
| 7 |
+
"num_beam" : 1,
|
| 8 |
+
"do_sample": false
|
| 9 |
+
}
|
md.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import torch
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
from transformers import AutoProcessor, Kosmos2_5ForConditionalGeneration
|
| 6 |
+
|
| 7 |
+
repo = "microsoft/kosmos-2.5"
|
| 8 |
+
device = "cuda:0"
|
| 9 |
+
dtype = torch.bfloat16
|
| 10 |
+
model = Kosmos2_5ForConditionalGeneration.from_pretrained(repo, device_map=device, torch_dtype=dtype)
|
| 11 |
+
processor = AutoProcessor.from_pretrained(repo)
|
| 12 |
+
|
| 13 |
+
# sample image
|
| 14 |
+
url = "https://huggingface.co/microsoft/kosmos-2.5/resolve/main/receipt_00008.png"
|
| 15 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 16 |
+
|
| 17 |
+
prompt = "<md>"
|
| 18 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
height, width = inputs.pop("height"), inputs.pop("width")
|
| 21 |
+
raw_width, raw_height = image.size
|
| 22 |
+
scale_height = raw_height / height
|
| 23 |
+
scale_width = raw_width / width
|
| 24 |
+
|
| 25 |
+
inputs = {k: v.to(device) if v is not None else None for k, v in inputs.items()}
|
| 26 |
+
inputs["flattened_patches"] = inputs["flattened_patches"].to(dtype)
|
| 27 |
+
generated_ids = model.generate(
|
| 28 |
+
**inputs,
|
| 29 |
+
max_new_tokens=1024,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 33 |
+
print(generated_text[0])
|
ocr.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import torch
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image, ImageDraw
|
| 5 |
+
from transformers import AutoProcessor, Kosmos2_5ForConditionalGeneration
|
| 6 |
+
|
| 7 |
+
repo = "microsoft/kosmos-2.5"
|
| 8 |
+
device = "cuda:0"
|
| 9 |
+
dtype = torch.bfloat16
|
| 10 |
+
model = Kosmos2_5ForConditionalGeneration.from_pretrained(repo, device_map=device, torch_dtype=dtype)
|
| 11 |
+
processor = AutoProcessor.from_pretrained(repo)
|
| 12 |
+
|
| 13 |
+
# sample image
|
| 14 |
+
url = "https://huggingface.co/microsoft/kosmos-2.5/resolve/main/receipt_00008.png"
|
| 15 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 16 |
+
|
| 17 |
+
# bs = 1
|
| 18 |
+
prompt = "<ocr>"
|
| 19 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 20 |
+
height, width = inputs.pop("height"), inputs.pop("width")
|
| 21 |
+
raw_width, raw_height = image.size
|
| 22 |
+
scale_height = raw_height / height
|
| 23 |
+
scale_width = raw_width / width
|
| 24 |
+
|
| 25 |
+
# bs > 1, batch generation
|
| 26 |
+
# inputs = processor(text=[prompt, prompt], images=[image,image], return_tensors="pt")
|
| 27 |
+
# height, width = inputs.pop("height"), inputs.pop("width")
|
| 28 |
+
# raw_width, raw_height = image.size
|
| 29 |
+
# scale_height = raw_height / height[0]
|
| 30 |
+
# scale_width = raw_width / width[0]
|
| 31 |
+
|
| 32 |
+
inputs = {k: v.to(device) if v is not None else None for k, v in inputs.items()}
|
| 33 |
+
inputs["flattened_patches"] = inputs["flattened_patches"].to(dtype)
|
| 34 |
+
generated_ids = model.generate(
|
| 35 |
+
**inputs,
|
| 36 |
+
max_new_tokens=1024,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
| 40 |
+
def post_process(y, scale_height, scale_width):
|
| 41 |
+
y = y.replace(prompt, "")
|
| 42 |
+
if "<md>" in prompt:
|
| 43 |
+
return y
|
| 44 |
+
pattern = r"<bbox><x_\d+><y_\d+><x_\d+><y_\d+></bbox>"
|
| 45 |
+
bboxs_raw = re.findall(pattern, y)
|
| 46 |
+
lines = re.split(pattern, y)[1:]
|
| 47 |
+
bboxs = [re.findall(r"\d+", i) for i in bboxs_raw]
|
| 48 |
+
bboxs = [[int(j) for j in i] for i in bboxs]
|
| 49 |
+
info = ""
|
| 50 |
+
for i in range(len(lines)):
|
| 51 |
+
box = bboxs[i]
|
| 52 |
+
x0, y0, x1, y1 = box
|
| 53 |
+
if not (x0 >= x1 or y0 >= y1):
|
| 54 |
+
x0 = int(x0 * scale_width)
|
| 55 |
+
y0 = int(y0 * scale_height)
|
| 56 |
+
x1 = int(x1 * scale_width)
|
| 57 |
+
y1 = int(y1 * scale_height)
|
| 58 |
+
info += f"{x0},{y0},{x1},{y0},{x1},{y1},{x0},{y1},{lines[i]}"
|
| 59 |
+
return info
|
| 60 |
+
|
| 61 |
+
output_text = post_process(generated_text[0], scale_height, scale_width)
|
| 62 |
+
print(output_text)
|
| 63 |
+
|
| 64 |
+
draw = ImageDraw.Draw(image)
|
| 65 |
+
lines = output_text.split("\n")
|
| 66 |
+
for line in lines:
|
| 67 |
+
# draw the bounding box
|
| 68 |
+
line = list(line.split(","))
|
| 69 |
+
if len(line) < 8:
|
| 70 |
+
continue
|
| 71 |
+
line = list(map(int, line[:8]))
|
| 72 |
+
draw.polygon(line, outline="red")
|
| 73 |
+
image.save("output.png")
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"image_processor_type": "Kosmos2_5ImageProcessor",
|
| 3 |
+
"processor_class": "Kosmos2_5Processor"
|
| 4 |
+
}
|
| 5 |
+
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|