Ejemplo n.º 1
0
def main(args):
    with open(args.conf, 'r') as f:
        config = json.load(f)

    modules = load_modules(config['models'])

    input_dir = config['preprocessing']['input-dir']
    input_file = config['preprocessing']['input-file']
    output_dir = config['preprocessing']['output-dir']
    output_file = config['preprocessing']['output-file']

    input_image_dir = os.path.join(input_dir, 'images')
    output_image_dir = os.path.join(output_dir, 'images')

    out = []
    times = []

    data = load_data(input_dir, input_file)
    n = len(data)

    for step, data_point in enumerate(data):
        start = time()
        
        res = {
            'input': [],
            'label': dict(
                (key, value) for key, value in data_point.items() if key != 'image'
            ),
        }

        img = load_image(input_image_dir, data_point['image'])

        for module_name, module in modules.items():
            x = process(
                img=img,
                model=module['model'],
                pre_fun=module['preprocessing'],
                post_fun=module['postprocessing']
            )

            if module['type'] == 'image':
                save_image(output_image_dir, data_point['image'], x)
                x = data_point['image']

            res['input'].append({
                'module_name': module_name,
                'type': module['type'],
                'value': x
            })

        out.append(res)
        times.append(time() - start)

        print_progress(step + 1, n, sum(times) / len(times), n - step - 1)

    filename = os.path.join(output_dir, output_file)
    with open(filename, 'w') as f:
        json.dump(out, f, indent=2)

    print(f'\nTotal time to convert {n} images is {timedelta(seconds=sum(times))}')
Ejemplo n.º 2
0
def drive(args):
    with open(args.conf.strip(), 'r') as f:
        config = json.load(f)

    modules = load_modules(config['models'])
    model = load_agent('imitation_learning_agent').load('lstm-2.h5')

    agent = ControlAgent(model, modules)
    experiment_suite = CoRL2017('Town01')
    run_driving_benchmark(agent, experiment_suite)
Ejemplo n.º 3
0
def main():
    config = None
    args = get_args()
    config, checkpoint = get_config_and_checkpoint(args)

    set_random_seeds(args, config)
    eval_log_dir = args.save_dir + "_eval"
    try:
        os.makedirs(args.save_dir)
        os.makedirs(eval_log_dir)
    except OSError:
        pass

    now = datetime.datetime.now()
    experiment_name = args.experiment_name + '_' + now.strftime(
        "%Y-%m-%d_%H-%M-%S")

    # Create checkpoint file
    save_dir_model = os.path.join(args.save_dir, 'model', experiment_name)
    save_dir_config = os.path.join(args.save_dir, 'config', experiment_name)
    try:
        os.makedirs(save_dir_model)
        os.makedirs(save_dir_config)
    except OSError as e:
        logger.error(e)
        exit()

    if args.config:
        shutil.copy2(args.config, save_dir_config)

    # Tensorboard Logging
    writer = SummaryWriter(
        os.path.join(args.save_dir, 'tensorboard', experiment_name))

    # Logger that writes to STDOUT and a file in the save_dir
    logger = setup_carla_logger(args.save_dir, experiment_name)

    device = torch.device("cuda:0" if args.cuda else "cpu")
    norm_reward = not config.no_reward_norm
    norm_obs = not config.no_obs_norm

    assert not (config.num_virtual_goals > 0) or (
        config.reward_class
        == 'SparseReward'), 'Cant use HER with dense reward'
    obs_converter = CarlaObservationConverter(
        h=84, w=84, rel_coord_system=config.rel_coord_system)
    action_converter = CarlaActionsConverter(config.action_type)
    envs = make_vec_envs(obs_converter,
                         action_converter,
                         args.starting_port,
                         config.seed,
                         config.num_processes,
                         config.gamma,
                         device,
                         config.reward_class,
                         num_frame_stack=1,
                         subset=config.experiments_subset,
                         norm_reward=norm_reward,
                         norm_obs=norm_obs,
                         apply_her=config.num_virtual_goals > 0,
                         video_every=args.video_interval,
                         video_dir=os.path.join(args.save_dir, 'video',
                                                experiment_name))

    if config.agent == 'forward':
        agent = agents.ForwardCarla()

    if config.agent == 'a2c':
        agent = agents.A2CCarla(obs_converter,
                                action_converter,
                                config.value_loss_coef,
                                config.entropy_coef,
                                lr=config.lr,
                                eps=config.eps,
                                alpha=config.alpha,
                                max_grad_norm=config.max_grad_norm)

    elif config.agent == 'acktr':
        agent = agents.A2CCarla(obs_converter,
                                action_converter,
                                config.value_loss_coef,
                                config.entropy_coef,
                                lr=config.lr,
                                eps=config.eps,
                                alpha=config.alpha,
                                max_grad_norm=config.max_grad_norm,
                                acktr=True)

    elif config.agent == 'ppo':
        agent = agents.PPOCarla(obs_converter,
                                action_converter,
                                config.clip_param,
                                config.ppo_epoch,
                                config.num_mini_batch,
                                config.value_loss_coef,
                                config.entropy_coef,
                                lr=config.lr,
                                eps=config.eps,
                                max_grad_norm=config.max_grad_norm)

    if checkpoint is not None:
        load_modules(agent.optimizer, agent.model, checkpoint)

    rollouts = RolloutStorage(config.num_steps, config.num_processes,
                              envs.observation_space, envs.action_space, 20,
                              config.num_virtual_goals,
                              config.rel_coord_system, obs_converter)

    obs = envs.reset()
    # Save the first observation
    obs = obs_to_dict(obs)
    rollouts.obs = obs_to_dict(rollouts.obs)
    for k in rollouts.obs:
        rollouts.obs[k][rollouts.step + 1].copy_(obs[k])
    rollouts.obs = dict_to_obs(rollouts.obs)
    rollouts.to(device)

    start = time.time()

    total_steps = 0
    total_episodes = 0
    total_reward = 0

    episode_reward = torch.zeros(config.num_processes)

    for j in range(config.num_updates):

        for step in range(config.num_steps):
            # Sample actions
            with torch.no_grad():
                value, action, action_log_prob, recurrent_hidden_states = agent.act(
                    rollouts.get_obs(step),
                    rollouts.recurrent_hidden_states[step],
                    rollouts.masks[step])

            # Observe reward and next obs
            obs, reward, done, info = envs.step(action)

            # For logging purposes
            carla_rewards = torch.tensor([i['carla-reward'] for i in info],
                                         dtype=torch.float)
            episode_reward += carla_rewards
            total_reward += carla_rewards.sum().item()
            total_steps += config.num_processes

            if done.any():
                total_episodes += done.sum()
                torch_done = torch.tensor(done.astype(int)).byte()
                mean_episode_reward = episode_reward[torch_done].mean().item()
                logger.info('{} episode(s) finished with reward {}'.format(
                    done.sum(), mean_episode_reward))
                writer.add_scalar('train/mean_ep_reward_vs_steps',
                                  mean_episode_reward, total_steps)
                writer.add_scalar('train/mean_ep_reward_vs_episodes',
                                  mean_episode_reward, total_episodes)
                episode_reward[torch_done] = 0

            # If done then clean the history of observations.
            masks = torch.FloatTensor(1 - done)

            rollouts.insert(obs, recurrent_hidden_states,
                            action, action_log_prob, value, reward,
                            masks.unsqueeze(-1))

        if config.num_virtual_goals > 0:
            rollouts.apply_her(config.num_virtual_goals,
                               device,
                               beta=config.beta)

        with torch.no_grad():
            next_value = agent.get_value(
                rollouts.get_obs(-1),  # Get last observation
                rollouts.recurrent_hidden_states[-1],
                rollouts.masks[-1]).detach()

        rollouts.compute_returns(next_value, config.use_gae, config.gamma,
                                 config.tau)

        value_loss, action_loss, dist_entropy = agent.update(rollouts)

        rollouts.after_update()

        if j % args.save_interval == 0 and args.save_dir != "" and config.agent != 'forward':
            save_path = os.path.join(save_dir_model, str(j) + '.pth.tar')
            save_modules(agent.optimizer, agent.model, args, config, save_path)

        total_num_steps = (j + 1) * config.num_processes * config.num_steps

        if j % args.log_interval == 0:

            # Logging to the stdout/our logs
            end = time.time()
            logger.info('------------------------------------')
            logger.info('Episodes {}, Updates {}, num timesteps {}, FPS {}'\
                .format(total_episodes, j + 1, total_num_steps, total_num_steps / (end - start)))
            logger.info('------------------------------------')

            # Logging to tensorboard
            writer.add_scalar('train/cum_reward_vs_steps', total_reward,
                              total_steps)
            writer.add_scalar('train/cum_reward_vs_updates', total_reward,
                              j + 1)

            if config.agent in ['a2c', 'acktr', 'ppo']:
                writer.add_scalar('debug/value_loss_vs_steps', value_loss,
                                  total_steps)
                writer.add_scalar('debug/value_loss_vs_updates', value_loss,
                                  j + 1)
                writer.add_scalar('debug/action_loss_vs_steps', action_loss,
                                  total_steps)
                writer.add_scalar('debug/action_loss_vs_updates', action_loss,
                                  j + 1)
                writer.add_scalar('debug/dist_entropy_vs_steps', dist_entropy,
                                  total_steps)
                writer.add_scalar('debug/dist_entropy_vs_updates',
                                  dist_entropy, j + 1)

            # Sample the last reward
            writer.add_scalar('debug/sampled_normalized_reward_vs_steps',
                              reward.mean(), total_steps)
            writer.add_scalar('debug/sampled_normalized_reward_vs_updates',
                              reward.mean(), j + 1)
            writer.add_scalar('debug/sampled_carla_reward_vs_steps',
                              carla_rewards.mean(), total_steps)
            writer.add_scalar('debug/sampled_carla_reward_vs_updates',
                              carla_rewards.mean(), j + 1)

        if (args.eval_interval is not None and j % args.eval_interval == 0):
            eval_envs = make_vec_envs(args.env_name, args.starting_port,
                                      obs_converter,
                                      args.x + config.num_processes,
                                      config.num_processes, config.gamma,
                                      eval_log_dir, config.add_timestep,
                                      device, True)

            vec_norm = get_vec_normalize(eval_envs)
            if vec_norm is not None:
                vec_norm.ob_rms = get_vec_normalize(envs).ob_rms

            eval_episode_rewards = []

            obs = eval_envs.reset()
            eval_recurrent_hidden_states = torch.zeros(config.num_processes,
                                                       20,
                                                       device=device)
            eval_masks = torch.zeros(config.num_processes, 1, device=device)

            while len(eval_episode_rewards) < 10:
                with torch.no_grad():
                    _, action, _, eval_recurrent_hidden_states = agent.act(
                        obs,
                        eval_recurrent_hidden_states,
                        eval_masks,
                        deterministic=True)

                # Obser reward and next obs
                carla_obs, reward, done, infos = eval_envs.step(action)

                eval_masks = torch.FloatTensor([[0.0] if done_ else [1.0]
                                                for done_ in done])
                for info in infos:
                    if 'episode' in info.keys():
                        eval_episode_rewards.append(info['episode']['r'])

            eval_envs.close()

            logger.info(
                " Evaluation using {} episodes: mean reward {:.5f}\n".format(
                    len(eval_episode_rewards), np.mean(eval_episode_rewards)))
Ejemplo n.º 4
0
        agent = agents.PPOCarla(obs_converter,
                                action_converter,
                                config.clip_param,
                                config.ppo_epoch,
                                config.num_mini_batch,
                                config.value_loss_coef,
                                config.entropy_coef,
                                lr=config.lr,
                                eps=config.eps,
                                max_grad_norm=config.max_grad_norm)

    else:
        raise NotImplementedError

    if checkpoint is not None:
        load_modules(agent.optimizer, agent.model, checkpoint)

    vec_norm = get_vec_normalize(envs)
    #if vec_norm is not None:
    #    vec_norm.ob_rms = get_vec_normalize(envs).ob_rms

    metrics_object = Metrics(experiment_suite.metrics_parameters,
                             experiment_suite.dynamic_tasks)
    recording = Recording(name_to_save=args.save_dir,
                          continue_experiment=False,
                          save_images=True)

    logging.info('START')
    iter = True
    obs = None
    while iter:
Ejemplo n.º 5
0
 def initialize(self):
     """
     Called when the WebSocket is instantiated, sets up our WebSocket
     actions, security policies, and attaches all of our plugin hooks/events.
     """
     example_log.debug("ExampleApplication.initialize()")
     # Register our security policy function in the 'security' dict
     self.ws.security.update({'example': example_policies})
     # Register some WebSocket actions...
     # These can be called from the client like so:
     # GateOne.ws.send(JSON.stringify({'example:test_example': whatever}));
     self.ws.actions.update({
         'example:test_example': self.test_example,
     })
     # Gate One provides a location where you can store information that you
     # want to be persistent across user sesions/connections and whatnot:
     if 'example' not in self.ws.persist:
         # If it doesn't belong in SESSIONS but you still need it to stick
         # around after the user disconnects put it here:
         self.ws.persist['example'] = {}
     # NOTE: If you don't care about your app having its own plugins you can
     # delete everything from here until the end of this function.
     # -- BEGIN PLUGIN CODE --
     # If you want your app to support plugins here's how you do it:
     # First let's check if someone has explicitly given us a list of plugins
     # that should be enabled (so we can exclude the others):
     enabled_plugins = self.ws.prefs['*']['example'].get(
         'enabled_plugins', [])
     # Now we'll use Gate One's utils.get_plugins() function to fetch a dict
     # containing all our Python, JavaScript, and CSS plugin files.  This is
     # really only so we can log which plugins are enabled because the
     # process of importing Python plugins happens inside of init() and
     # JS/CSS plugin files get sent via the send_plugin_static_files()
     # function inside of authenticate().
     self.plugins = get_plugins(  # Get everything in example/plugins/
         os.path.join(APPLICATION_PATH, 'plugins'), enabled_plugins)
     # Now let's separate the plugins by type (to save some typing)
     js_plugins = []
     for js_path in self.plugins['js']:
         name = js_path.split(os.path.sep)[-2]
         name = os.path.splitext(name)[0]
         js_plugins.append(name)
     css_plugins = []
     for css_path in css_plugins:
         name = css_path.split(os.path.sep)[-2]
         css_plugins.append(name)
     plugin_list = list(set(self.plugins['py'] + js_plugins + css_plugins))
     plugin_list.sort()  # So there's consistent ordering
     example_log.info(
         _("Active Example Plugins: %s" % ", ".join(plugin_list)))
     # Now let's attach plugin hooks.  Plugin hooks can be whatever you like
     # and called from anywhere in your application.  There's three types of
     # hooks you'll definitely want though:  initialize(), 'WebSocket' and
     # 'Events'
     #
     # The initialize() hook just calls a given plugin's "initializ()"
     # function if it has one.  The function will be passed `self` (the
     # current instance of your app).  This allows plugins to take care of
     # any initialization stuff that needs to happen before anything else.
     #
     # 'WebSocket' hooks are what allow plugins to add their own WebSocket
     # actions such as, "myplugin:do_something" which is a very important
     # part of Gate One.
     #
     # 'Events' hooks allow plugins to attach functions to `OnOff` events
     # such as 'self.on("example:some_event", handle_some_event)'
     #
     # With those three kinds of hooks plugins can add or override pretty
     # much anything.
     #
     # NOTE: All GOApplication instances include the OnOff mixin class so
     # they can use self.on(), self.off, self.trigger(), and self.once()
     #
     # How do plugins assign these hooks?  They simply include a 'hooks' dict
     # somewhere in the global scope of their .py files.  Example:
     # hooks = {
     #     'WebSocket': {'myplugin:some_func': some_func}
     #     'Events': {'example:authenticate': auth_func}
     # }
     self.plugin_hooks = {}  # We'll store our plugin hooks here
     imported = load_modules(self.plugins['py'])
     for plugin in imported:
         try:
             # Add the plugin's hooks dict to self.plugin_hooks:
             self.plugin_hooks.update({plugin.__name__: plugin.hooks})
             # Now we'll call the plugin's initialize() function:
             if hasattr(plugin, 'initialize'):
                 plugin.initialize(self)
         except AttributeError:
             pass  # No hooks--probably just a supporting .py file.
     # Now we hook up the hooks:
     for plugin_name, hooks in self.plugin_hooks.items():
         if 'WebSocket' in hooks:
             # Apply the plugin's WebSocket actions to ApplicationWebSocket
             for ws_command, func in hooks['WebSocket'].items():
                 self.ws.actions.update({ws_command: bind(func, self)})
             # Attach the plugin's event hooks to their respective events:
         if 'Events' in hooks:
             for event, callback in hooks['Events'].items():
                 self.on(event, bind(callback, self))
Ejemplo n.º 6
0
 def initialize(self):
     """
     Called when the WebSocket is instantiated, sets up our WebSocket
     actions, security policies, and attaches all of our plugin hooks/events.
     """
     example_log.debug("ExampleApplication.initialize()")
     # Register our security policy function in the 'security' dict
     self.ws.security.update({'example': example_policies})
     # Register some WebSocket actions...
     # These can be called from the client like so:
     # GateOne.ws.send(JSON.stringify({'example:test_example': whatever}));
     self.ws.actions.update({
         'example:test_example': self.test_example,
     })
     # Gate One provides a location where you can store information that you
     # want to be persistent across user sesions/connections and whatnot:
     if 'example' not in self.ws.persist:
         # If it doesn't belong in SESSIONS but you still need it to stick
         # around after the user disconnects put it here:
         self.ws.persist['example'] = {}
     # NOTE: If you don't care about your app having its own plugins you can
     # delete everything from here until the end of this function.
     # -- BEGIN PLUGIN CODE --
     # If you want your app to support plugins here's how you do it:
     # First let's check if someone has explicitly given us a list of plugins
     # that should be enabled (so we can exclude the others):
     enabled_plugins = self.ws.prefs['*']['example'].get(
         'enabled_plugins', [])
     # Now we'll use Gate One's utils.get_plugins() function to fetch a dict
     # containing all our Python, JavaScript, and CSS plugin files.  This is
     # really only so we can log which plugins are enabled because the
     # process of importing Python plugins happens inside of init() and
     # JS/CSS plugin files get sent via the send_plugin_static_files()
     # function inside of authenticate().
     self.plugins = get_plugins( # Get everything in example/plugins/
         os.path.join(APPLICATION_PATH, 'plugins'), enabled_plugins)
     # Now let's separate the plugins by type (to save some typing)
     js_plugins = []
     for js_path in self.plugins['js']:
         name = js_path.split(os.path.sep)[-1].split('.')[0]
         name = os.path.splitext(name)[0]
         js_plugins.append(name)
     css_plugins = []
     for css_path in css_plugins:
         name = css_path.split(os.path.sep)[-1].split('.')[0]
         name = os.path.splitext(name)[0]
         css_plugins.append(name)
     plugin_list = list(set(self.plugins['py'] + js_plugins + css_plugins))
     plugin_list.sort() # So there's consistent ordering
     example_log.info(_(
         "Active Example Plugins: %s" % ", ".join(plugin_list)))
     # Now let's attach plugin hooks.  Plugin hooks can be whatever you like
     # and called from anywhere in your application.  There's three types of
     # hooks you'll definitely want though:  initialize(), 'WebSocket' and
     # 'Events'
     #
     # The initialize() hook just calls a given plugin's "initializ()"
     # function if it has one.  The function will be passed `self` (the
     # current instance of your app).  This allows plugins to take care of
     # any initialization stuff that needs to happen before anything else.
     #
     # 'WebSocket' hooks are what allow plugins to add their own WebSocket
     # actions such as, "myplugin:do_something" which is a very important
     # part of Gate One.
     #
     # 'Events' hooks allow plugins to attach functions to `OnOff` events
     # such as 'self.on("example:some_event", handle_some_event)'
     #
     # With those three kinds of hooks plugins can add or override pretty
     # much anything.
     #
     # NOTE: All GOApplication instances include the OnOff mixin class so
     # they can use self.on(), self.off, self.trigger(), and self.once()
     #
     # How do plugins assign these hooks?  They simply include a 'hooks' dict
     # somewhere in the global scope of their .py files.  Example:
     # hooks = {
     #     'WebSocket': {'myplugin:some_func': some_func}
     #     'Events': {'example:authenticate': auth_func}
     # }
     self.plugin_hooks = {} # We'll store our plugin hooks here
     imported = load_modules(self.plugins['py'])
     for plugin in imported:
         try:
             # Add the plugin's hooks dict to self.plugin_hooks:
             self.plugin_hooks.update({plugin.__name__: plugin.hooks})
             # Now we'll call the plugin's initialize() function:
             if hasattr(plugin, 'initialize'):
                 plugin.initialize(self)
         except AttributeError:
             pass # No hooks--probably just a supporting .py file.
     # Now we hook up the hooks:
     for plugin_name, hooks in self.plugin_hooks.items():
         if 'WebSocket' in hooks:
             # Apply the plugin's WebSocket actions to ApplicationWebSocket
             for ws_command, func in hooks['WebSocket'].items():
                 self.ws.actions.update({ws_command: bind(func, self)})
             # Attach the plugin's event hooks to their respective events:
         if 'Events' in hooks:
             for event, callback in hooks['Events'].items():
                 self.on(event, bind(callback, self))
Ejemplo n.º 7
0
import os

from database.models import Base
from db import ENGINE
from utils import load_modules

load_modules(os.path.join(os.path.dirname(__file__), "database"), "database")
Base.metadata.create_all(ENGINE)