-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpngs.lisp
67 lines (57 loc) · 2.64 KB
/
pngs.lisp
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
(in-package :dc-bianet)
(defun infer-directory-pngs (environment-id label-directory)
(infer-label-directory-files
label-directory
(lambda (file)
(infer-png environment-id file))))
;; Needs work. Should pick the largest image in the tree. This will also require
;; changing the neural network so that it can correctly process variable-length
;; vectors.
(defun png-tree->suggest-topology (png-tree-path)
(loop with output-count = (length (subdirectory-names png-tree-path))
with input-count = (length (read-png (example-file png-tree-path ".png")))
for power = 1 then (1+ power)
while (< (expt 2 power) output-count)
finally (return (list input-count (expt 2 (1+ power)) output-count))))
(defun png-tree->frames (directory &key as-vector)
(loop with labels = (subdirectory-names directory)
with label->index = (list->key-index labels)
with label->expected-outputs = (label-outputs-hash label->index)
for label in labels
for label-folder = (join-paths directory label)
for expected-outputs = (gethash label label->expected-outputs)
appending (pngs->frames-for-label label-folder expected-outputs)
into frames
finally (return (if as-vector
(map 'vector 'identity frames)
frames))))
(defun png-data-set-to-csv (data-set-directory)
(data-set-directory-to-csv
data-set-directory
(lambda (file) (normalize-list (read-png file)))))
(defun infer-png (environment-id file)
(let ((environment (environment-by-id environment-id)))
(outputs-label
(label-vector (output-labels environment))
(infer (net environment)
(normalize-list (read-png file) :min 0 :max 255)))))
(defun infer-pngs (environment-id label-directory)
(infer-label-directory-files
label-directory
(lambda (file) (infer-png environment-id file))))
(defun read-png (filename &key (width 28) (height 28))
(loop with image-data = (png-read:image-data
(png-read:read-png-file filename))
with dimensions = (length (array-dimensions image-data))
for y from 0 below height
appending (loop for x from 0 below width
collecting (if (= dimensions 2)
(aref image-data x y)
(aref image-data x y 0)))
into intensity-list
finally (return (invert-intensity intensity-list))))
(defun invert-intensity (list &key (max 255))
(loop for element in list collect (- max element)))
(defun png-to-inputs (filename width height)
(normalize-list
(read-png filename :width width :height height) :min 0 :max 255))