Shiny App : Keras/tensorflow Problem

Hello,
Have been playing around with dogs and cats data set. I successfully built a Keras Classification CNN. So far so good.
Then I created a shiny app.... and it runs ok, locally!
When I try to publish the app (shinyapps.io), it seems that everything goes well. Though, when I open the deployed app I receive this:

I have to say that I am puzzled by the this message. I believe that it has something to do with path and/or with the environments (or not!), but can't find a solution.

Below the app code

library(shiny)
library(keras)
library(tidyverse)
library(reticulate)

model <- load_model_hdf5("dog_cat_class_model.h5")

ui <- fluidPage(
    titlePanel("Hello dear user"),
    sidebarLayout(
        sidebarPanel(
            fileInput("image_path", label = "Input a JPEG image")
        ),
        # Main panel for displaying outputs ----
        mainPanel(
            # Output: Histogram ----
            textOutput(outputId = "prediction"),
            plotOutput(outputId = "image")
        )
    )
)

server <- function(input, output) {
    
    image <- reactive({
        req(input$image_path)
        jpeg::readJPEG(input$image_path$datapath)
    })
    
    
    output$prediction <- renderText({
        
        image_new <- req(input$image_path$datapath)%>%
            image_load(.,target_size = c(150, 150))
        
        image_new_array <- image_to_array(image_new)
        
        ready_array <- array_reshape(image_new_array, c(1, 150, 150, 3))
        
        image_new_gen <- image_data_generator(rescale = 1/255)
        
        image_new_generator = flow_images_from_data(
            ready_array,
            generator = image_new_gen,
            batch_size = 1)
        
        preds <- predict_generator(model,
                                   image_new_generator,
                                   step=1,
                                   verbose=1)%>%
            as_tibble(.name_repair="unique")%>%
            rename(Prob_Dog=1)%>%
            mutate(Prob_Dog=(Prob_Dog*100))
        
        pred_prob<-round(preds$Prob_Dog,1) 
        
        paste0("The predicted prob of being a dog is ", pred_prob,"%" )
        
    })
    
    output$image <- renderPlot({
        plot(as.raster(image()))
    })
    
}

shinyApp(ui, server)

Thank You

You need to set up your virtual invironment properly to let shinyapps.io know that you are using python. Did you add the Rprofile file with all the settings to your app?

Have a look at this example app and tutorial:

What your could do is clone the repository and see if you are able to deploy the example app to shinyapps.io. If it works simply replace the code in the App file with your own.

1 Like

Hello @noveld thanks for your reply,
I was not aware of the need to have the Rprofile. I am new to all the interface architecture, virtual environments etc... currently I am using a miniconda setup.

I tried your suggestion and publishing in shinyapps.io that app was no problem. All good! So I created a virtual environment with reticulate, but when then try to publish my app, it seems that it lacks Keras and Tensorflow in that same environmnent. Then, from what I was able to get it seems that, it is not possible to do it in a windows machine (which I have). I got that here:
https://tensorflow.rstudio.com/reference/keras/install_keras/

One step forward, but stucked again...

What are my options now?

Thanks again

If it works locally I don't think you have an issue with your installation of keras or tensorflow.

Now that you set your Rprofile did the error messages disappear? What does the log file show?

Another problem might be that the file path for your model is wrong. Did you put the file in the www folder? Maybe put a text file or image in the same location and see if it loads when you deploy to shinyapps.io

Of course... sorry forgot to post the error.
The error that I am facing right now... When it runs the following line:

reticulate::virtualenv_install(virtualenv_dir, packages = PYTHON_DEPENDENCIES, ignore_installed=TRUE)

The verbose:

Traceback (most recent call last):
  File "C:\Users\PEDRO~1.DAV\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\PEDRO~1.DAV\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\__main__.py", line 23, in <module>
    from pip._internal.cli.main import main as _main  # isort:skip # noqa
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\cli\main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\cli\autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\cli\main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\cli\cmdoptions.py", line 25, in <module>
    from pip._internal.cli.progress_bars import BAR_TYPES
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\cli\progress_bars.py", line 12, in <module>
    from pip._internal.utils.logging import get_indentation
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\utils\logging.py", line 18, in <module>
    from pip._internal.utils.misc import ensure_dir
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\utils\misc.py", line 34, in <module>
    from pip._internal.locations import get_major_minor_version, site_packages, user_site
  File "C:\Users\PEDRO~1.DAV\DOCUME~1\VIRTUA~1\PYTHON~1\lib\site-packages\pip\_internal\locations.py", line 68, in <module>
    if platform.python_implementation().lower() == "pypy":
  File "C:\Users\PEDRO~1.DAV\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\platform.py", line 1267, in python_implementation
    return _sys_version()[0]
  File "C:\Users\PEDRO~1.DAV\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\platform.py", line 1225, in _sys_version
    repr(sys_version))
ValueError: failed to parse CPython sys.version: '3.6.10 |Anaconda, Inc.| (default, May  7 2020, 19:46:08) [MSC v.1916 64 bit (AMD64)]'
Error: Error installing package(s): "numpy"

I am sorry I don't really know what the problem could be. Maybe try to post your error log together with your Rprofile on stackoverflow.com. Maybe you have more luck there finding an answer.

Sure @noveld. Thanks a lot for your help.
although I cannot fully achieve ultimate goal I believe that you really pointed me in the best direction.

It seems that this is another kind of problem I am facing now.

Thanks for your help,
Pedro

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.