Created
November 6, 2016 10:32
-
-
Save Orbifold/2b80796427e7731e6ba46df622f8a838 to your computer and use it in GitHub Desktop.
Image recognition with MxNet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(devtools) | |
install_version("imager", version = "0.20", repos = "http://cran.us.r-project.org") | |
library("mxnet") | |
library(imager) | |
# can download the model from http://www.orbifold.net/default/R/MxNet/MxNetInception.zip | |
model = mx.model.load("Inception_BN", iteration=39) | |
mean.img = as.array(mx.nd.load("mean_224.nd")[["mean_img"]]) | |
synsets <<- readLines("synset.txt") | |
im <- load.image("Parrots.jpg") | |
plot(im) | |
preproc.image <- function(im, mean.image) { | |
shape <- dim(im) | |
short.edge <- min(shape[1:2]) | |
xx <- floor((shape[1] - short.edge) / 2) | |
yy <- floor((shape[2] - short.edge) / 2) | |
croped <- crop.borders(im, xx, yy) | |
resized <- resize(croped, 224, 224) | |
arr <- as.array(resized) * 255 | |
dim(arr) <- c(224, 224, 3) | |
normed <- arr - mean.img | |
dim(normed) <- c(224, 224, 3, 1) | |
return(normed) | |
} | |
normed <- preproc.image(im, mean.img) | |
prob <- predict(model, X=normed) | |
max.idx <- order(prob[,1], decreasing = TRUE)[1:5] | |
synsets[max.idx] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment