Skip to content

Instantly share code, notes, and snippets.

@Orbifold
Created November 6, 2016 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Orbifold/2b80796427e7731e6ba46df622f8a838 to your computer and use it in GitHub Desktop.
Save Orbifold/2b80796427e7731e6ba46df622f8a838 to your computer and use it in GitHub Desktop.
Image recognition with MxNet.
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