view thesis/cortex.org @ 451:0a4362d1f138

finishing up chapter 3.
author Robert McIntyre <rlm@mit.edu>
date Wed, 26 Mar 2014 20:38:17 -0400
parents 432f2c4646cb
children f339e3d5cc8c
line wrap: on
line source
1 #+title: =CORTEX=
2 #+author: Robert McIntyre
3 #+email: rlm@mit.edu
4 #+description: Using embodied AI to facilitate Artificial Imagination.
5 #+keywords: AI, clojure, embodiment
6 #+LaTeX_CLASS_OPTIONS: [nofloat]
8 * Empathy and Embodiment as problem solving strategieszzzzzzz
10 By the end of this thesis, you will have seen a novel approach to
11 interpreting video using embodiment and empathy. You will have also
12 seen one way to efficiently implement empathy for embodied
13 creatures. Finally, you will become familiar with =CORTEX=, a system
14 for designing and simulating creatures with rich senses, which you
15 may choose to use in your own research.
17 This is the core vision of my thesis: That one of the important ways
18 in which we understand others is by imagining ourselves in their
19 position and emphatically feeling experiences relative to our own
20 bodies. By understanding events in terms of our own previous
21 corporeal experience, we greatly constrain the possibilities of what
22 would otherwise be an unwieldy exponential search. This extra
23 constraint can be the difference between easily understanding what
24 is happening in a video and being completely lost in a sea of
25 incomprehensible color and movement.
27 ** Recognizing actions in video is extremely difficult
29 Consider for example the problem of determining what is happening
30 in a video of which this is one frame:
32 #+caption: A cat drinking some water. Identifying this action is
33 #+caption: beyond the state of the art for computers.
34 #+ATTR_LaTeX: :width 7cm
35 [[./images/cat-drinking.jpg]]
37 It is currently impossible for any computer program to reliably
38 label such a video as ``drinking''. And rightly so -- it is a very
39 hard problem! What features can you describe in terms of low level
40 functions of pixels that can even begin to describe at a high level
41 what is happening here?
43 Or suppose that you are building a program that recognizes chairs.
44 How could you ``see'' the chair in figure \ref{hidden-chair}?
46 #+caption: The chair in this image is quite obvious to humans, but I
47 #+caption: doubt that any modern computer vision program can find it.
48 #+name: hidden-chair
49 #+ATTR_LaTeX: :width 10cm
50 [[./images/fat-person-sitting-at-desk.jpg]]
52 Finally, how is it that you can easily tell the difference between
53 how the girls /muscles/ are working in figure \ref{girl}?
55 #+caption: The mysterious ``common sense'' appears here as you are able
56 #+caption: to discern the difference in how the girl's arm muscles
57 #+caption: are activated between the two images.
58 #+name: girl
59 #+ATTR_LaTeX: :width 7cm
60 [[./images/wall-push.png]]
62 Each of these examples tells us something about what might be going
63 on in our minds as we easily solve these recognition problems.
65 The hidden chairs show us that we are strongly triggered by cues
66 relating to the position of human bodies, and that we can determine
67 the overall physical configuration of a human body even if much of
68 that body is occluded.
70 The picture of the girl pushing against the wall tells us that we
71 have common sense knowledge about the kinetics of our own bodies.
72 We know well how our muscles would have to work to maintain us in
73 most positions, and we can easily project this self-knowledge to
74 imagined positions triggered by images of the human body.
76 ** =EMPATH= neatly solves recognition problems
78 I propose a system that can express the types of recognition
79 problems above in a form amenable to computation. It is split into
80 four parts:
82 - Free/Guided Play :: The creature moves around and experiences the
83 world through its unique perspective. Many otherwise
84 complicated actions are easily described in the language of a
85 full suite of body-centered, rich senses. For example,
86 drinking is the feeling of water sliding down your throat, and
87 cooling your insides. It's often accompanied by bringing your
88 hand close to your face, or bringing your face close to water.
89 Sitting down is the feeling of bending your knees, activating
90 your quadriceps, then feeling a surface with your bottom and
91 relaxing your legs. These body-centered action descriptions
92 can be either learned or hard coded.
93 - Posture Imitation :: When trying to interpret a video or image,
94 the creature takes a model of itself and aligns it with
95 whatever it sees. This alignment can even cross species, as
96 when humans try to align themselves with things like ponies,
97 dogs, or other humans with a different body type.
98 - Empathy :: The alignment triggers associations with
99 sensory data from prior experiences. For example, the
100 alignment itself easily maps to proprioceptive data. Any
101 sounds or obvious skin contact in the video can to a lesser
102 extent trigger previous experience. Segments of previous
103 experiences are stitched together to form a coherent and
104 complete sensory portrait of the scene.
105 - Recognition :: With the scene described in terms of first
106 person sensory events, the creature can now run its
107 action-identification programs on this synthesized sensory
108 data, just as it would if it were actually experiencing the
109 scene first-hand. If previous experience has been accurately
110 retrieved, and if it is analogous enough to the scene, then
111 the creature will correctly identify the action in the scene.
113 For example, I think humans are able to label the cat video as
114 ``drinking'' because they imagine /themselves/ as the cat, and
115 imagine putting their face up against a stream of water and
116 sticking out their tongue. In that imagined world, they can feel
117 the cool water hitting their tongue, and feel the water entering
118 their body, and are able to recognize that /feeling/ as drinking.
119 So, the label of the action is not really in the pixels of the
120 image, but is found clearly in a simulation inspired by those
121 pixels. An imaginative system, having been trained on drinking and
122 non-drinking examples and learning that the most important
123 component of drinking is the feeling of water sliding down one's
124 throat, would analyze a video of a cat drinking in the following
125 manner:
127 1. Create a physical model of the video by putting a ``fuzzy''
128 model of its own body in place of the cat. Possibly also create
129 a simulation of the stream of water.
131 2. Play out this simulated scene and generate imagined sensory
132 experience. This will include relevant muscle contractions, a
133 close up view of the stream from the cat's perspective, and most
134 importantly, the imagined feeling of water entering the
135 mouth. The imagined sensory experience can come from a
136 simulation of the event, but can also be pattern-matched from
137 previous, similar embodied experience.
139 3. The action is now easily identified as drinking by the sense of
140 taste alone. The other senses (such as the tongue moving in and
141 out) help to give plausibility to the simulated action. Note that
142 the sense of vision, while critical in creating the simulation,
143 is not critical for identifying the action from the simulation.
145 For the chair examples, the process is even easier:
147 1. Align a model of your body to the person in the image.
149 2. Generate proprioceptive sensory data from this alignment.
151 3. Use the imagined proprioceptive data as a key to lookup related
152 sensory experience associated with that particular proproceptive
153 feeling.
155 4. Retrieve the feeling of your bottom resting on a surface, your
156 knees bent, and your leg muscles relaxed.
158 5. This sensory information is consistent with the =sitting?=
159 sensory predicate, so you (and the entity in the image) must be
160 sitting.
162 6. There must be a chair-like object since you are sitting.
164 Empathy offers yet another alternative to the age-old AI
165 representation question: ``What is a chair?'' --- A chair is the
166 feeling of sitting.
168 My program, =EMPATH= uses this empathic problem solving technique
169 to interpret the actions of a simple, worm-like creature.
171 #+caption: The worm performs many actions during free play such as
172 #+caption: curling, wiggling, and resting.
173 #+name: worm-intro
174 #+ATTR_LaTeX: :width 15cm
175 [[./images/worm-intro-white.png]]
177 #+caption: =EMPATH= recognized and classified each of these poses by
178 #+caption: inferring the complete sensory experience from
179 #+caption: proprioceptive data.
180 #+name: worm-recognition-intro
181 #+ATTR_LaTeX: :width 15cm
182 [[./images/worm-poses.png]]
184 One powerful advantage of empathic problem solving is that it
185 factors the action recognition problem into two easier problems. To
186 use empathy, you need an /aligner/, which takes the video and a
187 model of your body, and aligns the model with the video. Then, you
188 need a /recognizer/, which uses the aligned model to interpret the
189 action. The power in this method lies in the fact that you describe
190 all actions form a body-centered viewpoint. You are less tied to
191 the particulars of any visual representation of the actions. If you
192 teach the system what ``running'' is, and you have a good enough
193 aligner, the system will from then on be able to recognize running
194 from any point of view, even strange points of view like above or
195 underneath the runner. This is in contrast to action recognition
196 schemes that try to identify actions using a non-embodied approach.
197 If these systems learn about running as viewed from the side, they
198 will not automatically be able to recognize running from any other
199 viewpoint.
201 Another powerful advantage is that using the language of multiple
202 body-centered rich senses to describe body-centerd actions offers a
203 massive boost in descriptive capability. Consider how difficult it
204 would be to compose a set of HOG filters to describe the action of
205 a simple worm-creature ``curling'' so that its head touches its
206 tail, and then behold the simplicity of describing thus action in a
207 language designed for the task (listing \ref{grand-circle-intro}):
209 #+caption: Body-centerd actions are best expressed in a body-centered
210 #+caption: language. This code detects when the worm has curled into a
211 #+caption: full circle. Imagine how you would replicate this functionality
212 #+caption: using low-level pixel features such as HOG filters!
213 #+name: grand-circle-intro
214 #+begin_listing clojure
215 #+begin_src clojure
216 (defn grand-circle?
217 "Does the worm form a majestic circle (one end touching the other)?"
218 [experiences]
219 (and (curled? experiences)
220 (let [worm-touch (:touch (peek experiences))
221 tail-touch (worm-touch 0)
222 head-touch (worm-touch 4)]
223 (and (< 0.55 (contact worm-segment-bottom-tip tail-touch))
224 (< 0.55 (contact worm-segment-top-tip head-touch))))))
225 #+end_src
226 #+end_listing
229 ** =CORTEX= is a toolkit for building sensate creatures
231 I built =CORTEX= to be a general AI research platform for doing
232 experiments involving multiple rich senses and a wide variety and
233 number of creatures. I intend it to be useful as a library for many
234 more projects than just this one. =CORTEX= was necessary to meet a
235 need among AI researchers at CSAIL and beyond, which is that people
236 often will invent neat ideas that are best expressed in the
237 language of creatures and senses, but in order to explore those
238 ideas they must first build a platform in which they can create
239 simulated creatures with rich senses! There are many ideas that
240 would be simple to execute (such as =EMPATH=), but attached to them
241 is the multi-month effort to make a good creature simulator. Often,
242 that initial investment of time proves to be too much, and the
243 project must make do with a lesser environment.
245 =CORTEX= is well suited as an environment for embodied AI research
246 for three reasons:
248 - You can create new creatures using Blender, a popular 3D modeling
249 program. Each sense can be specified using special blender nodes
250 with biologically inspired paramaters. You need not write any
251 code to create a creature, and can use a wide library of
252 pre-existing blender models as a base for your own creatures.
254 - =CORTEX= implements a wide variety of senses, including touch,
255 proprioception, vision, hearing, and muscle tension. Complicated
256 senses like touch, and vision involve multiple sensory elements
257 embedded in a 2D surface. You have complete control over the
258 distribution of these sensor elements through the use of simple
259 png image files. In particular, =CORTEX= implements more
260 comprehensive hearing than any other creature simulation system
261 available.
263 - =CORTEX= supports any number of creatures and any number of
264 senses. Time in =CORTEX= dialates so that the simulated creatures
265 always precieve a perfectly smooth flow of time, regardless of
266 the actual computational load.
268 =CORTEX= is built on top of =jMonkeyEngine3=, which is a video game
269 engine designed to create cross-platform 3D desktop games. =CORTEX=
270 is mainly written in clojure, a dialect of =LISP= that runs on the
271 java virtual machine (JVM). The API for creating and simulating
272 creatures and senses is entirely expressed in clojure, though many
273 senses are implemented at the layer of jMonkeyEngine or below. For
274 example, for the sense of hearing I use a layer of clojure code on
275 top of a layer of java JNI bindings that drive a layer of =C++=
276 code which implements a modified version of =OpenAL= to support
277 multiple listeners. =CORTEX= is the only simulation environment
278 that I know of that can support multiple entities that can each
279 hear the world from their own perspective. Other senses also
280 require a small layer of Java code. =CORTEX= also uses =bullet=, a
281 physics simulator written in =C=.
283 #+caption: Here is the worm from above modeled in Blender, a free
284 #+caption: 3D-modeling program. Senses and joints are described
285 #+caption: using special nodes in Blender.
286 #+name: worm-recognition-intro
287 #+ATTR_LaTeX: :width 12cm
288 [[./images/blender-worm.png]]
290 Here are some thing I anticipate that =CORTEX= might be used for:
292 - exploring new ideas about sensory integration
293 - distributed communication among swarm creatures
294 - self-learning using free exploration,
295 - evolutionary algorithms involving creature construction
296 - exploration of exoitic senses and effectors that are not possible
297 in the real world (such as telekenisis or a semantic sense)
298 - imagination using subworlds
300 During one test with =CORTEX=, I created 3,000 creatures each with
301 their own independent senses and ran them all at only 1/80 real
302 time. In another test, I created a detailed model of my own hand,
303 equipped with a realistic distribution of touch (more sensitive at
304 the fingertips), as well as eyes and ears, and it ran at around 1/4
305 real time.
307 #+BEGIN_LaTeX
308 \begin{sidewaysfigure}
309 \includegraphics[width=9.5in]{images/full-hand.png}
310 \caption{
311 I modeled my own right hand in Blender and rigged it with all the
312 senses that {\tt CORTEX} supports. My simulated hand has a
313 biologically inspired distribution of touch sensors. The senses are
314 displayed on the right, and the simulation is displayed on the
315 left. Notice that my hand is curling its fingers, that it can see
316 its own finger from the eye in its palm, and that it can feel its
317 own thumb touching its palm.}
318 \end{sidewaysfigure}
319 #+END_LaTeX
321 ** Contributions
323 - I built =CORTEX=, a comprehensive platform for embodied AI
324 experiments. =CORTEX= supports many features lacking in other
325 systems, such proper simulation of hearing. It is easy to create
326 new =CORTEX= creatures using Blender, a free 3D modeling program.
328 - I built =EMPATH=, which uses =CORTEX= to identify the actions of
329 a worm-like creature using a computational model of empathy.
331 * Building =CORTEX=
333 ** To explore embodiment, we need a world, body, and senses
335 ** Because of Time, simulation is perferable to reality
337 ** Video game engines are a great starting point
339 ** Bodies are composed of segments connected by joints
341 ** Eyes reuse standard video game components
343 ** Hearing is hard; =CORTEX= does it right
345 ** Touch uses hundreds of hair-like elements
347 ** Proprioception is the sense that makes everything ``real''
349 ** Muscles are both effectors and sensors
351 ** =CORTEX= brings complex creatures to life!
353 ** =CORTEX= enables many possiblities for further research
355 * Empathy in a simulated worm
357 Here I develop a computational model of empathy, using =CORTEX= as a
358 base. Empathy in this context is the ability to observe another
359 creature and infer what sorts of sensations that creature is
360 feeling. My empathy algorithm involves multiple phases. First is
361 free-play, where the creature moves around and gains sensory
362 experience. From this experience I construct a representation of the
363 creature's sensory state space, which I call \Phi-space. Using
364 \Phi-space, I construct an efficient function which takes the
365 limited data that comes from observing another creature and enriches
366 it full compliment of imagined sensory data. I can then use the
367 imagined sensory data to recognize what the observed creature is
368 doing and feeling, using straightforward embodied action predicates.
369 This is all demonstrated with using a simple worm-like creature, and
370 recognizing worm-actions based on limited data.
372 #+caption: Here is the worm with which we will be working.
373 #+caption: It is composed of 5 segments. Each segment has a
374 #+caption: pair of extensor and flexor muscles. Each of the
375 #+caption: worm's four joints is a hinge joint which allows
376 #+caption: about 30 degrees of rotation to either side. Each segment
377 #+caption: of the worm is touch-capable and has a uniform
378 #+caption: distribution of touch sensors on each of its faces.
379 #+caption: Each joint has a proprioceptive sense to detect
380 #+caption: relative positions. The worm segments are all the
381 #+caption: same except for the first one, which has a much
382 #+caption: higher weight than the others to allow for easy
383 #+caption: manual motor control.
384 #+name: basic-worm-view
385 #+ATTR_LaTeX: :width 10cm
386 [[./images/basic-worm-view.png]]
388 #+caption: Program for reading a worm from a blender file and
389 #+caption: outfitting it with the senses of proprioception,
390 #+caption: touch, and the ability to move, as specified in the
391 #+caption: blender file.
392 #+name: get-worm
393 #+begin_listing clojure
394 #+begin_src clojure
395 (defn worm []
396 (let [model (load-blender-model "Models/worm/worm.blend")]
397 {:body (doto model (body!))
398 :touch (touch! model)
399 :proprioception (proprioception! model)
400 :muscles (movement! model)}))
401 #+end_src
402 #+end_listing
404 ** Embodiment factors action recognition into managable parts
406 Using empathy, I divide the problem of action recognition into a
407 recognition process expressed in the language of a full compliment
408 of senses, and an imaganitive process that generates full sensory
409 data from partial sensory data. Splitting the action recognition
410 problem in this manner greatly reduces the total amount of work to
411 recognize actions: The imaganitive process is mostly just matching
412 previous experience, and the recognition process gets to use all
413 the senses to directly describe any action.
415 ** Action recognition is easy with a full gamut of senses
417 Embodied representations using multiple senses such as touch,
418 proprioception, and muscle tension turns out be be exceedingly
419 efficient at describing body-centered actions. It is the ``right
420 language for the job''. For example, it takes only around 5 lines
421 of LISP code to describe the action of ``curling'' using embodied
422 primitives. It takes about 10 lines to describe the seemingly
423 complicated action of wiggling.
425 The following action predicates each take a stream of sensory
426 experience, observe however much of it they desire, and decide
427 whether the worm is doing the action they describe. =curled?=
428 relies on proprioception, =resting?= relies on touch, =wiggling?=
429 relies on a fourier analysis of muscle contraction, and
430 =grand-circle?= relies on touch and reuses =curled?= as a gaurd.
432 #+caption: Program for detecting whether the worm is curled. This is the
433 #+caption: simplest action predicate, because it only uses the last frame
434 #+caption: of sensory experience, and only uses proprioceptive data. Even
435 #+caption: this simple predicate, however, is automatically frame
436 #+caption: independent and ignores vermopomorphic differences such as
437 #+caption: worm textures and colors.
438 #+name: curled
439 #+begin_listing clojure
440 #+begin_src clojure
441 (defn curled?
442 "Is the worm curled up?"
443 [experiences]
444 (every?
445 (fn [[_ _ bend]]
446 (> (Math/sin bend) 0.64))
447 (:proprioception (peek experiences))))
448 #+end_src
449 #+end_listing
451 #+caption: Program for summarizing the touch information in a patch
452 #+caption: of skin.
453 #+name: touch-summary
454 #+begin_listing clojure
455 #+begin_src clojure
456 (defn contact
457 "Determine how much contact a particular worm segment has with
458 other objects. Returns a value between 0 and 1, where 1 is full
459 contact and 0 is no contact."
460 [touch-region [coords contact :as touch]]
461 (-> (zipmap coords contact)
462 (select-keys touch-region)
463 (vals)
464 (#(map first %))
465 (average)
466 (* 10)
467 (- 1)
468 (Math/abs)))
469 #+end_src
470 #+end_listing
473 #+caption: Program for detecting whether the worm is at rest. This program
474 #+caption: uses a summary of the tactile information from the underbelly
475 #+caption: of the worm, and is only true if every segment is touching the
476 #+caption: floor. Note that this function contains no references to
477 #+caption: proprioction at all.
478 #+name: resting
479 #+begin_listing clojure
480 #+begin_src clojure
481 (def worm-segment-bottom (rect-region [8 15] [14 22]))
483 (defn resting?
484 "Is the worm resting on the ground?"
485 [experiences]
486 (every?
487 (fn [touch-data]
488 (< 0.9 (contact worm-segment-bottom touch-data)))
489 (:touch (peek experiences))))
490 #+end_src
491 #+end_listing
493 #+caption: Program for detecting whether the worm is curled up into a
494 #+caption: full circle. Here the embodied approach begins to shine, as
495 #+caption: I am able to both use a previous action predicate (=curled?=)
496 #+caption: as well as the direct tactile experience of the head and tail.
497 #+name: grand-circle
498 #+begin_listing clojure
499 #+begin_src clojure
500 (def worm-segment-bottom-tip (rect-region [15 15] [22 22]))
502 (def worm-segment-top-tip (rect-region [0 15] [7 22]))
504 (defn grand-circle?
505 "Does the worm form a majestic circle (one end touching the other)?"
506 [experiences]
507 (and (curled? experiences)
508 (let [worm-touch (:touch (peek experiences))
509 tail-touch (worm-touch 0)
510 head-touch (worm-touch 4)]
511 (and (< 0.55 (contact worm-segment-bottom-tip tail-touch))
512 (< 0.55 (contact worm-segment-top-tip head-touch))))))
513 #+end_src
514 #+end_listing
517 #+caption: Program for detecting whether the worm has been wiggling for
518 #+caption: the last few frames. It uses a fourier analysis of the muscle
519 #+caption: contractions of the worm's tail to determine wiggling. This is
520 #+caption: signigicant because there is no particular frame that clearly
521 #+caption: indicates that the worm is wiggling --- only when multiple frames
522 #+caption: are analyzed together is the wiggling revealed. Defining
523 #+caption: wiggling this way also gives the worm an opportunity to learn
524 #+caption: and recognize ``frustrated wiggling'', where the worm tries to
525 #+caption: wiggle but can't. Frustrated wiggling is very visually different
526 #+caption: from actual wiggling, but this definition gives it to us for free.
527 #+name: wiggling
528 #+begin_listing clojure
529 #+begin_src clojure
530 (defn fft [nums]
531 (map
532 #(.getReal %)
533 (.transform
534 (FastFourierTransformer. DftNormalization/STANDARD)
535 (double-array nums) TransformType/FORWARD)))
537 (def indexed (partial map-indexed vector))
539 (defn max-indexed [s]
540 (first (sort-by (comp - second) (indexed s))))
542 (defn wiggling?
543 "Is the worm wiggling?"
544 [experiences]
545 (let [analysis-interval 0x40]
546 (when (> (count experiences) analysis-interval)
547 (let [a-flex 3
548 a-ex 2
549 muscle-activity
550 (map :muscle (vector:last-n experiences analysis-interval))
551 base-activity
552 (map #(- (% a-flex) (% a-ex)) muscle-activity)]
553 (= 2
554 (first
555 (max-indexed
556 (map #(Math/abs %)
557 (take 20 (fft base-activity))))))))))
558 #+end_src
559 #+end_listing
561 With these action predicates, I can now recognize the actions of
562 the worm while it is moving under my control and I have access to
563 all the worm's senses.
565 #+caption: Use the action predicates defined earlier to report on
566 #+caption: what the worm is doing while in simulation.
567 #+name: report-worm-activity
568 #+begin_listing clojure
569 #+begin_src clojure
570 (defn debug-experience
571 [experiences text]
572 (cond
573 (grand-circle? experiences) (.setText text "Grand Circle")
574 (curled? experiences) (.setText text "Curled")
575 (wiggling? experiences) (.setText text "Wiggling")
576 (resting? experiences) (.setText text "Resting")))
577 #+end_src
578 #+end_listing
580 #+caption: Using =debug-experience=, the body-centered predicates
581 #+caption: work together to classify the behaviour of the worm.
582 #+caption: the predicates are operating with access to the worm's
583 #+caption: full sensory data.
584 #+name: basic-worm-view
585 #+ATTR_LaTeX: :width 10cm
586 [[./images/worm-identify-init.png]]
588 These action predicates satisfy the recognition requirement of an
589 empathic recognition system. There is power in the simplicity of
590 the action predicates. They describe their actions without getting
591 confused in visual details of the worm. Each one is frame
592 independent, but more than that, they are each indepent of
593 irrelevant visual details of the worm and the environment. They
594 will work regardless of whether the worm is a different color or
595 hevaily textured, or if the environment has strange lighting.
597 The trick now is to make the action predicates work even when the
598 sensory data on which they depend is absent. If I can do that, then
599 I will have gained much,
601 ** \Phi-space describes the worm's experiences
603 As a first step towards building empathy, I need to gather all of
604 the worm's experiences during free play. I use a simple vector to
605 store all the experiences.
607 Each element of the experience vector exists in the vast space of
608 all possible worm-experiences. Most of this vast space is actually
609 unreachable due to physical constraints of the worm's body. For
610 example, the worm's segments are connected by hinge joints that put
611 a practical limit on the worm's range of motions without limiting
612 its degrees of freedom. Some groupings of senses are impossible;
613 the worm can not be bent into a circle so that its ends are
614 touching and at the same time not also experience the sensation of
615 touching itself.
617 As the worm moves around during free play and its experience vector
618 grows larger, the vector begins to define a subspace which is all
619 the sensations the worm can practicaly experience during normal
620 operation. I call this subspace \Phi-space, short for
621 physical-space. The experience vector defines a path through
622 \Phi-space. This path has interesting properties that all derive
623 from physical embodiment. The proprioceptive components are
624 completely smooth, because in order for the worm to move from one
625 position to another, it must pass through the intermediate
626 positions. The path invariably forms loops as actions are repeated.
627 Finally and most importantly, proprioception actually gives very
628 strong inference about the other senses. For example, when the worm
629 is flat, you can infer that it is touching the ground and that its
630 muscles are not active, because if the muscles were active, the
631 worm would be moving and would not be perfectly flat. In order to
632 stay flat, the worm has to be touching the ground, or it would
633 again be moving out of the flat position due to gravity. If the
634 worm is positioned in such a way that it interacts with itself,
635 then it is very likely to be feeling the same tactile feelings as
636 the last time it was in that position, because it has the same body
637 as then. If you observe multiple frames of proprioceptive data,
638 then you can become increasingly confident about the exact
639 activations of the worm's muscles, because it generally takes a
640 unique combination of muscle contractions to transform the worm's
641 body along a specific path through \Phi-space.
643 There is a simple way of taking \Phi-space and the total ordering
644 provided by an experience vector and reliably infering the rest of
645 the senses.
647 ** Empathy is the process of tracing though \Phi-space
649 Here is the core of a basic empathy algorithm, starting with an
650 experience vector:
652 First, group the experiences into tiered proprioceptive bins. I use
653 powers of 10 and 3 bins, and the smallest bin has an approximate
654 size of 0.001 radians in all proprioceptive dimensions.
656 Then, given a sequence of proprioceptive input, generate a set of
657 matching experience records for each input, using the tiered
658 proprioceptive bins.
660 Finally, to infer sensory data, select the longest consective chain
661 of experiences. Conecutive experience means that the experiences
662 appear next to each other in the experience vector.
664 This algorithm has three advantages:
666 1. It's simple
668 3. It's very fast -- retrieving possible interpretations takes
669 constant time. Tracing through chains of interpretations takes
670 time proportional to the average number of experiences in a
671 proprioceptive bin. Redundant experiences in \Phi-space can be
672 merged to save computation.
674 2. It protects from wrong interpretations of transient ambiguous
675 proprioceptive data. For example, if the worm is flat for just
676 an instant, this flattness will not be interpreted as implying
677 that the worm has its muscles relaxed, since the flattness is
678 part of a longer chain which includes a distinct pattern of
679 muscle activation. Markov chains or other memoryless statistical
680 models that operate on individual frames may very well make this
681 mistake.
683 #+caption: Program to convert an experience vector into a
684 #+caption: proprioceptively binned lookup function.
685 #+name: bin
686 #+begin_listing clojure
687 #+begin_src clojure
688 (defn bin [digits]
689 (fn [angles]
690 (->> angles
691 (flatten)
692 (map (juxt #(Math/sin %) #(Math/cos %)))
693 (flatten)
694 (mapv #(Math/round (* % (Math/pow 10 (dec digits))))))))
696 (defn gen-phi-scan
697 "Nearest-neighbors with binning. Only returns a result if
698 the propriceptive data is within 10% of a previously recorded
699 result in all dimensions."
700 [phi-space]
701 (let [bin-keys (map bin [3 2 1])
702 bin-maps
703 (map (fn [bin-key]
704 (group-by
705 (comp bin-key :proprioception phi-space)
706 (range (count phi-space)))) bin-keys)
707 lookups (map (fn [bin-key bin-map]
708 (fn [proprio] (bin-map (bin-key proprio))))
709 bin-keys bin-maps)]
710 (fn lookup [proprio-data]
711 (set (some #(% proprio-data) lookups)))))
712 #+end_src
713 #+end_listing
715 #+caption: =longest-thread= finds the longest path of consecutive
716 #+caption: experiences to explain proprioceptive worm data.
717 #+name: phi-space-history-scan
718 #+ATTR_LaTeX: :width 10cm
719 [[./images/aurellem-gray.png]]
721 =longest-thread= infers sensory data by stitching together pieces
722 from previous experience. It prefers longer chains of previous
723 experience to shorter ones. For example, during training the worm
724 might rest on the ground for one second before it performs its
725 excercises. If during recognition the worm rests on the ground for
726 five seconds, =longest-thread= will accomodate this five second
727 rest period by looping the one second rest chain five times.
729 =longest-thread= takes time proportinal to the average number of
730 entries in a proprioceptive bin, because for each element in the
731 starting bin it performes a series of set lookups in the preceeding
732 bins. If the total history is limited, then this is only a constant
733 multiple times the number of entries in the starting bin. This
734 analysis also applies even if the action requires multiple longest
735 chains -- it's still the average number of entries in a
736 proprioceptive bin times the desired chain length. Because
737 =longest-thread= is so efficient and simple, I can interpret
738 worm-actions in real time.
740 #+caption: Program to calculate empathy by tracing though \Phi-space
741 #+caption: and finding the longest (ie. most coherent) interpretation
742 #+caption: of the data.
743 #+name: longest-thread
744 #+begin_listing clojure
745 #+begin_src clojure
746 (defn longest-thread
747 "Find the longest thread from phi-index-sets. The index sets should
748 be ordered from most recent to least recent."
749 [phi-index-sets]
750 (loop [result '()
751 [thread-bases & remaining :as phi-index-sets] phi-index-sets]
752 (if (empty? phi-index-sets)
753 (vec result)
754 (let [threads
755 (for [thread-base thread-bases]
756 (loop [thread (list thread-base)
757 remaining remaining]
758 (let [next-index (dec (first thread))]
759 (cond (empty? remaining) thread
760 (contains? (first remaining) next-index)
761 (recur
762 (cons next-index thread) (rest remaining))
763 :else thread))))
764 longest-thread
765 (reduce (fn [thread-a thread-b]
766 (if (> (count thread-a) (count thread-b))
767 thread-a thread-b))
768 '(nil)
769 threads)]
770 (recur (concat longest-thread result)
771 (drop (count longest-thread) phi-index-sets))))))
772 #+end_src
773 #+end_listing
775 There is one final piece, which is to replace missing sensory data
776 with a best-guess estimate. While I could fill in missing data by
777 using a gradient over the closest known sensory data points,
778 averages can be misleading. It is certainly possible to create an
779 impossible sensory state by averaging two possible sensory states.
780 Therefore, I simply replicate the most recent sensory experience to
781 fill in the gaps.
783 #+caption: Fill in blanks in sensory experience by replicating the most
784 #+caption: recent experience.
785 #+name: infer-nils
786 #+begin_listing clojure
787 #+begin_src clojure
788 (defn infer-nils
789 "Replace nils with the next available non-nil element in the
790 sequence, or barring that, 0."
791 [s]
792 (loop [i (dec (count s))
793 v (transient s)]
794 (if (zero? i) (persistent! v)
795 (if-let [cur (v i)]
796 (if (get v (dec i) 0)
797 (recur (dec i) v)
798 (recur (dec i) (assoc! v (dec i) cur)))
799 (recur i (assoc! v i 0))))))
800 #+end_src
801 #+end_listing
803 ** Efficient action recognition with =EMPATH=
805 To use =EMPATH= with the worm, I first need to gather a set of
806 experiences from the worm that includes the actions I want to
807 recognize. The =generate-phi-space= program (listint
808 \ref{generate-phi-space} runs the worm through a series of
809 exercices and gatheres those experiences into a vector. The
810 =do-all-the-things= program is a routine expressed in a simple
811 muscle contraction script language for automated worm control.
813 #+caption: Program to gather the worm's experiences into a vector for
814 #+caption: further processing. The =motor-control-program= line uses
815 #+caption: a motor control script that causes the worm to execute a series
816 #+caption: of ``exercices'' that include all the action predicates.
817 #+name: generate-phi-space
818 #+attr_latex: [!H]
819 #+begin_listing clojure
820 #+begin_src clojure
821 (def do-all-the-things
822 (concat
823 curl-script
824 [[300 :d-ex 40]
825 [320 :d-ex 0]]
826 (shift-script 280 (take 16 wiggle-script))))
828 (defn generate-phi-space []
829 (let [experiences (atom [])]
830 (run-world
831 (apply-map
832 worm-world
833 (merge
834 (worm-world-defaults)
835 {:end-frame 700
836 :motor-control
837 (motor-control-program worm-muscle-labels do-all-the-things)
838 :experiences experiences})))
839 @experiences))
840 #+end_src
841 #+end_listing
843 #+caption: Use longest thread and a phi-space generated from a short
844 #+caption: exercise routine to interpret actions during free play.
845 #+name: empathy-debug
846 #+begin_listing clojure
847 #+begin_src clojure
848 (defn init []
849 (def phi-space (generate-phi-space))
850 (def phi-scan (gen-phi-scan phi-space)))
852 (defn empathy-demonstration []
853 (let [proprio (atom ())]
854 (fn
855 [experiences text]
856 (let [phi-indices (phi-scan (:proprioception (peek experiences)))]
857 (swap! proprio (partial cons phi-indices))
858 (let [exp-thread (longest-thread (take 300 @proprio))
859 empathy (mapv phi-space (infer-nils exp-thread))]
860 (println-repl (vector:last-n exp-thread 22))
861 (cond
862 (grand-circle? empathy) (.setText text "Grand Circle")
863 (curled? empathy) (.setText text "Curled")
864 (wiggling? empathy) (.setText text "Wiggling")
865 (resting? empathy) (.setText text "Resting")
866 :else (.setText text "Unknown")))))))
868 (defn empathy-experiment [record]
869 (.start (worm-world :experience-watch (debug-experience-phi)
870 :record record :worm worm*)))
871 #+end_src
872 #+end_listing
874 The result of running =empathy-experiment= is that the system is
875 generally able to interpret worm actions using the action-predicates
876 on simulated sensory data just as well as with actual data. Figure
877 \ref{empathy-debug-image} was generated using =empathy-experiment=:
879 #+caption: From only proprioceptive data, =EMPATH= was able to infer
880 #+caption: the complete sensory experience and classify four poses
881 #+caption: (The last panel shows a composite image of \emph{wriggling},
882 #+caption: a dynamic pose.)
883 #+name: empathy-debug-image
884 #+ATTR_LaTeX: :width 10cm :placement [H]
885 [[./images/empathy-1.png]]
887 One way to measure the performance of =EMPATH= is to compare the
888 sutiability of the imagined sense experience to trigger the same
889 action predicates as the real sensory experience.
891 #+caption: Determine how closely empathy approximates actual
892 #+caption: sensory data.
893 #+name: test-empathy-accuracy
894 #+begin_listing clojure
895 #+begin_src clojure
896 (def worm-action-label
897 (juxt grand-circle? curled? wiggling?))
899 (defn compare-empathy-with-baseline [matches]
900 (let [proprio (atom ())]
901 (fn
902 [experiences text]
903 (let [phi-indices (phi-scan (:proprioception (peek experiences)))]
904 (swap! proprio (partial cons phi-indices))
905 (let [exp-thread (longest-thread (take 300 @proprio))
906 empathy (mapv phi-space (infer-nils exp-thread))
907 experience-matches-empathy
908 (= (worm-action-label experiences)
909 (worm-action-label empathy))]
910 (println-repl experience-matches-empathy)
911 (swap! matches #(conj % experience-matches-empathy)))))))
913 (defn accuracy [v]
914 (float (/ (count (filter true? v)) (count v))))
916 (defn test-empathy-accuracy []
917 (let [res (atom [])]
918 (run-world
919 (worm-world :experience-watch
920 (compare-empathy-with-baseline res)
921 :worm worm*))
922 (accuracy @res)))
923 #+end_src
924 #+end_listing
926 Running =test-empathy-accuracy= using the very short exercise
927 program defined in listing \ref{generate-phi-space}, and then doing
928 a similar pattern of activity manually yeilds an accuracy of around
929 73%. This is based on very limited worm experience. By training the
930 worm for longer, the accuracy dramatically improves.
932 #+caption: Program to generate \Phi-space using manual training.
933 #+name: manual-phi-space
934 #+begin_listing clojure
935 #+begin_src clojure
936 (defn init-interactive []
937 (def phi-space
938 (let [experiences (atom [])]
939 (run-world
940 (apply-map
941 worm-world
942 (merge
943 (worm-world-defaults)
944 {:experiences experiences})))
945 @experiences))
946 (def phi-scan (gen-phi-scan phi-space)))
947 #+end_src
948 #+end_listing
950 After about 1 minute of manual training, I was able to achieve 95%
951 accuracy on manual testing of the worm using =init-interactive= and
952 =test-empathy-accuracy=. The ability of the system to infer sensory
953 states is truly impressive.
955 ** Digression: bootstrapping touch using free exploration
957 * Contributions
962 # An anatomical joke:
963 # - Training
964 # - Skeletal imitation
965 # - Sensory fleshing-out
966 # - Classification