Last active
March 13, 2019 08:16
-
-
Save mick001/5070816817171f1a1d5880c20c1c93ed to your computer and use it in GitHub Desktop.
Image recognition tutorial in R using deep convolutional neural networks (MXNet package). Part 2. Full article at https://firsttimeprogrammer.blogspot.com/2016/08/image-recognition-tutorial-in-r-using.html
This file contains 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
# This script is used to resize images from 64x64 to 28x28 pixels | |
# Clear workspace | |
rm(list=ls()) | |
# Load EBImage library | |
require(EBImage) | |
# Load data | |
X <- read.csv("olivetti_X.csv", header = F) | |
labels <- read.csv("olivetti_y.csv", header = F) | |
# Dataframe of resized images | |
rs_df <- data.frame() | |
# Main loop: for each image, resize and set it to greyscale | |
for(i in 1:nrow(X)) | |
{ | |
# Try-catch | |
result <- tryCatch({ | |
# Image (as 1d vector) | |
img <- as.numeric(X[i,]) | |
# Reshape as a 64x64 image (EBImage object) | |
img <- Image(img, dim=c(64, 64), colormode = "Grayscale") | |
# Resize image to 28x28 pixels | |
img_resized <- resize(img, w = 28, h = 28) | |
# Get image matrix (there should be another function to do this faster and more neatly!) | |
img_matrix <- [email protected] | |
# Coerce to a vector | |
img_vector <- as.vector(t(img_matrix)) | |
# Add label | |
label <- labels[i,] | |
vec <- c(label, img_vector) | |
# Stack in rs_df using rbind | |
rs_df <- rbind(rs_df, vec) | |
# Print status | |
print(paste("Done",i,sep = " "))}, | |
# Error function (just prints the error). Btw you should get no errors! | |
error = function(e){print(e)}) | |
} | |
# Set names. The first columns are the labels, the other columns are the pixels. | |
names(rs_df) <- c("label", paste("pixel", c(1:784))) | |
# Train-test split | |
#------------------------------------------------------------------------------- | |
# Simple train-test split. No crossvalidation is done in this tutorial. | |
# Set seed for reproducibility purposes | |
set.seed(100) | |
# Shuffled df | |
shuffled <- rs_df[sample(1:400),] | |
# Train-test split | |
train_28 <- shuffled[1:360, ] | |
test_28 <- shuffled[361:400, ] | |
# Save train-test datasets | |
write.csv(train_28, "C://train_28.csv", row.names = FALSE) | |
write.csv(test_28, "C://test_28.csv", row.names = FALSE) | |
# Done! | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment