Пример #1
0
def main():
    print(camera_config.rigs['baseline_rigs'])
    env = dd.start(cameras=camera_config.rigs['three_cam_rig'][0], use_sim_start_command=False, render=False, fps=8)
    forward = dd.action(throttle=1, steering=0, brake=0)
    done = False
    while not done:
        observation, reward, done, info = env.step(forward)
        if observation is not None:
            image = observation['cameras'][0]['image']
            cv2.imshow('image', image)
            image1 = observation['cameras'][1]['image']
            cv2.imshow('image1', image1)
            image2 = observation['cameras'][2]['image']
            cv2.imshow('image2', image2)
            print('show')
            cv2.waitKey(1)
Пример #2
0
def run(experiment,
        env_id='DeepDrivePreproTensorflow-v0',
        should_record=False,
        net_path=None,
        should_benchmark=True,
        run_baseline_agent=False,
        camera_rigs=None,
        should_rotate_sim_types=False,
        should_record_recovery_from_random_actions=False,
        render=False,
        path_follower=False,
        fps=c.DEFAULT_FPS):
    if run_baseline_agent:
        net_path = ensure_baseline_weights(net_path)
    reward = 0
    episode_done = False
    max_episodes = 1000
    tf_config = tf.ConfigProto(
        gpu_options=tf.GPUOptions(
            per_process_gpu_memory_fraction=0.8,
            # leave room for the game,
            # NOTE: debugging python, i.e. with PyCharm can cause OOM errors, where running will not
            allow_growth=True), )
    sess = tf.Session(config=tf_config)
    if camera_rigs:
        cameras = camera_rigs[0]
    else:
        cameras = None

    if should_record and camera_rigs is not None and len(camera_rigs) >= 1:
        should_rotate_camera_rigs = True
    else:
        should_rotate_camera_rigs = False

    if should_rotate_camera_rigs:
        randomize_cameras(cameras)

    use_sim_start_command_first_lap = c.SIM_START_COMMAND is not None
    gym_env = deepdrive.start(
        experiment,
        env_id,
        should_benchmark=should_benchmark,
        cameras=cameras,
        use_sim_start_command=use_sim_start_command_first_lap,
        render=render,
        fps=fps)
    dd_env = gym_env.env

    # Perform random actions to reduce sampling error in the recorded dataset
    agent = Agent(gym_env.action_space,
                  sess,
                  env=gym_env.env,
                  should_record_recovery_from_random_actions=
                  should_record_recovery_from_random_actions,
                  should_record=should_record,
                  net_path=net_path,
                  random_action_count=4,
                  non_random_action_count=5,
                  path_follower=path_follower)
    if net_path:
        log.info('Running tensorflow agent checkpoint: %s', net_path)

    def close():
        gym_env.close()
        agent.close()

    session_done = False
    episode = 0
    try:
        while not session_done:
            if episode_done:
                obz = gym_env.reset()
                episode_done = False
            else:
                obz = None
            while not episode_done:
                action = agent.act(obz, reward, episode_done)
                obz, reward, episode_done, _ = gym_env.step(action)
                if render:
                    gym_env.render()
                if should_record_recovery_from_random_actions:
                    agent.set_random_action_repeat_count()
                if agent.recorded_obz_count > c.MAX_RECORDED_OBSERVATIONS:
                    session_done = True
                elif should_benchmark and dd_env.done_benchmarking:
                    session_done = True
            if session_done:
                log.info('Session done')
            else:
                log.info('Episode done, rotating camera')
                episode += 1
                if should_rotate_camera_rigs:
                    cameras = camera_rigs[episode % len(camera_rigs)]
                    randomize_cameras(cameras)
                    dd_env.change_viewpoint(
                        cameras,
                        use_sim_start_command=random_use_sim_start_command(
                            should_rotate_sim_types))
                if episode >= max_episodes:
                    session_done = True
    except KeyboardInterrupt:
        log.info('keyboard interrupt detected, closing')
        close()
    close()
Пример #3
0
def main():
    parser = argparse.ArgumentParser(description=None)
    parser.add_argument('-e',
                        '--env-id',
                        nargs='?',
                        default='DeepDrive-v0',
                        help='Select the environment to run')
    parser.add_argument(
        '-r',
        '--record',
        action='store_true',
        default=False,
        help='Records game driving, including recovering from random actions')
    parser.add_argument('--baseline',
                        action='store_true',
                        default=False,
                        help='Runs pretrained imitation learning based agent')
    parser.add_argument('-t',
                        '--train',
                        action='store_true',
                        default=False,
                        help='Trains tensorflow agent on stored driving data')
    parser.add_argument('--use-last-model',
                        action='store_true',
                        default=False,
                        help='Run the most recently trained model')
    parser.add_argument('--recording-dir',
                        nargs='?',
                        default=c.RECORDING_DIR,
                        help='Where to store and read recorded '
                        'environment data from')
    parser.add_argument(
        '--render',
        action='store_true',
        default=False,
        help=
        'SLOW: render of camera data in Python - Use Unreal for real time camera rendering'
    )
    parser.add_argument(
        '--record-recovery-from-random-actions',
        action='store_true',
        default=False,
        help=
        'Whether to occasionally perform random actions and record recovery from them'
    )
    parser.add_argument('--path-follower',
                        action='store_true',
                        default=False,
                        help='Whether to let the in-game path follower drive')
    parser.add_argument(
        '--net-path',
        nargs='?',
        default=None,
        help='Path to the tensorflow checkpoint you want to test drive. '
        'i.e. /home/a/DeepDrive/tensorflow/2018-01-01__11-11-11AM_train/model.ckpt-98331'
    )
    parser.add_argument(
        '--resume-train',
        nargs='?',
        default=None,
        help='Path to the tensorflow training session you want to resume, '
        'i.e. /home/a/DeepDrive/tensorflow/2018-01-01__11-11-11AM_train')
    parser.add_argument('-v',
                        '--verbose',
                        help='Increase output verbosity',
                        action='store_true')
    parser.add_argument('--camera-rigs',
                        nargs='?',
                        default=None,
                        help='Name of camera rigs to use')
    parser.add_argument('-n',
                        '--experiment-name',
                        nargs='?',
                        default=None,
                        help='Name of your experiment')
    parser.add_argument('--fps',
                        type=int,
                        default=c.DEFAULT_FPS,
                        help='Frames / steps per second')

    args = parser.parse_args()
    if args.verbose:
        logs.set_level(logging.DEBUG)

    if args.camera_rigs:
        camera_rigs = camera_config.rigs[args.camera_rigs]
    else:
        camera_rigs = camera_config.rigs['baseline_rigs']

    if args.use_last_model:
        if args.train:
            args.resume_train = get_latest_model()
        else:
            args.net_path = get_latest_model()

    if args.train:
        from tensorflow_agent.train import train
        # TODO: Add experiment name here as well, and integrate it into Tensorflow runs, recording names, model checkpoints, etc...
        train.run(resume_dir=args.resume_train,
                  recording_dir=args.recording_dir)
    elif args.path_follower:
        done = False
        render = False
        episode_count = 1
        gym_env = None
        try:
            gym_env = deepdrive.start(args.experiment_name,
                                      args.env_id,
                                      fps=args.fps)
            log.info('Path follower drive mode')
            for episode in range(episode_count):
                if done:
                    gym_env.reset()
                while True:
                    action = deepdrive.action(has_control=False)
                    obz, reward, done, _ = gym_env.step(action)
                    if render:
                        gym_env.render()
                    if done:
                        gym_env.reset()
        except KeyboardInterrupt:
            log.info('keyboard interrupt detected, closing')
        except Exception as e:
            log.error('Error running agent. %s', e)
            if gym_env:
                gym_env.close()
            raise e
        if gym_env:
            gym_env.close()
        log.info('Last episode complete, closing')
    else:
        from tensorflow_agent import agent
        if args.record and not args.record_recovery_from_random_actions:
            args.path_follower = True
        agent.run(args.experiment_name,
                  should_record=args.record,
                  net_path=args.net_path,
                  env_id=args.env_id,
                  run_baseline_agent=args.baseline,
                  render=args.render,
                  camera_rigs=camera_rigs,
                  should_record_recovery_from_random_actions=args.
                  record_recovery_from_random_actions,
                  path_follower=args.path_follower,
                  fps=args.fps)
Пример #4
0
def main():
    env = dd.start()
    forward = dd.action(throttle=1, steering=0, brake=0)
    done = False
    while not done:
        observation, reward, done, info = env.step(forward)