-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnistCecilia.lsh
229 lines (219 loc) · 11.5 KB
/
mnistCecilia.lsh
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
;;MNIST - lush, gblearn
;;Cecília Carneiro e Silva
;;DATE: 06/2016
(libload "gblearn2/gb-trainers")
(libload "gblearn2/gb-meters")
(libload "gblearn2/net-cscscf")
(libload "gblearn2/demos/dsource-mnist")
;;(defvar *mnist-path* "/home/cecilia/machineLearning/eblearn-code/demos/mnist/")
(defvar *mnist-path* "/home/ceciliacarneiro/ufu/graduacao/ia/mnist-database/")
;;(defvar *arq-path* "/home/cecilia/machineLearning/lushMnist/mnist/")
(defvar *arq-path* "/home/ceciliacarneiro/ufu/graduacao/ia/lushMnist/mnist/")
(de mnist-main (treino-size)
(let ((treino-db
(new dsource-idx3l-narrow
(new dsource-mnist
(load-matrix (concat-fname *mnist-path* "train-images.idx3-ubyte"))
(load-matrix (concat-fname *mnist-path* "train-labels.idx1-ubyte"))
28 28 0 0.01) ;;w h bias offset
treino-size 0))) ;;cria n do dsource-mnist
(let ((nOut 10)
(objetivo 1))
(let ((labels (int-matrix nOut))
(objetivos (float-matrix nOut nOut)))
(idx-f2dotc objetivos 1.5 objetivos)
(objetivos ()() (- objetivo))
(for (i 0 (- nOut 1))
(objetivos i i objetivo)
(labels i i))
;(print labels)
;(print (objetivos 1 1))
;(print (objetivos 0 1))
(let ((treino-parametro (new idx1-ddparam 0 treino-size)))
;(print treino-parametro)
(let ((lenet-rede
(new-lenet5 28 28
5 5
2 2
5 5
2 2
200 ;; dim of hidden layer
10 treino-parametro)))
;(print lenet-rede)
(let ((minha-rede
(new idx3-supervised-module
lenet-rede
(new edist-cost labels 1 1 objetivos)
(new max-classer labels))))
(let ((treino-rede
(new supervised-gradient minha-rede treino-parametro)))
(let ((treino-medida (new classifier-meter)))
(==> :minha-rede:machine forget 1 2) ;;inicializa os pesos da rede aleatorios
(==> treino-rede compute-diaghessian treino-db 200 0.02)
(==> treino-rede train treino-db treino-medida 1e-4 0)
(printf "Rede treinada com %d imagens.\n\n" treino-size)
(eval
`(lambda(detectar-size &optional (numero-imagem false))
(cond ((stringp numero-imagem)
(let* ((arquivos (arq-quebrar))
(matImagem (car arquivos))
(matLabel (cadr arquivos))
(itens (caddr arquivos)))
(let ((elemento (assoc numero-imagem itens)))
(cond (elemento
(let ((detectar-db
(new dsource-idx3l-narrow
(new dsource-mnist
matImagem
matLabel
28 28 0 0.01)
detectar-size 0)))
(let ((detectar-medida (new classifier-meter)))
(let ((detectar-rede ,treino-rede));;dps de treinada
(print itens)
(printf
"Testando o elemento %s do arquivo t10k-images.\n"
numero-imagem)
(let ((numero-lista (cadr elemento)))
(print numero-lista)
(let ((resultado
(nth
(==> detectar-rede
test-sample
detectar-db detectar-medida numero-lista) 2))
(esperado
(mostrar-imagem numero-lista matImagem matLabel)))
(printf
"Esperado = %d | Obtido = %d \n" esperado resultado)
))
))
)))
)))
((and (numberp numero-imagem) (< numero-imagem detectar-size))
(let* ((matImagem
(load-matrix (concat-fname *mnist-path* "t10k-images.idx3-ubyte")))
(matLabel
(load-matrix (concat-fname *mnist-path* "t10k-labels.idx1-ubyte"))))
(let ((detectar-db
(new dsource-idx3l-narrow
(new dsource-mnist
matImagem
matLabel
28 28 0 0.01)
detectar-size 0)))
(let ((detectar-medida (new classifier-meter)))
(let ((detectar-rede ,treino-rede));;dps de treinada
(printf
"Testando o elemento %d do arquivo t10k-images.\n" numero-imagem)
(let ((resultado
(nth
(==> detectar-rede
test-sample
detectar-db detectar-medida numero-imagem) 2))
(esperado (mostrar-imagem numero-imagem matImagem matLabel)))
(printf "Esperado = %d | Obtido = %d \n" esperado resultado)
))
)
))
)
(t (printf "Testando a rede para %d imagens.\n" detectar-size)
(let* ((matImagem
(load-matrix (concat-fname *mnist-path* "t10k-images.idx3-ubyte")))
(matLabel
(load-matrix (concat-fname *mnist-path* "t10k-labels.idx1-ubyte"))))
(let ((detectar-db
(new dsource-idx3l-narrow
(new dsource-mnist
matImagem
matLabel
28 28 0 0.01)
detectar-size 0)))
(let ((detectar-medida (new classifier-meter)))
(let ((detectar-rede ,treino-rede));;dps de treinada
(==> detectar-rede test detectar-db detectar-medida)
(==> detectar-medida display))
)))
))
)
))
)
))
))
))
)
(de new-lenet5 (image-height
image-width
ki0 kj0 si0 sj0 ki1 kj1 si1 sj1
hid output-size net-param)
(let ((table0 (full-table 1 20))
(table1 (full-table 20 50))
(table2 (full-table 50 hid)))
(new net-cscscf
image-height image-width
ki0 kj0 table0 si0 sj0
ki1 kj1 table1 si1 sj1
;; WARNING: those two numbers must be changed
;; when image-height/image-width change
(/ (- (/ (- image-height (1- ki0)) si0) (1- ki1)) si1)
(/ (- (/ (- image-width (1- kj0)) sj0) (1- kj1)) sj1)
table2
output-size
net-param)))
(de mostrar-imagem (numeroMat imagemMat labelMat)
(print "Imagem:")
(for (i 0 27)
(for (j 0 27)
(let ((intensidade (imagemMat numeroMat i j)))
(cond ((< intensidade 10) (printf "%d " intensidade))
((< intensidade 100) (printf "%d " intensidade))
(t (printf "%d " intensidade))
)))
(printf "\n"))
(labelMat numeroMat)
)
(de arq-quebrar ()
(let ((matImagem (load-matrix (concat-fname *mnist-path* "t10k-images.idx3-ubyte")))
(matLabel (load-matrix (concat-fname *mnist-path* "t10k-labels.idx1-ubyte"))))
(let* ((arquivos (files *arq-path*))
(arqValidos (glob "([0-9].vlq)|([0-9].ccs)|([0-9].usr)" arquivos)))
(let ((quantidade (length arqValidos))
(paraUsar '()))
(for (k 1 quantidade)
(let* ((arqAtual (nth arqValidos k))
(fd (open-read (concat-fname *arq-path* arqAtual)))
(labelValor (val (car (regex-split "([0-9])" arqAtual))))
(labelExt (car (regex-split "([a-z])+" arqAtual))))
(let* ((linhas (read-lines fd))
(tam (length linhas)))
(for (i 0 27)
(let ((linha (nth linhas i)) )
(cond ((stringp linha)
(let ((linhaTratada (regex-split "([0-9]+)" linha)))
;(print linhaTratada)
;(print (nth linhaTratada 1))
(for (j 1 27)
(matImagem k i j (val (nth linhaTratada j)))
)
)
))
)
)
(matLabel k labelValor)
(setq paraUsar (cons (list arqAtual k) paraUsar))
)
)
)
(list matImagem matLabel paraUsar)
)
))
)
(de print-mat (mat li lj lz)
(for (z 0 lz)
(for (i 0 li)
(for (j 0 lj)
(printf "%d " (mat z i j))
)
(printf "\n")
)
)
)