TExcelTAD
Home › Data for ML: Types, Formats, Sources & Quality › Ground Truth, Annotations, Classes and Metadata

Ground Truth, Annotations, Classes and Metadata

The previous lesson split a dataset into features and labels. This one looks at where labels come from and what surrounds them: ground truth, the annotations people create, the classes a label can take, and the metadata that records how every sample was made. These words decide how far you can trust a dataset, so they deserve a lesson of their own.

Ground truth

Ground truth is the correct answer for a sample, as well as anyone can establish it. The phrase comes from remote sensing: a satellite image is checked against what is actually on the ground. In ML, ground truth is what the labels are supposed to be, and the labels you have are an attempt to record it.

How good that attempt is depends on how the ground truth was obtained:

Source of ground truth Example Typical quality
a later, definitive event did the customer cancel within 30 days? did the loan default? very good, but only known after waiting
a precise measurement a lab test, a weighing scale good, within the instrument's error
expert judgement a radiologist reads an X-ray good but not perfect; experts disagree
crowd annotation many people tag photos for a few cents each variable; needs checks and agreement
automatic rules or weak labels "a review with 1 star is negative" cheap, noisy

Ground truth is not always objective. "Is this comment toxic?" has no single correct answer, and two careful annotators may disagree. Label Quality measures that disagreement.

Annotations

An annotation is a label added to raw data by a person or a tool. For tables it is often one value per row, but for other media it has structure:

Task Annotation Stored as
image classification one class per image a CSV file,label or one folder per class
object detection a box and a class per object [x, y, width, height] + class, in COCO JSON or YOLO text files
segmentation a class per pixel a mask image of the same size, or polygons
named-entity recognition labelled spans of text start and end character offsets + type
speech recognition a transcript, often with timestamps text per audio file or segment

Here is a small object-detection annotation in the style of the COCO format, with two images and three boxes, and the checks you would run on it:

python · try it yourself
import json
import pandas as pd

coco = json.loads("""
{
  "images": [
    {"id": 1, "file_name": "street_001.jpg", "width": 640, "height": 480},
    {"id": 2, "file_name": "street_002.jpg", "width": 640, "height": 480}
  ],
  "categories": [{"id": 1, "name": "person"}, {"id": 2, "name": "bicycle"}],
  "annotations": [
    {"id": 10, "image_id": 1, "category_id": 1, "bbox": [100, 120, 60, 180]},
    {"id": 11, "image_id": 1, "category_id": 2, "bbox": [90, 220, 140, 90]},
    {"id": 12, "image_id": 2, "category_id": 1, "bbox": [600, 200, 80, 150]}
  ]
}
""")
names = {c["id"]: c["name"] for c in coco["categories"]}
size = {im["id"]: (im["width"], im["height"]) for im in coco["images"]}
rows = []
for a in coco["annotations"]:
    x, y, w, h = a["bbox"]
    W, H = size[a["image_id"]]
    rows.append({"ann": a["id"], "image": a["image_id"], "class": names[a["category_id"]],
                 "area": w * h, "inside_image": x >= 0 and y >= 0 and x + w <= W and y + h <= H})
boxes = pd.DataFrame(rows)
print(boxes)
print()
print("boxes per class:", boxes["class"].value_counts().to_dict())
   ann  image    class   area  inside_image
0   10      1   person  10800          True
1   11      1  bicycle  12600          True
2   12      2   person  12000         False

boxes per class: {'person': 2, 'bicycle': 1}

Annotation 12 sticks out of its image: its box starts at x = 600 and is 80 pixels wide, but the image is only 640 wide. Such checks are cheap and catch real annotation tool bugs.

Classes: multi-class and multi-label

A class is one possible value of a categorical label. Two set-ups are easy to confuse:

  • Binary: two classes (spam / not spam).
  • Multi-class: each sample has exactly one of K classes (a digit is 0–9).
  • Multi-label: each sample can have any number of labels at once (a photo tagged "beach", "dog" and "sunset").

They are stored differently, and a model must be built for the right one:

python · try it yourself
import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer

multiclass = pd.Series(["cat", "dog", "cat", "bird"], name="species")
print("multi-class, one label per sample:")
print(pd.get_dummies(multiclass, dtype=int))

tags = [["beach", "dog"], ["city"], ["beach", "sunset", "dog"], []]
mlb = MultiLabelBinarizer()
Y = mlb.fit_transform(tags)
print()
print("multi-label, any number of labels per sample:")
print(pd.DataFrame(Y, columns=mlb.classes_))
print("labels per sample:", Y.sum(axis=1).tolist())
multi-class, one label per sample:
   bird  cat  dog
0     0    1    0
1     0    0    1
2     0    1    0
3     1    0    0

multi-label, any number of labels per sample:
   beach  city  dog  sunset
0      1     0    1       0
1      0     1    0       0
2      1     0    1       1
3      0     0    0       0
labels per sample: [2, 1, 3, 0]

In the multi-class table every row has exactly one 1. In the multi-label table, rows can have none, one or several. The last photo has no tags at all, which may mean "nothing of interest" or "not annotated yet": a question to ask the people who made the data.

Metadata

Metadata is data about the data: not a feature or a label, but the record of where each sample came from. Typical fields:

Field Why it matters
source, device, site models often learn the device instead of the task (a scanner model from one hospital)
collection time needed for time-based splits and for spotting drift
annotator id, annotation time lets you measure annotator quality and agreement
licence, consent status decides whether you may use the sample at all
version, checksum makes the dataset reproducible (see Dataset Versioning)

Metadata often reveals problems that the features hide. In this invented skin-lesion dataset, the label is almost perfectly predicted by which clinic took the photo:

python · try it yourself
import pandas as pd

meta = pd.DataFrame({
    "image": [f"img_{i:03}.png" for i in range(1, 11)],
    "clinic": ["A", "A", "A", "A", "A", "B", "B", "B", "B", "B"],
    "camera": ["X1"] * 5 + ["Z9"] * 5,
    "label": ["benign", "benign", "benign", "benign", "malignant",
              "malignant", "malignant", "malignant", "malignant", "benign"],
})
print(pd.crosstab(meta["clinic"], meta["label"]))
label   benign  malignant
clinic
A            4          1
B            1          4

Clinic B is a specialist centre that receives the suspicious cases, so most of its images are malignant, and it uses a different camera. A model can reach high accuracy by recognising the camera's colour profile, without learning anything about skin. Only the metadata lets you see this, and it is why splits are often made by source (all of clinic B in the test set) to check that a model generalises.

Pitfalls

  • Labels are not ground truth. Treat them as measurements with an error rate, and estimate that rate.
  • Multi-label stored as multi-class. Keeping only the first tag of each photo throws information away and confuses the model.
  • Missing annotation vs negative. An empty label list may mean "checked, nothing there" or "never checked". Record which.
  • Dropping metadata. Keep source, time, annotator and licence with every sample, even when they are not features.

Practice

  1. A news article can be about "politics", "economy" and "sport" at the same time. Multi-class or multi-label?
Answer

Multi-label: one article can carry several topics, so each topic gets its own 0/1 column.

  1. In the COCO example, write the condition that would also reject boxes with zero or negative width or height.
Answer

w > 0 and h > 0 and x >= 0 and y >= 0 and x + w <= W and y + h <= H

Test yourself

What is ground truth?

  1. Whatever the labels in the file say
  2. The correct answer for a sample, as well as it can be established
  3. The training data after cleaning
  4. The model's prediction

A photo can be tagged with any of 50 tags at once. The task is…

  1. Binary classification
  2. Multi-class classification
  3. Multi-label classification
  4. Regression

Which of these is metadata rather than a feature or label?

  1. The pixel values of an X-ray
  2. The diagnosis
  3. The ID of the scanner that took the X-ray
  4. The patient's age used as model input

In the skin-lesion example, why could a model score well without learning about skin?

  1. The images are too small
  2. The label is strongly linked to the clinic and its camera, which the model can recognise
  3. There are too many classes
  4. The labels are multi-label