import{s as Be,n as ke,o as Ne}from"../chunks/scheduler.35aab934.js";import{S as Qe,i as We,e as l,s as a,c as p,h as Ve,a as o,d as i,b as s,f as Ee,g as m,j as r,k as Ge,l as Ze,m as n,n as c,t as d,o as f,p as g}from"../chunks/index.de17b971.js";import{C as Re}from"../chunks/CopyLLMTxtMenu.48bc5cc2.js";import{C as Se}from"../chunks/CodeBlock.c1f62de5.js";import{H as h,E as qe}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8fdece89.js";function Ye(be){let u,S,Z,q,y,Y,w,X,$,F,b,ve="CLIP is a neural network adept at grasping visual concepts through natural language supervision. It operates by concurrently training a text encoder and an image encoder, focusing on a pretraining task that involves matching captions with corresponding images. This architecture allows CLIP to adapt to a variety of visual classification benchmarks seamlessly. It does so by simply receiving the names of the visual categories to be recognized, demonstrating “zero-shot” learning capabilities akin to those observed in GPT-2 and GPT-3 models.",A,v,D,M,Me="Given a batch of image-text pairs, CLIP computes the dense cosine similarity matrix between all possible (image, text) candidates within this batch. The core idea is to maximize the similarity between the correct pairs (shown in blue in the figure below) and minimize the similarity for incorrect pairs (shown in grey in the image). To do it, they optimize a symmetric cross-entropy loss over these similarity scores.",O,C,Ce='<img src="https://images.openai.com/blob/fbc4f633-9ad4-4dc2-bd94-0b6f1feee22f/overview-a.svg" alt="CLIP contrastive pre-training"/> <em>Image taken from OpenAI</em>',K,x,xe="Explaining in simple terms, we want to make the similarity between the image and its corresponding caption as high as we can, while the similarity between the image and the other captions should be small. We apply this logic to the caption too, so we want to maximize the similarity of the caption with its corresponding image, and minimize between all other images.",ee,T,te,U,Te="CLIP’s design features independent encoders for images and text, allowing flexibility in their choice. Users can switch the standard image encoder, like a Vision Transformer, for alternatives like ResNet, or opt for different text encoders, enhancing adaptability and experimentation. Of course, if you switch one of the encoders, you will need to train your model again, as your embedding distribution will be different.",ie,I,ne,J,Ue="CLIP, can be leveraged for a variety of applications. Here are some notable use cases:",ae,L,Ie="<li>Zero-shot image classification;</li> <li>Similarity search;</li> <li>Diffusion models conditioning.</li>",se,P,le,_,Je="For practical applications, one typically uses an image, and pre-defined classes as input. The provided Python example demonstrates how to use the transformers library for running CLIP. In this example, we want to zero-shot classify the image below between <code>dog</code> or <code>cat</code>.",oe,j,Le='<img src="http://images.cocodataset.org/val2017/000000039769.jpg" alt="A photo of cats"/>',re,z,pe,H,Pe="After executing this code, we got the following probabilities:",me,E,_e="<li>“a photo of a cat”: 99.49%</li> <li>“a photo of a dog”: 0.51%</li>",ce,G,de,B,je=`Despite CLIP’s proficiency in zero-shot classification, it is unlikely to outperform a specialized, fine-tuned model. Moreover, its generalization capabilities are somewhat limited, particularly in scenarios involving data or examples not encountered during training.
The paper also shows that CLIP’s effectiveness and biases are impacted by the choice of categories, as demonstrated in tests using the Fairface dataset. Notable disparities were found in gender and racial classifications, with gender accuracy over 96% and racial accuracy around 93%.`,fe,k,ge,N,ze="In conclusion, the CLIP model from OpenAI has revolutionized the multimodal field. What sets CLIP apart is its proficiency in zero-shot learning, allowing it to classify images into categories it wasn’t explicitly trained on. This remarkable ability to generalize comes from its innovative training method, where it learns to match images with text captions.",ue,Q,he,W,He='<li><a href="https://arxiv.org/abs/2103.00020" rel="nofollow">CLIP paper</a></li> <li><a href="https://lilianweng.github.io/posts/2021-05-31-contrastive/#clip" rel="nofollow">CLIP by Lilian Weng</a></li>',ye,V,we,R,$e;return y=new Re({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),w=new h({props:{title:"Contrastive Language-Image Pre-training (CLIP)",local:"contrastive-language-image-pre-training-clip",headingTag:"h1"}}),$=new h({props:{title:"Introduction",local:"introduction",headingTag:"h2"}}),v=new h({props:{title:"Contrastive pre-training",local:"contrastive-pre-training",headingTag:"h2"}}),T=new h({props:{title:"Text Encoder and Image Encoder",local:"text-encoder-and-image-encoder",headingTag:"h2"}}),I=new h({props:{title:"Use cases",local:"use-cases",headingTag:"h2"}}),P=new h({props:{title:"Usage",local:"usage",headingTag:"h2"}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jAlM0QlMjBsb2dpdHNfcGVyX2ltYWdlLnNvZnRtYXgoZGltJTNEMSk=",highlighted:`<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">import</span> requests

<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> CLIPProcessor, CLIPModel

model = CLIPModel.from_pretrained(<span class="hljs-string">&quot;openai/clip-vit-base-patch32&quot;</span>)
processor = CLIPProcessor.from_pretrained(<span class="hljs-string">&quot;openai/clip-vit-base-patch32&quot;</span>)

url = <span class="hljs-string">&quot;http://images.cocodataset.org/val2017/000000039769.jpg&quot;</span>
image = Image.<span class="hljs-built_in">open</span>(requests.get(url, stream=<span class="hljs-literal">True</span>).raw)

inputs = processor(
    text=[<span class="hljs-string">&quot;a photo of a cat&quot;</span>, <span class="hljs-string">&quot;a photo of a dog&quot;</span>],
    images=image,
    return_tensors=<span class="hljs-string">&quot;pt&quot;</span>,
    padding=<span class="hljs-literal">True</span>,
)

outputs = model(**inputs)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=<span class="hljs-number">1</span>)`,wrap:!1}}),G=new h({props:{title:"Limitations",local:"limitations",headingTag:"h2"}}),k=new h({props:{title:"Conclusion",local:"conclusion",headingTag:"h2"}}),Q=new h({props:{title:"References",local:"references",headingTag:"h2"}}),V=new qe({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit4/multimodal-models/clip-and-relatives/clip.mdx"}}),{c(){u=l("meta"),S=a(),Z=l("p"),q=a(),p(y.$$.fragment),Y=a(),p(w.$$.fragment),X=a(),p($.$$.fragment),F=a(),b=l("p"),b.textContent=ve,A=a(),p(v.$$.fragment),D=a(),M=l("p"),M.textContent=Me,O=a(),C=l("p"),C.innerHTML=Ce,K=a(),x=l("p"),x.textContent=xe,ee=a(),p(T.$$.fragment),te=a(),U=l("p"),U.textContent=Te,ie=a(),p(I.$$.fragment),ne=a(),J=l("p"),J.textContent=Ue,ae=a(),L=l("ul"),L.innerHTML=Ie,se=a(),p(P.$$.fragment),le=a(),_=l("p"),_.innerHTML=Je,oe=a(),j=l("p"),j.innerHTML=Le,re=a(),p(z.$$.fragment),pe=a(),H=l("p"),H.textContent=Pe,me=a(),E=l("ul"),E.innerHTML=_e,ce=a(),p(G.$$.fragment),de=a(),B=l("p"),B.textContent=je,fe=a(),p(k.$$.fragment),ge=a(),N=l("p"),N.textContent=ze,ue=a(),p(Q.$$.fragment),he=a(),W=l("ul"),W.innerHTML=He,ye=a(),p(V.$$.fragment),we=a(),R=l("p"),this.h()},l(e){const t=Ve("svelte-u9bgzb",document.head);u=o(t,"META",{name:!0,content:!0}),t.forEach(i),S=s(e),Z=o(e,"P",{}),Ee(Z).forEach(i),q=s(e),m(y.$$.fragment,e),Y=s(e),m(w.$$.fragment,e),X=s(e),m($.$$.fragment,e),F=s(e),b=o(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-ehzu06"&&(b.textContent=ve),A=s(e),m(v.$$.fragment,e),D=s(e),M=o(e,"P",{"data-svelte-h":!0}),r(M)!=="svelte-1k56xzf"&&(M.textContent=Me),O=s(e),C=o(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-1f2xlz1"&&(C.innerHTML=Ce),K=s(e),x=o(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-hxbljq"&&(x.textContent=xe),ee=s(e),m(T.$$.fragment,e),te=s(e),U=o(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-1nn5dxn"&&(U.textContent=Te),ie=s(e),m(I.$$.fragment,e),ne=s(e),J=o(e,"P",{"data-svelte-h":!0}),r(J)!=="svelte-qs9t2n"&&(J.textContent=Ue),ae=s(e),L=o(e,"UL",{"data-svelte-h":!0}),r(L)!=="svelte-d3etr2"&&(L.innerHTML=Ie),se=s(e),m(P.$$.fragment,e),le=s(e),_=o(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-147dtgw"&&(_.innerHTML=Je),oe=s(e),j=o(e,"P",{"data-svelte-h":!0}),r(j)!=="svelte-xhmj4j"&&(j.innerHTML=Le),re=s(e),m(z.$$.fragment,e),pe=s(e),H=o(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-16udf98"&&(H.textContent=Pe),me=s(e),E=o(e,"UL",{"data-svelte-h":!0}),r(E)!=="svelte-cuwovx"&&(E.innerHTML=_e),ce=s(e),m(G.$$.fragment,e),de=s(e),B=o(e,"P",{"data-svelte-h":!0}),r(B)!=="svelte-1z1bz9"&&(B.textContent=je),fe=s(e),m(k.$$.fragment,e),ge=s(e),N=o(e,"P",{"data-svelte-h":!0}),r(N)!=="svelte-1taci8q"&&(N.textContent=ze),ue=s(e),m(Q.$$.fragment,e),he=s(e),W=o(e,"UL",{"data-svelte-h":!0}),r(W)!=="svelte-18pv8si"&&(W.innerHTML=He),ye=s(e),m(V.$$.fragment,e),we=s(e),R=o(e,"P",{}),Ee(R).forEach(i),this.h()},h(){Ge(u,"name","hf:doc:metadata"),Ge(u,"content",Xe)},m(e,t){Ze(document.head,u),n(e,S,t),n(e,Z,t),n(e,q,t),c(y,e,t),n(e,Y,t),c(w,e,t),n(e,X,t),c($,e,t),n(e,F,t),n(e,b,t),n(e,A,t),c(v,e,t),n(e,D,t),n(e,M,t),n(e,O,t),n(e,C,t),n(e,K,t),n(e,x,t),n(e,ee,t),c(T,e,t),n(e,te,t),n(e,U,t),n(e,ie,t),c(I,e,t),n(e,ne,t),n(e,J,t),n(e,ae,t),n(e,L,t),n(e,se,t),c(P,e,t),n(e,le,t),n(e,_,t),n(e,oe,t),n(e,j,t),n(e,re,t),c(z,e,t),n(e,pe,t),n(e,H,t),n(e,me,t),n(e,E,t),n(e,ce,t),c(G,e,t),n(e,de,t),n(e,B,t),n(e,fe,t),c(k,e,t),n(e,ge,t),n(e,N,t),n(e,ue,t),c(Q,e,t),n(e,he,t),n(e,W,t),n(e,ye,t),c(V,e,t),n(e,we,t),n(e,R,t),$e=!0},p:ke,i(e){$e||(d(y.$$.fragment,e),d(w.$$.fragment,e),d($.$$.fragment,e),d(v.$$.fragment,e),d(T.$$.fragment,e),d(I.$$.fragment,e),d(P.$$.fragment,e),d(z.$$.fragment,e),d(G.$$.fragment,e),d(k.$$.fragment,e),d(Q.$$.fragment,e),d(V.$$.fragment,e),$e=!0)},o(e){f(y.$$.fragment,e),f(w.$$.fragment,e),f($.$$.fragment,e),f(v.$$.fragment,e),f(T.$$.fragment,e),f(I.$$.fragment,e),f(P.$$.fragment,e),f(z.$$.fragment,e),f(G.$$.fragment,e),f(k.$$.fragment,e),f(Q.$$.fragment,e),f(V.$$.fragment,e),$e=!1},d(e){e&&(i(S),i(Z),i(q),i(Y),i(X),i(F),i(b),i(A),i(D),i(M),i(O),i(C),i(K),i(x),i(ee),i(te),i(U),i(ie),i(ne),i(J),i(ae),i(L),i(se),i(le),i(_),i(oe),i(j),i(re),i(pe),i(H),i(me),i(E),i(ce),i(de),i(B),i(fe),i(ge),i(N),i(ue),i(he),i(W),i(ye),i(we),i(R)),i(u),g(y,e),g(w,e),g($,e),g(v,e),g(T,e),g(I,e),g(P,e),g(z,e),g(G,e),g(k,e),g(Q,e),g(V,e)}}}const Xe='{"title":"Contrastive Language-Image Pre-training (CLIP)","local":"contrastive-language-image-pre-training-clip","sections":[{"title":"Introduction","local":"introduction","sections":[],"depth":2},{"title":"Contrastive pre-training","local":"contrastive-pre-training","sections":[],"depth":2},{"title":"Text Encoder and Image Encoder","local":"text-encoder-and-image-encoder","sections":[],"depth":2},{"title":"Use cases","local":"use-cases","sections":[],"depth":2},{"title":"Usage","local":"usage","sections":[],"depth":2},{"title":"Limitations","local":"limitations","sections":[],"depth":2},{"title":"Conclusion","local":"conclusion","sections":[],"depth":2},{"title":"References","local":"references","sections":[],"depth":2}],"depth":1}';function Fe(be){return Ne(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class tt extends Qe{constructor(u){super(),We(this,u,Fe,Ye,Be,{})}}export{tt as component};
