Load a retrained keras mobilenet model

Hi,
I am using the mobilenet model application_mobilenet to create a personal model that I have retrained. After that, I saved the model with save_model_hdf5. But when I try to use the model again with load_model_hdf5, I've got this problem reading the model: Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: Unknown activation function:relu6

I found in this Keras Github Issue that when you want to reuse a personal mobilenet model you need to specify the Custom Objects to be loaded: how can I specify this custom objetcs in R?
Many thanks.

Hi!

Can you please create a reprex, so that we can reproduce your error? Thanks!

Hi,
The reproducible example is:

library(keras)
input_tensor <- layer_input(shape = list(224, 224, 3), name = "input_tensor")
conv_base <- application_mobilenet(
  weights = "imagenet",
  include_top = FALSE,
  input_shape = c(224, 224, 3)
)
output_tensor <- input_tensor %>%
  conv_base() %>%
  layer_global_average_pooling_2d() %>%
  layer_dense(units = 1024, name = "fc1") %>%
  layer_activation(activation = "relu") %>%
  layer_batch_normalization() %>%
  layer_dense(units = 2, activation = "softmax", name = "fc4")
model <- keras_model(input_tensor, output_tensor)
save_model_hdf5(object = model, filepath = "model.h5")
modelo <- load_model_hdf5(filepath = "model.h5")

My environment is:

> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.4 (2018-03-15)
 system   x86_64, mingw32             
 ui       RStudio (1.2.933)           
 language (EN)                        
 collate  Spanish_Spain.1252          
 tz       Europe/Berlin               
 date     2018-10-02                  

Packages --------------------------------------------------------------------------------------------------------------------------
 package    * version date       source                         
 assertthat   0.2.0   2017-04-11 CRAN (R 3.4.4)                 
 backports    1.1.2   2017-12-13 CRAN (R 3.4.3)                 
 base       * 3.4.4   2018-03-15 local                          
 base64enc    0.1-3   2015-07-28 CRAN (R 3.4.1)                 
 bindr        0.1.1   2018-03-13 CRAN (R 3.4.4)                 
 bindrcpp     0.2.2   2018-03-29 CRAN (R 3.4.4)                 
 compiler     3.4.4   2018-03-15 local                          
 datasets   * 3.4.4   2018-03-15 local                          
 devtools     1.13.6  2018-06-27 CRAN (R 3.4.4)                 
 digest       0.6.15  2018-01-28 CRAN (R 3.4.3)                 
 dplyr        0.7.4   2017-09-28 CRAN (R 3.4.3)                 
 glue         1.3.0   2018-07-17 CRAN (R 3.4.4)                 
 graphics   * 3.4.4   2018-03-15 local                          
 grDevices  * 3.4.4   2018-03-15 local                          
 grid         3.4.4   2018-03-15 local                          
 here         0.1     2017-05-28 CRAN (R 3.4.4)                 
 jsonlite     1.5     2017-06-01 CRAN (R 3.4.4)                 
 keras      * 2.2.0   2018-08-24 CRAN (R 3.4.4)                 
 knitr        1.20    2018-02-20 CRAN (R 3.4.4)                 
 lattice      0.20-35 2017-03-25 CRAN (R 3.4.4)                 
 magrittr     1.5     2014-11-22 CRAN (R 3.4.4)                 
 Matrix       1.2-12  2017-11-30 CRAN (R 3.4.4)                 
 memoise      1.1.0   2017-04-21 CRAN (R 3.4.4)                 
 methods    * 3.4.4   2018-03-15 local                          
 packrat      0.4.9-2 2018-04-20 CRAN (R 3.4.4)                 
 pillar       1.2.1   2018-02-27 CRAN (R 3.4.4)                 
 pkgconfig    2.0.1   2017-03-21 CRAN (R 3.4.4)                 
 R6           2.2.2   2017-06-17 CRAN (R 3.4.4)                 
 Rcpp         0.12.18 2018-07-23 CRAN (R 3.4.4)                 
 reticulate   1.10    2018-08-05 CRAN (R 3.4.4)                 
 rlang        0.2.2   2018-08-16 CRAN (R 3.4.4)                 
 rprojroot    1.3-2   2018-01-03 CRAN (R 3.4.4)                 
 stats      * 3.4.4   2018-03-15 local                          
 tensorflow   1.9     2018-08-07 CRAN (R 3.4.4)                 
 tfruns       1.3     2018-05-24 Github (rstudio/tfruns@03fb652)
 tibble       1.4.2   2018-01-22 CRAN (R 3.4.4)                 
 tools        3.4.4   2018-03-15 local                          
 utils      * 3.4.4   2018-03-15 local                          
 whisker      0.3-2   2013-04-28 CRAN (R 3.4.4)                 
 withr        2.1.2   2018-03-15 CRAN (R 3.4.4)                 
 zeallot      0.1.0   2018-01-28 CRAN (R 3.4.4)  
1 Like

Thanks! It would be better to use the reprex package, so that

  1. you can be sure all and only the packages which are strictly necessary for the MCVE to run are included
  2. we can also see the output and actual error.

But this is already a big help. Thanks again.

I ran your MCVE and I got no errors or warnings:

library(keras)
input_tensor <- layer_input(shape = list(224, 224, 3), name = "input_tensor")
conv_base <- application_mobilenet(
  weights = "imagenet",
  include_top = FALSE,
  input_shape = c(224, 224, 3)
)
output_tensor <- input_tensor %>%
  conv_base() %>%
  layer_global_average_pooling_2d() %>%
  layer_dense(units = 1024, name = "fc1") %>%
  layer_activation(activation = "relu") %>%
  layer_batch_normalization() %>%
  layer_dense(units = 2, activation = "softmax", name = "fc4")
model <- keras_model(input_tensor, output_tensor)
save_model_hdf5(object = model, filepath = "model.h5")
modelo <- load_model_hdf5(filepath = "model.h5")

Created on 2018-10-02 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> Warning: running command 'timedatectl' had status 1
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.4 (2018-03-15)
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  C.UTF-8                     
#>  tz       Etc/UTC                     
#>  date     2018-10-02
#> Packages -----------------------------------------------------------------
#>  package    * version date       source        
#>  backports    1.1.2   2017-12-13 RSPM (R 3.4.4)
#>  base       * 3.4.4   2018-07-08 local         
#>  base64enc    0.1-3   2015-07-28 RSPM (R 3.4.4)
#>  compiler     3.4.4   2018-07-08 local         
#>  datasets   * 3.4.4   2018-07-08 local         
#>  devtools     1.13.6  2018-06-27 RSPM (R 3.4.4)
#>  digest       0.6.17  2018-09-12 RSPM (R 3.4.4)
#>  evaluate     0.11    2018-07-17 RSPM (R 3.4.4)
#>  graphics   * 3.4.4   2018-07-08 local         
#>  grDevices  * 3.4.4   2018-07-08 local         
#>  grid         3.4.4   2018-07-08 local         
#>  htmltools    0.3.6   2017-04-28 RSPM (R 3.4.4)
#>  jsonlite     1.5     2017-06-01 RSPM (R 3.4.4)
#>  keras      * 2.2.0   2018-08-24 RSPM (R 3.4.4)
#>  knitr        1.20    2018-02-20 RSPM (R 3.4.4)
#>  lattice      0.20-35 2017-03-25 CRAN (R 3.4.4)
#>  magrittr     1.5     2014-11-22 RSPM (R 3.4.4)
#>  Matrix       1.2-12  2017-11-30 CRAN (R 3.4.4)
#>  memoise      1.1.0   2017-04-21 RSPM (R 3.4.4)
#>  methods    * 3.4.4   2018-07-08 local         
#>  R6           2.2.2   2017-06-17 RSPM (R 3.4.4)
#>  Rcpp         0.12.19 2018-10-01 CRAN (R 3.4.4)
#>  reticulate   1.10    2018-08-05 RSPM (R 3.4.4)
#>  rmarkdown    1.10    2018-06-11 RSPM (R 3.4.4)
#>  rprojroot    1.3-2   2018-01-03 RSPM (R 3.4.4)
#>  stats      * 3.4.4   2018-07-08 local         
#>  stringi      1.2.4   2018-07-20 RSPM (R 3.4.4)
#>  stringr      1.3.1   2018-05-10 RSPM (R 3.4.4)
#>  tensorflow   1.9     2018-08-07 RSPM (R 3.4.4)
#>  tfruns       1.4     2018-08-25 RSPM (R 3.4.4)
#>  tools        3.4.4   2018-07-08 local         
#>  utils      * 3.4.4   2018-07-08 local         
#>  whisker      0.3-2   2013-04-28 RSPM (R 3.4.4)
#>  withr        2.1.2   2018-03-15 RSPM (R 3.4.4)
#>  yaml         2.2.0   2018-07-25 RSPM (R 3.4.4)
#>  zeallot      0.1.0   2018-01-28 RSPM (R 3.4.4)

Let's see:

  • If you delete the model.h5 file and run your code again, do you still get the same error?
  • Did you use some "esoteric" setup when configuring the Tensorflow backend with the install_keras() function?
1 Like

A post was split to a new topic: problem loading a trained mobilenet - "Error in py_call_impl(callable, ..., ...) : TypeError: '<' not supported between instances of 'dict' and 'float'