> For the complete documentation index, see [llms.txt](https://docs.clore.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.clore.ai/guides/guides_v2-hi/vision-models/florence2.md).

# Florence-2

कैप्शनिंग, डिटेक्शन, और सेगमेंटेशन के लिए Microsoft Florence-2

कैप्शनिंग, डिटेक्शन, सेगमेंटेशन और अन्य के लिए Microsoft का शक्तिशाली विज़न मॉडल।

{% hint style="success" %}
सभी उदाहरण GPU सर्वरों पर चलाए जा सकते हैं, जिन्हें किराए पर लिया गया है [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace).
{% endhint %}

{% hint style="info" %}
इस गाइड के सभी उदाहरण GPU सर्वरों पर चलाए जा सकते हैं, जिन्हें किराए पर लिया गया हो [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace) मार्केटप्लेस।
{% endhint %}

## CLORE.AI पर किराए पर लेना

1. विज़िट करें [CLORE.AI मार्केटप्लेस](https://clore.ai/marketplace)
2. GPU प्रकार, VRAM और कीमत के अनुसार फ़िल्टर करें
3. चुनें **ऑन-डिमांड** (स्थिर दर) या **स्पॉट** (बोली मूल्य)
4. अपना ऑर्डर कॉन्फ़िगर करें:
   * Docker इमेज चुनें
   * पोर्ट सेट करें (SSH के लिए TCP, वेब UI के लिए HTTP)
   * यदि आवश्यक हो तो environment variables जोड़ें
   * स्टार्टअप कमांड दर्ज करें
5. भुगतान चुनें: **CLORE**, **BTC**या **USDT/USDC**
6. ऑर्डर बनाएं और डिप्लॉयमेंट की प्रतीक्षा करें

### अपने सर्वर तक पहुँचें

* कनेक्शन विवरण यहाँ खोजें **मेरे ऑर्डर**
* वेब इंटरफ़ेस: HTTP पोर्ट URL का उपयोग करें
* SSH: `ssh -p <port> root@<proxy-address>`

## Florence-2 क्या है?

Microsoft का Florence-2 एक विज़न फाउंडेशन मॉडल है जो संभालता है:

* छवि कैप्शनिंग (संक्षिप्त और विस्तृत)
* ऑब्जेक्ट डिटेक्शन और स्थानीयकरण
* घने क्षेत्र की कैप्शनिंग
* संदर्भित अभिव्यक्ति की समझ
* OCR और पाठ पहचान
* दृश्य प्रश्नोत्तर

## संसाधन

* **HuggingFace:** [microsoft/Florence-2-large](https://huggingface.co/microsoft/Florence-2-large)
* **पेपर:** [Florence-2 पेपर](https://arxiv.org/abs/2311.06242)
* **GitHub:** [microsoft/Florence-2](https://github.com/microsoft/Florence-2)
* **डेमो:** [HuggingFace Space](https://huggingface.co/spaces/microsoft/Florence-2)

## अनुशंसित हार्डवेयर

| घटक     | न्यूनतम       | अनुशंसित      | इष्टतम        |
| ------- | ------------- | ------------- | ------------- |
| GPU     | RTX 3060 12GB | RTX 4080 16GB | RTX 4090 24GB |
| VRAM    | 8GB           | 12GB          | 16GB          |
| CPU     | 4 कोर         | 8 कोर         | 16 कोर        |
| RAM     | 16GB          | 32GB          | 64GB          |
| स्टोरेज | 30GB SSD      | 50GB NVMe     | 100GB NVMe    |
| इंटरनेट | 100 Mbps      | 500 Mbps      | 1 Gbps        |

## CLORE.AI पर त्वरित परिनियोजन

**Docker इमेज:**

```
pytorch/pytorch:2.11.0-cuda12.8-cudnn9-devel
```

**पोर्ट:**

```
22/tcp
7860/http
```

**कमांड:**

```bash
pip install transformers accelerate einops timm gradio && \\
python -c "
import gradio as gr
from transformers import AutoProcessor, AutoModelForCausalLM
import torch
from PIL import Image

model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-large', torch_dtype=torch.float16, trust_remote_code=True).to('cuda')
processor = AutoProcessor.from_pretrained('microsoft/Florence-2-large', trust_remote_code=True)

def process(image, task):
    inputs = processor(text=task, images=image, return_tensors='pt').to('cuda', torch.float16)
    generated_ids = model.generate(input_ids=inputs['input_ids'], pixel_values=inputs['pixel_values'], max_new_tokens=1024)
    result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
    return processor.post_process_generation(result, task=task, image_size=image.size)

gr.Interface(fn=process, inputs=[gr.Image(type='pil'), gr.Dropdown(['<CAPTION>', '<DETAILED_CAPTION>', '<OD>'])], outputs='json').launch(server_name='0.0.0.0')
"
```

## अपनी सेवा तक पहुँचना

डिप्लॉयमेंट के बाद, अपना `http_pub` URL यहाँ **मेरे ऑर्डर**:

1. पर जाएँ **मेरे ऑर्डर** पेज
2. अपने ऑर्डर पर क्लिक करें
3. खोजें `http_pub` URL (उदा., `abc123.clorecloud.net`)

उपयोग करें `https://YOUR_HTTP_PUB_URL` की बजाय `localhost` नीचे दिए गए उदाहरणों में।

## इंस्टॉलेशन

```bash
pip install transformers accelerate einops timm
pip install flash-attn --no-build-isolation  # तेज़ inference के लिए वैकल्पिक
```

## आप क्या बना सकते हैं

### सामग्री विश्लेषण

* छवि विवरण स्वतः जनरेट करें
* छवियों से पाठ निकालें (OCR)
* बड़े पैमाने पर दृश्य सामग्री का विश्लेषण करें

### डेटा एनोटेशन

* कैप्शन के साथ डेटासेट को स्वतः लेबल करें
* ऑब्जेक्ट्स के लिए बाउंडिंग बॉक्स जनरेट करें
* घने एनोटेशन बनाएँ

### सुलभता

* छवियों के लिए alt-text जनरेट करें
* दृष्टिबाधित लोगों के लिए छवियों का वर्णन करें
* ऑडियो विवरण बनाएँ

### खोज और खोजबीन

* सामग्री के आधार पर छवियों को इंडेक्स करें
* विज़ुअल सर्च सिस्टम बनाएँ
* सामग्री मॉडरेशन

### दस्तावेज़ प्रसंस्करण

* दस्तावेज़ों से पाठ निकालें
* चार्ट और आरेख समझें
* स्कैन की गई सामग्री को प्रोसेस करें

## मूल उपयोग

### छवि कैप्शनिंग

```python
from transformers import AutoProcessor, AutoModelForCausalLM
from PIL import Image
import torch

model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Florence-2-large",
    torch_dtype=torch.float16,
    trust_remote_code=True
).to("cuda")

processor = AutoProcessor.from_pretrained(
    "microsoft/Florence-2-large",
    trust_remote_code=True
)

image = Image.open("photo.jpg")

# संक्षिप्त कैप्शन
task = "<CAPTION>"
inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024
)
result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
caption = processor.post_process_generation(result, task=task, image_size=image.size)
print(caption)

# आउटपुट: {'<CAPTION>': 'पार्क में खेलता हुआ एक कुत्ता'}

# विस्तृत कैप्शन
task = "<DETAILED_CAPTION>"
inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024
)
result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
detailed = processor.post_process_generation(result, task=task, image_size=image.size)
print(detailed)
```

### ऑब्जेक्ट डिटेक्शन

```python
task = "<OD>"  # ऑब्जेक्ट डिटेक्शन
inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024
)
result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
detections = processor.post_process_generation(result, task=task, image_size=image.size)

# आउटपुट: {'<OD>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['कुत्ता', 'गेंद', ...]}}
```

### OCR (पाठ पहचान)

```python
task = "<OCR>"
inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024
)
result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
text = processor.post_process_generation(result, task=task, image_size=image.size)
print(text)

# आउटपुट: {'<OCR>': 'छवि में मिला पाठ...'}
```

### घने क्षेत्र की कैप्शनिंग

```python
task = "<DENSE_REGION_CAPTION>"
inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024
)
result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
regions = processor.post_process_generation(result, task=task, image_size=image.size)

# आउटपुट: {'<DENSE_REGION_CAPTION>': {'bboxes': [...], 'labels': ['दौड़ता हुआ एक भूरा कुत्ता', 'हरी घास', ...]}}
```

### संदर्भित अभिव्यक्ति की समझ

पाठ विवरणों के आधार पर ऑब्जेक्ट्स खोजें:

```python
task = "<CAPTION_TO_PHRASE_GROUNDING>"
text_input = "बाईं ओर वाली लाल कार"

inputs = processor(
    text=task + text_input,
    images=image,
    return_tensors="pt"
).to("cuda", torch.float16)

generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024
)
result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
grounding = processor.post_process_generation(result, task=task, image_size=image.size)

# "बाईं ओर वाली लाल कार" का बाउंडिंग बॉक्स लौटाता है
```

## सभी उपलब्ध कार्य

```python
TASKS = [
    "<CAPTION>",                    # संक्षिप्त कैप्शन
    "<DETAILED_CAPTION>",           # विस्तृत विवरण
    "<MORE_DETAILED_CAPTION>",      # बहुत विस्तृत विवरण
    "<OD>",                          # ऑब्जेक्ट डिटेक्शन
    "<DENSE_REGION_CAPTION>",       # क्षेत्र विवरण
    "<REGION_PROPOSAL>",            # रुचि के क्षेत्रों का प्रस्ताव
    "<CAPTION_TO_PHRASE_GROUNDING>", # पाठ से ऑब्जेक्ट्स खोजें
    "<REFERRING_EXPRESSION_SEGMENTATION>", # पाठ से सेगमेंट करें
    "<REGION_TO_SEGMENTATION>",     # निर्दिष्ट क्षेत्र को सेगमेंट करें
    "<OPEN_VOCABULARY_DETECTION>",  # पाठ लेबलों के साथ डिटेक्ट करें
    "<REGION_TO_CATEGORY>",         # क्षेत्र को वर्गीकृत करें
    "<REGION_TO_DESCRIPTION>",      # क्षेत्र का वर्णन करें
    "<OCR>",                         # पाठ निकालें
    "<OCR_WITH_REGION>",            # स्थानों सहित पाठ निकालें
]
```

## बैच प्रोसेसिंग

```python
import os
from transformers import AutoProcessor, AutoModelForCausalLM
from PIL import Image
import torch
import json

model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Florence-2-large",
    torch_dtype=torch.float16,
    trust_remote_code=True
).to("cuda")
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)

def process_image(image_path, task):
    image = Image.open(image_path)
    inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
    generated_ids = model.generate(
        input_ids=inputs["input_ids"],
        pixel_values=inputs["pixel_values"],
        max_new_tokens=1024
    )
    result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
    return processor.post_process_generation(result, task=task, image_size=image.size)

# डायरेक्टरी प्रोसेस करें
input_dir = "./images"
results = {}

for filename in os.listdir(input_dir):
    if not filename.endswith(('.jpg', '.png')):
        continue

    path = os.path.join(input_dir, filename)
    results[filename] = {
        "caption": process_image(path, "<CAPTION>"),
        "objects": process_image(path, "<OD>"),
        "text": process_image(path, "<OCR>")
    }
    print(f"Processed: {filename}")

with open("results.json", "w") as f:
    json.dump(results, f, indent=2)
```

## Gradio इंटरफ़ेस

```python
import gradio as gr
from transformers import AutoProcessor, AutoModelForCausalLM
from PIL import Image, ImageDraw
import torch

model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Florence-2-large",
    torch_dtype=torch.float16,
    trust_remote_code=True
).to("cuda")
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)

def run_task(image, task):
    inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
    generated_ids = model.generate(
        input_ids=inputs["input_ids"],
        pixel_values=inputs["pixel_values"],
        max_new_tokens=1024
    )
    result = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
    parsed = processor.post_process_generation(result, task=task, image_size=image.size)

    # डिटेक्शन कार्य के लिए बॉक्स बनाएं
    output_image = image.copy()
    if task in ["<OD>", "<DENSE_REGION_CAPTION>"]:

        draw = ImageDraw.Draw(output_image)
        if "bboxes" in parsed.get(task, {}):
            for box, label in zip(parsed[task]["bboxes"], parsed[task]["labels"]):
                draw.rectangle(box, outline="red", width=2)
                draw.text((box[0], box[1]-15), label, fill="red")

    return output_image, str(parsed)

demo = gr.Interface(
    fn=run_task,
    inputs=[
        gr.Image(type="pil", label="Input Image"),
        gr.Dropdown(
            choices=["<CAPTION>", "<DETAILED_CAPTION>", "<OD>", "<DENSE_REGION_CAPTION>", "<OCR>"],
            value="<CAPTION>",
            label="कार्य"
        )
    ],
    outputs=[
        gr.Image(label="परिणाम"),
        gr.Textbox(label="आउटपुट", lines=10)
    ],
    title="Florence-2 Vision AI",
    description="बहु-कार्य विज़न मॉडल। CLORE.AI GPU सर्वरों पर चल रहा है."
)

demo.launch(server_name="0.0.0.0", server_port=7860)
```

## प्रदर्शन

| कार्य             | रिज़ॉल्यूशन | GPU      | गति   |
| ----------------- | ----------- | -------- | ----- |
| कैप्शन            | 768x768     | RTX 3090 | 200ms |
| कैप्शन            | 768x768     | RTX 4090 | 120ms |
| ऑब्जेक्ट डिटेक्शन | 768x768     | RTX 4090 | 150ms |
| OCR               | 768x768     | RTX 4090 | 180ms |
| घना कैप्शन        | 768x768     | A100     | 100ms |

## मॉडल वेरिएंट

| मॉडल                | पैरामीटर | VRAM | गति   |
| ------------------- | -------- | ---- | ----- |
| Florence-2-base     | 232M     | 4GB  | तेज़  |
| Florence-2-large    | 771M     | 8GB  | मध्यम |
| Florence-2-base-ft  | 232M     | 4GB  | तेज़  |
| Florence-2-large-ft | 771M     | 8GB  | मध्यम |

## सामान्य समस्याएँ और समाधान

### मेमोरी समाप्त

**समस्या:** CUDA OOM त्रुटि

**समाधान:**

```python

# बड़े के बजाय base मॉडल का उपयोग करें
model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Florence-2-base",
    torch_dtype=torch.float16,
    trust_remote_code=True
).to("cuda")

# या CPU ऑफ़लोड सक्षम करें
model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Florence-2-large",
    torch_dtype=torch.float16,
    trust_remote_code=True,
    device_map="auto"
)
```

### धीमी inference

**समस्या:** प्रसंस्करण में बहुत समय लग रहा है

**समाधान:**

* तेज़ inference के लिए Florence-2-base का उपयोग करें
* गति बढ़ाने के लिए flash-attention इंस्टॉल करें
* कई छवियों को एक साथ बैच करें
* प्रोडक्शन के लिए A100 GPU का उपयोग करें

```bash
pip install flash-attn --no-build-isolation
```

### OCR के खराब परिणाम

**समस्या:** पाठ पहचान सटीक नहीं है

**समाधान:**

* सुनिश्चित करें कि छवि उच्च रेज़ोल्यूशन की हो (कम से कम 768px)
* उपयोग करें `<OCR_WITH_REGION>` बेहतर स्थानीयकरण के लिए
* पूर्व-प्रसंस्करण: कंट्रास्ट बढ़ाएँ, छवि को डेस्क्यू करें
* OCR से पहले पाठ क्षेत्रों में क्रॉप करें

### डिटेक्शन में ऑब्जेक्ट्स छूट रहे हैं

**समस्या:** ऑब्जेक्ट्स का पता नहीं चला

**समाधान:**

* उपयोग करें `<DENSE_REGION_CAPTION>` अधिक क्षेत्रों के लिए
* कोशिश करें `<OPEN_VOCABULARY_DETECTION>` विशिष्ट लेबलों के साथ
* विशिष्ट ऑब्जेक्ट्स के लिए GroundingDINO के साथ संयोजन करें

## समस्या निवारण

### कार्य काम नहीं कर रहा है

* सटीक task name syntax जाँचें
* कुछ कार्यों को विशिष्ट इनपुट फ़ॉर्मेट चाहिए
* सत्यापित करें कि मॉडल संस्करण कार्य से मेल खाता है

### आउटपुट फ़ॉर्मेट अप्रत्याशित है

* अलग-अलग कार्य अलग-अलग फ़ॉर्मेट लौटाते हैं
* कार्य प्रकार के अनुसार आउटपुट को पार्स करें
* कार्य आउटपुट के लिए दस्तावेज़ देखें

### CUDA मेमोरी समस्याएँ

* Florence-2-large को लगभग 8GB VRAM चाहिए
* कम मेमोरी के लिए Florence-2-base का उपयोग करें
* gradient checkpointing सक्षम करें

### धीमी प्रोसेसिंग

* जब संभव हो batch inference का उपयोग करें
* FP16 मोड सक्षम करें
* TensorRT ऑप्टिमाइज़ेशन पर विचार करें

## लागत का अनुमान

CLORE.AI मार्केटप्लेस की सामान्य दरें (2024 तक):

| GPU       | प्रति घंटा दर | प्रति दिन दर | 4-घंटे का सत्र |
| --------- | ------------- | ------------ | -------------- |
| RTX 3060  | \~$0.03       | \~$0.70      | \~$0.12        |
| RTX 3090  | \~$0.06       | \~$1.50      | \~$0.25        |
| RTX 4090  | \~$0.10       | \~$2.30      | \~$0.40        |
| A100 40GB | \~$0.17       | \~$4.00      | \~$0.70        |
| A100 80GB | \~$0.25       | \~$6.00      | \~$1.00        |

*मूल्य प्रदाता और मांग के अनुसार बदलते हैं। देखें* [*CLORE.AI मार्केटप्लेस*](https://clore.ai/marketplace) *वर्तमान दरों के लिए।*

**पैसे बचाएँ:**

* का उपयोग करें **स्पॉट** बाधित किए जा सकने वाले कार्य के लिए मार्केट — लगभग एक-तिहाई सर्वरों की स्पॉट कीमत ऑन-डिमांड से कम होती है (मध्य \~13% छूट), बाकी उससे मेल खाते हैं
* से भुगतान करें **CLORE** टोकन
* विभिन्न प्रदाताओं के बीच कीमतों की तुलना करें

## अगले चरण

* [LLaVA](/guides/guides_v2-hi/vision-models/llava-vision-language.md) - विज़न चैट और QA
* [GroundingDINO](/guides/guides_v2-hi/vision-models/groundingdino.md) - ज़ीरो-शॉट डिटेक्शन
* [SAM2](/guides/guides_v2-hi/vision-models/sam2-video.md) - पता लगाए गए ऑब्जेक्ट्स को सेगमेंट करें


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-hi/vision-models/florence2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
