-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
148 lines (105 loc) · 4.51 KB
/
README.Rmd
File metadata and controls
148 lines (105 loc) · 4.51 KB
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
---
title: "Classification Object"
subtitle: "Using mxnet - Apache Incubating"
author: "Delermando Branquinho Filho"
date: "31 de janeiro de 2019"
output:
md_document:
variant: markdown_github
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## MXNET
Apache MXNet is an effort undergoing incubation at The Apache Software Foundation (ASF), sponsored by the Apache Incubator. Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects. While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.
[source](https://mxnet.incubator.apache.org/versions/master/)
### Classify Images with a PreTrained Model
MXNet is a flexible and efficient deep learning framework. One of the interesting things that a deep learning algorithm can do is classify real world images.
In this tutorial, we show how to use a pre-trained Inception-BatchNorm network to predict the class of an image. For information about the network architecture.
The pre-trained Inception-BatchNorm network is able to be downloaded from this link This model gives the recent state-of-art prediction accuracy on image net dataset.
```{r load_mxnet}
require(mxnet)
```
### Including Plots
Now load the imager package to load and preprocess the images in R:
```{r load_imager}
require(imager)
```
Make sure you unzip the pre-trained model in the current folder. Use the model loading function to load the model into R:
```{r model}
model = mx.model.load("Inception/Inception_BN", iteration=39)
```
Load in the mean image, which is used for preprocessing using:
```{r mean_img}
mean.img = as.array(mx.nd.load("Inception/mean_224.nd")[["mean_img"]])
```
### Load and Preprocess the Image
Now, we are ready to classify a real image. In this example, we simply take the parrots image from the imager package. You can use another image, if you prefer.
Load and plot the image:
```{r load_image}
im <- load.image("delermando4.jpg")
plot(im)
```
Before feeding the image to the deep network, we need to perform some preprocessing to make the image meet the deep network input requirements. Preprocessing includes cropping and subtracting the mean. Because MXNet is deeply integrated with R, we can do all the processing in an R function:
```{r preproc_function}
preproc.image <- function(im, mean.image) {
# crop the image
shape <- dim(im)
short.edge <- min(shape[1:2])
xx <- floor((shape[1] - short.edge) / 2)
yy <- floor((shape[2] - short.edge) / 2)
cropped <- crop.borders(im, xx, yy)
# resize to 224 x 224, needed by input of the model.
resized <- resize(cropped, 224, 224)
# convert to array (x, y, channel)
arr <- as.array(resized) * 255
dim(arr) <- c(224, 224, 3)
# subtract the mean
normed <- arr - mean.img
# Reshape to format needed by mxnet (width, height, channel, num)
dim(normed) <- c(224, 224, 3, 1)
return(normed)
}
```
Use the defined preprocessing function to get the normalized image:
```{r normed}
normed <- preproc.image(im, mean.img)
```
### Classify the Image
Now we are ready to classify the image! Use the predict function to get the probability over classes:
```{r prob}
prob <- predict(model, X=normed)
```
Use the max.col on the transpose of prob to get the class index:
```{r max_idx}
max.idx <- max.col(t(prob))
```
The index doesn't make much sense, so let's see what it really means. Read the names of the classes from the following file:
```{r synsets}
synsets <- readLines("Inception/synset.txt")
```
Let's see what the image really is:
```{r print_class}
print(paste("Predicted Top-class:", synsets[[max.idx]]))
```
### Trying again with others pictures - Coffee mug
```{r coffee}
im <- load.image("caneca.jpg")
plot(im)
normed <- preproc.image(im, mean.img)
prob <- predict(model, X=normed)
max.idx <- max.col(t(prob))
synsets <- readLines("Inception/synset.txt")
print(paste0("Predicted Top-class: ", synsets [[max.idx]]))
```
### Trying again with others pictures - Book
```{r error}
im <- load.image("chave.jpg")
normed <- preproc.image(im, mean.img)
plot(im)
prob <- predict(model, X=normed)
max.idx <- max.col(t(prob))
synsets <- readLines("Inception/synset.txt")
print(paste0("Predicted Top-class: ", synsets [[max.idx]]))
```
Not so good!