-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist.hs
78 lines (66 loc) · 2.13 KB
/
mnist.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import qualified Codec.Compression.GZip as GZip
import qualified Data.ByteString.Lazy.Char8 as Char8
import qualified Data.HashMap as HashMap
import qualified Numeric.LinearAlgebra.Data as HMatrix
import Data.Char
import Data.Either.Unwrap
import Data.HashMap
import Network.Download
import Numeric.LinearAlgebra.Data
import System.Directory
import System.FilePath
import System.IO
urlBase = "http://yann.lecun.com/exdb/mnist/"
keyFile = HashMap.fromList
[ ("train_img" , "train-images-idx3-ubyte.gz")
, ("train_label", "train-labels-idx1-ubyte.gz")
, ("test_img" , "t10k-images-idx3-ubyte.gz")
, ("test_label" , "t10k-labels-idx1-ubyte.gz") ]
saveFile = toFilePath "mnist.pkl"
trainNum = 60000
testNum = 10000
imageDim = (1, 28, 28)
imageSize = 784
download fileName = do
filePath <- toFilePath fileName
isExists <- doesFileExist filePath
if isExists
then return ()
else do
res <- openURI $ urlBase ++ fileName
putStrLn $ "Downloading " ++ fileName ++ " ... "
withFile filePath
WriteMode
(\handle -> do
hPrint handle $ fromRight res)
putStrLn "Done."
downloadMinst = mapM_ download $
elems keyFile
toFilePath fileName = do
datasetDir <- getCurrentDirectory
return $ datasetDir
++ "/" ++ fileName
charsToDoubles::[Char] -> [R]
charsToDoubles = fmap (fromIntegral . digitToInt)
doLoadLabel fileName = do
content <- doLoadFromGZip fileName
return $ HMatrix.fromList $
charsToDoubles $ drop 8 content
doLoadImage fileName = do
content <- doLoadFromGZip fileName
lst <- return $
charsToDoubles $ drop 16 content
putStrLn "Done."
return $ HMatrix.fromList lst
doLoadFromGZip fileName = do
filePath <- toFilePath fileName
putStrLn $ "Converting "
++ fileName ++ "to utf8 array ..."
compressed <- withFile filePath
ReadMode
(\handle -> hGetContents handle)
content <- fmap GZip.decompress $ return $
Char8.pack compressed
putStrLn "Done"
return $ Char8.unpack content
main = downloadMinst