Exemplo n.º 1
0
    parser.add_argument('-c', '--config', type=str, default=None, help='Adusted configuration file located in config/custom folder')
    parser.print_help()
    args = parser.parse_args()
    path = pathlib.Path().absolute()

    trainer = Trainer(args.environment, args.subdir)

    if args.config is not None:
        try:
            config_path = join(path, 'rl', 'config', 'custom', '{}.yml'.format(args.config))
            with open(config_path) as f:
                config = yaml.safe_load(f)
            print('\nLoaded config file from: {}\n'.format(config_path))

        except:
            print('specified config is not in path, getting original config: {}.yml...'.format(args.environment))
            # load config and variables needed
            config = get_parameters(args.environment)
    else:
        config = get_parameters(args.environment)

    if args.model is not None:
        config['main']['model'] = args.model
    trainer.create_model(name=args.name, config_file=config)
    trainer._tensorboard()
    t0 = time.time()
    trainer.train()
    ts = time.time()
    print('Running time for training: {} minutes.'.format((ts-t0)/60))
    #trainer.run(1000)
    trainer._save()
Exemplo n.º 2
0
class InstanceManager:
    """
    Creates dirs, manages envs and instances
    """
    def __init__(self, env=None):
        self.env_name = ''
        self.env_path = None

        self.instance_name = None
        self.instance_path = None

        self.trainer = Trainer()
        self.config = ConfigManager()

        if env is not None:
            self.set_env(env)

    def set_env(self, env):
        self.env_name = env
        self.env_path = os.path.join(settings.TRAINED_MODELS, env)
        os.makedirs(self.env_path, exist_ok=True)
        config_path = self.config.load(env)
        return config_path

    def new_instance(self, namestamp=None):

        # Assign a unique numerical ID to an instance
        numerical_ids = [
            int(x.split('_')[0]) for x in os.listdir(self.env_path)
        ]
        try:
            unique_id = max(numerical_ids) + 1
        except:
            unique_id = 0

        # Check if some IDs are missing (e.g. deleted)
        for num in range(len(numerical_ids)):
            if num not in numerical_ids:
                unique_id = num
                break

        if namestamp is None:
            date = datetime.datetime.now().strftime("%m-%d_%H-%M")
            namestamp = "{}_{}_{}_{}_{}".format(
                self.env_name, self.config.model_type, self.config.policy_type,
                self.config.trainer['n_workers'], date)
        self.instance_name = str(unique_id) + '_' + namestamp
        self.instance_path = os.path.join(self.env_path, self.instance_name)
        os.makedirs(self.instance_path, exist_ok=True)
        print(self.instance_path)
        self.config = ConfigManager(env_name=self.env_name)
        self.trainer.create_model(config=self.config, path=self.instance_path)

    def load_instance(self, path=None, num=None):
        """
        Loads an instance from the specified path
        """
        if num is not None:
            subdirs = []
            for f in os.listdir(self.env_path):
                fpath = os.path.join(self.env_path, f)
                print(f)
                if os.path.isdir(fpath) and num == int(f.split('_')[0]):
                    print(fpath)
                    path = fpath
                    break

        config_path = os.path.join(path, 'config.yml')
        self.config.load_dict(self.config.load_file(config_path))
        self.trainer.load_model(path=path, config=self.config)
        self.instance_path = path
        self.instance_name = os.path.split(path)[-1]

    def save_instance(self):
        """
        Saves the current instance (model weights and config files)
        """
        # try:
        print(self.instance_path)
        if self.config.trainer.get('steps_trained') is None:
            self.config.trainer['steps_trained'] = self.trainer.steps_trained
        else:
            self.config.trainer['steps_trained'] += self.trainer.steps_trained
        self.config.save(self.instance_path)
        self.trainer.save_model()
        # except:
        #     print('Nothing to save.')

    def tensorboard(self, browser=True):
        # Kill current session
        self._tensorboard_kill()

        # Open the dir of the current env
        cmd = 'tensorboard --logdir ' + self.instance_path
        print('Launching tensorboard at {}'.format(self.instance_path))
        DEVNULL = open(os.devnull, 'wb')
        subprocess.Popen(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL)

        if browser:
            time.sleep(2)
            webbrowser.open_new_tab(
                url='http://localhost:6006/#scalars&_smoothingWeight=0.995')

    def _tensorboard_kill(self):
        """
        Destroy all running instances of tensorboard
        """
        print('Closing current session of tensorboard.')
        if sys.platform == 'win32':
            os.system("taskkill /f /im  tensorboard.exe")
        elif sys.platform == 'linux':
            os.system('pkill tensorboard')
        else:
            print('No running instances of tensorboard.')