Last active
April 26, 2018 10:45
-
-
Save WayneCui/1b4fc7e6590b2d98f3013444792f6479 to your computer and use it in GitHub Desktop.
A mnist loader in Red
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
Red [ | |
author: "Wayne Cui" | |
description: {Should first download the mnist data from http://yann.lecun.com/exdb/mnist/} | |
] | |
train-image-filename: "data/train-images-idx3-ubyte" | |
train-label-filename: "data/train-labels-idx1-ubyte" | |
test-image-filename: "data/t10k-images-idx3-ubyte" | |
test-label-filename: "data/t10k-labels-idx1-ubyte" | |
load-data: function [ path-img path-label][ | |
label-data: read/binary to-file path-label | |
label-magic-num: copy/part label-data 4 ;should be #{00000801} (2049) | |
label-num-of-items: to integer! copy/part at label-data 5 4 | |
img-data: read/binary to-file path-img | |
img-magic-num: copy/part img-data 4 ;should be #{00000803} (2051) | |
img-num-of-items: to integer! copy/part at img-data 5 4 | |
rows: to integer! copy/part at img-data 9 4 ;should be 28 | |
cols: to integer! copy/part at img-data 13 4 ;should be 28 | |
labels: parse at label-data 9 [ collect some [keep skip]] | |
images: parse at img-data 17 compose/deep [ collect some [ collect (rows * cols) [keep skip ]]] | |
;probe images | |
result: copy [] | |
append/only result labels | |
append/only result images | |
result | |
] | |
display: function [ img width threshold ][ | |
render: copy "" | |
i: 1 | |
foreach pixel img [ | |
if i % width = 0 [ | |
append render lf | |
] | |
if pixel > threshold [ | |
append render "@" | |
] [ | |
append render "." | |
] | |
i: i + 1 | |
] | |
render | |
] | |
result: load-data test-image-filename test-label-filename | |
idx: 1000 | |
print result/1/:idx | |
print display result/2/:idx 28 150 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment