import{s as Je,n as We,o as Ze}from"../chunks/scheduler.7bc62968.js";import{S as Re,i as Ae,g as o,s as a,r as J,A as qe,h as l,f as n,c as s,j as Pe,u as W,x as r,k as je,y as Ge,a as i,v as Z,d as R,t as A,w as q}from"../chunks/index.2f8492b0.js";import{C as ze}from"../chunks/CodeBlock.4e96d6f7.js";import{H as de}from"../chunks/Heading.14fb2476.js";function Fe(ge){let m,G,P,z,h,F,c,fe=`Image segmentation is dividing an image into meaningful segments. It’s all about creating masks that spotlight each object in the picture.
The intuition behind this task is <em>that it can be viewed as a classification for each pixel of the image</em>.
Segmentation models are the
core models in various industries. They can be found in agriculture and autonomous driving. In the farming world, these models are used
for identifying different land sections and assessing the growth stage of crops. They’re also key players for self-driving cars, where
they are used to identify lanes, sidewalks, and other road users.`,B,p,ue='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/segmentation-example.png" alt="Image segmentation"/>',E,d,ve=`Different types of segmentations can be applied depending on the context and the intended goal.
The most commonly defined segmentations are the following.`,V,g,we=`<li><strong>Semantic Segmentation</strong>:  This involves assigning the most probable class to each pixel. For example, in semantic segmentation,
the model does not distinguish between two individual cats but rather focuses on the pixel class. It’s all about classification of
each pixel.</li> <li><strong>Instance Segmentation</strong>: This type involves identifying each instance of an object with a unique mask. It combines aspects of
object detection and segmentation to differentiate between individual objects of the same class.</li> <li><strong>Panoptic Segmentation</strong>: A hybrid approach that combines elements of semantic and instance segmentation. It assigns a class and
an instance to each pixel, effectively integrating the <em>what</em> and <em>where</em> aspects of the image.</li>`,Q,f,ye='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/segmentation-types.png" alt="Comparison of segmentation types"/>',Y,u,be=`Choosing the right segmentation type depends on the context and the intended goal. One cool thing is that recent models allow you to achieve the three
segmentation types with a single model. We recommend you to check out this <a href="https://huggingface.co/blog/mask2former" rel="nofollow">article</a>, which introduces Mask2former,
a new model by Meta that achieves the three segmentation types with only a Panoptic dataset.`,D,v,N,w,Te=`You’ve probably heard of U-Net, a popular network used for image segmentation. It’s designed with several convolutional layers and works
in two main phases: the downsampling phase, which compresses the image to understand its features, and the upsampling phase, which expands
the image back to its original size for detailed segmentation.`,X,y,Me=`Computer vision was once dominated by convolutional models, but it has recently shifted towards the vision transformer approach.
An example is <em><a href="https://arxiv.org/abs/2304.02643" rel="nofollow">Segment anything model (SAM)</a></em> that is a popular prompt based model introduced
in April 2023 by <em>Meta AI Research, FAIR</em>. The model is based on the Vision Transformer (ViT) model and focuses on creating a promptable
(i.e. you can provide words to describe what you would like to segment in the image) segmentation model capable of
zero-shot transfer on new images. The strength of the model comes from its training on the largest dataset available, which includes over
1 billion masks on 11 million images. I recommend you play with <a href="https://segment-anything.com/" rel="nofollow">Meta’s demo</a> on a few images and even
better you can play with the <a href="https://huggingface.co/ybelkada/segment-anything" rel="nofollow">model</a> in transformers.`,K,b,xe=`Here is an example of how to use the model in transformers. First, we will initialize the <code>mask-generation</code> pipeline.
Then, we will pass the image in pipeline for inference.`,O,T,ee,M,$e='More details on how to use the model can be found in the <a href="https://huggingface.co/docs/transformers/main/en/model_doc/sam" rel="nofollow">documentation</a>.',te,x,ne,$,ke=`You have now seen how to use a segmentation model, but how can you evaluate it? As demonstrated in the previous section, segmentation is
primarily a supervised learning task. This means that the dataset is composed of images and their corresponding masks, which serve as the
ground truth. A few metrics can be used to evaluate your model. The most common ones are:`,ie,k,He=`<li><strong>The Intersection over Union (IoU) or Jaccard index</strong> metric is the ratio between the intersection and the union of the predicted mask and the ground truth.
IoU is arguably the most common metric used in segmentation tasks. Its advantage lies in being less sensitive to class imbalance, making
it often a good choice when you begin modeling.</li>`,ae,H,Le='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/iou.png" alt="IoU"/>',se,L,_e=`<li><strong>Pixel accuracy</strong>: Pixel accuracy is calculated as the ratio of the number of correctly classified pixels to the total number of pixels.
While being an intuitive metric, it can be misleading due to its sensitivity to class imbalance.</li>`,oe,_,Ce='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/pixel-accuracy.png" alt="Pixel accuracy"/>',le,C,Ue=`<li><strong>Dice coefficient</strong>: It’s the ratio between the double of the intersection and the sum of the predicted mask and the ground truth.
The dice coefficient is simply the percentage of overlap between the prediction and the ground truth. It’s a good metric to use when
you need sensibility to small differences between the overlap.</li>`,re,U,Ie='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/dice-coefficient.png" alt="Dice coefficient"/>',me,I,he,S,Se='<li><a href="https://arxiv.org/abs/2304.02643" rel="nofollow">Segment Anything Paper</a></li> <li><a href="https://huggingface.co/blog/fine-tune-segformer" rel="nofollow">Fine-tuning Segformer blog post</a></li> <li><a href="https://huggingface.co/blog/mask2former" rel="nofollow">Mask2former blog post</a></li> <li><a href="https://huggingface.co/docs/transformers/main/tasks/semantic_segmentation" rel="nofollow">Hugging Face’s documentation on segmentation tasks</a></li> <li>If you want to go deeper into the topic, we recommend you to check out Stanford’s <a href="https://www.youtube.com/watch?v=nDPWywWRIRo" rel="nofollow">lecture on segmentation</a>.</li>',ce,j,pe;return h=new de({props:{title:"Image Segmentation",local:"image-segmentation",headingTag:"h1"}}),v=new de({props:{title:"Modern Approach: Vision Transformer-based Segmentation",local:"modern-approach-vision-transformer-based-segmentation",headingTag:"h3"}}),T=new ze({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMHBpcGVsaW5lJTBBJTBBcGlwZSUyMCUzRCUyMHBpcGVsaW5lKCUyMm1hc2stZ2VuZXJhdGlvbiUyMiUyQyUyMG1vZGVsJTNEJTIyZmFjZWJvb2slMkZzYW0tdml0LWJhc2UlMjIlMkMlMjBkZXZpY2UlM0QwKSUwQSUwQXJhd19pbWFnZSUyMCUzRCUyMEltYWdlLm9wZW4oJTIycGF0aCUyRnRvJTJGaW1hZ2UlMjIpLmNvbnZlcnQoJTIyUkdCJTIyKSUwQSUwQW1hc2tzJTIwJTNEJTIwcGlwZShyYXdfaW1hZ2Up",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline

pipe = pipeline(<span class="hljs-string">&quot;mask-generation&quot;</span>, model=<span class="hljs-string">&quot;facebook/sam-vit-base&quot;</span>, device=<span class="hljs-number">0</span>)

raw_image = Image.<span class="hljs-built_in">open</span>(<span class="hljs-string">&quot;path/to/image&quot;</span>).convert(<span class="hljs-string">&quot;RGB&quot;</span>)

masks = pipe(raw_image)`,wrap:!1}}),x=new de({props:{title:"How to Evaluate a Segmentation Model?",local:"how-to-evaluate-a-segmentation-model",headingTag:"h3"}}),I=new de({props:{title:"Resources and Further Reading",local:"resources-and-further-reading",headingTag:"h2"}}),{c(){m=o("meta"),G=a(),P=o("p"),z=a(),J(h.$$.fragment),F=a(),c=o("p"),c.innerHTML=fe,B=a(),p=o("p"),p.innerHTML=ue,E=a(),d=o("p"),d.textContent=ve,V=a(),g=o("ul"),g.innerHTML=we,Q=a(),f=o("p"),f.innerHTML=ye,Y=a(),u=o("p"),u.innerHTML=be,D=a(),J(v.$$.fragment),N=a(),w=o("p"),w.textContent=Te,X=a(),y=o("p"),y.innerHTML=Me,K=a(),b=o("p"),b.innerHTML=xe,O=a(),J(T.$$.fragment),ee=a(),M=o("p"),M.innerHTML=$e,te=a(),J(x.$$.fragment),ne=a(),$=o("p"),$.textContent=ke,ie=a(),k=o("ul"),k.innerHTML=He,ae=a(),H=o("p"),H.innerHTML=Le,se=a(),L=o("ul"),L.innerHTML=_e,oe=a(),_=o("p"),_.innerHTML=Ce,le=a(),C=o("ul"),C.innerHTML=Ue,re=a(),U=o("p"),U.innerHTML=Ie,me=a(),J(I.$$.fragment),he=a(),S=o("ul"),S.innerHTML=Se,ce=a(),j=o("p"),this.h()},l(e){const t=qe("svelte-u9bgzb",document.head);m=l(t,"META",{name:!0,content:!0}),t.forEach(n),G=s(e),P=l(e,"P",{}),Pe(P).forEach(n),z=s(e),W(h.$$.fragment,e),F=s(e),c=l(e,"P",{"data-svelte-h":!0}),r(c)!=="svelte-exkdtj"&&(c.innerHTML=fe),B=s(e),p=l(e,"P",{"data-svelte-h":!0}),r(p)!=="svelte-1bft94o"&&(p.innerHTML=ue),E=s(e),d=l(e,"P",{"data-svelte-h":!0}),r(d)!=="svelte-1w5fm6j"&&(d.textContent=ve),V=s(e),g=l(e,"UL",{"data-svelte-h":!0}),r(g)!=="svelte-n0x2m3"&&(g.innerHTML=we),Q=s(e),f=l(e,"P",{"data-svelte-h":!0}),r(f)!=="svelte-1beerg7"&&(f.innerHTML=ye),Y=s(e),u=l(e,"P",{"data-svelte-h":!0}),r(u)!=="svelte-17sd42v"&&(u.innerHTML=be),D=s(e),W(v.$$.fragment,e),N=s(e),w=l(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-19zwezj"&&(w.textContent=Te),X=s(e),y=l(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1334w5c"&&(y.innerHTML=Me),K=s(e),b=l(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-1bdv4ej"&&(b.innerHTML=xe),O=s(e),W(T.$$.fragment,e),ee=s(e),M=l(e,"P",{"data-svelte-h":!0}),r(M)!=="svelte-1kwqdq1"&&(M.innerHTML=$e),te=s(e),W(x.$$.fragment,e),ne=s(e),$=l(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-17c2odr"&&($.textContent=ke),ie=s(e),k=l(e,"UL",{"data-svelte-h":!0}),r(k)!=="svelte-1ltwdtc"&&(k.innerHTML=He),ae=s(e),H=l(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-wgpkoc"&&(H.innerHTML=Le),se=s(e),L=l(e,"UL",{"data-svelte-h":!0}),r(L)!=="svelte-li3e7e"&&(L.innerHTML=_e),oe=s(e),_=l(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-rdneb"&&(_.innerHTML=Ce),le=s(e),C=l(e,"UL",{"data-svelte-h":!0}),r(C)!=="svelte-1gypdy2"&&(C.innerHTML=Ue),re=s(e),U=l(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-138bear"&&(U.innerHTML=Ie),me=s(e),W(I.$$.fragment,e),he=s(e),S=l(e,"UL",{"data-svelte-h":!0}),r(S)!=="svelte-1g8ssyy"&&(S.innerHTML=Se),ce=s(e),j=l(e,"P",{}),Pe(j).forEach(n),this.h()},h(){je(m,"name","hf:doc:metadata"),je(m,"content",Be)},m(e,t){Ge(document.head,m),i(e,G,t),i(e,P,t),i(e,z,t),Z(h,e,t),i(e,F,t),i(e,c,t),i(e,B,t),i(e,p,t),i(e,E,t),i(e,d,t),i(e,V,t),i(e,g,t),i(e,Q,t),i(e,f,t),i(e,Y,t),i(e,u,t),i(e,D,t),Z(v,e,t),i(e,N,t),i(e,w,t),i(e,X,t),i(e,y,t),i(e,K,t),i(e,b,t),i(e,O,t),Z(T,e,t),i(e,ee,t),i(e,M,t),i(e,te,t),Z(x,e,t),i(e,ne,t),i(e,$,t),i(e,ie,t),i(e,k,t),i(e,ae,t),i(e,H,t),i(e,se,t),i(e,L,t),i(e,oe,t),i(e,_,t),i(e,le,t),i(e,C,t),i(e,re,t),i(e,U,t),i(e,me,t),Z(I,e,t),i(e,he,t),i(e,S,t),i(e,ce,t),i(e,j,t),pe=!0},p:We,i(e){pe||(R(h.$$.fragment,e),R(v.$$.fragment,e),R(T.$$.fragment,e),R(x.$$.fragment,e),R(I.$$.fragment,e),pe=!0)},o(e){A(h.$$.fragment,e),A(v.$$.fragment,e),A(T.$$.fragment,e),A(x.$$.fragment,e),A(I.$$.fragment,e),pe=!1},d(e){e&&(n(G),n(P),n(z),n(F),n(c),n(B),n(p),n(E),n(d),n(V),n(g),n(Q),n(f),n(Y),n(u),n(D),n(N),n(w),n(X),n(y),n(K),n(b),n(O),n(ee),n(M),n(te),n(ne),n($),n(ie),n(k),n(ae),n(H),n(se),n(L),n(oe),n(_),n(le),n(C),n(re),n(U),n(me),n(he),n(S),n(ce),n(j)),n(m),q(h,e),q(v,e),q(T,e),q(x,e),q(I,e)}}}const Be='{"title":"Image Segmentation","local":"image-segmentation","sections":[{"title":"Modern Approach: Vision Transformer-based Segmentation","local":"modern-approach-vision-transformer-based-segmentation","sections":[],"depth":3},{"title":"How to Evaluate a Segmentation Model?","local":"how-to-evaluate-a-segmentation-model","sections":[],"depth":3},{"title":"Resources and Further Reading","local":"resources-and-further-reading","sections":[],"depth":2}],"depth":1}';function Ee(ge){return Ze(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ne extends Re{constructor(m){super(),Ae(this,m,Ee,Fe,Je,{})}}export{Ne as component};
