Update README.md
Browse files
README.md
CHANGED
|
@@ -19,60 +19,60 @@ It achieves the following results on the evaluation set:
|
|
| 19 |
- Loss: 0.2107
|
| 20 |
|
| 21 |
### Inference Code versions
|
| 22 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 23 |
-
import matplotlib.pyplot as plt
|
| 24 |
-
import matplotlib.patches as patches
|
| 25 |
-
|
| 26 |
-
model = AutoModelForCausalLM.from_pretrained("Musa07/Florence-2-large-FormClassification-ft", trust_remote_code=True, device_map='cuda') # Load the model on GPU if available
|
| 27 |
-
processor = AutoProcessor.from_pretrained("Musa07/Florence-2-large-FormClassification-ft", trust_remote_code=True)
|
| 28 |
-
|
| 29 |
-
def run_example(task_prompt, image, max_new_tokens=128):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def plot_bbox(image, data):
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
image = Image.open('1.jpeg')
|
| 73 |
-
parsed_answer = run_example("<OD>", image=image)
|
| 74 |
-
print(parsed_answer)
|
| 75 |
-
plot_bbox(image, parsed_answer["<OD>"])
|
| 76 |
|
| 77 |
|
| 78 |
|
|
|
|
| 19 |
- Loss: 0.2107
|
| 20 |
|
| 21 |
### Inference Code versions
|
| 22 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 23 |
+
import matplotlib.pyplot as plt
|
| 24 |
+
import matplotlib.patches as patches
|
| 25 |
+
|
| 26 |
+
model = AutoModelForCausalLM.from_pretrained("Musa07/Florence-2-large-FormClassification-ft", trust_remote_code=True, device_map='cuda') # Load the model on GPU if available
|
| 27 |
+
processor = AutoProcessor.from_pretrained("Musa07/Florence-2-large-FormClassification-ft", trust_remote_code=True)
|
| 28 |
+
|
| 29 |
+
def run_example(task_prompt, image, max_new_tokens=128):
|
| 30 |
+
prompt = task_prompt
|
| 31 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 32 |
+
generated_ids = model.generate(
|
| 33 |
+
input_ids=inputs["input_ids"].cuda(),
|
| 34 |
+
pixel_values=inputs["pixel_values"].cuda(),
|
| 35 |
+
max_new_tokens=max_new_tokens,
|
| 36 |
+
early_stopping=False,
|
| 37 |
+
do_sample=False,
|
| 38 |
+
num_beams=3,
|
| 39 |
+
)
|
| 40 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 41 |
+
parsed_answer = processor.post_process_generation(
|
| 42 |
+
generated_text,
|
| 43 |
+
task=task_prompt,
|
| 44 |
+
image_size=(image.width, image.height)
|
| 45 |
+
)
|
| 46 |
+
return parsed_answer
|
| 47 |
+
|
| 48 |
+
def plot_bbox(image, data):
|
| 49 |
+
|
| 50 |
+
fig, ax = plt.subplots()
|
| 51 |
+
|
| 52 |
+
# Display the image
|
| 53 |
+
ax.imshow(image)
|
| 54 |
+
|
| 55 |
+
# Plot each bounding box
|
| 56 |
+
for bbox, label in zip(data['bboxes'], data['labels']):
|
| 57 |
+
# Unpack the bounding box coordinates
|
| 58 |
+
x1, y1, x2, y2 = bbox
|
| 59 |
+
# Create a Rectangle patch
|
| 60 |
+
rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=1, edgecolor='r', facecolor='none')
|
| 61 |
+
# Add the rectangle to the Axes
|
| 62 |
+
ax.add_patch(rect)
|
| 63 |
+
# Annotate the label
|
| 64 |
+
plt.text(x1, y1, label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
|
| 65 |
+
|
| 66 |
+
# Remove the axis ticks and labels
|
| 67 |
+
ax.axis('off')
|
| 68 |
+
|
| 69 |
+
# Show the plot
|
| 70 |
+
plt.show()
|
| 71 |
+
|
| 72 |
+
image = Image.open('1.jpeg')
|
| 73 |
+
parsed_answer = run_example("<OD>", image=image)
|
| 74 |
+
print(parsed_answer)
|
| 75 |
+
plot_bbox(image, parsed_answer["<OD>"])
|
| 76 |
|
| 77 |
|
| 78 |
|