from transformers import AutoProcessor, AutoModelForCausalLM import matplotlib.pyplot as plt import matplotlib.patches as patches model_id = "Nikhil-aka-Nick/florence2-finalV13" model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, device_map="cuda") # load the model on GPU processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True) def run_example(task_prompt, image, max_new_tokens=128): prompt = task_prompt inputs = processor(text=prompt, images=image, return_tensors="pt") generated_ids = model.generate( input_ids=inputs["input_ids"].cuda(), pixel_values=inputs["pixel_values"].cuda(), max_new_tokens=max_new_tokens, early_stopping=False, do_sample=False, num_beams=3, ) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0] parsed_answer = processor.post_process_generation( generated_text, task=task_prompt, image_size=(image.width, image.height) ) return parsed_answer import matplotlib.pyplot as plt import matplotlib.patches as patches def plot_bbox(image, data, figsize=(12, 12)): # Add figsize as a parameter with default size fig, ax = plt.subplots(figsize=figsize) # Display the image ax.imshow(image) # Plot each bounding box for bbox, label in zip(data['bboxes'], data['labels']): # Unpack the bounding box coordinates x1, y1, x2, y2 = bbox # Create a Rectangle patch rect = patches.Rectangle((x1, y1), x2-x1, y2-y1, linewidth=1, edgecolor='r', facecolor='none') # Add the rectangle to the Axes ax.add_patch(rect) # Annotate the label plt.text(x1, y1, label, color='white', fontsize=8, bbox=dict(facecolor='red', alpha=0.5)) # Remove the axis ticks and labels ax.axis('off') # Show the plot plt.show() from datasets import load_dataset dataset = load_dataset("Nikhil-aka-Nick/My_data_for_test") example_id = 5 image = dataset["train"][example_id]["image"] parsed_answer = run_example("", image=image) plot_bbox(image, parsed_answer[""])