import{s as Ze,n as Re,o as Ae}from"../chunks/scheduler.7bc62968.js";import{S as Ge,i as qe,g as o,s as a,r as j,A as ze,h as l,f as n,c as s,j as Je,u as J,x as r,k as We,y as Ee,a as i,v as W,d as Z,t as R,w as A}from"../chunks/index.2f8492b0.js";import{C as Fe}from"../chunks/CodeBlock.4e96d6f7.js";import{H as fe,E as Be}from"../chunks/EditOnGithub.2a9ce03a.js";function Ve(ue){let m,z,G,E,h,F,c,ve=`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,we='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/segmentation-example.png" alt="Image segmentation"/>',V,d,ye=`Different types of segmentations can be applied depending on the context and the intended goal.
The most commonly defined segmentations are the following.`,Q,g,be=`<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>`,Y,f,Te='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/segmentation-types.png" alt="Comparison of segmentation types"/>',D,u,Me=`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.`,N,v,X,w,xe=`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.`,K,y,$e=`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.`,O,b,ke=`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.`,ee,T,te,M,He='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>.',ne,x,ie,$,Le=`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:`,ae,k,_e=`<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>`,se,H,Ce='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/iou.png" alt="IoU"/>',oe,L,Ue=`<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>`,le,_,Ie='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/pixel-accuracy.png" alt="Pixel accuracy"/>',re,C,Se=`<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>`,me,U,Pe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/dice-coefficient.png" alt="Dice coefficient"/>',he,I,ce,S,je='<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>',pe,P,de,q,ge;return h=new fe({props:{title:"Image Segmentation",local:"image-segmentation",headingTag:"h1"}}),v=new fe({props:{title:"Modern Approach: Vision Transformer-based Segmentation",local:"modern-approach-vision-transformer-based-segmentation",headingTag:"h3"}}),T=new Fe({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 fe({props:{title:"How to Evaluate a Segmentation Model?",local:"how-to-evaluate-a-segmentation-model",headingTag:"h3"}}),I=new fe({props:{title:"Resources and Further Reading",local:"resources-and-further-reading",headingTag:"h2"}}),P=new Be({props:{source:"https://github.com/johko/computer-vision-course/blob/main/chapters/en/unit6/basic-cv-tasks/segmentation.mdx"}}),{c(){m=o("meta"),z=a(),G=o("p"),E=a(),j(h.$$.fragment),F=a(),c=o("p"),c.innerHTML=ve,B=a(),p=o("p"),p.innerHTML=we,V=a(),d=o("p"),d.textContent=ye,Q=a(),g=o("ul"),g.innerHTML=be,Y=a(),f=o("p"),f.innerHTML=Te,D=a(),u=o("p"),u.innerHTML=Me,N=a(),j(v.$$.fragment),X=a(),w=o("p"),w.textContent=xe,K=a(),y=o("p"),y.innerHTML=$e,O=a(),b=o("p"),b.innerHTML=ke,ee=a(),j(T.$$.fragment),te=a(),M=o("p"),M.innerHTML=He,ne=a(),j(x.$$.fragment),ie=a(),$=o("p"),$.textContent=Le,ae=a(),k=o("ul"),k.innerHTML=_e,se=a(),H=o("p"),H.innerHTML=Ce,oe=a(),L=o("ul"),L.innerHTML=Ue,le=a(),_=o("p"),_.innerHTML=Ie,re=a(),C=o("ul"),C.innerHTML=Se,me=a(),U=o("p"),U.innerHTML=Pe,he=a(),j(I.$$.fragment),ce=a(),S=o("ul"),S.innerHTML=je,pe=a(),j(P.$$.fragment),de=a(),q=o("p"),this.h()},l(e){const t=ze("svelte-u9bgzb",document.head);m=l(t,"META",{name:!0,content:!0}),t.forEach(n),z=s(e),G=l(e,"P",{}),Je(G).forEach(n),E=s(e),J(h.$$.fragment,e),F=s(e),c=l(e,"P",{"data-svelte-h":!0}),r(c)!=="svelte-exkdtj"&&(c.innerHTML=ve),B=s(e),p=l(e,"P",{"data-svelte-h":!0}),r(p)!=="svelte-1bft94o"&&(p.innerHTML=we),V=s(e),d=l(e,"P",{"data-svelte-h":!0}),r(d)!=="svelte-1w5fm6j"&&(d.textContent=ye),Q=s(e),g=l(e,"UL",{"data-svelte-h":!0}),r(g)!=="svelte-n0x2m3"&&(g.innerHTML=be),Y=s(e),f=l(e,"P",{"data-svelte-h":!0}),r(f)!=="svelte-1beerg7"&&(f.innerHTML=Te),D=s(e),u=l(e,"P",{"data-svelte-h":!0}),r(u)!=="svelte-17sd42v"&&(u.innerHTML=Me),N=s(e),J(v.$$.fragment,e),X=s(e),w=l(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-19zwezj"&&(w.textContent=xe),K=s(e),y=l(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1334w5c"&&(y.innerHTML=$e),O=s(e),b=l(e,"P",{"data-svelte-h":!0}),r(b)!=="svelte-1bdv4ej"&&(b.innerHTML=ke),ee=s(e),J(T.$$.fragment,e),te=s(e),M=l(e,"P",{"data-svelte-h":!0}),r(M)!=="svelte-1kwqdq1"&&(M.innerHTML=He),ne=s(e),J(x.$$.fragment,e),ie=s(e),$=l(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-17c2odr"&&($.textContent=Le),ae=s(e),k=l(e,"UL",{"data-svelte-h":!0}),r(k)!=="svelte-1ltwdtc"&&(k.innerHTML=_e),se=s(e),H=l(e,"P",{"data-svelte-h":!0}),r(H)!=="svelte-wgpkoc"&&(H.innerHTML=Ce),oe=s(e),L=l(e,"UL",{"data-svelte-h":!0}),r(L)!=="svelte-li3e7e"&&(L.innerHTML=Ue),le=s(e),_=l(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-rdneb"&&(_.innerHTML=Ie),re=s(e),C=l(e,"UL",{"data-svelte-h":!0}),r(C)!=="svelte-1gypdy2"&&(C.innerHTML=Se),me=s(e),U=l(e,"P",{"data-svelte-h":!0}),r(U)!=="svelte-138bear"&&(U.innerHTML=Pe),he=s(e),J(I.$$.fragment,e),ce=s(e),S=l(e,"UL",{"data-svelte-h":!0}),r(S)!=="svelte-1g8ssyy"&&(S.innerHTML=je),pe=s(e),J(P.$$.fragment,e),de=s(e),q=l(e,"P",{}),Je(q).forEach(n),this.h()},h(){We(m,"name","hf:doc:metadata"),We(m,"content",Qe)},m(e,t){Ee(document.head,m),i(e,z,t),i(e,G,t),i(e,E,t),W(h,e,t),i(e,F,t),i(e,c,t),i(e,B,t),i(e,p,t),i(e,V,t),i(e,d,t),i(e,Q,t),i(e,g,t),i(e,Y,t),i(e,f,t),i(e,D,t),i(e,u,t),i(e,N,t),W(v,e,t),i(e,X,t),i(e,w,t),i(e,K,t),i(e,y,t),i(e,O,t),i(e,b,t),i(e,ee,t),W(T,e,t),i(e,te,t),i(e,M,t),i(e,ne,t),W(x,e,t),i(e,ie,t),i(e,$,t),i(e,ae,t),i(e,k,t),i(e,se,t),i(e,H,t),i(e,oe,t),i(e,L,t),i(e,le,t),i(e,_,t),i(e,re,t),i(e,C,t),i(e,me,t),i(e,U,t),i(e,he,t),W(I,e,t),i(e,ce,t),i(e,S,t),i(e,pe,t),W(P,e,t),i(e,de,t),i(e,q,t),ge=!0},p:Re,i(e){ge||(Z(h.$$.fragment,e),Z(v.$$.fragment,e),Z(T.$$.fragment,e),Z(x.$$.fragment,e),Z(I.$$.fragment,e),Z(P.$$.fragment,e),ge=!0)},o(e){R(h.$$.fragment,e),R(v.$$.fragment,e),R(T.$$.fragment,e),R(x.$$.fragment,e),R(I.$$.fragment,e),R(P.$$.fragment,e),ge=!1},d(e){e&&(n(z),n(G),n(E),n(F),n(c),n(B),n(p),n(V),n(d),n(Q),n(g),n(Y),n(f),n(D),n(u),n(N),n(X),n(w),n(K),n(y),n(O),n(b),n(ee),n(te),n(M),n(ne),n(ie),n($),n(ae),n(k),n(se),n(H),n(oe),n(L),n(le),n(_),n(re),n(C),n(me),n(U),n(he),n(ce),n(S),n(pe),n(de),n(q)),n(m),A(h,e),A(v,e),A(T,e),A(x,e),A(I,e),A(P,e)}}}const Qe='{"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 Ye(ue){return Ae(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Oe extends Ge{constructor(m){super(),qe(this,m,Ye,Ve,Ze,{})}}export{Oe as component};
