使用网状、python 和 keras 部署闪亮的应用程序时出错

时间:2020-12-29 10:48:42

标签: r tensorflow keras shinyapps reticulate

我正在尝试部署使用 reticulate 和 keras 包的闪亮应用程序。我在本地运行它没有任何问题,但是当我尝试将它部署到 Shinyapps.io 时出现了真正的麻烦。我的app.r文件如下:

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)

我的 .Rprofile 文件如下(归功于 this source):

VIRTUALENV_NAME = "virt_tf"

if (Sys.info()[["user"]] == "shiny"){
  
  # Running on shinyapps.io
  Sys.setenv(PYTHON_PATH = 'python3')
  Sys.setenv(VIRTUALENV_NAME = VIRTUALENV_NAME) # Installs into default shiny virtualenvs dir
  Sys.setenv(RETICULATE_PYTHON = paste0('/home/shiny/.virtualenvs/', VIRTUALENV_NAME, '/bin/python'))
  
} else if (Sys.info()[["user"]] == "rstudio-connect"){
  
  # Running on remote server
  Sys.setenv(PYTHON_PATH = '/opt/python/3.7.6/bin/python')
  Sys.setenv(VIRTUALENV_NAME = paste0(VIRTUALENV_NAME, '/')) # include '/' => installs into rstudio-connect/apps/
  Sys.setenv(RETICULATE_PYTHON = paste0(VIRTUALENV_NAME, '/bin/python'))
  
} else {
  
  # Running locally
  options(shiny.port = 7450)
  Sys.setenv(PYTHON_PATH = 'python 3.6.12')
  Sys.setenv(VIRTUALENV_NAME = VIRTUALENV_NAME) # exclude '/' => installs into ~/.virtualenvs/
  # RETICULATE_PYTHON is not required locally, RStudio infers it based on the ~/.virtualenvs path
}

部署过程似乎完全按照R日志运行:

rsconnect::deployApp()
Preparing to deploy application...Update application currently deployed at
https://name.shinyapps.io/appname/? [Y/n] y
DONE
Uploading bundle for application: 3428026...DONE
Deploying bundle: 4035381 for application: 3428026 ...
Waiting for task: 846214175
  building: Parsing manifest
  building: Building image: 4594673
  building: Installing system dependencies
  building: Fetching packages
  building: Installing packages
  building: Installing files
  building: Pushing image: 4594673
  deploying: Starting instances
  terminating: Stopping old instances
Application successfully deployed to https://name.shinyapps.io/appname/

我从日志底部得到的错误:

Error in value[[3L]](cond) : 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 -> <Anonymous>
Execution halted

当我尝试将 tensorflow 包含到需要安装到我的虚拟环境中的软件包列表中时,我收到以下错误消息:

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

据我所知,shinyapps.io 促使我将 tensorflow 包安装到虚拟环境中。但是,我想,它应该在可用软件包列表中。但是如何强制使用呢?

0 个答案:

没有答案