部署 Shiny App (shinyapps.-io) 加载 Keras 模型

Deploying Shiny App (shinyapps.-io) loading Keras model

我一直在研究 CNN,在 R (RstudiO) 中使用 Keras/tensorflow,并且能够构建一些分类模型(使用 dog/cat 数据集)。然后我觉得有必要尝试为它创建一个前端,并决定尝试一下 Shiny。

幸运的是,我能够通过阅读一些相关内容来创建一个可用的本地 运行 应用程序(确实非常简单!)。自然而然的下一步是……在 shinyapps.io 中发布它。这就是问题出现的地方。下面的代码在本地完美运行,但是当我发布 shinyapps.io 时,部署会产生以下消息(在代码之后)。 看起来,阅读错误消息,它与Keras和Tensorflow有关,但我不知道它是什么。

简单的说app就是:加载图片>>加载预先保存的keras模型>>用模型猜猫狗

谢谢!

# Attach Packages
require(tensorflow)
library(shiny)
library(keras)
library(tidyverse)


# Load the model
model <- load_model_hdf5("dog_cat_class_model.h5")

# Define the UI
ui <- fluidPage(
    # App title ----
    titlePanel("Hello dear user"),
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
        # Sidebar panel for inputs ----
        sidebarPanel(
            # Input: File upload
            fileInput("image_path", label = "Input a JPEG image")
        ),
        # Main panel for displaying outputs ----
        mainPanel(
            # Output: Histogram ----
            textOutput(outputId = "prediction"),
            plotOutput(outputId = "image")
        )
    )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {

  # ------------------ App virtualenv setup (Do not edit) ------------------- #

    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)

网页链接错误信息:

Loading required package: tensorflow
Error: package or namespace load failed for ‘tensorflow’:
 .onLoad failed in loadNamespace() for 'tensorflow', details:
  call: py_module_import(module, convert = convert)
  error: ImportError: No module named tools

Detailed traceback: 
  File "/opt/R/4.0.3/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
    level=level
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "/opt/R/4.0.3/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
    level=level
  File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/__init__.py", line 40, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/opt/R/4.0.3/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
    level=level

Error in value[[3L]](cond) : 
  package or namespace load failed for ‘keras’:
 .onLoad failed in loadNamespace() for 'tensorflow', details:
  call: py_module_import(module, convert = convert)
  error: ImportError: cannot import name _argument_parser

Detailed traceback: 
  File "/opt/R/4.0.3/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
    level=level
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 101, in <module>
    from tensorflow_core import *
  File "/opt/R/4.0.3/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
    level=level
  File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/__init__.py", line 42, in <module>
    from . _api.v2 import audio
  File "/opt/R/4.0.3/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
    level=level
  File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/_api/v2/audio/__init__.py", line 10, in <module>
    from tensorflow.python.ops.
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted

实际上我上周才处理这个问题!

为了使 tensorflow/keras 环境正常工作,我将以下代码添加到我的应用程序中。

reticulate::virtualenv_create(envname = 'r-tensorflow', python = 'python3')
virtualenv_install('r-tensorflow', c('numpy', 'tensorflow', 'keras'), ignore_installed = FALSE)
reticulate::use_virtualenv(virtualenv = 'r-tensorflow', required = TRUE)

在本地,我将 RETICULATE_PYTHON 定义为

Sys.setenv(RETICULATE_PYTHON = "/home/max/.virtualenvs/r-tensorflow/bin/python")

当我 运行 reticulate::py_config 时,我看到以下输出。

python:         /home/max/.virtualenvs/r-tensorflow/bin/python
libpython:      /usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so
pythonhome:     /home/max/.virtualenvs/r-tensorflow:/home/max/.virtualenvs/r-tensorflow
version:        3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
numpy:          /home/max/.virtualenvs/r-tensorflow/lib/python3.8/site-packages/numpy
numpy_version:  1.18.5
tensorflow:     /home/max/.virtualenvs/r-tensorflow/lib/python3.8/site-packages/tensorflow

NOTE: Python version was forced by RETICULATE_PYTHON