import{s as Re,a as Ve,o as Ge}from"../chunks/scheduler.35aab934.js";import{S as Xe,i as Oe,e as l,s as a,c as p,h as Ke,a as o,d as s,b as i,f as Ye,g,j as r,k as Ie,l as et,m as n,n as c,t as f,o as u,p as d,q as tt,r as st}from"../chunks/index.de17b971.js";import{T as nt}from"../chunks/Tip.514d77c4.js";import{C as at}from"../chunks/CopyLLMTxtMenu.48bc5cc2.js";import{C as De}from"../chunks/CodeBlock.c1f62de5.js";import{H as D,E as it}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.8fdece89.js";function lt(V){let m;return{c(){m=tt(`This section assumes familiarity with image segmentation, Convolutional Neural
  Networks (CNNs), and the basics of Vision Transformers. If you're new to these
  concepts, we recommend exploring related materials in the course before
  proceeding.`)},l(h){m=st(h,`This section assumes familiarity with image segmentation, Convolutional Neural
  Networks (CNNs), and the basics of Vision Transformers. If you're new to these
  concepts, we recommend exploring related materials in the course before
  proceeding.`)},m(h,T){n(h,m,T)},d(h){h&&s(m)}}}function ot(V){let m,h,T,G,w,X,y,O,J,je="In this section, we’ll explore how Vision Transformers compare to Convolutional Neural Networks (CNNs) in image segmentation and detail the architecture of a vision transformer-based segmentation model as an example.",K,M,ee,b,te,$,ve='Before the emergence of Vision Transformers, CNNs (Convolutional Neural Networks) have been the go-to choice for image segmentation. Models like <a href="https://arxiv.org/abs/1505.04597" rel="nofollow">U-Net</a> and <a href="https://arxiv.org/abs/1703.06870" rel="nofollow">Mask R-CNN</a> captured the details that are needed to distinguish different objects in an image, making them state-of-the-art for segmentation tasks.',se,U,xe="Despite their excellent results over the past decade, CNN-based models have some limitations, which Transformers aim to solve:",ne,k,Ne='<li><strong>Spatial limitations</strong>: CNNs learn local patterns through small receptive fields. This local focus makes it hard for them to “link” features that are far apart but related within the image, affecting their ability to accurately segment complex scenes/objects. Unlike CNNs, ViTs are designed to capture global dependencies within an image, leveraging the attention mechanism. This means ViT-based models consider the entire image at once, allowing them to understand complex relationships between distant parts of an image. For segmentation, this global perspective can lead to a more accurate delineation of objects.</li> <li><strong>Task-Specific Components</strong>: Methods like Mask R-CNN incorporate hand-designed components (e.g., non-maximum suppression, spatial anchors) to encode prior knowledge about segmentation tasks. These components add complexity and require manual tuning. In contrast, ViT-based segmentation methods simplify the segmentation process by eliminating the need for hand-designed components, making them more straightforward to optimize.</li> <li><strong>Segmentation Task Specialization</strong>: CNN-based segmentation models approach semantic, instance, and panoptic segmentation tasks individually, leading to specialized architectures for each task and separate research efforts into each. Recent ViT-based models like <a href="https://arxiv.org/abs/2107.06278" rel="nofollow">MaskFormer</a>, <a href="https://arxiv.org/abs/2105.15203" rel="nofollow">SegFormer</a> or <a href="https://arxiv.org/abs/2304.02643" rel="nofollow">SAM</a> provide a unified approach to tackling semantic, instance, and panoptic segmentation tasks within a single framework.</li>',ae,C,ie,I,Se='MaskFormer (<a href="https://arxiv.org/abs/2107.06278" rel="nofollow">paper</a>, <a href="https://huggingface.co/docs/transformers/en/model_doc/maskformer" rel="nofollow">Hugging Face transformers documentation</a>), introduced in the paper “MaskFormer: Per-Pixel Classification is Not All You Need for Semantic Segmentation” is a model that predicts segmentation masks for each class present in an image, unifying semantic and instance segmentation in one architecture.',le,j,oe,v,Ee="The figure below shows the architecture diagram taken from the paper.",re,x,Be,me,N,qe="The architecture is composed of three components:",pe,S,We="<strong>Pixel-level Module</strong>: Uses a backbone to extract image features and a pixel decoder to generate per-pixel embeddings.",ge,E,_e="<strong>Transformer Module</strong>: Employs a standard Transformer decoder to compute per-segment embeddings from image features and learnable positional embeddings (queries), encoding global information about each segment.",ce,B,ze="<strong>Segmentation Module</strong>: Generates class probability predictions and mask embeddings for each segment using a linear classifier and a Multi-Layer Perceptron (MLP), respectively. The mask embeddings are used in combination with per-pixel embeddings to predict binary masks for each segment.",fe,q,Fe='The model is trained with a binary mask loss, the same one as <a href="https://huggingface.co/learn/computer-vision-course/unit3/vision-transformers/detr" rel="nofollow">DETR</a>, and a cross-entropy classification loss per predicted segment.',ue,W,de,_,Le="Panoptic segmentation is the task of labeling every pixel in an image with its category and identifying distinct objects within those categories, combining both semantic and instance segmentation.",he,z,Me,F,Ae="As you can see below, the results include multiple instances of the same classes, each with distinct masks.",Te,L,we,A,ye,Z,Ze=`With many pre-trained segmentation models available, transfer learning and finetuning are commonly used to adapt these models to specific use cases, especially since transformer-based segmentation models, like MaskFormer, are data-hungry and challenging to train from scratch.
These techniques leverage pre-trained representations to adapt these models to new data efficiently. Typically, for MaskFormer, the backbone, the pixel decoder, and the transformer decoder are kept frozen to leverage their learned general features, while the transformer module is finetuned to adapt its class prediction and mask generation capabilities to new segmentation tasks.`,Je,Q,Qe='<a href="https://colab.research.google.com/github/huggingface/computer-vision-course/blob/main/notebooks/Unit%203%20-%20Vision%20Transformers/transfer-learning-segmentation.ipynb" rel="nofollow">This notebook</a> will walk you through a transfer learning tutorial on image segmentation using MaskFormer.',be,H,$e,P,He='<li><a href="https://huggingface.co/docs/transformers/en/model_doc/maskformer" rel="nofollow">MaskFormer Hugging Face documentation</a></li> <li><a href="https://huggingface.co/docs/transformers/en/tasks/semantic_segmentation" rel="nofollow">Image Segmentation Hugging Face Task Guide</a></li>',Ue,Y,ke,R,Ce;return w=new at({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),y=new D({props:{title:"Transformer-based image segmentation",local:"transformer-based-image-segmentation",headingTag:"h1"}}),M=new nt({props:{warning:!0,$$slots:{default:[lt]},$$scope:{ctx:V}}}),b=new D({props:{title:"CNNs vs Transformers for Segmentation",local:"cnns-vs-transformers-for-segmentation",headingTag:"h2"}}),C=new D({props:{title:"Spotlight on MaskFormer: Illustrating ViT for Image Segmentation",local:"spotlight-on-maskformer-illustrating-vit-for-image-segmentation",headingTag:"h2"}}),j=new D({props:{title:"MaskFormer Architecture",local:"maskformer-architecture",headingTag:"h3"}}),W=new D({props:{title:"Panoptic Segmentation Inference Example with Hugging Face Transformers",local:"panoptic-segmentation-inference-example-with-hugging-face-transformers",headingTag:"h3"}}),z=new De({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwcmVxdWVzdHMlMEElMEFzZWdtZW50YXRpb24lMjAlM0QlMjBwaXBlbGluZSglMjJpbWFnZS1zZWdtZW50YXRpb24lMjIlMkMlMjAlMjJmYWNlYm9vayUyRm1hc2tmb3JtZXItc3dpbi1iYXNlLWNvY28lMjIpJTBBJTBBdXJsJTIwJTNEJTIwJTIyaHR0cCUzQSUyRiUyRmltYWdlcy5jb2NvZGF0YXNldC5vcmclMkZ2YWwyMDE3JTJGMDAwMDAwMDM5NzY5LmpwZyUyMiUwQWltYWdlJTIwJTNEJTIwSW1hZ2Uub3BlbihyZXF1ZXN0cy5nZXQodXJsJTJDJTIwc3RyZWFtJTNEVHJ1ZSkucmF3KSUwQSUwQXJlc3VsdHMlMjAlM0QlMjBzZWdtZW50YXRpb24oaW5wdXRzJTNEaW1hZ2UlMkMlMjBzdWJ0YXNrJTNEJTIycGFub3B0aWMlMjIpJTBBcmVzdWx0cw==",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">import</span> requests

segmentation = pipeline(<span class="hljs-string">&quot;image-segmentation&quot;</span>, <span class="hljs-string">&quot;facebook/maskformer-swin-base-coco&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)

results = segmentation(inputs=image, subtask=<span class="hljs-string">&quot;panoptic&quot;</span>)
results`,wrap:!1}}),L=new De({props:{code:"JTVCJTBBJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIyc2NvcmUlMjIlM0ElMjAwLjk5MzE5NyUyQyUwQSUyMCUyM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jQweDQ4MCUyMGF0JTIwMHgxN0VFODcxMDAlM0UlMEElMjAlMjAlN0QlMEElNUQ=",highlighted:`[
  {
    <span class="hljs-string">&quot;score&quot;</span>: 0.993197,
    <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;remote&quot;</span>,
    <span class="hljs-string">&quot;mask&quot;</span>: &lt;PIL.Image.Image image mode=L size=640x480 at 0x109363910&gt;
  },
  {
    <span class="hljs-string">&quot;score&quot;</span>: 0.997852,
    <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;cat&quot;</span>,
    <span class="hljs-string">&quot;mask&quot;</span>: &lt;PIL.Image.Image image mode=L size=640x480 at 0x1093635B0&gt;
  },
  {
    <span class="hljs-string">&quot;score&quot;</span>: 0.998006,
    <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;remote&quot;</span>,
    <span class="hljs-string">&quot;mask&quot;</span>: &lt;PIL.Image.Image image mode=L size=640x480 at 0x17EE84670&gt;
  },
  {
    <span class="hljs-string">&quot;score&quot;</span>: 0.997469,
    <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-string">&quot;cat&quot;</span>,
    <span class="hljs-string">&quot;mask&quot;</span>: &lt;PIL.Image.Image image mode=L size=640x480 at 0x17EE87100&gt;
  }
]`,wrap:!1}}),A=new D({props:{title:"Fine-tuning Vision Transformer-based Segmentation Models",local:"fine-tuning-vision-transformer-based-segmentation-models",headingTag:"h2"}}),H=new D({props:{title:"References",local:"references",headingTag:"h2"}}),Y=new it({props:{source:"https://github.com/huggingface/computer-vision-course/blob/main/chapters/en/unit3/vision-transformers/vision-transformers-for-image-segmentation.mdx"}}),{c(){m=l("meta"),h=a(),T=l("p"),G=a(),p(w.$$.fragment),X=a(),p(y.$$.fragment),O=a(),J=l("p"),J.textContent=je,K=a(),p(M.$$.fragment),ee=a(),p(b.$$.fragment),te=a(),$=l("p"),$.innerHTML=ve,se=a(),U=l("p"),U.textContent=xe,ne=a(),k=l("ul"),k.innerHTML=Ne,ae=a(),p(C.$$.fragment),ie=a(),I=l("p"),I.innerHTML=Se,le=a(),p(j.$$.fragment),oe=a(),v=l("p"),v.textContent=Ee,re=a(),x=l("img"),me=a(),N=l("p"),N.textContent=qe,pe=a(),S=l("p"),S.innerHTML=We,ge=a(),E=l("p"),E.innerHTML=_e,ce=a(),B=l("p"),B.innerHTML=ze,fe=a(),q=l("p"),q.innerHTML=Fe,ue=a(),p(W.$$.fragment),de=a(),_=l("p"),_.textContent=Le,he=a(),p(z.$$.fragment),Me=a(),F=l("p"),F.textContent=Ae,Te=a(),p(L.$$.fragment),we=a(),p(A.$$.fragment),ye=a(),Z=l("p"),Z.textContent=Ze,Je=a(),Q=l("p"),Q.innerHTML=Qe,be=a(),p(H.$$.fragment),$e=a(),P=l("ul"),P.innerHTML=He,Ue=a(),p(Y.$$.fragment),ke=a(),R=l("p"),this.h()},l(e){const t=Ke("svelte-u9bgzb",document.head);m=o(t,"META",{name:!0,content:!0}),t.forEach(s),h=i(e),T=o(e,"P",{}),Ye(T).forEach(s),G=i(e),g(w.$$.fragment,e),X=i(e),g(y.$$.fragment,e),O=i(e),J=o(e,"P",{"data-svelte-h":!0}),r(J)!=="svelte-1b5yok3"&&(J.textContent=je),K=i(e),g(M.$$.fragment,e),ee=i(e),g(b.$$.fragment,e),te=i(e),$=o(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-1abdguv"&&($.innerHTML=ve),se=i(e),U=o(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-lev539"&&(U.textContent=xe),ne=i(e),k=o(e,"UL",{"data-svelte-h":!0}),r(k)!=="svelte-nz5bha"&&(k.innerHTML=Ne),ae=i(e),g(C.$$.fragment,e),ie=i(e),I=o(e,"P",{"data-svelte-h":!0}),r(I)!=="svelte-1mnhmap"&&(I.innerHTML=Se),le=i(e),g(j.$$.fragment,e),oe=i(e),v=o(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-1sj3bsz"&&(v.textContent=Ee),re=i(e),x=o(e,"IMG",{width:!0,src:!0}),me=i(e),N=o(e,"P",{"data-svelte-h":!0}),r(N)!=="svelte-9n28dl"&&(N.textContent=qe),pe=i(e),S=o(e,"P",{"data-svelte-h":!0}),r(S)!=="svelte-1hsyotc"&&(S.innerHTML=We),ge=i(e),E=o(e,"P",{"data-svelte-h":!0}),r(E)!=="svelte-wt39oi"&&(E.innerHTML=_e),ce=i(e),B=o(e,"P",{"data-svelte-h":!0}),r(B)!=="svelte-fx562s"&&(B.innerHTML=ze),fe=i(e),q=o(e,"P",{"data-svelte-h":!0}),r(q)!=="svelte-mn68zr"&&(q.innerHTML=Fe),ue=i(e),g(W.$$.fragment,e),de=i(e),_=o(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-juntl"&&(_.textContent=Le),he=i(e),g(z.$$.fragment,e),Me=i(e),F=o(e,"P",{"data-svelte-h":!0}),r(F)!=="svelte-7k1roe"&&(F.textContent=Ae),Te=i(e),g(L.$$.fragment,e),we=i(e),g(A.$$.fragment,e),ye=i(e),Z=o(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-dsjzxd"&&(Z.textContent=Ze),Je=i(e),Q=o(e,"P",{"data-svelte-h":!0}),r(Q)!=="svelte-1k9svch"&&(Q.innerHTML=Qe),be=i(e),g(H.$$.fragment,e),$e=i(e),P=o(e,"UL",{"data-svelte-h":!0}),r(P)!=="svelte-1enbp79"&&(P.innerHTML=He),Ue=i(e),g(Y.$$.fragment,e),ke=i(e),R=o(e,"P",{}),Ye(R).forEach(s),this.h()},h(){Ie(m,"name","hf:doc:metadata"),Ie(m,"content",rt),Ie(x,"width","600"),Ve(x.src,Be="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/maskformer_architecture.png")||Ie(x,"src",Be)},m(e,t){et(document.head,m),n(e,h,t),n(e,T,t),n(e,G,t),c(w,e,t),n(e,X,t),c(y,e,t),n(e,O,t),n(e,J,t),n(e,K,t),c(M,e,t),n(e,ee,t),c(b,e,t),n(e,te,t),n(e,$,t),n(e,se,t),n(e,U,t),n(e,ne,t),n(e,k,t),n(e,ae,t),c(C,e,t),n(e,ie,t),n(e,I,t),n(e,le,t),c(j,e,t),n(e,oe,t),n(e,v,t),n(e,re,t),n(e,x,t),n(e,me,t),n(e,N,t),n(e,pe,t),n(e,S,t),n(e,ge,t),n(e,E,t),n(e,ce,t),n(e,B,t),n(e,fe,t),n(e,q,t),n(e,ue,t),c(W,e,t),n(e,de,t),n(e,_,t),n(e,he,t),c(z,e,t),n(e,Me,t),n(e,F,t),n(e,Te,t),c(L,e,t),n(e,we,t),c(A,e,t),n(e,ye,t),n(e,Z,t),n(e,Je,t),n(e,Q,t),n(e,be,t),c(H,e,t),n(e,$e,t),n(e,P,t),n(e,Ue,t),c(Y,e,t),n(e,ke,t),n(e,R,t),Ce=!0},p(e,[t]){const Pe={};t&2&&(Pe.$$scope={dirty:t,ctx:e}),M.$set(Pe)},i(e){Ce||(f(w.$$.fragment,e),f(y.$$.fragment,e),f(M.$$.fragment,e),f(b.$$.fragment,e),f(C.$$.fragment,e),f(j.$$.fragment,e),f(W.$$.fragment,e),f(z.$$.fragment,e),f(L.$$.fragment,e),f(A.$$.fragment,e),f(H.$$.fragment,e),f(Y.$$.fragment,e),Ce=!0)},o(e){u(w.$$.fragment,e),u(y.$$.fragment,e),u(M.$$.fragment,e),u(b.$$.fragment,e),u(C.$$.fragment,e),u(j.$$.fragment,e),u(W.$$.fragment,e),u(z.$$.fragment,e),u(L.$$.fragment,e),u(A.$$.fragment,e),u(H.$$.fragment,e),u(Y.$$.fragment,e),Ce=!1},d(e){e&&(s(h),s(T),s(G),s(X),s(O),s(J),s(K),s(ee),s(te),s($),s(se),s(U),s(ne),s(k),s(ae),s(ie),s(I),s(le),s(oe),s(v),s(re),s(x),s(me),s(N),s(pe),s(S),s(ge),s(E),s(ce),s(B),s(fe),s(q),s(ue),s(de),s(_),s(he),s(Me),s(F),s(Te),s(we),s(ye),s(Z),s(Je),s(Q),s(be),s($e),s(P),s(Ue),s(ke),s(R)),s(m),d(w,e),d(y,e),d(M,e),d(b,e),d(C,e),d(j,e),d(W,e),d(z,e),d(L,e),d(A,e),d(H,e),d(Y,e)}}}const rt='{"title":"Transformer-based image segmentation","local":"transformer-based-image-segmentation","sections":[{"title":"CNNs vs Transformers for Segmentation","local":"cnns-vs-transformers-for-segmentation","sections":[],"depth":2},{"title":"Spotlight on MaskFormer: Illustrating ViT for Image Segmentation","local":"spotlight-on-maskformer-illustrating-vit-for-image-segmentation","sections":[{"title":"MaskFormer Architecture","local":"maskformer-architecture","sections":[],"depth":3},{"title":"Panoptic Segmentation Inference Example with Hugging Face Transformers","local":"panoptic-segmentation-inference-example-with-hugging-face-transformers","sections":[],"depth":3}],"depth":2},{"title":"Fine-tuning Vision Transformer-based Segmentation Models","local":"fine-tuning-vision-transformer-based-segmentation-models","sections":[],"depth":2},{"title":"References","local":"references","sections":[],"depth":2}],"depth":1}';function mt(V){return Ge(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ht extends Xe{constructor(m){super(),Oe(this,m,mt,ot,Re,{})}}export{ht as component};
