Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (1505280, 1) #563

Closed
jeroen opened this issue Oct 12, 2018 · 6 comments

Comments

@jeroen
Copy link

jeroen commented Oct 12, 2018

I can no longer reproduce this rmd post by @thomasp85 about lime package. The explain() function I errors with: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (1505280, 1). Tried updating lime, keras, tensorflow, python, but no luck. Which part of the pipeline could be causing this problem?

screen shot 2018-10-12 at 2 34 05 pm

I'm using the homebrew version of tensorflow, and keras was installed via keras::install_keras().

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.14

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] magick_2.0      lime_0.4.0.9999 keras_2.2.0    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19       later_0.7.5        gower_0.1.2        pillar_1.3.0       compiler_3.5.1    
 [6] plyr_1.8.4         bindr_0.1.1        base64enc_0.1-3    iterators_1.0.10   tools_3.5.1       
[11] digest_0.6.17      zeallot_0.1.0      jsonlite_1.5       tibble_1.4.2       gtable_0.2.0      
[16] lattice_0.20-35    pkgconfig_2.0.2    rlang_0.2.2        Matrix_1.2-14      foreach_1.4.4     
[21] shiny_1.1.0        rstudioapi_0.8     parallel_3.5.1     bindrcpp_0.2.2     dplyr_0.7.6       
[26] knitr_1.20         htmlwidgets_1.3    grid_3.5.1         glmnet_2.0-16      tidyselect_0.2.4  
[31] reticulate_1.10    glue_1.3.0         R6_2.3.0           ggplot2_3.0.0      purrr_0.2.5       
[36] magrittr_1.5       whisker_0.3-2      shinythemes_1.1.1  promises_1.0.1     htmltools_0.3.6   
[41] codetools_0.2-15   scales_1.0.0       tfruns_1.4         stringdist_0.9.5.1 assertthat_0.2.0  
[46] xtable_1.8-3       mime_0.6           colorspace_1.3-2   httpuv_1.4.5       tensorflow_1.9    
[51] stringi_1.2.4      lazyeval_0.2.1     munsell_0.5.0      crayon_1.3.4
@skeydan
Copy link

skeydan commented Oct 12, 2018

This code (copy-pasted from the .Rmd you linked (thx!)) runs fine for me:

library(keras)
library(lime)
library(magick)

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

img <- image_read('https://www.data-imaginist.com/assets/images/kitten.jpg')
img_path <- file.path(tempdir(), 'kitten.jpg')
image_write(img, img_path)
plot(as.raster(img))

image_prep <- function(x) {
  arrays <- lapply(x, function(path) {
    img <- image_load(path, target_size = c(224,224))
    x <- image_to_array(img)
    x <- array_reshape(x, c(1, dim(x)))
    x <- imagenet_preprocess_input(x)
  })
  do.call(abind::abind, c(arrays, list(along = 1)))
}

model_labels <- readRDS(system.file('extdata', 'imagenet_labels.rds', package = 'lime'))
explainer <- lime(img_path, as_classifier(model, model_labels), image_prep)

explanation <- explain(img_path, explainer, n_labels = 2, n_features = 20)
plot_image_explanation(explanation)

My sessionInfo, for comparison:

R version 3.5.1 (2018-07-02)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Fedora 28 (Workstation Edition)

Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

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

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

other attached packages:
[1] magick_2.0       lime_0.4.0       keras_2.2.0.9001

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.19           later_0.7.5            gower_0.1.2           
 [4] pillar_1.3.0           compiler_3.5.1         plyr_1.8.4            
 [7] bindr_0.1.1            base64enc_0.1-3        iterators_1.0.10      
[10] tools_3.5.1            digest_0.6.18          zeallot_0.1.0         
[13] jsonlite_1.5           tibble_1.4.2           gtable_0.2.0          
[16] lattice_0.20-35        pkgconfig_2.0.2        rlang_0.2.2           
[19] Matrix_1.2-14          foreach_1.4.4          shiny_1.1.0           
[22] rstudioapi_0.8         curl_3.2               parallel_3.5.1        
[25] bindrcpp_0.2.2         dplyr_0.7.6            htmlwidgets_1.3       
[28] grid_3.5.1             glmnet_2.0-16          tidyselect_0.2.4      
[31] reticulate_1.10.0.9002 glue_1.3.0             R6_2.3.0              
[34] ggplot2_3.0.0.9000     purrr_0.2.5            magrittr_1.5          
[37] whisker_0.3-2          shinythemes_1.1.1      promises_1.0.1        
[40] htmltools_0.3.6        codetools_0.2-15       scales_1.0.0          
[43] tfruns_1.4             abind_1.4-5            stringdist_0.9.5.1    
[46] assertthat_0.2.0       xtable_1.8-3           mime_0.6              
[49] colorspace_1.3-2       httpuv_1.4.5           labeling_0.3          
[52] tensorflow_1.9         stringi_1.2.4          lazyeval_0.2.1        
[55] munsell_0.5.0          crayon_1.3.4          

Does this alone (prediction, not involving lime) run for you?

res <- predict(model, image_prep(img_path))

@jeroen
Copy link
Author

jeroen commented Oct 12, 2018

Yes, predict() works fine:

screen shot 2018-10-12 at 5 05 08 pm

@skeydan
Copy link

skeydan commented Oct 12, 2018

Okay, so I can reproduce with the current lime version from github.
With lime from CRAN, it works.

So I guess I'd ask you to open an issue there :-)

@jeroen
Copy link
Author

jeroen commented Oct 12, 2018

Hmm OK you might be right. I downgraded lime and now it no longer shows that error, but instead if freezes at explain (or at least I've been waiting for 30min...).

@skeydan
Copy link

skeydan commented Oct 12, 2018

It takes long for me too (5-10 mins?) but it eventually returns (and I can plot it)

@jeroen
Copy link
Author

jeroen commented Oct 12, 2018

OK thanks! Will move the discussion to --> thomasp85/lime#127

@jeroen jeroen closed this as completed Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants