Skip to content

Instantly share code, notes, and snippets.

@Orbifold
Created Nov 6, 2016
Embed
What would you like to do?
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