Created
April 27, 2022 15:20
-
-
Save smithcommajoseph/020c45b26266c730a791050db3e76086 to your computer and use it in GitHub Desktop.
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
# based on https://gist.github.com/daviddalpiaz/ae62ae5ccd0bada4b9acd6dbc9008706 | |
# load image files | |
load_image_file = function(filename) { | |
f = gzfile(filename, 'rb') | |
readBin(f, 'integer', n = 1, size = 4, endian = 'big') | |
n = readBin(f, 'integer', n = 1, size = 4, endian = 'big') | |
nrow = readBin(f, 'integer', n = 1, size = 4, endian = 'big') | |
ncol = readBin(f, 'integer', n = 1, size = 4, endian = 'big') | |
x = readBin(f, 'integer', n = n * nrow * ncol, size = 1, signed = FALSE) | |
close(f) | |
data.frame(matrix(x, ncol = nrow * ncol, byrow = TRUE)) | |
} | |
# load label files | |
load_label_file = function(filename) { | |
f = gzfile(filename, 'rb') | |
readBin(f, 'integer', n = 1, size = 4, endian = 'big') | |
n = readBin(f, 'integer', n = 1, size = 4, endian = 'big') | |
y = readBin(f, 'integer', n = n, size = 1, signed = FALSE) | |
close(f) | |
y | |
} | |
# load training data | |
train = load_image_file('mnist/train-images-idx3-ubyte.gz') | |
train$y = as.factor(load_label_file('mnist/train-labels-idx1-ubyte.gz')) | |
#load testing data | |
test = load_image_file('mnist/t10k-images-idx3-ubyte.gz') | |
test$y = as.factor(load_label_file('mnist/t10k-labels-idx1-ubyte.gz')) | |
# Now plot the first 100 images + labels | |
# This section obtained from | |
# https://www.r-bloggers.com/2015/11/a-little-h2o-deeplearning-experiment-on-the-mnist-data-set/ | |
par( mfrow = c(10,10), mai = c(0,0,0,0)) | |
for(i in 1:100){ | |
y = as.matrix(train[i, 1:784]) | |
dim(y) = c(28, 28) | |
image( y[,nrow(y):1], axes = FALSE, col = gray(255:0 / 255)) | |
text( 0.2, 0, train[i,785], cex = 3, col = 2, pos = c(3,4)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment