Semantic and instance segmentation
- Lesson 17
- 30 min
- Module 5: Segment
Where we are
Boxes told us where things are and how many there are. A box does not say which pixels belong to an object. In this lesson, we meet semantic and instance segmentation, and we see when a mask is worth its extra cost.
Lesson 17 of 30 · Vision AI: See, Count and InspectSemantic and instance segmentation
1. Seeing is a business problem
2. Baselines and pretrained classifiers
3. Embeddings, CLIP and few labels
4. Detect and count
6. Inspect for defects
7. Match, search and describe
8. Run vision in production
What you will be able to do
See the difference between a box, a semantic mask and instance masks. Learn which question each one answers, and why a semantic mask cannot count objects that touch. Then decide when Larkfield should pay for masks instead of boxes.
NotebookDo this lesson in a notebook
The notebook turns the outlines of the pallet boxes into masks. It builds a semantic mask and instance masks for one pallet, and counts the separate regions of the semantic mask. Its setup cells download the pallet photos (about 3 MB, no model and no key), so it needs no local setup. About 30 minutes; the cells run in a few seconds.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Pixels and channels, from Images are numbers.
- Boxes and IoU, from Boxes and IoU.
What you need on your computer
- For the notebook: nothing. It runs in Colab or Kaggle.
- This lesson adds nothing to the
larkvisionproject. The project gets a mask command at the end of this module.
Tested with Python 3.12 on macOS.
Two ways to colour a floor plan
Section titled “Two ways to colour a floor plan”Hamid plans a new layout for the returns warehouse. He prints the floor plan and takes two highlighter pens.
First, he colours every shelf in yellow. Now he can see how much of the floor the shelves cover. But where one shelf touches the next, the yellow runs together. He cannot count the shelves from the yellow.
Then he takes a fresh copy and gives each shelf its own colour: shelf 1 red, shelf 2 blue, shelf 3 green. Now he can count them, and he can measure each one.
Computers colour photos in the same two ways. This lesson gives each way its name.
The problem: a box is a rough shape
Section titled “The problem: a box is a rough shape”In Module 4, every object got a box: a rectangle around it. A box is cheap and good for counting. But a box is not the object. It also holds the pixels around the object.
For a cardboard box on a pallet, this does not matter much: the object is almost a rectangle. For a damage on a candle, it matters a lot. Look at one damaged tea light from the returns:

Text description of the diagram
Two views of the same corner of a tea light, zoomed so that each pixel is a small square. The damage is a thin curved strip along the bottom edge of the candle. Left: a blue rectangle drawn around the strip. It also covers wax above the strip and dark background below it: 1,012 pixels. Right: only the strip is coloured red: 408 pixels.
The damage is a thin curved strip. Its box is 1,012 pixels, but only 408 of them are damage. Suppose Hamid’s rule is “more than 500 damaged pixels: send the candle to the outlet”. The box sends this candle away. The true outline does not.
A box answers “where, roughly?”. Some questions need “which pixels, exactly?”.
The idea: a mask
Section titled “The idea: a mask”A mask is a grid of yes-or-no values, one for each pixel of the photo. “Yes” means the pixel belongs to the object. “No” means it does not. You can draw a mask as a black-and-white image of the same size as the photo.
Giving masks to the objects in a photo is called segmentation: we cut the photo into segments. There are two main kinds.
- Semantic segmentation says, for every pixel, which class it is: “box”, “candle”, “floor”. It does not separate two objects of the same class. This is Hamid’s yellow pen.
- Instance segmentation gives each object its own mask: box 1, box 2, box 3. Two touching boxes stay apart. This is the pen with a new colour for each shelf.
A third name you may meet is panoptic segmentation: both at once. Every pixel gets a class, and every countable object also gets its own number.
A tiny example
Section titled “A tiny example”A constructed photo of 6 × 10 = 60 pixels holds two boxes that touch. Box A has 16 pixels; box B has 12 pixels.
Text description of the diagram
Three grids of 6 rows and 10 columns. Grid 1, the photo: box A covers rows 2 to 5 and columns 2 to 5 (16 pixels). Box B covers rows 3 to 5 and columns 6 to 9 (12 pixels). B touches A on its right side. Grid 2, the semantic mask: the 28 box pixels hold 1 and the other 32 pixels hold 0; the ones form one region. Grid 3, the instance masks: box A’s pixels hold 1, box B’s pixels hold 2, the rest 0.
| Question | Semantic grid | Instance grid |
|---|---|---|
| How many pixels are “box”? | 28 | 16 + 12 = 28 |
| How many boxes? | One region: it looks like 1 | 2 |
| How big is box B? | It cannot say | 12 pixels |
Both grids agree on the area of “box”. Only the instance grid can count, and only the instance grid can measure one object.
Three answers on one pallet
Section titled “Three answers on one pallet”Our code draws Larkfield’s pallet photos, so we know the exact outline of every box. The outline is a polygon: the list of corners of the faces you can see. A polygon becomes a mask when we fill it in.
Here is the same medium pallet, S-062, with the three kinds of answer:

Text description of the diagram
Three copies of a synthetic pallet photo with 24 cardboard boxes in 6 columns and 4 layers. Panel 1: a thin blue rectangle around each box. Panel 2: every pixel of every box has the same blue, on a darker background. In a column, you cannot see where one box ends and the next begins. Panel 3: each box has its own colour, so each of the 24 boxes stands apart from its neighbours.
On this pallet, 52% of the pixels are “box”.
Why a semantic mask cannot count
Section titled “Why a semantic mask cannot count”The boxes in each column of S-062 stand on each other. There is no gap between them. Can we still count the boxes in the semantic mask? A simple method finds the connected regions: groups of “yes” pixels that touch each other.

Text description of the diagram
The synthetic pallet S-062 with its semantic mask. The mask falls into six coloured regions, numbered 1 to 6 from left to right. Each region is one column of four boxes stacked on each other. The gaps between columns separate the regions; the boxes within a column touch, so they join.
The semantic mask has only 6 regions for 24 boxes. The 8-pixel gaps between the columns keep the columns apart. The boxes in a column touch, so they join. On a tight pallet, where every box touches its neighbours, the whole pallet can become one region.
Common mistake
Counting regions in a semantic mask
Symptom: a team counts boxes by counting the connected regions of a semantic mask. On loose pallets the count is right. On S-062 it says 6 for 24 boxes.
Cause: a semantic mask has no idea of “one object”. Two objects of the same class that touch become one region.
Fix: count from instance masks, or from boxes, as in Module 4. Use a semantic mask for areas, not for counts.
Consequence in AI work: a mask can be right in every pixel and still give the wrong answer to your question. Choose the kind of answer from the question.
When is a mask worth its cost?
Section titled “When is a mask worth its cost?”Masks cost more than boxes, in three ways:
- Labels: a person draws a box with two clicks, and an outline with many. Assume 5 seconds for a box, as in Module 4, and 30 seconds for an outline. Both are assumptions: time your own tool. The 817 boxes of the 40 training pallets then take about 68 minutes as boxes, and about 6.8 hours as outlines. At 30 dollars an hour, that is about 34 dollars against about 204 dollars.
- Models: a model that draws masks is larger and slower than a detector of the same family.
- Checks: a person checks a box at a glance. Checking an outline takes longer.
So pay for a mask only when the box gives a different answer. How different is a box from the true outline? Treat the box as a mask, and compute its mask IoU with the true mask: the pixels in both, divided by the pixels in either.
Text description of the chart
A bar chart with four bars, from 0 to 1. Pallet boxes: loose 0.97, medium 0.96, tight 0.95. Damage on the candles: 0.56, the median of 18 photos.
For pallet boxes, the box and the outline share 95% to 97% of their pixels. A mask adds almost nothing. For damage on a candle, the median IoU is 0.56: the box holds about twice as many pixels as the damage. Damage is thin, curved or in several small pieces.
When to use masks, and when not
Section titled “When to use masks, and when not”Use boxes when the question is “how many?” or “where, roughly?”, and the objects are about rectangular. Counting boxes on a pallet is the example.
Use instance masks when the question is about the exact pixels of each object:
- How big is it? The area of a damage on a candle, so that Hamid can set a rule by size. This is the last lesson of this module.
- Cut it out. The catalogue team wants each product on a plain background. This is the next lesson.
- What touches what? For example, whether a stain crosses the edge of a label.
Use a semantic mask when you need the area of a class, and not the objects. Examples: how much of a shelf is empty, or how much of a cushion cover has stains. A semantic mask cannot count objects that touch.
Larkfield: where masks pay
Section titled “Larkfield: where masks pay”Priya looks at the three teams and their questions:
| Team | Question | Answer kind |
|---|---|---|
| Warehouse (pallets) | How many boxes? | Boxes are enough: box and outline agree at 0.95 or more |
| Returns (Hamid) | How much of the candle is damaged? | An instance mask for each damage |
| Catalogue (Omar) | The product without its background | An instance mask for each product |
The returns photos already have damage masks: people drew them for the VisA image set. The catalogue photos have only boxes. Drawing thousands of product outlines by hand would cost many hours. In the next lesson, a model draws them from a single box or click.
Guided practice: masks from outlines
Section titled “Guided practice: masks from outlines”- Open the notebook from the box Do this lesson in a notebook at the top of this page.
- Run the setup cells. They download the pallet photos and their outlines.
- In section 1, run the cell. It fills each polygon into a mask. S-062 has 24 masks of 512 × 384 pixels.
- In section 2, run the cell. It draws the photo, the semantic mask and the instance masks side by side.
- Predict: how many separate regions does the semantic mask have? Then run the next cell. You see
boxes: 24 | separate regions in the semantic mask: 6. - Go to section 3 for your task.
Your turn: how much of a tight pallet is boxes?
Section titled “Your turn: how much of a tight pallet is boxes?”Section 3 of the notebook has one task. For each of the 10 tight test pallets, compute the share of pixels inside a box. This share is the mean of the photo’s semantic mask. Put the mean of the 10 shares in tight_share. The check cell prints Check passed when your answer is right.
Before you run it, predict: is the share higher or lower than on S-062 (52%)?
Show the answer
shares = []for im in pallets["images"]: if im["crowding"] == "tight": ms = [mask(a["segmentation"][0], im["width"], im["height"]) for a in pallets["annotations"] if a["image_id"] == im["id"]] shares.append(np.any(ms, axis=0).mean())tight_share = float(np.mean(shares))The answer is 0.377: on average, 38% of a tight pallet photo is box. That is lower than on S-062, although the boxes touch. Tight is about the gaps between boxes, not about how much of the photo the pallet fills. This is a semantic question: you need no instance masks for it.
Knowledge check
Section titled “Knowledge check”Check your understanding
A photo shows three mugs. Two of them touch. A semantic mask marks every mug pixel. How many connected regions do you expect, and why can the mask still be useful?
Show the answer
Two regions: the two touching mugs join into one, and the third mug is its own region. So you cannot count the mugs from it. It is still useful for questions about the class as a whole. For example: how many pixels are “mug”?
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
On the pallets, a box and the true outline share 95% to 97% of their pixels. On candle damage, the median is 0.56. What does this tell you about where Larkfield should pay for masks?
Show the answer
On the pallets, a box is almost the outline, so masks add little to counting and are not worth their labelling cost. On the candles, the box holds about twice the damage’s pixels, so an area from a box would be about twice too large. Larkfield should pay for masks where the size or shape of the object drives the decision, such as damage area, and keep boxes for counting.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Omar wants each catalogue product on a white background. Hamid wants the share of each returned candle's top that is damaged. Which kind of segmentation does each need: semantic or instance? Give a reason for each.
Show the answer
Omar needs an instance mask for each product, because he must cut out one object at a time. A class mask would be enough only when each photo has one product, and it would fail as soon as two products touch. Hamid needs an instance mask for each candle, to know which candle the damage is on and the size of that candle’s top. With a damage mask for that candle, he divides the damage pixels by the candle’s pixels.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- A mask says yes or no for every pixel. Semantic segmentation gives each pixel a class; instance segmentation gives each object its own mask; panoptic does both.
- A semantic mask cannot count objects that touch: the medium pallet S-062 has 24 boxes and only 6 regions.
- Masks cost more than boxes: in labels (an assumed 6.8 hours against 68 minutes for 817 boxes), in models and in checks.
- Pay for masks when the exact pixels drive the decision. For a pallet box, the box is almost the outline (IoU 0.95 to 0.97). For damage on a candle, the box is about twice too big (median 0.56).
You can now say which kind of mask a question needs. But masks are expensive to draw by hand. Next, a model draws a mask from one click or one box, with no training, and we check how often it is right.