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

the question of "model$layers$output" #1384

Open
rainoffallingstar opened this issue Oct 22, 2023 · 4 comments
Open

the question of "model$layers$output" #1384

rainoffallingstar opened this issue Oct 22, 2023 · 4 comments

Comments

@rainoffallingstar
Copy link

rainoffallingstar commented Oct 22, 2023

Hi,
I was recently working on an idea : load a .keras weight from local but remove its classifier layer (maybe the same effect as the application_* functions ) and add a new layer for it, as keras for R was implemented from the keras python package, I looked forward for a python solution first:

from tensorflow import keras
original_model = keras.models.load_model('original_model.keras')
new_model = keras.models.Model(inputs=original_model.input, outputs=original_model.layers[-2].output)

these python codes work well ,then I thought the R version would be as the following according to the source codes of this repo :

library(keras)
original_model <- load_model_tf("original_model.keras")
new_model <- keras_model(inputs = original_model$input, outputs = original_model$layers[length(original_model$layers) - 1]$output)

but found that the model object hasn't a method for output,and model$layers$output returns NULL
and the operation on original_model$layers returns only the structure of model and the weight's parameter is loss.
It is there a way to fix this?

@t-kalinowski
Copy link
Member

What you're describing is certainly possible and I would expect it to work.

Would you be able to provide a minimal reproducible example I can run to reproduce the error you are seeing?

@rainoffallingstar
Copy link
Author

Sure,I would like to provide a weight file and Rscript used in the working procedure. These files could be found in google drive.

The minimal reproducible codes :

library(keras)
# load model
original_model <- load_model_tf("test_weight.keras",compile = FALSE)
# create new model
new_model <- keras_model(inputs = original_model$input, outputs = original_model$layers[length(original_model$layers) - 1]$output)
# check the outputs
outputs <-  original_model$layers[length(original_model$layers) - 1]$output
class(outputs)
# check the layers 
outputs2 <-  original_model$layers$output
outputs3 <- original_model$layers
outputs3$ouput

sessioninfo()

R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               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    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

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

other attached packages:
[1] reticulate_1.34.0 rlang_1.1.1       keras_2.13.0     

loaded via a namespace (and not attached):
 [1] vctrs_0.6.4       cli_3.6.1         zeallot_0.1.0     pkgload_1.3.2     png_0.1-8         generics_0.1.3   
 [7] jsonlite_1.8.7    glue_1.6.2        rprojroot_2.0.3   fansi_1.0.5       rappdirs_0.3.3    grid_4.3.1       
[13] tibble_3.2.1      tfruns_1.5.1      base64enc_0.1-3   lifecycle_1.0.3   whisker_0.4.1     compiler_4.3.1   
[19] fs_1.6.2          pkgconfig_2.0.3   Rcpp_1.0.11       here_1.0.1        rstudioapi_0.15.0 lattice_0.21-8   
[25] R6_2.5.1          utf8_1.2.3        pillar_1.9.0      tensorflow_2.14.0 magrittr_2.0.3    Matrix_1.6-1.1   
[31] tools_4.3.1       jpeg_0.1-10

@t-kalinowski
Copy link
Member

Hi, thanks. I'll need a little more to reproduce the issue. Can you please provide the code that was used to generate the ".keras" file? (python or R). Or if not that, then the code in Python where loading the model works as expected.

A cursory inspection indicates that the weights file is not a standard ".keras" file (which is expected to be a zip file w/ a json of config + weights as h5).

Ideally, everything provided is code in the issue thread.

@rainoffallingstar
Copy link
Author

rainoffallingstar commented Oct 23, 2023

sorry, I previously didn't know the code for model generation is necessary, for the weight file was trained in a dataset and saved automaticly by a shiny application under development github links. the weight file was saved when it reached the lowest val loss in validation set through the callbacks arg of fit_generator function during training process. By the way, the weight file was generated in tensorflow 2.9 before.
The code is really in a mass and I public it today.
I would like to pick the main pieces related to the generation of weight file here :

library(keras)
library(tfhub)
library(tfdatasets)
library(tfautograph)
library(reticulate)
mob <- application_mobilenet_v3_small(weights = "imagenet",
                                            include_top = FALSE, 
                                            input_shape = c(224, 224,3))
 data_augmentations <- keras_model_sequential() %>% 
      layer_random_flip() %>% 
      layer_random_rotation(0.2) %>% 
      layer_random_zoom(0.2) 

unfreeze_weights(mob)
inputs <- layer_input(shape = c(224, 224,3) )
outputs <- inputs %>% 
        data_augmentation %>% 
        layer_rescaling(1/255) %>% 
        mob %>%
        layer_dropout(rate = input$dropout_factor,name = "dropout") %>% 
        layer_average_pooling_2d(pool_size = 2,name = "avg_pool") %>%
        layer_flatten() %>%
        layer_dense(units = as.numeric(nums), activation = "softmax",name = "predictions")
model <- keras_model(inputs,
                           outputs)
model %>% 
      compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = "accuracy")
 checkpoint_path <- paste0(getwd(),"/model/",
                                               "/best_weight.keras")
 checkpoint_dir <- fs::path_dir(checkpoint_path)
 batch_size = 10
 cp_callback <- callback_model_checkpoint(
                       filepath = checkpoint_path,
                       save_best_only = TRUE,
                       monitor = "val_loss")
 history <- model %>% fit_generator(
                       generator = training_image_flow, 
                       epochs = 40, 
                       steps_per_epoch = training_image_flow$n/training_image_flow$batch_size,
                       validation_data = validation_image_flow,
                       validation_steps = validation_image_flow$n/validation_image_flow$batch_size,
                       callbacks = list(cp_callback)
                     )

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