polmiro

polmiro

Machine Learning in Elixir: Error in Chapter 9, identifying cats and dogs again

@seanmor5

the code provided I am running into an error when training the first model that uses transfer learning with ONNX model mobilenetv2-7. I am a bit stuck here, not really sure what may be wrong.

warning: Axon.Optimizers.adam/1 is deprecated. Use Polaris.Optimizers.adam/1 instead
  machine-learning-in-elixir-src/StopReinventingTheWheel.livemd#cell:s4yqqx5n4j6oyja2b2c57iqes4fvcbny:2


12:22:35.339 [warning] found unexpected key in the initial parameters map: "mobilenetv20_output_pred_fwd"
Epoch: 0, Batch: 700, accuracy: 0.7747430 loss: 0.4431267
Batch: 6, accuracy: 0.9151786 loss: 0.1636844
** (ArgumentError) argument at position 1 is not compatible with compiled function template.

{
 <<<<< Expected <<<<<
 #Nx.Tensor<
   f32[32][channels: 3][height: 160][width: 160]
 >
 ==========
 #Nx.Tensor<
   f32[26][channels: 3][height: 160][width: 160]
 >
 >>>>> Argument >>>>>
 , 
 <<<<< Expected <<<<<
 #Nx.Tensor<
   s64[32][1]
 >
 ==========
 #Nx.Tensor<
   s64[26][1]
 >
 >>>>> Argument >>>>>
 }

    (nx 0.6.2) lib/nx/defn.ex:323: anonymous fn/7 in Nx.Defn.compile_flatten/5
    (elixir 1.15.2) lib/enum.ex:1819: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (nx 0.6.2) lib/nx/lazy_container.ex:61: Nx.LazyContainer.Tuple.traverse/3
    (nx 0.6.2) lib/nx/defn.ex:320: Nx.Defn.compile_flatten/5
    (nx 0.6.2) lib/nx/defn.ex:312: anonymous fn/4 in Nx.Defn.compile/3
    (stdlib 5.0.2) timer.erl:270: :timer.tc/2
    (axon 0.6.0) lib/axon/loop.ex:1805: anonymous fn/4 in Axon.Loop.run_epoch/5
    /data/machine-learning-in-elixir-src/StopReinventingTheWheel.livemd#cell:s4yqqx5n4j6oyja2b2c57iqes4fvcbny:10: (file)

Marked As Solved

joshprintsimple

joshprintsimple

@seanmor5

The length of the val_paths has to be divisible by the batch number so that the tensors come out the right shape.
You can just add this line

val_paths = Enum.take(val_paths, 224)

after setting the val_paths. I also removed the Enum.take line at the end because the length of the train_paths is 24000 and that is also divisible by a batch size of 32.
so that cell would look like this now:

{test_paths, train_paths} =
  Path.wildcard("train/*.jpg")
  |> Enum.shuffle()
  |> Enum.split(1000)

{test_paths, val_paths} = test_paths |> Enum.split(750)
val_paths = Enum.take(val_paths, 224)

batch_size = 32
target_height = 160
target_width = 160

train_pipeline =
  CatsAndDogs.pipeline_with_augmentations(
    train_paths,
    batch_size,
    target_height,
    target_width
  )

val_pipeline =
  CatsAndDogs.pipeline(
    val_paths,
    batch_size,
    target_height,
    target_width
  )

test_pipeline =
  CatsAndDogs.pipeline(
    test_paths,
    batch_size,
    target_height,
    target_width
  )

# Enum.take(train_pipeline, 1)

Where Next?

Popular Pragmatic Bookshelf topics Top

New
brianokken
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
alanq
This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Sub Categories: