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:
- Pictures for things you can see: objects, drawings, hand signs.
- Body poses for how someone is standing or moving: exercise form, dance moves, gestures.
- Sounds for things you can hear: words, claps, instruments, machines.
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.
- Hold to record keeps adding examples while you hold the button. Move around slowly while it records.
- Upload adds photos (or .wav sound files) you already have. You can also drag files onto a class.
- Record one (Sounds only) listens for 2 seconds and keeps the loudest second. It's best for short sounds like a clap or a word.
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 score on its own training examples is the easy test. It has already seen the answers, so it's usually close to 100%.
- The held-back score is the fairer one. Before training, the trainer puts aside about a fifth of the examples in each class (the newest ones), then tests the model on them. That tells you how it does on examples it didn't train on. This needs at least 5 examples in each class.
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:
- When you're happy with it, click the Download button. The file is called
my_model.jsonin Pictures mode,my_pose_model.jsonin Body poses andmy_audio_model.jsonin Sounds. It holds your class names and the trained numbers, but no pictures or sounds. - 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.pyoraudio_pattern_trainer.py. - Run that starter (for example
python image_pattern_trainer.py) and open the address it prints. Your class names appear automatically. - 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
- Only record yourself, or people who have said yes.
- Check what's behind you or around you before you record.
- Your pictures and sounds stay on this computer. How your privacy is protected