import{s as Ie,f as Ce,n as xe,o as Ze}from"../chunks/scheduler.7bc62968.js";import{S as $e,i as Be,g as i,s as a,r as m,A as Ve,h as o,f as s,c as n,j as ve,u as d,x as c,k as H,y as We,a as l,v as u,d as h,t as M,w as b}from"../chunks/index.2f8492b0.js";import{C as Re}from"../chunks/CodeBlock.bb61a5a9.js";import{H as Q,E as Ee}from"../chunks/index.71444864.js";function ke(me){let p,L,_,N,j,Y,f,z,y,de='Object detection, a critical task in computer vision, has seen significant advancements with models like the YOLO (<a href="https://arxiv.org/abs/1506.02640" rel="nofollow">original paper</a>, <a href="https://github.com/ultralytics/ultralytics" rel="nofollow">latest code version</a>). However, traditional models like YOLO have limitations in detecting objects outside their training datasets. To address this, the AI community has shifted towards developing models that can identify a wider range of objects, leading to the creation of models akin to CLIP but for object detection.',A,w,F,T,ue="OWL-ViT represents a leap forward in open-vocabulary object detection. It starts with a training stage similar to CLIP, focusing on a vision and language encoder using a contrastive loss. This foundational stage enables the model to learn a shared representation space for both visual and textual data.",O,g,G,J,he="The innovation in OWL-ViT comes during its fine-tuning stage for object detection. Here, instead of the token pooling and final projection layer used in CLIP, OWL-VIT employs a linear projection of each output token to obtain per-object image embeddings. These embeddings are then used for classification, while box coordinates are derived from token representations through a small MLP. This approach allows OWL-ViT to detect objects and their spatial locations within images, a significant advancement over traditional object detection models.",q,U,Me="Below is a diagram of the Pre-training and Fine-tuning stages of OWL-ViT:",P,v,be='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/owlvit_architecture.jpg" alt="OWL-ViT Pre-training and Fine-tuning"/>',S,I,D,C,je="After fine-tuning, OWL-ViT excels in open-vocabulary object detection. It can identify objects not explicitly present in the training dataset, thanks to the shared embedding space of the vision and text encoders. This capability makes it possible to use both images and textual queries for object detection, enhancing its versatility.",K,x,ee,Z,fe="For practical applications, one typically uses text as the query and the image as context. The provided Python example demonstrates how to use the transformers library for running inference with OWL-ViT.",te,$,se,B,ye=`The image should look like this:
<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/OWL_ViT_example.jpg" alt="OWL-ViT Example"/>
This example shows a simple use case of OWL-ViT, where we specify objects likely in the dataset like cat and more abstract objects like cat tail which most likely aren’t in any object detection dataset. This shows the powerfulness of OWL-ViT in open-vocabulary object detection.`,le,V,we="Feel free to use the code to try more complex examples or this Gradio app:",ae,r,Te="",ge,ne,W,ie,R,Je="OWL-ViT’s approach to object detection represents a notable shift in how AI models understand and interact with the visual world. By integrating language understanding with visual perception, it pushes the boundaries of object detection, enabling more accurate and versatile models capable of identifying a broader range of objects. This evolution in model capabilities is crucial for applications requiring nuanced understanding of visual scenes, particularly in dynamic, real-world environments.",oe,E,Ue='For more in-depth information and technical details, refer to the <a href="https://arxiv.org/abs/2205.06230" rel="nofollow">OWL-VIT paper</a>. You can also find information about <a href="https://huggingface.co/docs/transformers/model_doc/owlv2" rel="nofollow">OWL-ViT 2 model</a> in the Hugging face documentation.',re,k,ce,X,pe;return j=new Q({props:{title:"Multimodal Object Detection (OWL-ViT)",local:"multimodal-object-detection-owl-vit",headingTag:"h1"}}),f=new Q({props:{title:"Introduction",local:"introduction",headingTag:"h3"}}),w=new Q({props:{title:"OWL-ViT: Enhancements and Capabilities",local:"owl-vit-enhancements-and-capabilities",headingTag:"h3"}}),g=new Q({props:{title:"Fine-tuning for Object Detection",local:"fine-tuning-for-object-detection",headingTag:"h4"}}),I=new Q({props:{title:"Open-Vocabulary Detection",local:"open-vocabulary-detection",headingTag:"h4"}}),x=new Q({props:{title:"Example Use Case",local:"example-use-case",headingTag:"h4"}}),$=new Re({props:{code:"aW1wb3J0JTIwcmVxdWVzdHMlMEFmcm9tJTIwUElMJTIwaW1wb3J0JTIwSW1hZ2UlMkMlMjBJbWFnZURyYXclMEFpbXBvcnQlMjB0b3JjaCUwQWZyb20lMjB0cmFuc2Zvcm1lcnMlMjBpbXBvcnQlMjBPd2xWaVRQcm9jZXNzb3IlMkMlMjBPd2xWaVRGb3JPYmplY3REZXRl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RCUyMiUwQSUyMCUyMCUyMCUyMCklMEElMjAlMjAlMjAlMjAlMjMlMjBEcmF3JTIwdGhlJTIwYm91bmRpbmclMjBib3glMjBvbiUyMHRoZSUyMGltYWdlJTBBJTIwJTIwJTIwJTIwZHJhdy5yZWN0YW5nbGUoYm94JTJDJTIwb3V0bGluZSUzRCUyMnJlZCUyMiklMEElMEElMjMlMjBEaXNwbGF5JTIwdGhlJTIwaW1hZ2UlMEFpbWFnZQ==",highlighted:`<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> OwlViTProcessor, OwlViTForObjectDetection

processor = OwlViTProcessor.from_pretrained(<span class="hljs-string">&quot;google/owlvit-base-patch32&quot;</span>)
model = OwlViTForObjectDetection.from_pretrained(<span class="hljs-string">&quot;google/owlvit-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)
texts = [[<span class="hljs-string">&quot;a photo of a cat&quot;</span>, <span class="hljs-string">&quot;a photo of a dog&quot;</span>, <span class="hljs-string">&quot;remote control&quot;</span>, <span class="hljs-string">&quot;cat tail&quot;</span>]]
inputs = processor(text=texts, images=image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
outputs = model(**inputs)

target_sizes = torch.Tensor([image.size[::-<span class="hljs-number">1</span>]])
results = processor.post_process_object_detection(
    outputs=outputs, target_sizes=target_sizes, threshold=<span class="hljs-number">0.1</span>
)
i = <span class="hljs-number">0</span>  <span class="hljs-comment"># Retrieve predictions for the first image for the corresponding text queries</span>
text = texts[i]
boxes, scores, labels = results[i][<span class="hljs-string">&quot;boxes&quot;</span>], results[i][<span class="hljs-string">&quot;scores&quot;</span>], results[i][<span class="hljs-string">&quot;labels&quot;</span>]

<span class="hljs-comment"># Create a draw object</span>
draw = ImageDraw.Draw(image)

<span class="hljs-comment"># Draw each bounding box</span>
<span class="hljs-keyword">for</span> box, score, label <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(boxes, scores, labels):
    box = [<span class="hljs-built_in">round</span>(i, <span class="hljs-number">2</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> box.tolist()]
    <span class="hljs-built_in">print</span>(
        <span class="hljs-string">f&quot;Detected <span class="hljs-subst">{text[label]}</span> with confidence <span class="hljs-subst">{<span class="hljs-built_in">round</span>(score.item(), <span class="hljs-number">3</span>)}</span> at location <span class="hljs-subst">{box}</span>&quot;</span>
    )
    <span class="hljs-comment"># Draw the bounding box on the image</span>
    draw.rectangle(box, outline=<span class="hljs-string">&quot;red&quot;</span>)

<span class="hljs-comment"># Display the image</span>
image`,wrap:!1}}),W=new Q({props:{title:"Conclusion",local:"conclusion",headingTag:"h3"}}),k=new Ee({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit4/multimodal-models/clip-and-relatives/owl_vit.mdx"}}),{c(){p=i("meta"),L=a(),_=i("p"),N=a(),m(j.$$.fragment),Y=a(),m(f.$$.fragment),z=a(),y=i("p"),y.innerHTML=de,A=a(),m(w.$$.fragment),F=a(),T=i("p"),T.textContent=ue,O=a(),m(g.$$.fragment),G=a(),J=i("p"),J.textContent=he,q=a(),U=i("p"),U.textContent=Me,P=a(),v=i("p"),v.innerHTML=be,S=a(),m(I.$$.fragment),D=a(),C=i("p"),C.textContent=je,K=a(),m(x.$$.fragment),ee=a(),Z=i("p"),Z.textContent=fe,te=a(),m($.$$.fragment),se=a(),B=i("p"),B.innerHTML=ye,le=a(),V=i("p"),V.textContent=we,ae=a(),r=i("iframe"),r.innerHTML=Te,ne=a(),m(W.$$.fragment),ie=a(),R=i("p"),R.textContent=Je,oe=a(),E=i("p"),E.innerHTML=Ue,re=a(),m(k.$$.fragment),ce=a(),X=i("p"),this.h()},l(e){const t=Ve("svelte-u9bgzb",document.head);p=o(t,"META",{name:!0,content:!0}),t.forEach(s),L=n(e),_=o(e,"P",{}),ve(_).forEach(s),N=n(e),d(j.$$.fragment,e),Y=n(e),d(f.$$.fragment,e),z=n(e),y=o(e,"P",{"data-svelte-h":!0}),c(y)!=="svelte-rcp1v5"&&(y.innerHTML=de),A=n(e),d(w.$$.fragment,e),F=n(e),T=o(e,"P",{"data-svelte-h":!0}),c(T)!=="svelte-z4tskn"&&(T.textContent=ue),O=n(e),d(g.$$.fragment,e),G=n(e),J=o(e,"P",{"data-svelte-h":!0}),c(J)!=="svelte-v134fy"&&(J.textContent=he),q=n(e),U=o(e,"P",{"data-svelte-h":!0}),c(U)!=="svelte-kfdaj8"&&(U.textContent=Me),P=n(e),v=o(e,"P",{"data-svelte-h":!0}),c(v)!=="svelte-yoirbd"&&(v.innerHTML=be),S=n(e),d(I.$$.fragment,e),D=n(e),C=o(e,"P",{"data-svelte-h":!0}),c(C)!=="svelte-tptnjo"&&(C.textContent=je),K=n(e),d(x.$$.fragment,e),ee=n(e),Z=o(e,"P",{"data-svelte-h":!0}),c(Z)!=="svelte-z24mom"&&(Z.textContent=fe),te=n(e),d($.$$.fragment,e),se=n(e),B=o(e,"P",{"data-svelte-h":!0}),c(B)!=="svelte-85rdgm"&&(B.innerHTML=ye),le=n(e),V=o(e,"P",{"data-svelte-h":!0}),c(V)!=="svelte-1n8z8mw"&&(V.textContent=we),ae=n(e),r=o(e,"IFRAME",{src:!0,frameborder:!0,width:!0,height:!0,"data-svelte-h":!0}),c(r)!=="svelte-iir95p"&&(r.innerHTML=Te),ne=n(e),d(W.$$.fragment,e),ie=n(e),R=o(e,"P",{"data-svelte-h":!0}),c(R)!=="svelte-gx5huh"&&(R.textContent=Je),oe=n(e),E=o(e,"P",{"data-svelte-h":!0}),c(E)!=="svelte-1y0r6n7"&&(E.innerHTML=Ue),re=n(e),d(k.$$.fragment,e),ce=n(e),X=o(e,"P",{}),ve(X).forEach(s),this.h()},h(){H(p,"name","hf:doc:metadata"),H(p,"content",Qe),Ce(r.src,ge="https://johko-OWL-ViT.hf.space")||H(r,"src",ge),H(r,"frameborder","0"),H(r,"width","850"),H(r,"height","450")},m(e,t){We(document.head,p),l(e,L,t),l(e,_,t),l(e,N,t),u(j,e,t),l(e,Y,t),u(f,e,t),l(e,z,t),l(e,y,t),l(e,A,t),u(w,e,t),l(e,F,t),l(e,T,t),l(e,O,t),u(g,e,t),l(e,G,t),l(e,J,t),l(e,q,t),l(e,U,t),l(e,P,t),l(e,v,t),l(e,S,t),u(I,e,t),l(e,D,t),l(e,C,t),l(e,K,t),u(x,e,t),l(e,ee,t),l(e,Z,t),l(e,te,t),u($,e,t),l(e,se,t),l(e,B,t),l(e,le,t),l(e,V,t),l(e,ae,t),l(e,r,t),l(e,ne,t),u(W,e,t),l(e,ie,t),l(e,R,t),l(e,oe,t),l(e,E,t),l(e,re,t),u(k,e,t),l(e,ce,t),l(e,X,t),pe=!0},p:xe,i(e){pe||(h(j.$$.fragment,e),h(f.$$.fragment,e),h(w.$$.fragment,e),h(g.$$.fragment,e),h(I.$$.fragment,e),h(x.$$.fragment,e),h($.$$.fragment,e),h(W.$$.fragment,e),h(k.$$.fragment,e),pe=!0)},o(e){M(j.$$.fragment,e),M(f.$$.fragment,e),M(w.$$.fragment,e),M(g.$$.fragment,e),M(I.$$.fragment,e),M(x.$$.fragment,e),M($.$$.fragment,e),M(W.$$.fragment,e),M(k.$$.fragment,e),pe=!1},d(e){e&&(s(L),s(_),s(N),s(Y),s(z),s(y),s(A),s(F),s(T),s(O),s(G),s(J),s(q),s(U),s(P),s(v),s(S),s(D),s(C),s(K),s(ee),s(Z),s(te),s(se),s(B),s(le),s(V),s(ae),s(r),s(ne),s(ie),s(R),s(oe),s(E),s(re),s(ce),s(X)),s(p),b(j,e),b(f,e),b(w,e),b(g,e),b(I,e),b(x,e),b($,e),b(W,e),b(k,e)}}}const Qe='{"title":"Multimodal Object Detection (OWL-ViT)","local":"multimodal-object-detection-owl-vit","sections":[{"title":"Introduction","local":"introduction","sections":[],"depth":3},{"title":"OWL-ViT: Enhancements and Capabilities","local":"owl-vit-enhancements-and-capabilities","sections":[{"title":"Fine-tuning for Object Detection","local":"fine-tuning-for-object-detection","sections":[],"depth":4},{"title":"Open-Vocabulary Detection","local":"open-vocabulary-detection","sections":[],"depth":4},{"title":"Example Use Case","local":"example-use-case","sections":[],"depth":4}],"depth":3},{"title":"Conclusion","local":"conclusion","sections":[],"depth":3}],"depth":1}';function He(me){return Ze(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ye extends $e{constructor(p){super(),Be(this,p,He,ke,Ie,{})}}export{Ye as component};
