0%
BuildBuild your first image classifierabout 28 min, 6 steps

Setting up your image lab

Decide what each label means, give every photo a job, collect training photos for two classes, and save the project.

What you are making

In this level you will teach a program to tell two kinds of object apart in a photo: a mug and a glass. This job is called image classification. The input is a photo. The output is one of your two class names, plus a score for each class. You will not write rules such as "look for a handle". You will show the program labelled examples and let it find its own patterns, the approach Level 1 called learning from examples.

You need a browser and either a webcam or photos copied from a phone. Use objects you own, on a clear surface, and keep faces, addresses, and private papers out of the frame. There is nothing to install. No camera or photos today? You can still do everything: the stage called "No photos yet? Use the shape demonstration", further down, gives you a ready-made set of drawings.

Meet the image lab

The image lab is part of this page. On a wide screen it sits beside the article. On a smaller screen, the Practice button opens it over the page, and closing it brings you back to the same place. It has four tabs, in the order you will use them:

  • Photos: name your two classes and add pictures.
  • Train: check your photo counts and press Train model.
  • Test & export: ask the trained model about photos it did not learn from.
  • Notes: your label rules and a log of what you tried.

One project follows you through every Level 2 reading, so the photos you add today will still be there later. Each stage below has a Try this stage button. It shows at the top of the lab which stage you are following, and on a small screen it opens the lab. It never changes your photos.

Build with me · 1

Write down what each label means

Before you take a single photo, decide what each answer means. The program copies whatever pattern your labels contain, so a rule you apply one day and forget the next gives it two different targets to learn.

For this project:

  • mug: a drinking container with a handle.
  • glass: a drinking container without a handle. Plastic cups count as glass here.

These are rules for this project, not a claim about how everyone uses the words. A travel cup with a handle is a mug. A clear cup without a handle is a glass. Leave out three kinds of photo for now: photos with both objects in them, empty scenes, and containers whose handle you cannot see. The model can only ever answer mug or glass, so it has no way to say "both" or "neither".

Two words will keep coming up. A class is one of the allowed answers: mug or glass. A label is the answer you attach to one particular photo. If you label a glass photo as mug, training receives the wrong answer and has no way to ask what you meant.

Your image lab at this stageWorked example
Text
INPUT: one photoQUESTION: which class does it show?ALLOWED ANSWERS: mug or glassNOT AN ANSWER: 97%

Open Notes. The starting text already holds these two rules; reword them if you like. Then open Photos and check that Class 1 is mug and Class 2 is glass. Keep class names short and plain, with no percentages in them.

What to look for

Two class names and a one-sentence rule for each. You can look at an ordinary cup and say which label it gets before any model is involved.

Make it yours

Prefer two other safe household objects, such as spoon and fork? Rename both classes and write equally clear rules. Use your names wherever the readings say mug and glass.

Build with me · 2

Give every photo one job

A model that has seen a photo during training has had the chance to learn that exact photo. Testing it on the same photo tells you very little. So before collecting, decide which objects will teach the model and which will be kept back to test it.

In Photos, the These photos are for menu gives each photo one of four jobs:

  • Training: the only photos the model learns from.
  • Development: photos kept out of training and used to check the model while you are still making changes. Many tools call this a validation set.
  • Final test: photos you keep closed until you have picked your final version, then use once.
  • Unlabelled targets: photos whose correct answer you do not know. The model can predict them, but nobody can score them.

The lab follows the menu exactly. If a photo of a reserved mug is added while Training is selected, the lab has no way to know it was meant to be kept back.

Split by physical object when you can: some mugs and glasses for training, different ones for development, and others again for the final test. All photos of one object stay in the same group. If you only own one mug and one glass, keep a separate session (a different room or background) for development, and write down that your test covers new views of familiar objects, not objects the model has never seen.

Your image lab at this stageWorked example
Text
Training            teaches the modelDevelopment         checks it while you are still improving itFinal test          one last check of the version you chooseUnlabelled targets  photos with no known answer: predictions only

In Notes, list which objects (or which sessions) are for Training, which for Development, and which for Final test. Put the reserved objects to one side. Then check that These photos are for is set to Training.

What to look for

You can point to each training object and each reserved object, and say why the reserved ones will not appear in Training.

Make it yours

If you have only two objects, describe the different room, background, or time of day you will use for development photos. Write 'new views of familiar objects' as the scope of your test.

Build with me · 3

Add the first class, one photo at a time

With Training selected, press the mug button under the menu. Every photo you add now is stored as a training photo labelled mug.

Take photo opens the camera only when you ask for it. Check the frame, take one photo, and the camera closes. One photo at a time lets you look at each one before adding the next. Upload images does the same with JPG, PNG, or WebP files, such as photos copied from a phone. Either way, the lab cuts each picture to a centred square and shrinks it to 224 by 224 pixels, the size its image engine expects. Keep the object near the middle so the crop does not cut it off. The photos stay in this browser; they are not sent to a training server.

Variety matters more than quantity. Ten nearly identical photos teach almost the same thing as one. Between photos, change one of these:

What to varyWhat it teaches the model
Front and side viewsA mug can face any direction
Nearer and fartherA mug can fill more or less of the picture
A different mugThe class is more than one object
A different backgroundThe background should not decide the answer
Different usable lightingBrightness can change while the label stays the same

A photo can be correctly labelled and still useless: a very dark or blurry frame shows no handle. Remove an accidental capture with the small x on its thumbnail.

Your image lab at this stageWorked example
Text
Photos  These photos are for: Training  Label button pressed: mug  Take photo   or   Upload images  Below: 12 training images labelled mug

Select Training and mug. Add photos with Take photo or Upload images, checking each thumbnail as it appears. For your own project, aim for about fifty varied photos per class. The lab lets you train with far fewer while you are learning the controls.

What to look for

The line under the buttons counts your training images labelled mug, and it rises as you add photos. You can still see the handle in each thumbnail after the square crop.

Make it yours

Before taking your next photo, say which row of the table it adds. If you cannot name a row, it is probably a near-copy of a photo you already have.

Build with me · 4

Add the second class without teaching a shortcut

Press the glass button and collect glass photos the same way. The controls do not change; only the label attached to each new photo does. Check which button is highlighted every time you switch objects. The lab trusts the label you selected.

Now the important part. Suppose every mug sits on a wooden desk and every glass sits on a blue mat. Then "blue mat" predicts "glass" perfectly in your collection, and a model can score well by learning the mat instead of the object. This is called a shortcut: something that happens to line up with the label in your photos but is not the thing you care about. It fails the moment someone puts a mug on the mat. The fix is to give both classes the same range of surroundings: both on the desk, both on the mat, both held in a hand, both in dim light.

Keep the two counts roughly similar. Do not make up for having only a few different glasses by taking a hundred near-identical glass photos. Count different objects and conditions, not just files.

Your image lab at this stageWorked example
Text
Training photos, both classes  mug:   desk, blue mat, in a hand, dim light  glass: desk, blue mat, in a hand, dim lightThe label changes. The surroundings do not.

Select glass and collect a similar range of photos to your mugs. Open Train to see both counts side by side. If some background appears in only one class, add photos of the other class on that background.

What to look for

Every background, hand, and lighting condition appears with both labels. Train model becomes available once each class has at least two training photos; a useful project has many more.

Make it yours

Pick one possible shortcut in your own photos, such as a hand in the frame or a patterned tablecloth. Add one photo of each class that includes it, so it no longer points to one answer.

Build with me · 5

No photos yet? Use the shape demonstration

You can learn the whole workflow without a camera. At the bottom of Photos, Use shape demonstration draws forty simple pictures in your browser: circles and squares in different sizes and positions, with the same colours and backgrounds in both classes. The lab gives them their jobs for you: 24 for Training, 8 for Development, and 8 for Final test.

The model you train on them is real, and so are its predictions. But the drawings are far simpler than photos, so a good score on them tells you nothing about mugs, glasses, or any real object.

Loading the demonstration replaces your current class names and photos. If you already have photos you want to keep, press Download project first (the next stage explains it). The lab asks before replacing anything: Replace with shapes or Keep my photos.

Your image lab at this stageWorked example
Text
Generated drawings  circle: 12 Training + 4 Development + 4 Final test  square: 12 Training + 4 Development + 4 Final testSame colours and backgrounds in both classes

Only if you have no photos: press Use shape demonstration. Then switch These photos are for between Training, Development, and Final test to see how the drawings were shared out. If you are using your own photos, do not press the button; just read this stage.

What to look for

The class names become circle and square, and Train shows 12 training images for each. Nothing has been trained yet: adding examples and training are separate steps.

Make it yours

The drawings use the same colours for both shapes on purpose. Predict what could go wrong if every circle were blue and every square were red. It is the shortcut problem from the previous stage.

Build with me · 6

Save a copy of your project

The lab saves your photos and notes in this browser as you work, and the same project follows you through every Level 2 reading. Browser storage is limited, though, and clearing your browsing data wipes it. If the lab runs out of space, it shows a warning.

Download project, at the bottom of the lab, saves one file holding your photos, each photo's job, the class names, the rounds setting, and your notes. Open project loads such a file back. Think of it as the editable source of your experiment: with it, you can always add photos and train again.

A project file does not contain a trained model. You have not trained one yet; the next reading shows how to save the model separately.

Your image lab at this stageWorked example
Text
Download project   saves photos, jobs, class names, rounds setting, notesOpen project       brings all of that back

In Notes, add a run name such as 'mug-glass run 1' and a line saying what you collected. Press Download project and keep the file somewhere you will find it again.

What to look for

You have a downloaded project file. You can say what is inside it (photos, jobs, class names, notes) and what is not (a trained model).

Make it yours

Press Open project and choose the file you just downloaded. Check that the counts in Train and the text in Notes come back unchanged.

Before the next reading

Point to your training objects and your reserved objects, and say why they are kept apart. If they have become mixed up, fix that now, before anything is trained. In the next reading you will press Train model for the first time.

Full reference solution

The whole Level 2 workflow on one page. Each reading covers part of it; this list shows where that part fits. The numbers in the readings are examples, and your own results will differ.

Text
1. Name the two classes and write a one-sentence rule for each.2. Reserve objects or sessions for Development and Final test before collecting.3. In Photos, choose Training and a label, then add varied photos of both classes.4. Add reserved photos under Development and Final test, never under Training.5. In Notes, record label rules, objects, conditions, the split, and a run name.6. In Train, check both counts, keep 20 rounds for the first run, and press Train model.7. Download project and Download model, with matching run names.8. In Test & export, predict Development and read every ID, actual label, prediction, and score.9. Work out accuracy yourself, read the confusion matrix, and compare with the Always- baseline.10. Write one hypothesis. Change only the Training photos (or one stated setting), retrain, and predict the same Development collection.11. Choose a version using Development results. Open its saved model if needed.12. Predict Final test once, after choosing, and report its fraction, conditions, and limits.13. For photos with no known answer, use Unlabelled targets, predict, and Download predictions.csv.14. Keep project, model, notes, and predictions file together under one run name. None of them replaces another.

Compare this with your version. Different names and personal choices are fine when the program follows the same logic.

Keep your progress

Sign in and every reading, quiz, and exercise you finish is saved.

Sign in