← Teacher guide

Train a model, then use it in Python

Students train a two-class picture model in the Pattern Trainer, test it on the board on photos it hasn't seen, read the starter's classify function, and run the same my_model.json in the Python starter on their own computer.

Pattern Detector projects · Start here · about 40 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict 3 min · Train and try 12 min · Investigate 10 min · Run it in Python 15 min

Before class

The idea: a model is trained on examples in one place (the Pattern Trainer, a web page) and used in another (a Python app), and what travels between them is one small file of class names and numbers. This lesson does that path once, with two things from the student's desk, before any project.

Before class: the starters need installing once per computer (Python 3, then pip install -r requirements.txt, about 0.5 GB, no TensorFlow; setup). Do it before the lesson, or the last step becomes an install lesson. On a student laptop on the school network, check tools.learningwaypoints.com/trainer/ loads (about 35 MB the first time, for the trainer and again for the board) and the camera is allowed. Without a camera, students use Upload photos with photos they have.

Privacy: training happens in the browser and photos never leave the computer (the trainer's Privacy page shows how to check). The board keeps students' test photos in this browser only. my_model.json holds class names and numbers, no photos. Train on things, not people.

Where this sits: the first step of Pattern Detector projects, after Apps with Gradio. It needs Gradio apps, if/else and f-strings; the trainer does the training and pattern_model.py hides numpy. Machine learning with Gradio shows how a model works inside; either can come first.

Running the lesson

  1. Predict: take a show of hands. Most say 'I don't know'.
  2. Train and try: 10 minutes. Ask: what did it say about the thing that was neither, and how sure was it?
  3. Investigate: everyone opens my_model.json in a text editor. Point out there is no photo in it.
  4. Run it in Python: pair students whose install worked with those whose didn't.

The hard part (on purpose)

Moving the downloaded file into the starters folder in place of the sample. Ask students to say out loud which folder the app reads from before they run it.

When students get stuck

What you will seeA question to askThe fix
The board says the file is not a model file, or is a project file.Did you click Download my_model.json (not Save project)?Download the model and open my_model.json.
The board or the trainer says the picture model didn't load.Can this computer open tools.learningwaypoints.com?Ask for the site to be allowed on the school network, then reload.
The Python app still says Cat or Coffee.Which my_model.json is in the starters folder?Replace the sample with your download (same name, my_model.json), then run the app again.
ModuleNotFoundError: No module named 'mediapipe' (or gradio).Is the starters' .venv switched on in this terminal?Activate .venv (see the setup), or run pip install -r requirements.txt.

Answers to the checks

  1. Predict: You train a model on 10 photos of your pencil case and 10 of your water bottle. Then you show it a banana. What will it say?
    Pencil case or water bottle, and often very sure. A model always picks one of the classes it was trained on; it has no idea of 'something else' unless you give it a class for that.
  2. Train and try: Open your model file, add at least 4 photos it has never seen, and see what it says.
  3. Inside the model file: What is in my_model.json?
    Your class names and a lot of numbers
    The trainer turned each photo into numbers, trained on those, and saved only the class names and the trained numbers. No photo can be rebuilt from them.
  4. What the app says: model.top(image) gives back ("Pencil case", 0.62). What does the app say?
    Might be Pencil case, but I'm not confident (0.62).
    0.62 is less than 0.8, so the if branch runs and the app says it isn't sure.
  5. Where the class names come from: Where do the class names in the app come from?
    From my_model.json, so you never type them
    PatternModel.load reads them from the file, in the same order as the trainer. Rename a class in the trainer, download again, and the app follows.

Stretch and support

Stretch: Add a third class, Neither, with photos of other things. Does the thing that was neither now get the right answer?

Support: Train in pairs on one laptop; skip the Python step and do it at the start of Brief B.

Exit ticket

What is inside my_model.json, and why can the same file work in the trainer, on the board and in Python?

Class names and the trained numbers. Each side turns a picture into numbers the same way, then runs the same trained numbers on them.

What each step is for

Where it leads

Next: Brief B (a model you train) builds an app around this model; Brief A (rules you write) can come first. Real world: photo apps, recycling sorters and parcel scanners are a trained model inside an app.