Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R Shiny does not open a model training in Python #4055

Open
Leprechault opened this issue May 19, 2024 · 1 comment
Open

R Shiny does not open a model training in Python #4055

Leprechault opened this issue May 19, 2024 · 1 comment

Comments

@Leprechault
Copy link

I want to create a Shiny app using R and Python cause the Yolov8 model was developed in Python. But, I try to use my app calling a *py code (yolov8_loader.py) in my app directory and it doesn't work, without any error.

In my example:

library(shiny)
library(shinydashboard)
library(rsconnect)
library(tidyverse)
library(reticulate)
library(purrr)
library(stringr)

# Create a py_env environment and install: pip install ultralytics
setwd('C:/Users/IFMT/anaconda3/envs/py_env')
renv::init()
Sys.setenv(RENV_PATHS_CACHE = 'C:/Users/IFMT/anaconda3/envs/py_env')
renv::use_python(type = 'conda', name = 'py_env')
# 

#Create a new Python file, e.g., yolov8_loader.py, with the following content inside the py_env environment:
# import ultralytics as yolo
#
#   def load_model():
#   model = yolo.YOLO("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt")
# return model

# Load the Python module
yolov8_loader <- source_python("yolov8_loader.py")

# Import the load_model function
load_model <- yolov8_loader$load_model

# Load the model
model <- load_model()

# Define the UI
ui <- fluidPage(
  # App title ----
  titlePanel("Hello YOLOv8!"),
  # 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) {
  image <- reactive({
    req(input$image_path)
    jpeg::readJPEG(input$image_path$datapath)
  })
  
  output$prediction <- renderText({
    img <- image() %>% 
      array_reshape(., dim = c(1, dim(.), 1))
    
    # Use the loaded model to make predictions
    prediction <- model(img)
    paste0("The predicted class is ", prediction)
  })
  
  output$image <- renderPlot({
    plot(as.raster(image()))
  })
}

shinyApp(ui, server)

But the yolov8_loader object is always:


NULL

Please, any help with this bug?

Alexandre

@gadenbuie
Copy link
Member

Hi @Leprechault, does this part of your code work on its own without Shiny?

library(reticulate)

# Create a py_env environment and install: pip install ultralytics
setwd('C:/Users/IFMT/anaconda3/envs/py_env')
renv::init()
Sys.setenv(RENV_PATHS_CACHE = 'C:/Users/IFMT/anaconda3/envs/py_env')
renv::use_python(type = 'conda', name = 'py_env')
# 

#Create a new Python file, e.g., yolov8_loader.py, with the following content inside the py_env environment:
# import ultralytics as yolo
#
#   def load_model():
#   model = yolo.YOLO("https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt")
# return model

# Load the Python module
yolov8_loader <- source_python("yolov8_loader.py")

# Import the load_model function
load_model <- yolov8_loader$load_model

# Load the model
load_model()

I suspect that the errors you're seeing are related to reticulate and the yolo model. If that part of the code works, but starts to fail when you involve Shiny, then we can help. Otherwise, I'd recommend you ask for help on https://forum.posit.co where others who have more experience with reticulate might be able to help you.

Another small note that Shiny for Python might be a great fit for this app, especially if you're using a Python-specific model. As a long-time Shiny for R users, I found it surprisingly easy to start using Shiny for Python and you might have a similar experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants