-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
357 lines (314 loc) · 12.9 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
####################################
# Data Professor #
# http://youtube.com/dataprofessor #
# http://github.com/dataprofessor #
####################################
# Modified from Winston Chang,
# https://shiny.rstudio.com/gallery/shiny-theme-selector.html
# Concepts about Reactive programming used by Shiny,
# https://shiny.rstudio.com/articles/reactivity-overview.html
# Load required R packages
library(shiny)
library(shinythemes)
library(shinycssloaders)
library(tidyverse)
library(keras)
library(recipes)
library(tidyclust)
# lapply(c("shiny", "shinythemes","shinycssloaders", "tidyverse", "keras", "recipes","tidyclust"), require, character.only = TRUE)
# Load model from keras
myKerasModel <- application_vgg16(weights="imagenet",include_top=TRUE)
# Change Output to second last layer to access the feature map instead of classification result.
output <- myKerasModel$layers[[length(myKerasModel$layers)-1]]$output
myKerasModel <- keras_model(inputs=myKerasModel$input, outputs=output)
# Define UI
ui <- fluidPage(theme = shinytheme("united"),
tags$head(
tags$style(HTML(
".footer {
position:relative;
bottom:0;
font-family: Ubuntu,Tahoma,Helvetica Neue,Helvetica,Arial,sans-serif;
width: 100%;
text-align: center;
height: 60px;
background-color: #e95420;
color: white;
justify-content: center;
}
.divmargin {
margin-right: 10px;
display: flex;
justify-content: center;
}"))),
navbarPage(
# theme = "cerulean", # <--- To use a theme, uncomment this
"Image Clustering",
id = "navbar",
sidebarPanel(
conditionalPanel(
condition = "input.navbar != 'userguide'",
selectInput("var_model","Load Images by Cluster",
choices = c("Flowers","Weapons","Flowers_predict","Weapons_predict"),
selected = 1),
#checkboxInput("var_clusRes", label="Prediction Cluster", value=FALSE),
actionButton("var_clusRes", label = "Load Model"),
htmlOutput("usedModel") |> withSpinner(color="#0dc5c1"),
)
),
tabPanel("Cluster New Image", value = "image_cluster",
mainPanel(
h1("Which cluster does your Image belong to?"),
h3("Upload an Image"),
fileInput("file1","",
multiple=FALSE,
accept=c(".jpg",".png")),
h3("Your Image"),
imageOutput("myImage") |> withSpinner(color="#0dc5c1"),
h3("Images from the Cluster"),
verbatimTextOutput("img_array"),
verbatimTextOutput("img_cluster"),
uiOutput("format_images_cluster")
) # mainPanel
), # Navbar 1, tabPanel
tabPanel("Show Clusters", value = "show_cluster",
mainPanel(
class = "myMainPanel",
h1("Examine and label Clusters"),
h3("Amount of Image by Cluster"),
plotOutput(outputId = "ggplot_cluster") |> withSpinner(color="#0dc5c1"),
h3("Display Images from Cluster"),
# fluidRow(
# column(width=4,imageOutput("myImage")),
# column(width=4,imageOutput("myImage2"))
# ),
h4("Choose a cluster for sample images"),
fluidRow(
column(width=6,selectInput("var_clus",
"Choose a cluster:",
choices = NULL,
selected = NULL)),
column(width=6, tableOutput("futureData"))
),
fluidRow(
column(width=6,textInput("label_cluster", "Label the Cluster")),
column(width=4,actionButton("store_label", label = "Write to DB"))
),
verbatimTextOutput("savedLabel"),
h4("Filenames and Images"),
verbatimTextOutput("filenames"),
uiOutput("format_images") |> withSpinner(color="#0dc5c1"),
) # mainPanel
),
tabPanel("User Guide", value ="userguide",
br(),br(),
uiOutput("ref"),
),
), # navbarPage
tags$footer(tags$div(tags$p("Visit the",style = "margin-right: 13px;"),tags$a(href="https://github.com/TheArmbreaker/clustering-with-keras", tags$img(src="https://img.shields.io/badge/Github-Repository-blue"), target="_blank", alt="Github Repository"),tags$p("for more information and references.", style = "margin-left: 13px;"), class="divmargin"),
tags$div(tags$p("R learning project by", style = "margin-right: 13px;"),tags$a(href="https://github.com/TheArmbreaker", tags$img(src="https://img.shields.io/badge/Github-Markus%20Armbrecht-darkgreen"), target="_blank", alt="Github Markus Armbrecht"), class="divmargin"),
class = "footer"),
) # fluidPage
# Define server function
server <- function(input, output, session) {
# Load Data
# react values
reactValues <- reactiveValues(trigger_cluster=NULL,
myRender=NULL,
ldModel="None",
label_cluster_name="none")
# load data from model results
df_clusters <- reactive({
if (input$var_model == "Flowers"){
df_clusters <- read.csv("noRecipe_flowers.csv",header=TRUE)
} else if (input$var_model == "Weapons"){
df_clusters <- read.csv("noRecipe_weapons.csv",header=TRUE)
} else if (input$var_model == "Flowers_predict"){
df_clusters <- read.csv("recipe_flowers.csv",header=TRUE)
} else if (input$var_model == "Weapons_predict") {
df_clusters <- read.csv("recipe_weapons.csv",header=TRUE)
}
})
# extract clusters for selectionInput.
observe({
cluster_vector <- df_clusters() |> select(.cluster) |> arrange(as.numeric(sub(".*_", "", .cluster)))
updateSelectInput(session,"var_clus",choices=cluster_vector,selected=NULL)
})
# Load Images
# extract filenames by cluster
img_data <- function(myFilter) {
df_img_files <- df_clusters() |>
filter(.cluster==myFilter) |>
select(myFiles)
df_img_sample <- sample(df_img_files$myFiles,4)
df_img <- data.frame(id = c(1:4), img_path = df_img_sample)
df_img
}
# observe Event for Show Clusters
# this renders each image in a separate environment
observeEvent(input$var_clus,{
for (i in 5:8){
local({
loc_i <- i
imagename <- paste("img_",i,sep="")
output[[imagename]] <-
renderImage({
list(
src = file.path(str_to_lower(str_split(input$var_model,"_")[[1]][1]),img_data(input$var_clus)[loc_i-4,"img_path"]),
width = "240", height = "180",
alt = "Image failed to render"
)}, deleteFile=FALSE)
})
}
}, ignoreInit = TRUE)
# observe Event for Cluster New Images
# this renders each image in a separate environment
observeEvent(reactValues$trigger,{
for (i in 1:4){
local({
loc_i <- i
imagename <- paste("img_",i,sep="")
output[[imagename]] <-
renderImage({
list(
src = file.path(str_to_lower(str_split(input$var_model,"_")[[1]][1]),img_data(reactValues$trigger)[loc_i,"img_path"]),
width = "240", height = "180",
alt = "Image failed to render"
)}, deleteFile=FALSE)
})
}
}, ignoreInit = TRUE)
# Load Model
# Set string with model path for predict() function
observeEvent(input$var_clusRes,{
model <- str_split(input$var_model,"_")[[1]][1]
#print(paste("test",model[[1]][1]))
if (model == "Flowers"){
print("Flowers")
reactValues$ldModel <- "Flowers"
} else if (model == "Weapons") {
print("Weapons")
reactValues$ldModel <- "Weapons"
}
else {
print("No Model loaded.")
reactValues$ldModel <- "None"
}
})
# load modal based on path argument
predModel <- function(myString){
if (myString == "Flowers"){
myClusterModel <- readRDS("flowers_cluster.rds")
} else if (myString == "Weapons") {
myClusterModel <- readRDS("weapons_cluster.rds")
}
else {
myClusterModel <- NULL
}
}
# Page Show Clusters
# display the filenames of the rendered images
output$filenames <- renderText({
req(input$var_clus)
myString=""
for (i in img_data(input$var_clus)[[2]]){
myString <- paste0(myString,"\n",i)
}
substr(myString,2,nchar(myString))
})
# display count of images in cluster
output$futureData <- renderTable(
{
df_clusters() |> count(.cluster) |>
filter(.cluster==input$var_clus)
}
)
# Output the loaded and rendered images
output$format_images <- renderUI({
myImages <-
lapply(5:8,
function(i){
imagename <- paste("img_",i,sep="")
div(style="display:inline-block", imageOutput(imagename))
})
do.call(tagList,myImages)
})
# display plot with count for cluster results
output$ggplot_cluster <- renderPlot(
{
req(input$var_model)
df_clusters() |>
count(.cluster) |>
ggplot(aes(x=reorder(.cluster,as.numeric(sub(".*_", "", .cluster))),y=n,fill=.cluster)) +
geom_bar(stat="identity") +
theme_minimal()+
scale_fill_viridis_d() +
labs(x="Cluster",y="Count") +
guides(fill=FALSE)
}
)
# Page Cluster New Image
# display the currently loaded prediction model
output$usedModel <- renderUI({
HTML(paste("<br/>","Active Model for prediction:",reactValues$ldModel,sep="<br/>"))
})
# render and output the image that is provided from the user
output$myImage <- renderImage(
{
req(input$file1)
list(src = input$file1$datapath,
alt = "Here should be an image.",
width = 240,
height = 180)
},deleteFile = FALSE
)
# feature extraction with keras and prediction with recipe-workflow
output$img_array <- renderText({
req(input$file1)
if (reactValues$ldModel != "None") {
# loading and preparing image provided by the user
img <- image_load(input$file1$datapath, grayscale=FALSE,target_size = c(224,224))
img_array <- image_to_array(img)
reshaped_image_array <- array_reshape(img_array,c(1,dim(img_array)))
prepro_img <- imagenet_preprocess_input(reshaped_image_array)
# extracting features with keras
features <- myKerasModel |> predict(prepro_img)
# prediction with workflow
res <- predict(predModel(reactValues$ldModel),new_data=as.data.frame(features))
res_char <- as.character(res$".pred_cluster")
reactValues$trigger <- res_char }
else {
"No Model loaded."
}
})
#output$img_cluster <- renderText({
# req(reactValues$trigger)
# reactValues$trigger
#})
# Output the loaded and rendered images
output$format_images_cluster <- renderUI({
myImages <-
lapply(1:4,
function(i){
imagename <- paste("img_",i,sep="")
div(style="display:inline-block", imageOutput(imagename))
})
do.call(tagList,myImages)
})
# name the cluster
observeEvent(input$store_label,{
reactValues$label_cluster_name <- input$label_cluster
output$savedLabel <- renderText({
paste("stored in database:",reactValues$label_cluster_name)
})
})
# Guide Page
getPage <- function(){
return(includeHTML("TecDoc_Shiny.html"))
}
output$ref <- renderUI({
getPage()
})
} # server
# Create Shiny object
shinyApp(ui = ui, server = server)