Go to Map Courses Lab Glossary How it works Troubleshooting Start →
ITEN
← Course 02 · Seeing (Lesson 02.01 · 25 minutes · Camera · First model)

Classifying a photo.

A mug, a bunch of keys and the empty table. Follow a photo into MobileNet, from the square of pixels to the answer, and find out why twenty photos per class are all you need.

What you learn
  1. How a network that works on images goes from pixels to shapes
  2. What the body and the head of a model are, and which one you train
  3. What the percentage next to the answer means, and what it doesn't mean
How do you want to follow it
Preparing the scene…
  1. 01 The question

    One photo, and one answer out of a few.

    Put a mug and a bunch of keys on the table. You want the phone to look at them and say which of the two it sees. That's a classification01.04. The model knows a few possible answers, called classes01.03, and for each photo it picks one. Here there are three classes, mug, keys and nothing.

    In practice. Before you collect the photos, write the classes down on a sheet of paper. If you mix up two classes yourself when you look at them, the model will mix them up even more.

01 / 07
(Go deeper)

A few more numbers.


(01)

MobileNet, a network made for the phone

The networks that won the image contests in 2015 had tens of millions of weights. MobileNet, presented by Google in 2017, splits each convolution into two lighter steps. First a separate filter for each channel, then a filter just one pixel wide that mixes them. The result is almost the same and takes eight to nine times fewer calculations.

Version 2, from 2018, has 3.4 million weights and does 300 million multiplications per photo. On ImageNet, which has a thousand classes, it gets the answer right on the first try 72 percent of the time. ResNet-50, seven times heavier, reaches 76.

  • 3.4 Mweights in MobileNet v2
  • 300 Mmultiplications for each photo
  • 72%right answers on the first try on ImageNet
(02)

From scores to percentages

The head gives each class a score, which can be negative or as large as it likes. To turn the scores into percentages you use a function called softmax. First it makes all the scores positive with the exponential, then it divides each one by the sum of all of them. That way the percentages always add up to one hundred.

Written for class i, with z as the scores, the formula is the one below. Its flaw is the same one you saw in the chapter on confidence. The percentages are shared among the classes the model knows, even when the photo contains none of them.

p(i) = exp(z_i) / ( exp(z_1) + exp(z_2) + … + exp(z_n) )
(03)

The code, if you program

In the browser you can write all this in a few lines with TensorFlow.js, the library the lab uses too. The body is a MobileNet that's already trained and gives back the 1,280 numbers. The head is a single dense layer with three outputs and the softmax.

You compute the 1,280 numbers of each photo once, then you train only the head. That's why training on the phone takes seconds and not hours.

const features = body.predict(photo);           // 1280 numbers per photo
const head = tf.sequential({ layers: [
  tf.layers.dense({ inputShape: [1280], units: 3, activation: "softmax" }),
] });
head.compile({ optimizer: "adam", loss: "categoricalCrossentropy" });
await head.fit(allFeatures, labels, { epochs: 20 });
(Try it on your phone)

Your first model


In the lab you train the model from this lesson with your own photos. You need ten minutes, a mug, a bunch of keys and a table.

  1. Open the lab with the button below. It already starts with the Photo input.
  2. Create three classes and name them mug, keys and nothing.
  3. Hold to take photos, 20 per class, while you move around the object. For nothing, point the camera at the empty table.
  4. Press Train and watch the two curves go down.
  5. In Use, try it in different light and on another table, and note when it gets it wrong.
Open the lab →
(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)

    Why are about twenty photos per class enough, and not thousands?

  2. (02)

    A model only knows mug and keys. You point the camera at a banana. What happens?

  3. (03)

    What does a filter in the first layer do?

  4. (04)

    Why should you keep the object in the center when you take the photo?

To close the lesson, answer the quiz

← The method in seven steps Back to the map
(Next lesson · 02.02) A good image dataset