t-kalinowski / deep-learning-with-R-2nd-edition-code

Code from the book "Deep Learning with R, 2nd Edition"
https://blogs.rstudio.com/ai/posts/2022-05-31-deep-learning-with-r-2e/
54 stars 22 forks source link

layer_random_flip() doesn't seem to like sequential layer #13

Closed jonbry closed 7 months ago

jonbry commented 7 months ago

I am working on the dog-cat model in Chapter 8, and I get the following error when trying to use layer_random_flip() with a keras_model_sequential():

data_augmentation <- keras_model_sequential() %>%
  layer_random_flip("horizontal") %>%
  layer_random_rotation(0.1) %>%
  layer_random_zoom(0.2)
#> Error in keras_model_sequential() %>% layer_random_flip("horizontal") %>% : could not find function "%>%"

Created on 2024-04-04 with reprex v2.1.0

Here is the error:

Error in py_call_impl(callable, call_args$unnamed, call_args$named) : 
  ValueError: Exception encountered when calling RandomFlip.call().

Attempt to convert a value (<Sequential name=sequential_4, built=False>) with an unsupported type (<class 'keras.src.models.sequential.Sequential'>) to a Tensor.

Arguments received by RandomFlip.call():
  • inputs=<Sequential name=sequential_4, built=False>
  • training=True

The traceback:

── Python Exception Message ─────────
Traceback (most recent call last):
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/keras/src/layers/preprocessing/tf_data_layer.py", line 46, in __call__
    return super().__call__(inputs, **kwargs)
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/keras/src/utils/traceback_utils.py", line 123, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/<user_name>/.virtualenvs/r-tensorflow/lib/python3.10/site-packages/tensorflow/python/framework/constant_op.py", line 108, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Exception encountered when calling RandomFlip.call().

Attempt to convert a value (<Sequential name=sequential_1, built=False>) with an unsupported type (<class 'keras.src.models.sequential.Sequential'>) to a Tensor.

Arguments received by RandomFlip.call():
  • inputs=<Sequential name=sequential_1, built=False>
  • training=True

── R Traceback ──────────────────────
     ▆
  1. ├─... %>% layer_random_zoom(0.2)
  2. ├─keras3::layer_random_zoom(., 0.2)
  3. │ └─keras3:::create_layer(keras$layers$RandomZoom, object, args)
  4. ├─keras3::layer_random_rotation(., 0.1)
  5. │ └─keras3:::create_layer(keras$layers$RandomRotation, object, args)
  6. └─keras3::layer_random_flip(., "horizontal")
  7.   └─keras3:::create_layer(keras$layers$RandomFlip, object, args)
  8.     └─keras3:::compose_layer(object, layer)
  9.       └─reticulate (local) layer(object, ...)
 10.         └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)

I noticed the Python exception shows the package being keras and I confirmed that the version is 3.10.0. Let me know what information I can provide to help troubleshoot the issue.

Thank you!

jonbry commented 7 months ago

I noticed on the example code on keras.posit.com uses the |> pipe operator. I'm not sure if this is helpful, but when I switched to |> I get the following regex output:

data_augmentation <- keras_model_sequential() |>
  layer_random_flip("horizontal") |>
  layer_random_rotation(0.1) |>
  layer_random_zoom(0.2)
#> Error in layer_random_zoom(layer_random_rotation(layer_random_flip(keras_model_sequential(), : could not find function "layer_random_zoom"

Created on 2024-04-04 with reprex v2.1.0

The ValueError in console is the same.

As a side question: Is this the best repository to post these questions? I'm never sure where exactly to post issues that come up when working through the book. Should I be posting this in the keras repository?

t-kalinowski commented 7 months ago

I think this is another manifestation of #10.

The example code works without issue if only keras3 is loaded, but breaks if both keras and keras3 are loaded. I'll aim to get a CRAN release of keras and keras3 out next week to fix this.

For now, I would recommend uninstalling 'keras'

remove.packages("keras")

and restarting the R session. If keras does end up accidentally loaded and you don't want to restart the session, you can temporarily "fix" the issue with this snippet:

assign(
  "class_filters", 
  reticulate:::.globals$class_filters[-length(reticulate:::.globals$class_filters)], 
  envir = reticulate:::.globals
)
t-kalinowski commented 7 months ago

As a side question: Is this the best repository to post these questions?

Here or rstudio/keras are both fine! Thanks for reporting issues!

jonbry commented 7 months ago

For now, I would recommend uninstalling 'keras'

remove.packages("keras")

Thank you so much for getting back to me so quickly! This solved the issue. I didn't realize keras could cause issues without me loading it directly.