-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrMxNet.R
More file actions
53 lines (44 loc) · 1.58 KB
/
rMxNet.R
File metadata and controls
53 lines (44 loc) · 1.58 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
require(mxnet)
require(imager)
im <- load.image("delermando4.jpg")
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)
}
model = mx.model.load("Inception/Inception_BN", iteration=39)
mean.img = as.array(mx.nd.load("Inception/mean_224.nd")[["mean_img"]])
normed <- preproc.image(im, mean.img)
prob <- predict(model, X=im)
dim(prob)
max.idx <- max.col(t(prob))
max.idx
synsets <- readLines("Inception/synset.txt")
print(paste0("Predicted Top-class: ", synsets [[max.idx]]))
im <- load.image("caneca.jpg")
normed <- preproc.image(im, mean.img)
prob <- predict(model, X=normed)
dim(prob)
max.idx <- max.col(t(prob))
max.idx
synsets <- readLines("Inception/synset.txt")
print(paste0("Predicted Top-class: ", synsets [[max.idx]]))
im <- load.image("chave.jpg")
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]]))