Go to Map Courses Lab Glossary How it works Troubleshooting Start →
ITEN
← Course 06 · From model to app (Lesson 06.01 · 18 minutes · No sensor · For everyone)

Measuring for real.

A score of 95 percent can hide a useless model. Here you'll learn to read the mistakes class by class, to choose the threshold, and to measure milliseconds, memory and battery properly, on the phone of the person who'll use the app.

What you learn
  1. Why accuracy alone is misleading, and what to look at instead
  2. What precision and recall are, and how the threshold moves them
  3. How to measure latency, memory and battery the right way
You need to know

If you don't remember one of these, the link takes you to the chapter that explains it.

How do you want to follow it
Preparing the scene…
  1. 01 The misleading score

    A single percentage hides almost everything.

    The leaf model gets 95 percent of its answers right. That sounds great. But if 95 of the 100 test photos show healthy leaves, you can get there just by always answering healthy. This percentage is called accuracy. On its own, it doesn't tell you which mistakes the model makes, or what they cost.

    In practice. Before you look at any score, count how many test examples there are for each class. If the numbers are very different, accuracy alone isn't enough.

01 / 07
(Go deeper)

A few more numbers.


(01)

The math of precision and recall

Among the test photos there are 100 sick leaves and 900 healthy ones. The model says sick for 110 leaves, and 90 of them really are sick. Precision is 90 out of 110, about 82 percent. Recall is 90 out of 100, which is 90 percent.

When you need a single number, people often use F1, an average of the two that punishes a model that neglects either one. Here it's about 0.86. It's handy for comparing two models, but you should choose the threshold by looking at the two numbers separately.

precision = true positives / (true positives + false positives)
recall    = true positives / (true positives + false negatives)
F1        = 2 × precision × recall / (precision + recall)
(02)

Measuring latency the right way

LiteRT has a tool made for this, benchmark_model. It reports the loading time, the first answer, the warm-up runs and the average of the runs that follow, each one separately. On iPhone, Xcode does the same for Core ML models. In the browser, you wrap performance.now around the call to the model.

Whatever tool you use, keep the conditions the same. Screen on, phone not charging, no heavy apps open, the same starting temperature. And repeat the measurement on different days, because system updates change the numbers.

for (let i = 0; i < 10; i++) model(example);            // warm-up
const t = [];
for (let i = 0; i < 100; i++) { const t0 = performance.now(); model(example); t.push(performance.now() - t0); }
t.sort((a, b) => a - b);
console.log("median", t[50], "95 percent", t[95]);
(03)

MLPerf Mobile, comparing phones

MLCommons, a group of companies and universities, publishes MLPerf Mobile, a set of standard tests for models on the phone. Image classification, detection, segmentation and questions about a text, always with the same models and the same rules.

The results are public, and there's an app to measure your own phone. They're for comparing chips, not your model. Your model has to be measured on your reference phone, with your data, as in this lesson.

(Try it on your phone)

The threshold and the two errors


Here are a model's answers on 500 test leaves, 100 sick and 400 healthy. Each leaf has its own percentage for sick. Move the threshold and watch how the confusion matrix, precision and recall change.

The tool needs JavaScript turned on.

The percentages are made up, but they behave like real ones. Find the highest threshold that misses no more than five sick leaves, and see how many false alarms it costs.

(Quiz)

4 questions to close the lesson.

When you have answered them all, the lesson counts as done and its stone on the map turns yellow. Answering is what counts, not the score.


  1. (01)

    95 percent of the test leaves are healthy. The model has 95 percent accuracy. What can you conclude?

  2. (02)

    The model says sick for 50 leaves, and 40 of them really are. There are 80 sick leaves in all. What are precision and recall?

  3. (03)

    How do you measure a model's latency properly?

  4. (04)

    Which phone should you measure on?

To close the lesson, answer the quiz

← Models that see and speak Back to the map
(Next lesson · 06.02) Quantizing