Guest User

Untitled

a guest
Nov 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. {-# LANGUAGE TypeApplications #-}
  2. module Main where
  3.  
  4. import Prelude hiding (readFile)
  5.  
  6. import Data.Binary
  7. import Data.Binary.Get
  8. import qualified Data.ByteString as B
  9. import qualified Data.ByteString.Lazy as BL
  10.  
  11. import System.IO.MMap
  12.  
  13. import Control.Monad
  14.  
  15. import Data.Maybe (listToMaybe)
  16. import Data.List (unfoldr, intercalate)
  17.  
  18. data Labels = Labels Int Int [Float]
  19. deriving (Show)
  20.  
  21. instance Binary Labels where
  22. put = error "unimplemented"
  23. get = do
  24. magic <- fromIntegral <$> getWord32be
  25. count <- fromIntegral <$> getWord32be
  26. labels <- replicateM count $ fromIntegral <$> getWord8
  27. return $ Labels magic count labels
  28.  
  29. data Images = Images !Int !Int !Int !Int ![[Float]]
  30. deriving (Show)
  31.  
  32. instance Binary Images where
  33. put = error "unimplemented"
  34. get = do
  35. magic <- fromIntegral <$> getWord32be
  36. count <- fromIntegral <$> getWord32be
  37. rows <- fromIntegral <$> getWord32be
  38. cols <- fromIntegral <$> getWord32be
  39. images <- replicateM count (getImage rows cols)
  40. return $ Images magic count rows cols images
  41. where
  42. getImage rows cols = do
  43. pixels <- B.unpack <$> getByteString (rows*cols)
  44. let image = fromIntegral @_ @Float <$> pixels
  45. return image
  46.  
  47. main = do
  48. ls <- decode @Labels <$> BL.readFile "train-labels-idx1-ubyte"
  49. ms <- decode @Images <$> readFileViaMmap "train-images-idx3-ubyte"
  50. let (Images _ _ rows cols images) = ms
  51. mapM_ print $ map (Image rows cols) images
  52.  
  53. readFileViaMmap path = do
  54. (ptr, _, _, size) <- mmapFilePtr path ReadOnly Nothing
  55. BL.fromStrict <$> B.packCStringLen (ptr, size)
  56.  
  57. data Image = Image Int Int [Float]
  58.  
  59. instance Show Image where
  60. show (Image rows cols pixels) = intercalate "\n" $ unfoldr showLine pixels
  61. where
  62. showLine pixels =
  63. let (line,rest) = splitAt cols pixels
  64. in listToMaybe rest >> return (map showPixel line,rest)
  65. showPixel p | p < 256 / 10 = '░'
  66. | p < 256 / 4 = '▒'
  67. | p < 256 / 2 = '▓'
  68. | p < 256 = '█'
Add Comment
Please, Sign In to add comment