0%
BuildRecognising imagesabout 41 min, 12 steps

Bring your Level 2 model in

Train an image model in blocks, reuse it for several photos in one run, then save it as a file and load it in a later run without training again.

Carry the Level 2 method into a program

In Level 2 you trained a mug-or-glass model in the image lab. This lesson does the same job with blocks, then reuses the trained model: first several times within one run, then in a later run, by saving it as a file and loading it back. If you downloaded a model from the Level 2 lab, you will be able to load that too.

Keep three things apart as you go:

  • The training collection is the set of labelled photos. You can inspect it, fix a label, or add photos.
  • The trained model holds the numbers learned from those photos. It is used for prediction and contains no photos.
  • The program is your blocks. It feeds new inputs to the model and decides what to do with its answers.

Define the classes and train

The first three stages set up and train a two-class model in blocks: the same mug-or-glass task as your Level 2 lab.

Build with me · 1

Carry over the class definitions

Before reusing a model, carry over the meaning of its labels. The example task distinguishes mugs with handles from glasses without handles. If an object could fit either class, write down how you will label it, so it is not labelled differently on different days.

The words printed here are a reminder for the person collecting examples. They do not train the model. Training still needs actual labelled images.

Blocks at this stageWorked example
make an image model calledcups
saymug: a drinking cup with a handle
sayglass: a drinking vessel without a handle

Create cups, then use two say statements to write the intended classes. Choose objects that fit your definitions.

What to look for

The two definitions print; no model accuracy has been established.

Make it yours

Choose two other safe everyday-object classes if preferred. Update all later capture labels and response assumptions consistently.

Build with me · 2

Build the editable training collection

The collection is the set of labelled photos. You can inspect it, correct a label, or add photos. A trained model is different: it holds learned numbers used for prediction, not photos.

This stack collects a small set of photos inside one run. Use several viewpoints and similar backgrounds for each class. Photos from another page or an earlier run are not available here.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups

Connect the mug and glass capture statements after one make-image-model block for cups. Inspect the groups before adding training.

What to look for

Two labelled groups are collected in this article's session.

Make it yours

Hold one object or viewpoint back. Write down how it differs from training so its later prediction is meaningful.

Build with me · 3

Train the version you will test

Training learns from the collection as it is at that moment. If you correct labels or add examples, you must train again before the model includes that change. Editing the collection does not change a model that was already trained.

Give experiment versions descriptive notes: which objects, how many views, and what changed. A score is hard to interpret if you cannot identify the model and collection that produced it.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups

Put train image model cups after collection. Build the rest of the prediction stages before running if you want to avoid repeating collection.

What to look for

The browser trains after both classes have been supplied.

Make it yours

Write a version note such as two household objects, ten views each, same desk background. Keep it with your observations.

Ask about a new photo

Prediction works exactly as it did in the game: capture one photo, store it, and read both the label and the score from that one stored photo.

Build with me · 4

Supply an image without its answer

The prediction photo arrives without a mug/glass answer attached. The model must choose using its learned parameters. Capturing this photo is not a training update, and the program does not automatically know whether you agree with its result.

Store the photo once and ask for both outputs from that stored input. Reusing the value keeps the label and score aligned.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRecognisedthen
move
sayScorethen
sure

Set photo from the camera after training, then set move and sure using cups and that photo variable. Print both.

What to look for

One label and score describe one captured test image; the measured answer may be wrong.

Make it yours

Show the held-back view and record its intended class before reading output.

Many predictions, one training run

Training takes time and a set of photos, but it only has to happen once per run. After that, the program can ask the model about as many new photos as you like. Keep collection and training above a loop, and put only the prediction inside it.

Build with me · 5

Reuse the trained model for a second photo

The repeat block surrounds prediction only. Collection and training stay above it. Both test photos therefore use the same trained model, so their results can be compared.

Reuse is not continued learning. The second prediction is not automatically better because the model saw the first test photo. No add-photos or train operation occurred between them.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
repeat2times
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRecognisedthen
move
sayScorethen
sure

Wrap only capture, label, score, and output in repeat 2. Keep setup and train outside.

What to look for

The application collects training data once and then asks for two prediction images.

Make it yours

Use two views of the same object. Explain any differences without claiming the first prediction trained the second.

Build with me · 6

Separate a prediction from an application action

A classifier returns evidence; your application decides what to show. This gate asks for a clearer object when the score is low. It does not add the new view to training or promise the next score will improve.

A two-class model can still label unrelated objects confidently. If the real application needs an answer for neither class, it needs photos and testing for that case; a threshold does not create a new class.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
repeat2times
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
if
sure
<60
then
sayPlease show the object more clearly
otherwise
sayRecognisedthen
move
sayScorethen
sure

Place the gate inside the prediction loop after sure is stored. Keep the ask-again message inside the low-score branch. In this version an unclear photo still uses up one of the attempts.

What to look for

Two attempts each produce either a request for a clearer view or an accepted label and score.

Make it yours

Test one mug or glass and one unrelated scene. Record whether either was confidently wrong.

Build with me · 7

Keep evaluation separate from improvement

Numbered attempts help you connect observations to your experiment notes. Leave training fixed while collecting this small evaluation. Changing collection halfway through would mix results from different models.

After reviewing the three outcomes, you may start a new development cycle with corrected or more varied examples. Starting that new cycle is different from quietly replacing a poor score in the old report.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
setattemptto0
repeat3times
changeattemptby1
sayAttemptthen
attempt
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRecognisedthen
move
sayScorethen
sure

Set attempt to 0 before repeat. Add 1 to it at the start of each turn, then capture and print the model result.

What to look for

Three numbered prediction results use one trained model.

Make it yours

For each attempt note intended class, predicted class, score, and conditions. Use those observations to propose one collection improvement.

Build with me · 8

Finish the complete reusable application

This complete program reuses one trained model for three predictions within one run. Downloading its code keeps the instructions, but not the camera photos or the trained model. The next stages save the trained model itself as a file.

Keep a model file together with a note of its class names. Keep the photos separately if you want to improve the model later. Knowing which file you have avoids a common mistake: treating a program or a project file as if it were a ready-to-use trained model.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
setattemptto0
repeat3times
changeattemptby1
sayAttemptthen
attempt
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRaw labelthen
move
sayRaw scorethen
sure
if
sure
<60
then
sayPlease show the object more clearly
otherwise
sayRecognisedthen
move
sayScorethen
sure
sayThe source is a recipe; this fitted camera model belonged to this run

Build the complete sequence, run it, and identify which statements train versus predict. Use the final reference to compare structure only after making your own version.

What to look for

One training run serves three numbered predictions. The closing line is a reminder that the trained model belonged to this run only.

Make it yours

Describe the four pieces in your own words: training collection, learned model, application program, and new prediction input. Explain which pieces would need to be preserved for reuse on another day.

Three different downloads

Everything so far disappears when the run ends. To use a trained model on another day, save the model itself. Three different downloads keep three different things:

DownloadWhat it keepsWhat it is for
Download practice codeThe program's instructionsReading or rerunning the program; it retrains from scratch
A project from the Level 2 labLabelled photos and class namesChanging the photos and training again
A model fileLearned numbers and class namesPredicting straight away, with no photos and no training

Build with me · 9

Save the trained model as its own file

A saved model file contains the learned numbers (the weights), the class names, and the settings needed for prediction. It is different from the program and from a collection of photos. This stage trains the model, then writes it to a file named image-model.codeguide.json. JSON is a common text format for storing data.

The editor offers the file for download. Keep the supplied filename if you want the next stage to load it without changing a path. You can also use a compatible model downloaded from the Level 2 image lab.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
save image modelcupsto fileimage-model.codeguide.json
sayThe fitted cups model has been saved

After collection and training, connect save image model cups to file image-model.codeguide.json. Run the complete stage and choose the resulting model download in Output.

What to look for

Output offers image-model.codeguide.json containing this run's trained classifier. It is a model file, not merely the block source.

Make it yours

Compare the purpose of this download with Download practice code and a training-project download. Which one lets you inspect original labelled examples?

Build with me · 10

Load the saved model without training again

Loading rebuilds cups from the numbers saved in the file. There are no collection or train blocks in this stack. The only camera capture is a new prediction input. This is reuse across runs, rather than only reuse inside a repeat loop.

A file must actually be present before its path can be loaded. The loader accepts a model file saved by this course, not a photograph or a program renamed to end in .json. It checks the file's format before using it.

Blocks at this stageWorked example
load image model fileimage-model.codeguide.jsonascups
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRecognisedthen
move
sayScorethen
sure

First save a model with the previous stage, then upload that downloaded JSON through this editor's Files tab. Check its name is image-model.codeguide.json. Load this stage and Run; if you have no saved model yet, the previous stage creates one entirely here.

What to look for

The program loads the trained classifier and requests one prediction photo. It does not ask you to recollect mug and glass training examples.

Make it yours

Show a new object view and record the predicted label and score. Explain why displaying the photo did not add it to the original training project.

This is where your Level 2 model comes in. The Level 2 image lab's Download model button saves a file in this same format, with the same name, image-model.codeguide.json. Upload it through the Files tab and the load stage uses your own mug-or-glass model, with no collecting and no training.

Build with me · 11

Use a loaded model in the same application

The application loop can use a loaded model exactly as it used the freshly trained one. Its job is to provide inputs and interpret outputs; it does not need to know whether the model was trained earlier in the run or loaded from a file.

Keep the model file and the class definitions together. If a different file contains different classes, the messages and assumptions around prediction may need to change. A compatible file format does not prove that a model performs your intended task.

Blocks at this stageWorked example
load image model fileimage-model.codeguide.jsonascups
repeat3times
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRaw labelthen
move
sayRaw scorethen
sure
if
sure
<60
then
sayPlease show the object more clearly
otherwise
sayRecognisedthen
move
sayScorethen
sure

Keep the saved model in Files. Put only prediction and the confidence gate inside repeat 3, with load above it.

What to look for

One file load serves three prediction attempts with no training capture sequence.

Make it yours

Explain how you would improve this model: revise the training collection, train a new version, save it, then load that new file. Prediction alone does none of those steps.

Build with me · 12

Verify a complete save-and-reload round trip

The full reference includes collection, training, saving, loading, and prediction, so it can be rebuilt from an empty editor. Saving writes the trained model to the file, and loading replaces cups with the copy read back from that file. The predictions that follow use the reloaded copy.

For everyday reuse, keep the model file and run the shorter load-only stage. Keep an editable project or collection separately if you intend to improve the training data. This completes the four pieces: program, labelled collection, trained model file, and new input.

Blocks at this stageWorked example
make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
save image modelcupsto fileimage-model.codeguide.json
load image model fileimage-model.codeguide.jsonascups
repeat3times
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRecognisedthen
move
sayScorethen
sure
if
sure
<60
then
sayPlease show the object more clearly
otherwise
sayRecognisedthen
move
sayScorethen
sure
sayThese predictions used the saved-and-reloaded model

Build the round-trip stack, run it, and download the model file. Then use the preceding load-only stage to confirm that another prediction run needs only the model file and a new photo, rather than collecting the training groups again.

What to look for

A real model file is produced and reloaded, then used for three predictions. The measured results depend on your collected photos.

Make it yours

Record a model version note with class meanings and collection conditions. Explain which version produced your saved evaluation results.

Name the four pieces

You have understood this lesson when you can name four things and say which operation creates or changes each one: the training collection (collecting photos), the trained model (training, or loading a file), the program (your blocks), and a new input (a camera capture). Prediction changes none of the first three. Showing the model a new photo does not teach it anything; only collecting and training again does.

Full reference solution

This is the complete worked program. Try building it yourself first, then use this reference to find the first place your version behaves differently. The Python below is generated from these exact blocks; helper functions are included so its behaviour can be inspected.

make an image model calledcups
add10webcam photos tocupslabeledmug
add10webcam photos tocupslabeledglass
show the photos incups
train image modelcups
save image modelcupsto fileimage-model.codeguide.json
load image model fileimage-model.codeguide.jsonascups
repeat3times
setphototo
a webcam photo
setmoveto
whatcupssees in
photo
setsureto
out of 100, how surecupsis about
photo
sayRecognisedthen
move
sayScorethen
sure
if
sure
<60
then
sayPlease show the object more clearly
otherwise
sayRecognisedthen
move
sayScorethen
sure
sayThese predictions used the saved-and-reloaded model
PythonHover over a line to see an explanation
# ---------------------------------------------------------------# This file uses image blocks, which need a camera and a browser.# That part runs here rather than anywhere Python runs.# Everything else below is ordinary Python.# ---------------------------------------------------------------  # ---------------------------------------------------------------# Building blocks, written out in plain Python.# This part is generated for you. Your script starts further down.# ---------------------------------------------------------------  # These blocks need a camera and a browser, so this part runs here rather# than anywhere Python runs. Everything above this line is ordinary Python.from js_images import pagefrom pyodide.ffi import run_sync  class ImageModel:    """A model that tells photos apart. It does not look at the pixels itself.    A pretrained network turns each photo into a list of numbers, and a small    model on top learns which lists go with which label."""     def __init__(self, name="model"):        self.name = name        self.labels = []        self.trained = False     def capture(self, count, label):        """Opens the camera panel and waits while you take the photos."""        taken = run_sync(page.capture(self.name, int(count), str(label)))        if str(label) not in self.labels:            self.labels.append(str(label))        print("Took " + str(taken) + " photos of '" + str(label) + "'.")     def train(self):        if len(self.labels) < 2:            raise ValueError(                "A model needs at least two labels to tell anything apart. "                "Add photos under a second label before training."            )        run_sync(page.train(self.name))        self.trained = True        print("Trained " + self.name + " on " + str(len(self.labels)) + " labels.")     def _ready(self):        if not self.trained:            raise ValueError(                "This model has not been trained yet. Add photos and train it first."            )     def classify(self, photo):        """What the model thinks is in the photo."""        self._ready()        return run_sync(page.classify(self.name, photo))     def confidence(self, photo):        """Largest class score out of 100. Unfamiliar photos can still receive        a high score; measure correctness separately on labelled examples."""        self._ready()        return run_sync(page.confidence(self.name, photo))     def show(self):        run_sync(page.show(self.name))  def new_image_model(name="model"):    return ImageModel(name)  def capture_one():    """Opens the camera panel for one photo and hands it back."""    return run_sync(page.capture_one())  def load_image_file(path, name="model"):    """Restore local learned weights without training again."""    with open(str(path), "r", encoding="utf-8") as source:        contents = source.read()    model = ImageModel(name)    model.labels = run_sync(page.load_image_file(name, contents))    model.trained = True    print("Loaded " + name + " with " + str(len(model.labels)) + " labels.")    return model  def save_image_file(model, path="image-model.codeguide.json"):    """Save the fitted model, separately from the program and training photos."""    model._ready()    contents = run_sync(page.save_image_file(model.name))    with open(str(path), "w", encoding="utf-8") as target:        target.write(contents)    print("Saved " + str(path))  def load_teachable_machine(url, name="model"):    """Loads a model you published from Teachable Machine. The link is the one    under Export Model, Upload my model."""    model = ImageModel(name)    model.labels = run_sync(page.load_teachable_machine(name, str(url)))    model.trained = True    print("Loaded " + name + " with " + str(len(model.labels)) + " labels.")    return model  # ---------------------------------------------------------------# Your script# ---------------------------------------------------------------  cups = new_image_model("cups")cups.capture(10, "mug")cups.capture(10, "glass")cups.show()cups.train()save_image_file(cups, "image-model.codeguide.json")cups = load_image_file("image-model.codeguide.json", "cups")for _ in range(int(3)):    photo = capture_one()    move = cups.classify(photo)    sure = cups.confidence(photo)    print("Recognised", move)    print("Score", sure)    if (sure < 60):        print("Please show the object more clearly")    else:        print("Recognised", move)        print("Score", sure)print("These predictions used the saved-and-reloaded model")

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