-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmnist.l
88 lines (80 loc) · 2.98 KB
/
mnist.l
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
79
80
81
82
83
84
85
86
;;
;;
;;
(defun mnist-read-a-2d-image
(f width height
&optional (img (make-array (list width height)
:element-type :byte)))
(catch :mnist-exit
(let ((r nil) v)
(dotimes (i height)
(dotimes (j width)
(if (eq (setq v (read f nil r)) r)
(throw :mnist-exit nil)
(setf (aref img i j) v)))))
img))
(defun mnist-read-2d-image
(fname width height)
(let (res img eof)
(with-open-file
(f fname :direction :input)
(while (setq img (mnist-read-a-2d-image f width height))
(push img res)))
res))
;; (defun mnist-read-a-image
;; (f length
;; &optional (n 1.0) (img (make-array length :element-type :byte)))
;; (catch :mnist-exit
;; (let ((r '(nil)) v)
;; (dotimes (i length)
;; (if (eq (setq v (read f nil r)) r)
;; (throw :mnist-exit nil)
;; (setf (aref img i) v))))
;; (scale n (coerce img float-vector))))
(defun mnist-read-a-image
(f length
&optional (n 1.0) (img (make-array length :element-type :float)))
(catch :mnist-exit
(let ((r '(nil)) v)
(dotimes (i length)
(if (eq (setq v (read f nil r)) r)
(throw :mnist-exit nil)
(setf (aref img i) (* v n)))))
img))
(defun mnist-read-image
(fname length
&optional (n 1.0))
(let (res img eof)
(with-open-file
(f fname :direction :input)
(while (setq img (mnist-read-a-image f length n))
(push img res)))
res))
(defun test-2d nil
(setq *test-images* (mnist-read-2d-image "test-images.txt" 28 28))
(format t "test-images:~A~%" (length *test-images*))
(setq *test-labels* (mnist-read-2d-image "test-labels.txt" 1 1))
(format t "test-labels:~A~%" (length *test-labels*))
(setq *train-labels* (mnist-read-2d-image "train-labels.txt" 1 1))
(format t "train-labels:~A~%" (length *train-labels*))
(setq *train-images* (mnist-read-2d-image "train-images.txt" 28 28))
(format t "train-images:~A~%" (length *train-images*))
)
(defun test nil
(let* ((start (unix::runtime)))
(setq *test-images* (mnist-read-image "test-images.txt" 784 (/ 1.0 255.0)))
(format t "test-images:~A~%" (length *test-images*))
(format t "load time: ~A~%" (* (/ 1000.0 internal-time-units-per-second) (- (unix::runtime) start)))
(setq start (unix::runtime))
(setq *test-labels* (mnist-read-image "test-labels.txt" 1 1.0))
(format t "test-labels:~A~%" (length *test-labels*))
(format t "load time: ~A~%" (* (/ 1000.0 internal-time-units-per-second) (- (unix::runtime) start)))
(setq start (unix::runtime))
(setq *train-labels* (mnist-read-image "train-labels.txt" 1 1.0))
(format t "train-labels:~A~%" (length *train-labels*))
(format t "load time: ~A~%" (* (/ 1000.0 internal-time-units-per-second) (- (unix::runtime) start)))
(setq start (unix::runtime))
(setq *train-images* (mnist-read-image "train-images.txt" 784 (/ 1.0 255.0)))
(format t "train-images:~A~%" (length *train-images*))
(format t "load time: ~A~%" (* (/ 1000.0 internal-time-units-per-second) (- (unix::runtime) start)))
))