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