Exemplo n.º 1
0
def run_random(path):
    import deepirl.utils.vizualization as v
    env = EyetrackingEnv(path, actions_width=16, actions_height=16)

    wnd = v.Window(800, 600)
    state1_drawer = v.ImgPlotDrawer(v.Rect(10, 10, 200, 400))
    state2_drawer = v.ImgPlotDrawer(v.Rect(220, 10, 200, 400))
    wnd.add_drawer(state1_drawer)
    wnd.add_drawer(state2_drawer)

    state = env.reset()

    while not env.is_terminal():
        state1_drawer.set_value(state[..., 0])
        state2_drawer.set_value(state[..., 1])

        x = env.replay._memory[env._frame]['x']
        y = env.replay._memory[env._frame]['y']
        action = env.action_from_x_y(x, y)

        # action = np.random.choice(env.num_actions)
        # action = env.num_actions // 2
        state = env.do_action(action)

        wnd.draw()
Exemplo n.º 2
0
def _test_explain():
    import time
    import deepirl.utils.vizualization as v
    wnd = v.Window(640, 480)
    state_drawer1 = v.ImageDrawer(v.Rect(10, 10, 200, 200))
    state_drawer2 = v.ImageDrawer(v.Rect(220, 10, 200, 200),
                                  color_map=v.ColorMap.HOT)
    wnd.add_drawer(state_drawer1)
    wnd.add_drawer(state_drawer2)

    env = SymbolsEnvironment(32,
                             32,
                             actions_width=10,
                             actions_height=10,
                             fixations_per_symbol=10,
                             same_image=True,
                             seed=42)
    replay = generate_expert_trajectories(env,
                                          num_trajectories=10,
                                          capacity=100)

    while True:
        for state, action in replay.iterate():
            state_drawer1.img = state[..., 0]
            state_drawer2.img = state[..., 1]
            wnd.draw()
            time.sleep(0.01)
Exemplo n.º 3
0
def main():
    import deepirl.utils.vizualization as v
    wnd = v.Window(512, 512)
    image_drawer = v.ImageDrawer(v.Rect(0, 0, 512, 512))
    wnd.add_drawer(image_drawer)

    batch_size = 64
    d = Dist(batch_size)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())

        w = 128
        h = 128
        d.stats(sess)
        #d.plot(sess, w, h)

        for _ in range(10):
            for i in range(1000):
                print('Training: {}'.format(i))
                points = np.random.multivariate_normal(mean=[-0.5, 0.5],
                                                       cov=[[1.7, 0.0],
                                                            [0.0, 0.2]],
                                                       size=batch_size)
                d.train(sess, points)
                if i % 100 == 0:
                    image_drawer.set_value(d.plot(sess, w, h))
                    wnd.draw()
            d.stats(sess)
            #d.plot(sess, w, h)

        d.stats(sess)
        d.plot(sess, w, h)
Exemplo n.º 4
0
def main():
    import deepirl.utils.vizualization as v

    w = 1080
    h = 1920

    z_size = 10
    batch_size = 512
    image = np.zeros((h, w), dtype=np.float32)
    z = np.random.standard_normal((z_size,))
    z_batch = np.zeros((batch_size, z_size), np.float32)
    z_batch[:] = z
    pos_batch = np.zeros((batch_size, 3), np.float32)
    x_batch = np.zeros((batch_size,), np.uint16)
    y_batch = np.zeros((batch_size,), np.uint16)

    wnd = v.Window(w // 2, h // 2)
    image_drawer = v.ImageDrawer(v.Rect(0, 0, wnd.width, wnd.height))
    wnd.add_drawer(image_drawer)

    with tf.Session() as sess:
        g = Generator(z_num=z_size)
        sess.run(tf.global_variables_initializer())

        idx = 0
        for i in range(h):
            for j in range(w):
                pos_batch[idx] = get_pos(j, i, w, h)
                x_batch[idx] = i
                y_batch[idx] = j

                idx += 1
                if idx >= batch_size:
                    values = g.sample(sess, z_batch, pos_batch)
                    for ii, jj, val in zip(x_batch, y_batch, values):
                        image[ii, jj] = val
                    image_drawer.img = image
                    wnd.draw()
                    idx = 0

    imsave('D:/bgr.png', image)
    while True:
        wnd.draw()
Exemplo n.º 5
0
def explain(env: Environment, sess: tf.Session, model: RQModelBase):
    wnd = v.Window(120, 220)
    state_drawer = v.ImageDrawer(v.Rect(10, 10, 100, 200))

    wnd.add_drawer(state_drawer)

    state = env.reset()
    while True:
        r, u, q, p = model.predict_r_u_q_p(sess, [state])
        if np.random.rand() < 0.1:
            action = np.random.choice(env.num_actions)
        else:
            action = np.random.choice(env.num_actions, p=p[0])
            #action = np.argmax(q[0])
        state = env.do_action(action)
        state_drawer.img = state

        if env.is_terminal():
            state = env.reset()

        wnd.draw()
Exemplo n.º 6
0
def main(arguments):
    import deepirl.utils.vizualization as v
    import time
    from deepirl.utils.replay import StateActionReplay

    wnd = v.Window(420, 420)
    screen = v.ImageDrawer(v.Rect(10, 10, 400, 400))
    wnd.add_drawer(screen)

    env = Snake(width=10, height=10)
    state = env.reset()

    while True:
        screen.img = state
        action = np.random.choice(env.num_actions)
        state = env.do_action(action)
        wnd.draw()
        #time.sleep(0.1)

        if env.is_terminal():
            state = env.reset()
Exemplo n.º 7
0
def main2():
    import deepirl.utils.vizualization as v
    mnist = tf.contrib.learn.datasets.load_dataset("mnist")
    train_data = mnist.train.images  # Returns np.array

    batch_size = 64
    wnd = v.Window(400, 400)
    image_drawer = v.ImageDrawer(v.Rect(0, 0, wnd.width, wnd.height))
    wnd.add_drawer(image_drawer)

    with tf.Session() as sess:
        gan = GAN(28, 28, z_size=32, batch_size=batch_size)
        sess.run(tf.global_variables_initializer())
        i = 0
        while True:
            indices = np.random.choice(len(train_data), batch_size, replace=False)
            samples = np.reshape(train_data[indices], (-1, 28, 28, 1))
            gan.train(sess, samples)
            i += 1
            if i % 10 == 0:
                image_drawer.img = gan.generate_sample(sess, 64, 64)
                wnd.draw()
Exemplo n.º 8
0
def frames_from_video(path: str,
                      gaze_points: dict,
                      width: int = 128,
                      height: int = 128,
                      output_path='',
                      draw: bool = True):
    import cv2
    import deepirl.utils.vizualization as v

    # LODS:
    #   target     src at x,y location
    #   0: 32x32     32 x 32
    #   2: 32x32     64 x 64
    #   4: 32x32    128 x 128
    #   8: 32x32    256 x 256
    #  -1: 32x32    original.width x original.height
    # 512x512 is not used since it is not very different from original
    # So we will have 5 channels
    lod_scales = [1, 2, 4, 8, -1]
    frame = np.zeros((height, width, len(lod_scales)), dtype=np.uint8)

    memory = EyeTrackingReplay(height=height,
                               width=width,
                               channels=len(lod_scales))
    cap = cv2.VideoCapture(path)

    # Check if camera opened successfully
    if not cap.isOpened():
        raise RuntimeError(
            "Error opening video stream or file: {0}".format(path))

    drawer_width = 256
    wnd = v.Window(len(lod_scales) * drawer_width, 490)
    lod_drawers = []
    for i in range(len(lod_scales)):
        drawer = v.ImgPlotDrawer(v.Rect(i * drawer_width, 0, drawer_width,
                                        480))
        wnd.add_drawer(drawer)
        lod_drawers.append(drawer)
    stats_drawer = v.StringDrawer(10, 440)
    wnd.add_drawer(stats_drawer)
    frame_i = 0

    left, top, right, bottom = 717, 226, 1423, 923
    # roi width is 706
    # roi height is 697
    roi = v.Rect(left, top, right - left, bottom - top)

    while cap.isOpened():
        # Capture frame-by-frame
        ret, frame_raw = cap.read()

        if not ret:
            break

        if frame_i in gaze_points:
            x, y = gaze_points[frame_i]
            x = float(x - roi.left) / roi.width
            y = float(y - roi.top) / roi.height

            skip = False
            # Skip if x is out of area
            if x < 0.0 or x > 1.0:
                skip = True

            # Skip if y is out of area
            if y < 0.0 or y > 1.0:
                skip = True

            if not skip:
                cropped = frame_raw[roi.top:roi.bottom, roi.left:roi.right]
                cropped_bw = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)

                for i, scale in enumerate(lod_scales):
                    if scale < 0:
                        part = cropped_bw
                    else:
                        part = get_part(cropped_bw, x, y, width * scale,
                                        height * scale)
                    if scale == 1:
                        frame[..., i] = part
                    else:
                        frame[...,
                              i] = cv2.resize(part, (width, height),
                                              interpolation=cv2.INTER_AREA)

                memory.append(frame / 255.0, 2.0 * (x - 0.5), 2.0 * (y - 0.5))

                if draw:
                    for i, drawer in enumerate(lod_drawers):
                        drawer.set_value(frame[..., i])
                    stats_drawer.text = 'Frame {0}\nGaze X {1}\nGaze Y {2}'.format(
                        frame_i, x, y)
                    wnd.draw()
                else:
                    print('Frame {0}\tGaze X {1:.3f}\tGaze Y {2:.3f}'.format(
                        frame_i, x, y))
        frame_i += 1
    memory.save(output_path)
Exemplo n.º 9
0
def play(sess: tf.Session,
         replay: EyeTrackingReplay,
         model: Model,
         channels=5):
    import deepirl.utils.vizualization as v
    import cv2

    drawer_size = 128
    wnd = v.Window(drawer_size * channels, drawer_size * 3)
    prob_drawer = v.ImageDrawer(
        v.Rect(0, drawer_size, drawer_size * 2, drawer_size * 2))
    ex_prob_drawer = v.ImageDrawer(
        v.Rect(drawer_size * 2, drawer_size, drawer_size * 2, drawer_size * 2))
    stats_drawer = v.StringDrawer(drawer_size * 4 + 10, drawer_size + 10)
    wnd.add_drawer(prob_drawer)
    wnd.add_drawer(ex_prob_drawer)
    wnd.add_drawer(stats_drawer)

    output_path = 'D:\\deepirl\\xray_bc.avi'
    frame_rate = 20
    frames = 0
    video_frames_to_write = 30 * frame_rate  # 30 secs
    writer = None
    if output_path:
        print('Recording video: {0}'.format(output_path))
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        writer = cv2.VideoWriter(output_path, fourcc, frame_rate,
                                 (wnd.width, wnd.height))

    w, h = 64, 64
    ex_policy = np.zeros((h, w), dtype=np.float32)
    indices = np.zeros((h * w, 2), dtype=np.float32)
    for j, x in enumerate(np.linspace(-1, 1, w)):
        for i, y in enumerate(np.linspace(-1, 1, h)):
            indices[i * w + j, 0] = x
            indices[i * w + j, 1] = y

    drawers = []
    for i in range(channels):
        drawer = v.ImageDrawer(
            v.Rect(i * drawer_size, 0, drawer_size, drawer_size))
        drawers.append(drawer)
        wnd.add_drawer(drawer)

    while True:
        #frames, xs, ys = replay.sample(BATCH_SIZE)
        #for frame, x, y in zip(frames, xs, ys):
        for res in replay.iterate():
            frame = res['frame']
            x = res['x']
            y = res['y']
            for i, drawer in enumerate(drawers):
                drawer.set_value(frame[..., i])

            yy = np.clip((h - 1) * (y + 1.0) * 0.5, 0.0, h - 1)
            xx = np.clip((w - 1) * (x + 1.0) * 0.5, 0.0, w - 1)
            ex_policy[int(yy), int(xx)] += 1.0
            ex_policy *= 0.95
            probs, mode = model.get_prob(sess, frame, indices)
            prob_drawer.set_value(np.reshape(probs, newshape=(h, w)))
            ex_prob_drawer.set_value(ex_policy)

            pred_x = mode[0][0]
            pred_y = mode[0][1]

            mse = np.sqrt(np.square(pred_x - x) + np.square(pred_y - y))

            stats_drawer.text = 'Expert:\n X: {0:.3f}\n Y: {1:.3f}\n\nPredicted:\n X: {2:.3f}\n Y: {3:.3f}\n\nMSE: {4:.3f}'.format(
                x, y, pred_x, pred_y, mse)
            wnd.draw()

            frames += 1

            if writer is not None:
                if frames < video_frames_to_write:
                    writer.write(wnd.screen)
                if frames == video_frames_to_write:
                    print('Saving video to: {0}'.format(output_path))
                    writer.release()
Exemplo n.º 10
0
for i in range(layers):
    x = tf.layers.dense(x, num_hidden, activation=activation, kernel_initializer=tf.initializers.random_normal(stddev=stddev))
x = tf.exp(x)
if use_color:
    x = tf.layers.dense(x, 3, activation=tf.nn.sigmoid)
else:
    x = tf.reshape(tf.layers.dense(x, 1, activation=tf.nn.sigmoid), [-1])
out = x


if use_color:
    image = np.zeros((h, w, 3), dtype=np.float32)
else:
    image = np.zeros((h, w), dtype=np.float32)
wnd = v.Window(w, h)
image_drawer = v.ImageDrawer(v.Rect(0, 0, wnd.width, wnd.height))
wnd.add_drawer(image_drawer)


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    while True:
        z = np.random.standard_normal((noise_dim,)) * 10.0
        z_batch = np.zeros((batch_size, noise_dim), np.float32)
        z_batch[:] = z
        pos_batch = np.zeros((batch_size, 3), np.float32)
        x_batch = np.zeros((batch_size,), np.uint16)
        y_batch = np.zeros((batch_size,), np.uint16)

        idx = 0
Exemplo n.º 11
0
def explain(env: Environment, sess: tf.Session, model: RQModelBase, replay: StateActionReplay,
            frame_rate: float = 20.0, output_path: str=''):
    wnd = v.Window(900, 700)

    writer = None
    frames = 0
    video_frames_to_write = 60 * frame_rate  # one minute
    if output_path:
        print('Recording video: {0}'.format(output_path))
        fourcc = cv2.VideoWriter_fourcc(*'MJPG')
        writer = cv2.VideoWriter(output_path, fourcc, frame_rate, (wnd.width, wnd.height))

    w = 200
    h = 320
    padding = 20

    state1_drawer = v.ImgPlotDrawer(v.Rect(padding, padding, w, h),
                                    caption="State channel:0")
    state2_drawer = v.ImgPlotDrawer(v.Rect(state1_drawer.bounds.right + padding, padding, w, h),
                                    caption="State channel:1", color_map=v.ColorMap.HOT)
    expert_drawer = v.ImgPlotDrawer(v.Rect(state2_drawer.bounds.right + w + padding * 2, padding, w, h),
                                    caption="Expert behaviour", color_map=v.ColorMap.HOT)

    r_drawer = v.ImgPlotDrawer(v.Rect(padding, state1_drawer.bounds.bottom + padding, w, h),
                               caption="Reward R(s,a)", color_map=v.ColorMap.HOT)
    u_drawer = v.ImgPlotDrawer(v.Rect(r_drawer.bounds.right + padding, state1_drawer.bounds.bottom + padding, w, h),
                               caption="Expected return U(s,a)", color_map=v.ColorMap.HOT)
    q_drawer = v.ImgPlotDrawer(v.Rect(u_drawer.bounds.right + padding, state1_drawer.bounds.bottom + padding, w, h),
                               caption="Quality Q(s,a)=R(s,a)+U(s,a)", color_map=v.ColorMap.HOT)
    p_drawer = v.ImgPlotDrawer(v.Rect(q_drawer.bounds.right + padding, state1_drawer.bounds.bottom + padding, w, h),
                               caption="Policy p(s,a)=softmax(Q(s,a))", color_map=v.ColorMap.HOT)

    wnd.add_drawer(state1_drawer)
    wnd.add_drawer(state2_drawer)
    wnd.add_drawer(expert_drawer)
    wnd.add_drawer(r_drawer)
    wnd.add_drawer(u_drawer)
    wnd.add_drawer(q_drawer)
    wnd.add_drawer(p_drawer)

    for state, action in replay.iterate():
        state1_drawer.set_value(state[..., 0])
        state2_drawer.set_value(state[..., 1])

        r, u, q, p = model.predict_r_u_q_p(sess, [state])

        expert = np.zeros_like(p[0])
        expert[action] = 1.0
        expert_drawer.set_value(square(expert))

        r_drawer.set_value(square(r[0]))
        u_drawer.set_value(square(u[0]))
        q_drawer.set_value(square(q[0]))
        p_drawer.set_value(square(p[0]))

        wnd.draw()
        frames += 1

        if writer is not None:
            if frames < video_frames_to_write:
                writer.write(wnd.screen)
            if frames == video_frames_to_write:
                print('Saving video to: {0}'.format(output_path))
                writer.release()

        # No need to sleep. Computation take too much time already
        #time.sleep(1.0 / frame_rate)

    if writer is not None:
        writer.release()
Exemplo n.º 12
0
if __name__ == '__main__':
    import keyboard
    import time
    import sys
    import deepirl.utils.vizualization as v
    from deepirl.utils.replay import StateActionReplay

    env = Tetris()
    state = env.reset()
    replay = StateActionReplay(100000, env.state_shape)
    episode = 0
    record_episodes = 1

    wnd = v.Window(220, 420)
    state_drawer = v.ImageDrawer(v.Rect(10, 10, 200, 400), color_map=v.ColorMap.HOT)
    wnd.add_drawer(state_drawer)

    class Hack(object):
        action = None

    hack = Hack()

    def pressed(event: keyboard.KeyboardEvent, hack_ref):
        hack_ref.action = None
        if event.name == 'down':
            hack_ref.action = ACTION_NO_OP
        if event.name == 'left':
            hack_ref.action = ACTION_LEFT
        if event.name == 'right':
            hack_ref.action = ACTION_RIGHT