Tensorflow related error when deploying shiny app on shinyapps.io

I am trying to deploy shiny app, that uses reticulate and keras packages. I do not have any problem to run it locally, but real troubles appear, when I try to deploy it to shinyapps/io. My app.r file is as follows:

virtualenv_dir = Sys.getenv("VIRTUALENV_NAME")
python_path = Sys.getenv("PYTHON_PATH")
reticulate::virtualenv_create(envname = virtualenv_dir, python = python_path)
reticulate::virtualenv_install(virtualenv_dir, packages = c("numpy", "h5py", "scipy", "scikit-image", "pyyaml", "pillow"), ignore_installed = TRUE)
reticulate::use_virtualenv(virtualenv = virtualenv_dir)

library(shiny)
library(keras)
library(reticulate)
library(magick)
library(raster)
library(EBImage)
library(rdrop2)
library(plotly)

np <- import("numpy", convert=FALSE)
ndi <- import("scipy.ndimage", convert=FALSE)
segment <- import("skimage.segmentation", convert=FALSE)
feature <- import("skimage.feature", convert=FALSE)

model = load_model_hdf5("model_v02122020.h5")

ui <- 
tagList(
    fluidPage(
        sidebarLayout(sidebarPanel(
            fileInput("upload", "Choose a file", accept = c('image/png', 'image/jpeg')),
            actionButton('click', 'Start')
        ),
        mainPanel(
            tabsetPanel(type="tabs",
                tabPanel("Input image", plotOutput("InputImagePlot", height="100%")),
                tabPanel("Output image", plotOutput("OutputImagePlot", height="100%")),
            )
        )
        )
    )
)
server <- 
function(input, output, session) {

    observeEvent(input$click, {
## some code for image processing
})
}
shinyApp(ui = ui, server = server)

My .Rprofile file is in accordance with this source: shiny-reticulate-app/.Rprofile at master · ranikay/shiny-reticulate-app · GitHub).

The deployment process seems to run completely according to R log. The error I get from the bottom of the log:

Error in value[[3L]]: Installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
You can install TensorFlow using the install_tensorflow() function.
/home/shiny/.virtualenvs/virt_tf/bin/python3
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted

When I try to include tensorflow into the list of packages required to be installed into my virtualenvironment I get the following error message:

Downloading tensorflow-2.3.1-cp35-cp35m-manylinux2010_x86_64.whl (320.4 MB)
Collecting tensorflow
Killed
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Error in value[[3L]]:
Error installing package(s): 'numpy', 'h5py', 'scipy', 'scikit-image', 'pyyaml', 'pillow', 'tensorflow'
Execution halted
Out of memory!

As far as I understand, shinyapps/io pushes me to install tensorflow package into virtualenvironment. But tensorflow cannot be installed, because it is too heavy. However, I guess, tensorflow should be in the list of available packages, and can be called with library() or request() commands, isn't it so?. But how to force using it?

1 Like

Try the following at the top of your app.R:

library(shiny)
library(tensorflow)
library(keras)

reticulate::virtualenv_create("myenv", python="/usr/bin/python3")
reticulate::use_virtualenv("myenv", required=TRUE)

if (!is_keras_available()) {
  install_keras(method="virtualenv", envname="myenv")
  reticulate::use_virtualenv("myenv", required=TRUE)
  library(keras)
  library(reticulate)
}

and in your .Rprofile that you include with your application bundle:

Sys.setenv(RETICULATE_PYTHON = "/home/shiny/.virtualenvs/myenv/bin/python3")

You will also need to change your application to at least xlarge (available to Basic and higher plans) to have more memory.

@josh, thank you for fast reply. I tried yor suggestion, and I get the same error message, related obviously to memory availability:

Collecting tensorflow==2.2.0
Downloading tensorflow-2.2.0-cp35-cp35m-manylinux2010_x86_64.whl (516.2 MB)
Out of memory!
Killed
Error in value[3L] :
Error installing package(s): 'tensorflow==2.2.0', 'keras', 'tensorflow-hub', 'h5py', 'pyyaml==3.12', 'requests', 'Pillow', 'scipy'
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted

Can you please let me know, where can I read about memory volume allocated for each sinyapps plan? On the website I can only see, that all plans from basic and higher have up to 8 GB of RAM, but I guess I need also more space on server hard drive, do I?

See Configuring your Application in the documentation.

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.