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

Error in DeepDream code from Chapter 12 (Listing 12.15) #8

Closed maitra closed 11 months ago

maitra commented 11 months ago

I run the following code:

base_image_path <- "coast.jpg"
original_img <- preprocess_image(base_image_path)
original_HxW <- dim(original_img)[2:3]
calc_octave_HxW <- function(octave) (as.integer(round(original_HxW / (octave_scale ^ octave))))
octaves <- seq(num_octaves - 1, 0) %>%
{ zip_lists(num = ., HxW = lapply(., calc_octave_HxW)) }
str(octaves)
shrunk_original_img <- original_img %>% tf$image$resize(octaves[[1]]$HxW)
img <- original_img
for (octave in octaves) {
cat(sprintf("Processing octave %i with shape (%s)\n", octave$num, paste(octave$HxW, collapse = ", ")))
img <- img %>%
tf$image$resize(octave$HxW) %>%
gradient_ascent_loop(iterations = iterations, learning_rate = step, max_loss = max_loss)
upscaled_shrunk_original_img <- shrunk_original_img %>% tf$image$resize(octave$HxW)
same_size_original <- original_img %>% tf$image$resize(octave$HxW)
lost_detail <- same_size_original - upscaled_shrunk_original_img
img %<>% "+"(lost_detail)
shrunk_original_img <- original_img %>% tf$image$resize(octave$HxW)
}
img <- deprocess_image(img)
img %>% display_image_tensor()
img %>% tf$io$encode_png() %>% tf$io$write_file("dream.png", .)

But I get the error:

List of 3
 $ :List of 2
  ..$ num: int 2
  ..$ HxW: int [1:2] 459 612
 $ :List of 2
  ..$ num: int 1
  ..$ HxW: int [1:2] 643 857
 $ :List of 2
  ..$ num: int 0
  ..$ HxW: int [1:2] 900 1200

Processing octave 2 with shape (459, 612)
Error in py_call_impl(callable, call_args$unnamed, call_args$named) : 
  TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type float64 of argument 'x'.
Run `reticulate::py_last_error()` for details.

No sure why there is a type mismatch.

t-kalinowski commented 11 months ago

Can you post a little more detail about your setup? What is the output of sessionInfo() and reticulate::py_config()? Also, what is the output from reticulate::py_last_error() (what is the R traceback?)

If you're on Windows, I suspect you might be encountering some variation of this issue: https://github.com/rstudio/reticulate/issues/1473. Alternatively, a more recent version of TensorFlow might now have more strict type promotion semantics.

The error message suggests that a well-placed tf$cast(x, "float32") should fix the error.

maitra commented 11 months ago

Thanks for looking into this! Sorry, I am on Fedora 38 Linux OS. Here are the outputs:


> reticulate::py_last_error()

── Python Exception Message ────────────────────────────────────────────────────
Traceback (most recent call last):
  File "/home/maitra/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/maitra/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/tensorflow/python/eager/execute.py", line 59, in quick_execute
    except TypeError as e:
tensorflow.python.framework.errors_impl.NotFoundError: Graph execution error:

Detected at node 'ReadFile' defined at (most recent call last):
    File "<string>", line 4, in fn
    File "/usr/lib64/R/library/reticulate/python/rpytools/call.py", line 7, in python_function
      value, error = rpycall.call_r_function(f, *args, **kwargs)
Node: 'ReadFile'
coast.jpg; No such file or directory
         [[{{node ReadFile}}]] [Op:__inference_fn_5336]

── R Traceback ─────────────────────────────────────────────────────────────────
    ▆
 1. └─reticulate (local) preprocess_image(base_image_path)
 2.   └─reticulate:::py_call_impl(callable, call_args$unnamed, call_args$named)

And here is:

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Fedora Linux 38 (Thirty Eight)

Matrix products: default
BLAS/LAPACK: FlexiBLAS OPENBLAS-OPENMP;  LAPACK version 3.11.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Chicago
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] tensorflow_2.14.0 keras_2.13.0     

loaded via a namespace (and not attached):
 [1] backports_1.4.1   R6_2.5.1          base64enc_0.1-3   Matrix_1.6-4     
 [5] lattice_0.22-5    reticulate_1.34.0 magrittr_2.0.3    rappdirs_0.3.3   
 [9] png_0.1-8         generics_0.1.3    lifecycle_1.0.4   cli_3.6.1        
[13] grid_4.3.2        zeallot_0.1.0     tfautograph_0.3.2 tfruns_1.5.1     
[17] compiler_4.3.2    rprojroot_2.0.4   here_1.0.1        whisker_0.4.1    
[21] Rcpp_1.0.11       rlang_1.1.2       jsonlite_1.8.7   

And here is:

> reticulate::py_config()
python:         /home/maitra/.virtualenvs/r-reticulate/bin/python
libpython:      /usr/lib64/libpython3.11.so
pythonhome:     /home/maitra/.virtualenvs/r-reticulate:/home/maitra/.virtualenvs/r-reticulate
version:        3.11.6 (main, Oct  3 2023, 00:00:00) [GCC 13.2.1 20230728 (Red Hat 13.2.1-1)]
numpy:          /home/maitra/.virtualenvs/r-reticulate/lib/python3.11/site-packages/numpy
numpy_version:  1.24.3
tensorflow:     /home/maitra/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/tensorflow

What am I casting here with tf$cast(x, "float32")? Where would I place this? Thank you again for your help!

t-kalinowski commented 11 months ago

What version of TensorFlow and Keras are you using?

tensorflow::tf_config()
keras::keras$`__version__`
maitra commented 11 months ago

Thank you again!

> tensorflow::tf_config()
TensorFlow v2.13.1 (~/.virtualenvs/r-reticulate/lib64/python3.11/site-packages/tensorflow)
Python v3.11 (~/.virtualenvs/r-reticulate/bin/python)
> keras::keras$`__version__`
Error in py_get_attr_impl(x, name, silent) : 
  AttributeError: module 'tensorflow.keras' has no attribute '__version__'
Run `reticulate::py_last_error()` for details.

I guess that is odd:

> reticulate::py_last_error()

── Python Exception Message ────────────────────────────────────────────────────
AttributeError: module 'tensorflow.keras' has no attribute '__version__'

── R Traceback ─────────────────────────────────────────────────────────────────
    ▆
 1. ├─keras::keras$`__version__`
 2. └─reticulate:::`$.python.builtin.module`(keras::keras, `__version__`)
 3.   └─reticulate:::`$.python.builtin.object`(x, name)
 4.     └─reticulate:::py_get_attr_or_item(x, name, TRUE)
 5.       └─reticulate::py_get_attr(x, name)
 6.         └─reticulate:::py_get_attr_impl(x, name, silent)
t-kalinowski commented 11 months ago

See https://github.com/t-kalinowski/deep-learning-with-R-2nd-edition-code/commit/5d666f93d52446511a8a8e4eb739eba1c0ffd199 for how to fix the error. Thank you for reporting!

maitra commented 11 months ago

Thanks for looing into this!