import{s as Ye,f as Ve,o as De}from"../chunks/scheduler.7bc62968.js";import{S as Re,i as Ge,g as l,s as a,r as p,A as Xe,h as o,f as s,c as i,j as He,u as g,x as r,k as ke,y as Oe,a as n,v as c,d as f,t as u,w as d,m as Ke,n as et}from"../chunks/index.2f8492b0.js";import{T as tt}from"../chunks/Tip.016f38d9.js";import{C as Pe}from"../chunks/CodeBlock.bb61a5a9.js";import{H as Y,E as st}from"../chunks/getInferenceSnippets.6109a300.js";function nt(D){let m;return{c(){m=Ke(`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=et(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 at(D){let m,h,T,R,w,G,y,Ie="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.",X,M,O,J,K,b,je='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.',ee,U,Ce="Despite their excellent results over the past decade, CNN-based models have some limitations, which Transformers aim to solve:",te,$,ve='<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>',se,k,ne,I,xe='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.',ae,j,ie,C,Ne="The figure below shows the architecture diagram taken from the paper.",le,v,Se,oe,x,Ee="The architecture is composed of three components:",re,N,Be="<strong>Pixel-level Module</strong>: Uses a backbone to extract image features and a pixel decoder to generate per-pixel embeddings.",me,S,qe="<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.",pe,E,We="<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.",ge,B,_e='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.',ce,q,fe,W,ze="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.",ue,_,de,z,Fe="As you can see below, the results include multiple instances of the same classes, each with distinct masks.",he,F,Me,A,Te,L,Ae=`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.`,we,Z,Le='<a href="https://colab.research.google.com/github/johko/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.',ye,Q,Je,H,Ze='<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>',be,P,Ue,V,$e;return w=new Y({props:{title:"Transformer-based image segmentation",local:"transformer-based-image-segmentation",headingTag:"h1"}}),M=new tt({props:{warning:!0,$$slots:{default:[nt]},$$scope:{ctx:D}}}),J=new Y({props:{title:"CNNs vs Transformers for Segmentation",local:"cnns-vs-transformers-for-segmentation",headingTag:"h2"}}),k=new Y({props:{title:"Spotlight on MaskFormer: Illustrating ViT for Image Segmentation",local:"spotlight-on-maskformer-illustrating-vit-for-image-segmentation",headingTag:"h2"}}),j=new Y({props:{title:"MaskFormer Architecture",local:"maskformer-architecture",headingTag:"h3"}}),q=new Y({props:{title:"Panoptic Segmentation Inference Example with Hugging Face Transformers",local:"panoptic-segmentation-inference-example-with-hugging-face-transformers",headingTag:"h3"}}),_=new Pe({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBZnJvbSUyMFBJTCUyMGltcG9ydCUyMEltYWdlJTBBaW1wb3J0JTIwcmVxdWVzdHMlMEElMEFzZWdtZW50YXRpb24lMjAlM0QlMjBwaXBlbGluZSglMjJpbWFnZS1zZWdtZW50YXRpb24lMjIlMkMlMjAlMjJmYWNlYm9vayUyRm1hc2tmb3JtZXItc3dpbi1iYXNlLWNvY28lMjIpJTBBJTBBdXJsJTIwJTNEJTIwJTIyaHR0cCUzQSUyRiUyRmltYWdlcy5jb2NvZGF0YXNldC5vcmclMkZ2YWwyMDE3JTJGMDAwMDAwMDM5NzY5LmpwZyUyMiUwQWltYWdlJTIwJTNEJTIwSW1hZ2Uub3BlbihyZXF1ZXN0cy5nZXQodXJsJTJDJTIwc3RyZWFtJTNEVHJ1ZSkucmF3KSUwQSUwQXJlc3VsdHMlMjAlM0QlMjBzZWdtZW50YXRpb24oaW1hZ2VzJTNEaW1hZ2UlMkMlMjBzdWJ0YXNrJTNEJTIycGFub3B0aWMlMjIpJTBBcmVzdWx0cw==",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(images=image, subtask=<span class="hljs-string">&quot;panoptic&quot;</span>)
results`,wrap:!1}}),F=new Pe({props:{code:"JTVCJTBBJTIwJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIyc2NvcmUlMjIlM0ElMjAwLjk5MzE5NyUyQyUwQSUyMCUyMCUyMCUyMCUyMmxhYmVsJTIyJTNBJTIwJTIycmVtb3RlJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIybWFzayUyMiUzQSUyMCUzQ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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 Y({props:{title:"Fine-tuning Vision Transformer-based Segmentation Models",local:"fine-tuning-vision-transformer-based-segmentation-models",headingTag:"h2"}}),Q=new Y({props:{title:"References",local:"references",headingTag:"h2"}}),P=new st({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"),R=a(),p(w.$$.fragment),G=a(),y=l("p"),y.textContent=Ie,X=a(),p(M.$$.fragment),O=a(),p(J.$$.fragment),K=a(),b=l("p"),b.innerHTML=je,ee=a(),U=l("p"),U.textContent=Ce,te=a(),$=l("ul"),$.innerHTML=ve,se=a(),p(k.$$.fragment),ne=a(),I=l("p"),I.innerHTML=xe,ae=a(),p(j.$$.fragment),ie=a(),C=l("p"),C.textContent=Ne,le=a(),v=l("img"),oe=a(),x=l("p"),x.textContent=Ee,re=a(),N=l("p"),N.innerHTML=Be,me=a(),S=l("p"),S.innerHTML=qe,pe=a(),E=l("p"),E.innerHTML=We,ge=a(),B=l("p"),B.innerHTML=_e,ce=a(),p(q.$$.fragment),fe=a(),W=l("p"),W.textContent=ze,ue=a(),p(_.$$.fragment),de=a(),z=l("p"),z.textContent=Fe,he=a(),p(F.$$.fragment),Me=a(),p(A.$$.fragment),Te=a(),L=l("p"),L.textContent=Ae,we=a(),Z=l("p"),Z.innerHTML=Le,ye=a(),p(Q.$$.fragment),Je=a(),H=l("ul"),H.innerHTML=Ze,be=a(),p(P.$$.fragment),Ue=a(),V=l("p"),this.h()},l(e){const t=Xe("svelte-u9bgzb",document.head);m=o(t,"META",{name:!0,content:!0}),t.forEach(s),h=i(e),T=o(e,"P",{}),He(T).forEach(s),R=i(e),g(w.$$.fragment,e),G=i(e),y=o(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1b5yok3"&&(y.textContent=Ie),X=i(e),g(M.$$.fragment,e),O=i(e),g(J.$$.fragment,e),K=i(e),b=o(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-1abdguv"&&(b.innerHTML=je),ee=i(e),U=o(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-lev539"&&(U.textContent=Ce),te=i(e),$=o(e,"UL",{"data-svelte-h":!0}),r($)!=="svelte-nz5bha"&&($.innerHTML=ve),se=i(e),g(k.$$.fragment,e),ne=i(e),I=o(e,"P",{"data-svelte-h":!0}),r(I)!=="svelte-1mnhmap"&&(I.innerHTML=xe),ae=i(e),g(j.$$.fragment,e),ie=i(e),C=o(e,"P",{"data-svelte-h":!0}),r(C)!=="svelte-1sj3bsz"&&(C.textContent=Ne),le=i(e),v=o(e,"IMG",{width:!0,src:!0}),oe=i(e),x=o(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-9n28dl"&&(x.textContent=Ee),re=i(e),N=o(e,"P",{"data-svelte-h":!0}),r(N)!=="svelte-1hsyotc"&&(N.innerHTML=Be),me=i(e),S=o(e,"P",{"data-svelte-h":!0}),r(S)!=="svelte-wt39oi"&&(S.innerHTML=qe),pe=i(e),E=o(e,"P",{"data-svelte-h":!0}),r(E)!=="svelte-fx562s"&&(E.innerHTML=We),ge=i(e),B=o(e,"P",{"data-svelte-h":!0}),r(B)!=="svelte-mn68zr"&&(B.innerHTML=_e),ce=i(e),g(q.$$.fragment,e),fe=i(e),W=o(e,"P",{"data-svelte-h":!0}),r(W)!=="svelte-juntl"&&(W.textContent=ze),ue=i(e),g(_.$$.fragment,e),de=i(e),z=o(e,"P",{"data-svelte-h":!0}),r(z)!=="svelte-7k1roe"&&(z.textContent=Fe),he=i(e),g(F.$$.fragment,e),Me=i(e),g(A.$$.fragment,e),Te=i(e),L=o(e,"P",{"data-svelte-h":!0}),r(L)!=="svelte-dsjzxd"&&(L.textContent=Ae),we=i(e),Z=o(e,"P",{"data-svelte-h":!0}),r(Z)!=="svelte-6nhzio"&&(Z.innerHTML=Le),ye=i(e),g(Q.$$.fragment,e),Je=i(e),H=o(e,"UL",{"data-svelte-h":!0}),r(H)!=="svelte-1enbp79"&&(H.innerHTML=Ze),be=i(e),g(P.$$.fragment,e),Ue=i(e),V=o(e,"P",{}),He(V).forEach(s),this.h()},h(){ke(m,"name","hf:doc:metadata"),ke(m,"content",it),ke(v,"width","600"),Ve(v.src,Se="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/maskformer_architecture.png")||ke(v,"src",Se)},m(e,t){Oe(document.head,m),n(e,h,t),n(e,T,t),n(e,R,t),c(w,e,t),n(e,G,t),n(e,y,t),n(e,X,t),c(M,e,t),n(e,O,t),c(J,e,t),n(e,K,t),n(e,b,t),n(e,ee,t),n(e,U,t),n(e,te,t),n(e,$,t),n(e,se,t),c(k,e,t),n(e,ne,t),n(e,I,t),n(e,ae,t),c(j,e,t),n(e,ie,t),n(e,C,t),n(e,le,t),n(e,v,t),n(e,oe,t),n(e,x,t),n(e,re,t),n(e,N,t),n(e,me,t),n(e,S,t),n(e,pe,t),n(e,E,t),n(e,ge,t),n(e,B,t),n(e,ce,t),c(q,e,t),n(e,fe,t),n(e,W,t),n(e,ue,t),c(_,e,t),n(e,de,t),n(e,z,t),n(e,he,t),c(F,e,t),n(e,Me,t),c(A,e,t),n(e,Te,t),n(e,L,t),n(e,we,t),n(e,Z,t),n(e,ye,t),c(Q,e,t),n(e,Je,t),n(e,H,t),n(e,be,t),c(P,e,t),n(e,Ue,t),n(e,V,t),$e=!0},p(e,[t]){const Qe={};t&2&&(Qe.$$scope={dirty:t,ctx:e}),M.$set(Qe)},i(e){$e||(f(w.$$.fragment,e),f(M.$$.fragment,e),f(J.$$.fragment,e),f(k.$$.fragment,e),f(j.$$.fragment,e),f(q.$$.fragment,e),f(_.$$.fragment,e),f(F.$$.fragment,e),f(A.$$.fragment,e),f(Q.$$.fragment,e),f(P.$$.fragment,e),$e=!0)},o(e){u(w.$$.fragment,e),u(M.$$.fragment,e),u(J.$$.fragment,e),u(k.$$.fragment,e),u(j.$$.fragment,e),u(q.$$.fragment,e),u(_.$$.fragment,e),u(F.$$.fragment,e),u(A.$$.fragment,e),u(Q.$$.fragment,e),u(P.$$.fragment,e),$e=!1},d(e){e&&(s(h),s(T),s(R),s(G),s(y),s(X),s(O),s(K),s(b),s(ee),s(U),s(te),s($),s(se),s(ne),s(I),s(ae),s(ie),s(C),s(le),s(v),s(oe),s(x),s(re),s(N),s(me),s(S),s(pe),s(E),s(ge),s(B),s(ce),s(fe),s(W),s(ue),s(de),s(z),s(he),s(Me),s(Te),s(L),s(we),s(Z),s(ye),s(Je),s(H),s(be),s(Ue),s(V)),s(m),d(w,e),d(M,e),d(J,e),d(k,e),d(j,e),d(q,e),d(_,e),d(F,e),d(A,e),d(Q,e),d(P,e)}}}const it='{"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 lt(D){return De(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ct extends Re{constructor(m){super(),Ge(this,m,lt,at,Ye,{})}}export{ct as component};
