CLIP

CLIP एक मशीन लर्निंग मॉडल है जो इमेज और टेक्स्ट के लिए एम्बेडिंग्ज़ जनरेट करने में सक्षम है। इन एम्बेडिंग्ज़ का उपयोग ज़ीरो-शॉट क्लासिफिकेशन, सेमान्टिक इमेज सर्च और कई अन्य उपयोग मामलों के लिए किया जा सकता है। Roboflow Inference Server पर CLIP का उपयोग करने के लिए तीन मार्ग उपलब्ध हैं:

  • embed_image: इमेज एम्बेडिंग्ज़ की गणना करने के लिए उपयोग किया जाता है

  • embed_text: टेक्स्ट एम्बेडिंग्ज़ की गणना करने के लिए उपयोग किया जाता है

  • compare: टेक्स्ट और इमेज के एम्बेडिंग्ज़ की गणना करके उनकी तुलना करने के लिए उपयोग किया जाता है

इमेज एम्बेड करें

किसी इमेज का एम्बेड करना उस इमेज की जानकारी को एक अधिक प्रबंधनीय आकार में संपीड़ित करने जैसा है। जब हम किसी इमेज को एम्बेड करते हैं, तो हम उन दसियों हज़ार पिक्सल्स वाले इमेज इनपुट को लेते हैं और उसे कुछ सैकड़ों संख्याओं में परिष्कृत करते हैं जिन्हें एम्बेडिंग कहा जाता है। ये एम्बेडिंग्ज़ मानव दृष्टि के लिए विशेष अर्थ रखते हैं, और जब इन्हें अन्य एम्बेडिंग्ज़ से तुलना किया जाता है, तो ये बहुत उपयोगी साबित हो सकते हैं।

CLIP और Roboflow Inference Server का उपयोग करके इमेज एम्बेडिंग उत्पन्न करने के लिए:

# रिक्वेस्ट पेलोड परिभाषित करें
infer_clip_payload = {
    # इमेजेज़ urls के रूप में या base64 एन्कोडेड स्ट्रिंग्स के रूप में प्रदान की जा सकती हैं
    "image": {
        # "type" भी "base64" हो सकता है
        "type": "url",
        # "value" भी इमेज डेटा की base64 एन्कोडेड स्ट्रिंग हो सकती है
        "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
    },
}

# इन्फरेंस सर्वर URL परिभाषित करें (localhost:9001, infer.roboflow.com, आदि)
base_url = "https://infer.roboflow.com"

# अपना Roboflow API Key परिभाषित करें
api_key = <YOUR API KEY HERE>

res = requests.post(
    f"{base_url}/clip/embed_image?api_key={api_key}",
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
[[-0.4853120744228363, ... ]]

एक ही रिक्वेस्ट के साथ कई इमेजेज़ को एम्बेड किया जा सकता है:

# रिक्वेस्ट पेलोड परिभाषित करें
infer_clip_payload = {
    # इमेजेज़ urls के रूप में या base64 एन्कोडेड स्ट्रिंग्स के रूप में प्रदान की जा सकती हैं
    "image": [
        {
            "type": "url",
            "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
        },
        {
            "type": "url",
            "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
        }
    ],
}

res = requests.post(
    f"{base_url}/clip/embed_image?api_key={api_key}",
    json=infer_clip_payload,
)

टेक्स्ट एम्बेड करें

CLIP इमेज की तरह टेक्स्ट के लिए भी एम्बेडिंग्ज़ जनरेट कर सकता है।

# रिक्वेस्ट पेलोड परिभाषित करें
infer_clip_payload = {
    "text": "the quick brown fox jumped over the lazy dog",
}

res = requests.post(
    f"{base_url}/clip/embed_text?api_key={api_key}",
    json=infer_clip_payload,
)

embeddings = res.json()['embeddings']

print(embeddings)
[[0.56842650744228363, ... ]]

कई टेक्स्ट ब्लॉक्स को एक ही रिक्वेस्ट में बैच किया जा सकता है:

# रिक्वेस्ट पेलोड परिभाषित करें
infer_clip_payload = {
    "text": [
        "the quick brown fox jumped over the lazy dog",
        "how vexingly quick daft zebras jump"
    ]
}

res = requests.post(
    f"{base_url}/clip/embed_text?api_key={api_key}",
    json=infer_clip_payload,
)

तुलना (Compare)

CLIP का असली मूल्य तब प्रकट होता है जब एम्बेडिंग्ज़ की तुलना की जाती है। तुलना दो एम्बेडिंग्ज़ के बीच का गणितीय दूरी होती है जो cosine similarity का उपयोग करके गणना की जाती है। इस दूरी को एक समानता स्कोर के रूप में सोचा जा सकता है। अगर दो एम्बेडिंग्ज़ की cosine similarity 1 के पास है, तो वे समान माने जाते हैं।

जब आप compare करते हैं, तो आप एक prompt और एक या कई subjects परिभाषित करते हैं। चूंकि आप टेक्स्ट या इमेज का कोई भी संयोजन तुलना कर सकते हैं, इसलिए आपको prompt type और subject type भी परिभाषित करना होगा।

# रिक्वेस्ट पेलोड परिभाषित करें
infer_clip_payload = {
    "prompt": {
        "type": "url",
        "value": "https://images.freeimages.com/images/large-previews/36c/raccoons-in-the-wild-4-1624830.jpg",
    },
    "prompt_type": "image",
    "subject": "A very cute raccoon",
    "subject_type": "text",
}

res = requests.post(
    f"{base_url}/clip/compare?api_key={api_key}",
    json=infer_clip_payload,
)

similarity = res.json()['similarity']

print(similarity)
[0.30969720949239016]

एक ही रिक्वेस्ट में कई prompts (एकल रिक्वेस्ट में आठ तक) लिस्ट के रूप में भेजे जा सकते हैं:

infer_clip_payload = {
    "subject": {
        "type": "url",
        "value": "https://i.imgur.com/Q6lDy8B.jpg",
    },
    "subject_type": "image",
    "prompt": [
        "A very cute raccoon",
        "A large dog",
        "A black cate",
    ],
    "prompt_type": "text",
}
[0.80559720949239016, 0.20329720949239016, 0.505559720949239016]

Last updated

Was this helpful?