← micro:bit and the computer

Train a recycling sorter

Train a picture model in the Pattern Trainer, then test it on photos it has never seen. It works on photos like its training photos and fails on others, so to fix a model you change its data.

Recycling centres use cameras and trained models to sort paper from plastic on a fast conveyor belt. Before a centre trusts one, someone has to test it on items it has never seen.

A recycling centre wants a camera that sorts Paper from Plastic. You will train a model in the Pattern Trainer, then test it on the board's photos: paper and plastic items your model has never seen, in different places and light.

To train a model: open the Pattern Trainer in a new tab and choose Pictures. Name the first class Paper, then + Add a class and name it Plastic. Add about 20 photos of each (hold the item up to the camera and Hold to record, or Upload photos), and click Train model. Then click Download my_model.json, and on the board click Open your model file and choose it.

No webcam, or no paper and plastic on your desk? Download photos to upload instead (unzip them, then in each class choose Upload photos and select every photo in its folder): plain photos and varied photos.

How this lesson works: Predict, Run, Investigate, Modify, Practise, Make
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

Your model gets your own items right almost every time on the webcam. The board tests it on other paper and plastic photographed on a messy bench. Its score on the messy bench will be…

RunRun it and compare the result with your prediction.

Train and test

Train Paper and Plastic with about 20 photos each, all on the same plain desk (or upload the plain photos). Download your model and Open your model file on the board. Compare the plain background and messy bench scores.

Try it yourselfYour edits are saved in this browser.

Train two classes (Paper and Plastic) on one plain background, open your model file and test it.

Use the board. This ticks when you get there.

InvestigateTrace how each line of code connects to what you see.

Read the results

Accuracy is the number correct divided by the number tested. A score for each group of test photos (plain, messy, low light) shows where a model fails, not just how often.

To find what is wrong, look at the ✗ photos and ask what they have in common that is not the item: the background, the light, the angle. Then compare that with the training photos.

Step What to write Why
1 overall = 23 ÷ 32 = 72% Accuracy is correct ÷ total
2 plain 8/8, messy 4/8, low light 5/8, neither 0/8 Groups show where it fails
3 The messy failures all have a cluttered background Look for what the failures share
4 The training photos were all on one plain desk The model can't tell item from background
5 Fix: add training photos on cluttered backgrounds Change the data, not the settings

Same model, different results: it fails only in low light. Work through steps 3 to 5 for it.

A model gets 23 of 32 test photos right. What is its accuracy, as a percentage (to the nearest whole number)?

This model scores: plain 8/8, messy 4/8, low light 5/8, not paper or plastic 0/8. Its training photos were all on one plain desk. Why does it fail on the messy bench?

Another model scores well everywhere except low light. Which fix is most likely to work?

Sam's model scores 50% on the messy bench. Sam adds 100 more photos of the same items on the same desk. The messy score doesn't change. What went wrong?

ModifyChange the code on purpose and check the result.

Fix the data

Retrain your model with photos of the items on at least three different backgrounds (or upload the varied photos). Download it again and open the new file on the board.

Look at the not paper or plastic group: the model gives a can or a jar a label anyway, often with high confidence. A model always picks one of its classes. Add a third class called Neither with photos of other things (cans, glass, fruit), retrain and test again.

Try it yourselfYour edits are saved in this browser.

Retrain with photos on at least three different backgrounds. Get the messy bench to 75% or more.

Use the board. This ticks when you get there.

You train Paper with 60 photos and Plastic with 10. Which way does the model lean when it is unsure?

The model labels a can or a bottle of glass as paper or plastic. Add a Neither class and get the 'not paper or plastic' group to 75% or more.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Diagnose

A model was trained on photos taken at midday by a window. It scores 100% plain, 88% messy, 25% low light. What is the problem, and the fix?

A model scores 90% on paper and plastic but labels every can as Plastic with 95% confidence. What is the problem, and the fix?

Paper has 80 photos and Plastic has 15. The model calls most plastic items Paper. What is the problem, and the fix?

MakeBuild something new using what you have learned. Expect a few tries; that is normal.

Make a sorter you would trust

Use what you found: varied backgrounds and light, balanced classes, and a Neither class. Then ask a classmate to bring three items your model has never seen. Does it still work?

Try it yourselfYour edits are saved in this browser.

Build a sorter that gets 80% or more overall and 75% or more in low light.

Use the board. This ticks when you get there.

Info

Evaluate

Write one or two sentences: which items or conditions your sorter would still get wrong, and what a real recycling centre would need to test before trusting it.

Revision summary

Now you can
Next lesson5. A model drives a micro:bitThe board sends your model's label down the USB cable as a line of text. The micro:bit's program decides what each label does, and must also handle labels it doesn't expect.
For teachers

Students train a Paper/Plastic picture model in the Pattern Trainer, test it on photos it has never seen, and fix it by changing the training data. About 50 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

Before class

The idea: a model learns only from the examples it is given. It works on new inputs that look like its training photos and fails on ones that don't. The fix is always in the data, not the settings.

How it works: students train in the Pattern Trainer (tools.learningwaypoints.com/trainer/, free, no account), click Download my_model.json, and open that file on the board with Open your model file. The board keeps it in the browser, so the next lessons start with it. The board tests the model on 40 fixed photos it has never seen, in five groups: plain background, messy bench, low light, crumpled or at odd angles, and things that are neither paper nor plastic. Checks tick on the board's scores, not on the training.

Before class: on a student laptop on the school network, open the Pattern Trainer, train a tiny model, download it and open it on the board. The trainer and the board each load about 35 MB from tools.learningwaypoints.com the first time, so check the network allows it. Bring a box of clean paper and plastic (and a few cans, jars or fruit) so every desk has variety.

Privacy: training happens in the browser and photos never leave the computer (the trainer's Privacy page shows how to check). my_model.json holds class names and numbers, no photos, and stays on the student's computer. Train on objects, not faces.

Teachable Machine still works: a student who trains there can export with Upload my model and paste the link into the box under Open your model file. Its webcam is mirrored; the Pattern Trainer's is not.

No webcam? The lesson links two folders of photos to upload instead: plain (30 per class on a white board: trains the biased model) and varied (messy benches, low light, odd angles and a Neither class: trains a good one). The photos come from TrashNet (MIT licence); the messy benches and low light are made from them.

The test photos are similar in style to the downloadable ones, so a model trained only on the varied folder can do well without the webcam. That is fine: the point is what changes the scores.

Running the lesson

  1. Predict: take a show of hands. Most expect 'about the same'.
  2. Train and test: give students 8 minutes to train. Plain is usually high and messy low. Ask: what is different about the messy photos?
  3. Read the results: model the table live with one student's results, one line at a time. Then students do the low-light version themselves.
  4. Fix the data: one change at a time. Students who add more photos of the same desk see no change: that is Sam's mistake, so point back to it.
  5. Neither: ask what label a can gets, and how confident the model is.
  6. Evaluate: collect a few answers about what a recycling centre should test.

The hard part (on purpose)

Accepting that a model trained on one desk fails on a messy bench. Ask what the test photos have that the training photos don't, before adding more data.

When students get stuck

Stretch and support

Stretch: Add a fourth class (Cardboard or Glass). Which pairs of classes does the model mix up most, and why?

Support: Use the downloaded photos instead of the webcam: plain first (the biased model), then varied.

Where it leads

Next: send the model's labels to a micro:bit that sorts with a servo flap.

Real world: automated recycling sorters, and any camera system tested on people or places it wasn't trained on (who might it fail for?).

What each step is for