Skip to content

Instantly share code, notes, and snippets.

@mick001
Last active March 13, 2019 08:16
Show Gist options
  • Save mick001/44e86be5e396e4c2bcd312e8947225de to your computer and use it in GitHub Desktop.
Save mick001/44e86be5e396e4c2bcd312e8947225de to your computer and use it in GitHub Desktop.
Image recognition in R using convolutional neural networks with the MXNet package. Part 2. Full article at: https://firsttimeprogrammer.blogspot.com/2016/07/image-recognition-in-r-using.html
rm(list=ls())
# Load MXNet
require(mxnet)
# Train test datasets
train <- read.csv("train_28.csv")
test <- read.csv("test_28.csv")
# Fix train and test datasets
train <- data.matrix(train)
train_x <- t(train[,-1])
train_y <- train[,1]
train_array <- train_x
dim(train_array) <- c(28, 28, 1, ncol(train_x))
test__ <- data.matrix(test)
test_x <- t(test[,-1])
test_y <- test[,1]
test_array <- test_x
dim(test_array) <- c(28, 28, 1, ncol(test_x))
# Model
data <- mx.symbol.Variable('data')
# 1st convolutional layer 5x5 kernel and 20 filters.
conv_1 <- mx.symbol.Convolution(data= data, kernel = c(5,5), num_filter = 20)
tanh_1 <- mx.symbol.Activation(data= conv_1, act_type = "tanh")
pool_1 <- mx.symbol.Pooling(data = tanh_1, pool_type = "max", kernel = c(2,2), stride = c(2,2))
# 2nd convolutional layer 5x5 kernel and 50 filters.
conv_2 <- mx.symbol.Convolution(data = pool_1, kernel = c(5,5), num_filter = 50)
tanh_2 <- mx.symbol.Activation(data = conv_2, act_type = "tanh")
pool_2 <- mx.symbol.Pooling(data = tanh_2, pool_type = "max", kernel = c(2,2), stride = c(2,2))
# 1st fully connected layer
flat <- mx.symbol.Flatten(data = pool_2)
fcl_1 <- mx.symbol.FullyConnected(data = flat, num_hidden = 500)
tanh_3 <- mx.symbol.Activation(data = fcl_1, act_type = "tanh")
# 2nd fully connected layer
fcl_2 <- mx.symbol.FullyConnected(data = tanh_3, num_hidden = 2)
# Output
NN_model <- mx.symbol.SoftmaxOutput(data = fcl_2)
# Set seed for reproducibility
mx.set.seed(100)
# Device used. Sadly not the GPU :-(
device <- mx.cpu()
# Train on 1200 samples
model <- mx.model.FeedForward.create(NN_model, X = train_array, y = train_y,
ctx = device,
num.round = 30,
array.batch.size = 100,
learning.rate = 0.05,
momentum = 0.9,
wd = 0.00001,
eval.metric = mx.metric.accuracy,
epoch.end.callback = mx.callback.log.train.metric(100))
# Test on 312 samples
predict_probs <- predict(model, test_array)
predicted_labels <- max.col(t(predict_probs)) - 1
table(test__[,1], predicted_labels)
sum(diag(table(test__[,1], predicted_labels)))/312
##############################################
# Output
##############################################
# predicted_labels
# 0 1
# 0 83 47
# 1 34 149
#
#
# [1] 0.7412141
#
@sparcycram
Copy link

Great code and blog Thanks
Found the following errors I think

line 34 pool2 should be pool_2 ??
line 60 test.array should be test_array ??

Unfortunately my result on my dogs and flower images is 1. Train-accuracy goes from 0.92 to 1 at step 2 and stays at 1. I suspect I have some sort of error any thoughts?

@mick001
Copy link
Author

mick001 commented Jul 27, 2016

Hi thanks for pointing out the typos and for taking the time to read the post! Training accuracy of 1 on some batches is good as long as the model has not overfitted the whole training data data, it is unusual to get 1 at the second epoch though.. Maybe check that the data is scaled properly and that it is in the right format, the train_array variable should be a 784 x n_samples matrix for 28x28 greyscale images. The more samples you use the better. I have received a lot of requests and I am considering making a more detailed tutorial with a reproducible example so that people can try that and get more confident with the model. Stay tuned if you're interested in that.

@you-ruei
Copy link

you-ruei commented Aug 8, 2016

Thank you for your code and blogs, it's so great
But I encountered a few problems
in plants_28.csv Set label <<<<< label set 2?

in Test on 312 samples My results:
predicted_labels
        1
  1 156
  2 156
I can not find what is causing this

Sincerely

@mick001
Copy link
Author

mick001 commented Aug 8, 2016

@you-ruei Hi, I made a new tutorial with a reproducible example so that you can check step by step that each intermediate result is correct. This more comprehensive tutorial can be found at https://firsttimeprogrammer.blogspot.it/2016/08/image-recognition-tutorial-in-r-using.html if you are new to deep learning with MXNet I suggest you to try this first, then you can apply the same logic to the dogs vs plants classification and maybe do other experiments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment