Git Product home page Git Product logo

collab-repo's Introduction

collab-repo

Florence-2-large sample usage

This notebook demonstrates how to use the Florence-2-large model for various vision-language tasks.

Installation

Install the necessary packages:

!pip install {' '.join(missing_packages)}
!pip install flash_attn einops timm

Usage

  1. Load the model and processor:
from transformers import AutoProcessor, AutoModelForCausalLM

model_id = 'microsoft/Florence-2-large' model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval().cuda()
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
  1. Define the prediction function:
def run_example(task_prompt, text_input=None):
    if text_input is None:
        prompt = task_prompt
    else:
        prompt = task_prompt + text_input
    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=1024,
      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
  1. Load an image:
from PIL import Image import requests

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
  1. Run pre-defined tasks:
#Captioning
task_prompt = '<CAPTION>'
run_example(task_prompt)

#Object detection
task_prompt = '<OD>'
results = run_example(task_prompt)

#... (plot the results)
#Dense region captioning
task_prompt = '<DENSE_REGION_CAPTION>'
results = run_example(task_prompt)

#... (plot the results)
#... (other tasks)
  1. Run tasks with additional inputs:
#Phrase grounding
task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
results = run_example(task_prompt, text_input="A green car parked in front of a yellow building")

#... (plot the results)
#Referring expression segmentation
task_prompt = '<REFERRING_EXPRESSION_SEGMENTATION>'
results = run_example(task_prompt, text_input="a green car")

#... (plot the results)
#... (other tasks)
  1. OCR tasks:
#Load an image with text
url = "https://cursivehandwriting.co.in/wp-content/uploads/2023/11/f9b3033e7a38c727af188a4604e0c0c4-e1700722488449.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw).convert('RGB')

#OCR
task_prompt = '<OCR>'
run_example(task_prompt)
print(results)
# ocr results format
# {'<OCR>': '\nHandwriting is an essential skill for both childrenand adults (Fider & Majnemer, 2007). Even inthe age of technology, it remains the primary toolof communication and knowledge assessment forstudents in the classroom. The demands for it are great,whether in the classroom or beyond. A 1992 studyMcHale & Cernak) found that 85 percent of all finemotor time in second-fourth-and sixth-grade classroomwas spent on paper and pencil activities. A morerecent study (Mari, Cernack, John & Henderson, 2003)noted that kindergarten children are now spending42 percent of their fine motor time on paper anpencil activities.\n'}

# OCR with region
task_prompt = ''
results = run_example(task_prompt)

#... (plot the results)

Results

Execute the code yourself to see the output and generated images for each task.

collab-repo's People

Contributors

mowamed avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.