rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
834 stars 282 forks source link

Problem in R code or in keras library when trying to visualize convnet filters? #1236

Open lovalery opened 3 years ago

lovalery commented 3 years ago

Hello,

As a beginner, I read the very instructive and didactic book by François Chollet with J.J. Allaire "Deep Learning with R".

However, I am currently facing a problem when trying to visualize the convnet filters: when I try to reproduce the code of the book, R returns an error (see reprex below from the code of the book).

Is this an error in the code or is it deprecated due to a recent update of the library?

I would be very grateful if you could clarify this for me and provide a solution.

Kind regards Loïc

REPREX:

library(keras)

model <- application_vgg16(
  weights = "imagenet",
  include_top = FALSE
)

layer_name <- "block3_conv1" 
filter_index <- 1            

layer_output <- get_layer(model, layer_name)$output
loss <- k_mean(layer_output[,,,filter_index])
#> Error in `[.python.builtin.object`(layer_output, , , , filter_index): unused arguments (alist(, , filter_index))

Created on 2021-06-29 by the reprex package (v2.0.0)

If needed, here are the details of my session:

sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 8.1 x64 (build 9600)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
#> [3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
#> [5] LC_TIME=French_France.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] tensorflow_2.5.0 keras_2.4.0 
#>
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.33        magrittr_2.0.1    rlang_0.4.11      fansi_0.5.0      
#>  [5] stringr_1.4.0     styler_1.4.1      highr_0.9         tools_3.6.3      
#>  [9] xfun_0.24         utf8_1.2.1        withr_2.4.2       htmltools_0.5.1.1
#> [13] ellipsis_0.3.2    yaml_2.2.1        digest_0.6.27     tibble_3.1.2     
#> [17] lifecycle_1.0.0   crayon_1.4.1      purrr_0.3.4       vctrs_0.3.8      
#> [21] fs_1.5.0          glue_1.4.2        evaluate_0.14     rmarkdown_2.9    
#> [25] reprex_2.0.0      stringi_1.6.2     compiler_3.6.3    pillar_1.6.1     
#> [29] backports_1.2.1   pkgconfig_2.0.3

Created on 2021-06-29 by the reprex package (v2.0.0)

t-kalinowski commented 3 years ago

Hi, I am unable to reproduce the bug with the current release version of keras (2.6.0). Please me me know if you are still encountering this issue.

github-actions[bot] commented 3 years ago

Automatically closed because there has not been a response for 30 days. When you're ready to work on this further, please comment here and the issue will automatically reopen.

lovalery commented 3 years ago

Hi @t-kalinowski, Thank you very much for your feedback and sorry for the delay in replying. Indeed the update to version 2.6.0 solved the problem... but unfortunately when I want to execute the following line of code (still found in the book of F. Chollet and J.J. Allaire, at bottom of page 153), I get a new error (please, see reprex below).

Thank you in advance for helping me to solve this new problem.

library(keras)

model <- application_vgg16(
  weights = "imagenet",
  include_top = FALSE
)

layer_name <- "block3_conv1" 
filter_index <- 1            

layer_output <- get_layer(model, layer_name)$output
loss <- k_mean(layer_output[,,,filter_index])

grads <- k_gradients(loss, model$input)[[1]]
#> Error in py_call_impl(callable, dots$args, dots$keywords): RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
#> 
#> Detailed traceback:
#>   File "C:\Users\toto\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\backend.py", line 4103, in gradients
#>     loss, variables, colocate_gradients_with_ops=True)
#>   File "C:\Users\toto\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\ops\gradients_impl.py", line 172, in gradients
#>     unconnected_gradients)
#>   File "C:\Users\toto\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\ops\gradients_util.py", line 492, in _GradientsHelper
#>     raise RuntimeError("tf.gradients is not supported when eager execution "

Created on 2021-10-02 by the reprex package (v2.0.1)

If needed, here are the details of my session:

sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 8.1 x64 (build 9600)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
#> [3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
#> [5] LC_TIME=French_France.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] keras_2.6.0 
#>
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.27   withr_2.4.2     magrittr_2.0.1  reprex_2.0.1   
#>  [5] evaluate_0.14   highr_0.9       stringi_1.7.4   rlang_0.4.11   
#>  [9] cli_3.0.1       rstudioapi_0.13 fs_1.5.0        rmarkdown_2.11 
#> [13] tools_4.0.2     stringr_1.4.0   glue_1.4.2      xfun_0.26      
#> [17] yaml_2.2.1      fastmap_1.1.0   compiler_4.0.2  htmltools_0.5.2
#> [21] knitr_1.34      tensorflow_2.6.0 

Created on 2021-10-02 by the reprex package (v2.0.1)

t-kalinowski commented 3 years ago

Using k_gradients() requires a non-eager context. The easiest way to enter graph mode is to wrap your code in a tf_function(). Alternatively, you can use tf$GradientTape in the eager context, though it has different semantics.

Some helpful links: