ucsahin commited on
Commit
5bce316
·
verified ·
1 Parent(s): 93306a5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +70 -0
README.md CHANGED
@@ -32,6 +32,76 @@ The following Colab notebook showcases how you can finetune the model with your
32
  - This model is a multimodal language model fine-tuned for the task of detecting tables in images given textual prompts. The model utilizes a combination of image and text inputs to predict bounding boxes around tables within the provided images.
33
  - The primary purpose of this model is to assist in automating the process of table detection within images. It can be utilized in various applications such as document processing, data extraction, and image analysis, where identifying tables within images is essential.
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  ### Training hyperparameters
37
 
 
32
  - This model is a multimodal language model fine-tuned for the task of detecting tables in images given textual prompts. The model utilizes a combination of image and text inputs to predict bounding boxes around tables within the provided images.
33
  - The primary purpose of this model is to assist in automating the process of table detection within images. It can be utilized in various applications such as document processing, data extraction, and image analysis, where identifying tables within images is essential.
34
 
35
+ ## How to Get Started with the Model
36
+
37
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
38
+ In Transformers, you can load the model as follows:
39
+
40
+ ```python
41
+ from transformers import AutoProcessor, AutoModelForCausalLM
42
+ import matplotlib.pyplot as plt
43
+ import matplotlib.patches as patches
44
+
45
+ model_id = "ucsahin/Florence-2-large-TableDetection"
46
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, device_map="cuda") # load the model on GPU
47
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
48
+
49
+ def run_example(task_prompt, image, max_new_tokens=128):
50
+ prompt = task_prompt
51
+ inputs = processor(text=prompt, images=image, return_tensors="pt")
52
+ generated_ids = model.generate(
53
+ input_ids=inputs["input_ids"].cuda(),
54
+ pixel_values=inputs["pixel_values"].cuda(),
55
+ max_new_tokens=max_new_tokens,
56
+ early_stopping=False,
57
+ do_sample=False,
58
+ num_beams=3,
59
+ )
60
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
61
+ parsed_answer = processor.post_process_generation(
62
+ generated_text,
63
+ task=task_prompt,
64
+ image_size=(image.width, image.height)
65
+ )
66
+ return parsed_answer
67
+
68
+ def plot_bbox(image, data):
69
+ # Create a figure and axes
70
+ fig, ax = plt.subplots()
71
+
72
+ # Display the image
73
+ ax.imshow(image)
74
+
75
+ # Plot each bounding box
76
+ for bbox, label in zip(data['bboxes'], data['labels']):
77
+ # Unpack the bounding box coordinates
78
+ x1, y1, x2, y2 = bbox
79
+ # Create a Rectangle patch
80
+ rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=1, edgecolor='r', facecolor='none')
81
+ # Add the rectangle to the Axes
82
+ ax.add_patch(rect)
83
+ # Annotate the label
84
+ plt.text(x1, y1, label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
85
+
86
+ # Remove the axis ticks and labels
87
+ ax.axis('off')
88
+
89
+ # Show the plot
90
+ plt.show()
91
+
92
+ ########### Inference
93
+ from datasets import load_dataset
94
+
95
+ dataset = load_dataset("ucsahin/pubtables-detection-1500-samples")
96
+
97
+ example_id = 5
98
+ image = dataset["train"][example_id]["image"]
99
+
100
+ parsed_answer = run_example("<OD>", image=image)
101
+
102
+ plot_bbox(image, parsed_answer["<OD>"])
103
+ ```
104
+
105
 
106
  ### Training hyperparameters
107