← Back to the Pattern Trainer

How to use the Pattern Trainer

You'll teach the computer your own groups of pictures, poses or sounds, check how well it learned, then use what it learned in your own Python app.

1. Plan your groups

Each group is called a class. Pick the mode at the top that fits your idea:

Think about what should happen when the camera or microphone sees none of your classes. The model always has to pick one of them, so it often helps to add a "nothing" class (an empty desk, standing still, background noise). Sounds mode starts with a Background noise class for this reason.

2. Collect examples

The model only knows what you show it, so variety matters more than anything else. For each class, change things around while you record: angles, distances, lighting, backgrounds, different hands or voices (if they say yes). Aim for 20 or more examples per class, and about the same number in each.

If the camera or microphone won't turn on, check your browser has permission and close any other app using it. No camera or microphone? Upload photos or .wav sound files instead.

Short on time? You can save your examples at any point (see Save your project).

3. Train and check

Click Train model. It takes a few seconds. You'll see two scores:

The table shows what got mixed up with what. If two classes keep getting confused, ask why: do they look or sound alike? Add more varied examples of both, then train again.

Even the held-back score is a bit generous. If you recorded everything in one go, the held-back examples look a lot like the rest. Real life will be harder.

4. Try to fool it

Now test it the way it will really be used: live, or with Test a photo / Test a sound using things it has never seen. Try a new place, different light, another person. Then try something that isn't any of your classes at all.

You'll probably notice the model can be very sure and still wrong. The percentages show how sure it is, not how right it is. Write down what fools it. You'll use that list to improve the model, and to pick your cut-off: the number below which your app says "I'm not sure" instead of guessing.

5. Save your project

Reloading or closing the tab loses your examples. To carry on next lesson, click Save project. Next time, click Open project, choose that file, then train again.

In Pictures mode, the project file includes tiny copies of your photos, so keep it to yourself if it shows people.

6. Use it in Python

You'll need the Pattern Detector starters. The first time, follow the "Set up" steps in their README (your teacher may have done this for you). After that:

  1. When you're happy with it, click the Download button. The file is called my_model.json in Pictures mode, my_pose_model.json in Body poses and my_audio_model.json in Sounds. It holds your class names and the trained numbers, but no pictures or sounds.
  2. Move it into the starters folder, replacing the sample file with the same name. If your browser called it my_model (1).json, rename it. Use the starter for your mode: image_pattern_trainer.py, pose_pattern_trainer.py or audio_pattern_trainer.py.
  3. Run that starter (for example python image_pattern_trainer.py) and open the address it prints. Your class names appear automatically.
  4. Change the lines marked # ADAPT:: what your app says for each class, and how sure it must be before it says it.

Your app gives the same answers as the trainer, because it uses the same models in the same way.

Good habits