-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralMagik.Rmd
137 lines (124 loc) · 4.15 KB
/
NeuralMagik.Rmd
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
---
title: "Neural-Magik!"
author: "F.García Díaz"
date: "1/4/2018"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE , message = FALSE)
```
### Probando Deep Neural-Networks en { width=5% }
Una aplicación para la clasificación de imágenes.
Implementa el modelo [***MobileNet***](https://arxiv.org/abs/1704.04861) en [***Keras***](https://keras.rstudio.com) pre-entrenado con [***ImageNet***](http://www.image-net.org).
#### Instrucciones
1) Cargar Imagen
2) Ver resultado abajo
```{r eruptions, echo=FALSE}
library(miniUI)
library(magick)
library(shiny)
library(keras)
library(DT)
library(text2vec)
library(shinyFiles)
library(XML)
# Cargar metadatos de ImageNet en formato XML (asi estan)
"%+%" <- function(x, y) paste(x, y, sep = "")
metadata <- xmlRoot(xmlParse("data/estructura.xml")) # Cargar metadatos
#vgg16 <- application_vgg16(weights = 'imagenet', include_top = TRUE)
vgg16 <- application_mobilenet(weights = 'imagenet', include_top = TRUE)
shinyApp(
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Elegir Imagen..."),
tags$hr()),
mainPanel(
tags$em("Imagen Cargada"),
imageOutput("imagen"),
uiOutput("resultados")
)
)
),
server <- function(input, output, session) {
output$imagen <- renderImage({
inFile = input$file1
print(inFile)
if (!is.null(inFile))
{
width <- session$clientData$output_imagen_width
height <- session$clientData$output_imagen_height
list(
src = inFile$datapath,
width=width,
height=height
)
}
else
{
width <- session$clientData$output_imagen_width
height <- session$clientData$output_imagen_height
list(
src = "data/noimage.png",
width=width,
height=height
)
}
},
deleteFile = FALSE
)
output$resultados <- renderUI({
# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.
dwidth <- session$clientData$output_imagen_width
dheight <- session$clientData$output_imagen_height
inFile <- input$file1
if (is.null(inFile))
return("No file selected...")
# Procesar
img <- image_load(inFile$datapath, target_size = c(224,224))
x = image_to_array(img)
dim(x) <- c(1, dim(x))
#x = imagenet_preprocess_input(x)
x = mobilenet_preprocess_input(x)
features = vgg16 %>% predict(x)
#imagenet_decode_predictions(features, top = 3)[[1]]
wnid <- mobilenet_decode_predictions(features, top = 3)[[1]]$class_name[1]
# obtener el nodo del WNID identificado en el XML (horrible)
nodo <- getNodeSet(metadata,"//synset[@wnid='" %+% wnid %+% "']")
especie <- xmlParent(nodo[[1]])
especie_lista <-list(xmlGetAttr(especie, "words"))
counter <- 1
# Obtener especie recursivamente //esto es horrible...y bue
while(!is.null(xmlParent(especie))) {
counter <- counter + 1
especie <- xmlParent(especie)
especie_lista[[counter]] <- xmlGetAttr(especie, "words")
}
# Output
tagList(
tags$em("Esto que cargaste se parece a un..."),
tags$blockquote(sapply(nodo, xmlGetAttr,"words")[1]),
tags$b("Definición:"),
tags$em(sapply(nodo, xmlGetAttr, "gloss")[1]),
tags$br(),
tags$br(),
tags$em("Arbol de clasificación de la imagen en IMAGENET"),
DT::datatable(as.data.frame(unlist(head(especie_lista,-1))),
caption = "Especie", rownames = FALSE,
colnames = NULL , options = list(info = FALSE, paging = FALSE, searching = FALSE),
height = dheight , width = dwidth ),
"El ranking de probabilidades de clasificaciones es...",
DT::datatable(as.data.frame(mobilenet_decode_predictions(features, top = 5)[[1]]),
caption = "Ranking", options = list(info = FALSE, paging = FALSE,
searching = FALSE), height = dheight , width = dwidth ) %>%
formatPercentage('score', 2)
)
})
}
)
```