Shiny app with a TensorFlow model: Argument 1 is not a vector

Hi there,

I have asked a similar post about this - which is still pending, however - I have fixed the Tensorflow errors which were being thrown before, using the below link:

Tensorflow related error when deploying shiny app on shinyapps.io - shiny / shinyapps.io - RStudio Community

This is the code in my app.R file


library(shiny)
library(tensorflow)
library(keras)
library(shinydashboard)
library(rsconnect)
library(tidyverse)
library (png)

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)
}

model <- load_model_tf("www/bird_mod/")
load("www/label_list.RData")
target_size <- c(224,224,3)
options(scipen=999)


ui <- fluidPage(
  dashboardPage(
  skin="blue",
  
  #(1) Header
  

  
  dashboardHeader(title=tags$h1("cagedbirdID",style="font-size: 120%; font-weight: bold; color: white"),
                  titleWidth = 350,
                  tags$li(class = "dropdown"),
                  dropdownMenu(type = "notifications", icon = icon("question-circle", "fa-1x"), badgeStatus = NULL,
                               headerText="",
                               tags$li(a(href = "https://twitter.com/sicilyfiennes",
                                         target = "_blank",
                                         tagAppendAttributes(icon("icon-circle"), class = "info"),
                                         "Created by Sicily Fiennes"))
                  )),
  
  
  
  #(2) Sidebar
  
  dashboardSidebar(
    width=350,
    fileInput("input_image","File" ,accept = c('.jpg','.jpeg')), #,'.png') 
    tags$br(),
    tags$p("Upload the image here.")
  ),
  
  
  #(3) Body
  
  dashboardBody(
    
    h4("Instruction:"),
    tags$br(),tags$p("1. Take a picture of a bird."),
    tags$p("2. Crop image so that bird fills out most of the image."),
    tags$p("3. Upload image with menu on the left."),
    tags$br(),
    
    fluidRow(
      column(h4("Image:"),imageOutput("output_image"), width=6),
      column(h4("Prediction:"),tags$br(),textOutput("warntext",), tags$br(),
             tags$p("This bird is most likely a:"),tableOutput("text"),width=6)
    ),tags$br()
    
  )))


server <- function(input, output) {
  
  image <- reactive({image_load(input$input_image$datapath, target_size = target_size[1:2])})
  
  
  prediction <- reactive({
    if(is.null(input$input_image)){return(NULL)}
    x <- image_to_array(image())
    x <- array_reshape(x, c(1, dim(x)))
    x <- x/255
    pred <- model %>% predict(x)
    pred <- data.frame("Bird" = label_list, "Confidence" = t(pred))
    pred <- pred[order(pred$Prediction, decreasing=T),][1:5,]
    pred$Prediction <- sprintf("%.2f %%", 100*pred$Prediction)
    pred
  })
  
  output$text <- renderTable({
    prediction()
  })
  
  output$warntext <- renderText({
    req(input$input_image)
    
    if(as.numeric(substr(prediction()[1,2],1,4)) >= 30){return(NULL)}
    warntext <- "Warning: I am not sure about this bird!"
    warntext
  })
  
  
  output$output_image <- renderImage({
    req(input$input_image)
    
    outfile <- input$input_image$datapath
    contentType <- input$input_image$type
    list(src = outfile,
         contentType=contentType,
         width = 400)
  }, deleteFile = TRUE)
  
}


shinyApp(ui, server)

I think this has something to do with the server section of my code because I can load Tensorflow fine now, after making the suggested edits to the .Rprofile file. There are also no absolute paths in the code, it is all relative paths. The app also works perfectly fine locally. The code was inspired from this post How to build your own image recognition app with R! [Part 2] – For-loops and piep kicks (wordpress.com)

These were the logs for my Shiny app:

2021-09-08T12:35:13.508161+00:00 shinyapps[4599106]: Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
2021-09-08T12:35:13.626892+00:00 shinyapps[4599106]: Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
2021-09-08T12:35:13.623194+00:00 shinyapps[4599106]: Collecting pyasn1<0.5.0,>=0.4.6
2021-09-08T12:35:13.669680+00:00 shinyapps[4599106]: Collecting oauthlib>=3.0.0
2021-09-08T12:35:13.977908+00:00 shinyapps[4599106]: Building wheel for termcolor (setup.py): finished with status 'done'
2021-09-08T12:35:13.761909+00:00 shinyapps[4599106]: Building wheels for collected packages: termcolor, wrapt
2021-09-08T12:35:13.762447+00:00 shinyapps[4599106]: Building wheel for termcolor (setup.py): started
2021-09-08T12:35:13.978460+00:00 shinyapps[4599106]: Created wheel for termcolor: filename=termcolor-1.1.0-py3-none-any.whl size=4847 sha256=ecf6bc899ff05a3e01b94365bd00682f5346051000ba9397bee95b001810f35e
2021-09-08T12:35:13.981102+00:00 shinyapps[4599106]: Building wheel for wrapt (setup.py): started
2021-09-08T12:35:13.978494+00:00 shinyapps[4599106]: Stored in directory: /home/shiny/.cache/pip/wheels/a0/16/9c/5473df82468f958445479c59e784896fa24f4a5fc024b0f501
2021-09-08T12:35:15.295751+00:00 shinyapps[4599106]: Building wheel for wrapt (setup.py): finished with status 'done'
2021-09-08T12:35:15.296506+00:00 shinyapps[4599106]: Created wheel for wrapt: filename=wrapt-1.12.1-cp38-cp38-linux_x86_64.whl size=78575 sha256=e75c03413c5fb5382cd09a92e2b518a1b8bb4d595ffdf9e8368d679dd54047f4
2021-09-08T12:35:15.298620+00:00 shinyapps[4599106]: Successfully built termcolor wrapt
2021-09-08T12:35:15.444900+00:00 shinyapps[4599106]: Installing collected packages: urllib3, pyasn1, idna, charset-normalizer, certifi, six, rsa, requests, pyasn1-modules, oauthlib, cachetools, requests-oauthlib, google-auth, werkzeug, tensorboard-plugin-wit, tensorboard-data-server, protobuf, numpy, markdown, grpcio, google-auth-oauthlib, absl-py, wrapt, typing-extensions, termcolor, tensorflow-estimator, tensorboard, opt-einsum, keras-preprocessing, h5py, google-pasta, gast, flatbuffers, astunparse, tensorflow-hub, tensorflow, pyyaml, Pillow, keras
2021-09-08T12:35:15.296565+00:00 shinyapps[4599106]: Stored in directory: /home/shiny/.cache/pip/wheels/5f/fd/9e/b6cf5890494cb8ef0b5eaff72e5d55a70fb56316007d6dfe73
2021-09-08T12:35:16.656624+00:00 shinyapps[4599106]: Attempting uninstall: numpy
2021-09-08T12:35:16.656877+00:00 shinyapps[4599106]: Found existing installation: numpy 1.21.2
2021-09-08T12:35:17.384876+00:00 shinyapps[4599106]: Uninstalling numpy-1.21.2:
2021-09-08T12:35:17.399381+00:00 shinyapps[4599106]: Successfully uninstalled numpy-1.21.2
2021-09-08T12:35:36.847074+00:00 shinyapps[4599106]: Successfully installed Pillow-8.3.2 absl-py-0.13.0 astunparse-1.6.3 cachetools-4.2.2 certifi-2021.5.30 charset-normalizer-2.0.4 flatbuffers-1.12 gast-0.3.3 google-auth-1.35.0 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.32.0 h5py-2.10.0 idna-3.2 keras-2.6.0 keras-preprocessing-1.1.2 markdown-3.3.4 numpy-1.19.5 oauthlib-3.1.1 opt-einsum-3.3.0 protobuf-3.17.3 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyyaml-5.4.1 requests-2.26.0 requests-oauthlib-1.3.0 rsa-4.7.2 six-1.15.0 tensorboard-2.6.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.0 tensorflow-2.4.0 tensorflow-estimator-2.4.0 tensorflow-hub-0.12.0 termcolor-1.1.0 typing-extensions-3.7.4.3 urllib3-1.26.6 werkzeug-2.0.1 wrapt-1.12.1
2021-09-08T12:35:37.130182+00:00 shinyapps[4599106]:
2021-09-08T12:35:37.130184+00:00 shinyapps[4599106]: Installation complete.
2021-09-08T12:35:37.130185+00:00 shinyapps[4599106]:
2021-09-08T12:35:37.292705+00:00 shinyapps[4599106]: 2021-09-08 12:35:37.292441: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/shiny/.virtualenvs/myenv/lib:/opt/R/4.0.4/lib/R/lib:/usr/local/lib:/usr/lib/x86_64-linux-gnu:/usr/lib/jvm/java-11-openjdk-amd64/lib/server
2021-09-08T12:35:37.292807+00:00 shinyapps[4599106]: 2021-09-08 12:35:37.292496: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2021-09-08T12:35:39.705337+00:00 shinyapps[4599106]: 2021-09-08 12:35:39.705235: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-09-08T12:35:39.705814+00:00 shinyapps[4599106]: 2021-09-08 12:35:39.705776: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-09-08T12:35:39.705801+00:00 shinyapps[4599106]: 2021-09-08 12:35:39.705736: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/shiny/.virtualenvs/myenv/lib:/opt/R/4.0.4/lib/R/lib:/usr/local/lib:/usr/lib/x86_64-linux-gnu:/usr/lib/jvm/java-11-openjdk-amd64/lib/server
2021-09-08T12:35:39.705870+00:00 shinyapps[4599106]: 2021-09-08 12:35:39.705823: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (79b940c5198d): /proc/driver/nvidia/version does not exist
2021-09-08T12:35:39.706108+00:00 shinyapps[4599106]: 2021-09-08 12:35:39.706053: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX512F
2021-09-08T12:35:39.706109+00:00 shinyapps[4599106]: To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-09-08T12:35:39.707015+00:00 shinyapps[4599106]: 2021-09-08 12:35:39.706958: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-09-08T12:35:58.086329+00:00 shinyapps[4599106]:
2021-09-08T12:35:58.086333+00:00 shinyapps[4599106]: Listening on http://127.0.0.1:40632
2021-09-08T12:36:10.561450+00:00 shinyapps[4599106]: 2021-09-08 12:36:10.561385: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 3000000000 Hz
2021-09-08T12:36:10.561023+00:00 shinyapps[4599106]: 2021-09-08 12:36:10.560913: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
2021-09-08T12:36:12.719962+00:00 shinyapps[4599106]: Warning: Error in order: Argument 1 is not a vector
2021-09-08T12:36:12.726522+00:00 shinyapps[4599106]: 128: order
2021-09-08T12:36:12.726525+00:00 shinyapps[4599106]: 125: reactive:prediction [/srv/connect/apps/birdapp/app.R#156]
2021-09-08T12:36:12.726526+00:00 shinyapps[4599106]: 109: prediction
2021-09-08T12:36:12.726528+00:00 shinyapps[4599106]: 94: renderFunc
2021-09-08T12:36:12.726528+00:00 shinyapps[4599106]: 93: output$text
2021-09-08T12:36:12.726530+00:00 shinyapps[4599106]: 6: eval
2021-09-08T12:36:12.726529+00:00 shinyapps[4599106]: 12: fn
2021-09-08T12:36:12.726529+00:00 shinyapps[4599106]: 7: connect$retry
2021-09-08T12:36:12.726526+00:00 shinyapps[4599106]: 108: renderTable [/srv/connect/apps/birdapp/app.R#162]
2021-09-08T12:36:12.732958+00:00 shinyapps[4599106]: 112:
2021-09-08T12:36:12.726527+00:00 shinyapps[4599106]: 107: func
2021-09-08T12:36:12.726528+00:00 shinyapps[4599106]: 13: runApp
2021-09-08T12:36:12.729641+00:00 shinyapps[4599106]: Warning: Error in order: Argument 1 is not a vector
2021-09-08T12:36:12.726540+00:00 shinyapps[4599106]: 5: eval

My best guess is that you have path error issues on the server. It looks like you can't load dependencies because the folder path 'LD_LIBRARY_PATH' is incorrect.

Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH:

and

Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH:

You could try reinstalling:
libcuda

Might help; might not.

Thank you for your response? I have the GPU version of Tensorflow set up on my computer, but the version here is just looking to the CPU one. Does anyone know how to specify your CUDA installation on the Shiny server?

Any advice would be greatly appreciated!

You are trying to deploy to shinyapps.io, so the app has to be executable with the resources available at their servers and as far as I know they do not have the option to use GPU accelerated workers so it is not currently possible to use the GPU version.

thank you for your comment!! Do you think this would be a problem if my model was trained using the GPU version? As you can see from the logs, it still accessed the CPU version of TensorFlow - but I'm still not certain what the Argument 1 is not a vector refers to?