Vision tasks and label types: one label per image vs boxes vs masks
How to write down exactly what a vision system should output, pick the label type that output needs, and see what each label looks like as a PyTorch tensor.
A label is the answer a vision model learns to give for an image, written down for each training photo. Labels come in different forms. Some describe the whole photo with one class or a list of tags. Others mark positions with a box around each object, a class for every pixel or a set of named points. Some give a distance for every pixel. The form has to match what the finished system outputs, and it decides how much each image costs to label. With the wrong form, a dataset cannot answer the task's question, however large it gets.
The data decides what a model can learn
A vision model learns only from the photos and labels it is given. For example, say you train a model to spot cracked phone screens. It scores 98 percent on its test photos, then misses the cracks in the factory. Every training photo was taken on the same white table, and the factory photographs phones on a moving belt.

The same thing happens when a case is missing from the training photos altogether. Say you train a crosswalk detector on 40,000 daytime street photos and no night photos. At deployment the model sees a frame at 9 p.m. and misses the crosswalk.
A bigger network or a longer training run does not fix this. A crosswalk under streetlights never appeared in the training photos, so the model had no examples to learn it from. The fix is to add the missing case: night photos with labeled crosswalks. A model can only learn patterns that are present and labeled in its training data. If a class, a viewpoint or a lighting condition never appears there, the model has no examples to fit for it.

The test photos have the same limit. A test score is only as trustworthy as the test images. For example, a model might score 0.96 on test photos that look almost the same as its training photos, and 0.78 on test photos that include the hard cases. The 0.78 says more about how the model will do once deployed.

Reuse a dataset or build your own
The first decision is whether to reuse an existing dataset or collect your own. If a public dataset already fits the task, start from it. Someone has already done the collection and the labeling, which saves weeks.
Build your own when the available datasets do not cover the task. The gap might be your domain, your camera and viewpoint or the label type you need. It might also be the right to use the images, or the rare cases the task depends on. Collecting and labeling images is slow and easy to get wrong, so build only when reuse falls short.

Define the task before collecting data
To know whether a dataset fits, you first need the task written down. A vision task has four parts: what the system outputs, who uses it, what input it gets and what a mistake costs.
Start with the prediction
Before choosing data or a model, write down exactly what the system outputs for one input image. It might be one class, a set of boxes, a mask or a number. That output decides what data and labels you need.

Who uses it and where it runs
Name who uses the prediction and what they do with it. "Flag damaged packages for a person to review" is a different system from "reject damaged packages automatically", even with the same model. In the first, a person catches the model's mistakes. In the second, every mistake goes straight through.

Then say where it runs: a fixed camera, photos users upload, a phone app or a live video stream. Each one sets limits on speed, image quality and how much control you have over the camera.
The input and the cost of a mistake
Fix the kind of input before choosing labels or models. It might be RGB color photos or video, thermal images that record heat, depth maps from a depth sensor or medical scans. The input decides what evidence is available and what sensor the deployed system needs.
Then write down what a wrong prediction costs. A missed defect ships to a customer, and a false alarm stops a production line. The cost decides how carefully the data is collected and how strictly the model is tested.

A worked task statement
All of this goes into one paragraph, the task statement, which any candidate dataset gets checked against. Here is one for a warehouse system:
The system takes one RGB photo from a fixed overhead camera above a packing station and outputs one label per photo:
intactordamaged. A packer sees the label on a screen and pulls flagged boxes for a manual check. It runs on a wall-mounted mini PC, one photo every 2 seconds, under fixed indoor lighting. A misseddamagedbox ships a broken product. A falsedamagedslows the line by about 30 seconds.
A dataset for this task has to match it: overhead RGB photos, indoor light and two classes with one label per photo. It also has to reflect that a miss costs more than a false alarm.

The task sets the label type
The kind of label follows from the task. If the task needs a location, a label for the whole image cannot supply it. If the task needs an answer for every pixel, a box cannot supply it. Three questions map to three label families:
- "What is this?" needs an image-level label.
- "Where is it?" needs a box or a point.
- "Which pixels are it?" needs a mask.

Image-level labels describe the whole photo, such as one class or a set of tags. Spatial labels tie information to positions in the image, such as boxes, points or masks. They cost more to make, and they record where things are as well as what they are.
Spatial labels come in two kinds. Sparse ones mark a few positions, so a box is four numbers. Dense ones give a value for every pixel, such as a mask or a depth map, and they are the most expensive to make.

The label type shapes the model and the score
The label type decides what the model has to output and how its answers are scored. Boxes need a model that outputs boxes, scored by how much each predicted box overlaps the true one. Masks need a model that predicts a class for every pixel, scored pixel by pixel. The label type also sets the loss, the number training tries to make smaller, which measures how far each prediction is from its label.

Changing the label type halfway through a project changes the model and the scoring along with it, and it usually means labeling the images again. Say the task is counting overlapping objects but the labels give one class per image. Then the data cannot answer the question, no matter how much of it you collect. Settle the label type before any labeling starts.

One label per image
An image-level label attaches to the whole photo, such as "cat", "defective" or "night". It says something is true of the image without saying where. That is enough when the image has one clear subject and the question is what it is. Examples are a single product on a conveyor, a cropped face or a scan that is either normal or not.

In a photo with several objects, one label loses information. Take a street photo with two cars, a bus, a cyclist and a pedestrian. A classifier trained on one label per image has to pick one, perhaps "bus" because the bus fills the most pixels. That label does not record the cyclist, the number of cars or where any of them are. If the task is counting cyclists and pedestrians, this label cannot support it, however many such images you collect.

Many labels per image: multi-label tagging
Multi-label tagging gives one image several labels, each one true or false on its own. A street photo can be tagged "outdoor", "rain", "bicycle" and "person" all at once. A product photo can carry its category, its color and "white background".

A single-class model picks exactly one label. A multi-label model decides yes or no for each tag separately. The difference shows up in the scores the model outputs. A single-class model usually ends with softmax, a function that turns a list of raw scores into numbers from 0 to 1 that add up to 1, and you take the largest one. A multi-label model usually ends with sigmoid, which turns each raw score into its own number from 0 to 1, and you keep every tag above a cutoff.
The code below picks raw scores so the outputs match the example in the image below, then applies each rule:
import torch
tags = ["outdoor", "rain", "bicycle", "person", "night"]
# single-class: softmax turns 5 raw scores into 5 numbers that add up to 1
single_logits = torch.log(torch.tensor([0.6, 0.1, 0.1, 0.1, 0.1]))
single = torch.softmax(single_logits, dim=0)
print("single-class:", [round(v, 2) for v in single.tolist()], "sum", round(single.sum().item(), 2))
print("prediction:", tags[single.argmax()])
# multi-label: sigmoid turns each raw score into its own number from 0 to 1
p = torch.tensor([0.95, 0.88, 0.20, 0.91, 0.04])
multi_logits = torch.log(p / (1 - p))
multi = torch.sigmoid(multi_logits)
print("multi-label: ", [round(v, 2) for v in multi.tolist()], "sum", round(multi.sum().item(), 2))
print("predictions:", [t for t, s in zip(tags, multi) if s > 0.5])
# single-class: [0.6, 0.1, 0.1, 0.1, 0.1] sum 1.0
# prediction: outdoor
# multi-label: [0.95, 0.88, 0.2, 0.91, 0.04] sum 2.98
# predictions: ['outdoor', 'rain', 'person']The single-class scores add up to 1, so raising one score lowers the others, and the model can only answer "outdoor". The multi-label scores add up to 2.98 because each one is its own yes or no. With a cutoff of 0.5 for each tag, this image comes out as outdoor, rain and person.

A missing tag is ambiguous
Say "helmet" is not tagged on an image. That could mean someone checked and no helmet is visible, or that nobody checked for helmets. A model trained on that image treats both cases as "no helmet". A multi-label dataset needs to state whether every tag was checked on every image, or only some of them.

Labels with position
Bounding boxes
A bounding box is a rectangle drawn around an object, with its sides parallel to the image edges. It is usually stored as four numbers, such as the top-left corner (x1, y1) and the bottom-right corner (x2, y2). Those numbers give the object's rough position and size, which is why boxes are a widely used position label.

A rectangle says little about shape. A box around a bicycle is mostly empty space between the frame and the wheels. A box around a cat says the cat is somewhere inside it, without saying which pixels are cat. A thin pole leaning diagonally is the extreme case:
import torch
# a thin pole leaning diagonally across a 10 by 10 crop
pole = torch.zeros(10, 10, dtype=torch.bool)
for i in range(10):
pole[i, i] = True
rows, cols = torch.nonzero(pole, as_tuple=True)
box = (cols.min().item(), rows.min().item(), cols.max().item() + 1, rows.max().item() + 1)
box_area = (box[2] - box[0]) * (box[3] - box[1])
print("box (x1, y1, x2, y2):", box)
print("pixels in the box:", box_area, " pole pixels:", pole.sum().item())
print(f"share of the box that is pole: {pole.sum().item() / box_area:.0%}")
# box (x1, y1, x2, y2): (0, 0, 10, 10)
# pixels in the box: 100 pole pixels: 10
# share of the box that is pole: 10%The tightest box around the pole covers the whole 10 by 10 crop, and only 10 of its 100 pixels are pole. A model trained on that box learns that the pole is somewhere in the square, which is fine for "is there a pole here" and poor for "which pixels does the pole cover".
Segmentation masks
A segmentation mask labels every pixel in the image. It is a grid the same size as the image, holding a class number or an object number at each pixel. Masks come in two kinds:
- A semantic mask labels each pixel with a class such as road, sidewalk or sky, so two cars share the same "car" label.
- An instance mask gives each object its own mask, so two cars become two separate masks. That tells you how many objects there are and which pixels belong to each.

Keypoints
A keypoint label is a set of named points that mean the same thing in every image. "Left shoulder" is the left shoulder in every example, and the label stores each point's pixel position.
Each keypoint also carries a flag: visible, hidden but guessable, or impossible to place. A knee hidden behind a desk stays in the label, marked as hidden, so the model still learns where it is likely to be.

A keypoint is a single point, so a few pixels of labeling error is a large error. For example, a joint at pixel (212, 125) that gets labeled at about (215, 128) is about 4 pixels off. The coordinate convention also has to be fixed and kept the same across the dataset. That means where the origin is, which axis comes first and whether a coordinate marks a pixel's center or its corner.

Depth and motion
A depth map label stores, at each pixel, how far that point is from the camera. Metric depth is in real units, such as 3.2 meters. Relative depth only gives an order, such as "this is farther than that".
Depth labels and motion labels, which give each pixel's movement to the next video frame, have a value at every pixel. The motion label is often called optical flow. Nobody can mark by hand how far away every pixel is, so these labels come from a depth sensor, a pair of cameras or a 3D renderer that draws synthetic scenes. They usually come with a second mask marking the pixels the sensor measured.

What each label looks like as a tensor
The code below writes every label type for one tiny 4 by 6 street photo with two cars, so the shapes can be compared side by side:
import torch
H, W = 4, 6 # one tiny 4 by 6 street photo with two cars
labels = {
# image-level
"one class": torch.tensor(1), # index into ["bus", "car", "bike"]
"multi-label": torch.tensor([1, 0, 0, 1, 0]), # outdoor, rain, bicycle, person, night
# spatial, sparse
"boxes": torch.tensor([[0, 1, 2, 3], [3, 1, 6, 3]]), # (x1, y1, x2, y2) per car
"keypoints": torch.tensor([[0.5, 2.5, 2], [1.5, 2.5, 1]]), # (x, y, flag): front wheel, back wheel
# spatial, dense
"semantic mask": torch.zeros(H, W, dtype=torch.long), # a class number per pixel
"instance masks": torch.zeros(2, H, W, dtype=torch.bool), # one yes/no grid per car
"depth map": torch.full((H, W), 3.2), # meters from the camera per pixel
"depth valid": torch.ones(H, W, dtype=torch.bool), # which pixels the sensor measured
}
labels["semantic mask"][1:3, 0:2] = 1 # both cars get class 1, "car"
labels["semantic mask"][1:3, 3:6] = 1
labels["instance masks"][0, 1:3, 0:2] = True
labels["instance masks"][1, 1:3, 3:6] = True
labels["depth valid"][0, :] = False # the sky row: the sensor got no reading
for name, t in labels.items():
print(f"{name:15} shape {tuple(t.shape)}")
print("classes in the semantic mask:", labels["semantic mask"].unique().tolist())
print("cars in the instance masks: ", labels["instance masks"].shape[0])
# one class shape ()
# multi-label shape (5,)
# boxes shape (2, 4)
# keypoints shape (2, 3)
# semantic mask shape (4, 6)
# instance masks shape (2, 4, 6)
# depth map shape (4, 6)
# depth valid shape (4, 6)
# classes in the semantic mask: [0, 1]
# cars in the instance masks: 2The keypoint flag here uses 2 for visible, 1 for hidden but guessable and 0 for impossible to place, so the back wheel is hidden. The image-level labels have no height or width, the sparse labels have one row per object or point, and the dense labels are the size of the image. The last two lines show the semantic vs instance difference in numbers: the semantic mask only knows that class 1 is present, while the instance masks count two cars.
| Label type | Answers | Size per image | Cost to make |
|---|---|---|---|
| one class | what is it | 1 number | lowest |
| multi-label tags | which things are present | 1 number per tag | low |
| boxes | where, roughly | 4 numbers per object | medium |
| keypoints | where each named part is | 3 numbers per point | medium |
| masks | which pixels | 1 number per pixel | high |
| depth or motion | how far or how it moves | 1 or 2 numbers per pixel | needs a sensor or renderer |
Common mistakes
- Labeling before writing the task statement. Write down the output, the user, the input and the cost of a mistake. Then pick the label type.
- One label per image for a counting or location task. The labels cannot answer "how many" or "where", however many images there are.
- Treating a missing tag as "no". Record whether every tag was checked on every image.
- A semantic mask when the task needs to tell objects apart. Two touching cars merge into one region. Use instance masks.
- Mixing coordinate conventions. Fix where the origin is, which axis comes first and whether a coordinate marks a pixel's center or corner, then keep it for the whole dataset.
- Training photos that leave out the deployment conditions. If the deployed camera works at night, the training and test photos need night photos.
Related concepts
What is computer vision? covers the main vision tasks by the shape of their answer, from a single class to a mask. How images are stored as tensors covers (x, y) vs [row, column] and how a box moves after a crop. Common image data bugs in PyTorch covers labels that do not move with the image after a flip or crop.
QuiddityML teaches task definitions and label types at the start of the vision data unit in its Computer Vision track, where the exercises include picking the label type a task needs and matching the per-tag threshold rule for multi-label tags to its line of code (QuiddityML).