3.18. Robot SimulationΒΆ

In [1]:
from jyro.simulator import (Robot, Pioneer, Pioneer16Sonars,
                            PioneerFrontLightSensors, Camera,
                            VSimulator, DepthCamera)
import random
import numpy as np
In [11]:
robot = Pioneer("Pioneer", 3.5, 2, 0)
robot.addDevice(Pioneer16Sonars())
robot.addDevice(DepthCamera(4))
light_sensors = PioneerFrontLightSensors(3.0)
light_sensors.lightMode = 'ambient'
robot.addDevice(light_sensors)
Out[11]:
_images/Robot_Simulation_2_0.svg
In [12]:
def worldf(physics):
    physics.addBox(0, 0, 4, 4, fill="backgroundgreen", wallcolor="gray")
    physics.addLight(2, 0.75, 1.0) # increased brightness for new linear version of lights
In [13]:
sim = VSimulator(robot, worldf)
In [18]:
camera = robot.device["camera"]
In [19]:
image = camera.getImage()
image
Out[19]:
_images/Robot_Simulation_6_0.png
In [20]:
image.size
Out[20]:
(60, 40)
In [21]:
data = camera.getData()
data.shape
Out[21]:
(40, 60, 3)
In [23]:
robot.move(0.50, 0.35)
In [24]:
sim.step()
In [25]:
def random_action():
    """Generate a random action from a limited set of possible settings"""
    possible = [-1.0, -0.5, 0.0, 0.5, 1.0]
    return [random.choice(possible), random.choice(possible)]

def get_senses(robot):
    light = robot["light"].getData()
    sonar = [v/3.0 for v in robot["sonar"].getData()]
    camera = robot["camera"].getData()
    return [light, sonar, camera]
In [26]:
senses = get_senses(robot)
list(map(len, senses))
Out[26]:
[2, 16, 40]
In [27]:
def brain(robot):
    senses = get_senses(robot)
    net.propagate(senses)
    translate, rotate = random_action()
    #self.move(translate, rotate)
    robot.move(0.50, 0.35)
In [28]:
robot.brain = brain
In [29]:
from conx import Network, Layer, FlattenLayer, SGD, ImageLayer, Conv2DLayer
import numpy as np
Using Theano backend.
conx, version 3.4.3
In [55]:
net = Network("Robot Prediction Network")
net.add(Layer("light", 2))
net.add(Layer("sonar", 16))
net.add(ImageLayer("camera", (40,60), 3))
net.add(FlattenLayer("flatten"))
net.add(Conv2DLayer("conv", 16, (3,3)))
net.add(Layer("hidden", 50, activation="relu"))
net.add(Layer("output1", 2, activation="sigmoid"))
net.add(Layer("hidden2", 5, activation="sigmoid"))
net.add(Layer("hidden3", 10, activation="sigmoid", dropout=0.25))
net.add(Layer("hidden4", 10, activation="sigmoid"))
net.add(Layer("output2", 5, activation="sigmoid"))
In [56]:
net.connect("sonar", "hidden2")
net.connect("light", "hidden")
net.connect("camera", "conv")
net.connect("conv", "flatten")
net.connect("flatten", "hidden2")
net.connect("hidden", "hidden2")
net.connect("hidden2", "hidden3")
##net.connect("hidden2", "output2")
net.connect("hidden3", "output2")
net.connect("hidden3", "hidden4")
net.connect("hidden4", "output1")
In [57]:
net.compile(optimizer="adam", error="mse")
#net.config["hspace"] = 200
In [58]:
net
Out[58]:
Robot Prediction NetworkLayer: output1 (output) shape = (2,) Keras class = Dense activation = sigmoidoutput1Layer: output2 (output) shape = (5,) Keras class = Dense activation = sigmoidoutput2Weights from hidden4 to output1 output1/kernel has shape (10, 2) output1/bias has shape (2,)Layer: hidden4 (hidden) shape = (10,) Keras class = Dense activation = sigmoidhidden4Weights from hidden3 to output2 output2/kernel has shape (10, 5) output2/bias has shape (5,)Weights from hidden3 to output2 output2/kernel has shape (10, 5) output2/bias has shape (5,)Weights from hidden3 to hidden4 hidden4/kernel has shape (10, 10) hidden4/bias has shape (10,)Layer: hidden3 (hidden) shape = (10,) dropout = 0.25 Keras class = Dense activation = sigmoidhidden3Weights from hidden2 to hidden3 hidden3/kernel has shape (5, 10) hidden3/bias has shape (10,)Layer: hidden2 (hidden) shape = (5,) Keras class = Dense activation = sigmoidhidden2Weights from hidden to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Weights from sonar to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Weights from flatten to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Layer: flatten (hidden) Keras class = FlattenflattenWeights from hidden to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Layer: hidden (hidden) shape = (50,) Keras class = Dense activation = reluhiddenWeights from sonar to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Weights from conv to flattenLayer: conv (hidden) Keras class = Conv2Dconv160Weights from light to hidden hidden/kernel has shape (2, 50) hidden/bias has shape (50,)Layer: light (input) shape = (2,) Keras class = InputlightWeights from sonar to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Layer: sonar (input) shape = (16,) Keras class = InputsonarWeights from camera to conv conv/kernel has shape (3, 3, 3, 16) conv/bias has shape (16,)Layer: camera (input) shape = (40, 60, 3) Keras class = Inputcamera
In [59]:
matrix = net.propagate_to("conv", get_senses(robot))
In [60]:
net["conv"].feature = 6
In [61]:
net.propagate_to_features("conv", get_senses(robot), scale=3)
Out[61]:

Feature 0

Feature 1

Feature 2

Feature 3

Feature 4

Feature 5

Feature 6

Feature 7

Feature 8

Feature 9

Feature 10

Feature 11

Feature 12

Feature 13

Feature 14

Feature 15
In [62]:
net.dataset.add([[0] * 2, [0] * 16, data], [[0] * 2, [1] + ([0] * 4)])
In [63]:
net.dashboard()
In [64]:
net
Out[64]:
Robot Prediction NetworkLayer: output1 (output) shape = (2,) Keras class = Dense activation = sigmoidoutput1Layer: output2 (output) shape = (5,) Keras class = Dense activation = sigmoidoutput2Weights from hidden4 to output1 output1/kernel has shape (10, 2) output1/bias has shape (2,)Layer: hidden4 (hidden) shape = (10,) Keras class = Dense activation = sigmoidhidden4Weights from hidden3 to output2 output2/kernel has shape (10, 5) output2/bias has shape (5,)Weights from hidden3 to output2 output2/kernel has shape (10, 5) output2/bias has shape (5,)Weights from hidden3 to hidden4 hidden4/kernel has shape (10, 10) hidden4/bias has shape (10,)Layer: hidden3 (hidden) shape = (10,) dropout = 0.25 Keras class = Dense activation = sigmoidhidden3Weights from hidden2 to hidden3 hidden3/kernel has shape (5, 10) hidden3/bias has shape (10,)Layer: hidden2 (hidden) shape = (5,) Keras class = Dense activation = sigmoidhidden2Weights from hidden to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Weights from sonar to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Weights from flatten to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Layer: flatten (hidden) Keras class = FlattenflattenWeights from hidden to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Layer: hidden (hidden) shape = (50,) Keras class = Dense activation = reluhiddenWeights from sonar to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Weights from conv to flattenLayer: conv (hidden) Keras class = Conv2Dconv166Weights from light to hidden hidden/kernel has shape (2, 50) hidden/bias has shape (50,)Layer: light (input) shape = (2,) Keras class = InputlightWeights from sonar to hidden2 hidden2/kernel has shape (35330, 5) hidden2/bias has shape (5,)Layer: sonar (input) shape = (16,) Keras class = InputsonarWeights from camera to conv conv/kernel has shape (3, 3, 3, 16) conv/bias has shape (16,)Layer: camera (input) shape = (40, 60, 3) Keras class = Inputcamera
In [65]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1000...
Total count: 1
      correct: 0
      incorrect: 1
Total percentage correct: 0.0
In [71]:
net.train(epochs=100, plot=True)
_images/Robot_Simulation_27_0.svg
========================================================================
       |  Training |   output1 |   output2
Epochs |     Error |       acc |       acc
------ | --------- | --------- | ---------
#  310 |   0.07325 |   0.00000 |   0.00000
In [72]:
net.test(show=True)
========================================================
Testing validation dataset with tolerance 0.1000...
# | inputs | targets | outputs | result
---------------------------------------
0 | [[0.00,0.00],[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00, 0.00],[[[1.00,1.00,1.00],  [1.00,1.00,1.00],  [1.00,1.00,1.00],  ...,   [1.00,1.00,1.00],  [1.00,1.00,1.00],  [1.00,1.00,1.00]], [[0.95,0.95,0.95],  [0.95,0.95,0.95],  [0.95,0.95,0.95],  ...,   [0.95,0.95,0.95],  [0.95,0.95,0.95],  [0.95,0.95,0.95]], [[0.90,0.90,0.90],  [0.90,0.90,0.90],  [0.90,0.90,0.90],  ...,   [0.90,0.90,0.90],  [0.90,0.90,0.90],  [0.90,0.90,0.90]], ...,  [[0.85,0.85,0.85],  [0.85,0.85,0.85],  [0.85,0.85,0.85],  ...,   [0.85,0.85,0.85],  [0.85,0.85,0.85],  [0.85,0.85,0.85]], [[0.90,0.90,0.90],  [0.90,0.90,0.90],  [0.90,0.90,0.90],  ...,   [0.90,0.90,0.90],  [0.90,0.90,0.90],  [0.90,0.90,0.90]], [[0.95,0.95,0.95],  [0.95,0.95,0.95],  [0.95,0.95,0.95],  ...,   [0.95,0.95,0.95],  [0.95,0.95,0.95],  [0.95,0.95,0.95]]]] | [[0.00,0.00],[1.00,0.00,0.00,0.00,0.00]] | [[0.11,0.20],[0.75,0.22,0.25,0.21,0.23]] | X
Total count: 1
      correct: 0
      incorrect: 1
Total percentage correct: 0.0