Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions server/deep_learning.R
Original file line number Diff line number Diff line change
Expand Up @@ -870,17 +870,34 @@ deep_learning = function() {

observeEvent(input$start_img_class_inference, {
req(input$infer_img_class_upload, input$infer_img_class_checkpoint_dropdown)
img_class_inference_result(list(status = "Running...", prediction = "Processing...", error = NULL))
img_class_inference_result(list(status = "Running...", prediction = "Processing...", error = NULL, image_url = NULL))
tryCatch({
req <- dl_request("/inference/image-classification") %>%
req_body_multipart(
image = curl::form_file(input$infer_img_class_upload$datapath),
model_checkpoint = input$infer_img_class_checkpoint_dropdown
)
resp_data <- resp_body_json(req_perform(req))
img_class_inference_result(list(status = "Success", prediction = resp_data$prediction, error = NULL))
if (isTRUE(input$infer_img_class_explain)) {
# Grad-CAM: returns prediction, confidence, and a heatmap URL.
req <- dl_request("/explain/image-classification") %>%
req_body_multipart(
image = curl::form_file(input$infer_img_class_upload$datapath),
model_checkpoint = input$infer_img_class_checkpoint_dropdown
)
resp_data <- resp_body_json(req_perform(req))
pred_text <- resp_data$prediction
if (!is.null(resp_data$confidence)) {
pred_text <- sprintf("%s (confidence: %.1f%%)", pred_text, 100 * as.numeric(resp_data$confidence))
}
img_class_inference_result(list(status = "Success", prediction = pred_text,
error = NULL, image_url = resp_data$output_url))
} else {
req <- dl_request("/inference/image-classification") %>%
req_body_multipart(
image = curl::form_file(input$infer_img_class_upload$datapath),
model_checkpoint = input$infer_img_class_checkpoint_dropdown
)
resp_data <- resp_body_json(req_perform(req))
img_class_inference_result(list(status = "Success", prediction = resp_data$prediction,
error = NULL, image_url = NULL))
}
}, error = function(e) {
img_class_inference_result(list(status = "Error", prediction = NULL, error = as.character(e)))
img_class_inference_result(list(status = "Error", prediction = NULL, error = as.character(e), image_url = NULL))
})
})

Expand Down Expand Up @@ -930,7 +947,16 @@ deep_learning = function() {
output$img_class_prediction_output <- renderText({
img_class_inference_result()$prediction
})

# Grad-CAM heatmap overlay (only populated when "Explain" was requested).
output$img_class_explain_output <- renderImage({
res <- img_class_inference_result()
req(res$status == "Success", res$image_url)
image_url <- paste0(api_url, res$image_url)
temp_file <- tempfile(fileext = ".png")
download.file(image_url, temp_file, mode = "wb")
list(src = temp_file, contentType = 'image/png', alt = "Grad-CAM heatmap")
}, deleteFile = TRUE)

output$seg_inference_status_ui <- renderUI({
res <- seg_inference_result()
if (res$status == "Running...") {
Expand Down
7 changes: 7 additions & 0 deletions ui/deeplearning_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ deeplearning_ui = function() {
textInput("infer_img_class_run_name", "Enter Run Name to Find Checkpoints", ""),
selectInput("infer_img_class_checkpoint_dropdown", "Select Checkpoint", choices = NULL),
fileInput("infer_img_class_upload", "Upload Image for Classification"),
checkboxInput("infer_img_class_explain", "Explain prediction (Grad-CAM heatmap)", value = FALSE),
actionButton("start_img_class_inference", "Run Inference", class = "btn-info", style="margin-top: 10px;")
),
hr(),
Expand All @@ -367,6 +368,12 @@ deeplearning_ui = function() {
div(
style = "background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 5px; padding: 15px; margin-top: 5px; min-height: 50px; font-size: 1.1em;",
textOutput("img_class_prediction_output")
),
conditionalPanel(
condition = "input.infer_img_class_explain == true",
h5("Why? Grad-CAM heatmap", style = "margin-top: 15px;"),
helpText("Warmer regions contributed more to the predicted class."),
imageOutput("img_class_explain_output", height = "auto")
)
),

Expand Down