Machine Learning in Elixir: comments on chapter 11, version B 3.0

@seanmor5

As Piet reported, the code on page 260 fails at

    image
    |> Nx.from_binary(:u8)

Based on the example at Examples — Bumblebee v0.5.3, I replaced the image line with this pipeline:

    image.file_ref
    |> Kino.Input.file_path()
    |> File.read!()

Now the Run button successfully ingests the image, runs the model, and reports the top predictions. The serving = ... code on page 259 contained the option top_k: 1, so only one prediction is shown. After changing 1 to 5 and uploading the book’s cat image, the five predictions match those shown on page 261, albeit with different probabilities.

Here is the complete modified code from page 260:

image_input = Kino.Input.image("Image", size: {224, 224})
form = Kino.Control.form([image: image_input], submit: "Run")
frame = Kino.Frame.new()

form
|> Kino.Control.stream()
|> Stream.filter(& &1.data.image)
|> Kino.listen(fn %{data: %{image: image}} ->
  Kino.Frame.render(frame, Kino.Markdown.new("Running..."))

  image =
    image.file_ref
    |> Kino.Input.file_path()
    |> File.read!()
    |> Nx.from_binary(:u8)
    |> Nx.reshape({image.height, image.width, 3})

  output = Nx.Serving.run(serving, image)

  output.predictions
  |> Enum.map(&{&1.label, &1.score})
  |> Kino.Bumblebee.ScoredList.new()
  |> then(&Kino.Frame.render(frame, &1))
end)

Kino.Layout.grid([form, frame], boxed: true, gap: 16)