Exemplo n.º 1
0
 def new_episode(self):
     self._make_sure_inited()
     # initialize the state
     rgb = self.images.next()
     # TODO: reshape
     self.y = UncertainImage(rgb)
     id_environment = self.id_image_stream
     id_episode = unique_timestamp_string()
     self.timestamp = time.time()
     self.commands = np.array([0], dtype='int')
     self.commands_source = 'rest'
     return EpisodeDesc(id_episode, id_environment, extra=None)
Exemplo n.º 2
0
 def obs2ui(self, y):
     """ Converts observatiosn from outside in an UncertainImage
         of the right dimensions to deal with this DDS. """
     shape = self.discdds.get_shape()
     y = (y * 255).astype('uint8')
     y = resize(y, width=shape[1], height=shape[0])
     ui = UncertainImage(y)
     return ui
Exemplo n.º 3
0
    def update(self):
        rgb0 = self.input.rgb
        H, W = self.discdds.get_shape()
        rgb = resize(rgb0, width=W, height=H)

        y0 = UncertainImage(rgb)
        y1 = self.action.predict(y0)
        pred = y1.get_rgba_fill()

        pred2 = resize(pred, height=rgb0.shape[0], width=rgb0.shape[1])
        self.output.prediction = pred2[:, :, :3]
Exemplo n.º 4
0
def make_logcases(id_stream, n, delta, id_tcs, id_discdds, discdds, seed):

    stream = get_conftools_streams().instance(id_stream)
    stream_data = stream.read_all()
    miniplans = iterate_testcases(stream_data, delta)
    sampled = reservoir_sample(miniplans, N=n, seed=seed)

    for i, m in enumerate(sampled):
        id_tc = id_tcs[i]
        I0 = UncertainImage(m.y0)
        I1 = UncertainImage(m.y1)
        assert len(m.u) == delta
        plan = discdds.commands_to_indices(m.u)
        tc = TestCase(id_tc=id_tc,
                      id_discdds=id_discdds,
                      y0=I0,
                      y1=I1,
                      true_plan=plan)
        # TODO: add field desc
        # desc = 'Sampled from stream %s, i=%s for seed %s.' % (i, seed)
        yield tc
Exemplo n.º 5
0
def test_distance_neighbor_dist2_repeated():
    # A constant pattern
    a = np.ones((100, 100, 3))

    # Let's get some translated slices
    w = 80
    N = 10
    A = []
    for i in range(N):
        A.append(a[i:i + w, 0:0 + w, ...])
    print()
    areas = [0.01, 0.03, 0.05, 0.07]
    Ds = [DistanceNeighborDist((a, a)) for a in areas]
    L2 = DistanceNorm(2)
    for i in range(N):
        y0 = UncertainImage(A[0])
        y1 = UncertainImage(A[i])
        vl2 = L2.distance(y0, y1)
        ds = [x.distance(y0, y1) for x in Ds]
        assert_allclose(ds, 0)  # < this must be zero
        s = ";".join("%1.5f" % x for x in ds)
        print('- step %d L2: %1.5f %s' % (i, vl2, s))
Exemplo n.º 6
0
def test_distance_neighbor_dist2_rgb():

    # A random pattern
    a = np.random.rand(100, 100, 3)

    # Let's get some translated slices
    w = 80
    N = 10
    A = []
    for i in range(N):
        A.append(a[i:i + w, 0:0 + w, ...])
    print()
    areas = [0.01, 0.03, 0.05, 0.07]
    Ds = [DistanceNeighborDist((a, a)) for a in areas]
    L2 = DistanceNorm(2)
    for i in range(N):
        y0 = UncertainImage(A[0])
        y1 = UncertainImage(A[i])
        vl2 = L2.distance(y0, y1)
        ds = [x.distance(y0, y1) for x in Ds]
        s = ";".join("%1.5f" % x for x in ds)
        print('- step %d L2: %1.5f %s' % (i, vl2, s))
Exemplo n.º 7
0
def compute_predstats(id_discdds, id_stream, delta, id_distances):
    dds = get_conftools_discdds().instance(id_discdds)
    stream = get_conftools_streams().instance(id_stream)
    distances_library = get_conftools_uncertain_image_distances()
    distances = dict(
        map(lambda x: (x, distances_library.instance(x)), id_distances))
    dtype = [(x, 'float32') for x in id_distances]

    results = []
    for logitem in iterate_testcases(stream.read_all(), delta):
        assert_allclose(len(logitem.u), delta)
        y0 = UncertainImage(logitem.y0)
        y1 = UncertainImage(logitem.y1)
        py0 = dds.predict(y0, dds.commands_to_indices(logitem.u))
        ds = []
        for name in id_distances:
            d = distances[name].distance(y1, py0)
            #  d0 = distances[name].distance(y1, y0)
            ds.append(d)

        a = np.array(tuple(ds), dtype=dtype)
        results.append(a)

    return results
Exemplo n.º 8
0
def discdds_make_test_cases(config, outdir, id_discdds, id_image, num_cases,
                            plan_length):
    # Get the DiffeoSystem
    discdds = config.discdds.instance(id_discdds)
    # Get the RGB image
    rgb = config.images.instance(id_image)

    shape = discdds.get_shape()
    rgb = resize(rgb, shape[1], shape[0])
    assert rgb.shape[0] == shape[0]
    assert rgb.shape[1] == shape[1]

    image = UncertainImage(rgb)

    for i in range(num_cases):
        tcname = 'tc_%s_%s_d%d_%03d' % (id_discdds, id_image, plan_length, i)
        discdds_make_test_case(outdir, tcname, id_discdds, discdds, image,
                               plan_length)
Exemplo n.º 9
0
    def update(self):
        rgb0 = self.input.rgb
        pad = self.config.pad

        H, W = self.discdds.get_shape()
        rgb = resize(rgb0, width=W, height=H)

        y0 = UncertainImage(rgb)

        uimages = [a.predict(y0) for a in self.actions]

        images = [x.get_rgba_fill()[..., :3] for x in uimages]
        images = [
            resize(x, height=rgb0.shape[0], width=rgb0.shape[1])
            for x in images
        ]
        canvas = make_images_grid(images, pad=pad)

        self.output.prediction = canvas
Exemplo n.º 10
0
    def go(self):
        outdir = self.options.output
        which = self.options.get_extra()

        diffeo2dds_config = get_diffeo2dds_config()

        if not which:
            todo = diffeo2dds_config.discdds.keys()
        else:
            todo = diffeo2dds_config.discdds.expand_names(which)

        id_image = self.options.image
        image = UncertainImage(diffeo2dds_config.images.instance(id_image))

        for id_dds in todo:
            self.info('Writing %s' % id_dds)
            dds = diffeo2dds_config.discdds.instance(id_dds)
            report = Report(id_dds)

            dds.display(report, image=image)

            write_report_files(report, basename=os.path.join(outdir, id_dds))
Exemplo n.º 11
0
class DiffeoSystemSimulation(RobotInterface):
    """ 
        Simulation of a DiffeoSystem as a RobotInterface.
    """
    
    @contract(image_stream='str|code_spec',
              discdds='str|code_spec')
    def __init__(self, image_stream, discdds):
        '''
        :param discdds: Which DDS dynamics to use.
        
        :param image_stream: What ImageStream to use. 
        Each image is used to start a new episode.
        
        
        '''
        diffeo2ddslearn_config = get_diffeo2ddslearn_config()
        self.id_image_stream, self.image_stream = \
            diffeo2ddslearn_config.image_streams.instance_smarter(image_stream)
        if self.id_image_stream is None:
            self.id_image_stream = 'image_stream'
            
        diffeo2dds_config = get_diffeo2dds_config() 
        self.id_discdds, self.discdds = \
            diffeo2dds_config.discdds.instance_smarter(discdds)

        if self.id_discdds is None:
            self.id_discdds = 'discdds'
            
        self.shape = None
    
    def _make_sure_inited(self):
        # don't do this in the constructor, because we cannot pickle
        # generators 
        if self.shape is not None:
            return
    
        # get the first image, look how it is to get shape
        self.images = self.image_stream.read_all() 
        x0 = self.images.next()
        self.shape = x0.shape[:2] 
            
        
    @contract(returns=BootSpec)
    def get_spec(self):
        self._make_sure_inited()
        ncmds = self.discdds.get_num_commands()
        cmd = make_streamels_finite_commands(ncommands=ncmds, default=0)
        cmd_spec = StreamSpec(id_stream=None, streamels=cmd, extra={})
        obs_spec = StreamSpec(id_stream=None,
                              streamels=make_streamels_rgb_float(self.shape),
                              extra={})
        return BootSpec(obs_spec, cmd_spec)
    
    @contract(returns=EpisodeDesc)
    def new_episode(self):
        self._make_sure_inited()
        # initialize the state
        rgb = self.images.next()
        # TODO: reshape
        self.y = UncertainImage(rgb)
        id_environment = self.id_image_stream
        id_episode = unique_timestamp_string()
        self.timestamp = time.time()
        self.commands = np.array([0], dtype='int')
        self.commands_source = 'rest'
        return EpisodeDesc(id_episode, id_environment, extra=None)
        
    @contract(commands='array', commands_source='str')
    def set_commands(self, commands, commands_source):  # @UnusedVariable
        u = int(commands[0])
        warnings.warn('understand what was going on')
        self.y = self.discdds.predict(self.y, plan=[u])
        self.commands = commands
        self.commands_source = commands_source
        self.timestamp += 1
        
    @contract(returns=RobotObservations)
    def get_observations(self):
        rgb = self.y.get_values()
        timestamp = self.timestamp 
        observations = rgb
        commands = self.commands
        commands_source = self.commands_source
        episode_end = False
        robot_pose = None
        obs = RobotObservations(timestamp, observations,
                                commands, commands_source,
                                episode_end, robot_pose)
        return obs