Last active
March 13, 2019 08:16
-
-
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
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
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 | |
# |
@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
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